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