1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ucbhelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_unotools.hxx"
34 #include "unotools/ucbhelper.hxx"
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/sdbc/XResultSet.hpp>
37 #include <com/sun/star/sdbc/XRow.hpp>
38 #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
39 #include <com/sun/star/ucb/XCommandProcessor.hpp>
40 #include <com/sun/star/ucb/CommandAbortedException.hpp>
41 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
42 #include <com/sun/star/ucb/NameClashException.hpp>
43 #include <com/sun/star/ucb/NameClash.hpp>
44 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
45 #include <com/sun/star/ucb/TransferInfo.hpp>
46 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
47 #include <com/sun/star/ucb/XCommandInfo.hpp>
48 #include <com/sun/star/ucb/XContentAccess.hpp>
49 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
50 #include <com/sun/star/ucb/XContentCreator.hpp>
51 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
52 #include <com/sun/star/ucb/XSortedDynamicResultSetFactory.hpp>
53 #include <com/sun/star/util/DateTime.hpp>
54 #include <com/sun/star/container/XChild.hpp>
55 #ifndef _COM_SUN_STAR_UCB_INTERACTIVEIODEXCEPTION_HPP_
56 #include <com/sun/star/ucb/InteractiveIOException.hpp>
58 #include <com/sun/star/task/XInteractionHandler.hpp>
59 #include <ucbhelper/commandenvironment.hxx>
60 #include <ucbhelper/content.hxx>
61 #include <comphelper/processfactory.hxx>
62 #include <osl/file.hxx>
64 #include <tools/wldcrd.hxx>
65 #include <tools/ref.hxx>
66 #include <tools/debug.hxx>
67 #include <tools/urlobj.hxx>
68 #include <tools/datetime.hxx>
69 #include <ucbhelper/contentbroker.hxx>
71 #include "unotools/localfilehelper.hxx"
73 using namespace ucbhelper
;
74 using namespace com::sun::star
;
75 using namespace com::sun::star::beans
;
76 using namespace com::sun::star::container
;
77 using namespace com::sun::star::lang
;
78 using namespace com::sun::star::sdbc
;
79 using namespace com::sun::star::task
;
80 using namespace com::sun::star::uno
;
81 using namespace com::sun::star::ucb
;
83 using namespace comphelper
;
86 DECLARE_LIST( StringList_Impl
, OUString
* )
88 #define CONVERT_DATETIME( aUnoDT, aToolsDT ) \
89 aToolsDT = DateTime( Date( aUnoDT.Day, aUnoDT.Month, aUnoDT.Year ), \
90 Time( aUnoDT.Hours, aUnoDT.Minutes, aUnoDT.Seconds, aUnoDT.HundredthSeconds ) );
95 sal_Bool
UCBContentHelper::Transfer_Impl( const String
& rSource
, const String
& rDest
, sal_Bool bMoveData
, sal_Int32 nNameClash
)
97 sal_Bool bRet
= sal_True
, bKillSource
= sal_False
;
98 INetURLObject
aSourceObj( rSource
);
99 DBG_ASSERT( aSourceObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
101 INetURLObject
aDestObj( rDest
);
102 DBG_ASSERT( aDestObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
103 if ( bMoveData
&& aSourceObj
.GetProtocol() != aDestObj
.GetProtocol() )
105 bMoveData
= sal_False
;
106 bKillSource
= sal_True
;
108 String aName
= aDestObj
.getName();
109 aDestObj
.removeSegment();
110 aDestObj
.setFinalSlash();
114 Content
aDestPath( aDestObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
115 uno::Reference
< ::com::sun::star::ucb::XCommandInfo
> xInfo
= aDestPath
.getCommands();
116 OUString aTransferName
= OUString::createFromAscii( "transfer" );
117 if ( xInfo
->hasCommandByName( aTransferName
) )
119 aDestPath
.executeCommand( aTransferName
, makeAny(
120 ::com::sun::star::ucb::TransferInfo( bMoveData
, aSourceObj
.GetMainURL( INetURLObject::NO_DECODE
), aName
, nNameClash
) ) );
124 DBG_ERRORFILE( "transfer command not available" );
127 catch( ::com::sun::star::ucb::CommandAbortedException
& )
131 catch( ::com::sun::star::uno::Exception
& )
137 UCBContentHelper::Kill( rSource
);
142 // -----------------------------------------------------------------------
144 sal_Bool
UCBContentHelper::IsDocument( const String
& rContent
)
146 sal_Bool bRet
= sal_False
;
147 INetURLObject
aObj( rContent
);
148 DBG_ASSERT( aObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
152 Content
aCnt( aObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
153 bRet
= aCnt
.isDocument();
155 catch( ::com::sun::star::ucb::CommandAbortedException
& )
157 DBG_WARNING( "CommandAbortedException" );
159 catch( ::com::sun::star::ucb::IllegalIdentifierException
& )
161 DBG_WARNING( "IllegalIdentifierException" );
163 catch( ContentCreationException
& )
165 DBG_WARNING( "IllegalIdentifierException" );
167 catch( ::com::sun::star::uno::Exception
& )
169 DBG_WARNING( "Any other exception" );
175 // -----------------------------------------------------------------------
177 Any
UCBContentHelper::GetProperty( const String
& rContent
, const ::rtl::OUString
& rName
)
179 INetURLObject
aObj( rContent
);
180 DBG_ASSERT( aObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
183 Content
aCnt( aObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
184 return aCnt
.getPropertyValue( rName
);
186 catch( ::com::sun::star::ucb::CommandAbortedException
& )
188 DBG_WARNING( "CommandAbortedException" );
190 catch( ::com::sun::star::ucb::IllegalIdentifierException
& )
192 DBG_WARNING( "IllegalIdentifierException" );
194 catch( ContentCreationException
& )
196 DBG_WARNING( "IllegalIdentifierException" );
198 catch( ::com::sun::star::uno::Exception
& )
200 DBG_WARNING( "Any other exception" );
206 sal_Bool
UCBContentHelper::IsFolder( const String
& rContent
)
208 sal_Bool bRet
= sal_False
;
209 INetURLObject
aObj( rContent
);
210 DBG_ASSERT( aObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
213 Content
aCnt( aObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
214 bRet
= aCnt
.isFolder();
216 catch( ::com::sun::star::ucb::CommandAbortedException
& )
218 DBG_WARNING( "CommandAbortedException" );
220 catch( ::com::sun::star::ucb::IllegalIdentifierException
& )
222 DBG_WARNING( "IllegalIdentifierException" );
224 catch( ContentCreationException
& )
226 DBG_WARNING( "IllegalIdentifierException" );
228 catch( ::com::sun::star::uno::Exception
& )
230 DBG_WARNING( "Any other exception" );
236 // -----------------------------------------------------------------------
238 sal_Bool
UCBContentHelper::GetTitle( const String
& rContent
, String
& rTitle
)
240 sal_Bool bRet
= sal_False
;
241 INetURLObject
aObj( rContent
);
242 DBG_ASSERT( aObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
245 Content
aCnt( aObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
247 if ( aCnt
.getPropertyValue( OUString::createFromAscii( "Title" ) ) >>= aTemp
)
249 rTitle
= String( aTemp
);
253 catch( ::com::sun::star::ucb::CommandAbortedException
& )
256 catch( ::com::sun::star::uno::Exception
& )
262 // -----------------------------------------------------------------------
264 sal_Bool
UCBContentHelper::Kill( const String
& rContent
)
266 sal_Bool bRet
= sal_True
;
267 INetURLObject
aDeleteObj( rContent
);
268 DBG_ASSERT( aDeleteObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
272 Content
aCnt( aDeleteObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
273 aCnt
.executeCommand( OUString::createFromAscii( "delete" ), makeAny( sal_Bool( sal_True
) ) );
275 catch( ::com::sun::star::ucb::CommandAbortedException
& )
277 DBG_WARNING( "CommandAbortedException" );
280 catch( ::com::sun::star::uno::Exception
& )
282 DBG_WARNING( "Any other exception" );
289 // -----------------------------------------------------------------------
291 Sequence
< OUString
> UCBContentHelper::GetFolderContents( const String
& rFolder
, sal_Bool bFolder
, sal_Bool bSorted
)
293 StringList_Impl
* pFiles
= NULL
;
294 INetURLObject
aFolderObj( rFolder
);
295 DBG_ASSERT( aFolderObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
298 Content
aCnt( aFolderObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
299 uno::Reference
< XResultSet
> xResultSet
;
300 Sequence
< OUString
> aProps( bSorted
? 2 : 1 );
301 OUString
* pProps
= aProps
.getArray();
302 pProps
[0] = OUString::createFromAscii( "Title" );
304 pProps
[1] = OUString::createFromAscii( "IsFolder" );
308 ResultSetInclude eInclude
= bFolder
? INCLUDE_FOLDERS_AND_DOCUMENTS
: INCLUDE_DOCUMENTS_ONLY
;
311 xResultSet
= aCnt
.createCursor( aProps
, eInclude
);
315 uno::Reference
< com::sun::star::ucb::XDynamicResultSet
> xDynResultSet
;
316 xDynResultSet
= aCnt
.createDynamicCursor( aProps
, eInclude
);
318 uno::Reference
< com::sun::star::ucb::XAnyCompareFactory
> xFactory
;
319 uno::Reference
< XMultiServiceFactory
> xMgr
= getProcessServiceFactory();
320 uno::Reference
< com::sun::star::ucb::XSortedDynamicResultSetFactory
> xSRSFac(
321 xMgr
->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SortedDynamicResultSetFactory") ), UNO_QUERY
);
323 Sequence
< com::sun::star::ucb::NumberedSortingInfo
> aSortInfo( 2 );
324 com::sun::star::ucb::NumberedSortingInfo
* pInfo
= aSortInfo
.getArray();
325 pInfo
[ 0 ].ColumnIndex
= 2;
326 pInfo
[ 0 ].Ascending
= sal_False
;
327 pInfo
[ 1 ].ColumnIndex
= 1;
328 pInfo
[ 1 ].Ascending
= sal_True
;
330 uno::Reference
< com::sun::star::ucb::XDynamicResultSet
> xDynamicResultSet
;
332 xSRSFac
->createSortedDynamicResultSet( xDynResultSet
, aSortInfo
, xFactory
);
333 if ( xDynamicResultSet
.is() )
335 xResultSet
= xDynamicResultSet
->getStaticResultSet();
339 catch( ::com::sun::star::ucb::CommandAbortedException
& )
341 // folder not exists?
343 catch( ::com::sun::star::uno::Exception
& )
347 if ( xResultSet
.is() )
349 pFiles
= new StringList_Impl
;
350 uno::Reference
< com::sun::star::ucb::XContentAccess
> xContentAccess( xResultSet
, UNO_QUERY
);
353 while ( xResultSet
->next() )
355 OUString aId
= xContentAccess
->queryContentIdentifierString();
356 OUString
* pFile
= new OUString( aId
);
357 pFiles
->Insert( pFile
, LIST_APPEND
);
360 catch( ::com::sun::star::ucb::CommandAbortedException
& )
363 catch( ::com::sun::star::uno::Exception
& )
368 catch( ::com::sun::star::uno::Exception
& )
374 ULONG nCount
= pFiles
->Count();
375 Sequence
< OUString
> aRet( nCount
);
376 OUString
* pRet
= aRet
.getArray();
377 for ( ULONG i
= 0; i
< nCount
; ++i
)
379 OUString
* pFile
= pFiles
->GetObject(i
);
380 pRet
[i
] = *( pFile
);
387 return Sequence
< OUString
> ();
390 // -----------------------------------------------------------------------
392 Sequence
< OUString
> UCBContentHelper::GetResultSet( const String
& rURL
)
394 StringList_Impl
* pList
= NULL
;
397 Content
aCnt( rURL
, uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>() );
398 uno::Reference
< XResultSet
> xResultSet
;
399 uno::Reference
< com::sun::star::ucb::XDynamicResultSet
> xDynResultSet
;
400 Sequence
< OUString
> aProps(3);
401 OUString
* pProps
= aProps
.getArray();
402 pProps
[0] = OUString::createFromAscii( "Title" );
403 pProps
[1] = OUString::createFromAscii( "ContentType" );
404 // TODO: can be optimized, property never used:
405 pProps
[2] = OUString::createFromAscii( "IsFolder" );
409 xDynResultSet
= aCnt
.createDynamicCursor( aProps
, INCLUDE_FOLDERS_AND_DOCUMENTS
);
410 if ( xDynResultSet
.is() )
411 xResultSet
= xDynResultSet
->getStaticResultSet();
413 catch( ::com::sun::star::ucb::CommandAbortedException
& )
416 catch( ::com::sun::star::uno::Exception
& )
420 if ( xResultSet
.is() )
422 pList
= new StringList_Impl
;
423 uno::Reference
< com::sun::star::sdbc::XRow
> xRow( xResultSet
, UNO_QUERY
);
424 uno::Reference
< com::sun::star::ucb::XContentAccess
> xContentAccess( xResultSet
, UNO_QUERY
);
428 while ( xResultSet
->next() )
430 String
aTitle( xRow
->getString(1) );
431 String
aType( xRow
->getString(2) );
432 String aRow
= aTitle
;
436 aRow
+= String( xContentAccess
->queryContentIdentifierString() );
437 OUString
* pRow
= new OUString( aRow
);
438 pList
->Insert( pRow
, LIST_APPEND
);
441 catch( ::com::sun::star::ucb::CommandAbortedException
& )
444 catch( ::com::sun::star::uno::Exception
& )
449 catch( ::com::sun::star::uno::Exception
& )
455 ULONG nCount
= pList
->Count();
456 Sequence
< OUString
> aRet( nCount
);
457 OUString
* pRet
= aRet
.getArray();
458 for ( ULONG i
= 0; i
< nCount
; ++i
)
460 OUString
* pEntry
= pList
->GetObject(i
);
461 pRet
[i
] = *( pEntry
);
468 return Sequence
< OUString
> ();
471 // -----------------------------------------------------------------------
473 sal_Bool
UCBContentHelper::CopyTo( const String
& rSource
, const String
& rDest
)
475 return Transfer_Impl( rSource
, rDest
, sal_False
, NameClash::ERROR
);
478 // -----------------------------------------------------------------------
480 sal_Bool
UCBContentHelper::MoveTo( const String
& rSource
, const String
& rDest
, sal_Int32 nNameClash
)
482 return Transfer_Impl( rSource
, rDest
, sal_True
, nNameClash
);
485 // -----------------------------------------------------------------------
487 sal_Bool
UCBContentHelper::CanMakeFolder( const String
& rFolder
)
491 Content
aCnt( rFolder
, uno::Reference
< XCommandEnvironment
> () );
492 uno::Reference
< XContentCreator
> xCreator
= uno::Reference
< XContentCreator
>( aCnt
.get(), UNO_QUERY
);
493 if ( !xCreator
.is() )
496 Sequence
< ContentInfo
> aInfo
= xCreator
->queryCreatableContentsInfo();
497 sal_Int32 nCount
= aInfo
.getLength();
501 for ( sal_Int32 i
= 0; i
< nCount
; ++i
)
503 // Simply look for the first KIND_FOLDER...
504 const ContentInfo
& rCurr
= aInfo
[i
];
505 if ( rCurr
.Attributes
& ContentInfoAttribute::KIND_FOLDER
)
509 catch( ::com::sun::star::ucb::CommandAbortedException
& ) {}
510 catch( RuntimeException
& ) {}
511 catch( Exception
& ) {}
516 // -----------------------------------------------------------------------
518 sal_Bool
UCBContentHelper::MakeFolder( const String
& rFolder
, sal_Bool bNewOnly
)
520 INetURLObject
aURL( rFolder
);
521 DBG_ASSERT( aURL
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
522 String aTitle
= aURL
.getName( INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
);
523 aURL
.removeSegment();
526 uno::Reference
< XMultiServiceFactory
> xFactory
= ::comphelper::getProcessServiceFactory();
527 uno::Reference
< XInteractionHandler
> xInteractionHandler
= uno::Reference
< XInteractionHandler
> (
528 xFactory
->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uui.InteractionHandler") ) ), UNO_QUERY
);
529 if ( Content::create( aURL
.GetMainURL( INetURLObject::NO_DECODE
), new CommandEnvironment( xInteractionHandler
, uno::Reference
< XProgressHandler
>() ), aCnt
) )
530 return MakeFolder( aCnt
, aTitle
, aNew
, bNewOnly
);
535 sal_Bool
UCBContentHelper::MakeFolder( Content
& aCnt
, const String
& aTitle
, Content
& rNew
, sal_Bool bNewOnly
)
537 sal_Bool bAlreadyExists
= sal_False
;
541 uno::Reference
< XContentCreator
> xCreator( aCnt
.get(), UNO_QUERY
);
542 if ( !xCreator
.is() )
545 Sequence
< ContentInfo
> aInfo
= xCreator
->queryCreatableContentsInfo();
546 sal_Int32 nCount
= aInfo
.getLength();
550 for ( sal_Int32 i
= 0; i
< nCount
; ++i
)
552 // Simply look for the first KIND_FOLDER...
553 const ContentInfo
& rCurr
= aInfo
[i
];
554 if ( rCurr
.Attributes
& ContentInfoAttribute::KIND_FOLDER
)
556 // Make sure the only required bootstrap property is "Title",
557 const Sequence
< Property
> & rProps
= rCurr
.Properties
;
558 if ( rProps
.getLength() != 1 )
561 if ( !rProps
[ 0 ].Name
.equalsAsciiL(
562 RTL_CONSTASCII_STRINGPARAM( "Title" ) ) )
565 Sequence
<OUString
> aNames(1);
566 OUString
* pNames
= aNames
.getArray();
567 pNames
[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
568 Sequence
<Any
> aValues(1);
569 Any
* pValues
= aValues
.getArray();
570 pValues
[0] = makeAny( OUString( aTitle
) );
572 if ( !aCnt
.insertNewContent( rCurr
.Type
, aNames
, aValues
, rNew
) )
579 catch ( InteractiveIOException
& r
)
581 if ( r
.Code
== IOErrorCode_ALREADY_EXISTING
)
583 bAlreadyExists
= sal_True
;
586 catch ( NameClashException
& )
588 bAlreadyExists
= sal_True
;
590 catch( ::com::sun::star::ucb::CommandAbortedException
& )
593 catch( RuntimeException
& )
600 if( bAlreadyExists
&& !bNewOnly
)
602 INetURLObject
aObj( aCnt
.getURL() );
603 aObj
.Append( aTitle
);
604 rNew
= Content( aObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< XCommandEnvironment
>() );
611 // -----------------------------------------------------------------------
613 sal_Bool
UCBContentHelper::HasParentFolder( const String
& rFolder
)
615 sal_Bool bRet
= sal_False
;
618 Content
aCnt( rFolder
, uno::Reference
< XCommandEnvironment
> () );
619 uno::Reference
< XChild
> xChild( aCnt
.get(), UNO_QUERY
);
622 uno::Reference
< XContent
> xParent( xChild
->getParent(), UNO_QUERY
);
625 String aParentURL
= String( xParent
->getIdentifier()->getContentIdentifier() );
626 bRet
= ( aParentURL
.Len() > 0 && aParentURL
!= rFolder
);
630 catch( ::com::sun::star::ucb::CommandAbortedException
& )
633 catch( ::com::sun::star::uno::Exception
& )
640 // -----------------------------------------------------------------------
642 ULONG
UCBContentHelper::GetSize( const String
& rContent
)
646 INetURLObject
aObj( rContent
);
647 DBG_ASSERT( aObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
650 Content
aCnt( aObj
.GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
651 aCnt
.getPropertyValue( OUString::createFromAscii( "Size" ) ) >>= nTemp
;
653 catch( ::com::sun::star::ucb::CommandAbortedException
& )
656 catch( ::com::sun::star::uno::Exception
& )
659 nSize
= (UINT32
)nTemp
;
663 // -----------------------------------------------------------------------
665 sal_Bool
UCBContentHelper::IsYounger( const String
& rIsYoung
, const String
& rIsOlder
)
667 DateTime aYoungDate
, aOlderDate
;
668 INetURLObject
aYoungObj( rIsYoung
);
669 DBG_ASSERT( aYoungObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
670 INetURLObject
aOlderObj( rIsOlder
);
671 DBG_ASSERT( aOlderObj
.GetProtocol() != INET_PROT_NOT_VALID
, "Invalid URL!" );
674 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> aCmdEnv
;
675 Content
aYoung( aYoungObj
.GetMainURL( INetURLObject::NO_DECODE
), aCmdEnv
);
676 ::com::sun::star::util::DateTime aTempYoungDate
;
677 aYoung
.getPropertyValue( OUString::createFromAscii( "DateModified" ) ) >>= aTempYoungDate
;
678 CONVERT_DATETIME( aTempYoungDate
, aYoungDate
);
679 Content
aOlder( aOlderObj
.GetMainURL( INetURLObject::NO_DECODE
), aCmdEnv
);
680 ::com::sun::star::util::DateTime aTempOlderDate
;
681 aOlder
.getPropertyValue( OUString::createFromAscii( "DateModified" ) ) >>= aTempOlderDate
;
682 CONVERT_DATETIME( aTempOlderDate
, aOlderDate
);
684 catch( ::com::sun::star::ucb::CommandAbortedException
& )
687 catch( ::com::sun::star::uno::Exception
& )
691 return ( aYoungDate
> aOlderDate
);
694 // -----------------------------------------------------------------------
695 sal_Bool
UCBContentHelper::Find( const String
& rFolder
, const String
& rName
, String
& rFile
, BOOL bAllowWildCards
)
697 BOOL bWild
= bAllowWildCards
&& ( rName
.Search( '*' ) != STRING_NOTFOUND
|| rName
.Search( '?' ) != STRING_NOTFOUND
);
699 sal_Bool bRet
= sal_False
;
701 // get a list of URLs for all children of rFolder
702 Sequence
< ::rtl::OUString
> aFiles
= GetFolderContents( rFolder
, sal_False
);
704 const ::rtl::OUString
* pFiles
= aFiles
.getConstArray();
705 UINT32 i
, nCount
= aFiles
.getLength();
706 for ( i
= 0; i
< nCount
; ++i
)
708 // get the last name of the URLs and compare it with rName
709 INetURLObject
aFileObject( pFiles
[i
] );
710 String aFile
= aFileObject
.getName(
711 INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
).toAsciiLowerCase();
712 if ( (bWild
&& WildCard( rName
).Matches( aFile
)) || aFile
== rName
)
715 rFile
= aFileObject
.GetMainURL( INetURLObject::NO_DECODE
);
724 // -----------------------------------------------------------------------
725 sal_Bool
UCBContentHelper::Exists( const String
& rURL
)
728 String sObjectPhysicalName
;
729 sal_Bool bIsLocalFile
= ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL
, sObjectPhysicalName
);
730 // try to create a directory entry for the URL given
733 ::rtl::OUString
sIn( sObjectPhysicalName
), sOut
;
734 if ( osl_File_E_None
== osl_getFileURLFromSystemPath( sIn
.pData
, &sOut
.pData
) )
736 // #106526 osl_getDirectoryItem is an existence check
737 // no further osl_getFileStatus call necessary
739 return (FileBase::E_None
== DirectoryItem::get(sOut
, aItem
));
744 // divide URL into folder and name part
745 sal_Bool bRet
= sal_False
;
746 INetURLObject
aObj( rURL
);
747 ::rtl::OUString aFileName
= aObj
.getName(
748 INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
).toAsciiLowerCase();
749 aObj
.removeSegment();
750 aObj
.removeFinalSlash();
752 // get a list of URLs for all children of rFolder
753 Sequence
< ::rtl::OUString
> aFiles
= GetFolderContents( aObj
.GetMainURL( INetURLObject::NO_DECODE
), sal_True
, sal_False
);
755 const ::rtl::OUString
* pFiles
= aFiles
.getConstArray();
756 UINT32 i
, nCount
= aFiles
.getLength();
757 for ( i
= 0; i
< nCount
; ++i
)
759 // get the last name of the URLs and compare it with rName
760 INetURLObject
aFileObject( pFiles
[i
] );
761 ::rtl::OUString aFile
= aFileObject
.getName(
762 INetURLObject::LAST_SEGMENT
, true, INetURLObject::DECODE_WITH_CHARSET
).toAsciiLowerCase();
763 if ( aFile
== aFileName
)
774 // -----------------------------------------------------------------------
775 sal_Bool
UCBContentHelper::FindInPath( const String
& rPath
, const String
& rName
, String
& rFile
, char cDelim
, BOOL bAllowWildCards
)
777 // extract the single folder names from the path variable and try to find the file in one of these folders
778 USHORT nTokenCount
= rPath
.GetTokenCount( cDelim
);
779 for ( USHORT nToken
= 0; nToken
< nTokenCount
; ++nToken
)
781 String aPath
= rPath
.GetToken( nToken
, cDelim
);
782 if ( Find( aPath
, rName
, rFile
, bAllowWildCards
) )
789 // -----------------------------------------------------------------------
790 sal_Bool
UCBContentHelper::IsSubPath( const ::rtl::OUString
& rPath
, const ::rtl::OUString
& rSubfolderCandidate
, const uno::Reference
< ::com::sun::star::ucb::XContentProvider
>& xProv
)
792 sal_Bool bResult
= sal_False
;
794 uno::Reference
< ::com::sun::star::ucb::XContentProvider
> xContentProvider
= xProv
;
796 // the comparing is done in the following way:
797 // - first compare in case sensitive way
798 // - if name are different try a fallback comparing inf case insensitive way
799 // - if the last comparing succeeded get casepreserving normalized names for the files and compare them
800 // ( the second step is required because retrieving of the normalized names might be very expensive in some cases )
802 INetURLObject
aCandidate( rSubfolderCandidate
);
803 INetURLObject
aCandidateLowCase( rSubfolderCandidate
.toAsciiLowerCase() ); // will be used for case insensitive comparing
804 INetURLObject
aParentFolder( rPath
);
805 INetURLObject
aParentFolderLowCase( rPath
.toAsciiLowerCase() ); // will be used for case insensitive comparing
807 if ( aCandidate
.GetProtocol() == aParentFolder
.GetProtocol() )
809 if ( !xContentProvider
.is() )
811 ::ucbhelper::ContentBroker
* pBroker
= NULL
;
812 if ( aCandidate
.GetProtocol() == INET_PROT_FILE
)
814 pBroker
= ::ucbhelper::ContentBroker::get();
816 xContentProvider
= pBroker
->getContentProviderInterface();
820 INetURLObject aLastTmpObj
;
823 if ( aParentFolder
== aCandidate
)
825 // if case sensitive comparing succeeded there is no need for additional checks
828 else if ( xContentProvider
.is() && aParentFolderLowCase
== aCandidateLowCase
)
830 // the comparing was done using caseinsensitive way
831 // the case sensitive comparing have failed already
832 // the normalized urls must be retrieved
835 uno::Reference
< ::com::sun::star::ucb::XContent
> xSecCont
=
836 xContentProvider
->queryContent(
837 uno::Reference
< ::com::sun::star::ucb::XContentIdentifierFactory
>(
838 xContentProvider
, ::com::sun::star::uno::UNO_QUERY_THROW
)->createContentIdentifier(
839 aParentFolder
.GetMainURL( INetURLObject::NO_DECODE
) ) );
841 uno::Reference
< ::com::sun::star::ucb::XContent
> xLocCont
=
842 xContentProvider
->queryContent(
843 uno::Reference
< ::com::sun::star::ucb::XContentIdentifierFactory
>(
844 xContentProvider
, ::com::sun::star::uno::UNO_QUERY_THROW
)->createContentIdentifier(
845 aCandidate
.GetMainURL( INetURLObject::NO_DECODE
) ) );
847 if ( !xSecCont
.is() || !xLocCont
.is() )
848 throw ::com::sun::star::uno::RuntimeException();
850 ::rtl::OUString aSecNormStr
;
851 ::rtl::OUString aLocNormStr
;
854 ( ( uno::Reference
< ::com::sun::star::ucb::XCommandProcessor
>(
855 xSecCont
, ::com::sun::star::uno::UNO_QUERY_THROW
)->execute(
856 ::com::sun::star::ucb::Command(
857 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getCasePreservingURL" ) ),
859 ::com::sun::star::uno::Any() ),
861 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>() )
863 && ( uno::Reference
< ::com::sun::star::ucb::XCommandProcessor
>(
864 xLocCont
, ::com::sun::star::uno::UNO_QUERY_THROW
)->execute(
865 ::com::sun::star::ucb::Command(
866 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getCasePreservingURL" ) ),
868 ::com::sun::star::uno::Any() ),
870 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>() )
872 && aLocNormStr
.equals( aSecNormStr
) );
874 catch( ::com::sun::star::uno::Exception
& )
878 // INetURLObject::removeSegment sometimes return true without exchanging URL,
879 // for example in case of "file:///"
880 aLastTmpObj
= aCandidate
;
882 } while( aCandidate
.removeSegment() && aCandidateLowCase
.removeSegment() && aCandidate
!= aLastTmpObj
&& !bResult
);