1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_unotools.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/ucb/XContentAccess.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
35*cdf0e10cSrcweir #include <ucbhelper/fileidentifierconverter.hxx>
36*cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
37*cdf0e10cSrcweir #include <rtl/ustring.hxx>
38*cdf0e10cSrcweir #include <osl/file.hxx>
39*cdf0e10cSrcweir #include <tools/debug.hxx>
40*cdf0e10cSrcweir #include <tools/list.hxx>
41*cdf0e10cSrcweir #include <tools/urlobj.hxx>
42*cdf0e10cSrcweir #include <ucbhelper/content.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace ::osl;
45*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46*cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace utl
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertSystemPathToURL( const String& rName, const String& rBaseURL, String& rReturn )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
56*cdf0e10cSrcweir     if ( !pBroker )
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir     	rtl::OUString aRet;
59*cdf0e10cSrcweir         if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
60*cdf0e10cSrcweir 			rReturn = aRet;
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir     else
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
65*cdf0e10cSrcweir                 pBroker->getContentProviderManagerInterface();
66*cdf0e10cSrcweir         try
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             rReturn = ::ucbhelper::getFileURLFromSystemPath( xManager, rBaseURL, rName );
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
71*cdf0e10cSrcweir         {
72*cdf0e10cSrcweir             return sal_False;
73*cdf0e10cSrcweir         }
74*cdf0e10cSrcweir     }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertURLToSystemPath( const String& rName, String& rReturn )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
82*cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
83*cdf0e10cSrcweir     if ( !pBroker )
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir     	rtl::OUString aRet;
86*cdf0e10cSrcweir         if( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
87*cdf0e10cSrcweir 			rReturn = aRet;
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir     else
90*cdf0e10cSrcweir     {
91*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
92*cdf0e10cSrcweir                 pBroker->getContentProviderManagerInterface();
93*cdf0e10cSrcweir         try
94*cdf0e10cSrcweir         {
95*cdf0e10cSrcweir             rReturn = ::ucbhelper::getSystemPathFromFileURL( xManager, rName );
96*cdf0e10cSrcweir         }
97*cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
98*cdf0e10cSrcweir         {
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertPhysicalNameToURL( const String& rName, String& rReturn )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
108*cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
109*cdf0e10cSrcweir     if ( !pBroker )
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir     	rtl::OUString aRet;
112*cdf0e10cSrcweir         if ( FileBase::getFileURLFromSystemPath( rName, aRet ) == FileBase::E_None )
113*cdf0e10cSrcweir 			rReturn = aRet;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir     else
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
118*cdf0e10cSrcweir                 pBroker->getContentProviderManagerInterface();
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 		try
121*cdf0e10cSrcweir 		{
122*cdf0e10cSrcweir             rtl::OUString aBase( ::ucbhelper::getLocalFileURL( xManager ) );
123*cdf0e10cSrcweir             rReturn = ::ucbhelper::getFileURLFromSystemPath( xManager, aBase, rName );
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir sal_Bool LocalFileHelper::ConvertURLToPhysicalName( const String& rName, String& rReturn )
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir 	rReturn = ::rtl::OUString();
136*cdf0e10cSrcweir     ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
137*cdf0e10cSrcweir     if ( !pBroker )
138*cdf0e10cSrcweir     {
139*cdf0e10cSrcweir 		::rtl::OUString aRet;
140*cdf0e10cSrcweir         if ( FileBase::getSystemPathFromFileURL( rName, aRet ) == FileBase::E_None )
141*cdf0e10cSrcweir 			rReturn = aRet;
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir     else
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentProviderManager > xManager =
146*cdf0e10cSrcweir 				pBroker->getContentProviderManagerInterface();
147*cdf0e10cSrcweir         try
148*cdf0e10cSrcweir         {
149*cdf0e10cSrcweir 			INetURLObject aObj( rName );
150*cdf0e10cSrcweir 			INetURLObject aLocal( ::ucbhelper::getLocalFileURL( xManager ) );
151*cdf0e10cSrcweir 			if ( aObj.GetProtocol() == aLocal.GetProtocol() )
152*cdf0e10cSrcweir             	rReturn = ::ucbhelper::getSystemPathFromFileURL( xManager, rName );
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir         catch ( ::com::sun::star::uno::RuntimeException& )
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     return ( rReturn.Len() != 0 );
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir sal_Bool LocalFileHelper::IsLocalFile( const String& rName )
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir     String aTmp;
165*cdf0e10cSrcweir     return ConvertURLToPhysicalName( rName, aTmp );
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir sal_Bool LocalFileHelper::IsFileContent( const String& rName )
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir     String aTmp;
171*cdf0e10cSrcweir     return ConvertURLToSystemPath( rName, aTmp );
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir DECLARE_LIST( StringList_Impl, ::rtl::OUString* )
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir ::com::sun::star::uno::Sequence < ::rtl::OUString > LocalFileHelper::GetFolderContents( const ::rtl::OUString& rFolder, sal_Bool bFolder )
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	StringList_Impl* pFiles = NULL;
179*cdf0e10cSrcweir 	try
180*cdf0e10cSrcweir 	{
181*cdf0e10cSrcweir         ::ucbhelper::Content aCnt( rFolder, Reference< XCommandEnvironment > () );
182*cdf0e10cSrcweir         Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
183*cdf0e10cSrcweir         ::com::sun::star::uno::Sequence< ::rtl::OUString > aProps(1);
184*cdf0e10cSrcweir         ::rtl::OUString* pProps = aProps.getArray();
185*cdf0e10cSrcweir         pProps[0] = ::rtl::OUString::createFromAscii( "Url" );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 		try
188*cdf0e10cSrcweir 		{
189*cdf0e10cSrcweir             ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
190*cdf0e10cSrcweir             xResultSet = aCnt.createCursor( aProps, eInclude );
191*cdf0e10cSrcweir 		}
192*cdf0e10cSrcweir 		catch( ::com::sun::star::ucb::CommandAbortedException& )
193*cdf0e10cSrcweir 		{
194*cdf0e10cSrcweir 		}
195*cdf0e10cSrcweir         catch( Exception& )
196*cdf0e10cSrcweir 		{
197*cdf0e10cSrcweir 		}
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		if ( xResultSet.is() )
200*cdf0e10cSrcweir 		{
201*cdf0e10cSrcweir 			pFiles = new StringList_Impl;
202*cdf0e10cSrcweir             Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
203*cdf0e10cSrcweir 			try
204*cdf0e10cSrcweir 			{
205*cdf0e10cSrcweir 				while ( xResultSet->next() )
206*cdf0e10cSrcweir 				{
207*cdf0e10cSrcweir                     ::rtl::OUString aId = xContentAccess->queryContentIdentifierString();
208*cdf0e10cSrcweir                     ::rtl::OUString* pFile = new ::rtl::OUString( aId );
209*cdf0e10cSrcweir 					pFiles->Insert( pFile, LIST_APPEND );
210*cdf0e10cSrcweir 				}
211*cdf0e10cSrcweir 			}
212*cdf0e10cSrcweir 			catch( ::com::sun::star::ucb::CommandAbortedException& )
213*cdf0e10cSrcweir 			{
214*cdf0e10cSrcweir 			}
215*cdf0e10cSrcweir             catch( Exception& )
216*cdf0e10cSrcweir 			{
217*cdf0e10cSrcweir 			}
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 	}
220*cdf0e10cSrcweir     catch( Exception& )
221*cdf0e10cSrcweir 	{
222*cdf0e10cSrcweir 	}
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 	if ( pFiles )
225*cdf0e10cSrcweir 	{
226*cdf0e10cSrcweir         sal_uLong nCount = pFiles->Count();
227*cdf0e10cSrcweir         Sequence < ::rtl::OUString > aRet( nCount );
228*cdf0e10cSrcweir         ::rtl::OUString* pRet = aRet.getArray();
229*cdf0e10cSrcweir 		for ( sal_uInt16 i = 0; i < nCount; ++i )
230*cdf0e10cSrcweir 		{
231*cdf0e10cSrcweir             ::rtl::OUString* pFile = pFiles->GetObject(i);
232*cdf0e10cSrcweir 			pRet[i] = *( pFile );
233*cdf0e10cSrcweir 			delete pFile;
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 		delete pFiles;
236*cdf0e10cSrcweir 		return aRet;
237*cdf0e10cSrcweir 	}
238*cdf0e10cSrcweir 	else
239*cdf0e10cSrcweir         return Sequence < ::rtl::OUString > ();
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir }
243