1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #define _SVSTDARR_STRINGS
28 #include <com/sun/star/uno/Sequence.h>
29 #include <com/sun/star/uno/Exception.hpp>
30 #include <com/sun/star/ucb/XContentIdentifier.hpp>
31 #include <com/sun/star/ucb/XContentProvider.hpp>
32 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
33 #include <com/sun/star/ucb/TransferInfo.hpp>
34 #ifndef _COM_SUN_STAR_UCB_NAMECLASH_HDL_
35 #include <com/sun/star/ucb/NameClash.hdl>
36 #endif
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/XRow.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <comphelper/processfactory.hxx>
41 #include <comphelper/types.hxx>
42 #include <tools/urlobj.hxx>
43 #include <tools/datetime.hxx>
44 #include <tools/debug.hxx>
45 #include <ucbhelper/contentidentifier.hxx>
46 #include <ucbhelper/contentbroker.hxx>
47 #include <ucbhelper/content.hxx>
48 #include <svl/svstdarr.hxx>
49 #include <swunohelper.hxx>
50 #include <swunodef.hxx>
51 #include <errhdl.hxx>
52 
53 namespace SWUnoHelper {
54 
GetEnumAsInt32(const UNO_NMSPC::Any & rVal)55 sal_Int32 GetEnumAsInt32( const UNO_NMSPC::Any& rVal )
56 {
57 	sal_Int32 eVal;
58 	try
59 	{
60 		eVal = comphelper::getEnumAsINT32( rVal );
61 	}
62 	catch( UNO_NMSPC::Exception & )
63 	{
64 		eVal = 0;
65 		ASSERT( sal_False, "can't get EnumAsInt32" );
66 	}
67 	return eVal;
68 }
69 
70 
71 // methods for UCB actions
UCB_DeleteFile(const String & rURL)72 sal_Bool UCB_DeleteFile( const String& rURL )
73 {
74 	sal_Bool bRemoved;
75 	try
76     {
77 		ucbhelper::Content aTempContent( rURL,
78 								STAR_REFERENCE( ucb::XCommandEnvironment )());
79 		aTempContent.executeCommand(
80 						rtl::OUString::createFromAscii( "delete" ),
81 						UNO_NMSPC::makeAny( sal_Bool( sal_True ) ) );
82 		bRemoved = sal_True;
83     }
84     catch( UNO_NMSPC::Exception& )
85     {
86 		bRemoved = sal_False;
87 		ASSERT( sal_False, "Exeception from executeCommand( delete )" );
88     }
89 	return bRemoved;
90 }
91 
UCB_CopyFile(const String & rURL,const String & rNewURL,sal_Bool bCopyIsMove)92 sal_Bool UCB_CopyFile( const String& rURL, const String& rNewURL, sal_Bool bCopyIsMove )
93 {
94 	sal_Bool bCopyCompleted = sal_True;
95 	try
96 	{
97 		INetURLObject aURL( rNewURL );
98 		String sName( aURL.GetName() );
99 		aURL.removeSegment();
100 		String sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
101 
102 		ucbhelper::Content aTempContent( sMainURL,
103 								STAR_REFERENCE( ucb::XCommandEnvironment )());
104 
105 		UNO_NMSPC::Any aAny;
106 		STAR_NMSPC::ucb::TransferInfo aInfo;
107 		aInfo.NameClash = STAR_NMSPC::ucb::NameClash::ERROR;
108 		aInfo.NewTitle = sName;
109 		aInfo.SourceURL = rURL;
110 		aInfo.MoveData = bCopyIsMove;
111 		aAny <<= aInfo;
112 		aTempContent.executeCommand(
113 							rtl::OUString::createFromAscii( "transfer" ),
114                         	aAny );
115 	}
116 	catch( UNO_NMSPC::Exception& )
117 	{
118 		ASSERT( sal_False, "Exeception from executeCommand( transfer )" );
119     	bCopyCompleted = sal_False;
120 	}
121 	return bCopyCompleted;
122 }
123 
UCB_IsCaseSensitiveFileName(const String & rURL)124 sal_Bool UCB_IsCaseSensitiveFileName( const String& rURL )
125 {
126 	sal_Bool bCaseSensitive;
127 	try
128 	{
129 		STAR_REFERENCE( lang::XMultiServiceFactory ) xMSF =
130 									comphelper::getProcessServiceFactory();
131 
132 		INetURLObject aTempObj( rURL );
133 		aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
134 		STAR_REFERENCE( ucb::XContentIdentifier ) xRef1 = new
135 				ucbhelper::ContentIdentifier( xMSF,
136 							aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
137 
138 		aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
139 		STAR_REFERENCE( ucb::XContentIdentifier ) xRef2 = new
140 				ucbhelper::ContentIdentifier( xMSF,
141 							aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
142 
143 		STAR_REFERENCE( ucb::XContentProvider ) xProv =
144 				ucbhelper::ContentBroker::get()->getContentProviderInterface();
145 
146 		sal_Int32 nCompare = xProv->compareContentIds( xRef1, xRef2 );
147 		bCaseSensitive = 0 != nCompare;
148 	}
149 	catch( UNO_NMSPC::Exception& )
150 	{
151 		bCaseSensitive = sal_False;
152 		ASSERT( sal_False, "Exeception from compareContentIds()" );
153 	}
154 	return bCaseSensitive;
155 }
156 
UCB_IsReadOnlyFileName(const String & rURL)157 sal_Bool UCB_IsReadOnlyFileName( const String& rURL )
158 {
159 	sal_Bool bIsReadOnly = sal_False;
160 	try
161 	{
162 		ucbhelper::Content aCnt( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )());
163 		UNO_NMSPC::Any aAny = aCnt.getPropertyValue(
164 							rtl::OUString::createFromAscii( "IsReadOnly" ));
165 		if(aAny.hasValue())
166 			bIsReadOnly = *(sal_Bool*)aAny.getValue();
167 	}
168 	catch( UNO_NMSPC::Exception& )
169 	{
170 		bIsReadOnly = sal_False;
171 	}
172 	return bIsReadOnly;
173 }
174 
UCB_IsFile(const String & rURL)175 sal_Bool UCB_IsFile( const String& rURL )
176 {
177     sal_Bool bExists = sal_False;
178     try
179     {
180         ::ucbhelper::Content aContent( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )() );
181         bExists = aContent.isDocument();
182     }
183     catch (UNO_NMSPC::Exception &)
184     {
185     }
186     return bExists;
187 }
188 
UCB_IsDirectory(const String & rURL)189 sal_Bool UCB_IsDirectory( const String& rURL )
190 {
191     sal_Bool bExists = sal_False;
192     try
193     {
194         ::ucbhelper::Content aContent( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )() );
195         bExists = aContent.isFolder();
196     }
197     catch (UNO_NMSPC::Exception &)
198     {
199     }
200     return bExists;
201 }
202 
203 	// get a list of files from the folder of the URL
204 	// options: pExtension = 0 -> all, else this specific extension
205 	// 			pDateTime != 0 -> returns also the modified date/time of
206 	//                       the files in a SvPtrarr -->
207 	//                       !! objects must be deleted from the caller!!
UCB_GetFileListOfFolder(const String & rURL,SvStrings & rList,const String * pExtension,SvPtrarr * pDateTimeList)208 sal_Bool UCB_GetFileListOfFolder( const String& rURL, SvStrings& rList,
209 								const String* pExtension,
210 								SvPtrarr* pDateTimeList )
211 {
212 	sal_Bool bOk = sal_False;
213 	try
214 	{
215 		ucbhelper::Content aCnt( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )());
216 		STAR_REFERENCE( sdbc::XResultSet ) xResultSet;
217 
218 		sal_uInt16 nSeqSize = pDateTimeList ? 2 : 1;
219 		UNO_NMSPC::Sequence < rtl::OUString > aProps( nSeqSize );
220 		rtl::OUString* pProps = aProps.getArray();
221 		pProps[ 0 ] = rtl::OUString::createFromAscii( "Title" );
222 		if( pDateTimeList )
223 			pProps[ 1 ] = rtl::OUString::createFromAscii( "DateModified" );
224 
225     	try
226     	{
227         	xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
228     	}
229     	catch( UNO_NMSPC::Exception& )
230     	{
231         	DBG_ERRORFILE( "create cursor failed!" );
232     	}
233 
234     	if( xResultSet.is() )
235     	{
236 			STAR_REFERENCE( sdbc::XRow ) xRow( xResultSet, UNO_NMSPC::UNO_QUERY );
237 			xub_StrLen nExtLen = pExtension ? pExtension->Len() : 0;
238         	try
239         	{
240             	if( xResultSet->first() )
241             	{
242                 	do {
243 						String sTitle( xRow->getString( 1 ) );
244 						if( !nExtLen ||
245 							( sTitle.Len() > nExtLen &&
246 							  sTitle.Equals( *pExtension,
247 							  			sTitle.Len() - nExtLen, nExtLen )) )
248 						{
249 							String* pStr = new String( sTitle );
250 							rList.Insert( pStr, rList.Count() );
251 
252 							if( pDateTimeList )
253 							{
254 								STAR_NMSPC::util::DateTime aStamp = xRow->getTimestamp(2);
255 								::DateTime* pDateTime = new ::DateTime(
256 										::Date( aStamp.Day,
257 												aStamp.Month,
258 												aStamp.Year ),
259 										::Time( aStamp.Hours,
260 												aStamp.Minutes,
261 												aStamp.Seconds,
262 												aStamp.HundredthSeconds ));
263 								void* p = pDateTime;
264 								pDateTimeList->Insert( p,
265 													pDateTimeList->Count() );
266 							}
267 						}
268 
269 					} while( xResultSet->next() );
270 				}
271 				bOk = sal_True;
272 			}
273             catch( UNO_NMSPC::Exception& )
274             {
275                 DBG_ERRORFILE( "Exception caught!" );
276             }
277 		}
278 	}
279 	catch( UNO_NMSPC::Exception& )
280 	{
281 		DBG_ERRORFILE( "Exception caught!" );
282 		bOk = sal_False;
283     }
284 	return bOk;
285 }
286 
287 }
288