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