Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "scriptcont.hxx"
21 : : #include "sbmodule.hxx"
22 : : #include <com/sun/star/container/XNameContainer.hpp>
23 : : #include <com/sun/star/xml/sax/XParser.hpp>
24 : : #include <com/sun/star/xml/sax/InputSource.hpp>
25 : : #include <com/sun/star/io/XOutputStream.hpp>
26 : : #include <com/sun/star/io/XInputStream.hpp>
27 : : #include <com/sun/star/io/XActiveDataSource.hpp>
28 : : #include <com/sun/star/ucb/XSimpleFileAccess2.hpp>
29 : : #include <com/sun/star/embed/ElementModes.hpp>
30 : : #include <com/sun/star/embed/XEncryptionProtectedSource.hpp>
31 : : #include <com/sun/star/beans/XPropertySet.hpp>
32 : : #include <com/sun/star/embed/XTransactedObject.hpp>
33 : : #include <com/sun/star/task/ErrorCodeIOException.hpp>
34 : : #include <com/sun/star/script/ModuleType.hpp>
35 : : #include <comphelper/processfactory.hxx>
36 : : #include <comphelper/storagehelper.hxx>
37 : : #include <unotools/streamwrap.hxx>
38 : : #include <unotools/ucbstreamhelper.hxx>
39 : : #include <osl/mutex.hxx>
40 : : #include <rtl/digest.h>
41 : : #include <rtl/strbuf.hxx>
42 : :
43 : : // For password functionality
44 : : #include <tools/urlobj.hxx>
45 : :
46 : :
47 : : #include <unotools/pathoptions.hxx>
48 : : #include <svtools/sfxecode.hxx>
49 : : #include <svtools/ehdl.hxx>
50 : : #include <basic/basmgr.hxx>
51 : : #include <basic/sbmod.hxx>
52 : : #include <basic/basicmanagerrepository.hxx>
53 : : #include "basic/modsizeexceeded.hxx"
54 : : #include <xmlscript/xmlmod_imexp.hxx>
55 : : #include <cppuhelper/factory.hxx>
56 : : #include <com/sun/star/util/VetoException.hpp>
57 : : #include <com/sun/star/script/XLibraryQueryExecutable.hpp>
58 : : #include <cppuhelper/implbase1.hxx>
59 : : namespace basic
60 : : {
61 : :
62 : : using namespace com::sun::star::document;
63 : : using namespace com::sun::star::container;
64 : : using namespace com::sun::star::io;
65 : : using namespace com::sun::star::uno;
66 : : using namespace com::sun::star::ucb;
67 : : using namespace com::sun::star::lang;
68 : : using namespace com::sun::star::script;
69 : : using namespace com::sun::star::xml::sax;
70 : : using namespace com::sun::star;
71 : : using namespace cppu;
72 : : using namespace osl;
73 : :
74 : : using ::rtl::OUString;
75 : :
76 : : //============================================================================
77 : : // Implementation class SfxScriptLibraryContainer
78 : :
79 : 1412 : const sal_Char* SAL_CALL SfxScriptLibraryContainer::getInfoFileName() const { return "script"; }
80 : 1412 : const sal_Char* SAL_CALL SfxScriptLibraryContainer::getOldInfoFileName() const { return "script"; }
81 : 1412 : const sal_Char* SAL_CALL SfxScriptLibraryContainer::getLibElementFileExtension() const { return "xba"; }
82 : 1412 : const sal_Char* SAL_CALL SfxScriptLibraryContainer::getLibrariesDir() const { return "Basic"; }
83 : :
84 : : // OldBasicPassword interface
85 : 0 : void SfxScriptLibraryContainer::setLibraryPassword
86 : : ( const String& rLibraryName, const String& rPassword )
87 : : {
88 : : try
89 : : {
90 [ # # ]: 0 : SfxLibrary* pImplLib = getImplLib( rLibraryName );
91 [ # # ]: 0 : if( rPassword.Len() )
92 : : {
93 : 0 : pImplLib->mbDoc50Password = true;
94 : 0 : pImplLib->mbPasswordProtected = sal_True;
95 [ # # ]: 0 : pImplLib->maPassword = rPassword;
96 : : }
97 : : }
98 : 0 : catch(const NoSuchElementException& ) {}
99 [ # # ]: 0 : }
100 : :
101 : 0 : String SfxScriptLibraryContainer::getLibraryPassword( const String& rLibraryName )
102 : : {
103 : 0 : SfxLibrary* pImplLib = getImplLib( rLibraryName );
104 : 0 : String aPassword;
105 [ # # ]: 0 : if( pImplLib->mbPasswordVerified )
106 [ # # ]: 0 : aPassword = pImplLib->maPassword;
107 : 0 : return aPassword;
108 : : }
109 : :
110 : 0 : void SfxScriptLibraryContainer::clearLibraryPassword( const String& rLibraryName )
111 : : {
112 : : try
113 : : {
114 [ # # ]: 0 : SfxLibrary* pImplLib = getImplLib( rLibraryName );
115 : 0 : pImplLib->mbDoc50Password = false;
116 : 0 : pImplLib->mbPasswordProtected = sal_False;
117 : 0 : pImplLib->maPassword = OUString();
118 : : }
119 : 0 : catch(const NoSuchElementException& ) {}
120 [ # # ]: 0 : }
121 : :
122 : 0 : sal_Bool SfxScriptLibraryContainer::hasLibraryPassword( const String& rLibraryName )
123 : : {
124 : 0 : SfxLibrary* pImplLib = getImplLib( rLibraryName );
125 : 0 : return pImplLib->mbPasswordProtected;
126 : : }
127 : :
128 : : // Ctor for service
129 : 1290 : SfxScriptLibraryContainer::SfxScriptLibraryContainer( void )
130 : 1290 : :maScriptLanguage( "StarBasic" )
131 : : {
132 : : // all initialisation has to be done
133 : : // by calling XInitialization::initialize
134 : 1290 : }
135 : :
136 : 122 : SfxScriptLibraryContainer::SfxScriptLibraryContainer( const uno::Reference< embed::XStorage >& xStorage )
137 : 122 : :maScriptLanguage( "StarBasic" )
138 : : {
139 [ + - ]: 122 : init( OUString(), xStorage );
140 : 122 : }
141 : :
142 : : // Methods to get library instances of the correct type
143 : 202 : SfxLibrary* SfxScriptLibraryContainer::implCreateLibrary( const OUString& aName )
144 : : {
145 : : (void)aName; // Only needed for SfxDialogLibrary
146 [ + - ]: 202 : SfxLibrary* pRet = new SfxScriptLibrary( maModifiable, mxMSF, mxSFI );
147 : 202 : return pRet;
148 : : }
149 : :
150 : 558 : SfxLibrary* SfxScriptLibraryContainer::implCreateLibraryLink
151 : : ( const OUString& aName, const OUString& aLibInfoFileURL,
152 : : const OUString& StorageURL, sal_Bool ReadOnly )
153 : : {
154 : : (void)aName; // Only needed for SfxDialogLibrary
155 : : SfxLibrary* pRet =
156 : : new SfxScriptLibrary
157 [ + - ]: 558 : ( maModifiable, mxMSF, mxSFI, aLibInfoFileURL, StorageURL, ReadOnly );
158 : 558 : return pRet;
159 : : }
160 : :
161 : 624 : Any SAL_CALL SfxScriptLibraryContainer::createEmptyLibraryElement( void )
162 : : {
163 : 624 : OUString aMod;
164 : 624 : Any aRetAny;
165 [ + - ]: 624 : aRetAny <<= aMod;
166 : 624 : return aRetAny;
167 : : }
168 : :
169 : 0 : bool SAL_CALL SfxScriptLibraryContainer::isLibraryElementValid( Any aElement ) const
170 : : {
171 : 0 : return SfxScriptLibrary::containsValidModule( aElement );
172 : : }
173 : :
174 : 0 : void SAL_CALL SfxScriptLibraryContainer::writeLibraryElement
175 : : (
176 : : const Reference < XNameContainer >& xLib,
177 : : const OUString& aElementName,
178 : : const Reference< XOutputStream >& xOutput
179 : : )
180 : : throw(Exception)
181 : : {
182 : : // Create sax writer
183 : : Reference< XExtendedDocumentHandler > xHandler(
184 [ # # ]: 0 : mxMSF->createInstance(
185 [ # # ][ # # ]: 0 : OUString( "com.sun.star.xml.sax.Writer" ) ), UNO_QUERY );
186 [ # # ]: 0 : if( !xHandler.is() )
187 : : {
188 : : OSL_FAIL( "### couldn't create sax-writer component\n" );
189 : 0 : return;
190 : : }
191 : :
192 [ # # ]: 0 : Reference< XTruncate > xTruncate( xOutput, UNO_QUERY );
193 : : OSL_ENSURE( xTruncate.is(), "Currently only the streams that can be truncated are expected!" );
194 [ # # ]: 0 : if ( xTruncate.is() )
195 [ # # ][ # # ]: 0 : xTruncate->truncate();
196 : :
197 [ # # ]: 0 : Reference< XActiveDataSource > xSource( xHandler, UNO_QUERY );
198 [ # # ][ # # ]: 0 : xSource->setOutputStream( xOutput );
199 : :
200 [ # # ]: 0 : xmlscript::ModuleDescriptor aMod;
201 : 0 : aMod.aName = aElementName;
202 : 0 : aMod.aLanguage = maScriptLanguage;
203 [ # # ][ # # ]: 0 : Any aElement = xLib->getByName( aElementName );
204 : 0 : aElement >>= aMod.aCode;
205 : :
206 [ # # ]: 0 : Reference< script::vba::XVBAModuleInfo > xModInfo( xLib, UNO_QUERY );
207 [ # # ][ # # ]: 0 : if( xModInfo.is() && xModInfo->hasModuleInfo( aElementName ) )
[ # # ][ # # ]
[ # # ]
208 : : {
209 [ # # ][ # # ]: 0 : script::ModuleInfo aModInfo = xModInfo->getModuleInfo( aElementName );
210 [ # # # # : 0 : switch( aModInfo.ModuleType )
# # ]
211 : : {
212 : : case ModuleType::NORMAL:
213 : 0 : aMod.aModuleType = OUString( "normal" );
214 : 0 : break;
215 : : case ModuleType::CLASS:
216 : 0 : aMod.aModuleType = OUString( "class" );
217 : 0 : break;
218 : : case ModuleType::FORM:
219 : 0 : aMod.aModuleType = OUString( "form" );
220 : 0 : break;
221 : : case ModuleType::DOCUMENT:
222 : 0 : aMod.aModuleType = OUString( "document" );
223 : 0 : break;
224 : : case ModuleType::UNKNOWN:
225 : : // nothing
226 : 0 : break;
227 [ # # ]: 0 : }
228 : : }
229 : :
230 [ # # ][ # # ]: 0 : xmlscript::exportScriptModule( xHandler, aMod );
[ # # ]
231 : : }
232 : :
233 : :
234 : 74 : Any SAL_CALL SfxScriptLibraryContainer::importLibraryElement
235 : : ( const Reference < XNameContainer >& xLib,
236 : : const OUString& aElementName, const OUString& aFile,
237 : : const uno::Reference< io::XInputStream >& xInStream )
238 : : {
239 : 74 : Any aRetAny;
240 : :
241 [ + - ]: 74 : Reference< XParser > xParser( mxMSF->createInstance(
242 [ + - ][ + - ]: 74 : OUString( "com.sun.star.xml.sax.Parser" ) ), UNO_QUERY );
243 [ + - ]: 74 : if( !xParser.is() )
244 : : {
245 : : OSL_FAIL( "### couldn't create sax parser component\n" );
246 : : return aRetAny;
247 : : }
248 : :
249 : :
250 : : // Read from storage?
251 : 74 : sal_Bool bStorage = xInStream.is();
252 : 74 : Reference< XInputStream > xInput;
253 : :
254 [ + + ]: 74 : if( bStorage )
255 : : {
256 [ + - ]: 12 : xInput = xInStream;
257 : : }
258 : : else
259 : : {
260 : : try
261 : : {
262 [ + - ][ + - ]: 62 : xInput = mxSFI->openFileRead( aFile );
[ + - ][ # # ]
263 : : }
264 [ # # ]: 0 : catch(const Exception& )
265 : : //catch( Exception& e )
266 : : {
267 : : // TODO:
268 : : //throw WrappedTargetException( e );
269 : : }
270 : : }
271 : :
272 [ + - ]: 74 : if( !xInput.is() )
273 : : return aRetAny;
274 : :
275 [ + - ]: 74 : InputSource source;
276 [ + - ]: 74 : source.aInputStream = xInput;
277 : 74 : source.sSystemId = aFile;
278 : :
279 : : // start parsing
280 [ + - ]: 74 : xmlscript::ModuleDescriptor aMod;
281 : :
282 : : try
283 : : {
284 [ + - ][ + - ]: 74 : xParser->setDocumentHandler( ::xmlscript::importScriptModule( aMod ) );
[ + - ]
285 [ + - ][ + - ]: 74 : xParser->parseStream( source );
286 : : }
287 [ # # # # ]: 0 : catch(const Exception& )
288 : : {
289 [ # # # # : 0 : SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aFile );
# # ]
290 : 0 : sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
291 [ # # # # ]: 0 : ErrorHandler::HandleError( nErrorCode );
292 : : }
293 : :
294 [ + - ]: 74 : aRetAny <<= aMod.aCode;
295 : :
296 : : // TODO: Check language
297 : : // aMod.aLanguage
298 : : // aMod.aName ignored
299 [ - + ]: 74 : if( !aMod.aModuleType.isEmpty() )
300 : : {
301 : : /* If in VBA compatibility mode, force creation of the VBA Globals
302 : : object. Each application will create an instance of its own
303 : : implementation and store it in its Basic manager. Implementations
304 : : will do all necessary additional initialization, such as
305 : : registering the global "This***Doc" UNO constant, starting the
306 : : document events processor etc.
307 : : */
308 [ # # ][ # # ]: 0 : if( getVBACompatibilityMode() ) try
309 : : {
310 [ # # ]: 0 : Reference< frame::XModel > xModel( mxOwnerDocument ); // weak-ref -> ref
311 [ # # ]: 0 : Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
312 [ # # ][ # # ]: 0 : xFactory->createInstance( ::rtl::OUString( "ooo.vba.VBAGlobals" ) );
[ # # ]
313 : : }
314 [ # # ]: 0 : catch(const Exception& )
315 : : {
316 : : }
317 : :
318 [ # # ]: 0 : script::ModuleInfo aModInfo;
319 : 0 : aModInfo.ModuleType = ModuleType::UNKNOWN;
320 [ # # ]: 0 : if( aMod.aModuleType == "normal" )
321 : : {
322 : 0 : aModInfo.ModuleType = ModuleType::NORMAL;
323 : : }
324 [ # # ]: 0 : else if( aMod.aModuleType == "class" )
325 : : {
326 : 0 : aModInfo.ModuleType = ModuleType::CLASS;
327 : : }
328 [ # # ]: 0 : else if( aMod.aModuleType == "form" )
329 : : {
330 : 0 : aModInfo.ModuleType = ModuleType::FORM;
331 [ # # ][ # # ]: 0 : aModInfo.ModuleObject = mxOwnerDocument;
332 : : }
333 [ # # ]: 0 : else if( aMod.aModuleType == "document" )
334 : : {
335 : 0 : aModInfo.ModuleType = ModuleType::DOCUMENT;
336 : :
337 : : // #163691# use the same codename access instance for all document modules
338 [ # # ]: 0 : if( !mxCodeNameAccess.is() ) try
339 : : {
340 [ # # ]: 0 : Reference<frame::XModel > xModel( mxOwnerDocument );
341 [ # # ]: 0 : Reference< XMultiServiceFactory> xSF( xModel, UNO_QUERY_THROW );
342 [ # # ][ # # ]: 0 : mxCodeNameAccess.set( xSF->createInstance( rtl::OUString("ooo.vba.VBAObjectModuleObjectProvider" ) ), UNO_QUERY );
[ # # ][ # # ]
343 : : }
344 [ # # ]: 0 : catch(const Exception& ) {}
345 : :
346 [ # # ]: 0 : if( mxCodeNameAccess.is() )
347 : : {
348 : : try
349 : : {
350 [ # # ][ # # ]: 0 : aModInfo.ModuleObject.set( mxCodeNameAccess->getByName( aElementName), uno::UNO_QUERY );
[ # # ][ # # ]
351 : : }
352 [ # # ]: 0 : catch(const uno::Exception&)
353 : : {
354 : : OSL_TRACE("Failed to get documument object for %s", rtl::OUStringToOString( aElementName, RTL_TEXTENCODING_UTF8 ).getStr() );
355 : : }
356 : : }
357 : : }
358 : :
359 [ # # ]: 0 : Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( xLib, UNO_QUERY );
360 [ # # ]: 0 : if( xVBAModuleInfo.is() )
361 : : {
362 [ # # ][ # # ]: 0 : if( xVBAModuleInfo->hasModuleInfo( aElementName ) )
[ # # ]
363 [ # # ][ # # ]: 0 : xVBAModuleInfo->removeModuleInfo( aElementName );
364 [ # # ][ # # ]: 0 : xVBAModuleInfo->insertModuleInfo( aElementName, aModInfo );
365 [ # # ]: 0 : }
366 : : }
367 : :
368 [ + - ][ + - ]: 74 : return aRetAny;
369 : : }
370 : :
371 : 0 : SfxLibraryContainer* SfxScriptLibraryContainer::createInstanceImpl( void )
372 : : {
373 [ # # ]: 0 : return new SfxScriptLibraryContainer();
374 : : }
375 : :
376 : 0 : void SAL_CALL SfxScriptLibraryContainer::importFromOldStorage( const ::rtl::OUString& aFile )
377 : : {
378 : : // TODO: move loading from old storage to binary filters?
379 [ # # ][ # # ]: 0 : SotStorageRef xStorage = new SotStorage( sal_False, aFile );
[ # # ][ # # ]
380 [ # # ][ # # ]: 0 : if( xStorage.Is() && xStorage->GetError() == ERRCODE_NONE )
[ # # ]
381 : : {
382 [ # # ][ # # ]: 0 : BasicManager* pBasicManager = new BasicManager( *(SotStorage*)xStorage, aFile );
[ # # ][ # # ]
383 : :
384 : : // Set info
385 [ # # ][ # # ]: 0 : LibraryContainerInfo aInfo( this, NULL, static_cast< OldBasicPassword* >( this ) );
[ # # ]
386 [ # # ]: 0 : pBasicManager->SetLibraryContainerInfo( aInfo );
387 : :
388 : : // Now the libraries should be copied to this SfxScriptLibraryContainer
389 [ # # ][ # # ]: 0 : BasicManager::LegacyDeleteBasicManager( pBasicManager );
390 [ # # ]: 0 : }
391 : 0 : }
392 : :
393 : :
394 : : // Storing with password encryption
395 : :
396 : : // Methods XLibraryContainerPassword
397 : 20 : sal_Bool SAL_CALL SfxScriptLibraryContainer::isLibraryPasswordProtected( const OUString& Name )
398 : : throw (NoSuchElementException, RuntimeException)
399 : : {
400 [ + - ]: 20 : LibraryContainerMethodGuard aGuard( *this );
401 [ + - ][ + - ]: 20 : SfxLibrary* pImplLib = getImplLib( Name );
[ + - ]
402 : 20 : sal_Bool bRet = pImplLib->mbPasswordProtected;
403 [ + - ]: 20 : return bRet;
404 : : }
405 : :
406 : 0 : sal_Bool SAL_CALL SfxScriptLibraryContainer::isLibraryPasswordVerified( const OUString& Name )
407 : : throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
408 : : {
409 [ # # ]: 0 : LibraryContainerMethodGuard aGuard( *this );
410 [ # # ][ # # ]: 0 : SfxLibrary* pImplLib = getImplLib( Name );
[ # # ]
411 [ # # ]: 0 : if( !pImplLib->mbPasswordProtected )
412 [ # # ]: 0 : throw IllegalArgumentException();
413 : 0 : sal_Bool bRet = pImplLib->mbPasswordVerified;
414 [ # # ]: 0 : return bRet;
415 : : }
416 : :
417 : 0 : sal_Bool SAL_CALL SfxScriptLibraryContainer::verifyLibraryPassword
418 : : ( const OUString& Name, const OUString& Password )
419 : : throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
420 : : {
421 [ # # ]: 0 : LibraryContainerMethodGuard aGuard( *this );
422 [ # # ][ # # ]: 0 : SfxLibrary* pImplLib = getImplLib( Name );
[ # # ]
423 [ # # ][ # # ]: 0 : if( !pImplLib->mbPasswordProtected || pImplLib->mbPasswordVerified )
424 [ # # ]: 0 : throw IllegalArgumentException();
425 : :
426 : : // Test password
427 : 0 : sal_Bool bSuccess = sal_False;
428 [ # # ]: 0 : if( pImplLib->mbDoc50Password )
429 : : {
430 : 0 : bSuccess = ( Password == pImplLib->maPassword );
431 [ # # ]: 0 : if( bSuccess )
432 : 0 : pImplLib->mbPasswordVerified = sal_True;
433 : : }
434 : : else
435 : : {
436 : 0 : pImplLib->maPassword = Password;
437 [ # # ]: 0 : bSuccess = implLoadPasswordLibrary( pImplLib, Name, sal_True );
438 [ # # ]: 0 : if( bSuccess )
439 : : {
440 : : // The library gets modified by verifiying the password, because other-
441 : : // wise for saving the storage would be copied and that doesn't work
442 : : // with mtg's storages when the password is verified
443 [ # # ]: 0 : pImplLib->implSetModified( sal_True );
444 : 0 : pImplLib->mbPasswordVerified = sal_True;
445 : :
446 : : // Reload library to get source
447 [ # # ]: 0 : if( pImplLib->mbLoaded )
448 [ # # ]: 0 : implLoadPasswordLibrary( pImplLib, Name );
449 : : }
450 : : }
451 [ # # ]: 0 : return bSuccess;
452 : : }
453 : :
454 : 0 : void SAL_CALL SfxScriptLibraryContainer::changeLibraryPassword( const OUString& Name,
455 : : const OUString& OldPassword, const OUString& NewPassword )
456 : : throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
457 : : {
458 [ # # ]: 0 : LibraryContainerMethodGuard aGuard( *this );
459 [ # # ][ # # ]: 0 : SfxLibrary* pImplLib = getImplLib( Name );
[ # # ]
460 [ # # ]: 0 : if( OldPassword == NewPassword )
461 : 0 : return;
462 : :
463 : 0 : sal_Bool bOldPassword = !OldPassword.isEmpty();
464 : 0 : sal_Bool bNewPassword = !NewPassword.isEmpty();
465 [ # # ][ # # ]: 0 : sal_Bool bStorage = mxStorage.is() && !pImplLib->mbLink;
466 : :
467 [ # # ][ # # ]: 0 : if( pImplLib->mbReadOnly || (bOldPassword && !pImplLib->mbPasswordProtected) )
[ # # ]
468 [ # # ]: 0 : throw IllegalArgumentException();
469 : :
470 : : // Library must be loaded
471 [ # # ]: 0 : loadLibrary( Name );
472 : :
473 : 0 : sal_Bool bKillCryptedFiles = sal_False;
474 : 0 : sal_Bool bKillUncryptedFiles = sal_False;
475 : :
476 : : // Remove or change password?
477 [ # # ]: 0 : if( bOldPassword )
478 : : {
479 [ # # ][ # # ]: 0 : if( isLibraryPasswordVerified( Name ) )
480 : : {
481 [ # # ]: 0 : if( pImplLib->maPassword != OldPassword )
482 [ # # ]: 0 : throw IllegalArgumentException();
483 : : }
484 : : else
485 : : {
486 [ # # ][ # # ]: 0 : if( !verifyLibraryPassword( Name, OldPassword ) )
487 [ # # ]: 0 : throw IllegalArgumentException();
488 : :
489 : : // Reload library to get source
490 : : // Should be done in verifyLibraryPassword loadLibrary( Name );
491 : : }
492 : :
493 [ # # ]: 0 : if( !bNewPassword )
494 : : {
495 : 0 : pImplLib->mbPasswordProtected = sal_False;
496 : 0 : pImplLib->mbPasswordVerified = sal_False;
497 : 0 : pImplLib->maPassword = OUString();
498 : :
499 [ # # ]: 0 : maModifiable.setModified( sal_True );
500 [ # # ]: 0 : pImplLib->implSetModified( sal_True );
501 : :
502 [ # # ][ # # ]: 0 : if( !bStorage && !pImplLib->mbDoc50Password )
503 : : {
504 : : // Store application basic uncrypted
505 : 0 : uno::Reference< embed::XStorage > xStorage;
506 [ # # ]: 0 : storeLibraries_Impl( xStorage, false );
507 : 0 : bKillCryptedFiles = sal_True;
508 : : }
509 : : }
510 : : }
511 : :
512 : : // Set new password?
513 [ # # ]: 0 : if( bNewPassword )
514 : : {
515 : 0 : pImplLib->mbPasswordProtected = sal_True;
516 : 0 : pImplLib->mbPasswordVerified = sal_True;
517 : 0 : pImplLib->maPassword = NewPassword;
518 : :
519 [ # # ]: 0 : maModifiable.setModified( sal_True );
520 [ # # ]: 0 : pImplLib->implSetModified( sal_True );
521 : :
522 [ # # ][ # # ]: 0 : if( !bStorage && !pImplLib->mbDoc50Password )
523 : : {
524 : : // Store applictaion basic crypted
525 : 0 : uno::Reference< embed::XStorage > xStorage;
526 [ # # ]: 0 : storeLibraries_Impl( xStorage, false );
527 : 0 : bKillUncryptedFiles = sal_True;
528 : : }
529 : : }
530 : :
531 [ # # ][ # # ]: 0 : if( bKillCryptedFiles || bKillUncryptedFiles )
532 : : {
533 [ # # ]: 0 : Sequence< OUString > aElementNames = pImplLib->getElementNames();
534 : 0 : sal_Int32 nNameCount = aElementNames.getLength();
535 : 0 : const OUString* pNames = aElementNames.getConstArray();
536 [ # # ]: 0 : OUString aLibDirPath = createAppLibraryFolder( pImplLib, Name );
537 : : try
538 : : {
539 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
540 : : {
541 : 0 : OUString aElementName = pNames[ i ];
542 : :
543 [ # # ]: 0 : INetURLObject aElementInetObj( aLibDirPath );
544 : : aElementInetObj.insertName( aElementName, sal_False,
545 [ # # ]: 0 : INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
546 [ # # ]: 0 : if( bKillUncryptedFiles )
547 [ # # ]: 0 : aElementInetObj.setExtension( maLibElementFileExtension );
548 : : else
549 [ # # ]: 0 : aElementInetObj.setExtension( OUString( "pba" ) );
550 [ # # ][ # # ]: 0 : String aElementPath( aElementInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
551 : :
552 [ # # ][ # # ]: 0 : if( mxSFI->exists( aElementPath ) )
[ # # ][ # # ]
553 [ # # ][ # # ]: 0 : mxSFI->kill( aElementPath );
[ # # ]
554 [ # # ][ # # ]: 0 : }
[ # # ]
555 : : }
556 [ # # ][ # # ]: 0 : catch(const Exception& ) {}
557 [ # # ][ # # ]: 0 : }
558 : : }
559 : :
560 : :
561 : 0 : void setStreamKey( uno::Reference< io::XStream > xStream, const ::rtl::OUString& aPass )
562 : : {
563 [ # # ]: 0 : uno::Reference< embed::XEncryptionProtectedSource > xEncrStream( xStream, uno::UNO_QUERY );
564 [ # # ]: 0 : if ( xEncrStream.is() )
565 [ # # ][ # # ]: 0 : xEncrStream->setEncryptionPassword( aPass );
566 : 0 : }
567 : :
568 : :
569 : : // Impl methods
570 : 0 : sal_Bool SfxScriptLibraryContainer::implStorePasswordLibrary( SfxLibrary* pLib,
571 : : const ::rtl::OUString& aName, const uno::Reference< embed::XStorage >& xStorage, const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler )
572 : : {
573 : 0 : OUString aDummyLocation;
574 : 0 : Reference< XSimpleFileAccess2 > xDummySFA;
575 [ # # ]: 0 : return implStorePasswordLibrary( pLib, aName, xStorage, aDummyLocation, xDummySFA, xHandler );
576 : : }
577 : :
578 : 0 : sal_Bool SfxScriptLibraryContainer::implStorePasswordLibrary( SfxLibrary* pLib, const ::rtl::OUString& aName,
579 : : const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage,
580 : : const ::rtl::OUString& aTargetURL, const Reference< XSimpleFileAccess2 > xToUseSFI, const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler )
581 : : {
582 : 0 : bool bExport = !aTargetURL.isEmpty();
583 : :
584 [ # # ]: 0 : BasicManager* pBasicMgr = getBasicManager();
585 : : OSL_ENSURE( pBasicMgr, "SfxScriptLibraryContainer::implStorePasswordLibrary: cannot do this without a BasicManager!" );
586 [ # # ]: 0 : if ( !pBasicMgr )
587 : 0 : return sal_False;
588 : :
589 : : // Only need to handle the export case here,
590 : : // save/saveas etc are handled in sfxbasemodel::storeSelf &
591 : : // sfxbasemodel::impl_store
592 [ # # ]: 0 : uno::Sequence<rtl::OUString> aNames;
593 [ # # ][ # # ]: 0 : if ( bExport && pBasicMgr->LegacyPsswdBinaryLimitExceeded(aNames) )
[ # # ][ # # ]
594 : : {
595 [ # # ]: 0 : if ( xHandler.is() )
596 : : {
597 [ # # ]: 0 : ModuleSizeExceeded* pReq = new ModuleSizeExceeded( aNames );
598 [ # # ][ # # ]: 0 : uno::Reference< task::XInteractionRequest > xReq( pReq );
599 [ # # ][ # # ]: 0 : xHandler->handle( xReq );
600 [ # # ][ # # ]: 0 : if ( pReq->isAbort() )
601 [ # # ]: 0 : throw util::VetoException();
602 : : }
603 : : }
604 : :
605 [ # # ][ # # ]: 0 : StarBASIC* pBasicLib = pBasicMgr->GetLib( aName );
[ # # ]
606 [ # # ]: 0 : if( !pBasicLib )
607 : 0 : return sal_False;
608 : :
609 [ # # ]: 0 : Sequence< OUString > aElementNames = pLib->getElementNames();
610 : 0 : sal_Int32 nNameCount = aElementNames.getLength();
611 : 0 : const OUString* pNames = aElementNames.getConstArray();
612 : :
613 : 0 : sal_Bool bLink = pLib->mbLink;
614 [ # # ][ # # ]: 0 : sal_Bool bStorage = xStorage.is() && !bLink;
615 [ # # ]: 0 : if( bStorage )
616 : : {
617 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
618 : : {
619 : 0 : OUString aElementName = pNames[ i ];
620 : :
621 : : // Write binary image stream
622 [ # # ][ # # ]: 0 : SbModule* pMod = pBasicLib->FindModule( aElementName );
[ # # ]
623 [ # # ]: 0 : if( pMod )
624 : : {
625 : 0 : OUString aCodeStreamName = aElementName;
626 [ # # ][ # # ]: 0 : aCodeStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".bin") );
[ # # ]
627 : :
628 : : try {
629 [ # # ]: 0 : uno::Reference< io::XStream > xCodeStream = xStorage->openStreamElement(
630 : : aCodeStreamName,
631 [ # # ]: 0 : embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
632 : :
633 [ # # ]: 0 : if ( !xCodeStream.is() )
634 [ # # ]: 0 : throw uno::RuntimeException();
635 : :
636 [ # # ]: 0 : SvMemoryStream aMemStream;
637 [ # # ]: 0 : /*sal_Bool bStore = */pMod->StoreBinaryData( aMemStream );
638 : :
639 : 0 : sal_Int32 nSize = (sal_Int32)aMemStream.Tell();
640 [ # # ]: 0 : Sequence< sal_Int8 > aBinSeq( nSize );
641 [ # # ]: 0 : sal_Int8* pData = aBinSeq.getArray();
642 [ # # ][ # # ]: 0 : ::rtl_copyMemory( pData, aMemStream.GetData(), nSize );
643 : :
644 [ # # ][ # # ]: 0 : Reference< XOutputStream > xOut = xCodeStream->getOutputStream();
645 [ # # ]: 0 : if ( !xOut.is() )
646 [ # # ]: 0 : throw io::IOException(); // access denied because the stream is readonly
647 : :
648 [ # # ][ # # ]: 0 : xOut->writeBytes( aBinSeq );
649 [ # # ][ # # ]: 0 : xOut->closeOutput();
[ # # ][ # # ]
[ # # ]
650 : : }
651 [ # # ]: 0 : catch(const uno::Exception& )
652 : : {
653 : : // TODO: handle error
654 : 0 : }
655 : : }
656 : :
657 [ # # ][ # # ]: 0 : if( pLib->mbPasswordVerified || pLib->mbDoc50Password )
658 : : {
659 [ # # ][ # # ]: 0 : if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
[ # # ]
660 : : {
661 : : #if OSL_DEBUG_LEVEL > 0
662 : : ::rtl::OStringBuffer aMessage;
663 : : aMessage.append( "invalid library element '" );
664 : : aMessage.append( ::rtl::OUStringToOString( aElementName, osl_getThreadTextEncoding() ) );
665 : : aMessage.append( "'." );
666 : : OSL_FAIL( aMessage.makeStringAndClear().getStr() );
667 : : #endif
668 : 0 : continue;
669 : : }
670 : :
671 : 0 : OUString aSourceStreamName = aElementName;
672 [ # # ][ # # ]: 0 : aSourceStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
[ # # ]
673 : :
674 : : try {
675 [ # # ]: 0 : uno::Reference< io::XStream > xSourceStream = xStorage->openStreamElement(
676 : : aSourceStreamName,
677 [ # # ]: 0 : embed::ElementModes::READWRITE );
678 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( xSourceStream, uno::UNO_QUERY );
679 [ # # ]: 0 : if ( !xProps.is() )
680 [ # # ]: 0 : throw uno::RuntimeException();
681 : :
682 : 0 : OUString aMime( "text/xml" );
683 [ # # ][ # # ]: 0 : xProps->setPropertyValue( rtl::OUString("MediaType"), uno::makeAny( aMime ) );
[ # # ]
684 : :
685 : : // Set encryption key
686 [ # # ]: 0 : setStreamKey( xSourceStream, pLib->maPassword );
687 : :
688 [ # # ][ # # ]: 0 : Reference< XOutputStream > xOutput = xSourceStream->getOutputStream();
689 [ # # ]: 0 : Reference< XNameContainer > xLib( pLib );
690 [ # # ][ # # ]: 0 : writeLibraryElement( xLib, aElementName, xOutput );
691 : : }
692 [ # # ]: 0 : catch(const uno::Exception& )
693 : : {
694 : : OSL_FAIL( "Problem on storing of password library!\n" );
695 : : // TODO: error handling
696 : 0 : }
697 : : }
698 : : else // !mbPasswordVerified
699 : : {
700 : : // TODO
701 : : // What to do if not verified?! In any case it's already loaded here
702 : : }
703 [ # # ]: 0 : }
704 : :
705 : : }
706 : : // Application libraries have only to be saved if the password
707 : : // is verified because otherwise they can't be modified
708 [ # # ][ # # ]: 0 : else if( pLib->mbPasswordVerified || bExport )
709 : : {
710 : : try
711 : : {
712 : 0 : Reference< XSimpleFileAccess2 > xSFI = mxSFI;
713 [ # # ]: 0 : if( xToUseSFI.is() )
714 [ # # ]: 0 : xSFI = xToUseSFI;
715 : :
716 : 0 : OUString aLibDirPath;
717 [ # # ]: 0 : if( bExport )
718 : : {
719 [ # # ]: 0 : INetURLObject aInetObj( aTargetURL );
720 [ # # ]: 0 : aInetObj.insertName( aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
721 [ # # ]: 0 : aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
722 : :
723 [ # # ][ # # ]: 0 : if( !xSFI->isFolder( aLibDirPath ) )
[ # # ]
724 [ # # ][ # # ]: 0 : xSFI->createFolder( aLibDirPath );
[ # # ]
725 : : }
726 : : else
727 : : {
728 [ # # ]: 0 : aLibDirPath = createAppLibraryFolder( pLib, aName );
729 : : }
730 : :
731 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
732 : : {
733 : 0 : OUString aElementName = pNames[ i ];
734 : :
735 [ # # ]: 0 : INetURLObject aElementInetObj( aLibDirPath );
736 : : aElementInetObj.insertName( aElementName, sal_False,
737 [ # # ]: 0 : INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
738 [ # # ]: 0 : aElementInetObj.setExtension( OUString( "pba" ) );
739 [ # # ][ # # ]: 0 : String aElementPath = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
740 : :
741 [ # # ][ # # ]: 0 : if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
[ # # ]
742 : : {
743 : : #if OSL_DEBUG_LEVEL > 0
744 : : ::rtl::OStringBuffer aMessage;
745 : : aMessage.append( "invalid library element '" );
746 : : aMessage.append( ::rtl::OUStringToOString( aElementName, osl_getThreadTextEncoding() ) );
747 : : aMessage.append( "'." );
748 : : OSL_FAIL( aMessage.makeStringAndClear().getStr() );
749 : : #endif
750 : 0 : continue;
751 : : }
752 : :
753 : : try
754 : : {
755 : : uno::Reference< embed::XStorage > xElementRootStorage =
756 : : ::comphelper::OStorageHelper::GetStorageFromURL(
757 : : aElementPath,
758 [ # # ][ # # ]: 0 : embed::ElementModes::READWRITE );
759 [ # # ]: 0 : if ( !xElementRootStorage.is() )
760 [ # # ]: 0 : throw uno::RuntimeException();
761 : :
762 : : // Write binary image stream
763 [ # # ][ # # ]: 0 : SbModule* pMod = pBasicLib->FindModule( aElementName );
[ # # ]
764 [ # # ]: 0 : if( pMod )
765 : : {
766 : 0 : OUString aCodeStreamName( "code.bin" );
767 : :
768 [ # # ]: 0 : uno::Reference< io::XStream > xCodeStream = xElementRootStorage->openStreamElement(
769 : : aCodeStreamName,
770 [ # # ]: 0 : embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
771 : :
772 [ # # ]: 0 : SvMemoryStream aMemStream;
773 [ # # ]: 0 : /*sal_Bool bStore = */pMod->StoreBinaryData( aMemStream );
774 : :
775 : 0 : sal_Int32 nSize = (sal_Int32)aMemStream.Tell();
776 [ # # ]: 0 : Sequence< sal_Int8 > aBinSeq( nSize );
777 [ # # ]: 0 : sal_Int8* pData = aBinSeq.getArray();
778 [ # # ][ # # ]: 0 : ::rtl_copyMemory( pData, aMemStream.GetData(), nSize );
779 : :
780 [ # # ][ # # ]: 0 : Reference< XOutputStream > xOut = xCodeStream->getOutputStream();
781 [ # # ]: 0 : if ( xOut.is() )
782 : : {
783 [ # # ][ # # ]: 0 : xOut->writeBytes( aBinSeq );
784 [ # # ][ # # ]: 0 : xOut->closeOutput();
785 [ # # ][ # # ]: 0 : }
786 : : }
787 : :
788 : : // Write encrypted source stream
789 : 0 : OUString aSourceStreamName( "source.xml" );
790 : :
791 : 0 : uno::Reference< io::XStream > xSourceStream;
792 : : try
793 : : {
794 [ # # ]: 0 : xSourceStream = xElementRootStorage->openStreamElement(
795 : : aSourceStreamName,
796 [ # # ][ # # ]: 0 : embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
797 : :
798 : : // #87671 Allow encryption
799 [ # # ]: 0 : uno::Reference< embed::XEncryptionProtectedSource > xEncr( xSourceStream, uno::UNO_QUERY );
800 : : OSL_ENSURE( xEncr.is(),
801 : : "StorageStream opened for writing must implement XEncryptionProtectedSource!\n" );
802 [ # # ]: 0 : if ( !xEncr.is() )
803 [ # # ]: 0 : throw uno::RuntimeException();
804 [ # # ][ # # ]: 0 : xEncr->setEncryptionPassword( pLib->maPassword );
805 : : }
806 [ # # # # ]: 0 : catch(const ::com::sun::star::packages::WrongPasswordException& )
807 : : {
808 [ # # ]: 0 : xSourceStream = xElementRootStorage->openEncryptedStreamElement(
809 : : aSourceStreamName,
810 : : embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,
811 [ # # # # ]: 0 : pLib->maPassword );
812 : : }
813 : :
814 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( xSourceStream, uno::UNO_QUERY );
815 [ # # ]: 0 : if ( !xProps.is() )
816 [ # # ]: 0 : throw uno::RuntimeException();
817 : 0 : OUString aMime( "text/xml" );
818 [ # # ][ # # ]: 0 : xProps->setPropertyValue( rtl::OUString("MediaType"), uno::makeAny( aMime ) );
[ # # ]
819 : :
820 [ # # ][ # # ]: 0 : Reference< XOutputStream > xOut = xSourceStream->getOutputStream();
821 [ # # ]: 0 : Reference< XNameContainer > xLib( pLib );
822 [ # # ]: 0 : writeLibraryElement( xLib, aElementName, xOut );
823 : : // i50568: sax writer already closes stream
824 : : // xOut->closeOutput();
825 : :
826 [ # # ]: 0 : uno::Reference< embed::XTransactedObject > xTransact( xElementRootStorage, uno::UNO_QUERY );
827 : : OSL_ENSURE( xTransact.is(), "The storage must implement XTransactedObject!\n" );
828 [ # # ]: 0 : if ( !xTransact.is() )
829 [ # # ]: 0 : throw uno::RuntimeException();
830 : :
831 [ # # ][ # # ]: 0 : xTransact->commit();
[ # # ]
832 : : }
833 [ # # ]: 0 : catch(const uno::Exception& )
834 : : {
835 : : // TODO: handle error
836 : : }
837 : :
838 [ # # ][ # # ]: 0 : }
[ # # ][ # # ]
[ # # ][ # # ]
839 : : }
840 [ # # ]: 0 : catch(const Exception& )
841 : : {
842 : : }
843 : : }
844 [ # # ][ # # ]: 0 : return sal_True;
845 : : }
846 : :
847 : 0 : sal_Bool SfxScriptLibraryContainer::implLoadPasswordLibrary
848 : : ( SfxLibrary* pLib, const OUString& Name, sal_Bool bVerifyPasswordOnly )
849 : : throw(WrappedTargetException, RuntimeException)
850 : : {
851 : 0 : sal_Bool bRet = sal_True;
852 : :
853 : 0 : sal_Bool bLink = pLib->mbLink;
854 [ # # ][ # # ]: 0 : sal_Bool bStorage = mxStorage.is() && !bLink;
855 : :
856 : : // Already loaded? Then only verifiedPassword can change something
857 : 0 : SfxScriptLibrary* pScriptLib = static_cast< SfxScriptLibrary* >( pLib );
858 [ # # ]: 0 : if( pScriptLib->mbLoaded )
859 : : {
860 [ # # ][ # # ]: 0 : if( pScriptLib->mbLoadedBinary && !bVerifyPasswordOnly &&
[ # # ][ # # ]
861 : 0 : (pScriptLib->mbLoadedSource || !pLib->mbPasswordVerified) )
862 : 0 : return sal_False;
863 : : }
864 : :
865 : 0 : StarBASIC* pBasicLib = NULL;
866 : 0 : sal_Bool bLoadBinary = sal_False;
867 [ # # ][ # # ]: 0 : if( !pScriptLib->mbLoadedBinary && !bVerifyPasswordOnly && !pLib->mbPasswordVerified )
[ # # ]
868 : : {
869 [ # # ]: 0 : BasicManager* pBasicMgr = getBasicManager();
870 : : OSL_ENSURE( pBasicMgr, "SfxScriptLibraryContainer::implLoadPasswordLibrary: cannot do this without a BasicManager!" );
871 : 0 : sal_Bool bLoaded = pScriptLib->mbLoaded;
872 : 0 : pScriptLib->mbLoaded = sal_True; // Necessary to get lib
873 [ # # ][ # # ]: 0 : pBasicLib = pBasicMgr ? pBasicMgr->GetLib( Name ) : NULL;
[ # # ][ # # ]
[ # # ][ # # ]
874 : 0 : pScriptLib->mbLoaded = bLoaded; // Restore flag
875 [ # # ]: 0 : if( !pBasicLib )
876 : 0 : return sal_False;
877 : :
878 : 0 : bLoadBinary = sal_True;
879 : 0 : pScriptLib->mbLoadedBinary = true;
880 : : }
881 : :
882 : 0 : sal_Bool bLoadSource = sal_False;
883 [ # # ][ # # ]: 0 : if( !pScriptLib->mbLoadedSource && pLib->mbPasswordVerified && !bVerifyPasswordOnly )
[ # # ]
884 : : {
885 : 0 : bLoadSource = sal_True;
886 : 0 : pScriptLib->mbLoadedSource = true;
887 : : }
888 : :
889 [ # # ]: 0 : Sequence< OUString > aElementNames = pLib->getElementNames();
890 : 0 : sal_Int32 nNameCount = aElementNames.getLength();
891 : 0 : const OUString* pNames = aElementNames.getConstArray();
892 : :
893 [ # # ]: 0 : if( bStorage )
894 : : {
895 : 0 : uno::Reference< embed::XStorage > xLibrariesStor;
896 : 0 : uno::Reference< embed::XStorage > xLibraryStor;
897 [ # # ]: 0 : if( bStorage )
898 : : {
899 : : try {
900 [ # # ][ # # ]: 0 : xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
[ # # ]
901 [ # # ]: 0 : if ( !xLibrariesStor.is() )
902 [ # # ]: 0 : throw uno::RuntimeException();
903 : :
904 [ # # ][ # # ]: 0 : xLibraryStor = xLibrariesStor->openStorageElement( Name, embed::ElementModes::READ );
[ # # ]
905 [ # # ]: 0 : if ( !xLibraryStor.is() )
906 [ # # ][ # # ]: 0 : throw uno::RuntimeException();
907 : : }
908 [ # # ]: 0 : catch(const uno::Exception& )
909 : : {
910 : : OSL_FAIL( "### couldn't open sub storage for library\n" );
911 : 0 : return sal_False;
912 : : }
913 : : }
914 : :
915 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
916 : : {
917 : 0 : OUString aElementName = pNames[ i ];
918 : :
919 : : // Load binary
920 [ # # ]: 0 : if( bLoadBinary )
921 : : {
922 [ # # ][ # # ]: 0 : SbModule* pMod = pBasicLib->FindModule( aElementName );
[ # # ]
923 [ # # ]: 0 : if( !pMod )
924 : : {
925 [ # # ][ # # ]: 0 : pMod = pBasicLib->MakeModule( aElementName, String() );
[ # # ][ # # ]
[ # # ]
926 [ # # ]: 0 : pBasicLib->SetModified( sal_False );
927 : : }
928 : :
929 : 0 : OUString aCodeStreamName= aElementName;
930 [ # # ][ # # ]: 0 : aCodeStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".bin") );
[ # # ]
931 : :
932 : : try {
933 [ # # ]: 0 : uno::Reference< io::XStream > xCodeStream = xLibraryStor->openStreamElement(
934 : : aCodeStreamName,
935 [ # # ]: 0 : embed::ElementModes::READ );
936 [ # # ]: 0 : if ( !xCodeStream.is() )
937 [ # # ]: 0 : throw uno::RuntimeException();
938 : :
939 [ # # ]: 0 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xCodeStream );
940 [ # # ][ # # ]: 0 : if ( !pStream || pStream->GetError() )
[ # # ]
941 : : {
942 [ # # ]: 0 : sal_Int32 nError = pStream ? pStream->GetError() : ERRCODE_IO_GENERAL;
943 [ # # ][ # # ]: 0 : delete pStream;
944 [ # # ]: 0 : throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), nError );
945 : : }
946 : :
947 [ # # ]: 0 : /*sal_Bool bRet = */pMod->LoadBinaryData( *pStream );
948 : : // TODO: Check return value
949 : :
950 [ # # ][ # # ]: 0 : delete pStream;
[ # # ]
951 : : }
952 [ # # ]: 0 : catch(const uno::Exception& )
953 : : {
954 : : // TODO: error handling
955 : 0 : }
956 : : }
957 : :
958 : : // Load source
959 [ # # ][ # # ]: 0 : if( bLoadSource || bVerifyPasswordOnly )
960 : : {
961 : : // Access encrypted source stream
962 : 0 : OUString aSourceStreamName = aElementName;
963 [ # # ][ # # ]: 0 : aSourceStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
[ # # ]
964 : :
965 : : try {
966 [ # # ]: 0 : uno::Reference< io::XStream > xSourceStream = xLibraryStor->openEncryptedStreamElement(
967 : : aSourceStreamName,
968 : : embed::ElementModes::READ,
969 [ # # ]: 0 : pLib->maPassword );
970 [ # # ]: 0 : if ( !xSourceStream.is() )
971 [ # # ]: 0 : throw uno::RuntimeException();
972 : :
973 : : // if this point is reached then the password is correct
974 [ # # ]: 0 : if ( !bVerifyPasswordOnly )
975 : : {
976 [ # # ][ # # ]: 0 : uno::Reference< io::XInputStream > xInStream = xSourceStream->getInputStream();
977 [ # # ]: 0 : if ( !xInStream.is() )
978 [ # # ]: 0 : throw io::IOException(); // read access denied, seems to be impossible
979 : :
980 [ # # ]: 0 : Reference< XNameContainer > xLib( pLib );
981 : : Any aAny = importLibraryElement( xLib,
982 : : aElementName, aSourceStreamName,
983 [ # # ]: 0 : xInStream );
984 [ # # ][ # # ]: 0 : if( pLib->hasByName( aElementName ) )
985 : : {
986 [ # # ]: 0 : if( aAny.hasValue() )
987 [ # # ]: 0 : pLib->maNameContainer.replaceByName( aElementName, aAny );
988 : : }
989 : : else
990 : : {
991 [ # # ]: 0 : pLib->maNameContainer.insertByName( aElementName, aAny );
992 : 0 : }
993 [ # # ]: 0 : }
994 : : }
995 [ # # ]: 0 : catch(const uno::Exception& )
996 : : {
997 : 0 : bRet = sal_False;
998 : 0 : }
999 : : }
1000 [ # # ][ # # ]: 0 : }
1001 : : }
1002 : : else
1003 : : {
1004 : : try
1005 : : {
1006 [ # # ]: 0 : OUString aLibDirPath = createAppLibraryFolder( pLib, Name );
1007 : :
1008 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
1009 : : {
1010 : 0 : OUString aElementName = pNames[ i ];
1011 : :
1012 [ # # ]: 0 : INetURLObject aElementInetObj( aLibDirPath );
1013 : : aElementInetObj.insertName( aElementName, sal_False,
1014 [ # # ]: 0 : INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1015 [ # # ]: 0 : aElementInetObj.setExtension( OUString( "pba" ) );
1016 [ # # ][ # # ]: 0 : String aElementPath = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
1017 : :
1018 : 0 : uno::Reference< embed::XStorage > xElementRootStorage;
1019 : : try {
1020 : : xElementRootStorage = ::comphelper::OStorageHelper::GetStorageFromURL(
1021 : : aElementPath,
1022 [ # # ][ # # ]: 0 : embed::ElementModes::READ );
[ # # ][ # # ]
1023 [ # # ]: 0 : } catch(const uno::Exception& )
1024 : : {
1025 : : // TODO: error handling
1026 : : }
1027 : :
1028 [ # # ]: 0 : if ( xElementRootStorage.is() )
1029 : : {
1030 : : // Load binary
1031 [ # # ]: 0 : if( bLoadBinary )
1032 : : {
1033 [ # # ][ # # ]: 0 : SbModule* pMod = pBasicLib->FindModule( aElementName );
[ # # ]
1034 [ # # ]: 0 : if( !pMod )
1035 : : {
1036 [ # # ][ # # ]: 0 : pMod = pBasicLib->MakeModule( aElementName, String() );
[ # # ][ # # ]
[ # # ]
1037 [ # # ]: 0 : pBasicLib->SetModified( sal_False );
1038 : : }
1039 : :
1040 : : try {
1041 : 0 : OUString aCodeStreamName( "code.bin" );
1042 [ # # ]: 0 : uno::Reference< io::XStream > xCodeStream = xElementRootStorage->openStreamElement(
1043 : : aCodeStreamName,
1044 [ # # ]: 0 : embed::ElementModes::READ );
1045 : :
1046 [ # # ]: 0 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xCodeStream );
1047 [ # # ][ # # ]: 0 : if ( !pStream || pStream->GetError() )
[ # # ]
1048 : : {
1049 [ # # ]: 0 : sal_Int32 nError = pStream ? pStream->GetError() : ERRCODE_IO_GENERAL;
1050 [ # # ][ # # ]: 0 : delete pStream;
1051 : : throw task::ErrorCodeIOException( ::rtl::OUString(),
1052 : : uno::Reference< uno::XInterface >(),
1053 [ # # ]: 0 : nError );
1054 : : }
1055 : :
1056 [ # # ]: 0 : /*sal_Bool bRet = */pMod->LoadBinaryData( *pStream );
1057 : : // TODO: Check return value
1058 : :
1059 [ # # ][ # # ]: 0 : delete pStream;
[ # # ]
1060 : : }
1061 [ # # ]: 0 : catch(const uno::Exception& )
1062 : : {
1063 : : // TODO: error handling
1064 : : }
1065 : : }
1066 : :
1067 : : // Load source
1068 [ # # ][ # # ]: 0 : if( bLoadSource || bVerifyPasswordOnly )
1069 : : {
1070 : : // Access encrypted source stream
1071 : 0 : OUString aSourceStreamName( "source.xml" );
1072 : : try {
1073 [ # # ]: 0 : uno::Reference< io::XStream > xSourceStream = xElementRootStorage->openEncryptedStreamElement(
1074 : : aSourceStreamName,
1075 : : embed::ElementModes::READ,
1076 [ # # ]: 0 : pLib->maPassword );
1077 [ # # ]: 0 : if ( !xSourceStream.is() )
1078 [ # # ]: 0 : throw uno::RuntimeException();
1079 : :
1080 [ # # ]: 0 : if ( !bVerifyPasswordOnly )
1081 : : {
1082 [ # # ][ # # ]: 0 : uno::Reference< io::XInputStream > xInStream = xSourceStream->getInputStream();
1083 [ # # ]: 0 : if ( !xInStream.is() )
1084 [ # # ]: 0 : throw io::IOException(); // read access denied, seems to be impossible
1085 : :
1086 [ # # ]: 0 : Reference< XNameContainer > xLib( pLib );
1087 : : Any aAny = importLibraryElement( xLib,
1088 : : aElementName,
1089 : : aSourceStreamName,
1090 [ # # ]: 0 : xInStream );
1091 [ # # ][ # # ]: 0 : if( pLib->hasByName( aElementName ) )
1092 : : {
1093 [ # # ]: 0 : if( aAny.hasValue() )
1094 [ # # ]: 0 : pLib->maNameContainer.replaceByName( aElementName, aAny );
1095 : : }
1096 : : else
1097 : : {
1098 [ # # ]: 0 : pLib->maNameContainer.insertByName( aElementName, aAny );
1099 : 0 : }
1100 [ # # ]: 0 : }
1101 : : }
1102 [ # # ]: 0 : catch (const uno::Exception& )
1103 : : {
1104 : 0 : bRet = sal_False;
1105 : 0 : }
1106 : : }
1107 : : }
1108 [ # # ][ # # ]: 0 : }
[ # # ]
1109 : :
1110 : : }
1111 [ # # ]: 0 : catch(const Exception& )
1112 : : {
1113 : : // TODO
1114 : : //throw e;
1115 : : }
1116 : : }
1117 : :
1118 [ # # ]: 0 : return bRet;
1119 : : }
1120 : :
1121 : :
1122 : 38 : void SfxScriptLibraryContainer::onNewRootStorage()
1123 : : {
1124 : 38 : }
1125 : :
1126 : : sal_Bool SAL_CALL
1127 : 63 : SfxScriptLibraryContainer:: HasExecutableCode( const ::rtl::OUString& Library ) throw (uno::RuntimeException)
1128 : : {
1129 : 63 : BasicManager* pBasicMgr = getBasicManager();
1130 : : OSL_ENSURE( pBasicMgr, "we need a basicmanager, really we do" );
1131 [ + - ]: 63 : if ( pBasicMgr )
1132 [ + - ]: 63 : return pBasicMgr->HasExeCode( Library ); // need to change this to take name
1133 : : // default to it has code if we can't decide
1134 : 63 : return sal_True;
1135 : : }
1136 : :
1137 : : //============================================================================
1138 : : // Service
1139 : 123 : void createRegistryInfo_SfxScriptLibraryContainer()
1140 : : {
1141 [ + + ][ + - ]: 123 : static OAutoRegistration< SfxScriptLibraryContainer > aAutoRegistration;
[ + - ][ # # ]
1142 : 123 : }
1143 : :
1144 : 0 : ::rtl::OUString SAL_CALL SfxScriptLibraryContainer::getImplementationName( ) throw (RuntimeException)
1145 : : {
1146 : 0 : return getImplementationName_static();
1147 : : }
1148 : :
1149 : 0 : Sequence< ::rtl::OUString > SAL_CALL SfxScriptLibraryContainer::getSupportedServiceNames( ) throw (RuntimeException)
1150 : : {
1151 : 0 : return getSupportedServiceNames_static();
1152 : : }
1153 : :
1154 : 97 : Sequence< OUString > SfxScriptLibraryContainer::getSupportedServiceNames_static()
1155 : : {
1156 : 97 : Sequence< OUString > aServiceNames( 2 );
1157 [ + - ]: 97 : aServiceNames[0] = OUString("com.sun.star.script.DocumentScriptLibraryContainer" );
1158 : : // plus, for compatibility:
1159 [ + - ]: 97 : aServiceNames[1] = OUString("com.sun.star.script.ScriptLibraryContainer" );
1160 : 97 : return aServiceNames;
1161 : : }
1162 : :
1163 : 97 : OUString SfxScriptLibraryContainer::getImplementationName_static()
1164 : : {
1165 : 97 : return OUString("com.sun.star.comp.sfx2.ScriptLibraryContainer" );
1166 : : }
1167 : :
1168 : 1290 : Reference< XInterface > SAL_CALL SfxScriptLibraryContainer::Create
1169 : : ( const Reference< XComponentContext >& )
1170 : : throw( Exception )
1171 : : {
1172 : : Reference< XInterface > xRet =
1173 [ + - ][ + - ]: 1290 : static_cast< XInterface* >( static_cast< OWeakObject* >(new SfxScriptLibraryContainer()) );
1174 : 1290 : return xRet;
1175 : : }
1176 : :
1177 : : //============================================================================
1178 : : // Implementation class SfxScriptLibrary
1179 : :
1180 : : // Ctor
1181 : 202 : SfxScriptLibrary::SfxScriptLibrary( ModifiableHelper& _rModifiable,
1182 : : const Reference< XMultiServiceFactory >& xMSF,
1183 : : const Reference< XSimpleFileAccess2 >& xSFI )
1184 : 202 : : SfxLibrary( _rModifiable, getCppuType( (const OUString *)0 ), xMSF, xSFI )
1185 : : , mbLoadedSource( false )
1186 [ + - ]: 202 : , mbLoadedBinary( false )
1187 : : {
1188 : 202 : }
1189 : :
1190 : 558 : SfxScriptLibrary::SfxScriptLibrary( ModifiableHelper& _rModifiable,
1191 : : const Reference< XMultiServiceFactory >& xMSF,
1192 : : const Reference< XSimpleFileAccess2 >& xSFI,
1193 : : const OUString& aLibInfoFileURL,
1194 : : const OUString& aStorageURL,
1195 : : sal_Bool ReadOnly )
1196 : 558 : : SfxLibrary( _rModifiable, getCppuType( (const OUString *)0 ), xMSF, xSFI,
1197 : : aLibInfoFileURL, aStorageURL, ReadOnly)
1198 : : , mbLoadedSource( false )
1199 [ + - ]: 558 : , mbLoadedBinary( false )
1200 : : {
1201 : 558 : }
1202 : :
1203 : : // Provide modify state including resources
1204 : 28 : sal_Bool SfxScriptLibrary::isModified( void )
1205 : : {
1206 : 28 : return implIsModified(); // No resources
1207 : : }
1208 : :
1209 : 0 : void SfxScriptLibrary::storeResources( void )
1210 : : {
1211 : : // No resources
1212 : 0 : }
1213 : :
1214 : 0 : void SfxScriptLibrary::storeResourcesToURL( const ::rtl::OUString& URL,
1215 : : const Reference< task::XInteractionHandler >& Handler )
1216 : : {
1217 : : (void)URL;
1218 : : (void)Handler;
1219 : 0 : }
1220 : :
1221 : 0 : void SfxScriptLibrary::storeResourcesAsURL
1222 : : ( const ::rtl::OUString& URL, const ::rtl::OUString& NewName )
1223 : : {
1224 : : (void)URL;
1225 : : (void)NewName;
1226 : 0 : }
1227 : :
1228 : 0 : void SfxScriptLibrary::storeResourcesToStorage( const ::com::sun::star::uno::Reference
1229 : : < ::com::sun::star::embed::XStorage >& xStorage )
1230 : : {
1231 : : // No resources
1232 : : (void)xStorage;
1233 : 0 : }
1234 : :
1235 : 0 : bool SfxScriptLibrary::containsValidModule( const Any& aElement )
1236 : : {
1237 : 0 : OUString sModuleText;
1238 : 0 : aElement >>= sModuleText;
1239 : 0 : return ( !sModuleText.isEmpty() );
1240 : : }
1241 : :
1242 : 0 : bool SAL_CALL SfxScriptLibrary::isLibraryElementValid( ::com::sun::star::uno::Any aElement ) const
1243 : : {
1244 : 0 : return SfxScriptLibrary::containsValidModule( aElement );
1245 : : }
1246 : :
1247 [ + + ][ + - ]: 42805 : IMPLEMENT_FORWARD_XINTERFACE2( SfxScriptLibrary, SfxLibrary, SfxScriptLibrary_BASE );
1248 [ # # ][ # # ]: 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( SfxScriptLibrary, SfxLibrary, SfxScriptLibrary_BASE );
[ # # ]
1249 : :
1250 : : script::ModuleInfo SAL_CALL
1251 : 111 : SfxScriptLibrary::getModuleInfo( const ::rtl::OUString& ModuleName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
1252 : : {
1253 [ + + ]: 111 : if ( !hasModuleInfo( ModuleName ) )
1254 [ + - ]: 40 : throw NoSuchElementException();
1255 : 71 : return mModuleInfos[ ModuleName ];
1256 : : }
1257 : :
1258 : : sal_Bool SAL_CALL
1259 : 317 : SfxScriptLibrary::hasModuleInfo( const ::rtl::OUString& ModuleName ) throw (RuntimeException)
1260 : : {
1261 : 317 : sal_Bool bRes = sal_False;
1262 [ + - ]: 317 : ModuleInfoMap::iterator it = mModuleInfos.find( ModuleName );
1263 : :
1264 [ + - ][ + + ]: 317 : if ( it != mModuleInfos.end() )
1265 : 136 : bRes = sal_True;
1266 : :
1267 : 317 : return bRes;
1268 : : }
1269 : :
1270 : 65 : void SAL_CALL SfxScriptLibrary::insertModuleInfo( const ::rtl::OUString& ModuleName, const script::ModuleInfo& ModuleInfo ) throw (IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
1271 : : {
1272 [ - + ]: 65 : if ( hasModuleInfo( ModuleName ) )
1273 [ # # ]: 0 : throw ElementExistException();
1274 : 65 : mModuleInfos[ ModuleName ] = ModuleInfo;
1275 : 65 : }
1276 : :
1277 : 0 : void SAL_CALL SfxScriptLibrary::removeModuleInfo( const ::rtl::OUString& ModuleName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
1278 : : {
1279 : : // #FIXME add NoSuchElementException to the spec
1280 [ # # ]: 0 : if ( !hasModuleInfo( ModuleName ) )
1281 [ # # ]: 0 : throw NoSuchElementException();
1282 [ # # ]: 0 : mModuleInfos.erase( mModuleInfos.find( ModuleName ) );
1283 : 0 : }
1284 : :
1285 : :
1286 : : //============================================================================
1287 : :
1288 : : } // namespace basic
1289 : :
1290 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|