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

Generated by: LCOV version 1.10