1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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 * )
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 //////////////////////////////////////////////////////////////////////
68 pRet
= xFactory
.get();
74 /****************************************************************************/
80 /****************************************************************************/
84 FileProvider::FileProvider( const Reference
< XComponentContext
>& rxContext
)
85 : m_xContext( rxContext
),
91 FileProvider::~FileProvider()
98 //////////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////////
103 FileProvider::acquire(
107 OWeakObject::acquire();
112 FileProvider::release(
116 OWeakObject::release();
121 FileProvider::queryInterface(
123 throw( RuntimeException
)
125 Any aRet
= cppu::queryInterface(
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 ///////////////////////////////////////////////////////////////////////////////
140 void SAL_CALL
FileProvider::init()
143 m_pMyShell
= new shell( m_xContext
, this, sal_True
);
148 FileProvider::initialize(
149 const Sequence
< Any
>& aArguments
)
150 throw (Exception
, RuntimeException
)
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
);
159 m_pMyShell
= new shell( m_xContext
, this, sal_True
);
164 ////////////////////////////////////////////////////////////////////////////////
166 // XTypeProvider methods.
169 XTYPEPROVIDER_IMPL_7( FileProvider
,
173 XContentIdentifierFactory
,
175 XFileIdentifierConverter
,
179 ////////////////////////////////////////////////////////////////////////////////
180 // XServiceInfo methods.
182 rtl::OUString SAL_CALL
183 FileProvider::getImplementationName()
184 throw( RuntimeException
)
186 return fileaccess::shell::getImplementationName_static();
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(
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
237 return Reference
< XSingleServiceFactory
> ( cppu::createSingleFactory(
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 ////////////////////////////////////////////////////////////////////////////////
256 ////////////////////////////////////////////////////////////////////////////////
259 Reference
< XContent
> SAL_CALL
260 FileProvider::queryContent(
261 const Reference
< XContentIdentifier
>& xIdentifier
)
262 throw( IllegalIdentifierException
,
267 sal_Bool err
= m_pMyShell
->getUnqFromUrl( xIdentifier
->getContentIdentifier(),
271 throw IllegalIdentifierException( ::rtl::OUString( OSL_LOG_PREFIX
), uno::Reference
< uno::XInterface
>() );
273 return Reference
< XContent
>( new BaseContent( m_pMyShell
,xIdentifier
,aUnc
) );
279 FileProvider::compareContentIds(
280 const Reference
< XContentIdentifier
>& Id1
,
281 const Reference
< XContentIdentifier
>& Id2
)
282 throw( RuntimeException
)
285 rtl::OUString aUrl1
= Id1
->getContentIdentifier();
286 rtl::OUString aUrl2
= Id2
->getContentIdentifier();
288 sal_Int32 iComp
= aUrl1
.compareTo( aUrl2
);
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
)
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
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() );
337 Reference
< XContentIdentifier
> SAL_CALL
338 FileProvider::createContentIdentifier(
339 const rtl::OUString
& ContentId
)
340 throw( RuntimeException
)
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
356 XPropertySetInfoImpl2();
357 ~XPropertySetInfoImpl2();
363 throw( RuntimeException
);
365 virtual void SAL_CALL
370 virtual void SAL_CALL
376 virtual Sequence
< Property
> SAL_CALL
379 throw( RuntimeException
);
381 virtual Property SAL_CALL
383 const rtl::OUString
& aName
)
384 throw( UnknownPropertyException
,
387 virtual sal_Bool SAL_CALL
388 hasPropertyByName( const rtl::OUString
& Name
)
389 throw( RuntimeException
);
393 Sequence
< Property
> m_seq
;
397 XPropertySetInfoImpl2::XPropertySetInfoImpl2()
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()
424 XPropertySetInfoImpl2::acquire(
428 OWeakObject::acquire();
433 XPropertySetInfoImpl2::release(
437 OWeakObject::release();
442 XPropertySetInfoImpl2::queryInterface(
444 throw( RuntimeException
)
446 Any aRet
= cppu::queryInterface( rType
,
447 (static_cast< XPropertySetInfo
* >(this)) );
448 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
453 XPropertySetInfoImpl2::getPropertyByName(
454 const rtl::OUString
& aName
)
455 throw( UnknownPropertyException
,
458 for( sal_Int32 i
= 0; i
< m_seq
.getLength(); ++i
)
459 if( m_seq
[i
].Name
== aName
)
462 throw UnknownPropertyException( ::rtl::OUString( OSL_LOG_PREFIX
), uno::Reference
< uno::XInterface
>() );
467 Sequence
< Property
> SAL_CALL
468 XPropertySetInfoImpl2::getProperties(
470 throw( RuntimeException
)
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
)
491 void SAL_CALL
FileProvider::initProperties( void )
493 osl::MutexGuard
aGuard( m_aMutex
);
494 if( ! m_xPropertySetInfo
.is() )
496 osl_getLocalHostname( &m_HostName
.pData
);
499 m_FileSystemNotation
= FileSystemNotation::UNIX_NOTATION
;
501 m_FileSystemNotation
= FileSystemNotation::DOS_NOTATION
;
503 m_FileSystemNotation
= FileSystemNotation::UNKNOWN_NOTATION
;
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
);
521 Reference
< XPropertySetInfo
> SAL_CALL
522 FileProvider::getPropertySetInfo( )
523 throw( RuntimeException
)
526 return m_xPropertySetInfo
;
531 FileProvider::setPropertyValue( const rtl::OUString
& aPropertyName
,
533 throw( UnknownPropertyException
,
534 PropertyVetoException
,
535 IllegalArgumentException
,
536 WrappedTargetException
,
539 if( aPropertyName
.compareToAscii( "FileSystemNotation" ) == 0 ||
540 aPropertyName
.compareToAscii( "HomeDirectory" ) == 0 ||
541 aPropertyName
.compareToAscii( "HostName" ) == 0 )
544 throw UnknownPropertyException( ::rtl::OUString( OSL_LOG_PREFIX
), uno::Reference
< uno::XInterface
>() );
550 FileProvider::getPropertyValue(
551 const rtl::OUString
& aPropertyName
)
552 throw( UnknownPropertyException
,
553 WrappedTargetException
,
557 if( aPropertyName
.compareToAscii( "FileSystemNotation" ) == 0 )
560 aAny
<<= m_FileSystemNotation
;
563 else if( aPropertyName
.compareToAscii( "HomeDirectory" ) == 0 )
566 aAny
<<= m_HomeDirectory
;
569 else if( aPropertyName
.compareToAscii( "HostName" ) == 0 )
576 throw UnknownPropertyException( ::rtl::OUString( OSL_LOG_PREFIX
), uno::Reference
< uno::XInterface
>() );
581 FileProvider::addPropertyChangeListener(
582 const rtl::OUString
&,
583 const Reference
< XPropertyChangeListener
>& )
584 throw( UnknownPropertyException
,
585 WrappedTargetException
,
593 FileProvider::removePropertyChangeListener(
594 const rtl::OUString
&,
595 const Reference
< XPropertyChangeListener
>& )
596 throw( UnknownPropertyException
,
597 WrappedTargetException
,
604 FileProvider::addVetoableChangeListener(
605 const rtl::OUString
&,
606 const Reference
< XVetoableChangeListener
>& )
607 throw( UnknownPropertyException
,
608 WrappedTargetException
,
616 FileProvider::removeVetoableChangeListener(
617 const rtl::OUString
&,
618 const Reference
< XVetoableChangeListener
>& )
619 throw( UnknownPropertyException
,
620 WrappedTargetException
,
628 // XFileIdentifierConverter
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,
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] == ':' ?
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();
667 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */