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_unotools.hxx"
26 #include <com/sun/star/sdbc/XResultSet.hpp>
27 #include <com/sun/star/ucb/XContentAccess.hpp>
28 #include <com/sun/star/ucb/CommandAbortedException.hpp>
29 
30 #include <unotools/localfilehelper.hxx>
31 #include <ucbhelper/fileidentifierconverter.hxx>
32 #include <ucbhelper/contentbroker.hxx>
33 #include <rtl/ustring.hxx>
34 #include <osl/file.hxx>
35 #include <tools/debug.hxx>
36 #include <tools/list.hxx>
37 #include <tools/urlobj.hxx>
38 #include <ucbhelper/content.hxx>
39 
40 using namespace ::osl;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::ucb;
43 
44 namespace utl
45 {
46 
ConvertSystemPathToURL(const String & rName,const String & rBaseURL,String & rReturn)47 sal_Bool LocalFileHelper::ConvertSystemPathToURL( const String& rName, const String& rBaseURL, String& rReturn )
48 {
49 	rReturn = ::rtl::OUString();
50 
51     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
52     if ( !pBroker )
53     {
54     	rtl::OUString aRet;
55         if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
56 			rReturn = aRet;
57     }
58     else
59     {
60         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
61                 pBroker->getContentProviderManagerInterface();
62         try
63         {
64             rReturn = ::ucbhelper::getFileURLFromSystemPath( xManager, rBaseURL, rName );
65         }
66         catch ( ::com::sun::star::uno::RuntimeException& )
67         {
68             return sal_False;
69         }
70     }
71 
72     return ( rReturn.Len() != 0 );
73 }
74 
ConvertURLToSystemPath(const String & rName,String & rReturn)75 sal_Bool LocalFileHelper::ConvertURLToSystemPath( const String& rName, String& rReturn )
76 {
77 	rReturn = ::rtl::OUString();
78     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
79     if ( !pBroker )
80     {
81     	rtl::OUString aRet;
82         if( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
83 			rReturn = aRet;
84     }
85     else
86     {
87         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
88                 pBroker->getContentProviderManagerInterface();
89         try
90         {
91             rReturn = ::ucbhelper::getSystemPathFromFileURL( xManager, rName );
92         }
93         catch ( ::com::sun::star::uno::RuntimeException& )
94         {
95         }
96     }
97 
98     return ( rReturn.Len() != 0 );
99 }
100 
ConvertPhysicalNameToURL(const String & rName,String & rReturn)101 sal_Bool LocalFileHelper::ConvertPhysicalNameToURL( const String& rName, String& rReturn )
102 {
103 	rReturn = ::rtl::OUString();
104     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
105     if ( !pBroker )
106     {
107     	rtl::OUString aRet;
108         if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
109 			rReturn = aRet;
110     }
111     else
112     {
113         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
114                 pBroker->getContentProviderManagerInterface();
115 
116 		try
117 		{
118             rtl::OUString aBase( ::ucbhelper::getLocalFileURL( xManager ) );
119             rReturn = ::ucbhelper::getFileURLFromSystemPath( xManager, aBase, rName );
120         }
121         catch ( ::com::sun::star::uno::RuntimeException& )
122         {
123         }
124     }
125 
126     return ( rReturn.Len() != 0 );
127 }
128 
ConvertURLToPhysicalName(const String & rName,String & rReturn)129 sal_Bool LocalFileHelper::ConvertURLToPhysicalName( const String& rName, String& rReturn )
130 {
131 	rReturn = ::rtl::OUString();
132     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
133     if ( !pBroker )
134     {
135 		::rtl::OUString aRet;
136         if ( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
137 			rReturn = aRet;
138     }
139     else
140     {
141         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
142 				pBroker->getContentProviderManagerInterface();
143         try
144         {
145 			INetURLObject aObj( rName );
146 			INetURLObject aLocal( ::ucbhelper::getLocalFileURL( xManager ) );
147 			if ( aObj.GetProtocol() == aLocal.GetProtocol() )
148             	rReturn = ::ucbhelper::getSystemPathFromFileURL( xManager, rName );
149         }
150         catch ( ::com::sun::star::uno::RuntimeException& )
151         {
152         }
153     }
154 
155     return ( rReturn.Len() != 0 );
156 }
157 
IsLocalFile(const String & rName)158 sal_Bool LocalFileHelper::IsLocalFile( const String& rName )
159 {
160     String aTmp;
161     return ConvertURLToPhysicalName( rName, aTmp );
162 }
163 
IsFileContent(const String & rName)164 sal_Bool LocalFileHelper::IsFileContent( const String& rName )
165 {
166     String aTmp;
167     return ConvertURLToSystemPath( rName, aTmp );
168 }
169 
DECLARE_LIST(StringList_Impl,::rtl::OUString *)170 DECLARE_LIST( StringList_Impl, ::rtl::OUString* )
171 
172 ::com::sun::star::uno::Sequence < ::rtl::OUString > LocalFileHelper::GetFolderContents( const ::rtl::OUString& rFolder, sal_Bool bFolder )
173 {
174 	StringList_Impl* pFiles = NULL;
175 	try
176 	{
177         ::ucbhelper::Content aCnt( rFolder, Reference< XCommandEnvironment > () );
178         Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
179         ::com::sun::star::uno::Sequence< ::rtl::OUString > aProps(1);
180         ::rtl::OUString* pProps = aProps.getArray();
181         pProps[0] = ::rtl::OUString::createFromAscii( "Url" );
182 
183 		try
184 		{
185             ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
186             xResultSet = aCnt.createCursor( aProps, eInclude );
187 		}
188 		catch( ::com::sun::star::ucb::CommandAbortedException& )
189 		{
190 		}
191         catch( Exception& )
192 		{
193 		}
194 
195 		if ( xResultSet.is() )
196 		{
197 			pFiles = new StringList_Impl;
198             Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
199 			try
200 			{
201 				while ( xResultSet->next() )
202 				{
203                     ::rtl::OUString aId = xContentAccess->queryContentIdentifierString();
204                     ::rtl::OUString* pFile = new ::rtl::OUString( aId );
205 					pFiles->Insert( pFile, LIST_APPEND );
206 				}
207 			}
208 			catch( ::com::sun::star::ucb::CommandAbortedException& )
209 			{
210 			}
211             catch( Exception& )
212 			{
213 			}
214 		}
215 	}
216     catch( Exception& )
217 	{
218 	}
219 
220 	if ( pFiles )
221 	{
222         sal_uLong nCount = pFiles->Count();
223         Sequence < ::rtl::OUString > aRet( nCount );
224         ::rtl::OUString* pRet = aRet.getArray();
225 		for ( sal_uInt16 i = 0; i < nCount; ++i )
226 		{
227             ::rtl::OUString* pFile = pFiles->GetObject(i);
228 			pRet[i] = *( pFile );
229 			delete pFile;
230 		}
231 		delete pFiles;
232 		return aRet;
233 	}
234 	else
235         return Sequence < ::rtl::OUString > ();
236 }
237 
238 }
239