LibreOffice Module ucb (master)  1
prov.cxx
Go to the documentation of this file.
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>
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/factory.hxx>
30 #include "filglob.hxx"
31 #include "filid.hxx"
32 #include "filtask.hxx"
33 #include "bc.hxx"
34 #include "prov.hxx"
35 
36 using namespace fileaccess;
37 using namespace com::sun::star;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::beans;
41 using namespace com::sun::star::ucb;
42 using namespace com::sun::star::container;
43 
44 #if OSL_DEBUG_LEVEL > 0
45 #define THROW_WHERE SAL_WHERE
46 #else
47 #define THROW_WHERE ""
48 #endif
49 
50 
51 extern "C" SAL_DLLPUBLIC_EXPORT void * ucpfile_component_getFactory(
52  const char * pImplName, void * pServiceManager, void * )
53 {
54  void * pRet = nullptr;
55 
57  static_cast< XMultiServiceFactory * >( pServiceManager ) );
59 
60 
61  // File Content Provider.
62 
63 
65  equalsAscii( pImplName ) )
66  {
67  xFactory = FileProvider::createServiceFactory( xSMgr );
68  }
69 
70 
71  if ( xFactory.is() )
72  {
73  xFactory->acquire();
74  pRet = xFactory.get();
75  }
76 
77  return pRet;
78 }
79 
80 /****************************************************************************/
81 /* */
82 /* */
83 /* FileProvider */
84 /* */
85 /* */
86 /****************************************************************************/
88  : m_xContext(rxContext)
89  , m_FileSystemNotation(FileSystemNotation::UNKNOWN_NOTATION)
90 {
91 }
92 
94 {
95 }
96 
97 // XInitialization
99 {
100  if( ! m_pMyShell )
101  m_pMyShell.reset( new TaskManager( m_xContext, this, true ) );
102 }
103 
104 
105 void SAL_CALL
107  const Sequence< Any >& aArguments )
108 {
109  if( ! m_pMyShell ) {
110  OUString config;
111  if( aArguments.hasElements() &&
112  (aArguments[0] >>= config) &&
113  config == "NoConfig" )
114  m_pMyShell.reset( new TaskManager( m_xContext, this, false ) );
115  else
116  m_pMyShell.reset( new TaskManager( m_xContext, this, true ) );
117  }
118 }
119 
120 // XServiceInfo methods.
121 OUString SAL_CALL
123 {
125 }
126 
127 sal_Bool SAL_CALL FileProvider::supportsService(const OUString& ServiceName )
128 {
129  return cppu::supportsService(this, ServiceName);
130 }
131 
132 Sequence< OUString > SAL_CALL
134 {
136 }
137 
140  const Reference< XMultiServiceFactory >& rxServiceMgr )
141 {
143  rxServiceMgr,
147 }
148 
151  const Reference< XMultiServiceFactory >& xMultiServiceFactory )
152 {
153  XServiceInfo* xP = new FileProvider(comphelper::getComponentContext(xMultiServiceFactory));
154  return Reference< XInterface >::query( xP );
155 }
156 
157 
158 // XContent
159 
160 
161 Reference< XContent > SAL_CALL
163  const Reference< XContentIdentifier >& xIdentifier )
164 {
165  init();
166  OUString aUnc;
167  bool err = fileaccess::TaskManager::getUnqFromUrl( xIdentifier->getContentIdentifier(),
168  aUnc );
169 
170  if( err )
171  {
172  throw IllegalIdentifierException( THROW_WHERE );
173  }
174 
175  return Reference< XContent >( new BaseContent( m_pMyShell.get(), xIdentifier, aUnc ) );
176 }
177 
178 
179 sal_Int32 SAL_CALL
183 {
184  init();
185  OUString aUrl1 = Id1->getContentIdentifier();
186  OUString aUrl2 = Id2->getContentIdentifier();
187 
188  sal_Int32 iComp = aUrl1.compareTo( aUrl2 );
189 
190  if ( 0 != iComp )
191  {
192  OUString aPath1, aPath2;
193 
196 
197  osl::FileBase::RC error;
198  osl::DirectoryItem aItem1, aItem2;
199 
200  error = osl::DirectoryItem::get( aPath1, aItem1 );
201  if ( error == osl::FileBase::E_None )
202  error = osl::DirectoryItem::get( aPath2, aItem2 );
203 
204  if ( error != osl::FileBase::E_None )
205  return iComp;
206 
207  osl::FileStatus aStatus1( osl_FileStatus_Mask_FileURL );
208  osl::FileStatus aStatus2( osl_FileStatus_Mask_FileURL );
209  error = aItem1.getFileStatus( aStatus1 );
210  if ( error == osl::FileBase::E_None )
211  error = aItem2.getFileStatus( aStatus2 );
212 
213  if ( error == osl::FileBase::E_None )
214  {
215  iComp = aStatus1.getFileURL().compareTo( aStatus2.getFileURL() );
216 
217 // Quick hack for Windows to threat all file systems as case insensitive
218 #ifdef _WIN32
219  if ( 0 != iComp )
220  {
221  error = osl::FileBase::getSystemPathFromFileURL( aStatus1.getFileURL(), aPath1 );
222  if ( error == osl::FileBase::E_None )
223  error = osl::FileBase::getSystemPathFromFileURL( aStatus2.getFileURL(), aPath2 );
224 
225  if ( error == osl::FileBase::E_None )
226  iComp = aPath1.compareToIgnoreAsciiCase( aPath2 );
227  }
228 #endif
229  }
230  }
231 
232  return iComp;
233 }
234 
235 
238  const OUString& ContentId )
239 {
240  init();
241  FileContentIdentifier* p = new FileContentIdentifier( ContentId,false );
243 }
244 
245 
246 //XPropertySetInfoImpl
247 
248 namespace {
249 
250 class XPropertySetInfoImpl2
251  : public cppu::OWeakObject,
252  public XPropertySetInfo
253 {
254 public:
255  XPropertySetInfoImpl2();
256 
257  // XInterface
258  virtual Any SAL_CALL
259  queryInterface( const Type& aType ) override;
260 
261  virtual void SAL_CALL
262  acquire()
263  throw() override;
264 
265  virtual void SAL_CALL
266  release()
267  throw() override;
268 
269 
270  virtual Sequence< Property > SAL_CALL
271  getProperties() override;
272 
273  virtual Property SAL_CALL
274  getPropertyByName( const OUString& aName ) override;
275 
276  virtual sal_Bool SAL_CALL
277  hasPropertyByName( const OUString& Name ) override;
278 
279 
280 private:
281  Sequence< Property > m_seq;
282 };
283 
284 }
285 
286 XPropertySetInfoImpl2::XPropertySetInfoImpl2()
287  : m_seq( 3 )
288 {
289  m_seq[0] = Property( "HostName",
290  -1,
292  PropertyAttribute::READONLY );
293 
294  m_seq[1] = Property( "HomeDirectory",
295  -1,
297  PropertyAttribute::READONLY );
298 
299  m_seq[2] = Property( "FileSystemNotation",
300  -1,
302  PropertyAttribute::READONLY );
303 }
304 
305 void SAL_CALL
306 XPropertySetInfoImpl2::acquire()
307  throw()
308 {
309  OWeakObject::acquire();
310 }
311 
312 
313 void SAL_CALL
314 XPropertySetInfoImpl2::release()
315  throw()
316 {
317  OWeakObject::release();
318 }
319 
320 
321 Any SAL_CALL
322 XPropertySetInfoImpl2::queryInterface( const Type& rType )
323 {
324  Any aRet = cppu::queryInterface( rType,
325  static_cast< XPropertySetInfo* >(this) );
326  return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
327 }
328 
329 
330 Property SAL_CALL
331 XPropertySetInfoImpl2::getPropertyByName( const OUString& aName )
332 {
333  auto pProp = std::find_if(m_seq.begin(), m_seq.end(),
334  [&aName](const Property& rProp) { return rProp.Name == aName; });
335  if (pProp != m_seq.end())
336  return *pProp;
337 
338  throw UnknownPropertyException( aName );
339 }
340 
341 
342 Sequence< Property > SAL_CALL
343 XPropertySetInfoImpl2::getProperties()
344 {
345  return m_seq;
346 }
347 
348 
349 sal_Bool SAL_CALL
350 XPropertySetInfoImpl2::hasPropertyByName(
351  const OUString& aName )
352 {
353  return std::any_of(m_seq.begin(), m_seq.end(),
354  [&aName](const Property& rProp) { return rProp.Name == aName; });
355 }
356 
357 
359 {
360  osl::MutexGuard aGuard( m_aMutex );
361  if( ! m_xPropertySetInfo.is() )
362  {
363  osl_getLocalHostname( &m_HostName.pData );
364 
365 #if defined ( UNX )
366  m_FileSystemNotation = FileSystemNotation::UNIX_NOTATION;
367 #elif defined( WNT )
368  m_FileSystemNotation = FileSystemNotation::DOS_NOTATION;
369 #else
370  m_FileSystemNotation = FileSystemNotation::UNKNOWN_NOTATION;
371 #endif
372  osl::Security aSecurity;
373  aSecurity.getHomeDir( m_HomeDirectory );
374 
375  // static const sal_Int32 UNKNOWN_NOTATION = (sal_Int32)0;
376  // static const sal_Int32 UNIX_NOTATION = (sal_Int32)1;
377  // static const sal_Int32 DOS_NOTATION = (sal_Int32)2;
378  // static const sal_Int32 MAC_NOTATION = (sal_Int32)3;
379 
380  XPropertySetInfoImpl2* p = new XPropertySetInfoImpl2();
381  m_xPropertySetInfo.set( p );
382  }
383 }
384 
385 
386 // XPropertySet
387 
390 {
391  initProperties();
392  return m_xPropertySetInfo;
393 }
394 
395 
396 void SAL_CALL
397 FileProvider::setPropertyValue( const OUString& aPropertyName,
398  const Any& )
399 {
400  if( !(aPropertyName == "FileSystemNotation" ||
401  aPropertyName == "HomeDirectory" ||
402  aPropertyName == "HostName") )
403  throw UnknownPropertyException( aPropertyName );
404 }
405 
406 
407 Any SAL_CALL
409  const OUString& aPropertyName )
410 {
411  initProperties();
412  if( aPropertyName == "FileSystemNotation" )
413  {
414  return Any(m_FileSystemNotation);
415  }
416  else if( aPropertyName == "HomeDirectory" )
417  {
418  return Any(m_HomeDirectory);
419  }
420  else if( aPropertyName == "HostName" )
421  {
422  return Any(m_HostName);
423  }
424  else
425  throw UnknownPropertyException( aPropertyName );
426 }
427 
428 
429 void SAL_CALL
431  const OUString&,
433 {
434 }
435 
436 
437 void SAL_CALL
439  const OUString&,
441 {
442 }
443 
444 void SAL_CALL
446  const OUString&,
448 {
449 }
450 
451 
452 void SAL_CALL
454  const OUString&,
456 {
457 }
458 
459 
460 // XFileIdentifierConverter
461 
462 sal_Int32 SAL_CALL
463 FileProvider::getFileProviderLocality( const OUString& BaseURL )
464 {
465  // If the base URL is a 'file' URL, return 10 (very 'local'), otherwise
466  // return -1 (mismatch). What is missing is a fast comparison to ASCII,
467  // ignoring case:
468  return BaseURL.getLength() >= 5
469  && (BaseURL[0] == 'F' || BaseURL[0] == 'f')
470  && (BaseURL[1] == 'I' || BaseURL[1] == 'i')
471  && (BaseURL[2] == 'L' || BaseURL[2] == 'l')
472  && (BaseURL[3] == 'E' || BaseURL[3] == 'e')
473  && BaseURL[4] == ':' ?
474  10 : -1;
475 }
476 
477 OUString SAL_CALL FileProvider::getFileURLFromSystemPath( const OUString&,
478  const OUString& SystemPath )
479 {
480  OUString aNormalizedPath;
481  if ( osl::FileBase::getFileURLFromSystemPath( SystemPath,aNormalizedPath ) != osl::FileBase::E_None )
482  return OUString();
483 
484  return aNormalizedPath;
485 }
486 
487 OUString SAL_CALL FileProvider::getSystemPathFromFileURL( const OUString& URL )
488 {
489  OUString aSystemPath;
490  if (osl::FileBase::getSystemPathFromFileURL( URL,aSystemPath ) != osl::FileBase::E_None )
491  return OUString();
492 
493  return aSystemPath;
494 }
495 
496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual OUString SAL_CALL getFileURLFromSystemPath(const OUString &BaseURL, const OUString &SystemPath) override
Definition: prov.cxx:477
Type
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: prov.cxx:408
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
Definition: prov.cxx:445
FileProvider(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Definition: prov.cxx:87
osl::Mutex m_aMutex
static css::uno::Reference< css::lang::XSingleServiceFactory > createServiceFactory(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxServiceMgr)
Definition: prov.cxx:139
virtual OUString SAL_CALL getImplementationName() override
Definition: prov.cxx:122
static css::uno::Reference< css::uno::XInterface > SAL_CALL CreateInstance(const css::uno::Reference< css::lang::XMultiServiceFactory > &xMultiServiceFactory)
Definition: prov.cxx:150
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
Definition: prov.cxx:397
static OUString getImplementationName_static()
Definition: filtask.cxx:2944
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
Definition: prov.cxx:453
virtual sal_Int32 SAL_CALL getFileProviderLocality(const OUString &BaseURL) override
Definition: prov.cxx:463
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT void * ucpfile_component_getFactory(const char *pImplName, void *pServiceManager, void *)
Definition: prov.cxx:51
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: prov.cxx:133
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: prov.hxx:154
err
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
Definition: prov.cxx:106
virtual css::uno::Reference< css::ucb::XContentIdentifier > SAL_CALL createContentIdentifier(const OUString &ContentId) override
Definition: prov.cxx:237
virtual sal_Int32 SAL_CALL compareContentIds(const css::uno::Reference< css::ucb::XContentIdentifier > &Id1, const css::uno::Reference< css::ucb::XContentIdentifier > &Id2) override
Definition: prov.cxx:180
unsigned char sal_Bool
static css::uno::Sequence< OUString > getSupportedServiceNames_static()
Definition: filtask.cxx:2951
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier) override
Definition: prov.cxx:162
virtual ~FileProvider() override
Definition: prov.cxx:93
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
css::uno::Reference< css::uno::XInterface >(SAL_CALL *ComponentInstantiation)(const css CPPUHELPER_DLLPUBLIC css::uno::Reference< css::lang::XSingleServiceFactory > SAL_CALL createSingleFactory(const css::uno::Reference< css::lang::XMultiServiceFactory > &rServiceManager, const ::rtl::OUString &rImplementationName, ComponentInstantiation pCreateFunction, const css::uno::Sequence< ::rtl::OUString > &rServiceNames, rtl_ModuleCount *pModCount=NULL)
std::unique_ptr< TaskManager > m_pMyShell
Definition: prov.hxx:164
#define THROW_WHERE
Definition: prov.cxx:45
Reference< XMultiServiceFactory > xSMgr
static bool getUnqFromUrl(const OUString &Url, OUString &Unq)
Definition: filtask.cxx:1957
tuple config
void * p
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
Definition: prov.cxx:438
Reference< XSingleServiceFactory > xFactory
friend class BaseContent
Definition: prov.hxx:55
virtual OUString SAL_CALL getSystemPathFromFileURL(const OUString &URL) override
Definition: prov.cxx:487
Reference< XComponentContext > m_xContext
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: prov.cxx:127
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
Definition: prov.cxx:430
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: prov.cxx:389