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 <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"
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
46 #define THROW_WHERE ""
50 extern "C" SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
ucpfile_component_getFactory(
51 const sal_Char
* pImplName
, void * pServiceManager
, void * )
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
);
74 pRet
= xFactory
.get();
80 /****************************************************************************/
86 /****************************************************************************/
87 FileProvider::FileProvider( const Reference
< XComponentContext
>& rxContext
)
88 : m_xContext(rxContext
)
89 , m_FileSystemNotation(FileSystemNotation::UNKNOWN_NOTATION
)
94 FileProvider::~FileProvider()
101 void SAL_CALL
FileProvider::init()
104 m_pMyShell
= new shell( m_xContext
, this, true );
109 FileProvider::initialize(
110 const Sequence
< Any
>& aArguments
)
111 throw (Exception
, RuntimeException
, std::exception
)
115 if( aArguments
.getLength() > 0 &&
116 (aArguments
[0] >>= config
) &&
117 config
== "NoConfig" )
118 m_pMyShell
= new shell( m_xContext
, this, false );
120 m_pMyShell
= new shell( m_xContext
, this, true );
124 // XServiceInfo methods.
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(
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
174 return Reference
< XSingleServiceFactory
> ( cppu::createSingleFactory(
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
);
196 Reference
< XContent
> SAL_CALL
197 FileProvider::queryContent(
198 const Reference
< XContentIdentifier
>& xIdentifier
)
199 throw( IllegalIdentifierException
,
200 RuntimeException
, std::exception
)
204 bool err
= fileaccess::shell::getUnqFromUrl( xIdentifier
->getContentIdentifier(),
208 throw IllegalIdentifierException( THROW_WHERE
);
210 return Reference
< XContent
>( new BaseContent( m_pMyShell
,xIdentifier
,aUnc
) );
216 FileProvider::compareContentIds(
217 const Reference
< XContentIdentifier
>& Id1
,
218 const Reference
< XContentIdentifier
>& Id2
)
219 throw( RuntimeException
, std::exception
)
222 OUString aUrl1
= Id1
->getContentIdentifier();
223 OUString aUrl2
= Id2
->getContentIdentifier();
225 sal_Int32 iComp
= aUrl1
.compareTo( aUrl2
);
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
)
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
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() );
274 Reference
< XContentIdentifier
> SAL_CALL
275 FileProvider::createContentIdentifier(
276 const OUString
& ContentId
)
277 throw( RuntimeException
, std::exception
)
280 FileContentIdentifier
* p
= new FileContentIdentifier( ContentId
,false );
281 return Reference
< XContentIdentifier
>( p
);
286 //XPropertySetInfoImpl
288 class XPropertySetInfoImpl2
289 : public cppu::OWeakObject
,
290 public XPropertySetInfo
293 XPropertySetInfoImpl2();
294 virtual ~XPropertySetInfoImpl2();
300 throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
302 virtual void SAL_CALL
305 throw() SAL_OVERRIDE
;
307 virtual void SAL_CALL
310 throw() SAL_OVERRIDE
;
313 virtual Sequence
< Property
> SAL_CALL
316 throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
318 virtual Property SAL_CALL
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
;
330 Sequence
< Property
> m_seq
;
334 XPropertySetInfoImpl2::XPropertySetInfoImpl2()
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()
361 XPropertySetInfoImpl2::acquire(
365 OWeakObject::acquire();
370 XPropertySetInfoImpl2::release(
374 OWeakObject::release();
379 XPropertySetInfoImpl2::queryInterface(
381 throw( RuntimeException
, std::exception
)
383 Any aRet
= cppu::queryInterface( rType
,
384 (static_cast< XPropertySetInfo
* >(this)) );
385 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
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
)
399 throw UnknownPropertyException( THROW_WHERE
);
404 Sequence
< Property
> SAL_CALL
405 XPropertySetInfoImpl2::getProperties(
407 throw( RuntimeException
, std::exception
)
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
)
428 void SAL_CALL
FileProvider::initProperties()
430 osl::MutexGuard
aGuard( m_aMutex
);
431 if( ! m_xPropertySetInfo
.is() )
433 osl_getLocalHostname( &m_HostName
.pData
);
436 m_FileSystemNotation
= FileSystemNotation::UNIX_NOTATION
;
438 m_FileSystemNotation
= FileSystemNotation::DOS_NOTATION
;
440 m_FileSystemNotation
= FileSystemNotation::UNKNOWN_NOTATION
;
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
);
458 Reference
< XPropertySetInfo
> SAL_CALL
459 FileProvider::getPropertySetInfo( )
460 throw( RuntimeException
, std::exception
)
463 return m_xPropertySetInfo
;
468 FileProvider::setPropertyValue( const OUString
& aPropertyName
,
470 throw( UnknownPropertyException
,
471 PropertyVetoException
,
472 IllegalArgumentException
,
473 WrappedTargetException
,
474 RuntimeException
, std::exception
)
476 if( aPropertyName
== "FileSystemNotation" ||
477 aPropertyName
== "HomeDirectory" ||
478 aPropertyName
== "HostName" )
481 throw UnknownPropertyException( THROW_WHERE
);
487 FileProvider::getPropertyValue(
488 const OUString
& aPropertyName
)
489 throw( UnknownPropertyException
,
490 WrappedTargetException
,
491 RuntimeException
, std::exception
)
494 if( aPropertyName
== "FileSystemNotation" )
497 aAny
<<= m_FileSystemNotation
;
500 else if( aPropertyName
== "HomeDirectory" )
503 aAny
<<= m_HomeDirectory
;
506 else if( aPropertyName
== "HostName" )
513 throw UnknownPropertyException( THROW_WHERE
);
518 FileProvider::addPropertyChangeListener(
520 const Reference
< XPropertyChangeListener
>& )
521 throw( UnknownPropertyException
,
522 WrappedTargetException
,
523 RuntimeException
, std::exception
)
530 FileProvider::removePropertyChangeListener(
532 const Reference
< XPropertyChangeListener
>& )
533 throw( UnknownPropertyException
,
534 WrappedTargetException
,
535 RuntimeException
, std::exception
)
541 FileProvider::addVetoableChangeListener(
543 const Reference
< XVetoableChangeListener
>& )
544 throw( UnknownPropertyException
,
545 WrappedTargetException
,
546 RuntimeException
, std::exception
)
553 FileProvider::removeVetoableChangeListener(
555 const Reference
< XVetoableChangeListener
>& )
556 throw( UnknownPropertyException
,
557 WrappedTargetException
,
558 RuntimeException
, std::exception
)
565 // XFileIdentifierConverter
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,
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] == ':' ?
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
)
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
)
604 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */