1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fsfactory.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "fsfactory.hxx"
35 #include "cppuhelper/factory.hxx"
36 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
37 #include <com/sun/star/embed/ElementModes.hpp>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/io/XSeekable.hpp>
42 #include <ucbhelper/fileidentifierconverter.hxx>
43 #include <ucbhelper/contentbroker.hxx>
44 #include <ucbhelper/content.hxx>
46 #include <unotools/tempfile.hxx>
47 #include <unotools/ucbhelper.hxx>
49 #include "fsstorage.hxx"
52 using namespace ::com::sun::star
;
54 //-------------------------------------------------------------------------
55 uno::Sequence
< ::rtl::OUString
> SAL_CALL
FSStorageFactory::impl_staticGetSupportedServiceNames()
57 uno::Sequence
< ::rtl::OUString
> aRet(2);
58 aRet
[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.FileSystemStorageFactory");
59 aRet
[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.FileSystemStorageFactory");
63 //-------------------------------------------------------------------------
64 ::rtl::OUString SAL_CALL
FSStorageFactory::impl_staticGetImplementationName()
66 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.FileSystemStorageFactory");
69 //-------------------------------------------------------------------------
70 uno::Reference
< uno::XInterface
> SAL_CALL
FSStorageFactory::impl_staticCreateSelfInstance(
71 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
73 return uno::Reference
< uno::XInterface
>( *new FSStorageFactory( xServiceManager
) );
76 //-------------------------------------------------------------------------
77 uno::Reference
< uno::XInterface
> SAL_CALL
FSStorageFactory::createInstance()
78 throw ( uno::Exception
,
79 uno::RuntimeException
)
81 ::rtl::OUString aTempURL
;
83 aTempURL
= ::utl::TempFile( NULL
, sal_True
).GetURL();
85 if ( !aTempURL
.getLength() )
86 throw uno::RuntimeException(); // TODO: can not create tempfile
88 ::ucbhelper::Content
aResultContent(
89 aTempURL
, uno::Reference
< ucb::XCommandEnvironment
>() );
91 return uno::Reference
< uno::XInterface
>(
92 static_cast< OWeakObject
* >(
93 new FSStorage( aResultContent
,
94 embed::ElementModes::READWRITE
,
95 uno::Sequence
< beans::PropertyValue
>(),
100 //-------------------------------------------------------------------------
101 uno::Reference
< uno::XInterface
> SAL_CALL
FSStorageFactory::createInstanceWithArguments(
102 const uno::Sequence
< uno::Any
>& aArguments
)
103 throw ( uno::Exception
,
104 uno::RuntimeException
)
106 // The request for storage can be done with up to three arguments
108 // The first argument specifies a source for the storage
110 // The second value is a mode the storage should be open in.
111 // And the third value is a media descriptor.
113 sal_Int32 nArgNum
= aArguments
.getLength();
114 OSL_ENSURE( nArgNum
< 4, "Wrong parameter number" );
117 return createInstance();
119 // first try to retrieve storage open mode if any
120 // by default the storage will be open in readonly mode
121 sal_Int32 nStorageMode
= embed::ElementModes::READ
;
124 if( !( aArguments
[1] >>= nStorageMode
) )
126 OSL_ENSURE( sal_False
, "Wrong second argument!\n" );
127 throw uno::Exception(); // TODO: Illegal argument
129 // it's allways possible to read written storage in this implementation
130 nStorageMode
|= embed::ElementModes::READ
;
133 // retrieve storage source URL
134 ::rtl::OUString aURL
;
136 if ( aArguments
[0] >>= aURL
)
138 if ( !aURL
.getLength() )
140 OSL_ENSURE( sal_False
, "Empty URL is provided!\n" );
141 throw uno::Exception(); // TODO: illegal argument
146 OSL_ENSURE( sal_False
, "Wrong first argument!\n" );
147 throw uno::Exception(); // TODO: Illegal argument
150 // retrieve mediadescriptor and set storage properties
151 uno::Sequence
< beans::PropertyValue
> aDescr
;
152 uno::Sequence
< beans::PropertyValue
> aPropsToSet
;
156 if( aArguments
[2] >>= aDescr
)
158 aPropsToSet
.realloc(1);
159 aPropsToSet
[0].Name
= ::rtl::OUString::createFromAscii( "URL" );
160 aPropsToSet
[0].Value
<<= aURL
;
162 for ( sal_Int32 nInd
= 0, nNumArgs
= 1; nInd
< aDescr
.getLength(); nInd
++ )
164 if ( aDescr
[nInd
].Name
.equalsAscii( "InteractionHandler" ) )
166 aPropsToSet
.realloc( ++nNumArgs
);
167 aPropsToSet
[nNumArgs
-1].Name
= aDescr
[nInd
].Name
;
168 aPropsToSet
[nNumArgs
-1].Value
= aDescr
[nInd
].Value
;
172 OSL_ENSURE( sal_False
, "Unacceptable property, will be ignored!\n" );
177 OSL_ENSURE( sal_False
, "Wrong third argument!\n" );
178 throw uno::Exception(); // TODO: Illegal argument
182 // allow to use other ucp's
183 // if ( !isLocalNotFile_Impl( aURL ) )
184 if ( aURL
.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.pkg", 16 )
185 || aURL
.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.zip", 16 )
186 || ::utl::UCBContentHelper::IsDocument( aURL
) )
188 OSL_ENSURE( sal_False
, "File system storages can be based only on file URLs!\n" ); // ???
189 throw uno::Exception(); // TODO: illegal argument
192 if ( ( nStorageMode
& embed::ElementModes::WRITE
) && !( nStorageMode
& embed::ElementModes::NOCREATE
) )
193 FSStorage::MakeFolderNoUI( aURL
, sal_False
);
194 else if ( !::utl::UCBContentHelper::IsFolder( aURL
) )
195 throw io::IOException(); // there is no such folder
197 ::ucbhelper::Content
aResultContent(
198 aURL
, uno::Reference
< ucb::XCommandEnvironment
>() );
200 // create storage based on source
201 return uno::Reference
< uno::XInterface
>(
202 static_cast< OWeakObject
* >( new FSStorage( aResultContent
,
209 //-------------------------------------------------------------------------
210 ::rtl::OUString SAL_CALL
FSStorageFactory::getImplementationName()
211 throw ( uno::RuntimeException
)
213 return impl_staticGetImplementationName();
216 //-------------------------------------------------------------------------
217 sal_Bool SAL_CALL
FSStorageFactory::supportsService( const ::rtl::OUString
& ServiceName
)
218 throw ( uno::RuntimeException
)
220 uno::Sequence
< ::rtl::OUString
> aSeq
= impl_staticGetSupportedServiceNames();
222 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
223 if ( ServiceName
.compareTo( aSeq
[nInd
] ) == 0 )
229 //-------------------------------------------------------------------------
230 uno::Sequence
< ::rtl::OUString
> SAL_CALL
FSStorageFactory::getSupportedServiceNames()
231 throw ( uno::RuntimeException
)
233 return impl_staticGetSupportedServiceNames();
236 //-------------------------------------------------------------------------
240 SAL_DLLPUBLIC_EXPORT
void SAL_CALL
component_getImplementationEnvironment (
241 const sal_Char
** ppEnvTypeName
, uno_Environment
** /* ppEnv */)
243 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
246 SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
component_writeInfo (
247 void * /* pServiceManager */, void * pRegistryKey
)
251 uno::Reference
< registry::XRegistryKey
> xRegistryKey (
252 reinterpret_cast< registry::XRegistryKey
*>(pRegistryKey
));
254 uno::Reference
< registry::XRegistryKey
> xNewKey
;
255 xNewKey
= xRegistryKey
->createKey(
256 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/") ) +
257 FSStorageFactory::impl_staticGetImplementationName() +
258 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "/UNO/SERVICES")));
260 const uno::Sequence
< ::rtl::OUString
> aServices (
261 FSStorageFactory::impl_staticGetSupportedServiceNames());
262 for( sal_Int32 i
= 0; i
< aServices
.getLength(); i
++ )
263 xNewKey
->createKey( aServices
.getConstArray()[i
] );
270 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory (
271 const sal_Char
* pImplementationName
, void * pServiceManager
, void * /* pRegistryKey */)
276 uno::Reference
< lang::XSingleServiceFactory
> xFactory
;
277 if (FSStorageFactory::impl_staticGetImplementationName().compareToAscii (pImplementationName
) == 0)
279 xFactory
= cppu::createOneInstanceFactory (
280 reinterpret_cast< lang::XMultiServiceFactory
* >(pServiceManager
),
281 FSStorageFactory::impl_staticGetImplementationName(),
282 FSStorageFactory::impl_staticCreateSelfInstance
,
283 FSStorageFactory::impl_staticGetSupportedServiceNames() );
288 pResult
= xFactory
.get();