xref: /aoo41x/main/package/source/xstor/xfactory.cxx (revision a3872823)
1*a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a3872823SAndrew Rist  * distributed with this work for additional information
6*a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9*a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
13*a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a3872823SAndrew Rist  * software distributed under the License is distributed on an
15*a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a3872823SAndrew Rist  * specific language governing permissions and limitations
18*a3872823SAndrew Rist  * under the License.
19*a3872823SAndrew Rist  *
20*a3872823SAndrew Rist  *************************************************************/
21*a3872823SAndrew Rist 
22*a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
27cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
28cdf0e10cSrcweir #include <com/sun/star/embed/StorageFormats.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "xfactory.hxx"
35cdf0e10cSrcweir #include "xstorage.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::com::sun::star;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //-------------------------------------------------------------------------
CheckPackageSignature_Impl(const uno::Reference<io::XInputStream> & xInputStream,const uno::Reference<io::XSeekable> & xSeekable)41cdf0e10cSrcweir sal_Bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream,
42cdf0e10cSrcweir 									 const uno::Reference< io::XSeekable >& xSeekable )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	if ( !xInputStream.is() || !xSeekable.is() )
45cdf0e10cSrcweir 		throw uno::RuntimeException();
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	if ( xSeekable->getLength() )
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 		uno::Sequence< sal_Int8 > aData( 4 );
50cdf0e10cSrcweir 		xSeekable->seek( 0 );
51cdf0e10cSrcweir 		sal_Int32 nRead = xInputStream->readBytes( aData, 4 );
52cdf0e10cSrcweir 		xSeekable->seek( 0 );
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 		// TODO/LATER: should the disk spanned files be supported?
55cdf0e10cSrcweir     	// 0x50, 0x4b, 0x07, 0x08
56cdf0e10cSrcweir 		return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 );
57cdf0e10cSrcweir 	}
58cdf0e10cSrcweir 	else
59cdf0e10cSrcweir 		return sal_True; // allow to create a storage based on empty stream
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticGetSupportedServiceNames()63cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OStorageFactory::impl_staticGetSupportedServiceNames()
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aRet(2);
66cdf0e10cSrcweir     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.StorageFactory");
67cdf0e10cSrcweir     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.StorageFactory");
68cdf0e10cSrcweir     return aRet;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticGetImplementationName()72cdf0e10cSrcweir ::rtl::OUString SAL_CALL OStorageFactory::impl_staticGetImplementationName()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.StorageFactory");
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)78cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::impl_staticCreateSelfInstance(
79cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >( *new OStorageFactory( xServiceManager ) );
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir //-------------------------------------------------------------------------
createInstance()85cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance()
86cdf0e10cSrcweir 	throw ( uno::Exception,
87cdf0e10cSrcweir 			uno::RuntimeException )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 	// TODO: reimplement TempStream service to support XStream interface
90cdf0e10cSrcweir 	uno::Reference < io::XStream > xTempStream(
91cdf0e10cSrcweir 						m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
92cdf0e10cSrcweir 						uno::UNO_QUERY );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	if ( !xTempStream.is() )
95cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO:
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >(
98cdf0e10cSrcweir 				static_cast< OWeakObject* >( new OStorage(	xTempStream,
99cdf0e10cSrcweir 															embed::ElementModes::READWRITE,
100cdf0e10cSrcweir 															uno::Sequence< beans::PropertyValue >(),
101cdf0e10cSrcweir 															m_xFactory,
102cdf0e10cSrcweir 															embed::StorageFormats::PACKAGE ) ),
103cdf0e10cSrcweir 				uno::UNO_QUERY );
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir //-------------------------------------------------------------------------
createInstanceWithArguments(const uno::Sequence<uno::Any> & aArguments)107cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments(
108cdf0e10cSrcweir 			const uno::Sequence< uno::Any >& aArguments )
109cdf0e10cSrcweir 	throw ( uno::Exception,
110cdf0e10cSrcweir 			uno::RuntimeException )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	// The request for storage can be done with up to three arguments
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	// The first argument specifies a source for the storage
115cdf0e10cSrcweir 	// it can be URL, XStream, XInputStream.
116cdf0e10cSrcweir 	// The second value is a mode the storage should be open in.
117cdf0e10cSrcweir 	// And the third value is a media descriptor.
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	sal_Int32 nArgNum = aArguments.getLength();
120cdf0e10cSrcweir 	OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if ( !nArgNum )
123cdf0e10cSrcweir 		return createInstance();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	// first try to retrieve storage open mode if any
126cdf0e10cSrcweir 	// by default the storage will be open in readonly mode
127cdf0e10cSrcweir 	sal_Int32 nStorageMode = embed::ElementModes::READ;
128cdf0e10cSrcweir 	if ( nArgNum >= 2 )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		if( !( aArguments[1] >>= nStorageMode ) )
131cdf0e10cSrcweir 		{
132cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Wrong second argument!\n" );
133cdf0e10cSrcweir 			throw lang::IllegalArgumentException(); // TODO:
134cdf0e10cSrcweir 		}
135cdf0e10cSrcweir 		// it's allways possible to read written storage in this implementation
136cdf0e10cSrcweir 		nStorageMode |= embed::ElementModes::READ;
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
140cdf0e10cSrcweir 	  && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
141cdf0e10cSrcweir 		throw lang::IllegalArgumentException(); // TODO:
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	// retrieve storage source stream
144cdf0e10cSrcweir 	::rtl::OUString aURL;
145cdf0e10cSrcweir 	uno::Reference< io::XStream > xStream;
146cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInputStream;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	if ( aArguments[0] >>= aURL )
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		if ( !aURL.getLength() )
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Empty URL is provided!\n" );
153cdf0e10cSrcweir 			throw lang::IllegalArgumentException(); // TODO:
154cdf0e10cSrcweir 		}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 		if ( aURL.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.pkg", 16 ) )
157cdf0e10cSrcweir 		{
158cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Packages URL's are not valid for storages!\n" ); // ???
159cdf0e10cSrcweir 			throw lang::IllegalArgumentException(); // TODO:
160cdf0e10cSrcweir 		}
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 		uno::Reference < ::com::sun::star::ucb::XSimpleFileAccess > xTempAccess(
163cdf0e10cSrcweir 				m_xFactory->createInstance (
164cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
165cdf0e10cSrcweir 				uno::UNO_QUERY );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 		if ( !xTempAccess.is() )
168cdf0e10cSrcweir 			throw uno::RuntimeException(); // TODO:
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 		if ( nStorageMode & embed::ElementModes::WRITE )
171cdf0e10cSrcweir 			xStream = xTempAccess->openFileReadWrite( aURL );
172cdf0e10cSrcweir 		else
173cdf0e10cSrcweir 			xInputStream = xTempAccess->openFileRead( aURL );
174cdf0e10cSrcweir 	}
175cdf0e10cSrcweir 	else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Wrong first argument!\n" );
178cdf0e10cSrcweir 		throw uno::Exception(); // TODO: Illegal argument
179cdf0e10cSrcweir 	}
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	// retrieve mediadescriptor and set storage properties
182cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aDescr;
183cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aPropsToSet;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	if ( nArgNum >= 3 )
188cdf0e10cSrcweir 	{
189cdf0e10cSrcweir 		if( aArguments[2] >>= aDescr )
190cdf0e10cSrcweir 		{
191cdf0e10cSrcweir 			if ( aURL.getLength() )
192cdf0e10cSrcweir 			{
193cdf0e10cSrcweir 				aPropsToSet.realloc(1);
194cdf0e10cSrcweir 				aPropsToSet[0].Name = ::rtl::OUString::createFromAscii( "URL" );
195cdf0e10cSrcweir 				aPropsToSet[0].Value <<= aURL;
196cdf0e10cSrcweir 			}
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 			for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ )
199cdf0e10cSrcweir 			{
200cdf0e10cSrcweir 				if ( aDescr[nInd].Name.equalsAscii( "InteractionHandler" )
201cdf0e10cSrcweir 				  || aDescr[nInd].Name.equalsAscii( "Password" )
202cdf0e10cSrcweir 				  || aDescr[nInd].Name.equalsAscii( "RepairPackage" )
203cdf0e10cSrcweir 				  || aDescr[nInd].Name.equalsAscii( "StatusIndicator" ) )
204cdf0e10cSrcweir 				  // || aDescr[nInd].Name.equalsAscii( "Unpacked" ) // TODO:
205cdf0e10cSrcweir 				{
206cdf0e10cSrcweir 					aPropsToSet.realloc( ++nNumArgs );
207cdf0e10cSrcweir 					aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name;
208cdf0e10cSrcweir 					aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value;
209cdf0e10cSrcweir 				}
210cdf0e10cSrcweir 				else if ( aDescr[nInd].Name.equalsAscii( "StorageFormat" ) )
211cdf0e10cSrcweir 				{
212cdf0e10cSrcweir 					::rtl::OUString aFormatName;
213cdf0e10cSrcweir                     sal_Int32 nFormatID = 0;
214cdf0e10cSrcweir                     if ( aDescr[nInd].Value >>= aFormatName )
215cdf0e10cSrcweir                     {
216cdf0e10cSrcweir                         if ( aFormatName.equals( PACKAGE_STORAGE_FORMAT_STRING ) )
217cdf0e10cSrcweir                             nStorageType = embed::StorageFormats::PACKAGE;
218cdf0e10cSrcweir                         else if ( aFormatName.equals( ZIP_STORAGE_FORMAT_STRING ) )
219cdf0e10cSrcweir                             nStorageType = embed::StorageFormats::ZIP;
220cdf0e10cSrcweir                         else if ( aFormatName.equals( OFOPXML_STORAGE_FORMAT_STRING ) )
221cdf0e10cSrcweir                             nStorageType = embed::StorageFormats::OFOPXML;
222cdf0e10cSrcweir                         else
223cdf0e10cSrcweir                             throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
224cdf0e10cSrcweir                     }
225cdf0e10cSrcweir                     else if ( aDescr[nInd].Value >>= nFormatID )
226cdf0e10cSrcweir                     {
227cdf0e10cSrcweir                         if ( nFormatID != embed::StorageFormats::PACKAGE
228cdf0e10cSrcweir                           && nFormatID != embed::StorageFormats::ZIP
229cdf0e10cSrcweir                           && nFormatID != embed::StorageFormats::OFOPXML )
230cdf0e10cSrcweir                             throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
231cdf0e10cSrcweir 
232cdf0e10cSrcweir                         nStorageType = nFormatID;
233cdf0e10cSrcweir                     }
234cdf0e10cSrcweir                     else
235cdf0e10cSrcweir                         throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
236cdf0e10cSrcweir 				}
237cdf0e10cSrcweir 				else
238cdf0e10cSrcweir 					OSL_ENSURE( sal_False, "Unacceptable property, will be ignored!\n" );
239cdf0e10cSrcweir 			}
240cdf0e10cSrcweir 		}
241cdf0e10cSrcweir 		else
242cdf0e10cSrcweir 		{
243cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Wrong third argument!\n" );
244cdf0e10cSrcweir 			throw uno::Exception(); // TODO: Illegal argument
245cdf0e10cSrcweir 		}
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 	}
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	// create storage based on source
250cdf0e10cSrcweir 	if ( xInputStream.is() )
251cdf0e10cSrcweir 	{
252cdf0e10cSrcweir 		// if xInputStream is set the storage should be open from it
253cdf0e10cSrcweir 		if ( ( nStorageMode & embed::ElementModes::WRITE ) )
254cdf0e10cSrcweir 		  	throw uno::Exception(); // TODO: access denied
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 		uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
257cdf0e10cSrcweir 		if ( !xSeekable.is() )
258cdf0e10cSrcweir 		{
259cdf0e10cSrcweir 			// TODO: wrap stream to let it be seekable
260cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Nonseekable streams are not supported for now!\n" );
261cdf0e10cSrcweir 		}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) )
264cdf0e10cSrcweir 			throw io::IOException(); // TODO: this is not a package file
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 		return uno::Reference< uno::XInterface >(
267cdf0e10cSrcweir 					static_cast< OWeakObject* >( new OStorage( xInputStream, nStorageMode, aPropsToSet, m_xFactory, nStorageType ) ),
268cdf0e10cSrcweir 					uno::UNO_QUERY );
269cdf0e10cSrcweir 	}
270cdf0e10cSrcweir 	else if ( xStream.is() )
271cdf0e10cSrcweir 	{
272cdf0e10cSrcweir                if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() )
273cdf0e10cSrcweir 		  || !xStream->getInputStream().is() )
274cdf0e10cSrcweir 		  	throw uno::Exception(); // TODO: access denied
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 		uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
277cdf0e10cSrcweir 		if ( !xSeekable.is() )
278cdf0e10cSrcweir 		{
279cdf0e10cSrcweir 			// TODO: wrap stream to let it be seekable
280cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Nonseekable streams are not supported for now!\n" );
281cdf0e10cSrcweir 		}
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 		if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) )
284cdf0e10cSrcweir 			throw io::IOException(); // TODO: this is not a package file
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 		return uno::Reference< uno::XInterface >(
287cdf0e10cSrcweir 					static_cast< OWeakObject* >( new OStorage( xStream, nStorageMode, aPropsToSet, m_xFactory, nStorageType ) ),
288cdf0e10cSrcweir 					uno::UNO_QUERY );
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	throw uno::Exception(); // general error during creation
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir //-------------------------------------------------------------------------
getImplementationName()295cdf0e10cSrcweir ::rtl::OUString SAL_CALL OStorageFactory::getImplementationName()
296cdf0e10cSrcweir 	throw ( uno::RuntimeException )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir 	return impl_staticGetImplementationName();
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)302cdf0e10cSrcweir sal_Bool SAL_CALL OStorageFactory::supportsService( const ::rtl::OUString& ServiceName )
303cdf0e10cSrcweir 	throw ( uno::RuntimeException )
304cdf0e10cSrcweir {
305cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
308cdf0e10cSrcweir     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
309cdf0e10cSrcweir         	return sal_True;
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 	return sal_False;
312cdf0e10cSrcweir }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir //-------------------------------------------------------------------------
getSupportedServiceNames()315cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OStorageFactory::getSupportedServiceNames()
316cdf0e10cSrcweir 	throw ( uno::RuntimeException )
317cdf0e10cSrcweir {
318cdf0e10cSrcweir 	return impl_staticGetSupportedServiceNames();
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321