LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/basic/source/uno - scriptcont.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 83 576 14.4 %
Date: 2013-07-09 Functions: 24 52 46.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10