Bump version to 5.0-14
[LibreOffice.git] / package / source / xstor / xfactory.cxx
blob95bb21a846224cd83ba3a59fc6a38d396c01ee80
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 <sal/config.h>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <com/sun/star/embed/StorageFormats.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/io/TempFile.hpp>
28 #include <com/sun/star/io/XSeekable.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <comphelper/storagehelper.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <osl/diagnose.h>
35 #include "xfactory.hxx"
36 #include "xstorage.hxx"
38 using namespace ::com::sun::star;
40 #if OSL_DEBUG_LEVEL > 0
41 #define THROW_WHERE SAL_WHERE
42 #else
43 #define THROW_WHERE ""
44 #endif
46 bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream,
47 const uno::Reference< io::XSeekable >& xSeekable )
49 if ( !xInputStream.is() || !xSeekable.is() )
50 throw uno::RuntimeException();
52 if ( xSeekable->getLength() )
54 uno::Sequence< sal_Int8 > aData( 4 );
55 xSeekable->seek( 0 );
56 sal_Int32 nRead = xInputStream->readBytes( aData, 4 );
57 xSeekable->seek( 0 );
59 // TODO/LATER: should the disk spanned files be supported?
60 // 0x50, 0x4b, 0x07, 0x08
61 return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 );
63 else
64 return true; // allow to create a storage based on empty stream
67 uno::Sequence< OUString > SAL_CALL OStorageFactory::impl_staticGetSupportedServiceNames()
69 uno::Sequence< OUString > aRet(2);
70 aRet[0] = "com.sun.star.embed.StorageFactory";
71 aRet[1] = "com.sun.star.comp.embed.StorageFactory";
72 return aRet;
75 OUString SAL_CALL OStorageFactory::impl_staticGetImplementationName()
77 return OUString("com.sun.star.comp.embed.StorageFactory");
80 uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::impl_staticCreateSelfInstance(
81 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
83 return uno::Reference< uno::XInterface >( *new OStorageFactory( comphelper::getComponentContext(xServiceManager) ) );
86 uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance()
87 throw ( uno::Exception,
88 uno::RuntimeException, std::exception )
90 // TODO: reimplement TempStream service to support XStream interface
91 uno::Reference < io::XStream > xTempStream(
92 io::TempFile::create(m_xContext),
93 uno::UNO_QUERY_THROW );
95 return uno::Reference< uno::XInterface >(
96 static_cast< OWeakObject* >( new OStorage( xTempStream,
97 embed::ElementModes::READWRITE,
98 uno::Sequence< beans::PropertyValue >(),
99 m_xContext,
100 embed::StorageFormats::PACKAGE ) ),
101 uno::UNO_QUERY );
104 uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments(
105 const uno::Sequence< uno::Any >& aArguments )
106 throw ( uno::Exception,
107 uno::RuntimeException, std::exception )
109 // The request for storage can be done with up to three arguments
111 // The first argument specifies a source for the storage
112 // it can be URL, XStream, XInputStream.
113 // The second value is a mode the storage should be open in.
114 // And the third value is a media descriptor.
116 sal_Int32 nArgNum = aArguments.getLength();
117 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
119 if ( !nArgNum )
120 return createInstance();
122 // first try to retrieve storage open mode if any
123 // by default the storage will be open in readonly mode
124 sal_Int32 nStorageMode = embed::ElementModes::READ;
125 if ( nArgNum >= 2 )
127 if( !( aArguments[1] >>= nStorageMode ) )
129 OSL_FAIL( "Wrong second argument!\n" );
130 throw lang::IllegalArgumentException(); // TODO:
132 // it's always possible to read written storage in this implementation
133 nStorageMode |= embed::ElementModes::READ;
136 if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
137 && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
138 throw lang::IllegalArgumentException(); // TODO:
140 // retrieve storage source stream
141 OUString aURL;
142 uno::Reference< io::XStream > xStream;
143 uno::Reference< io::XInputStream > xInputStream;
145 if ( aArguments[0] >>= aURL )
147 if ( aURL.isEmpty() )
149 OSL_FAIL( "Empty URL is provided!\n" );
150 throw lang::IllegalArgumentException(); // TODO:
153 if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:") )
155 OSL_FAIL( "Packages URL's are not valid for storages!\n" ); // ???
156 throw lang::IllegalArgumentException(); // TODO:
159 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(
160 ucb::SimpleFileAccess::create(
161 m_xContext ) );
163 if ( nStorageMode & embed::ElementModes::WRITE )
164 xStream = xTempAccess->openFileReadWrite( aURL );
165 else
166 xInputStream = xTempAccess->openFileRead( aURL );
168 else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
170 OSL_FAIL( "Wrong first argument!\n" );
171 throw uno::Exception(); // TODO: Illegal argument
174 // retrieve mediadescriptor and set storage properties
175 uno::Sequence< beans::PropertyValue > aDescr;
176 uno::Sequence< beans::PropertyValue > aPropsToSet;
178 sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
180 if ( nArgNum >= 3 )
182 if( aArguments[2] >>= aDescr )
184 if ( !aURL.isEmpty() )
186 aPropsToSet.realloc(1);
187 aPropsToSet[0].Name = "URL";
188 aPropsToSet[0].Value <<= aURL;
191 for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ )
193 if ( aDescr[nInd].Name == "InteractionHandler"
194 || aDescr[nInd].Name == "Password"
195 || aDescr[nInd].Name == "RepairPackage"
196 || aDescr[nInd].Name == "StatusIndicator" )
197 // || aDescr[nInd].Name == "Unpacked" ) // TODO:
199 aPropsToSet.realloc( ++nNumArgs );
200 aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name;
201 aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value;
203 else if ( aDescr[nInd].Name == "StorageFormat" )
205 OUString aFormatName;
206 sal_Int32 nFormatID = 0;
207 if ( aDescr[nInd].Value >>= aFormatName )
209 if ( aFormatName == PACKAGE_STORAGE_FORMAT_STRING )
210 nStorageType = embed::StorageFormats::PACKAGE;
211 else if ( aFormatName == ZIP_STORAGE_FORMAT_STRING )
212 nStorageType = embed::StorageFormats::ZIP;
213 else if ( aFormatName == OFOPXML_STORAGE_FORMAT_STRING )
214 nStorageType = embed::StorageFormats::OFOPXML;
215 else
216 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
218 else if ( aDescr[nInd].Value >>= nFormatID )
220 if ( nFormatID != embed::StorageFormats::PACKAGE
221 && nFormatID != embed::StorageFormats::ZIP
222 && nFormatID != embed::StorageFormats::OFOPXML )
223 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
225 nStorageType = nFormatID;
227 else
228 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
230 else
231 OSL_FAIL( "Unacceptable property, will be ignored!\n" );
234 else
236 OSL_FAIL( "Wrong third argument!\n" );
237 throw uno::Exception(); // TODO: Illegal argument
242 // create storage based on source
243 if ( xInputStream.is() )
245 // if xInputStream is set the storage should be open from it
246 if ( ( nStorageMode & embed::ElementModes::WRITE ) )
247 throw uno::Exception(); // TODO: access denied
249 uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
250 if ( !xSeekable.is() )
252 // TODO: wrap stream to let it be seekable
253 OSL_FAIL( "Nonseekable streams are not supported for now!\n" );
256 if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) )
257 throw io::IOException(); // TODO: this is not a package file
259 return uno::Reference< uno::XInterface >(
260 static_cast< OWeakObject* >( new OStorage( xInputStream, nStorageMode, aPropsToSet, m_xContext, nStorageType ) ),
261 uno::UNO_QUERY );
263 else if ( xStream.is() )
265 if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() )
266 || !xStream->getInputStream().is() )
267 throw uno::Exception(); // TODO: access denied
269 uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
270 if ( !xSeekable.is() )
272 // TODO: wrap stream to let it be seekable
273 OSL_FAIL( "Nonseekable streams are not supported for now!\n" );
276 if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) )
277 throw io::IOException(); // TODO: this is not a package file
279 return uno::Reference< uno::XInterface >(
280 static_cast< OWeakObject* >( new OStorage( xStream, nStorageMode, aPropsToSet, m_xContext, nStorageType ) ),
281 uno::UNO_QUERY );
284 throw uno::Exception(); // general error during creation
287 OUString SAL_CALL OStorageFactory::getImplementationName()
288 throw ( uno::RuntimeException, std::exception )
290 return impl_staticGetImplementationName();
293 sal_Bool SAL_CALL OStorageFactory::supportsService( const OUString& ServiceName )
294 throw ( uno::RuntimeException, std::exception )
296 return cppu::supportsService(this, ServiceName);
299 uno::Sequence< OUString > SAL_CALL OStorageFactory::getSupportedServiceNames()
300 throw ( uno::RuntimeException, std::exception )
302 return impl_staticGetSupportedServiceNames();
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */