1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b5088357SAndrew Rist  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b5088357SAndrew Rist  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19*b5088357SAndrew Rist  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
27cdf0e10cSrcweir #include <com/sun/star/ucb/XContentAccess.hpp>
28cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
31cdf0e10cSrcweir #include <ucbhelper/fileidentifierconverter.hxx>
32cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
33cdf0e10cSrcweir #include <rtl/ustring.hxx>
34cdf0e10cSrcweir #include <osl/file.hxx>
35cdf0e10cSrcweir #include <tools/debug.hxx>
36cdf0e10cSrcweir #include <tools/list.hxx>
37cdf0e10cSrcweir #include <tools/urlobj.hxx>
38cdf0e10cSrcweir #include <ucbhelper/content.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::osl;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace utl
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 
ConvertSystemPathToURL(const String & rName,const String & rBaseURL,String & rReturn)47cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertSystemPathToURL( const String& rName, const String& rBaseURL, String& rReturn )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
52cdf0e10cSrcweir     if ( !pBroker )
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir     	rtl::OUString aRet;
55cdf0e10cSrcweir         if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
56cdf0e10cSrcweir 			rReturn = aRet;
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir     else
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
61cdf0e10cSrcweir                 pBroker->getContentProviderManagerInterface();
62cdf0e10cSrcweir         try
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             rReturn = ::ucbhelper::getFileURLFromSystemPath( xManager, rBaseURL, rName );
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
67cdf0e10cSrcweir         {
68cdf0e10cSrcweir             return sal_False;
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
ConvertURLToSystemPath(const String & rName,String & rReturn)75cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertURLToSystemPath( const String& rName, String& rReturn )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
78cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
79cdf0e10cSrcweir     if ( !pBroker )
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir     	rtl::OUString aRet;
82cdf0e10cSrcweir         if( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
83cdf0e10cSrcweir 			rReturn = aRet;
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir     else
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
88cdf0e10cSrcweir                 pBroker->getContentProviderManagerInterface();
89cdf0e10cSrcweir         try
90cdf0e10cSrcweir         {
91cdf0e10cSrcweir             rReturn = ::ucbhelper::getSystemPathFromFileURL( xManager, rName );
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
ConvertPhysicalNameToURL(const String & rName,String & rReturn)101cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertPhysicalNameToURL( const String& rName, String& rReturn )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
104cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
105cdf0e10cSrcweir     if ( !pBroker )
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir     	rtl::OUString aRet;
108cdf0e10cSrcweir         if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
109cdf0e10cSrcweir 			rReturn = aRet;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     else
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
114cdf0e10cSrcweir                 pBroker->getContentProviderManagerInterface();
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		try
117cdf0e10cSrcweir 		{
118cdf0e10cSrcweir             rtl::OUString aBase( ::ucbhelper::getLocalFileURL( xManager ) );
119cdf0e10cSrcweir             rReturn = ::ucbhelper::getFileURLFromSystemPath( xManager, aBase, rName );
120cdf0e10cSrcweir         }
121cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
122cdf0e10cSrcweir         {
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
ConvertURLToPhysicalName(const String & rName,String & rReturn)129cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertURLToPhysicalName( const String& rName, String& rReturn )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
132cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
133cdf0e10cSrcweir     if ( !pBroker )
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir 		::rtl::OUString aRet;
136cdf0e10cSrcweir         if ( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
137cdf0e10cSrcweir 			rReturn = aRet;
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir     else
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
142cdf0e10cSrcweir 				pBroker->getContentProviderManagerInterface();
143cdf0e10cSrcweir         try
144cdf0e10cSrcweir         {
145cdf0e10cSrcweir 			INetURLObject aObj( rName );
146cdf0e10cSrcweir 			INetURLObject aLocal( ::ucbhelper::getLocalFileURL( xManager ) );
147cdf0e10cSrcweir 			if ( aObj.GetProtocol() == aLocal.GetProtocol() )
148cdf0e10cSrcweir             	rReturn = ::ucbhelper::getSystemPathFromFileURL( xManager, rName );
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
IsLocalFile(const String & rName)158cdf0e10cSrcweir sal_Bool LocalFileHelper::IsLocalFile( const String& rName )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     String aTmp;
161cdf0e10cSrcweir     return ConvertURLToPhysicalName( rName, aTmp );
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
IsFileContent(const String & rName)164cdf0e10cSrcweir sal_Bool LocalFileHelper::IsFileContent( const String& rName )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     String aTmp;
167cdf0e10cSrcweir     return ConvertURLToSystemPath( rName, aTmp );
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
DECLARE_LIST(StringList_Impl,::rtl::OUString *)170cdf0e10cSrcweir DECLARE_LIST( StringList_Impl, ::rtl::OUString* )
171cdf0e10cSrcweir 
172cdf0e10cSrcweir ::com::sun::star::uno::Sequence < ::rtl::OUString > LocalFileHelper::GetFolderContents( const ::rtl::OUString& rFolder, sal_Bool bFolder )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir 	StringList_Impl* pFiles = NULL;
175cdf0e10cSrcweir 	try
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir         ::ucbhelper::Content aCnt( rFolder, Reference< XCommandEnvironment > () );
178cdf0e10cSrcweir         Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
179cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< ::rtl::OUString > aProps(1);
180cdf0e10cSrcweir         ::rtl::OUString* pProps = aProps.getArray();
181cdf0e10cSrcweir         pProps[0] = ::rtl::OUString::createFromAscii( "Url" );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		try
184cdf0e10cSrcweir 		{
185cdf0e10cSrcweir             ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
186cdf0e10cSrcweir             xResultSet = aCnt.createCursor( aProps, eInclude );
187cdf0e10cSrcweir 		}
188cdf0e10cSrcweir 		catch( ::com::sun::star::ucb::CommandAbortedException& )
189cdf0e10cSrcweir 		{
190cdf0e10cSrcweir 		}
191cdf0e10cSrcweir         catch( Exception& )
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 		if ( xResultSet.is() )
196cdf0e10cSrcweir 		{
197cdf0e10cSrcweir 			pFiles = new StringList_Impl;
198cdf0e10cSrcweir             Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
199cdf0e10cSrcweir 			try
200cdf0e10cSrcweir 			{
201cdf0e10cSrcweir 				while ( xResultSet->next() )
202cdf0e10cSrcweir 				{
203cdf0e10cSrcweir                     ::rtl::OUString aId = xContentAccess->queryContentIdentifierString();
204cdf0e10cSrcweir                     ::rtl::OUString* pFile = new ::rtl::OUString( aId );
205cdf0e10cSrcweir 					pFiles->Insert( pFile, LIST_APPEND );
206cdf0e10cSrcweir 				}
207cdf0e10cSrcweir 			}
208cdf0e10cSrcweir 			catch( ::com::sun::star::ucb::CommandAbortedException& )
209cdf0e10cSrcweir 			{
210cdf0e10cSrcweir 			}
211cdf0e10cSrcweir             catch( Exception& )
212cdf0e10cSrcweir 			{
213cdf0e10cSrcweir 			}
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir     catch( Exception& )
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 	}
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 	if ( pFiles )
221cdf0e10cSrcweir 	{
222cdf0e10cSrcweir         sal_uLong nCount = pFiles->Count();
223cdf0e10cSrcweir         Sequence < ::rtl::OUString > aRet( nCount );
224cdf0e10cSrcweir         ::rtl::OUString* pRet = aRet.getArray();
225cdf0e10cSrcweir 		for ( sal_uInt16 i = 0; i < nCount; ++i )
226cdf0e10cSrcweir 		{
227cdf0e10cSrcweir             ::rtl::OUString* pFile = pFiles->GetObject(i);
228cdf0e10cSrcweir 			pRet[i] = *( pFile );
229cdf0e10cSrcweir 			delete pFile;
230cdf0e10cSrcweir 		}
231cdf0e10cSrcweir 		delete pFiles;
232cdf0e10cSrcweir 		return aRet;
233cdf0e10cSrcweir 	}
234cdf0e10cSrcweir 	else
235cdf0e10cSrcweir         return Sequence < ::rtl::OUString > ();
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir }
239