Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/uno/Sequence.h>
21 : #include <com/sun/star/uno/Exception.hpp>
22 : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
23 : #include <com/sun/star/ucb/XContentIdentifier.hpp>
24 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
25 : #include <com/sun/star/ucb/TransferInfo.hpp>
26 : #include <com/sun/star/ucb/NameClash.hpp>
27 : #include <com/sun/star/sdbc/XResultSet.hpp>
28 : #include <com/sun/star/sdbc/XRow.hpp>
29 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 : #include <comphelper/processfactory.hxx>
31 : #include <comphelper/types.hxx>
32 : #include <tools/urlobj.hxx>
33 : #include <tools/datetime.hxx>
34 : #include <rtl/ustring.hxx>
35 : #include <ucbhelper/contentidentifier.hxx>
36 : #include <ucbhelper/content.hxx>
37 : #include <swunohelper.hxx>
38 :
39 : namespace SWUnoHelper {
40 :
41 0 : sal_Int32 GetEnumAsInt32( const ::com::sun::star::uno::Any& rVal )
42 : {
43 : sal_Int32 eVal;
44 : try
45 : {
46 0 : eVal = comphelper::getEnumAsINT32( rVal );
47 : }
48 0 : catch( ::com::sun::star::uno::Exception & )
49 : {
50 0 : eVal = 0;
51 : OSL_FAIL( "can't get EnumAsInt32" );
52 : }
53 0 : return eVal;
54 : }
55 :
56 : // methods for UCB actions
57 0 : sal_Bool UCB_DeleteFile( const OUString& rURL )
58 : {
59 : sal_Bool bRemoved;
60 : try
61 : {
62 : ucbhelper::Content aTempContent( rURL,
63 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
64 0 : comphelper::getProcessComponentContext() );
65 : aTempContent.executeCommand(OUString("delete"),
66 0 : ::com::sun::star::uno::makeAny( sal_True ) );
67 0 : bRemoved = sal_True;
68 : }
69 0 : catch( ::com::sun::star::uno::Exception& )
70 : {
71 0 : bRemoved = sal_False;
72 : OSL_FAIL( "Exeception from executeCommand( delete )" );
73 : }
74 0 : return bRemoved;
75 : }
76 :
77 0 : sal_Bool UCB_CopyFile( const OUString& rURL, const OUString& rNewURL, sal_Bool bCopyIsMove )
78 : {
79 0 : sal_Bool bCopyCompleted = sal_True;
80 : try
81 : {
82 0 : INetURLObject aURL( rNewURL );
83 0 : OUString sName( aURL.GetName() );
84 0 : aURL.removeSegment();
85 0 : OUString sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
86 :
87 : ucbhelper::Content aTempContent( sMainURL,
88 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
89 0 : comphelper::getProcessComponentContext() );
90 :
91 0 : ::com::sun::star::uno::Any aAny;
92 0 : ::com::sun::star::ucb::TransferInfo aInfo;
93 0 : aInfo.NameClash = ::com::sun::star::ucb::NameClash::ERROR;
94 0 : aInfo.NewTitle = sName;
95 0 : aInfo.SourceURL = rURL;
96 0 : aInfo.MoveData = bCopyIsMove;
97 0 : aAny <<= aInfo;
98 : aTempContent.executeCommand( OUString("transfer"),
99 0 : aAny );
100 : }
101 0 : catch( ::com::sun::star::uno::Exception& )
102 : {
103 : OSL_FAIL( "Exeception from executeCommand( transfer )" );
104 0 : bCopyCompleted = sal_False;
105 : }
106 0 : return bCopyCompleted;
107 : }
108 :
109 0 : sal_Bool UCB_IsCaseSensitiveFileName( const OUString& rURL )
110 : {
111 : sal_Bool bCaseSensitive;
112 : try
113 : {
114 0 : INetURLObject aTempObj( rURL );
115 0 : aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
116 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef1 = new
117 0 : ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
118 :
119 0 : aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
120 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef2 = new
121 0 : ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
122 :
123 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XUniversalContentBroker > xUcb =
124 0 : com::sun::star::ucb::UniversalContentBroker::create(comphelper::getProcessComponentContext());
125 :
126 0 : sal_Int32 nCompare = xUcb->compareContentIds( xRef1, xRef2 );
127 0 : bCaseSensitive = 0 != nCompare;
128 : }
129 0 : catch( ::com::sun::star::uno::Exception& )
130 : {
131 0 : bCaseSensitive = sal_False;
132 : OSL_FAIL( "Exeception from compareContentIds()" );
133 : }
134 0 : return bCaseSensitive;
135 : }
136 :
137 0 : sal_Bool UCB_IsReadOnlyFileName( const OUString& rURL )
138 : {
139 0 : sal_Bool bIsReadOnly = sal_False;
140 : try
141 : {
142 0 : ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
143 : ::com::sun::star::uno::Any aAny = aCnt.getPropertyValue(
144 0 : OUString("IsReadOnly"));
145 0 : if(aAny.hasValue())
146 0 : bIsReadOnly = *(sal_Bool*)aAny.getValue();
147 : }
148 0 : catch( ::com::sun::star::uno::Exception& )
149 : {
150 0 : bIsReadOnly = sal_False;
151 : }
152 0 : return bIsReadOnly;
153 : }
154 :
155 0 : sal_Bool UCB_IsFile( const OUString& rURL )
156 : {
157 0 : sal_Bool bExists = sal_False;
158 : try
159 : {
160 0 : ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
161 0 : bExists = aContent.isDocument();
162 : }
163 0 : catch (::com::sun::star::uno::Exception &)
164 : {
165 : }
166 0 : return bExists;
167 : }
168 :
169 0 : sal_Bool UCB_IsDirectory( const OUString& rURL )
170 : {
171 0 : sal_Bool bExists = sal_False;
172 : try
173 : {
174 0 : ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
175 0 : bExists = aContent.isFolder();
176 : }
177 0 : catch (::com::sun::star::uno::Exception &)
178 : {
179 : }
180 0 : return bExists;
181 : }
182 :
183 : // get a list of files from the folder of the URL
184 : // options: pExtension = 0 -> all, else this specific extension
185 : // pDateTime != 0 -> returns also the modified date/time of
186 : // the files in a std::vector<OUString> -->
187 : // !! objects must be deleted from the caller!!
188 0 : bool UCB_GetFileListOfFolder( const OUString& rURL,
189 : std::vector<OUString>& rList,
190 : const OUString* pExtension,
191 : std::vector< ::DateTime* >* pDateTimeList )
192 : {
193 0 : bool bOk = false;
194 : try
195 : {
196 0 : ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
197 0 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
198 :
199 0 : sal_uInt16 nSeqSize = pDateTimeList ? 2 : 1;
200 0 : ::com::sun::star::uno::Sequence < OUString > aProps( nSeqSize );
201 0 : OUString* pProps = aProps.getArray();
202 0 : pProps[ 0 ] = "Title";
203 0 : if( pDateTimeList )
204 0 : pProps[ 1 ] = "DateModified";
205 :
206 : try
207 : {
208 0 : xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
209 : }
210 0 : catch( ::com::sun::star::uno::Exception& )
211 : {
212 : OSL_FAIL( "create cursor failed!" );
213 : }
214 :
215 0 : if( xResultSet.is() )
216 : {
217 0 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow > xRow( xResultSet, ::com::sun::star::uno::UNO_QUERY );
218 0 : const sal_Int32 nExtLen = pExtension ? pExtension->getLength() : 0;
219 : try
220 : {
221 0 : if( xResultSet->first() )
222 : {
223 0 : do {
224 0 : const OUString sTitle( xRow->getString( 1 ) );
225 0 : if( !nExtLen ||
226 0 : ( sTitle.getLength() > nExtLen &&
227 0 : sTitle.endsWith( *pExtension )) )
228 : {
229 0 : rList.push_back( sTitle );
230 :
231 0 : if( pDateTimeList )
232 : {
233 0 : ::com::sun::star::util::DateTime aStamp = xRow->getTimestamp(2);
234 : ::DateTime* pDateTime = new ::DateTime(
235 : ::Date( aStamp.Day,
236 : aStamp.Month,
237 : aStamp.Year ),
238 : ::Time( aStamp.Hours,
239 : aStamp.Minutes,
240 : aStamp.Seconds,
241 0 : aStamp.NanoSeconds ));
242 0 : pDateTimeList->push_back( pDateTime );
243 : }
244 0 : }
245 :
246 0 : } while( xResultSet->next() );
247 : }
248 0 : bOk = true;
249 : }
250 0 : catch( ::com::sun::star::uno::Exception& )
251 : {
252 : OSL_FAIL( "Exception caught!" );
253 0 : }
254 0 : }
255 : }
256 0 : catch( ::com::sun::star::uno::Exception& )
257 : {
258 : OSL_FAIL( "Exception caught!" );
259 0 : bOk = false;
260 : }
261 0 : return bOk;
262 : }
263 :
264 : }
265 :
266 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|