xref: /trunk/main/sfx2/source/bastyp/helper.cxx (revision d119d52d)
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_sfx2.hxx"
26 
27 #include "helper.hxx"
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/ucb/CommandAbortedException.hpp>
32 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
33 #include <com/sun/star/ucb/NameClash.hpp>
34 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
35 #include <com/sun/star/ucb/TransferInfo.hpp>
36 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
37 #include <com/sun/star/ucb/XCommandInfo.hpp>
38 #include <com/sun/star/ucb/XContentAccess.hpp>
39 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
40 #include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
41 #include <com/sun/star/util/DateTime.hpp>
42 #include <com/sun/star/io/XInputStream.hpp>
43 #include <unotools/localedatawrapper.hxx>
44 #include <rtl/strbuf.hxx>
45 
46 #include <tools/ref.hxx>
47 #include <tools/debug.hxx>
48 #include <tools/urlobj.hxx>
49 #include <tools/datetime.hxx>
50 #include <vcl/svapp.hxx>
51 #include <ucbhelper/content.hxx>
52 #include <ucbhelper/commandenvironment.hxx>
53 #include <comphelper/processfactory.hxx>
54 #include <osl/file.hxx>
55 
56 using namespace com::sun::star;
57 using namespace rtl;
58 using namespace comphelper;
59 using namespace osl;
60 
DECLARE_LIST(StringList_Impl,OUString *)61 DECLARE_LIST( StringList_Impl, OUString* )
62 
63 #define CONVERT_DATETIME( aUnoDT, aToolsDT ) \
64 	aToolsDT = DateTime( Date( aUnoDT.Day, aUnoDT.Month, aUnoDT.Year ), \
65 						 Time( aUnoDT.Hours, aUnoDT.Minutes, aUnoDT.Seconds, aUnoDT.HundredthSeconds ) );
66 
67 void AppendDateTime_Impl( const util::DateTime rDT,
68 						  String& rRow, const LocaleDataWrapper& rWrapper )
69 {
70 	DateTime aDT;
71 	CONVERT_DATETIME( rDT, aDT );
72 	String aDateStr = rWrapper.getDate( aDT );
73 	aDateStr += String::CreateFromAscii( ", " );
74 	aDateStr += rWrapper.getTime( aDT );
75 	rRow += aDateStr;
76 }
77 
78 // SfxContentHelper ------------------------------------------------------
79 
Transfer_Impl(const String & rSource,const String & rDest,sal_Bool bMoveData,sal_Int32 nNameClash)80 sal_Bool SfxContentHelper::Transfer_Impl( const String& rSource, const String& rDest, sal_Bool bMoveData, sal_Int32 nNameClash )
81 {
82 	sal_Bool bRet = sal_True, bKillSource = sal_False;
83     INetURLObject aSourceObj( rSource );
84     DBG_ASSERT( aSourceObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
85 
86     INetURLObject aDestObj( rDest );
87     DBG_ASSERT( aDestObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
88 	if ( bMoveData && aSourceObj.GetProtocol() != aDestObj.GetProtocol() )
89 	{
90 		bMoveData = sal_False;
91 		bKillSource = sal_True;
92 	}
93 	String aName = aDestObj.getName();
94 	aDestObj.removeSegment();
95 	aDestObj.setFinalSlash();
96 
97 	try
98 	{
99 		::ucbhelper::Content aDestPath( aDestObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
100 		uno::Reference< ucb::XCommandInfo > xInfo = aDestPath.getCommands();
101 		OUString aTransferName = OUString::createFromAscii( "transfer" );
102 		if ( xInfo->hasCommandByName( aTransferName ) )
103 		{
104 			aDestPath.executeCommand( aTransferName, uno::makeAny(
105                 ucb::TransferInfo( bMoveData, aSourceObj.GetMainURL( INetURLObject::NO_DECODE ), aName, nNameClash ) ) );
106 		}
107 		else
108 		{
109 			DBG_ERRORFILE( "transfer command not available" );
110 		}
111 	}
112 	catch( ucb::CommandAbortedException& )
113 	{
114 		bRet = sal_False;
115 	}
116 	catch( uno::Exception& )
117 	{
118 		DBG_ERRORFILE( "Any other exception" );
119 		bRet = sal_False;
120 	}
121 
122 	if ( bKillSource )
123 		SfxContentHelper::Kill( rSource );
124 
125 	return bRet;
126 }
127 
128 // -----------------------------------------------------------------------
129 
IsDocument(const String & rContent)130 sal_Bool SfxContentHelper::IsDocument( const String& rContent )
131 {
132 	sal_Bool bRet = sal_False;
133     INetURLObject aObj( rContent );
134     DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
135 
136 	try
137 	{
138 		::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
139 		bRet = aCnt.isDocument();
140 	}
141 	catch( ucb::CommandAbortedException& )
142 	{
143         DBG_WARNING( "CommandAbortedException" );
144 	}
145     catch( ucb::IllegalIdentifierException& )
146 	{
147         DBG_WARNING( "IllegalIdentifierException" );
148 	}
149     catch( ucb::ContentCreationException& )
150 	{
151         DBG_WARNING( "IllegalIdentifierException" );
152 	}
153     catch( uno::Exception& )
154 	{
155 		DBG_ERRORFILE( "Any other exception" );
156 	}
157 
158 	return bRet;
159 }
160 
161 // -----------------------------------------------------------------------
162 
IsFolder(const String & rContent)163 sal_Bool SfxContentHelper::IsFolder( const String& rContent )
164 {
165 	sal_Bool bRet = sal_False;
166     INetURLObject aObj( rContent );
167     DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
168 	try
169 	{
170 		::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
171 		bRet = aCnt.isFolder();
172 	}
173 	catch( ucb::CommandAbortedException& )
174 	{
175         DBG_WARNING( "CommandAbortedException" );
176 	}
177     catch( ucb::IllegalIdentifierException& )
178 	{
179         DBG_WARNING( "IllegalIdentifierException" );
180 	}
181     catch( ucb::ContentCreationException& )
182 	{
183         DBG_WARNING( "IllegalIdentifierException" );
184 	}
185 	catch( uno::Exception& )
186 	{
187 		DBG_ERRORFILE( "Any other exception" );
188 	}
189 
190 	return bRet;
191 }
192 
193 // -----------------------------------------------------------------------
194 
GetTitle(const String & rContent,String & rTitle)195 sal_Bool SfxContentHelper::GetTitle( const String& rContent, String& rTitle )
196 {
197 	sal_Bool bRet = sal_False;
198     INetURLObject aObj( rContent );
199     DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
200 	try
201 	{
202 		::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
203 		OUString aTemp;
204 		aCnt.getPropertyValue( OUString::createFromAscii( "Title" ) ) >>= aTemp;
205 		rTitle = String( aTemp );
206 		bRet = sal_True;
207 	}
208 	catch( ucb::CommandAbortedException& )
209 	{
210 		DBG_ERRORFILE( "CommandAbortedException" );
211 	}
212 	catch( uno::Exception& )
213 	{
214 		DBG_ERRORFILE( "Any other exception" );
215 	}
216 	return bRet;
217 }
218 
219 // -----------------------------------------------------------------------
220 
Kill(const String & rContent)221 sal_Bool SfxContentHelper::Kill( const String& rContent )
222 {
223 	sal_Bool bRet = sal_True;
224     INetURLObject aDeleteObj( rContent );
225     DBG_ASSERT( aDeleteObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
226 
227 	try
228 	{
229 		::ucbhelper::Content aCnt( aDeleteObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
230 		aCnt.executeCommand( OUString::createFromAscii( "delete" ), uno::makeAny( sal_Bool( sal_True ) ) );
231 	}
232 	catch( ucb::CommandAbortedException& )
233 	{
234         DBG_WARNING( "CommandAbortedException" );
235 		bRet = sal_False;
236 	}
237 	catch( uno::Exception& )
238 	{
239 		DBG_ERRORFILE( "Any other exception" );
240 		bRet = sal_False;
241 	}
242 
243 	return bRet;
244 }
245 
246 // -----------------------------------------------------------------------
247 
GetFolderContents(const String & rFolder,sal_Bool bFolder,sal_Bool bSorted)248 uno::Sequence < OUString > SfxContentHelper::GetFolderContents( const String& rFolder, sal_Bool bFolder, sal_Bool bSorted )
249 {
250 	StringList_Impl* pFiles = NULL;
251     INetURLObject aFolderObj( rFolder );
252     DBG_ASSERT( aFolderObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
253 	try
254 	{
255 		::ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
256 		uno::Reference< sdbc::XResultSet > xResultSet;
257         uno::Sequence< OUString > aProps(2);
258 		OUString* pProps = aProps.getArray();
259         pProps[0] = OUString::createFromAscii( "Title" );
260         pProps[1] = OUString::createFromAscii( "IsFolder" );
261 
262 		try
263 		{
264 			::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
265             if ( !bSorted )
266             {
267                 xResultSet = aCnt.createCursor( aProps, eInclude );
268             }
269             else
270             {
271                 uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
272                 xDynResultSet = aCnt.createDynamicCursor( aProps, eInclude );
273 
274                 uno::Reference < ucb::XAnyCompareFactory > xFactory;
275                 uno::Reference < lang::XMultiServiceFactory > xMgr = getProcessServiceFactory();
276                 uno::Reference < ucb::XSortedDynamicResultSetFactory > xSRSFac(
277                     xMgr->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SortedDynamicResultSetFactory") ), uno::UNO_QUERY );
278 
279                 uno::Sequence< ucb::NumberedSortingInfo > aSortInfo( 2 );
280                 ucb::NumberedSortingInfo* pInfo = aSortInfo.getArray();
281                 pInfo[ 0 ].ColumnIndex = 2;
282                 pInfo[ 0 ].Ascending   = sal_False;
283                 pInfo[ 1 ].ColumnIndex = 1;
284                 pInfo[ 1 ].Ascending   = sal_True;
285 
286                 uno::Reference< ucb::XDynamicResultSet > xDynamicResultSet;
287                 xDynamicResultSet =
288                     xSRSFac->createSortedDynamicResultSet( xDynResultSet, aSortInfo, xFactory );
289                 if ( xDynamicResultSet.is() )
290                 {
291                     xResultSet = xDynamicResultSet->getStaticResultSet();
292                 }
293             }
294 		}
295 		catch( ucb::CommandAbortedException& )
296 		{
297 			DBG_ERRORFILE( "createCursor: CommandAbortedException" );
298 		}
299 		catch( uno::Exception& )
300 		{
301 			DBG_ERRORFILE( "createCursor: Any other exception" );
302 		}
303 
304 		if ( xResultSet.is() )
305 		{
306 			pFiles = new StringList_Impl;
307 			uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
308 			try
309 			{
310 				while ( xResultSet->next() )
311 				{
312 					OUString aId = xContentAccess->queryContentIdentifierString();
313 					OUString* pFile = new OUString( aId );
314 					pFiles->Insert( pFile, LIST_APPEND );
315 				}
316 			}
317 			catch( ucb::CommandAbortedException& )
318 			{
319 				DBG_ERRORFILE( "XContentAccess::next(): CommandAbortedException" );
320 			}
321 			catch( uno::Exception& )
322 			{
323 				DBG_ERRORFILE( "XContentAccess::next(): Any other exception" );
324 			}
325 		}
326 	}
327 	catch( uno::Exception& )
328 	{
329 		DBG_ERRORFILE( "GetFolderContents: Any other exception" );
330 	}
331 
332 	if ( pFiles )
333 	{
334         sal_uIntPtr nCount = pFiles->Count();
335 		uno::Sequence < OUString > aRet( nCount );
336 		OUString* pRet = aRet.getArray();
337 		for ( sal_uIntPtr i = 0; i < nCount; ++i )
338 		{
339 			OUString* pFile = pFiles->GetObject(i);
340 			pRet[i] = *( pFile );
341 			delete pFile;
342 		}
343 		delete pFiles;
344 		return aRet;
345 	}
346 	else
347 		return uno::Sequence < OUString > ();
348 }
349 
350 // -----------------------------------------------------------------------
351 
GetFolderContentProperties(const String & rFolder,sal_Bool bIsFolder)352 uno::Sequence < OUString > SfxContentHelper::GetFolderContentProperties( const String& rFolder, sal_Bool bIsFolder )
353 {
354 	StringList_Impl* pProperties = NULL;
355     INetURLObject aFolderObj( rFolder );
356     DBG_ASSERT( aFolderObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
357 	try
358 	{
359         uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
360         uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
361                     xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), uno::UNO_QUERY );
362 
363         ::ucbhelper::Content aCnt( aFolderObj.GetMainURL( INetURLObject::NO_DECODE ), new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ) );
364 		uno::Reference< sdbc::XResultSet > xResultSet;
365 		uno::Sequence< OUString > aProps(5);
366 		OUString* pProps = aProps.getArray();
367 		pProps[0] = OUString::createFromAscii( "Title" );
368 		pProps[1] = OUString::createFromAscii( "ContentType" );
369 		pProps[2] = OUString::createFromAscii( "Size" );
370 		pProps[3] = OUString::createFromAscii( "DateModified" );
371 		pProps[4] = OUString::createFromAscii( "IsFolder" );
372 
373 		try
374 		{
375             uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
376 			::ucbhelper::ResultSetInclude eInclude = bIsFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
377 			xDynResultSet = aCnt.createDynamicCursor( aProps, eInclude );
378 
379 			uno::Reference < ucb::XAnyCompareFactory > xCmpFactory;
380 			uno::Reference < lang::XMultiServiceFactory > xMgr = getProcessServiceFactory();
381 		    uno::Reference < ucb::XSortedDynamicResultSetFactory > xSRSFac(
382 				xMgr->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SortedDynamicResultSetFactory") ), uno::UNO_QUERY );
383 
384 			uno::Sequence< ucb::NumberedSortingInfo > aSortInfo( 2 );
385 			ucb::NumberedSortingInfo* pInfo = aSortInfo.getArray();
386 			pInfo[ 0 ].ColumnIndex = 5;
387 			pInfo[ 0 ].Ascending   = sal_False;
388 			pInfo[ 1 ].ColumnIndex = 1;
389 			pInfo[ 1 ].Ascending   = sal_True;
390 
391 			uno::Reference< ucb::XDynamicResultSet > xDynamicResultSet;
392 			xDynamicResultSet =
393 				xSRSFac->createSortedDynamicResultSet( xDynResultSet, aSortInfo, xCmpFactory );
394 			if ( xDynamicResultSet.is() )
395 			{
396 				xResultSet = xDynamicResultSet->getStaticResultSet();
397 			}
398 
399 //			if ( xDynResultSet.is() )
400 //				xResultSet = xDynResultSet->getStaticResultSet();
401 		}
402 		catch( ucb::CommandAbortedException& )
403 		{
404 			DBG_ERRORFILE( "createCursor: CommandAbortedException" );
405 		}
406 		catch( uno::Exception& )
407 		{
408 			DBG_ERRORFILE( "createCursor: Any other exception" );
409 		}
410 
411 		if ( xResultSet.is() )
412 		{
413 			LocaleDataWrapper aLocaleWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
414 			pProperties = new StringList_Impl;
415 			uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
416 			uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
417 			sal_uIntPtr nFolderPos = LIST_APPEND;
418 
419 			try
420 			{
421 				while ( xResultSet->next() )
422 				{
423 					String aTitle( xRow->getString(1) );
424 					String aType( xRow->getString(2) );
425 					sal_Int64 nSize = xRow->getLong(3);
426 					util::DateTime aDT = xRow->getTimestamp(4);
427 					sal_Bool bFolder = xRow->getBoolean(5);
428 
429 					String aRow = aTitle;
430 					aRow += '\t';
431 //!					aRow += aType;
432 //!					aRow += '\t';
433 					aRow += String::CreateFromInt64( nSize );
434 					aRow += '\t';
435 					AppendDateTime_Impl( aDT, aRow, aLocaleWrapper );
436 					aRow += '\t';
437 					aRow += String( xContentAccess->queryContentIdentifierString() );
438 					aRow += '\t';
439 					aRow += bFolder ? '1' : '0';
440 					OUString* pRow = new OUString( aRow );
441 					sal_uIntPtr nPos = LIST_APPEND;
442 					if ( bFolder )
443 					{
444 						if ( LIST_APPEND == nFolderPos )
445 							nFolderPos = 0;
446 						else
447 							nFolderPos++;
448 						nPos = nFolderPos;
449 					}
450 					pProperties->Insert( pRow, nPos );
451 				}
452 			}
453 			catch( ucb::CommandAbortedException& )
454 			{
455 				DBG_ERRORFILE( "XContentAccess::next(): CommandAbortedException" );
456 			}
457 			catch( uno::Exception& )
458 			{
459 				DBG_ERRORFILE( "XContentAccess::next(): Any other exception" );
460 			}
461 		}
462 	}
463 	catch( uno::Exception& )
464 	{
465 		DBG_ERRORFILE( "GetFolderContents: Any other exception" );
466 	}
467 
468 	if ( pProperties )
469 	{
470         sal_uIntPtr nCount = pProperties->Count();
471 		uno::Sequence < OUString > aRet( nCount );
472 		OUString* pRet = aRet.getArray();
473 		for ( sal_uIntPtr i = 0; i < nCount; ++i )
474 		{
475 			OUString* pProperty = pProperties->GetObject(i);
476 			pRet[i] = *( pProperty );
477 			delete pProperty;
478 		}
479 		delete pProperties;
480 		return aRet;
481 	}
482 	else
483 		return uno::Sequence < OUString > ();
484 }
485 
486 // -----------------------------------------------------------------------
487 
GetResultSet(const String & rURL)488 uno::Sequence < OUString > SfxContentHelper::GetResultSet( const String& rURL )
489 {
490 	StringList_Impl* pList = NULL;
491 	try
492 	{
493 		::ucbhelper::Content aCnt( rURL, uno::Reference< ucb::XCommandEnvironment >() );
494 		uno::Reference< sdbc::XResultSet > xResultSet;
495 		uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
496 		uno::Sequence< OUString > aProps(3);
497 		OUString* pProps = aProps.getArray();
498 		pProps[0] = OUString::createFromAscii( "Title" );
499 		pProps[1] = OUString::createFromAscii( "ContentType" );
500 		pProps[2] = OUString::createFromAscii( "IsFolder" );
501 
502 		try
503 		{
504 			xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
505 			if ( xDynResultSet.is() )
506 				xResultSet = xDynResultSet->getStaticResultSet();
507 		}
508 		catch( ucb::CommandAbortedException& )
509 		{
510 			DBG_ERRORFILE( "createCursor: CommandAbortedException" );
511 		}
512 		catch( uno::Exception& )
513 		{
514 			DBG_ERRORFILE( "createCursor: Any other exception" );
515 		}
516 
517 		if ( xResultSet.is() )
518 		{
519 			pList = new StringList_Impl;
520 			uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
521 			uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
522 
523 			try
524 			{
525 				while ( xResultSet->next() )
526 				{
527 					String aTitle( xRow->getString(1) );
528 					String aType( xRow->getString(2) );
529 					String aRow = aTitle;
530 					aRow += '\t';
531 					aRow += aType;
532 					aRow += '\t';
533 					aRow += String( xContentAccess->queryContentIdentifierString() );
534 					OUString* pRow = new OUString( aRow );
535 					pList->Insert( pRow, LIST_APPEND );
536 				}
537 			}
538 			catch( ucb::CommandAbortedException& )
539 			{
540 				DBG_ERRORFILE( "XContentAccess::next(): CommandAbortedException" );
541 			}
542 			catch( uno::Exception& )
543 			{
544 				DBG_ERRORFILE( "XContentAccess::next(): Any other exception" );
545 			}
546 		}
547 	}
548 	catch( uno::Exception& e )
549 	{
550         (void) e;
551         DBG_ERRORFILE(
552             rtl::OUStringToOString(
553                 (rtl::OUString(
554                     RTL_CONSTASCII_USTRINGPARAM(
555                         "GetResultSet: Any other exception: ")) +
556                  e.Message),
557                 RTL_TEXTENCODING_UTF8).
558             getStr());
559 	}
560 
561 	if ( pList )
562 	{
563 		sal_uIntPtr nCount = pList->Count();
564 		uno::Sequence < OUString > aRet( nCount );
565 		OUString* pRet = aRet.getArray();
566 		for ( sal_uIntPtr i = 0; i < nCount; ++i )
567 		{
568 			OUString* pEntry = pList->GetObject(i);
569 			pRet[i] = *( pEntry );
570 			delete pEntry;
571 		}
572 		delete pList;
573 		return aRet;
574 	}
575 	else
576 		return uno::Sequence < OUString > ();
577 }
578 
579 // -----------------------------------------------------------------------
580 
GetHelpTreeViewContents(const String & rURL)581 uno::Sequence< OUString > SfxContentHelper::GetHelpTreeViewContents( const String& rURL )
582 {
583 	StringList_Impl* pProperties = NULL;
584 	try
585 	{
586         uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
587         uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
588                     xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), uno::UNO_QUERY );
589 
590         ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ) );
591 		uno::Reference< sdbc::XResultSet > xResultSet;
592 		uno::Sequence< OUString > aProps(2);
593 		OUString* pProps = aProps.getArray();
594 		pProps[0] = OUString::createFromAscii( "Title" );
595 		pProps[1] = OUString::createFromAscii( "IsFolder" );
596 
597 		try
598 		{
599             uno::Reference< ucb::XDynamicResultSet > xDynResultSet;
600 			xDynResultSet = aCnt.createDynamicCursor( aProps, ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
601 			if ( xDynResultSet.is() )
602 				xResultSet = xDynResultSet->getStaticResultSet();
603 		}
604 		catch( ucb::CommandAbortedException& )
605 		{
606 		}
607 		catch( uno::Exception& )
608 		{
609 		}
610 
611 		if ( xResultSet.is() )
612 		{
613 			pProperties = new StringList_Impl;
614 			uno::Reference< sdbc::XRow > xRow( xResultSet, uno::UNO_QUERY );
615 			uno::Reference< ucb::XContentAccess > xContentAccess( xResultSet, uno::UNO_QUERY );
616 
617 			try
618 			{
619 				while ( xResultSet->next() )
620 				{
621 					String aTitle( xRow->getString(1) );
622 					sal_Bool bFolder = xRow->getBoolean(2);
623 					String aRow = aTitle;
624 					aRow += '\t';
625 					aRow += String( xContentAccess->queryContentIdentifierString() );
626 					aRow += '\t';
627 					aRow += bFolder ? '1' : '0';
628 					OUString* pRow = new OUString( aRow );
629 					pProperties->Insert( pRow, LIST_APPEND );
630 				}
631 			}
632 			catch( ucb::CommandAbortedException& )
633 			{
634 			}
635 			catch( uno::Exception& )
636 			{
637 			}
638 		}
639 	}
640 	catch( uno::Exception& )
641 	{
642 	}
643 
644 	if ( pProperties )
645 	{
646         sal_uIntPtr nCount = pProperties->Count();
647 		uno::Sequence < OUString > aRet( nCount );
648 		OUString* pRet = aRet.getArray();
649 		for ( sal_uIntPtr i = 0; i < nCount; ++i )
650 		{
651 			OUString* pProperty = pProperties->GetObject(i);
652 			pRet[i] = *( pProperty );
653 			delete pProperty;
654 		}
655 		delete pProperties;
656 		return aRet;
657 	}
658 	else
659 		return uno::Sequence < OUString > ();
660 }
661 
662 // -----------------------------------------------------------------------
663 
GetActiveHelpString(const String & rURL)664 String SfxContentHelper::GetActiveHelpString( const String& rURL )
665 {
666 	String aRet;
667 	try
668 	{
669         uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
670         uno::Reference< task::XInteractionHandler > xInteractionHandler = uno::Reference< task::XInteractionHandler > (
671                     xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), uno::UNO_QUERY );
672         ::ucbhelper::Content aCnt( rURL, new ::ucbhelper::CommandEnvironment( xInteractionHandler, uno::Reference< ucb::XProgressHandler >() ) );
673 		// open the "active help" stream
674 		uno::Reference< io::XInputStream > xStream = aCnt.openStream();
675 		// and convert it to a String
676 		uno::Sequence< sal_Int8 > lData;
677 		sal_Int32 nRead = xStream->readBytes( lData, 1024 );
678 		while ( nRead > 0 )
679 		{
680 			OStringBuffer sBuffer( nRead );
681 			for( sal_Int32 i = 0; i < nRead; ++i )
682     			sBuffer.append( (sal_Char)lData[i] );
683 			OUString sString = OStringToOUString( sBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
684 			aRet += String( sString );
685 
686 			nRead = xStream->readBytes( lData, 1024 );
687 		}
688 	}
689 	catch( uno::Exception& )
690 	{
691 	}
692 
693 	return aRet;
694 }
695 
696 // -----------------------------------------------------------------------
697 
IsHelpErrorDocument(const String & rURL)698 sal_Bool SfxContentHelper::IsHelpErrorDocument( const String& rURL )
699 {
700 	sal_Bool bRet = sal_False;
701 	try
702 	{
703 		::ucbhelper::Content aCnt( INetURLObject( rURL ).GetMainURL( INetURLObject::NO_DECODE ),
704 					  uno::Reference< ucb::XCommandEnvironment > () );
705 		if ( !( aCnt.getPropertyValue( OUString::createFromAscii( "IsErrorDocument" ) ) >>= bRet ) )
706 		{
707 			DBG_ERRORFILE( "Property 'IsErrorDocument' is missing" );
708 		}
709 	}
710 	catch( uno::Exception& )
711 	{
712 	}
713 
714 	return bRet;
715 }
716 
717 // -----------------------------------------------------------------------
718 
CopyTo(const String & rSource,const String & rDest)719 sal_Bool SfxContentHelper::CopyTo( const String& rSource, const String& rDest )
720 {
721     return Transfer_Impl( rSource, rDest, sal_False, ucb::NameClash::ERROR );
722 }
723 
724 // -----------------------------------------------------------------------
725 
MoveTo(const String & rSource,const String & rDest,sal_Int32 nNameClash)726 sal_Bool SfxContentHelper::MoveTo( const String& rSource, const String& rDest, sal_Int32 nNameClash )
727 {
728     return Transfer_Impl( rSource, rDest, sal_True, nNameClash );
729 }
730 
731 // -----------------------------------------------------------------------
732 
MakeFolder(const String & rFolder)733 sal_Bool SfxContentHelper::MakeFolder( const String& rFolder )
734 {
735     INetURLObject aURL( rFolder );
736     DBG_ASSERT( aURL.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
737 	String aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
738 	aURL.removeSegment();
739     uno::Sequence < OUString > aNames(2);
740 	OUString* pNames = aNames.getArray();
741 	pNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
742 	pNames[1] = OUString( RTL_CONSTASCII_USTRINGPARAM( "IsFolder" ) );
743 	uno::Sequence<uno::Any> aValues(2);
744 	uno::Any* pValues = aValues.getArray();
745 	pValues[0] = uno::makeAny( OUString( aTitle ) );
746 	pValues[1] = uno::makeAny( sal_Bool( sal_True ) );
747 	uno::Reference< ucb::XCommandEnvironment > aCmdEnv;
748 	sal_Bool bRet = sal_False;
749 	try
750 	{
751 		::ucbhelper::Content aCnt( aURL.GetMainURL( INetURLObject::NO_DECODE ), aCmdEnv );
752         ::ucbhelper::Content aNewFolder;
753 		OUString aType( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.staroffice.fsys-folder" ) );
754 		bRet = aCnt.insertNewContent( aType, aNames, aValues, aNewFolder );
755 	}
756 	catch( ucb::CommandAbortedException& )
757 	{
758 		// double name?
759 	}
760 	catch( ucb::IllegalIdentifierException& )
761 	{
762 		DBG_ERRORFILE( "Illegal identifier" );
763 	}
764     catch( uno::Exception& )
765 	{
766 		DBG_ERRORFILE( "Any other exception" );
767 	}
768 
769 	return bRet;
770 }
771 
772 // -----------------------------------------------------------------------
773 
QueryDiskSpace(const String & rPath,sal_Int64 & rFreeBytes)774 ErrCode SfxContentHelper::QueryDiskSpace( const String& rPath, sal_Int64& rFreeBytes )
775 {
776 	ErrCode nErr = 0;
777 	rFreeBytes = 0;
778     INetURLObject aObj( rPath );
779     DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
780 	try
781 	{
782 		::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
783 		aCnt.getPropertyValue( OUString::createFromAscii( "FreeSpace" ) ) >>= rFreeBytes;
784 	}
785 	catch( ucb::CommandAbortedException& )
786 	{
787 		DBG_ERRORFILE( "CommandAbortedException" );
788 		nErr = ERRCODE_IO_GENERAL;
789 	}
790 	catch( uno::Exception& )
791 	{
792 		DBG_ERRORFILE( "Any other exception" );
793 		nErr = ERRCODE_IO_GENERAL;
794 	}
795 	return nErr;
796 }
797 
798 // -----------------------------------------------------------------------
799 
GetSize(const String & rContent)800 sal_uIntPtr SfxContentHelper::GetSize( const String& rContent )
801 {
802 	sal_uIntPtr nSize = 0;
803 	sal_Int64 nTemp = 0;
804     INetURLObject aObj( rContent );
805     DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
806 	try
807 	{
808 		::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
809 		aCnt.getPropertyValue( OUString::createFromAscii( "Size" ) ) >>= nTemp;
810 	}
811 	catch( ucb::CommandAbortedException& )
812 	{
813 		DBG_ERRORFILE( "CommandAbortedException" );
814 	}
815 	catch( uno::Exception& )
816 	{
817 		DBG_ERRORFILE( "Any other exception" );
818 	}
819 	nSize = (sal_uInt32)nTemp;
820 	return nSize;
821 }
822 
823 // -----------------------------------------------------------------------
824 // please don't use it (only used in appbas.cxx and appcfg.cxx)
Exists(const String & rContent)825 sal_Bool SfxContentHelper::Exists( const String& rContent )
826 {
827 	sal_Bool bRet = sal_False;
828 	INetURLObject aObj( rContent );
829 	DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Invalid URL!" );
830 
831 	try
832 	{
833 		::ucbhelper::Content aCnt( aObj.GetMainURL( INetURLObject::NO_DECODE ), uno::Reference< ucb::XCommandEnvironment > () );
834 		// just try to get the property; if no exception is thrown, the content exists!
835 		aCnt.isDocument();
836 		bRet = sal_True;
837 	}
838 	catch( ucb::CommandAbortedException& )
839 	{
840         	DBG_WARNING( "CommandAbortedException" );
841 	}
842 	catch( ucb::IllegalIdentifierException& )
843 	{
844 	        DBG_WARNING( "IllegalIdentifierException" );
845 	}
846 	catch( ucb::ContentCreationException& )
847 	{
848         	DBG_WARNING( "IllegalIdentifierException" );
849 	}
850 	catch( uno::Exception& )
851 	{
852 		DBG_ERRORFILE( "Any other exception" );
853 	}
854 
855 	return bRet;
856 
857 }
858 
859 // -----------------------------------------------------------------------
860 
Find(const String & rFolder,const String & rName,String & rFile)861 sal_Bool SfxContentHelper::Find( const String& rFolder, const String& rName, String& rFile )
862 {
863 	sal_Bool bRet = sal_False;
864     rtl::OUString aFile;
865 
866     if ( FileBase::searchFileURL( rName, rFolder, aFile ) == FileBase::E_None )
867 	{
868 		rFile = aFile;
869 		bRet = sal_True;
870 	}
871 
872 	return bRet;
873 }
874 
875 
876