bump product version to 4.1.6.2
[LibreOffice.git] / unotools / source / ucbhelper / localfilehelper.cxx
blob25b2adfdbfbf42b4c104e1b204fe07fe15bb2dc5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/sdbc/XResultSet.hpp>
21 #include <com/sun/star/ucb/XContentAccess.hpp>
22 #include <com/sun/star/ucb/CommandAbortedException.hpp>
23 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
24 #include <comphelper/processfactory.hxx>
25 #include <unotools/localfilehelper.hxx>
26 #include <ucbhelper/fileidentifierconverter.hxx>
27 #include <rtl/ustring.hxx>
28 #include <osl/file.hxx>
29 #include <tools/urlobj.hxx>
30 #include <ucbhelper/content.hxx>
31 #include <vector>
33 using namespace ::osl;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::ucb;
37 namespace utl
40 sal_Bool LocalFileHelper::ConvertSystemPathToURL( const OUString& rName, const OUString& rBaseURL, OUString& rReturn )
42 rReturn = "";
44 Reference< XUniversalContentBroker > pBroker(
45 UniversalContentBroker::create(
46 comphelper::getProcessComponentContext() ) );
47 try
49 rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, rBaseURL, rName );
51 catch ( ::com::sun::star::uno::RuntimeException& )
53 return sal_False;
56 return !rReturn.isEmpty();
59 sal_Bool LocalFileHelper::ConvertURLToSystemPath( const OUString& rName, OUString& rReturn )
61 rReturn = "";
62 Reference< XUniversalContentBroker > pBroker(
63 UniversalContentBroker::create(
64 comphelper::getProcessComponentContext() ) );
65 try
67 rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
69 catch ( ::com::sun::star::uno::RuntimeException& )
73 return !rReturn.isEmpty();
76 bool LocalFileHelper::ConvertPhysicalNameToURL(const OUString& rName, OUString& rReturn)
78 rReturn = OUString();
79 Reference< XUniversalContentBroker > pBroker(
80 UniversalContentBroker::create(
81 comphelper::getProcessComponentContext() ) );
82 try
84 OUString aBase( ::ucbhelper::getLocalFileURL() );
85 rReturn = ::ucbhelper::getFileURLFromSystemPath( pBroker, aBase, rName );
87 catch (const ::com::sun::star::uno::RuntimeException&)
91 return !rReturn.isEmpty();
94 bool LocalFileHelper::ConvertURLToPhysicalName(const OUString& rName, OUString& rReturn)
96 rReturn = "";
97 Reference< XUniversalContentBroker > pBroker(
98 UniversalContentBroker::create(
99 comphelper::getProcessComponentContext() ) );
102 INetURLObject aObj( rName );
103 INetURLObject aLocal( ::ucbhelper::getLocalFileURL() );
104 if ( aObj.GetProtocol() == aLocal.GetProtocol() )
105 rReturn = ::ucbhelper::getSystemPathFromFileURL( pBroker, rName );
107 catch (const ::com::sun::star::uno::RuntimeException&)
111 return !rReturn.isEmpty();
114 sal_Bool LocalFileHelper::IsLocalFile(const OUString& rName)
116 OUString aTmp;
117 return ConvertURLToPhysicalName(rName, aTmp);
120 sal_Bool LocalFileHelper::IsFileContent(const OUString& rName)
122 OUString aTmp;
123 return ConvertURLToSystemPath(rName, aTmp);
126 typedef ::std::vector< OUString* > StringList_Impl;
128 ::com::sun::star::uno::Sequence < OUString > LocalFileHelper::GetFolderContents( const OUString& rFolder, sal_Bool bFolder )
130 StringList_Impl* pFiles = NULL;
133 ::ucbhelper::Content aCnt(
134 rFolder, Reference< XCommandEnvironment >(),
135 comphelper::getProcessComponentContext() );
136 Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
137 ::com::sun::star::uno::Sequence< OUString > aProps(1);
138 OUString* pProps = aProps.getArray();
139 pProps[0] = "Url";
143 ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY;
144 xResultSet = aCnt.createCursor( aProps, eInclude );
146 catch( ::com::sun::star::ucb::CommandAbortedException& )
149 catch( Exception& )
153 if ( xResultSet.is() )
155 pFiles = new StringList_Impl;
156 Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
159 while ( xResultSet->next() )
161 OUString aId = xContentAccess->queryContentIdentifierString();
162 OUString* pFile = new OUString( aId );
163 pFiles->push_back( pFile );
166 catch( ::com::sun::star::ucb::CommandAbortedException& )
169 catch( Exception& )
174 catch( Exception& )
178 if ( pFiles )
180 size_t nCount = pFiles->size();
181 Sequence < OUString > aRet( nCount );
182 OUString* pRet = aRet.getArray();
183 for ( size_t i = 0; i < nCount; ++i )
185 OUString* pFile = (*pFiles)[ i ];
186 pRet[i] = *( pFile );
187 delete pFile;
189 delete pFiles;
190 return aRet;
192 else
193 return Sequence < OUString > ();
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */