Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / file / prov.cxx
blob4e4b6cf832e02f168c22e8a8068b84f02d1bfce9
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 <cppuhelper/factory.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/ucb/FileSystemNotation.hpp>
27 #include <com/sun/star/beans/PropertyState.hpp>
28 #include "filglob.hxx"
29 #include "filid.hxx"
30 #include "shell.hxx"
31 #include "bc.hxx"
32 #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 //=========================================================================
44 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL ucpfile_component_getFactory(
45 const sal_Char * pImplName, void * pServiceManager, void * )
47 void * pRet = 0;
49 Reference< XMultiServiceFactory > xSMgr(
50 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ) );
51 Reference< XSingleServiceFactory > xFactory;
53 //////////////////////////////////////////////////////////////////////
54 // File Content Provider.
55 //////////////////////////////////////////////////////////////////////
57 if ( fileaccess::shell::getImplementationName_static().
58 compareToAscii( pImplName ) == 0 )
60 xFactory = FileProvider::createServiceFactory( xSMgr );
63 //////////////////////////////////////////////////////////////////////
65 if ( xFactory.is() )
67 xFactory->acquire();
68 pRet = xFactory.get();
71 return pRet;
74 /****************************************************************************/
75 /* */
76 /* */
77 /* FileProvider */
78 /* */
79 /* */
80 /****************************************************************************/
84 FileProvider::FileProvider( const Reference< XComponentContext >& rxContext )
85 : m_xContext( rxContext ),
86 m_pMyShell( 0 )
91 FileProvider::~FileProvider()
93 if( m_pMyShell )
94 delete m_pMyShell;
98 //////////////////////////////////////////////////////////////////////////
99 // XInterface
100 //////////////////////////////////////////////////////////////////////////
102 void SAL_CALL
103 FileProvider::acquire(
104 void )
105 throw()
107 OWeakObject::acquire();
111 void SAL_CALL
112 FileProvider::release(
113 void )
114 throw()
116 OWeakObject::release();
120 Any SAL_CALL
121 FileProvider::queryInterface(
122 const Type& rType )
123 throw( RuntimeException )
125 Any aRet = cppu::queryInterface(
126 rType,
127 (static_cast< XContentProvider* >(this)),
128 (static_cast< XInitialization* >(this)),
129 (static_cast< XContentIdentifierFactory* >(this)),
130 (static_cast< XServiceInfo* >(this)),
131 (static_cast< XTypeProvider* >(this)),
132 (static_cast< XFileIdentifierConverter* >(this)),
133 (static_cast< XPropertySet* >(this)) );
134 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
137 ///////////////////////////////////////////////////////////////////////////////
138 // XInitialization
140 void SAL_CALL FileProvider::init()
142 if( ! m_pMyShell )
143 m_pMyShell = new shell( m_xContext, this, sal_True );
147 void SAL_CALL
148 FileProvider::initialize(
149 const Sequence< Any >& aArguments )
150 throw (Exception, RuntimeException)
152 if( ! m_pMyShell ) {
153 rtl::OUString config;
154 if( aArguments.getLength() > 0 &&
155 (aArguments[0] >>= config) &&
156 config.compareToAscii("NoConfig") == 0 )
157 m_pMyShell = new shell( m_xContext, this, sal_False );
158 else
159 m_pMyShell = new shell( m_xContext, this, sal_True );
164 ////////////////////////////////////////////////////////////////////////////////
166 // XTypeProvider methods.
169 XTYPEPROVIDER_IMPL_7( FileProvider,
170 XTypeProvider,
171 XServiceInfo,
172 XInitialization,
173 XContentIdentifierFactory,
174 XPropertySet,
175 XFileIdentifierConverter,
176 XContentProvider )
179 ////////////////////////////////////////////////////////////////////////////////
180 // XServiceInfo methods.
182 rtl::OUString SAL_CALL
183 FileProvider::getImplementationName()
184 throw( RuntimeException )
186 return fileaccess::shell::getImplementationName_static();
190 sal_Bool SAL_CALL
191 FileProvider::supportsService(
192 const rtl::OUString& ServiceName )
193 throw( RuntimeException )
195 return ServiceName == rtl::OUString("com.sun.star.ucb.FileContentProvider");
199 Sequence< rtl::OUString > SAL_CALL
200 FileProvider::getSupportedServiceNames(
201 void )
202 throw( RuntimeException )
204 return fileaccess::shell::getSupportedServiceNames_static();
209 Reference< XSingleServiceFactory > SAL_CALL
210 FileProvider::createServiceFactory(
211 const Reference< XMultiServiceFactory >& rxServiceMgr )
214 * Create a single service factory.<BR>
215 * Note: The function pointer ComponentInstantiation points to a function throws Exception.
217 * @param rServiceManager the service manager used by the implementation.
218 * @param rImplementationName the implementation name. An empty string is possible.
219 * @param ComponentInstantiation the function pointer to create an object.
220 * @param rServiceNames the service supported by the implementation.
221 * @return a factory that support the interfaces XServiceProvider, XServiceInfo
222 * XSingleServiceFactory and XComponent.
224 * @see createOneInstanceFactory
227 * Reference< ::com::sun::star::XSingleServiceFactory > createSingleFactory
229 * const ::com::sun::star::Reference< ::com::sun::star::XMultiServiceFactory > & rServiceManager,
230 * const ::rtl::OUString & rImplementationName,
231 * ComponentInstantiation pCreateFunction,
233 * const ::com::sun::star::Sequence< ::rtl::OUString > & rServiceNames
234 * );
237 return Reference< XSingleServiceFactory > ( cppu::createSingleFactory(
238 rxServiceMgr,
239 fileaccess::shell::getImplementationName_static(),
240 FileProvider::CreateInstance,
241 fileaccess::shell::getSupportedServiceNames_static() ) );
244 Reference< XInterface > SAL_CALL
245 FileProvider::CreateInstance(
246 const Reference< XMultiServiceFactory >& xMultiServiceFactory )
248 XServiceInfo* xP = (XServiceInfo*) new FileProvider( comphelper::getComponentContext(xMultiServiceFactory) );
249 return Reference< XInterface >::query( xP );
254 ////////////////////////////////////////////////////////////////////////////////
255 // XContent
256 ////////////////////////////////////////////////////////////////////////////////
259 Reference< XContent > SAL_CALL
260 FileProvider::queryContent(
261 const Reference< XContentIdentifier >& xIdentifier )
262 throw( IllegalIdentifierException,
263 RuntimeException)
265 init();
266 rtl::OUString aUnc;
267 sal_Bool err = m_pMyShell->getUnqFromUrl( xIdentifier->getContentIdentifier(),
268 aUnc );
270 if( err )
271 throw IllegalIdentifierException( ::rtl::OUString( OSL_LOG_PREFIX ), uno::Reference< uno::XInterface >() );
273 return Reference< XContent >( new BaseContent( m_pMyShell,xIdentifier,aUnc ) );
278 sal_Int32 SAL_CALL
279 FileProvider::compareContentIds(
280 const Reference< XContentIdentifier >& Id1,
281 const Reference< XContentIdentifier >& Id2 )
282 throw( RuntimeException )
284 init();
285 rtl::OUString aUrl1 = Id1->getContentIdentifier();
286 rtl::OUString aUrl2 = Id2->getContentIdentifier();
288 sal_Int32 iComp = aUrl1.compareTo( aUrl2 );
290 if ( 0 != iComp )
292 rtl::OUString aPath1, aPath2;
294 m_pMyShell->getUnqFromUrl( aUrl1, aPath1 );
295 m_pMyShell->getUnqFromUrl( aUrl2, aPath2 );
297 osl::FileBase::RC error;
298 osl::DirectoryItem aItem1, aItem2;
300 error = osl::DirectoryItem::get( aPath1, aItem1 );
301 if ( error == osl::FileBase::E_None )
302 error = osl::DirectoryItem::get( aPath2, aItem2 );
304 if ( error != osl::FileBase::E_None )
305 return iComp;
307 osl::FileStatus aStatus1( osl_FileStatus_Mask_FileURL );
308 osl::FileStatus aStatus2( osl_FileStatus_Mask_FileURL );
309 error = aItem1.getFileStatus( aStatus1 );
310 if ( error == osl::FileBase::E_None )
311 error = aItem2.getFileStatus( aStatus2 );
313 if ( error == osl::FileBase::E_None )
315 iComp = aStatus1.getFileURL().compareTo( aStatus2.getFileURL() );
317 // Quick hack for Windows to threat all file systems as case insensitive
318 #ifdef WNT
319 if ( 0 != iComp )
321 error = osl::FileBase::getSystemPathFromFileURL( aStatus1.getFileURL(), aPath1 );
322 if ( error == osl::FileBase::E_None )
323 error = osl::FileBase::getSystemPathFromFileURL( aStatus2.getFileURL(), aPath2 );
325 if ( error == osl::FileBase::E_None )
326 iComp = rtl_ustr_compareIgnoreAsciiCase( aPath1.getStr(), aPath2.getStr() );
328 #endif
332 return iComp;
337 Reference< XContentIdentifier > SAL_CALL
338 FileProvider::createContentIdentifier(
339 const rtl::OUString& ContentId )
340 throw( RuntimeException )
342 init();
343 FileContentIdentifier* p = new FileContentIdentifier( m_pMyShell,ContentId,false );
344 return Reference< XContentIdentifier >( p );
349 //XPropertySetInfoImpl
351 class XPropertySetInfoImpl2
352 : public cppu::OWeakObject,
353 public XPropertySetInfo
355 public:
356 XPropertySetInfoImpl2();
357 ~XPropertySetInfoImpl2();
359 // XInterface
360 virtual Any SAL_CALL
361 queryInterface(
362 const Type& aType )
363 throw( RuntimeException);
365 virtual void SAL_CALL
366 acquire(
367 void )
368 throw();
370 virtual void SAL_CALL
371 release(
372 void )
373 throw();
376 virtual Sequence< Property > SAL_CALL
377 getProperties(
378 void )
379 throw( RuntimeException );
381 virtual Property SAL_CALL
382 getPropertyByName(
383 const rtl::OUString& aName )
384 throw( UnknownPropertyException,
385 RuntimeException);
387 virtual sal_Bool SAL_CALL
388 hasPropertyByName( const rtl::OUString& Name )
389 throw( RuntimeException );
392 private:
393 Sequence< Property > m_seq;
397 XPropertySetInfoImpl2::XPropertySetInfoImpl2()
398 : m_seq( 3 )
400 m_seq[0] = Property( rtl::OUString("HostName"),
402 getCppuType( static_cast< rtl::OUString* >( 0 ) ),
403 PropertyAttribute::READONLY );
405 m_seq[1] = Property( rtl::OUString("HomeDirectory"),
407 getCppuType( static_cast< rtl::OUString* >( 0 ) ),
408 PropertyAttribute::READONLY );
410 m_seq[2] = Property( rtl::OUString("FileSystemNotation"),
412 getCppuType( static_cast< sal_Int32* >( 0 ) ),
413 PropertyAttribute::READONLY );
417 XPropertySetInfoImpl2::~XPropertySetInfoImpl2()
419 // nothing
423 void SAL_CALL
424 XPropertySetInfoImpl2::acquire(
425 void )
426 throw()
428 OWeakObject::acquire();
432 void SAL_CALL
433 XPropertySetInfoImpl2::release(
434 void )
435 throw()
437 OWeakObject::release();
441 Any SAL_CALL
442 XPropertySetInfoImpl2::queryInterface(
443 const Type& rType )
444 throw( RuntimeException )
446 Any aRet = cppu::queryInterface( rType,
447 (static_cast< XPropertySetInfo* >(this)) );
448 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
452 Property SAL_CALL
453 XPropertySetInfoImpl2::getPropertyByName(
454 const rtl::OUString& aName )
455 throw( UnknownPropertyException,
456 RuntimeException)
458 for( sal_Int32 i = 0; i < m_seq.getLength(); ++i )
459 if( m_seq[i].Name == aName )
460 return m_seq[i];
462 throw UnknownPropertyException( ::rtl::OUString( OSL_LOG_PREFIX ), uno::Reference< uno::XInterface >() );
467 Sequence< Property > SAL_CALL
468 XPropertySetInfoImpl2::getProperties(
469 void )
470 throw( RuntimeException )
472 return m_seq;
476 sal_Bool SAL_CALL
477 XPropertySetInfoImpl2::hasPropertyByName(
478 const rtl::OUString& aName )
479 throw( RuntimeException )
481 for( sal_Int32 i = 0; i < m_seq.getLength(); ++i )
482 if( m_seq[i].Name == aName )
483 return true;
484 return false;
491 void SAL_CALL FileProvider::initProperties( void )
493 osl::MutexGuard aGuard( m_aMutex );
494 if( ! m_xPropertySetInfo.is() )
496 osl_getLocalHostname( &m_HostName.pData );
498 #if defined ( UNX )
499 m_FileSystemNotation = FileSystemNotation::UNIX_NOTATION;
500 #elif defined( WNT )
501 m_FileSystemNotation = FileSystemNotation::DOS_NOTATION;
502 #else
503 m_FileSystemNotation = FileSystemNotation::UNKNOWN_NOTATION;
504 #endif
505 osl::Security aSecurity;
506 aSecurity.getHomeDir( m_HomeDirectory );
508 // static const sal_Int32 UNKNOWN_NOTATION = (sal_Int32)0;
509 // static const sal_Int32 UNIX_NOTATION = (sal_Int32)1;
510 // static const sal_Int32 DOS_NOTATION = (sal_Int32)2;
511 // static const sal_Int32 MAC_NOTATION = (sal_Int32)3;
513 XPropertySetInfoImpl2* p = new XPropertySetInfoImpl2();
514 m_xPropertySetInfo = Reference< XPropertySetInfo >( p );
519 // XPropertySet
521 Reference< XPropertySetInfo > SAL_CALL
522 FileProvider::getPropertySetInfo( )
523 throw( RuntimeException )
525 initProperties();
526 return m_xPropertySetInfo;
530 void SAL_CALL
531 FileProvider::setPropertyValue( const rtl::OUString& aPropertyName,
532 const Any& )
533 throw( UnknownPropertyException,
534 PropertyVetoException,
535 IllegalArgumentException,
536 WrappedTargetException,
537 RuntimeException )
539 if( aPropertyName.compareToAscii( "FileSystemNotation" ) == 0 ||
540 aPropertyName.compareToAscii( "HomeDirectory" ) == 0 ||
541 aPropertyName.compareToAscii( "HostName" ) == 0 )
542 return;
543 else
544 throw UnknownPropertyException( ::rtl::OUString( OSL_LOG_PREFIX ), uno::Reference< uno::XInterface >() );
549 Any SAL_CALL
550 FileProvider::getPropertyValue(
551 const rtl::OUString& aPropertyName )
552 throw( UnknownPropertyException,
553 WrappedTargetException,
554 RuntimeException )
556 initProperties();
557 if( aPropertyName.compareToAscii( "FileSystemNotation" ) == 0 )
559 Any aAny;
560 aAny <<= m_FileSystemNotation;
561 return aAny;
563 else if( aPropertyName.compareToAscii( "HomeDirectory" ) == 0 )
565 Any aAny;
566 aAny <<= m_HomeDirectory;
567 return aAny;
569 else if( aPropertyName.compareToAscii( "HostName" ) == 0 )
571 Any aAny;
572 aAny <<= m_HostName;
573 return aAny;
575 else
576 throw UnknownPropertyException( ::rtl::OUString( OSL_LOG_PREFIX ), uno::Reference< uno::XInterface >() );
580 void SAL_CALL
581 FileProvider::addPropertyChangeListener(
582 const rtl::OUString&,
583 const Reference< XPropertyChangeListener >& )
584 throw( UnknownPropertyException,
585 WrappedTargetException,
586 RuntimeException)
588 return;
592 void SAL_CALL
593 FileProvider::removePropertyChangeListener(
594 const rtl::OUString&,
595 const Reference< XPropertyChangeListener >& )
596 throw( UnknownPropertyException,
597 WrappedTargetException,
598 RuntimeException )
600 return;
603 void SAL_CALL
604 FileProvider::addVetoableChangeListener(
605 const rtl::OUString&,
606 const Reference< XVetoableChangeListener >& )
607 throw( UnknownPropertyException,
608 WrappedTargetException,
609 RuntimeException )
611 return;
615 void SAL_CALL
616 FileProvider::removeVetoableChangeListener(
617 const rtl::OUString&,
618 const Reference< XVetoableChangeListener >& )
619 throw( UnknownPropertyException,
620 WrappedTargetException,
621 RuntimeException)
623 return;
628 // XFileIdentifierConverter
630 sal_Int32 SAL_CALL
631 FileProvider::getFileProviderLocality( const rtl::OUString& BaseURL )
632 throw( RuntimeException )
634 // If the base URL is a 'file' URL, return 10 (very 'local'), otherwise
635 // return -1 (missmatch). What is missing is a fast comparison to ASCII,
636 // ignoring case:
637 return BaseURL.getLength() >= 5
638 && (BaseURL[0] == 'F' || BaseURL[0] == 'f')
639 && (BaseURL[1] == 'I' || BaseURL[1] == 'i')
640 && (BaseURL[2] == 'L' || BaseURL[2] == 'l')
641 && (BaseURL[3] == 'E' || BaseURL[3] == 'e')
642 && BaseURL[4] == ':' ?
643 10 : -1;
646 rtl::OUString SAL_CALL FileProvider::getFileURLFromSystemPath( const rtl::OUString&,
647 const rtl::OUString& SystemPath )
648 throw( RuntimeException )
650 rtl::OUString aNormalizedPath;
651 if ( osl::FileBase::getFileURLFromSystemPath( SystemPath,aNormalizedPath ) != osl::FileBase::E_None )
652 return rtl::OUString();
654 return aNormalizedPath;
657 rtl::OUString SAL_CALL FileProvider::getSystemPathFromFileURL( const rtl::OUString& URL )
658 throw( RuntimeException )
660 rtl::OUString aSystemPath;
661 if (osl::FileBase::getSystemPathFromFileURL( URL,aSystemPath ) != osl::FileBase::E_None )
662 return rtl::OUString();
664 return aSystemPath;
667 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */