update credits
[LibreOffice.git] / package / source / xstor / xfactory.cxx
blob11fb0744d5e98af81e9d20ec357ed415c925d061
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <com/sun/star/ucb/SimpleFileAccess.hpp>
21 #include <com/sun/star/embed/ElementModes.hpp>
22 #include <com/sun/star/embed/StorageFormats.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/io/TempFile.hpp>
25 #include <com/sun/star/io/XSeekable.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/storagehelper.hxx>
30 #include "xfactory.hxx"
31 #include "xstorage.hxx"
34 using namespace ::com::sun::star;
36 //-------------------------------------------------------------------------
37 sal_Bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream,
38 const uno::Reference< io::XSeekable >& xSeekable )
40 if ( !xInputStream.is() || !xSeekable.is() )
41 throw uno::RuntimeException();
43 if ( xSeekable->getLength() )
45 uno::Sequence< sal_Int8 > aData( 4 );
46 xSeekable->seek( 0 );
47 sal_Int32 nRead = xInputStream->readBytes( aData, 4 );
48 xSeekable->seek( 0 );
50 // TODO/LATER: should the disk spanned files be supported?
51 // 0x50, 0x4b, 0x07, 0x08
52 return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 );
54 else
55 return sal_True; // allow to create a storage based on empty stream
58 //-------------------------------------------------------------------------
59 uno::Sequence< OUString > SAL_CALL OStorageFactory::impl_staticGetSupportedServiceNames()
61 uno::Sequence< OUString > aRet(2);
62 aRet[0] = "com.sun.star.embed.StorageFactory";
63 aRet[1] = "com.sun.star.comp.embed.StorageFactory";
64 return aRet;
67 //-------------------------------------------------------------------------
68 OUString SAL_CALL OStorageFactory::impl_staticGetImplementationName()
70 return OUString("com.sun.star.comp.embed.StorageFactory");
73 //-------------------------------------------------------------------------
74 uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::impl_staticCreateSelfInstance(
75 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
77 return uno::Reference< uno::XInterface >( *new OStorageFactory( comphelper::getComponentContext(xServiceManager) ) );
80 //-------------------------------------------------------------------------
81 uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance()
82 throw ( uno::Exception,
83 uno::RuntimeException )
85 // TODO: reimplement TempStream service to support XStream interface
86 uno::Reference < io::XStream > xTempStream(
87 io::TempFile::create(m_xContext),
88 uno::UNO_QUERY_THROW );
90 return uno::Reference< uno::XInterface >(
91 static_cast< OWeakObject* >( new OStorage( xTempStream,
92 embed::ElementModes::READWRITE,
93 uno::Sequence< beans::PropertyValue >(),
94 m_xContext,
95 embed::StorageFormats::PACKAGE ) ),
96 uno::UNO_QUERY );
99 //-------------------------------------------------------------------------
100 uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments(
101 const uno::Sequence< uno::Any >& aArguments )
102 throw ( uno::Exception,
103 uno::RuntimeException )
105 // The request for storage can be done with up to three arguments
107 // The first argument specifies a source for the storage
108 // it can be URL, XStream, XInputStream.
109 // The second value is a mode the storage should be open in.
110 // And the third value is a media descriptor.
112 sal_Int32 nArgNum = aArguments.getLength();
113 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
115 if ( !nArgNum )
116 return createInstance();
118 // first try to retrieve storage open mode if any
119 // by default the storage will be open in readonly mode
120 sal_Int32 nStorageMode = embed::ElementModes::READ;
121 if ( nArgNum >= 2 )
123 if( !( aArguments[1] >>= nStorageMode ) )
125 OSL_FAIL( "Wrong second argument!\n" );
126 throw lang::IllegalArgumentException(); // TODO:
128 // it's always possible to read written storage in this implementation
129 nStorageMode |= embed::ElementModes::READ;
132 if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
133 && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
134 throw lang::IllegalArgumentException(); // TODO:
136 // retrieve storage source stream
137 OUString aURL;
138 uno::Reference< io::XStream > xStream;
139 uno::Reference< io::XInputStream > xInputStream;
141 if ( aArguments[0] >>= aURL )
143 if ( aURL.isEmpty() )
145 OSL_FAIL( "Empty URL is provided!\n" );
146 throw lang::IllegalArgumentException(); // TODO:
149 if ( aURL.equalsIgnoreAsciiCase("vnd.sun.star.pkg") )
151 OSL_FAIL( "Packages URL's are not valid for storages!\n" ); // ???
152 throw lang::IllegalArgumentException(); // TODO:
155 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(
156 ucb::SimpleFileAccess::create(
157 m_xContext ) );
159 if ( nStorageMode & embed::ElementModes::WRITE )
160 xStream = xTempAccess->openFileReadWrite( aURL );
161 else
162 xInputStream = xTempAccess->openFileRead( aURL );
164 else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
166 OSL_FAIL( "Wrong first argument!\n" );
167 throw uno::Exception(); // TODO: Illegal argument
170 // retrieve mediadescriptor and set storage properties
171 uno::Sequence< beans::PropertyValue > aDescr;
172 uno::Sequence< beans::PropertyValue > aPropsToSet;
174 sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
176 if ( nArgNum >= 3 )
178 if( aArguments[2] >>= aDescr )
180 if ( !aURL.isEmpty() )
182 aPropsToSet.realloc(1);
183 aPropsToSet[0].Name = "URL";
184 aPropsToSet[0].Value <<= aURL;
187 for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ )
189 if ( aDescr[nInd].Name == "InteractionHandler"
190 || aDescr[nInd].Name == "Password"
191 || aDescr[nInd].Name == "RepairPackage"
192 || aDescr[nInd].Name == "StatusIndicator" )
193 // || aDescr[nInd].Name == "Unpacked" ) // TODO:
195 aPropsToSet.realloc( ++nNumArgs );
196 aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name;
197 aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value;
199 else if ( aDescr[nInd].Name == "StorageFormat" )
201 OUString aFormatName;
202 sal_Int32 nFormatID = 0;
203 if ( aDescr[nInd].Value >>= aFormatName )
205 if ( aFormatName.equals( PACKAGE_STORAGE_FORMAT_STRING ) )
206 nStorageType = embed::StorageFormats::PACKAGE;
207 else if ( aFormatName.equals( ZIP_STORAGE_FORMAT_STRING ) )
208 nStorageType = embed::StorageFormats::ZIP;
209 else if ( aFormatName.equals( OFOPXML_STORAGE_FORMAT_STRING ) )
210 nStorageType = embed::StorageFormats::OFOPXML;
211 else
212 throw lang::IllegalArgumentException( OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
214 else if ( aDescr[nInd].Value >>= nFormatID )
216 if ( nFormatID != embed::StorageFormats::PACKAGE
217 && nFormatID != embed::StorageFormats::ZIP
218 && nFormatID != embed::StorageFormats::OFOPXML )
219 throw lang::IllegalArgumentException( OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
221 nStorageType = nFormatID;
223 else
224 throw lang::IllegalArgumentException( OSL_LOG_PREFIX, uno::Reference< uno::XInterface >(), 1 );
226 else
227 OSL_FAIL( "Unacceptable property, will be ignored!\n" );
230 else
232 OSL_FAIL( "Wrong third argument!\n" );
233 throw uno::Exception(); // TODO: Illegal argument
238 // create storage based on source
239 if ( xInputStream.is() )
241 // if xInputStream is set the storage should be open from it
242 if ( ( nStorageMode & embed::ElementModes::WRITE ) )
243 throw uno::Exception(); // TODO: access denied
245 uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
246 if ( !xSeekable.is() )
248 // TODO: wrap stream to let it be seekable
249 OSL_FAIL( "Nonseekable streams are not supported for now!\n" );
252 if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) )
253 throw io::IOException(); // TODO: this is not a package file
255 return uno::Reference< uno::XInterface >(
256 static_cast< OWeakObject* >( new OStorage( xInputStream, nStorageMode, aPropsToSet, m_xContext, nStorageType ) ),
257 uno::UNO_QUERY );
259 else if ( xStream.is() )
261 if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() )
262 || !xStream->getInputStream().is() )
263 throw uno::Exception(); // TODO: access denied
265 uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
266 if ( !xSeekable.is() )
268 // TODO: wrap stream to let it be seekable
269 OSL_FAIL( "Nonseekable streams are not supported for now!\n" );
272 if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) )
273 throw io::IOException(); // TODO: this is not a package file
275 return uno::Reference< uno::XInterface >(
276 static_cast< OWeakObject* >( new OStorage( xStream, nStorageMode, aPropsToSet, m_xContext, nStorageType ) ),
277 uno::UNO_QUERY );
280 throw uno::Exception(); // general error during creation
283 //-------------------------------------------------------------------------
284 OUString SAL_CALL OStorageFactory::getImplementationName()
285 throw ( uno::RuntimeException )
287 return impl_staticGetImplementationName();
290 //-------------------------------------------------------------------------
291 sal_Bool SAL_CALL OStorageFactory::supportsService( const OUString& ServiceName )
292 throw ( uno::RuntimeException )
294 uno::Sequence< OUString > aSeq = impl_staticGetSupportedServiceNames();
296 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
297 if ( ServiceName == aSeq[nInd] )
298 return sal_True;
300 return sal_False;
303 //-------------------------------------------------------------------------
304 uno::Sequence< OUString > SAL_CALL OStorageFactory::getSupportedServiceNames()
305 throw ( uno::RuntimeException )
307 return impl_staticGetSupportedServiceNames();
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */