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