Debian Codegraph @

ucb/source/ucp/file/prov.cxx

raw
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 <osl/security.hxx>
21 #include <osl/file.hxx>
22 #include <osl/socket.h>
23 #include <cppuhelper/queryinterface.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/ucb/FileSystemNotation.hpp>
27 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
28 #include <cppuhelper/supportsservice.hxx>
29 #include "filglob.hxx"
30 #include "filid.hxx"
31 #include "filtask.hxx"
32 #include "bc.hxx"
33 #include "prov.hxx"
34
35 using namespace fileaccess;
36 using namespace com::sun::star;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::beans;
40 using namespace com::sun::star::ucb;
41
42 #if OSL_DEBUG_LEVEL > 0
43 #define THROW_WHERE SAL_WHERE
44 #else
45 #define THROW_WHERE ""
46 #endif
47
48
49 /****************************************************************************/
50 /* */
51 /* */
52 /* FileProvider */
53 /* */
54 /* */
55 /****************************************************************************/
56 FileProvider::FileProvider( const Reference< XComponentContext >& rxContext )
57 : m_xContext(rxContext)
58 , m_FileSystemNotation(FileSystemNotation::UNKNOWN_NOTATION)
59 {
60 }
61
62 FileProvider::~FileProvider()
63 {
64 }
65
66 // XInitialization
67 void FileProvider::init()
68 {
69 if( ! m_pMyShell )
70 m_pMyShell.reset( new TaskManager( m_xContext, this, true ) );
71 }
72
73
74 void SAL_CALL
75 FileProvider::initialize(
76 const Sequence< Any >& aArguments )
77 {
78 if( ! m_pMyShell ) {
79 OUString config;
80 if( aArguments.hasElements() &&
81 (aArguments[0] >>= config) &&
82 config == "NoConfig" )
83 m_pMyShell.reset( new TaskManager( m_xContext, this, false ) );
84 else
85 m_pMyShell.reset( new TaskManager( m_xContext, this, true ) );
86 }
87 }
88
89 // XServiceInfo methods.
90 OUString SAL_CALL
91 FileProvider::getImplementationName()
92 {
93 return u"com.sun.star.comp.ucb.FileProvider"_ustr;
94 }
95
96 sal_Bool SAL_CALL FileProvider::supportsService(const OUString& ServiceName )
97 {
98 return cppu::supportsService(this, ServiceName);
99 }
100
101 Sequence< OUString > SAL_CALL
102 FileProvider::getSupportedServiceNames()
103 {
104 return { u"com.sun.star.ucb.FileContentProvider"_ustr };
105 }
106
107 // XContent
108
109 Reference< XContent > SAL_CALL
110 FileProvider::queryContent(
111 const Reference< XContentIdentifier >& xIdentifier )
112 {
113 init();
114 OUString aUnc;
115 bool err = fileaccess::TaskManager::getUnqFromUrl( xIdentifier->getContentIdentifier(),
116 aUnc );
117
118 if( err )
119 {
120 throw IllegalIdentifierException( THROW_WHERE );
121 }
122
123 return Reference<XContent>(new BaseContent(m_pMyShell.get(), xIdentifier, aUnc));
124 }
125
126 sal_Int32 SAL_CALL
127 FileProvider::compareContentIds(
128 const Reference< XContentIdentifier >& Id1,
129 const Reference< XContentIdentifier >& Id2 )
130 {
131 init();
132 OUString aUrl1 = Id1->getContentIdentifier();
133 OUString aUrl2 = Id2->getContentIdentifier();
134
135 sal_Int32 iComp = aUrl1.compareTo( aUrl2 );
136
137 if ( 0 != iComp )
138 {
139 OUString aPath1, aPath2;
140
141 fileaccess::TaskManager::getUnqFromUrl( aUrl1, aPath1 );
142 fileaccess::TaskManager::getUnqFromUrl( aUrl2, aPath2 );
143
144 osl::FileBase::RC error;
145 osl::DirectoryItem aItem1, aItem2;
146
147 error = osl::DirectoryItem::get( aPath1, aItem1 );
148 if ( error == osl::FileBase::E_None )
149 error = osl::DirectoryItem::get( aPath2, aItem2 );
150
151 if ( error != osl::FileBase::E_None )
152 return iComp;
153
154 osl::FileStatus aStatus1( osl_FileStatus_Mask_FileURL );
155 osl::FileStatus aStatus2( osl_FileStatus_Mask_FileURL );
156 error = aItem1.getFileStatus( aStatus1 );
157 if ( error == osl::FileBase::E_None )
158 error = aItem2.getFileStatus( aStatus2 );
159
160 if ( error == osl::FileBase::E_None )
161 {
162 iComp = aStatus1.getFileURL().compareTo( aStatus2.getFileURL() );
163
164 // Quick hack for Windows to threat all file systems as case insensitive
165 #ifdef _WIN32
166 if ( 0 != iComp )
167 {
168 error = osl::FileBase::getSystemPathFromFileURL( aStatus1.getFileURL(), aPath1 );
169 if ( error == osl::FileBase::E_None )
170 error = osl::FileBase::getSystemPathFromFileURL( aStatus2.getFileURL(), aPath2 );
171
172 if ( error == osl::FileBase::E_None )
173 iComp = aPath1.compareToIgnoreAsciiCase( aPath2 );
174 }
175 #endif
176 }
177 }
178
179 return iComp;
180 }
181
182
183 Reference< XContentIdentifier > SAL_CALL
184 FileProvider::createContentIdentifier(
185 const OUString& ContentId )
186 {
187 init();
188 return new FileContentIdentifier( ContentId,false );
189 }
190
191
192 //XPropertySetInfoImpl
193
194 namespace fileaccess {
195
196 class XPropertySetInfoImpl2
197 : public cppu::OWeakObject,
198 public XPropertySetInfo
199 {
200 public:
201 XPropertySetInfoImpl2();
202
203 // XInterface
204 virtual Any SAL_CALL
205 queryInterface( const Type& aType ) override;
206
207 virtual void SAL_CALL
208 acquire()
209 noexcept override;
210
211 virtual void SAL_CALL
212 release()
213 noexcept override;
214
215
216 virtual Sequence< Property > SAL_CALL
217 getProperties() override;
218
219 virtual Property SAL_CALL
220 getPropertyByName( const OUString& aName ) override;
221
222 virtual sal_Bool SAL_CALL
223 hasPropertyByName( const OUString& Name ) override;
224
225
226 private:
227 Sequence< Property > m_seq;
228 };
229
230 }
231
232 XPropertySetInfoImpl2::XPropertySetInfoImpl2()
233 : m_seq{ Property( u"HostName"_ustr,
234 -1,
235 cppu::UnoType<OUString>::get(),
236 PropertyAttribute::READONLY ),
237 Property( u"HomeDirectory"_ustr,
238 -1,
239 cppu::UnoType<OUString>::get(),
240 PropertyAttribute::READONLY ),
241 Property( u"FileSystemNotation"_ustr,
242 -1,
243 cppu::UnoType<sal_Int32>::get(),
244 PropertyAttribute::READONLY )}
245 {
246 }
247
248 void SAL_CALL
249 XPropertySetInfoImpl2::acquire()
250 noexcept
251 {
252 OWeakObject::acquire();
253 }
254
255
256 void SAL_CALL
257 XPropertySetInfoImpl2::release()
258 noexcept
259 {
260 OWeakObject::release();
261 }
262
263
264 Any SAL_CALL
265 XPropertySetInfoImpl2::queryInterface( const Type& rType )
266 {
267 Any aRet = cppu::queryInterface( rType,
268 static_cast< XPropertySetInfo* >(this) );
269 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
270 }
271
272
273 Property SAL_CALL
274 XPropertySetInfoImpl2::getPropertyByName( const OUString& aName )
275 {
276 auto pProp = std::find_if(std::cbegin(m_seq), std::cend(m_seq),
277 [&aName](const Property& rProp) { return rProp.Name == aName; });
278 if (pProp != std::cend(m_seq))
279 return *pProp;
280
281 throw UnknownPropertyException( aName );
282 }
283
284
285 Sequence< Property > SAL_CALL
286 XPropertySetInfoImpl2::getProperties()
287 {
288 return m_seq;
289 }
290
291
292 sal_Bool SAL_CALL
293 XPropertySetInfoImpl2::hasPropertyByName(
294 const OUString& aName )
295 {
296 return std::any_of(std::cbegin(m_seq), std::cend(m_seq),
297 [&aName](const Property& rProp) { return rProp.Name == aName; });
298 }
299
300
301 void FileProvider::initProperties(std::unique_lock<std::mutex>& /*rGuard*/)
302 {
303 if( m_xPropertySetInfo.is() )
304 return;
305
306 osl_getLocalHostname( &m_HostName.pData );
307
308 #if defined ( UNX )
309 m_FileSystemNotation = FileSystemNotation::UNIX_NOTATION;
310 #elif defined( _WIN32 )
311 m_FileSystemNotation = FileSystemNotation::DOS_NOTATION;
312 #else
313 m_FileSystemNotation = FileSystemNotation::UNKNOWN_NOTATION;
314 #endif
315 osl::Security aSecurity;
316 aSecurity.getHomeDir( m_HomeDirectory );
317
318 // static const sal_Int32 UNKNOWN_NOTATION = (sal_Int32)0;
319 // static const sal_Int32 UNIX_NOTATION = (sal_Int32)1;
320 // static const sal_Int32 DOS_NOTATION = (sal_Int32)2;
321 // static const sal_Int32 MAC_NOTATION = (sal_Int32)3;
322
323 m_xPropertySetInfo = new XPropertySetInfoImpl2();
324 }
325
326
327 // XPropertySet
328
329 Reference< XPropertySetInfo > SAL_CALL
330 FileProvider::getPropertySetInfo( )
331 {
332 std::unique_lock aGuard( m_aMutex );
333 initProperties(aGuard);
334 return m_xPropertySetInfo;
335 }
336
337
338 void SAL_CALL
339 FileProvider::setPropertyValue( const OUString& aPropertyName,
340 const Any& )
341 {
342 if( !(aPropertyName == "FileSystemNotation" ||
343 aPropertyName == "HomeDirectory" ||
344 aPropertyName == "HostName") )
345 throw UnknownPropertyException( aPropertyName );
346 }
347
348
349 Any SAL_CALL
350 FileProvider::getPropertyValue(
351 const OUString& aPropertyName )
352 {
353 std::unique_lock aGuard( m_aMutex );
354 initProperties(aGuard);
355 if( aPropertyName == "FileSystemNotation" )
356 {
357 return Any(m_FileSystemNotation);
358 }
359 else if( aPropertyName == "HomeDirectory" )
360 {
361 return Any(m_HomeDirectory);
362 }
363 else if( aPropertyName == "HostName" )
364 {
365 return Any(m_HostName);
366 }
367 else
368 throw UnknownPropertyException( aPropertyName );
369 }
370
371
372 void SAL_CALL
373 FileProvider::addPropertyChangeListener(
374 const OUString&,
375 const Reference< XPropertyChangeListener >& )
376 {
377 }
378
379
380 void SAL_CALL
381 FileProvider::removePropertyChangeListener(
382 const OUString&,
383 const Reference< XPropertyChangeListener >& )
384 {
385 }
386
387 void SAL_CALL
388 FileProvider::addVetoableChangeListener(
389 const OUString&,
390 const Reference< XVetoableChangeListener >& )
391 {
392 }
393
394
395 void SAL_CALL
396 FileProvider::removeVetoableChangeListener(
397 const OUString&,
398 const Reference< XVetoableChangeListener >& )
399 {
400 }
401
402
403 // XFileIdentifierConverter
404
405 sal_Int32 SAL_CALL
406 FileProvider::getFileProviderLocality( const OUString& BaseURL )
407 {
408 // If the base URL is a 'file' URL, return 10 (very 'local'), otherwise
409 // return -1 (mismatch). What is missing is a fast comparison to ASCII,
410 // ignoring case:
411 return BaseURL.getLength() >= 5
412 && (BaseURL[0] == 'F' || BaseURL[0] == 'f')
413 && (BaseURL[1] == 'I' || BaseURL[1] == 'i')
414 && (BaseURL[2] == 'L' || BaseURL[2] == 'l')
415 && (BaseURL[3] == 'E' || BaseURL[3] == 'e')
416 && BaseURL[4] == ':' ?
417 10 : -1;
418 }
419
420 OUString SAL_CALL FileProvider::getFileURLFromSystemPath( const OUString&,
421 const OUString& SystemPath )
422 {
423 OUString aNormalizedPath;
424 if ( osl::FileBase::getFileURLFromSystemPath( SystemPath,aNormalizedPath ) != osl::FileBase::E_None )
425 return OUString();
426
427 return aNormalizedPath;
428 }
429
430 OUString SAL_CALL FileProvider::getSystemPathFromFileURL( const OUString& URL )
431 {
432 OUString aSystemPath;
433 if (osl::FileBase::getSystemPathFromFileURL( URL,aSystemPath ) != osl::FileBase::E_None )
434 return OUString();
435
436 return aSystemPath;
437 }
438
439 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
440 ucb_file_FileProvider_get_implementation(
441 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
442 {
443 return cppu::acquire(new FileProvider(context));
444 }
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */