1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/IOException.hpp>
28 #include <com/sun/star/io/TempFile.hpp>
29 #include <com/sun/star/io/XSeekable.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/storagehelper.hxx>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <osl/diagnose.h>
36 #include "xfactory.hxx"
37 #include "xstorage.hxx"
39 using namespace ::com::sun::star
;
41 #if OSL_DEBUG_LEVEL > 0
42 #define THROW_WHERE SAL_WHERE
44 #define THROW_WHERE ""
47 bool CheckPackageSignature_Impl( const uno::Reference
< io::XInputStream
>& xInputStream
,
48 const uno::Reference
< io::XSeekable
>& xSeekable
)
50 if ( !xInputStream
.is() || !xSeekable
.is() )
51 throw uno::RuntimeException();
53 if ( xSeekable
->getLength() )
55 uno::Sequence
< sal_Int8
> aData( 4 );
57 sal_Int32 nRead
= xInputStream
->readBytes( aData
, 4 );
60 // TODO/LATER: should the disk spanned files be supported?
61 // 0x50, 0x4b, 0x07, 0x08
62 return ( nRead
== 4 && aData
[0] == 0x50 && aData
[1] == 0x4b && aData
[2] == 0x03 && aData
[3] == 0x04 );
65 return true; // allow to create a storage based on empty stream
68 uno::Sequence
< OUString
> OStorageFactory::impl_staticGetSupportedServiceNames()
70 uno::Sequence
< OUString
> aRet(2);
71 aRet
[0] = "com.sun.star.embed.StorageFactory";
72 aRet
[1] = "com.sun.star.comp.embed.StorageFactory";
76 OUString
OStorageFactory::impl_staticGetImplementationName()
78 return OUString("com.sun.star.comp.embed.StorageFactory");
81 uno::Reference
< uno::XInterface
> OStorageFactory::impl_staticCreateSelfInstance(
82 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
84 return uno::Reference
< uno::XInterface
>( *new OStorageFactory( comphelper::getComponentContext(xServiceManager
) ) );
87 uno::Reference
< uno::XInterface
> SAL_CALL
OStorageFactory::createInstance()
89 // TODO: reimplement TempStream service to support XStream interface
90 uno::Reference
< io::XStream
> xTempStream(
91 io::TempFile::create(m_xContext
),
92 uno::UNO_QUERY_THROW
);
94 return uno::Reference
< uno::XInterface
>(
95 static_cast< OWeakObject
* >( new OStorage( xTempStream
,
96 embed::ElementModes::READWRITE
,
97 uno::Sequence
< beans::PropertyValue
>(),
99 embed::StorageFormats::PACKAGE
) ),
103 uno::Reference
< uno::XInterface
> SAL_CALL
OStorageFactory::createInstanceWithArguments(
104 const uno::Sequence
< uno::Any
>& aArguments
)
106 // The request for storage can be done with up to three arguments
108 // The first argument specifies a source for the storage
109 // it can be URL, XStream, XInputStream.
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_FAIL( "Wrong second argument!" );
127 throw lang::IllegalArgumentException(); // TODO:
129 // it's always possible to read written storage in this implementation
130 nStorageMode
|= embed::ElementModes::READ
;
133 if ( ( nStorageMode
& embed::ElementModes::TRUNCATE
) == embed::ElementModes::TRUNCATE
134 && ( nStorageMode
& embed::ElementModes::WRITE
) != embed::ElementModes::WRITE
)
135 throw lang::IllegalArgumentException(); // TODO:
137 // retrieve storage source stream
139 uno::Reference
< io::XStream
> xStream
;
140 uno::Reference
< io::XInputStream
> xInputStream
;
142 if ( aArguments
[0] >>= aURL
)
144 if ( aURL
.isEmpty() )
146 OSL_FAIL( "Empty URL is provided!" );
147 throw lang::IllegalArgumentException(); // TODO:
150 if ( aURL
.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:") )
152 OSL_FAIL( "Packages URL's are not valid for storages!" ); // ???
153 throw lang::IllegalArgumentException(); // TODO:
156 uno::Reference
< ucb::XSimpleFileAccess3
> xTempAccess(
157 ucb::SimpleFileAccess::create(
160 if ( nStorageMode
& embed::ElementModes::WRITE
)
161 xStream
= xTempAccess
->openFileReadWrite( aURL
);
163 xInputStream
= xTempAccess
->openFileRead( aURL
);
165 else if ( !( aArguments
[0] >>= xStream
) && !( aArguments
[0] >>= xInputStream
) )
167 OSL_FAIL( "Wrong first argument!" );
168 throw uno::Exception("wrong first arg", nullptr); // TODO: Illegal argument
171 // retrieve mediadescriptor and set storage properties
172 uno::Sequence
< beans::PropertyValue
> aDescr
;
173 uno::Sequence
< beans::PropertyValue
> aPropsToSet
;
175 sal_Int32 nStorageType
= embed::StorageFormats::PACKAGE
;
179 if( aArguments
[2] >>= aDescr
)
181 if ( !aURL
.isEmpty() )
183 aPropsToSet
.realloc(1);
184 aPropsToSet
[0].Name
= "URL";
185 aPropsToSet
[0].Value
<<= aURL
;
188 for ( sal_Int32 nInd
= 0, nNumArgs
= 1; nInd
< aDescr
.getLength(); nInd
++ )
190 if ( aDescr
[nInd
].Name
== "InteractionHandler"
191 || aDescr
[nInd
].Name
== "Password"
192 || aDescr
[nInd
].Name
== "RepairPackage"
193 || aDescr
[nInd
].Name
== "StatusIndicator" )
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
== PACKAGE_STORAGE_FORMAT_STRING
)
206 nStorageType
= embed::StorageFormats::PACKAGE
;
207 else if ( aFormatName
== ZIP_STORAGE_FORMAT_STRING
)
208 nStorageType
= embed::StorageFormats::ZIP
;
209 else if ( aFormatName
== OFOPXML_STORAGE_FORMAT_STRING
)
210 nStorageType
= embed::StorageFormats::OFOPXML
;
212 throw lang::IllegalArgumentException( THROW_WHERE
, 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( THROW_WHERE
, uno::Reference
< uno::XInterface
>(), 1 );
221 nStorageType
= nFormatID
;
224 throw lang::IllegalArgumentException( THROW_WHERE
, uno::Reference
< uno::XInterface
>(), 1 );
226 else if (aDescr
[nInd
].Name
== "NoFileSync")
228 // Forward NoFileSync to the storage.
229 aPropsToSet
.realloc(++nNumArgs
);
230 aPropsToSet
[nNumArgs
- 1].Name
= aDescr
[nInd
].Name
;
231 aPropsToSet
[nNumArgs
- 1].Value
= aDescr
[nInd
].Value
;
234 OSL_FAIL( "Unacceptable property, will be ignored!" );
239 OSL_FAIL( "Wrong third argument!" );
240 throw uno::Exception("wrong 3rd arg", nullptr); // TODO: Illegal argument
245 // create storage based on source
246 if ( xInputStream
.is() )
248 // if xInputStream is set the storage should be open from it
249 if ( nStorageMode
& embed::ElementModes::WRITE
)
250 throw uno::Exception("storagemode==write", nullptr); // TODO: access denied
252 uno::Reference
< io::XSeekable
> xSeekable( xInputStream
, uno::UNO_QUERY
);
253 if ( !xSeekable
.is() )
255 // TODO: wrap stream to let it be seekable
256 OSL_FAIL( "Nonseekable streams are not supported for now!" );
259 if ( !CheckPackageSignature_Impl( xInputStream
, xSeekable
) )
260 throw io::IOException(); // TODO: this is not a package file
262 return uno::Reference
< uno::XInterface
>(
263 static_cast< OWeakObject
* >( new OStorage( xInputStream
, nStorageMode
, aPropsToSet
, m_xContext
, nStorageType
) ),
266 else if ( xStream
.is() )
268 if ( ( ( nStorageMode
& embed::ElementModes::WRITE
) && !xStream
->getOutputStream().is() )
269 || !xStream
->getInputStream().is() )
270 throw uno::Exception("access denied", nullptr); // TODO: access denied
272 uno::Reference
< io::XSeekable
> xSeekable( xStream
, uno::UNO_QUERY
);
273 if ( !xSeekable
.is() )
275 // TODO: wrap stream to let it be seekable
276 OSL_FAIL( "Nonseekable streams are not supported for now!" );
279 if ( !CheckPackageSignature_Impl( xStream
->getInputStream(), xSeekable
) )
280 throw io::IOException(); // TODO: this is not a package file
282 return uno::Reference
< uno::XInterface
>(
283 static_cast< OWeakObject
* >( new OStorage( xStream
, nStorageMode
, aPropsToSet
, m_xContext
, nStorageType
) ),
287 throw uno::Exception("no input stream or regular stream", nullptr); // general error during creation
290 OUString SAL_CALL
OStorageFactory::getImplementationName()
292 return impl_staticGetImplementationName();
295 sal_Bool SAL_CALL
OStorageFactory::supportsService( const OUString
& ServiceName
)
297 return cppu::supportsService(this, ServiceName
);
300 uno::Sequence
< OUString
> SAL_CALL
OStorageFactory::getSupportedServiceNames()
302 return impl_staticGetSupportedServiceNames();
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */