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