1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 using namespace ::osl
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::ucb
;
40 sal_Bool
LocalFileHelper::ConvertSystemPathToURL( const OUString
& rName
, const OUString
& rBaseURL
, OUString
& rReturn
)
44 Reference
< XUniversalContentBroker
> pBroker(
45 UniversalContentBroker::create(
46 comphelper::getProcessComponentContext() ) );
49 rReturn
= ::ucbhelper::getFileURLFromSystemPath( pBroker
, rBaseURL
, rName
);
51 catch ( ::com::sun::star::uno::RuntimeException
& )
56 return !rReturn
.isEmpty();
59 sal_Bool
LocalFileHelper::ConvertURLToSystemPath( const OUString
& rName
, OUString
& rReturn
)
62 Reference
< XUniversalContentBroker
> pBroker(
63 UniversalContentBroker::create(
64 comphelper::getProcessComponentContext() ) );
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
)
79 Reference
< XUniversalContentBroker
> pBroker(
80 UniversalContentBroker::create(
81 comphelper::getProcessComponentContext() ) );
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
)
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
)
117 return ConvertURLToPhysicalName(rName
, aTmp
);
120 sal_Bool
LocalFileHelper::IsFileContent(const OUString
& rName
)
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();
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
& )
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
& )
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
);
193 return Sequence
< OUString
> ();
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */