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: localfilehelper.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"
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/ucb/XContentAccess.hpp>
35 #include <com/sun/star/ucb/CommandAbortedException.hpp>
37 #include <unotools/localfilehelper.hxx>
38 #include <ucbhelper/fileidentifierconverter.hxx>
39 #include <ucbhelper/contentbroker.hxx>
40 #include <rtl/ustring.hxx>
41 #include <osl/file.hxx>
42 #include <tools/debug.hxx>
43 #include <tools/list.hxx>
44 #include <tools/urlobj.hxx>
45 #include <ucbhelper/content.hxx>
47 using namespace ::osl
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::ucb
;
54 sal_Bool
LocalFileHelper::ConvertSystemPathToURL( const String
& rName
, const String
& rBaseURL
, String
& rReturn
)
56 rReturn
= ::rtl::OUString();
58 ::ucbhelper::ContentBroker
* pBroker
= ::ucbhelper::ContentBroker::get();
62 if ( FileBase::getFileURLFromSystemPath( rName
, aRet
) == FileBase::E_None
)
67 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContentProviderManager
> xManager
=
68 pBroker
->getContentProviderManagerInterface();
71 rReturn
= ::ucbhelper::getFileURLFromSystemPath( xManager
, rBaseURL
, rName
);
73 catch ( ::com::sun::star::uno::RuntimeException
& )
79 return ( rReturn
.Len() != 0 );
82 sal_Bool
LocalFileHelper::ConvertURLToSystemPath( const String
& rName
, String
& rReturn
)
84 rReturn
= ::rtl::OUString();
85 ::ucbhelper::ContentBroker
* pBroker
= ::ucbhelper::ContentBroker::get();
89 if( FileBase::getSystemPathFromFileURL( rName
, aRet
) == FileBase::E_None
)
94 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContentProviderManager
> xManager
=
95 pBroker
->getContentProviderManagerInterface();
98 rReturn
= ::ucbhelper::getSystemPathFromFileURL( xManager
, rName
);
100 catch ( ::com::sun::star::uno::RuntimeException
& )
105 return ( rReturn
.Len() != 0 );
108 sal_Bool
LocalFileHelper::ConvertPhysicalNameToURL( const String
& rName
, String
& rReturn
)
110 rReturn
= ::rtl::OUString();
111 ::ucbhelper::ContentBroker
* pBroker
= ::ucbhelper::ContentBroker::get();
115 if ( FileBase::getFileURLFromSystemPath( rName
, aRet
) == FileBase::E_None
)
120 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContentProviderManager
> xManager
=
121 pBroker
->getContentProviderManagerInterface();
125 rtl::OUString
aBase( ::ucbhelper::getLocalFileURL( xManager
) );
126 rReturn
= ::ucbhelper::getFileURLFromSystemPath( xManager
, aBase
, rName
);
128 catch ( ::com::sun::star::uno::RuntimeException
& )
133 return ( rReturn
.Len() != 0 );
136 sal_Bool
LocalFileHelper::ConvertURLToPhysicalName( const String
& rName
, String
& rReturn
)
138 rReturn
= ::rtl::OUString();
139 ::ucbhelper::ContentBroker
* pBroker
= ::ucbhelper::ContentBroker::get();
142 ::rtl::OUString aRet
;
143 if ( FileBase::getSystemPathFromFileURL( rName
, aRet
) == FileBase::E_None
)
148 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContentProviderManager
> xManager
=
149 pBroker
->getContentProviderManagerInterface();
152 INetURLObject
aObj( rName
);
153 INetURLObject
aLocal( ::ucbhelper::getLocalFileURL( xManager
) );
154 if ( aObj
.GetProtocol() == aLocal
.GetProtocol() )
155 rReturn
= ::ucbhelper::getSystemPathFromFileURL( xManager
, rName
);
157 catch ( ::com::sun::star::uno::RuntimeException
& )
162 return ( rReturn
.Len() != 0 );
165 sal_Bool
LocalFileHelper::IsLocalFile( const String
& rName
)
168 return ConvertURLToPhysicalName( rName
, aTmp
);
171 sal_Bool
LocalFileHelper::IsFileContent( const String
& rName
)
174 return ConvertURLToSystemPath( rName
, aTmp
);
177 DECLARE_LIST( StringList_Impl
, ::rtl::OUString
* )
179 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> LocalFileHelper::GetFolderContents( const ::rtl::OUString
& rFolder
, sal_Bool bFolder
)
181 StringList_Impl
* pFiles
= NULL
;
184 ::ucbhelper::Content
aCnt( rFolder
, Reference
< XCommandEnvironment
> () );
185 Reference
< ::com::sun::star::sdbc::XResultSet
> xResultSet
;
186 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aProps(1);
187 ::rtl::OUString
* pProps
= aProps
.getArray();
188 pProps
[0] = ::rtl::OUString::createFromAscii( "Url" );
192 ::ucbhelper::ResultSetInclude eInclude
= bFolder
? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS
: ::ucbhelper::INCLUDE_DOCUMENTS_ONLY
;
193 xResultSet
= aCnt
.createCursor( aProps
, eInclude
);
195 catch( ::com::sun::star::ucb::CommandAbortedException
& )
202 if ( xResultSet
.is() )
204 pFiles
= new StringList_Impl
;
205 Reference
< XContentAccess
> xContentAccess( xResultSet
, UNO_QUERY
);
208 while ( xResultSet
->next() )
210 ::rtl::OUString aId
= xContentAccess
->queryContentIdentifierString();
211 ::rtl::OUString
* pFile
= new ::rtl::OUString( aId
);
212 pFiles
->Insert( pFile
, LIST_APPEND
);
215 catch( ::com::sun::star::ucb::CommandAbortedException
& )
229 ULONG nCount
= pFiles
->Count();
230 Sequence
< ::rtl::OUString
> aRet( nCount
);
231 ::rtl::OUString
* pRet
= aRet
.getArray();
232 for ( USHORT i
= 0; i
< nCount
; ++i
)
234 ::rtl::OUString
* pFile
= pFiles
->GetObject(i
);
235 pRet
[i
] = *( pFile
);
242 return Sequence
< ::rtl::OUString
> ();