xref: /aoo41x/main/svl/source/fsstor/fsfactory.cxx (revision 40df464e)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "fsfactory.hxx"
28cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
29cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
32cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <ucbhelper/fileidentifierconverter.hxx>
36cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
37cdf0e10cSrcweir #include <ucbhelper/content.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <unotools/tempfile.hxx>
40cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include "fsstorage.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticGetSupportedServiceNames()48cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL FSStorageFactory::impl_staticGetSupportedServiceNames()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aRet(2);
51cdf0e10cSrcweir     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.FileSystemStorageFactory");
52cdf0e10cSrcweir     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.FileSystemStorageFactory");
53cdf0e10cSrcweir     return aRet;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticGetImplementationName()57cdf0e10cSrcweir ::rtl::OUString SAL_CALL FSStorageFactory::impl_staticGetImplementationName()
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.FileSystemStorageFactory");
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)63cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::impl_staticCreateSelfInstance(
64cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >( *new FSStorageFactory( xServiceManager ) );
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir //-------------------------------------------------------------------------
createInstance()70cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
71cdf0e10cSrcweir 	throw ( uno::Exception,
72cdf0e10cSrcweir 			uno::RuntimeException )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	::rtl::OUString aTempURL;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	aTempURL = ::utl::TempFile( NULL, sal_True ).GetURL();
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	if ( !aTempURL.getLength() )
79cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO: can not create tempfile
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	::ucbhelper::Content aResultContent(
82cdf0e10cSrcweir         aTempURL, uno::Reference< ucb::XCommandEnvironment >() );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >(
85cdf0e10cSrcweir         static_cast< OWeakObject* >(
86cdf0e10cSrcweir             new FSStorage(	aResultContent,
87cdf0e10cSrcweir                             embed::ElementModes::READWRITE,
88cdf0e10cSrcweir                             uno::Sequence< beans::PropertyValue >(),
89cdf0e10cSrcweir                             m_xFactory ) ),
90cdf0e10cSrcweir         uno::UNO_QUERY );
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir //-------------------------------------------------------------------------
createInstanceWithArguments(const uno::Sequence<uno::Any> & aArguments)94cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
95cdf0e10cSrcweir 			const uno::Sequence< uno::Any >& aArguments )
96cdf0e10cSrcweir 	throw ( uno::Exception,
97cdf0e10cSrcweir 			uno::RuntimeException )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	// The request for storage can be done with up to three arguments
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	// The first argument specifies a source for the storage
102cdf0e10cSrcweir 	// it must be URL.
103cdf0e10cSrcweir 	// The second value is a mode the storage should be open in.
104cdf0e10cSrcweir 	// And the third value is a media descriptor.
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	sal_Int32 nArgNum = aArguments.getLength();
107cdf0e10cSrcweir 	OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	if ( !nArgNum )
110cdf0e10cSrcweir 		return createInstance();
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	// first try to retrieve storage open mode if any
113cdf0e10cSrcweir 	// by default the storage will be open in readonly mode
114cdf0e10cSrcweir 	sal_Int32 nStorageMode = embed::ElementModes::READ;
115cdf0e10cSrcweir 	if ( nArgNum >= 2 )
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		if( !( aArguments[1] >>= nStorageMode ) )
118cdf0e10cSrcweir 		{
119cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Wrong second argument!\n" );
120cdf0e10cSrcweir 			throw uno::Exception(); // TODO: Illegal argument
121cdf0e10cSrcweir 		}
122cdf0e10cSrcweir 		// it's allways possible to read written storage in this implementation
123cdf0e10cSrcweir 		nStorageMode |= embed::ElementModes::READ;
124cdf0e10cSrcweir 	}
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	// retrieve storage source URL
127cdf0e10cSrcweir 	::rtl::OUString aURL;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	if ( aArguments[0] >>= aURL )
130cdf0e10cSrcweir 	{
131cdf0e10cSrcweir 		if ( !aURL.getLength() )
132cdf0e10cSrcweir 		{
133cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Empty URL is provided!\n" );
134cdf0e10cSrcweir 			throw uno::Exception(); // TODO: illegal argument
135cdf0e10cSrcweir 		}
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 	else
138cdf0e10cSrcweir 	{
139cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Wrong first argument!\n" );
140cdf0e10cSrcweir 		throw uno::Exception(); // TODO: Illegal argument
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	// retrieve mediadescriptor and set storage properties
144cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aDescr;
145cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aPropsToSet;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	if ( nArgNum >= 3 )
148cdf0e10cSrcweir 	{
149cdf0e10cSrcweir 		if( aArguments[2] >>= aDescr )
150cdf0e10cSrcweir 		{
151cdf0e10cSrcweir 			aPropsToSet.realloc(1);
152cdf0e10cSrcweir 			aPropsToSet[0].Name = ::rtl::OUString::createFromAscii( "URL" );
153cdf0e10cSrcweir 			aPropsToSet[0].Value <<= aURL;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ )
156cdf0e10cSrcweir 			{
157cdf0e10cSrcweir 				if ( aDescr[nInd].Name.equalsAscii( "InteractionHandler" ) )
158cdf0e10cSrcweir 				{
159cdf0e10cSrcweir 					aPropsToSet.realloc( ++nNumArgs );
160cdf0e10cSrcweir 					aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name;
161cdf0e10cSrcweir 					aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value;
162cdf0e10cSrcweir 					break;
163cdf0e10cSrcweir 				}
164cdf0e10cSrcweir 				else
165cdf0e10cSrcweir 					OSL_ENSURE( sal_False, "Unacceptable property, will be ignored!\n" );
166cdf0e10cSrcweir 			}
167cdf0e10cSrcweir 		}
168cdf0e10cSrcweir 		else
169cdf0e10cSrcweir 		{
170cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Wrong third argument!\n" );
171cdf0e10cSrcweir 			throw uno::Exception(); // TODO: Illegal argument
172cdf0e10cSrcweir 		}
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	// allow to use other ucp's
176cdf0e10cSrcweir 	// if ( !isLocalNotFile_Impl( aURL ) )
177cdf0e10cSrcweir 	if ( aURL.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.pkg", 16 )
178cdf0e10cSrcweir 	  || aURL.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.zip", 16 )
179cdf0e10cSrcweir 	  || ::utl::UCBContentHelper::IsDocument( aURL ) )
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "File system storages can be based only on file URLs!\n" ); // ???
182cdf0e10cSrcweir 		throw uno::Exception(); // TODO: illegal argument
183cdf0e10cSrcweir 	}
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
186cdf0e10cSrcweir 		FSStorage::MakeFolderNoUI( aURL, sal_False );
187cdf0e10cSrcweir 	else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
188cdf0e10cSrcweir 		throw io::IOException(); // there is no such folder
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	::ucbhelper::Content aResultContent(
191cdf0e10cSrcweir         aURL, uno::Reference< ucb::XCommandEnvironment >() );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	// create storage based on source
194cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >(
195cdf0e10cSrcweir         static_cast< OWeakObject* >( new FSStorage( aResultContent,
196cdf0e10cSrcweir                                                     nStorageMode,
197cdf0e10cSrcweir                                                     aPropsToSet,
198cdf0e10cSrcweir                                                     m_xFactory ) ),
199cdf0e10cSrcweir         uno::UNO_QUERY );
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir //-------------------------------------------------------------------------
getImplementationName()203cdf0e10cSrcweir ::rtl::OUString SAL_CALL FSStorageFactory::getImplementationName()
204cdf0e10cSrcweir 	throw ( uno::RuntimeException )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	return impl_staticGetImplementationName();
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)210cdf0e10cSrcweir sal_Bool SAL_CALL FSStorageFactory::supportsService( const ::rtl::OUString& ServiceName )
211cdf0e10cSrcweir 	throw ( uno::RuntimeException )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
216cdf0e10cSrcweir     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
217cdf0e10cSrcweir         	return sal_True;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	return sal_False;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir //-------------------------------------------------------------------------
getSupportedServiceNames()223cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
224cdf0e10cSrcweir 	throw ( uno::RuntimeException )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	return impl_staticGetSupportedServiceNames();
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir //-------------------------------------------------------------------------
230cdf0e10cSrcweir 
231cdf0e10cSrcweir extern "C"
232cdf0e10cSrcweir {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)233cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment (
234cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */)
235cdf0e10cSrcweir {
236cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void *)239cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory (
240cdf0e10cSrcweir 	const sal_Char * pImplementationName, void * pServiceManager, void * /* pRegistryKey */)
241cdf0e10cSrcweir {
242cdf0e10cSrcweir 	void * pResult = 0;
243cdf0e10cSrcweir 	if (pServiceManager)
244cdf0e10cSrcweir 	{
245cdf0e10cSrcweir 		uno::Reference< lang::XSingleServiceFactory > xFactory;
246cdf0e10cSrcweir 		if (FSStorageFactory::impl_staticGetImplementationName().compareToAscii (pImplementationName) == 0)
247cdf0e10cSrcweir 		{
248cdf0e10cSrcweir 			xFactory = cppu::createOneInstanceFactory (
249cdf0e10cSrcweir 				reinterpret_cast< lang::XMultiServiceFactory* >(pServiceManager),
250cdf0e10cSrcweir 				FSStorageFactory::impl_staticGetImplementationName(),
251cdf0e10cSrcweir 				FSStorageFactory::impl_staticCreateSelfInstance,
252cdf0e10cSrcweir 				FSStorageFactory::impl_staticGetSupportedServiceNames() );
253cdf0e10cSrcweir 		}
254cdf0e10cSrcweir 		if (xFactory.is())
255cdf0e10cSrcweir 		{
256cdf0e10cSrcweir 			xFactory->acquire();
257cdf0e10cSrcweir 			pResult = xFactory.get();
258cdf0e10cSrcweir 		}
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir 	return pResult;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir } // extern "C"
264cdf0e10cSrcweir 
265