bump product version to 5.0.4.1
[LibreOffice.git] / ucb / source / ucp / file / prov.cxx
blobde4ef78c768ca8fbdff8aed9dcd7eeab56a47bf2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <osl/security.hxx>
21 #include <osl/file.hxx>
22 #include <osl/socket.h>
23 #include <comphelper/processfactory.hxx>
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <com/sun/star/ucb/FileSystemNotation.hpp>
26 #include <com/sun/star/beans/PropertyState.hpp>
27 #include <cppuhelper/factory.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include "filglob.hxx"
30 #include "filid.hxx"
31 #include "shell.hxx"
32 #include "bc.hxx"
33 #include "prov.hxx"
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 using namespace com::sun::star::container;
43 #if OSL_DEBUG_LEVEL > 0
44 #define THROW_WHERE SAL_WHERE
45 #else
46 #define THROW_WHERE ""
47 #endif
50 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL ucpfile_component_getFactory(
51 const sal_Char * pImplName, void * pServiceManager, void * )
53 void * pRet = 0;
55 Reference< XMultiServiceFactory > xSMgr(
56 static_cast< XMultiServiceFactory * >( pServiceManager ) );
57 Reference< XSingleServiceFactory > xFactory;
60 // File Content Provider.
63 if ( fileaccess::shell::getImplementationName_static().
64 equalsAscii( pImplName ) )
66 xFactory = FileProvider::createServiceFactory( xSMgr );
71 if ( xFactory.is() )
73 xFactory->acquire();
74 pRet = xFactory.get();
77 return pRet;
80 /****************************************************************************/
81 /* */
82 /* */
83 /* FileProvider */
84 /* */
85 /* */
86 /****************************************************************************/
87 FileProvider::FileProvider( const Reference< XComponentContext >& rxContext )
88 : m_xContext(rxContext)
89 , m_FileSystemNotation(FileSystemNotation::UNKNOWN_NOTATION)
90 , m_pMyShell(NULL)
94 FileProvider::~FileProvider()
96 if( m_pMyShell )
97 delete m_pMyShell;
100 // XInitialization
101 void SAL_CALL FileProvider::init()
103 if( ! m_pMyShell )
104 m_pMyShell = new shell( m_xContext, this, true );
108 void SAL_CALL
109 FileProvider::initialize(
110 const Sequence< Any >& aArguments )
111 throw (Exception, RuntimeException, std::exception)
113 if( ! m_pMyShell ) {
114 OUString config;
115 if( aArguments.getLength() > 0 &&
116 (aArguments[0] >>= config) &&
117 config == "NoConfig" )
118 m_pMyShell = new shell( m_xContext, this, false );
119 else
120 m_pMyShell = new shell( m_xContext, this, true );
124 // XServiceInfo methods.
125 OUString SAL_CALL
126 FileProvider::getImplementationName()
127 throw( RuntimeException, std::exception )
129 return fileaccess::shell::getImplementationName_static();
132 sal_Bool SAL_CALL FileProvider::supportsService(const OUString& ServiceName )
133 throw( RuntimeException, std::exception )
135 return cppu::supportsService(this, ServiceName);
138 Sequence< OUString > SAL_CALL
139 FileProvider::getSupportedServiceNames(
140 void )
141 throw( RuntimeException, std::exception )
143 return fileaccess::shell::getSupportedServiceNames_static();
146 Reference< XSingleServiceFactory > SAL_CALL
147 FileProvider::createServiceFactory(
148 const Reference< XMultiServiceFactory >& rxServiceMgr )
151 * Create a single service factory.<BR>
152 * Note: The function pointer ComponentInstantiation points to a function throws Exception.
154 * @param rServiceManager the service manager used by the implementation.
155 * @param rImplementationName the implementation name. An empty string is possible.
156 * @param ComponentInstantiation the function pointer to create an object.
157 * @param rServiceNames the service supported by the implementation.
158 * @return a factory that support the interfaces XServiceProvider, XServiceInfo
159 * XSingleServiceFactory and XComponent.
161 * @see createOneInstanceFactory
164 * Reference< ::com::sun::star::XSingleServiceFactory > createSingleFactory
166 * const ::com::sun::star::Reference< ::com::sun::star::XMultiServiceFactory > & rServiceManager,
167 * const OUString & rImplementationName,
168 * ComponentInstantiation pCreateFunction,
170 * const ::com::sun::star::Sequence< OUString > & rServiceNames
171 * );
174 return Reference< XSingleServiceFactory > ( cppu::createSingleFactory(
175 rxServiceMgr,
176 fileaccess::shell::getImplementationName_static(),
177 FileProvider::CreateInstance,
178 fileaccess::shell::getSupportedServiceNames_static() ) );
181 Reference< XInterface > SAL_CALL
182 FileProvider::CreateInstance(
183 const Reference< XMultiServiceFactory >& xMultiServiceFactory )
185 XServiceInfo* xP = (XServiceInfo*) new FileProvider( comphelper::getComponentContext(xMultiServiceFactory) );
186 return Reference< XInterface >::query( xP );
192 // XContent
196 Reference< XContent > SAL_CALL
197 FileProvider::queryContent(
198 const Reference< XContentIdentifier >& xIdentifier )
199 throw( IllegalIdentifierException,
200 RuntimeException, std::exception)
202 init();
203 OUString aUnc;
204 bool err = fileaccess::shell::getUnqFromUrl( xIdentifier->getContentIdentifier(),
205 aUnc );
207 if( err )
208 throw IllegalIdentifierException( THROW_WHERE );
210 return Reference< XContent >( new BaseContent( m_pMyShell,xIdentifier,aUnc ) );
215 sal_Int32 SAL_CALL
216 FileProvider::compareContentIds(
217 const Reference< XContentIdentifier >& Id1,
218 const Reference< XContentIdentifier >& Id2 )
219 throw( RuntimeException, std::exception )
221 init();
222 OUString aUrl1 = Id1->getContentIdentifier();
223 OUString aUrl2 = Id2->getContentIdentifier();
225 sal_Int32 iComp = aUrl1.compareTo( aUrl2 );
227 if ( 0 != iComp )
229 OUString aPath1, aPath2;
231 fileaccess::shell::getUnqFromUrl( aUrl1, aPath1 );
232 fileaccess::shell::getUnqFromUrl( aUrl2, aPath2 );
234 osl::FileBase::RC error;
235 osl::DirectoryItem aItem1, aItem2;
237 error = osl::DirectoryItem::get( aPath1, aItem1 );
238 if ( error == osl::FileBase::E_None )
239 error = osl::DirectoryItem::get( aPath2, aItem2 );
241 if ( error != osl::FileBase::E_None )
242 return iComp;
244 osl::FileStatus aStatus1( osl_FileStatus_Mask_FileURL );
245 osl::FileStatus aStatus2( osl_FileStatus_Mask_FileURL );
246 error = aItem1.getFileStatus( aStatus1 );
247 if ( error == osl::FileBase::E_None )
248 error = aItem2.getFileStatus( aStatus2 );
250 if ( error == osl::FileBase::E_None )
252 iComp = aStatus1.getFileURL().compareTo( aStatus2.getFileURL() );
254 // Quick hack for Windows to threat all file systems as case insensitive
255 #ifdef WNT
256 if ( 0 != iComp )
258 error = osl::FileBase::getSystemPathFromFileURL( aStatus1.getFileURL(), aPath1 );
259 if ( error == osl::FileBase::E_None )
260 error = osl::FileBase::getSystemPathFromFileURL( aStatus2.getFileURL(), aPath2 );
262 if ( error == osl::FileBase::E_None )
263 iComp = rtl_ustr_compareIgnoreAsciiCase( aPath1.getStr(), aPath2.getStr() );
265 #endif
269 return iComp;
274 Reference< XContentIdentifier > SAL_CALL
275 FileProvider::createContentIdentifier(
276 const OUString& ContentId )
277 throw( RuntimeException, std::exception )
279 init();
280 FileContentIdentifier* p = new FileContentIdentifier( ContentId,false );
281 return Reference< XContentIdentifier >( p );
286 //XPropertySetInfoImpl
288 class XPropertySetInfoImpl2
289 : public cppu::OWeakObject,
290 public XPropertySetInfo
292 public:
293 XPropertySetInfoImpl2();
294 virtual ~XPropertySetInfoImpl2();
296 // XInterface
297 virtual Any SAL_CALL
298 queryInterface(
299 const Type& aType )
300 throw( RuntimeException, std::exception) SAL_OVERRIDE;
302 virtual void SAL_CALL
303 acquire(
304 void )
305 throw() SAL_OVERRIDE;
307 virtual void SAL_CALL
308 release(
309 void )
310 throw() SAL_OVERRIDE;
313 virtual Sequence< Property > SAL_CALL
314 getProperties(
315 void )
316 throw( RuntimeException, std::exception ) SAL_OVERRIDE;
318 virtual Property SAL_CALL
319 getPropertyByName(
320 const OUString& aName )
321 throw( UnknownPropertyException,
322 RuntimeException, std::exception) SAL_OVERRIDE;
324 virtual sal_Bool SAL_CALL
325 hasPropertyByName( const OUString& Name )
326 throw( RuntimeException, std::exception ) SAL_OVERRIDE;
329 private:
330 Sequence< Property > m_seq;
334 XPropertySetInfoImpl2::XPropertySetInfoImpl2()
335 : m_seq( 3 )
337 m_seq[0] = Property( OUString("HostName"),
339 cppu::UnoType<OUString>::get(),
340 PropertyAttribute::READONLY );
342 m_seq[1] = Property( OUString("HomeDirectory"),
344 cppu::UnoType<OUString>::get(),
345 PropertyAttribute::READONLY );
347 m_seq[2] = Property( OUString("FileSystemNotation"),
349 cppu::UnoType<sal_Int32>::get(),
350 PropertyAttribute::READONLY );
354 XPropertySetInfoImpl2::~XPropertySetInfoImpl2()
356 // nothing
360 void SAL_CALL
361 XPropertySetInfoImpl2::acquire(
362 void )
363 throw()
365 OWeakObject::acquire();
369 void SAL_CALL
370 XPropertySetInfoImpl2::release(
371 void )
372 throw()
374 OWeakObject::release();
378 Any SAL_CALL
379 XPropertySetInfoImpl2::queryInterface(
380 const Type& rType )
381 throw( RuntimeException, std::exception )
383 Any aRet = cppu::queryInterface( rType,
384 (static_cast< XPropertySetInfo* >(this)) );
385 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
389 Property SAL_CALL
390 XPropertySetInfoImpl2::getPropertyByName(
391 const OUString& aName )
392 throw( UnknownPropertyException,
393 RuntimeException, std::exception)
395 for( sal_Int32 i = 0; i < m_seq.getLength(); ++i )
396 if( m_seq[i].Name == aName )
397 return m_seq[i];
399 throw UnknownPropertyException( THROW_WHERE );
404 Sequence< Property > SAL_CALL
405 XPropertySetInfoImpl2::getProperties(
406 void )
407 throw( RuntimeException, std::exception )
409 return m_seq;
413 sal_Bool SAL_CALL
414 XPropertySetInfoImpl2::hasPropertyByName(
415 const OUString& aName )
416 throw( RuntimeException, std::exception )
418 for( sal_Int32 i = 0; i < m_seq.getLength(); ++i )
419 if( m_seq[i].Name == aName )
420 return true;
421 return false;
428 void SAL_CALL FileProvider::initProperties()
430 osl::MutexGuard aGuard( m_aMutex );
431 if( ! m_xPropertySetInfo.is() )
433 osl_getLocalHostname( &m_HostName.pData );
435 #if defined ( UNX )
436 m_FileSystemNotation = FileSystemNotation::UNIX_NOTATION;
437 #elif defined( WNT )
438 m_FileSystemNotation = FileSystemNotation::DOS_NOTATION;
439 #else
440 m_FileSystemNotation = FileSystemNotation::UNKNOWN_NOTATION;
441 #endif
442 osl::Security aSecurity;
443 aSecurity.getHomeDir( m_HomeDirectory );
445 // static const sal_Int32 UNKNOWN_NOTATION = (sal_Int32)0;
446 // static const sal_Int32 UNIX_NOTATION = (sal_Int32)1;
447 // static const sal_Int32 DOS_NOTATION = (sal_Int32)2;
448 // static const sal_Int32 MAC_NOTATION = (sal_Int32)3;
450 XPropertySetInfoImpl2* p = new XPropertySetInfoImpl2();
451 m_xPropertySetInfo = Reference< XPropertySetInfo >( p );
456 // XPropertySet
458 Reference< XPropertySetInfo > SAL_CALL
459 FileProvider::getPropertySetInfo( )
460 throw( RuntimeException, std::exception )
462 initProperties();
463 return m_xPropertySetInfo;
467 void SAL_CALL
468 FileProvider::setPropertyValue( const OUString& aPropertyName,
469 const Any& )
470 throw( UnknownPropertyException,
471 PropertyVetoException,
472 IllegalArgumentException,
473 WrappedTargetException,
474 RuntimeException, std::exception )
476 if( aPropertyName == "FileSystemNotation" ||
477 aPropertyName == "HomeDirectory" ||
478 aPropertyName == "HostName" )
479 return;
480 else
481 throw UnknownPropertyException( THROW_WHERE );
486 Any SAL_CALL
487 FileProvider::getPropertyValue(
488 const OUString& aPropertyName )
489 throw( UnknownPropertyException,
490 WrappedTargetException,
491 RuntimeException, std::exception )
493 initProperties();
494 if( aPropertyName == "FileSystemNotation" )
496 Any aAny;
497 aAny <<= m_FileSystemNotation;
498 return aAny;
500 else if( aPropertyName == "HomeDirectory" )
502 Any aAny;
503 aAny <<= m_HomeDirectory;
504 return aAny;
506 else if( aPropertyName == "HostName" )
508 Any aAny;
509 aAny <<= m_HostName;
510 return aAny;
512 else
513 throw UnknownPropertyException( THROW_WHERE );
517 void SAL_CALL
518 FileProvider::addPropertyChangeListener(
519 const OUString&,
520 const Reference< XPropertyChangeListener >& )
521 throw( UnknownPropertyException,
522 WrappedTargetException,
523 RuntimeException, std::exception)
525 return;
529 void SAL_CALL
530 FileProvider::removePropertyChangeListener(
531 const OUString&,
532 const Reference< XPropertyChangeListener >& )
533 throw( UnknownPropertyException,
534 WrappedTargetException,
535 RuntimeException, std::exception )
537 return;
540 void SAL_CALL
541 FileProvider::addVetoableChangeListener(
542 const OUString&,
543 const Reference< XVetoableChangeListener >& )
544 throw( UnknownPropertyException,
545 WrappedTargetException,
546 RuntimeException, std::exception )
548 return;
552 void SAL_CALL
553 FileProvider::removeVetoableChangeListener(
554 const OUString&,
555 const Reference< XVetoableChangeListener >& )
556 throw( UnknownPropertyException,
557 WrappedTargetException,
558 RuntimeException, std::exception)
560 return;
565 // XFileIdentifierConverter
567 sal_Int32 SAL_CALL
568 FileProvider::getFileProviderLocality( const OUString& BaseURL )
569 throw( RuntimeException, std::exception )
571 // If the base URL is a 'file' URL, return 10 (very 'local'), otherwise
572 // return -1 (missmatch). What is missing is a fast comparison to ASCII,
573 // ignoring case:
574 return BaseURL.getLength() >= 5
575 && (BaseURL[0] == 'F' || BaseURL[0] == 'f')
576 && (BaseURL[1] == 'I' || BaseURL[1] == 'i')
577 && (BaseURL[2] == 'L' || BaseURL[2] == 'l')
578 && (BaseURL[3] == 'E' || BaseURL[3] == 'e')
579 && BaseURL[4] == ':' ?
580 10 : -1;
583 OUString SAL_CALL FileProvider::getFileURLFromSystemPath( const OUString&,
584 const OUString& SystemPath )
585 throw( RuntimeException, std::exception )
587 OUString aNormalizedPath;
588 if ( osl::FileBase::getFileURLFromSystemPath( SystemPath,aNormalizedPath ) != osl::FileBase::E_None )
589 return OUString();
591 return aNormalizedPath;
594 OUString SAL_CALL FileProvider::getSystemPathFromFileURL( const OUString& URL )
595 throw( RuntimeException, std::exception )
597 OUString aSystemPath;
598 if (osl::FileBase::getSystemPathFromFileURL( URL,aSystemPath ) != osl::FileBase::E_None )
599 return OUString();
601 return aSystemPath;
604 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */