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