LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/basic/source/uno - namecont.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 724 1638 44.2 %
Date: 2013-07-09 Functions: 71 118 60.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 <config_features.h>
      21             : 
      22             : #include <com/sun/star/container/XNameContainer.hpp>
      23             : #include <com/sun/star/container/XContainer.hpp>
      24             : #include <com/sun/star/embed/ElementModes.hpp>
      25             : #include <com/sun/star/embed/XTransactedObject.hpp>
      26             : #include <com/sun/star/lang/XServiceInfo.hpp>
      27             : #include <vcl/svapp.hxx>
      28             : #include <osl/mutex.hxx>
      29             : #include <tools/errinf.hxx>
      30             : #include <rtl/ustring.hxx>
      31             : #include <rtl/uri.hxx>
      32             : #include <rtl/strbuf.hxx>
      33             : #include <comphelper/processfactory.hxx>
      34             : #include <comphelper/anytostring.hxx>
      35             : 
      36             : #include "namecont.hxx"
      37             : #include <basic/basicmanagerrepository.hxx>
      38             : #include <tools/diagnose_ex.h>
      39             : #include <tools/urlobj.hxx>
      40             : #include <unotools/streamwrap.hxx>
      41             : #include <unotools/pathoptions.hxx>
      42             : #include <svtools/sfxecode.hxx>
      43             : #include <svtools/ehdl.hxx>
      44             : #include <basic/basmgr.hxx>
      45             : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
      46             : #include <com/sun/star/xml/sax/Parser.hpp>
      47             : #include <com/sun/star/xml/sax/InputSource.hpp>
      48             : #include <com/sun/star/io/XOutputStream.hpp>
      49             : #include <com/sun/star/xml/sax/Writer.hpp>
      50             : #include <com/sun/star/io/XInputStream.hpp>
      51             : #include <com/sun/star/io/XActiveDataSource.hpp>
      52             : #include <com/sun/star/beans/XPropertySet.hpp>
      53             : #include <com/sun/star/uno/DeploymentException.hpp>
      54             : #include <com/sun/star/lang/DisposedException.hpp>
      55             : #include <com/sun/star/script/LibraryNotLoadedException.hpp>
      56             : #include <com/sun/star/script/vba/VBAScriptEventId.hpp>
      57             : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
      58             : #include <com/sun/star/util/PathSubstitution.hpp>
      59             : #include <com/sun/star/util/theMacroExpander.hpp>
      60             : #include <com/sun/star/deployment/ExtensionManager.hpp>
      61             : #include <comphelper/storagehelper.hxx>
      62             : #include <cppuhelper/exc_hlp.hxx>
      63             : #include <basic/sbmod.hxx>
      64             : #include <boost/scoped_ptr.hpp>
      65             : 
      66             : namespace basic
      67             : {
      68             : 
      69             : using namespace com::sun::star::document;
      70             : using namespace com::sun::star::container;
      71             : using namespace com::sun::star::uno;
      72             : using namespace com::sun::star::lang;
      73             : using namespace com::sun::star::io;
      74             : using namespace com::sun::star::ucb;
      75             : using namespace com::sun::star::script;
      76             : using namespace com::sun::star::beans;
      77             : using namespace com::sun::star::xml::sax;
      78             : using namespace com::sun::star::util;
      79             : using namespace com::sun::star::task;
      80             : using namespace com::sun::star::embed;
      81             : using namespace com::sun::star::frame;
      82             : using namespace com::sun::star::deployment;
      83             : using namespace com::sun::star;
      84             : using namespace cppu;
      85             : using namespace osl;
      86             : 
      87             : using com::sun::star::uno::Reference;
      88             : 
      89             : using ::rtl::Uri;
      90             : 
      91             : // #i34411: Flag for error handling during migration
      92             : static bool GbMigrationSuppressErrors = false;
      93             : 
      94             : //============================================================================
      95             : // Implementation class NameContainer
      96             : 
      97             : // Methods XElementAccess
      98           0 : Type NameContainer::getElementType()
      99             :     throw(RuntimeException)
     100             : {
     101           0 :     return mType;
     102             : }
     103             : 
     104        2676 : sal_Bool NameContainer::hasElements()
     105             :     throw(RuntimeException)
     106             : {
     107        2676 :     sal_Bool bRet = (mnElementCount > 0);
     108        2676 :     return bRet;
     109             : }
     110             : 
     111             : // Methods XNameAccess
     112        9914 : Any NameContainer::getByName( const OUString& aName )
     113             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
     114             : {
     115        9914 :     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
     116        9914 :     if( aIt == mHashMap.end() )
     117             :     {
     118           0 :         throw NoSuchElementException();
     119             :     }
     120        9914 :     sal_Int32 iHashResult = (*aIt).second;
     121        9914 :     Any aRetAny = mValues.getConstArray()[ iHashResult ];
     122        9914 :     return aRetAny;
     123             : }
     124             : 
     125        5276 : Sequence< OUString > NameContainer::getElementNames()
     126             :     throw(RuntimeException)
     127             : {
     128        5276 :     return mNames;
     129             : }
     130             : 
     131        5512 : sal_Bool NameContainer::hasByName( const OUString& aName )
     132             :     throw(RuntimeException)
     133             : {
     134        5512 :     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
     135        5512 :     sal_Bool bRet = ( aIt != mHashMap.end() );
     136        5512 :     return bRet;
     137             : }
     138             : 
     139             : 
     140             : // Methods XNameReplace
     141          37 : void NameContainer::replaceByName( const OUString& aName, const Any& aElement )
     142             :     throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
     143             : {
     144          37 :     Type aAnyType = aElement.getValueType();
     145          37 :     if( mType != aAnyType )
     146             :     {
     147           0 :         throw IllegalArgumentException();
     148             :     }
     149          37 :     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
     150          37 :     if( aIt == mHashMap.end() )
     151             :     {
     152           0 :         throw NoSuchElementException();
     153             :     }
     154          37 :     sal_Int32 iHashResult = (*aIt).second;
     155          74 :     Any aOldElement = mValues.getConstArray()[ iHashResult ];
     156          37 :     mValues.getArray()[ iHashResult ] = aElement;
     157             : 
     158             : 
     159             :     // Fire event
     160          37 :     if( maContainerListeners.getLength() > 0 )
     161             :     {
     162           0 :         ContainerEvent aEvent;
     163           0 :         aEvent.Source = mpxEventSource;
     164           0 :         aEvent.Accessor <<= aName;
     165           0 :         aEvent.Element = aElement;
     166           0 :         aEvent.ReplacedElement = aOldElement;
     167           0 :         maContainerListeners.notifyEach( &XContainerListener::elementReplaced, aEvent );
     168             :     }
     169             : 
     170             :     /*  After the container event has been fired (one listener will update the
     171             :         core Basic manager), fire change event. Listeners can rely that the
     172             :         Basic source code of the core Basic manager is up-to-date. */
     173          37 :     if( maChangesListeners.getLength() > 0 )
     174             :     {
     175           0 :         ChangesEvent aEvent;
     176           0 :         aEvent.Source = mpxEventSource;
     177           0 :         aEvent.Base <<= aEvent.Source;
     178           0 :         aEvent.Changes.realloc( 1 );
     179           0 :         aEvent.Changes[ 0 ].Accessor <<= aName;
     180           0 :         aEvent.Changes[ 0 ].Element <<= aElement;
     181           0 :         aEvent.Changes[ 0 ].ReplacedElement = aOldElement;
     182           0 :         maChangesListeners.notifyEach( &XChangesListener::changesOccurred, aEvent );
     183          37 :     }
     184          37 : }
     185             : 
     186             : 
     187             : // Methods XNameContainer
     188        3670 : void NameContainer::insertByName( const OUString& aName, const Any& aElement )
     189             :     throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
     190             : {
     191        3670 :     Type aAnyType = aElement.getValueType();
     192        3670 :     if( mType != aAnyType )
     193             :     {
     194           0 :         throw IllegalArgumentException();
     195             :     }
     196        3670 :     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
     197        3670 :     if( aIt != mHashMap.end() )
     198             :     {
     199           0 :         throw ElementExistException();
     200             :     }
     201             : 
     202        3670 :     sal_Int32 nCount = mNames.getLength();
     203        3670 :     mNames.realloc( nCount + 1 );
     204        3670 :     mValues.realloc( nCount + 1 );
     205        3670 :     mNames.getArray()[ nCount ] = aName;
     206        3670 :     mValues.getArray()[ nCount ] = aElement;
     207             : 
     208        3670 :     mHashMap[ aName ] = nCount;
     209        3670 :     mnElementCount++;
     210             : 
     211             :     // Fire event
     212        3670 :     if( maContainerListeners.getLength() > 0 )
     213             :     {
     214         602 :         ContainerEvent aEvent;
     215         602 :         aEvent.Source = mpxEventSource;
     216         602 :         aEvent.Accessor <<= aName;
     217         602 :         aEvent.Element = aElement;
     218         602 :         maContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
     219             :     }
     220             : 
     221             :     /*  After the container event has been fired (one listener will update the
     222             :         core Basic manager), fire change event. Listeners can rely that the
     223             :         Basic source code of the core Basic manager is up-to-date. */
     224        3670 :     if( maChangesListeners.getLength() > 0 )
     225             :     {
     226           0 :         ChangesEvent aEvent;
     227           0 :         aEvent.Source = mpxEventSource;
     228           0 :         aEvent.Base <<= aEvent.Source;
     229           0 :         aEvent.Changes.realloc( 1 );
     230           0 :         aEvent.Changes[ 0 ].Accessor <<= aName;
     231           0 :         aEvent.Changes[ 0 ].Element <<= aElement;
     232           0 :         maChangesListeners.notifyEach( &XChangesListener::changesOccurred, aEvent );
     233        3670 :     }
     234        3670 : }
     235             : 
     236           0 : void NameContainer::removeByName( const OUString& aName )
     237             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
     238             : {
     239           0 :     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
     240           0 :     if( aIt == mHashMap.end() )
     241             :     {
     242           0 :         OUString sMessage = "\"" + aName + "\" not found";
     243           0 :         throw NoSuchElementException(sMessage, uno::Reference< uno::XInterface >());
     244             :     }
     245             : 
     246           0 :     sal_Int32 iHashResult = (*aIt).second;
     247           0 :     Any aOldElement = mValues.getConstArray()[ iHashResult ];
     248           0 :     mHashMap.erase( aIt );
     249           0 :     sal_Int32 iLast = mNames.getLength() - 1;
     250           0 :     if( iLast != iHashResult )
     251             :     {
     252           0 :         OUString* pNames = mNames.getArray();
     253           0 :         Any* pValues = mValues.getArray();
     254           0 :         pNames[ iHashResult ] = pNames[ iLast ];
     255           0 :         pValues[ iHashResult ] = pValues[ iLast ];
     256           0 :         mHashMap[ pNames[ iHashResult ] ] = iHashResult;
     257             :     }
     258           0 :     mNames.realloc( iLast );
     259           0 :     mValues.realloc( iLast );
     260           0 :     mnElementCount--;
     261             : 
     262             :     // Fire event
     263           0 :     if( maContainerListeners.getLength() > 0 )
     264             :     {
     265           0 :         ContainerEvent aEvent;
     266           0 :         aEvent.Source = mpxEventSource;
     267           0 :         aEvent.Accessor <<= aName;
     268           0 :         aEvent.Element = aOldElement;
     269           0 :         maContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
     270             :     }
     271             : 
     272             :     /*  After the container event has been fired (one listener will update the
     273             :         core Basic manager), fire change event. Listeners can rely that the
     274             :         Basic source code of the core Basic manager is up-to-date. */
     275           0 :     if( maChangesListeners.getLength() > 0 )
     276             :     {
     277           0 :         ChangesEvent aEvent;
     278           0 :         aEvent.Source = mpxEventSource;
     279           0 :         aEvent.Base <<= aEvent.Source;
     280           0 :         aEvent.Changes.realloc( 1 );
     281           0 :         aEvent.Changes[ 0 ].Accessor <<= aName;
     282             :         // aEvent.Changes[ 0 ].Element remains empty (meaning "replaced with nothing")
     283           0 :         aEvent.Changes[ 0 ].ReplacedElement = aOldElement;
     284           0 :         maChangesListeners.notifyEach( &XChangesListener::changesOccurred, aEvent );
     285           0 :     }
     286           0 : }
     287             : 
     288             : 
     289             : // Methods XContainer
     290        1211 : void SAL_CALL NameContainer::addContainerListener( const Reference< XContainerListener >& xListener )
     291             :     throw (RuntimeException)
     292             : {
     293        1211 :     if( !xListener.is() )
     294             :     {
     295             :         throw RuntimeException("addContainerListener called with null xListener",
     296           0 :                                static_cast< cppu::OWeakObject * >(this));
     297             :     }
     298        1211 :     maContainerListeners.addInterface( xListener );
     299        1211 : }
     300             : 
     301           0 : void SAL_CALL NameContainer::removeContainerListener( const Reference< XContainerListener >& xListener )
     302             :     throw (RuntimeException)
     303             : {
     304           0 :     if( !xListener.is() )
     305             :     {
     306           0 :         throw RuntimeException();
     307             :     }
     308           0 :     maContainerListeners.removeInterface( xListener );
     309           0 : }
     310             : 
     311             : // Methods XChangesNotifier
     312          26 : void SAL_CALL NameContainer::addChangesListener( const Reference< XChangesListener >& xListener )
     313             :     throw (RuntimeException)
     314             : {
     315          26 :     if( !xListener.is() )
     316             :     {
     317           0 :         throw RuntimeException();
     318             :     }
     319          26 :     maChangesListeners.addInterface( xListener );
     320          26 : }
     321             : 
     322           0 : void SAL_CALL NameContainer::removeChangesListener( const Reference< XChangesListener >& xListener )
     323             :     throw (RuntimeException)
     324             : {
     325           0 :     if( !xListener.is() )
     326             :     {
     327           0 :         throw RuntimeException();
     328             :     }
     329           0 :     maChangesListeners.removeInterface( xListener );
     330           0 : }
     331             : 
     332             : //============================================================================
     333             : // ModifiableHelper
     334             : 
     335        2876 : void ModifiableHelper::setModified( sal_Bool _bModified )
     336             : {
     337        2876 :     if ( _bModified == mbModified )
     338             :     {
     339        2944 :         return;
     340             :     }
     341        2808 :     mbModified = _bModified;
     342             : 
     343        2808 :     if ( m_aModifyListeners.getLength() == 0 )
     344             :     {
     345        2808 :         return;
     346             :     }
     347           0 :     EventObject aModifyEvent( m_rEventSource );
     348           0 :     m_aModifyListeners.notifyEach( &XModifyListener::modified, aModifyEvent );
     349             : }
     350             : 
     351             : //============================================================================
     352             : 
     353        1871 : VBAScriptListenerContainer::VBAScriptListenerContainer( ::osl::Mutex& rMutex ) :
     354        1871 :     VBAScriptListenerContainer_BASE( rMutex )
     355             : {
     356        1871 : }
     357             : 
     358           0 : bool VBAScriptListenerContainer::implTypedNotify( const Reference< vba::XVBAScriptListener >& rxListener, const vba::VBAScriptEvent& rEvent )
     359             :     throw (Exception)
     360             : {
     361           0 :     rxListener->notifyVBAScriptEvent( rEvent );
     362           0 :     return true;    // notify all other listeners too
     363             : }
     364             : 
     365             : //============================================================================
     366             : 
     367             : // Implementation class SfxLibraryContainer
     368             : DBG_NAME( SfxLibraryContainer )
     369             : 
     370             : // Ctor
     371        1871 : SfxLibraryContainer::SfxLibraryContainer( void )
     372             :     : SfxLibraryContainer_BASE( maMutex )
     373             : 
     374             :     , maVBAScriptListeners( maMutex )
     375             :     , mnRunningVBAScripts( 0 )
     376             :     , mbVBACompat( sal_False )
     377             :     , maModifiable( *this, maMutex )
     378        1871 :     , maNameContainer( getCppuType( (Reference< XNameAccess >*) NULL ) )
     379             :     , mbOldInfoFormat( false )
     380             :     , mbOasis2OOoFormat( false )
     381             :     , mpBasMgr( NULL )
     382        3742 :     , mbOwnBasMgr( false )
     383             : {
     384             :     DBG_CTOR( SfxLibraryContainer, NULL );
     385             : 
     386        1871 :     mxContext = comphelper::getProcessComponentContext();
     387             : 
     388        1871 :     mxSFI = ucb::SimpleFileAccess::create( mxContext );
     389             : 
     390        1871 :     mxStringSubstitution = util::PathSubstitution::create( mxContext );
     391        1871 : }
     392             : 
     393        2002 : SfxLibraryContainer::~SfxLibraryContainer()
     394             : {
     395        1001 :     if( mbOwnBasMgr )
     396             :     {
     397           0 :         BasicManager::LegacyDeleteBasicManager( mpBasMgr );
     398             :     }
     399             :     DBG_DTOR( SfxLibraryContainer, NULL );
     400        1001 : }
     401             : 
     402       21338 : void SfxLibraryContainer::checkDisposed() const
     403             : {
     404       21338 :     if ( isDisposed() )
     405             :     {
     406             :         throw DisposedException( OUString(),
     407           0 :                                  *const_cast< SfxLibraryContainer* >( this ) );
     408             :     }
     409       21338 : }
     410             : 
     411       21338 : void SfxLibraryContainer::enterMethod()
     412             : {
     413       21338 :     maMutex.acquire();
     414       21338 :     checkDisposed();
     415       21338 : }
     416             : 
     417       21338 : void SfxLibraryContainer::leaveMethod()
     418             : {
     419       21338 :     maMutex.release();
     420       21338 : }
     421             : 
     422          57 : BasicManager* SfxLibraryContainer::getBasicManager( void )
     423             : {
     424          57 :     if ( mpBasMgr )
     425             :     {
     426          28 :         return mpBasMgr;
     427             :     }
     428          29 :     Reference< XModel > xDocument( mxOwnerDocument.get(), UNO_QUERY );
     429             :     SAL_WARN_IF(
     430             :         !xDocument.is(), "basic",
     431             :         ("SfxLibraryContainer::getBasicManager: cannot obtain a BasicManager"
     432             :          " without document!"));
     433          29 :     if ( xDocument.is() )
     434             :     {
     435          29 :         mpBasMgr = BasicManagerRepository::getDocumentBasicManager( xDocument );
     436             :     }
     437          29 :     return mpBasMgr;
     438             : }
     439             : 
     440             : // Methods XStorageBasedLibraryContainer
     441           0 : Reference< XStorage > SAL_CALL SfxLibraryContainer::getRootStorage() throw (RuntimeException)
     442             : {
     443           0 :     LibraryContainerMethodGuard aGuard( *this );
     444           0 :     return mxStorage;
     445             : }
     446             : 
     447        1228 : void SAL_CALL SfxLibraryContainer::setRootStorage( const Reference< XStorage >& _rxRootStorage )
     448             :     throw (IllegalArgumentException, RuntimeException)
     449             : {
     450        1228 :     LibraryContainerMethodGuard aGuard( *this );
     451        1228 :     if ( !_rxRootStorage.is() )
     452             :     {
     453           0 :         throw IllegalArgumentException();
     454             :     }
     455        1228 :     mxStorage = _rxRootStorage;
     456        1228 :     onNewRootStorage();
     457        1228 : }
     458             : 
     459         640 : void SAL_CALL SfxLibraryContainer::storeLibrariesToStorage( const Reference< XStorage >& _rxRootStorage )
     460             :     throw (IllegalArgumentException, WrappedTargetException, RuntimeException)
     461             : {
     462         640 :     LibraryContainerMethodGuard aGuard( *this );
     463         640 :     if ( !_rxRootStorage.is() )
     464             :     {
     465           0 :         throw IllegalArgumentException();
     466             :     }
     467             :     try
     468             :     {
     469         640 :         storeLibraries_Impl( _rxRootStorage, true );
     470             :     }
     471           0 :     catch( const Exception& )
     472             :     {
     473             :         throw WrappedTargetException( OUString(),
     474           0 :                                       *this, ::cppu::getCaughtException() );
     475         640 :     }
     476         640 : }
     477             : 
     478             : 
     479             : // Methods XModifiable
     480           0 : sal_Bool SfxLibraryContainer::isModified()
     481             :     throw (RuntimeException)
     482             : {
     483           0 :     LibraryContainerMethodGuard aGuard( *this );
     484           0 :     if ( maModifiable.isModified() )
     485             :     {
     486           0 :         return sal_True;
     487             :     }
     488             :     // the library container is not modified, go through the libraries and check whether they are modified
     489           0 :     Sequence< OUString > aNames = maNameContainer.getElementNames();
     490           0 :     const OUString* pNames = aNames.getConstArray();
     491           0 :     sal_Int32 nNameCount = aNames.getLength();
     492             : 
     493           0 :     for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
     494             :     {
     495           0 :         OUString aName = pNames[ i ];
     496           0 :         SfxLibrary* pImplLib = getImplLib( aName );
     497           0 :         if( pImplLib->isModified() )
     498             :         {
     499           0 :             if ( aName == "Standard" )
     500             :             {
     501             :                 // this is a workaround that has to be implemented because
     502             :                 // empty standard library should stay marked as modified
     503             :                 // but should not be treated as modified while it is empty
     504           0 :                 if ( pImplLib->hasElements() )
     505           0 :                     return sal_True;
     506             :             }
     507             :             else
     508             :             {
     509           0 :                 return sal_True;
     510             :             }
     511             :         }
     512           0 :     }
     513             : 
     514           0 :     return sal_False;
     515             : }
     516             : 
     517         802 : void SAL_CALL SfxLibraryContainer::setModified( sal_Bool _bModified )
     518             :     throw (PropertyVetoException, RuntimeException)
     519             : {
     520         802 :     LibraryContainerMethodGuard aGuard( *this );
     521         802 :     maModifiable.setModified( _bModified );
     522         802 : }
     523             : 
     524           0 : void SAL_CALL SfxLibraryContainer::addModifyListener( const Reference< XModifyListener >& _rxListener )
     525             :     throw (RuntimeException)
     526             : {
     527           0 :     LibraryContainerMethodGuard aGuard( *this );
     528           0 :     maModifiable.addModifyListener( _rxListener );
     529           0 : }
     530             : 
     531           0 : void SAL_CALL SfxLibraryContainer::removeModifyListener( const Reference< XModifyListener >& _rxListener )
     532             :     throw (RuntimeException)
     533             : {
     534           0 :     LibraryContainerMethodGuard aGuard( *this );
     535           0 :     maModifiable.removeModifyListener( _rxListener );
     536           0 : }
     537             : 
     538             : // Methods XPersistentLibraryContainer
     539           0 : Any SAL_CALL SfxLibraryContainer::getRootLocation() throw (RuntimeException)
     540             : {
     541           0 :     LibraryContainerMethodGuard aGuard( *this );
     542           0 :     return makeAny( getRootStorage() );
     543             : }
     544             : 
     545           0 : OUString SAL_CALL SfxLibraryContainer::getContainerLocationName() throw (RuntimeException)
     546             : {
     547           0 :     LibraryContainerMethodGuard aGuard( *this );
     548           0 :     return maLibrariesDir;
     549             : }
     550             : 
     551          62 : void SAL_CALL SfxLibraryContainer::storeLibraries(  )
     552             :     throw (WrappedTargetException, RuntimeException)
     553             : {
     554          62 :     LibraryContainerMethodGuard aGuard( *this );
     555             :     try
     556             :     {
     557          62 :         storeLibraries_Impl( mxStorage, mxStorage.is()  );
     558             :         // we need to store *all* libraries if and only if we are based on a storage:
     559             :         // in this case, storeLibraries_Impl will remove the source storage, after loading
     560             :         // all libraries, so we need to force them to be stored, again
     561             :     }
     562           0 :     catch( const Exception& )
     563             :     {
     564           0 :         throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
     565          62 :     }
     566          62 : }
     567             : 
     568           0 : static void checkAndCopyFileImpl( const INetURLObject& rSourceFolderInetObj,
     569             :                                   const INetURLObject& rTargetFolderInetObj,
     570             :                                   const OUString& rCheckFileName,
     571             :                                   const OUString& rCheckExtension,
     572             :                                   Reference< XSimpleFileAccess3 > xSFI )
     573             : {
     574           0 :     INetURLObject aTargetFolderInetObj( rTargetFolderInetObj );
     575             :     aTargetFolderInetObj.insertName( rCheckFileName, sal_True, INetURLObject::LAST_SEGMENT,
     576           0 :                                      sal_True, INetURLObject::ENCODE_ALL );
     577           0 :     aTargetFolderInetObj.setExtension( rCheckExtension );
     578           0 :     OUString aTargetFile = aTargetFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
     579           0 :     if( !xSFI->exists( aTargetFile ) )
     580             :     {
     581           0 :         INetURLObject aSourceFolderInetObj( rSourceFolderInetObj );
     582             :         aSourceFolderInetObj.insertName( rCheckFileName, sal_True, INetURLObject::LAST_SEGMENT,
     583           0 :                                          sal_True, INetURLObject::ENCODE_ALL );
     584           0 :         aSourceFolderInetObj.setExtension( rCheckExtension );
     585           0 :         OUString aSourceFile = aSourceFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
     586           0 :         xSFI->copy( aSourceFile, aTargetFile );
     587           0 :     }
     588           0 : }
     589             : 
     590         985 : static void createVariableURL( OUString& rStr, const OUString& rLibName,
     591             :                                const OUString& rInfoFileName, bool bUser )
     592             : {
     593         985 :     if( bUser )
     594             :     {
     595         985 :         rStr = OUString("$(USER)/basic/");
     596             :     }
     597             :     else
     598             :     {
     599           0 :         rStr = OUString("$(INST)/share/basic/");
     600             :     }
     601         985 :     rStr += rLibName;
     602         985 :     rStr += "/";
     603         985 :     rStr += rInfoFileName;
     604         985 :     rStr += ".xlb/";
     605         985 : }
     606             : 
     607        1823 : void SfxLibraryContainer::init( const OUString& rInitialDocumentURL, const uno::Reference< embed::XStorage >& rxInitialStorage )
     608             : {
     609             :     // this might be called from within the ctor, and the impl_init might (indirectly) create
     610             :     // an UNO reference to ourself.
     611             :     // Ensure that we're not destroyed while we're in here
     612        1823 :     osl_atomic_increment( &m_refCount );
     613        1823 :     init_Impl( rInitialDocumentURL, rxInitialStorage );
     614        1822 :     osl_atomic_decrement( &m_refCount );
     615        1822 : }
     616             : 
     617        1823 : void SfxLibraryContainer::init_Impl( const OUString& rInitialDocumentURL,
     618             :                                      const uno::Reference< embed::XStorage >& rxInitialStorage )
     619             : {
     620        1823 :     uno::Reference< embed::XStorage > xStorage = rxInitialStorage;
     621             : 
     622        1823 :     maInitialDocumentURL = rInitialDocumentURL;
     623        1823 :     maInfoFileName = OUString::createFromAscii( getInfoFileName() );
     624        1823 :     maOldInfoFileName = OUString::createFromAscii( getOldInfoFileName() );
     625        1823 :     maLibElementFileExtension = OUString::createFromAscii( getLibElementFileExtension() );
     626        1823 :     maLibrariesDir = OUString::createFromAscii( getLibrariesDir() );
     627             : 
     628        1823 :     meInitMode = DEFAULT;
     629        3646 :     INetURLObject aInitUrlInetObj( maInitialDocumentURL );
     630        3646 :     OUString aInitFileName = aInitUrlInetObj.GetMainURL( INetURLObject::NO_DECODE );
     631        1823 :     if( !aInitFileName.isEmpty() )
     632             :     {
     633             :         // We need a BasicManager to avoid problems
     634           0 :         StarBASIC* pBas = new StarBASIC();
     635           0 :         mpBasMgr = new BasicManager( pBas );
     636           0 :         mbOwnBasMgr = true;
     637             : 
     638           0 :         OUString aExtension = aInitUrlInetObj.getExtension();
     639           0 :         if( aExtension.compareToAscii( "xlc" ) == COMPARE_EQUAL )
     640             :         {
     641           0 :             meInitMode = CONTAINER_INIT_FILE;
     642           0 :             INetURLObject aLibPathInetObj( aInitUrlInetObj );
     643           0 :             aLibPathInetObj.removeSegment();
     644           0 :             maLibraryPath = aLibPathInetObj.GetMainURL( INetURLObject::NO_DECODE );
     645             :         }
     646           0 :         else if( aExtension.compareToAscii( "xlb" ) == COMPARE_EQUAL )
     647             :         {
     648           0 :             meInitMode = LIBRARY_INIT_FILE;
     649           0 :             uno::Reference< embed::XStorage > xDummyStor;
     650           0 :             ::xmlscript::LibDescriptor aLibDesc;
     651           0 :             implLoadLibraryIndexFile( NULL, aLibDesc, xDummyStor, aInitFileName );
     652           0 :             return;
     653             :         }
     654             :         else
     655             :         {
     656             :             // Decide between old and new document
     657           0 :             sal_Bool bOldStorage = SotStorage::IsOLEStorage( aInitFileName );
     658           0 :             if ( bOldStorage )
     659             :             {
     660           0 :                 meInitMode = OLD_BASIC_STORAGE;
     661           0 :                 importFromOldStorage( aInitFileName );
     662           0 :                 return;
     663             :             }
     664             :             else
     665             :             {
     666           0 :                 meInitMode = OFFICE_DOCUMENT;
     667             :                 try
     668             :                 {
     669           0 :                     xStorage = ::comphelper::OStorageHelper::GetStorageFromURL( aInitFileName, embed::ElementModes::READ );
     670             :                 }
     671           0 :                 catch (const uno::Exception& )
     672             :                 {
     673             :                     // TODO: error handling
     674             :                 }
     675             :             }
     676           0 :         }
     677             :     }
     678             :     else
     679             :     {
     680             :         // Default paths
     681        1823 :         maLibraryPath = SvtPathOptions().GetBasicPath();
     682             :     }
     683             : 
     684        3645 :     Reference< XParser > xParser = xml::sax::Parser::create(mxContext);
     685             : 
     686        3644 :     uno::Reference< io::XInputStream > xInput;
     687             : 
     688        1822 :     mxStorage = xStorage;
     689        1822 :     bool bStorage = mxStorage.is();
     690             : 
     691             : 
     692             :     // #110009: Scope to force the StorageRefs to be destructed and
     693             :     // so the streams to be closed before the preload operation
     694             :     {
     695             : 
     696        1822 :     uno::Reference< embed::XStorage > xLibrariesStor;
     697        3644 :     OUString aFileName;
     698             : 
     699        1822 :     int nPassCount = 1;
     700        1822 :     if( !bStorage && meInitMode == DEFAULT )
     701             :     {
     702         124 :         nPassCount = 2;
     703             :     }
     704        3768 :     for( int nPass = 0 ; nPass < nPassCount ; nPass++ )
     705             :     {
     706        1946 :         if( bStorage )
     707             :         {
     708             :             SAL_WARN_IF(
     709             :                 meInitMode != DEFAULT && meInitMode != OFFICE_DOCUMENT, "basic",
     710             :                 "Wrong InitMode for document");
     711             :             try
     712             :             {
     713        1698 :                 uno::Reference< io::XStream > xStream;
     714        1698 :                 xLibrariesStor = xStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
     715             : 
     716           3 :                 if ( xLibrariesStor.is() )
     717             :                 {
     718           3 :                     aFileName = maInfoFileName;
     719           3 :                     aFileName += "-lc.xml";
     720             : 
     721             :                     try
     722             :                     {
     723           3 :                         xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
     724             :                     }
     725           0 :                     catch(const uno::Exception& )
     726             :                     {}
     727             : 
     728           3 :                     if( !xStream.is() )
     729             :                     {
     730           0 :                         mbOldInfoFormat = true;
     731             : 
     732             :                         // Check old version
     733           0 :                         aFileName = maOldInfoFileName;
     734           0 :                         aFileName += ".xml";
     735             : 
     736             :                         try
     737             :                         {
     738           0 :                             xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
     739             :                         }
     740           0 :                         catch(const uno::Exception& )
     741             :                         {}
     742             : 
     743           0 :                         if( !xStream.is() )
     744             :                         {
     745             :                             // Check for EA2 document version with wrong extensions
     746           0 :                             aFileName = maOldInfoFileName;
     747           0 :                             aFileName += ".xli";
     748           0 :                             xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
     749             :                         }
     750             :                     }
     751             :                 }
     752             : 
     753           3 :                 if ( xStream.is() )
     754             :                 {
     755           3 :                     xInput = xStream->getInputStream();
     756        1698 :                 }
     757             :             }
     758        1695 :             catch(const uno::Exception& )
     759             :             {
     760             :                 // TODO: error handling?
     761             :             }
     762             :         }
     763             :         else
     764             :         {
     765         248 :             INetURLObject* pLibInfoInetObj = NULL;
     766         248 :             if( meInitMode == CONTAINER_INIT_FILE )
     767             :             {
     768           0 :                 aFileName = aInitFileName;
     769             :             }
     770             :             else
     771             :             {
     772         248 :                 if( nPass == 1 )
     773             :                 {
     774         124 :                     pLibInfoInetObj = new INetURLObject( maLibraryPath.getToken(0, (sal_Unicode)';') );
     775             :                 }
     776             :                 else
     777             :                 {
     778         124 :                     pLibInfoInetObj = new INetURLObject( maLibraryPath.getToken(1, (sal_Unicode)';') );
     779             :                 }
     780         248 :                 pLibInfoInetObj->insertName( maInfoFileName, sal_False, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
     781         248 :                 pLibInfoInetObj->setExtension( OUString("xlc") );
     782         248 :                 aFileName = pLibInfoInetObj->GetMainURL( INetURLObject::NO_DECODE );
     783             :             }
     784             : 
     785             :             try
     786             :             {
     787         248 :                 xInput = mxSFI->openFileRead( aFileName );
     788             :             }
     789         122 :             catch(const Exception& )
     790             :             {
     791             :                 // Silently tolerate empty or missing files
     792         122 :                 xInput.clear();
     793             :             }
     794             : 
     795             :             // Old variant?
     796         248 :             if( !xInput.is() && nPass == 0 )
     797             :             {
     798          62 :                 INetURLObject aLibInfoInetObj( maLibraryPath.getToken(1, (sal_Unicode)';') );
     799          62 :                 aLibInfoInetObj.insertName( maOldInfoFileName, sal_False, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
     800          62 :                 aLibInfoInetObj.setExtension( OUString( "xli") );
     801          62 :                 aFileName = aLibInfoInetObj.GetMainURL( INetURLObject::NO_DECODE );
     802             : 
     803             :                 try
     804             :                 {
     805          62 :                     xInput = mxSFI->openFileRead( aFileName );
     806           0 :                     mbOldInfoFormat = true;
     807             :                 }
     808          62 :                 catch(const Exception& )
     809             :                 {
     810          62 :                     xInput.clear();
     811          62 :                 }
     812             :             }
     813             : 
     814         248 :             delete pLibInfoInetObj;
     815             :         }
     816             : 
     817        1946 :         if( xInput.is() )
     818             :         {
     819         129 :             InputSource source;
     820         129 :             source.aInputStream = xInput;
     821         129 :             source.sSystemId    = aFileName;
     822             : 
     823             :             // start parsing
     824         129 :             ::xmlscript::LibDescriptorArray* pLibArray = new ::xmlscript::LibDescriptorArray();
     825             : 
     826             :             try
     827             :             {
     828         129 :                 xParser->setDocumentHandler( ::xmlscript::importLibraryContainer( pLibArray ) );
     829         129 :                 xParser->parseStream( source );
     830             :             }
     831           0 :             catch ( const xml::sax::SAXException& e )
     832             :             {
     833             :                 SAL_WARN("basic", e.Message);
     834           0 :                 return;
     835             :             }
     836           0 :             catch ( const io::IOException& e )
     837             :             {
     838             :                 SAL_WARN("basic", e.Message);
     839           0 :                 return;
     840             :             }
     841             : 
     842         129 :             sal_Int32 nLibCount = pLibArray->mnLibCount;
     843         706 :             for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
     844             :             {
     845         577 :                 ::xmlscript::LibDescriptor& rLib = pLibArray->mpLibs[i];
     846             : 
     847             :                 // Check storage URL
     848         577 :                 OUString aStorageURL = rLib.aStorageURL;
     849         577 :                 if( !bStorage && aStorageURL.isEmpty() && nPass == 0 )
     850             :                 {
     851          62 :                     OUString aLibraryPath;
     852          62 :                     if( meInitMode == CONTAINER_INIT_FILE )
     853             :                     {
     854           0 :                         aLibraryPath = maLibraryPath;
     855             :                     }
     856             :                     else
     857             :                     {
     858          62 :                         aLibraryPath = maLibraryPath.getToken(1, (sal_Unicode)';');
     859             :                     }
     860         124 :                     INetURLObject aInetObj( aLibraryPath );
     861             : 
     862             :                     aInetObj.insertName( rLib.aName, sal_True, INetURLObject::LAST_SEGMENT,
     863          62 :                                          sal_True, INetURLObject::ENCODE_ALL );
     864         124 :                     OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
     865          62 :                     if( mxSFI->isFolder( aLibDirPath ) )
     866             :                     {
     867          62 :                         createVariableURL( rLib.aStorageURL, rLib.aName, maInfoFileName, true );
     868          62 :                         maModifiable.setModified( sal_True );
     869             :                     }
     870           0 :                     else if( rLib.bLink )
     871             :                     {
     872             :                         // Check "share" path
     873           0 :                         INetURLObject aShareInetObj( maLibraryPath.getToken(0, (sal_Unicode)';') );
     874             :                         aShareInetObj.insertName( rLib.aName, sal_True, INetURLObject::LAST_SEGMENT,
     875           0 :                                                   sal_True, INetURLObject::ENCODE_ALL );
     876           0 :                         OUString aShareLibDirPath = aShareInetObj.GetMainURL( INetURLObject::NO_DECODE );
     877           0 :                         if( mxSFI->isFolder( aShareLibDirPath ) )
     878             :                         {
     879           0 :                             createVariableURL( rLib.aStorageURL, rLib.aName, maInfoFileName, false );
     880           0 :                             maModifiable.setModified( sal_True );
     881             :                         }
     882             :                         else
     883             :                         {
     884             :                             // #i25537: Ignore lib if library folder does not really exist
     885           0 :                             continue;
     886           0 :                         }
     887          62 :                     }
     888             :                 }
     889             : 
     890        1154 :                 OUString aLibName = rLib.aName;
     891             : 
     892             :                 // If the same library name is used by the shared and the
     893             :                 // user lib container index files the user file wins
     894         577 :                 if( nPass == 1 && hasByName( aLibName ) )
     895             :                 {
     896           0 :                     continue;
     897             :                 }
     898             :                 SfxLibrary* pImplLib;
     899         577 :                 if( rLib.bLink )
     900             :                 {
     901             :                     Reference< XNameAccess > xLib =
     902         512 :                         createLibraryLink( aLibName, rLib.aStorageURL, rLib.bReadOnly );
     903         512 :                     pImplLib = static_cast< SfxLibrary* >( xLib.get() );
     904             :                 }
     905             :                 else
     906             :                 {
     907          65 :                     Reference< XNameContainer > xLib = createLibrary( aLibName );
     908          65 :                     pImplLib = static_cast< SfxLibrary* >( xLib.get() );
     909          65 :                     pImplLib->mbLoaded = sal_False;
     910          65 :                     pImplLib->mbReadOnly = rLib.bReadOnly;
     911          65 :                     if( !bStorage )
     912             :                     {
     913             :                         checkStorageURL( rLib.aStorageURL, pImplLib->maLibInfoFileURL,
     914          62 :                                          pImplLib->maStorageURL, pImplLib->maUnexpandedStorageURL );
     915          65 :                     }
     916             :                 }
     917         577 :                 maModifiable.setModified( sal_False );
     918             : 
     919             :                 // Read library info files
     920         577 :                 if( !mbOldInfoFormat )
     921             :                 {
     922         577 :                     uno::Reference< embed::XStorage > xLibraryStor;
     923         577 :                     if( !pImplLib->mbInitialised && bStorage )
     924             :                     {
     925             :                         try
     926             :                         {
     927           9 :                             xLibraryStor = xLibrariesStor->openStorageElement( rLib.aName,
     928           6 :                                                                                 embed::ElementModes::READ );
     929             :                         }
     930           0 :                         catch(const uno::Exception& )
     931             :                         {
     932             :                             #if OSL_DEBUG_LEVEL > 0
     933             :                             Any aError( ::cppu::getCaughtException() );
     934             :                             SAL_WARN(
     935             :                                 "basic",
     936             :                                 "couldn't open sub storage for library \""
     937             :                                     << rLib.aName << "\". Exception: "
     938             :                                     << comphelper::anyToString(aError));
     939             :                             #endif
     940             :                         }
     941             :                     }
     942             : 
     943             :                     // Link is already initialised in createLibraryLink()
     944         577 :                     if( !pImplLib->mbInitialised && (!bStorage || xLibraryStor.is()) )
     945             :                     {
     946          65 :                         OUString aIndexFileName;
     947          65 :                         bool bLoaded = implLoadLibraryIndexFile( pImplLib, rLib, xLibraryStor, aIndexFileName );
     948             :                         SAL_WARN_IF(
     949             :                             bLoaded && aLibName != rLib.aName, "basic",
     950             :                             ("Different library names in library container and"
     951             :                              " library info files!"));
     952          65 :                         if( GbMigrationSuppressErrors && !bLoaded )
     953             :                         {
     954           0 :                             removeLibrary( aLibName );
     955          65 :                         }
     956         577 :                     }
     957             :                 }
     958           0 :                 else if( !bStorage )
     959             :                 {
     960             :                     // Write new index file immediately because otherwise
     961             :                     // the library elements will be lost when storing into
     962             :                     // the new info format
     963           0 :                     uno::Reference< embed::XStorage > xTmpStorage;
     964           0 :                     implStoreLibraryIndexFile( pImplLib, rLib, xTmpStorage );
     965             :                 }
     966             : 
     967         577 :                 implImportLibDescriptor( pImplLib, rLib );
     968             : 
     969         577 :                 if( nPass == 1 )
     970             :                 {
     971         512 :                     pImplLib->mbSharedIndexFile = true;
     972         512 :                     pImplLib->mbReadOnly = sal_True;
     973             :                 }
     974         577 :             }
     975             : 
     976             :             // Keep flag for documents to force writing the new index files
     977         129 :             if( !bStorage )
     978             :             {
     979         126 :                 mbOldInfoFormat = false;
     980             :             }
     981         129 :             delete pLibArray;
     982             :         }
     983        1822 :     }
     984             : 
     985             :     // #110009: END Scope to force the StorageRefs to be destructed
     986             :     }
     987             : 
     988        1822 :     if( !bStorage && meInitMode == DEFAULT )
     989             :     {
     990             :         try
     991             :         {
     992         124 :             implScanExtensions();
     993             :         }
     994           2 :         catch(const uno::Exception& )
     995             :         {
     996             :             // TODO: error handling?
     997             :             SAL_WARN("basic", "Cannot access extensions!");
     998             :         }
     999             :     }
    1000             : 
    1001             :     // Preload?
    1002             :     {
    1003        1822 :         Sequence< OUString > aNames = maNameContainer.getElementNames();
    1004        1822 :         const OUString* pNames = aNames.getConstArray();
    1005        1822 :         sal_Int32 nNameCount = aNames.getLength();
    1006        2399 :         for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
    1007             :         {
    1008         577 :             OUString aName = pNames[ i ];
    1009         577 :             SfxLibrary* pImplLib = getImplLib( aName );
    1010         577 :             if( pImplLib->mbPreload )
    1011             :             {
    1012           0 :                 loadLibrary( aName );
    1013             :             }
    1014        2399 :         }
    1015             :     }
    1016             : 
    1017        1822 :     if( meInitMode == DEFAULT )
    1018             :     {
    1019        1822 :         INetURLObject aUserBasicInetObj( maLibraryPath.getToken(1, (sal_Unicode)';') );
    1020        3644 :         OUString aStandardStr("Standard");
    1021             : 
    1022             :         static char const strPrevFolderName_1[] = "__basic_80";
    1023             :         static char const strPrevFolderName_2[] = "__basic_80_2";
    1024        3644 :         INetURLObject aPrevUserBasicInetObj_1( aUserBasicInetObj );
    1025        1822 :         aPrevUserBasicInetObj_1.removeSegment();
    1026        3644 :         INetURLObject aPrevUserBasicInetObj_2 = aPrevUserBasicInetObj_1;
    1027        1822 :         aPrevUserBasicInetObj_1.Append( OString( strPrevFolderName_1 ));
    1028        1822 :         aPrevUserBasicInetObj_2.Append( OString( strPrevFolderName_2 ));
    1029             : 
    1030             :         // #i93163
    1031        1822 :         bool bCleanUp = false;
    1032             :         try
    1033             :         {
    1034        1822 :             INetURLObject aPrevUserBasicInetObj = aPrevUserBasicInetObj_1;
    1035        3644 :             OUString aPrevFolder = aPrevUserBasicInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1036        1822 :             if( mxSFI->isFolder( aPrevFolder ) )
    1037             :             {
    1038             :                 // Check if Standard folder exists and is complete
    1039           0 :                 INetURLObject aUserBasicStandardInetObj( aUserBasicInetObj );
    1040             :                 aUserBasicStandardInetObj.insertName( aStandardStr, sal_True, INetURLObject::LAST_SEGMENT,
    1041           0 :                                                       sal_True, INetURLObject::ENCODE_ALL );
    1042           0 :                 INetURLObject aPrevUserBasicStandardInetObj( aPrevUserBasicInetObj );
    1043             :                 aPrevUserBasicStandardInetObj.insertName( aStandardStr, sal_True, INetURLObject::LAST_SEGMENT,
    1044           0 :                                                         sal_True, INetURLObject::ENCODE_ALL );
    1045           0 :                 OUString aPrevStandardFolder = aPrevUserBasicStandardInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1046           0 :                 if( mxSFI->isFolder( aPrevStandardFolder ) )
    1047             :                 {
    1048           0 :                     OUString aXlbExtension( "xlb" );
    1049           0 :                     OUString aCheckFileName;
    1050             : 
    1051             :                     // Check if script.xlb exists
    1052           0 :                     aCheckFileName = OUString("script");
    1053             :                     checkAndCopyFileImpl( aUserBasicStandardInetObj,
    1054             :                                           aPrevUserBasicStandardInetObj,
    1055           0 :                                           aCheckFileName, aXlbExtension, mxSFI );
    1056             : 
    1057             :                     // Check if dialog.xlb exists
    1058           0 :                     aCheckFileName = OUString("dialog");
    1059             :                     checkAndCopyFileImpl( aUserBasicStandardInetObj,
    1060             :                                           aPrevUserBasicStandardInetObj,
    1061           0 :                                           aCheckFileName, aXlbExtension, mxSFI );
    1062             : 
    1063             :                     // Check if module1.xba exists
    1064           0 :                     OUString aXbaExtension("xba");
    1065           0 :                     aCheckFileName = OUString("Module1");
    1066             :                     checkAndCopyFileImpl( aUserBasicStandardInetObj,
    1067             :                                           aPrevUserBasicStandardInetObj,
    1068           0 :                                           aCheckFileName, aXbaExtension, mxSFI );
    1069             :                 }
    1070             :                 else
    1071             :                 {
    1072           0 :                     OUString aStandardFolder = aUserBasicStandardInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1073           0 :                     mxSFI->copy( aStandardFolder, aPrevStandardFolder );
    1074             :                 }
    1075             : 
    1076           0 :                 OUString aPrevCopyToFolder = aPrevUserBasicInetObj_2.GetMainURL( INetURLObject::NO_DECODE );
    1077           0 :                 mxSFI->copy( aPrevFolder, aPrevCopyToFolder );
    1078             :             }
    1079             :             else
    1080             :             {
    1081        1822 :                 aPrevUserBasicInetObj = aPrevUserBasicInetObj_2;
    1082        1822 :                 aPrevFolder = aPrevUserBasicInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1083             :             }
    1084        1822 :             if( mxSFI->isFolder( aPrevFolder ) )
    1085             :             {
    1086           0 :                 SfxLibraryContainer* pPrevCont = createInstanceImpl();
    1087           0 :                 Reference< XInterface > xRef = static_cast< XInterface* >( static_cast< OWeakObject* >(pPrevCont) );
    1088             : 
    1089             :                 // Rename previous basic folder to make storage URLs correct during initialisation
    1090           0 :                 OUString aFolderUserBasic = aUserBasicInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1091           0 :                 INetURLObject aUserBasicTmpInetObj( aUserBasicInetObj );
    1092           0 :                 aUserBasicTmpInetObj.removeSegment();
    1093           0 :                 aUserBasicTmpInetObj.Append( OString( "__basic_tmp" ));
    1094           0 :                 OUString aFolderTmp = aUserBasicTmpInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1095             : 
    1096           0 :                 mxSFI->move( aFolderUserBasic, aFolderTmp );
    1097             :                 try
    1098             :                 {
    1099           0 :                     mxSFI->move( aPrevFolder, aFolderUserBasic );
    1100             :                 }
    1101           0 :                 catch(const Exception& )
    1102             :                 {
    1103             :                     // Move back user/basic folder
    1104             :                     try
    1105             :                     {
    1106           0 :                            mxSFI->kill( aFolderUserBasic );
    1107             :                     }
    1108           0 :                     catch(const Exception& )
    1109             :                     {}
    1110           0 :                     mxSFI->move( aFolderTmp, aFolderUserBasic );
    1111           0 :                     throw;
    1112             :                 }
    1113             : 
    1114           0 :                 INetURLObject aPrevUserBasicLibInfoInetObj( aUserBasicInetObj );
    1115             :                 aPrevUserBasicLibInfoInetObj.insertName( maInfoFileName, sal_False, INetURLObject::LAST_SEGMENT,
    1116           0 :                                                     sal_True, INetURLObject::ENCODE_ALL );
    1117           0 :                 aPrevUserBasicLibInfoInetObj.setExtension( OUString("xlc"));
    1118           0 :                 OUString aLibInfoFileName = aPrevUserBasicLibInfoInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1119           0 :                 Sequence<Any> aInitSeq( 1 );
    1120           0 :                 aInitSeq.getArray()[0] <<= aLibInfoFileName;
    1121           0 :                 GbMigrationSuppressErrors = true;
    1122           0 :                 pPrevCont->initialize( aInitSeq );
    1123           0 :                 GbMigrationSuppressErrors = false;
    1124             : 
    1125             :                 // Rename folders back
    1126           0 :                 mxSFI->move( aFolderUserBasic, aPrevFolder );
    1127           0 :                 mxSFI->move( aFolderTmp, aFolderUserBasic );
    1128             : 
    1129           0 :                 OUString aUserSearchStr("vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE");
    1130           0 :                 OUString aSharedSearchStr("vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE");
    1131           0 :                 OUString aBundledSearchStr("vnd.sun.star.expand:$BUNDLED_EXTENSIONS");
    1132           0 :                 OUString aInstSearchStr("$(INST)");
    1133             : 
    1134           0 :                 Sequence< OUString > aNames = pPrevCont->getElementNames();
    1135           0 :                 const OUString* pNames = aNames.getConstArray();
    1136           0 :                 sal_Int32 nNameCount = aNames.getLength();
    1137             : 
    1138           0 :                 for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
    1139             :                 {
    1140           0 :                     OUString aLibName = pNames[ i ];
    1141           0 :                     if( hasByName( aLibName ) )
    1142             :                     {
    1143           0 :                         if( aLibName == aStandardStr )
    1144             :                         {
    1145           0 :                             SfxLibrary* pImplLib = getImplLib( aStandardStr );
    1146           0 :                             INetURLObject aStandardFolderInetObj( pImplLib->maStorageURL );
    1147           0 :                             OUString aStandardFolder = pImplLib->maStorageURL;
    1148           0 :                             mxSFI->kill( aStandardFolder );
    1149             :                         }
    1150             :                         else
    1151             :                         {
    1152           0 :                             continue;
    1153             :                         }
    1154             :                     }
    1155             : 
    1156           0 :                     SfxLibrary* pImplLib = pPrevCont->getImplLib( aLibName );
    1157           0 :                     if( pImplLib->mbLink )
    1158             :                     {
    1159           0 :                         OUString aStorageURL = pImplLib->maUnexpandedStorageURL;
    1160           0 :                         bool bCreateLink = true;
    1161           0 :                         if( aStorageURL.indexOf( aUserSearchStr   ) != -1 ||
    1162           0 :                             aStorageURL.indexOf( aSharedSearchStr ) != -1 ||
    1163           0 :                             aStorageURL.indexOf( aBundledSearchStr ) != -1 ||
    1164           0 :                             aStorageURL.indexOf( aInstSearchStr   ) != -1 )
    1165             :                         {
    1166           0 :                             bCreateLink = false;
    1167             :                         }
    1168           0 :                         if( bCreateLink )
    1169             :                         {
    1170           0 :                             createLibraryLink( aLibName, pImplLib->maStorageURL, pImplLib->mbReadOnly );
    1171           0 :                         }
    1172             :                     }
    1173             :                     else
    1174             :                     {
    1175             :                         // Move folder if not already done
    1176           0 :                         INetURLObject aUserBasicLibFolderInetObj( aUserBasicInetObj );
    1177           0 :                         aUserBasicLibFolderInetObj.Append( aLibName );
    1178           0 :                         OUString aLibFolder = aUserBasicLibFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1179             : 
    1180           0 :                         INetURLObject aPrevUserBasicLibFolderInetObj( aPrevUserBasicInetObj );
    1181           0 :                         aPrevUserBasicLibFolderInetObj.Append( aLibName );
    1182           0 :                         OUString aPrevLibFolder = aPrevUserBasicLibFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1183             : 
    1184           0 :                         if( mxSFI->isFolder( aPrevLibFolder ) && !mxSFI->isFolder( aLibFolder ) )
    1185             :                         {
    1186           0 :                             mxSFI->move( aPrevLibFolder, aLibFolder );
    1187             :                         }
    1188             : 
    1189           0 :                         if( aLibName == aStandardStr )
    1190             :                         {
    1191           0 :                             maNameContainer.removeByName( aLibName );
    1192             :                         }
    1193             : 
    1194             :                         // Create library
    1195           0 :                         Reference< XNameContainer > xLib = createLibrary( aLibName );
    1196           0 :                            SfxLibrary* pNewLib = static_cast< SfxLibrary* >( xLib.get() );
    1197           0 :                         pNewLib->mbLoaded = false;
    1198           0 :                         pNewLib->implSetModified( sal_False );
    1199             :                         checkStorageURL( aLibFolder, pNewLib->maLibInfoFileURL,
    1200           0 :                                          pNewLib->maStorageURL, pNewLib->maUnexpandedStorageURL );
    1201             : 
    1202           0 :                         uno::Reference< embed::XStorage > xDummyStor;
    1203           0 :                         ::xmlscript::LibDescriptor aLibDesc;
    1204           0 :                         implLoadLibraryIndexFile( pNewLib, aLibDesc, xDummyStor, pNewLib->maLibInfoFileURL );
    1205           0 :                         implImportLibDescriptor( pNewLib, aLibDesc );
    1206             :                     }
    1207           0 :                 }
    1208           0 :                 mxSFI->kill( aPrevFolder );
    1209        1822 :             }
    1210             :         }
    1211           0 :         catch(const Exception& e)
    1212             :         {
    1213           0 :             bCleanUp = true;
    1214             :             SAL_WARN("basic", "Upgrade of Basic installation failed somehow: " << e.Message);
    1215             :         }
    1216             : 
    1217             :         // #i93163
    1218        1822 :         if( bCleanUp )
    1219             :         {
    1220             :             static const char strErrorSavFolderName[] = "__basic_80_err";
    1221           0 :             INetURLObject aPrevUserBasicInetObj_Err( aUserBasicInetObj );
    1222           0 :             aPrevUserBasicInetObj_Err.removeSegment();
    1223           0 :             aPrevUserBasicInetObj_Err.Append( OString( strErrorSavFolderName ));
    1224           0 :             OUString aPrevFolder_Err = aPrevUserBasicInetObj_Err.GetMainURL( INetURLObject::NO_DECODE );
    1225             : 
    1226           0 :             bool bSaved = false;
    1227             :             try
    1228             :             {
    1229           0 :                 OUString aPrevFolder_1 = aPrevUserBasicInetObj_1.GetMainURL( INetURLObject::NO_DECODE );
    1230           0 :                 if( mxSFI->isFolder( aPrevFolder_1 ) )
    1231             :                 {
    1232           0 :                     mxSFI->move( aPrevFolder_1, aPrevFolder_Err );
    1233           0 :                     bSaved = true;
    1234           0 :                 }
    1235             :             }
    1236           0 :             catch(const Exception& )
    1237             :             {}
    1238             :             try
    1239             :             {
    1240           0 :                 OUString aPrevFolder_2 = aPrevUserBasicInetObj_2.GetMainURL( INetURLObject::NO_DECODE );
    1241           0 :                 if( !bSaved && mxSFI->isFolder( aPrevFolder_2 ) )
    1242             :                 {
    1243           0 :                     mxSFI->move( aPrevFolder_2, aPrevFolder_Err );
    1244             :                 }
    1245             :                 else
    1246             :                 {
    1247           0 :                     mxSFI->kill( aPrevFolder_2 );
    1248           0 :                 }
    1249             :             }
    1250           0 :             catch(const Exception& )
    1251           0 :             {}
    1252        1822 :         }
    1253        1823 :     }
    1254             : }
    1255             : 
    1256         124 : void SfxLibraryContainer::implScanExtensions( void )
    1257             : {
    1258             : #if HAVE_FEATURE_EXTENSIONS
    1259         124 :     ScriptExtensionIterator aScriptIt;
    1260         248 :     OUString aLibURL;
    1261             : 
    1262         124 :     bool bPureDialogLib = false;
    1263         248 :     while ( !(aLibURL = aScriptIt.nextBasicOrDialogLibrary( bPureDialogLib )).isEmpty())
    1264             :     {
    1265           0 :         if( bPureDialogLib && maInfoFileName == "script" )
    1266             :         {
    1267           0 :             continue;
    1268             :         }
    1269             :         // Extract lib name
    1270           0 :         sal_Int32 nLen = aLibURL.getLength();
    1271           0 :         sal_Int32 indexLastSlash = aLibURL.lastIndexOf( '/' );
    1272           0 :         sal_Int32 nReduceCopy = 0;
    1273           0 :         if( indexLastSlash == nLen - 1 )
    1274             :         {
    1275           0 :             nReduceCopy = 1;
    1276           0 :             indexLastSlash = aLibURL.lastIndexOf( '/', nLen - 1 );
    1277             :         }
    1278             : 
    1279           0 :         OUString aLibName = aLibURL.copy( indexLastSlash + 1, nLen - indexLastSlash - nReduceCopy - 1 );
    1280             : 
    1281             :         // If a library of the same exists the existing library wins
    1282           0 :         if( hasByName( aLibName ) )
    1283             :         {
    1284           0 :             continue;
    1285             :         }
    1286             :         // Add index file to URL
    1287           0 :         OUString aIndexFileURL = aLibURL;
    1288           0 :         if( nReduceCopy == 0 )
    1289             :         {
    1290           0 :             aIndexFileURL += "/";
    1291             :         }
    1292           0 :         aIndexFileURL += maInfoFileName + ".xlb";
    1293             : 
    1294             :         // Create link
    1295           0 :         const bool bReadOnly = false;
    1296           0 :         Reference< XNameAccess > xLib = createLibraryLink( aLibName, aIndexFileURL, bReadOnly );
    1297         124 :     }
    1298             : #endif
    1299         122 : }
    1300             : 
    1301             : // Handle maLibInfoFileURL and maStorageURL correctly
    1302         574 : void SfxLibraryContainer::checkStorageURL( const OUString& aSourceURL,
    1303             :                                            OUString& aLibInfoFileURL, OUString& aStorageURL,
    1304             :                                            OUString& aUnexpandedStorageURL )
    1305             : {
    1306         574 :     OUString aExpandedSourceURL = expand_url( aSourceURL );
    1307         574 :     if( aExpandedSourceURL != aSourceURL )
    1308             :     {
    1309         574 :         aUnexpandedStorageURL = aSourceURL;
    1310             :     }
    1311        1148 :     INetURLObject aInetObj( aExpandedSourceURL );
    1312        1148 :     OUString aExtension = aInetObj.getExtension();
    1313         574 :     if( aExtension.compareToAscii( "xlb" ) == COMPARE_EQUAL )
    1314             :     {
    1315             :         // URL to xlb file
    1316         574 :         aLibInfoFileURL = aExpandedSourceURL;
    1317         574 :         aInetObj.removeSegment();
    1318         574 :         aStorageURL = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1319             :     }
    1320             :     else
    1321             :     {
    1322             :         // URL to library folder
    1323           0 :         aStorageURL = aExpandedSourceURL;
    1324           0 :         aInetObj.insertName( maInfoFileName, sal_False, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    1325           0 :         aInetObj.setExtension( OUString("xlb") );
    1326           0 :         aLibInfoFileURL = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1327         574 :     }
    1328         574 : }
    1329             : 
    1330        7407 : SfxLibrary* SfxLibraryContainer::getImplLib( const OUString& rLibraryName )
    1331             : {
    1332        7407 :     Any aLibAny = maNameContainer.getByName( rLibraryName ) ;
    1333       14814 :     Reference< XNameAccess > xNameAccess;
    1334        7407 :     aLibAny >>= xNameAccess;
    1335        7407 :     SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
    1336       14814 :     return pImplLib;
    1337             : }
    1338             : 
    1339             : 
    1340             : // Storing with password encryption
    1341             : 
    1342             : // Empty implementation, avoids unneccesary implementation in dlgcont.cxx
    1343           0 : sal_Bool SfxLibraryContainer::implStorePasswordLibrary( SfxLibrary*,
    1344             :                                                         const OUString&,
    1345             :                                                         const uno::Reference< embed::XStorage >&,
    1346             :                                                         const uno::Reference< task::XInteractionHandler >&  )
    1347             : {
    1348           0 :     return sal_False;
    1349             : }
    1350             : 
    1351           0 : sal_Bool SfxLibraryContainer::implStorePasswordLibrary(
    1352             :     SfxLibrary* /*pLib*/,
    1353             :     const OUString& /*aName*/,
    1354             :     const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& /*xStorage*/,
    1355             :     const OUString& /*aTargetURL*/,
    1356             :     const Reference< XSimpleFileAccess3 > /*xToUseSFI*/,
    1357             :     const uno::Reference< task::XInteractionHandler >&  )
    1358             : {
    1359           0 :     return sal_False;
    1360             : }
    1361             : 
    1362           0 : sal_Bool SfxLibraryContainer::implLoadPasswordLibrary(
    1363             :     SfxLibrary* /*pLib*/,
    1364             :     const OUString& /*Name*/,
    1365             :     sal_Bool /*bVerifyPasswordOnly*/ )
    1366             : throw(WrappedTargetException, RuntimeException)
    1367             : {
    1368           0 :     return sal_True;
    1369             : }
    1370             : 
    1371             : 
    1372             : 
    1373             : #define EXPAND_PROTOCOL "vnd.sun.star.expand"
    1374             : 
    1375         574 : OUString SfxLibraryContainer::createAppLibraryFolder( SfxLibrary* pLib, const OUString& aName )
    1376             : {
    1377         574 :     OUString aLibDirPath = pLib->maStorageURL;
    1378         574 :     if( aLibDirPath.isEmpty() )
    1379             :     {
    1380           0 :         INetURLObject aInetObj( maLibraryPath.getToken(1, (sal_Unicode)';') );
    1381           0 :         aInetObj.insertName( aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    1382             :         checkStorageURL( aInetObj.GetMainURL( INetURLObject::NO_DECODE ), pLib->maLibInfoFileURL,
    1383           0 :                          pLib->maStorageURL, pLib->maUnexpandedStorageURL );
    1384           0 :         aLibDirPath = pLib->maStorageURL;
    1385             :     }
    1386             : 
    1387         574 :     if( !mxSFI->isFolder( aLibDirPath ) )
    1388             :     {
    1389             :         try
    1390             :         {
    1391           0 :             mxSFI->createFolder( aLibDirPath );
    1392             :         }
    1393           0 :         catch(const Exception& )
    1394             :         {}
    1395             :     }
    1396             : 
    1397         574 :     return aLibDirPath;
    1398             : }
    1399             : 
    1400             : // Storing
    1401           0 : void SfxLibraryContainer::implStoreLibrary( SfxLibrary* pLib,
    1402             :                                             const OUString& aName,
    1403             :                                             const uno::Reference< embed::XStorage >& xStorage )
    1404             : {
    1405           0 :     OUString aDummyLocation;
    1406           0 :     Reference< XSimpleFileAccess3 > xDummySFA;
    1407           0 :     Reference< XInteractionHandler > xDummyHandler;
    1408           0 :     implStoreLibrary( pLib, aName, xStorage, aDummyLocation, xDummySFA, xDummyHandler );
    1409           0 : }
    1410             : 
    1411             : // New variant for library export
    1412           0 : void SfxLibraryContainer::implStoreLibrary( SfxLibrary* pLib,
    1413             :                                             const OUString& aName,
    1414             :                                             const uno::Reference< embed::XStorage >& xStorage,
    1415             :                                             const OUString& aTargetURL,
    1416             :                                             Reference< XSimpleFileAccess3 > xToUseSFI,
    1417             :                                             const Reference< XInteractionHandler >& xHandler )
    1418             : {
    1419           0 :     sal_Bool bLink = pLib->mbLink;
    1420           0 :     bool bStorage = xStorage.is() && !bLink;
    1421             : 
    1422           0 :     Sequence< OUString > aElementNames = pLib->getElementNames();
    1423           0 :     sal_Int32 nNameCount = aElementNames.getLength();
    1424           0 :     const OUString* pNames = aElementNames.getConstArray();
    1425             : 
    1426           0 :     if( bStorage )
    1427             :     {
    1428           0 :         for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
    1429             :         {
    1430           0 :             OUString aElementName = pNames[ i ];
    1431           0 :             OUString aStreamName = aElementName;
    1432           0 :             aStreamName += ".xml";
    1433             : 
    1434           0 :             if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
    1435             :             {
    1436             :                 SAL_WARN(
    1437             :                     "basic",
    1438             :                     "invalid library element \"" << aElementName << '"');
    1439           0 :                 continue;
    1440             :             }
    1441             :             try
    1442             :             {
    1443           0 :                 uno::Reference< io::XStream > xElementStream = xStorage->openStreamElement(
    1444             :                                                                     aStreamName,
    1445           0 :                                                                     embed::ElementModes::READWRITE );
    1446             :                 //    throw uno::RuntimeException(); // TODO: method must either return the stream or throw an exception
    1447             : 
    1448           0 :                 OUString aMime( "text/xml" );
    1449             : 
    1450           0 :                 uno::Reference< beans::XPropertySet > xProps( xElementStream, uno::UNO_QUERY );
    1451             :                 SAL_WARN_IF(
    1452             :                     !xProps.is(), "basic",
    1453             :                     "The StorageStream must implement XPropertySet interface!");
    1454             :                 //if ( !xProps.is() ) //TODO
    1455             : 
    1456           0 :                 if ( xProps.is() )
    1457             :                 {
    1458           0 :                     xProps->setPropertyValue("MediaType", uno::makeAny( aMime ) );
    1459             : 
    1460             :                     // #87671 Allow encryption
    1461           0 :                     xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::makeAny( sal_True ) );
    1462             : 
    1463           0 :                     Reference< XOutputStream > xOutput = xElementStream->getOutputStream();
    1464           0 :                     Reference< XNameContainer > xLib( pLib );
    1465           0 :                     writeLibraryElement( xLib, aElementName, xOutput );
    1466           0 :                 }
    1467             :             }
    1468           0 :             catch(const uno::Exception& )
    1469             :             {
    1470             :                 SAL_WARN("basic", "Problem during storing of library!");
    1471             :                 // TODO: error handling?
    1472             :             }
    1473           0 :         }
    1474           0 :         pLib->storeResourcesToStorage( xStorage );
    1475             :     }
    1476             :     else
    1477             :     {
    1478             :         // Export?
    1479           0 :         bool bExport = !aTargetURL.isEmpty();
    1480             :         try
    1481             :         {
    1482           0 :             Reference< XSimpleFileAccess3 > xSFI = mxSFI;
    1483           0 :             if( xToUseSFI.is() )
    1484             :             {
    1485           0 :                 xSFI = xToUseSFI;
    1486             :             }
    1487           0 :             OUString aLibDirPath;
    1488           0 :             if( bExport )
    1489             :             {
    1490           0 :                 INetURLObject aInetObj( aTargetURL );
    1491           0 :                 aInetObj.insertName( aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    1492           0 :                 aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1493             : 
    1494           0 :                 if( !xSFI->isFolder( aLibDirPath ) )
    1495             :                 {
    1496           0 :                     xSFI->createFolder( aLibDirPath );
    1497             :                 }
    1498           0 :                 pLib->storeResourcesToURL( aLibDirPath, xHandler );
    1499             :             }
    1500             :             else
    1501             :             {
    1502           0 :                 aLibDirPath = createAppLibraryFolder( pLib, aName );
    1503           0 :                 pLib->storeResources();
    1504             :             }
    1505             : 
    1506           0 :             for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
    1507             :             {
    1508           0 :                 OUString aElementName = pNames[ i ];
    1509             : 
    1510           0 :                 INetURLObject aElementInetObj( aLibDirPath );
    1511             :                 aElementInetObj.insertName( aElementName, sal_False,
    1512             :                                             INetURLObject::LAST_SEGMENT, sal_True,
    1513           0 :                                             INetURLObject::ENCODE_ALL );
    1514           0 :                 aElementInetObj.setExtension( maLibElementFileExtension );
    1515           0 :                 OUString aElementPath( aElementInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
    1516             : 
    1517           0 :                 if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
    1518             :                 {
    1519             :                     SAL_WARN(
    1520             :                         "basic",
    1521             :                         "invalid library element \"" << aElementName << '"');
    1522           0 :                     continue;
    1523             :                 }
    1524             : 
    1525             :                 // TODO: Check modified
    1526             :                 try
    1527             :                 {
    1528           0 :                     if( xSFI->exists( aElementPath ) )
    1529             :                     {
    1530           0 :                         xSFI->kill( aElementPath );
    1531             :                     }
    1532           0 :                     Reference< XOutputStream > xOutput = xSFI->openFileWrite( aElementPath );
    1533           0 :                     Reference< XNameContainer > xLib( pLib );
    1534           0 :                     writeLibraryElement( xLib, aElementName, xOutput );
    1535           0 :                     xOutput->closeOutput();
    1536             :                 }
    1537           0 :                 catch(const Exception& )
    1538             :                 {
    1539           0 :                     if( bExport )
    1540             :                     {
    1541           0 :                         throw;
    1542             :                     }
    1543           0 :                     SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aElementPath );
    1544           0 :                     sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    1545           0 :                     ErrorHandler::HandleError( nErrorCode );
    1546             :                 }
    1547           0 :             }
    1548             :         }
    1549           0 :         catch(const Exception& )
    1550             :         {
    1551           0 :             if( bExport )
    1552             :             {
    1553           0 :                 throw;
    1554             :             }
    1555             :         }
    1556           0 :     }
    1557           0 : }
    1558             : 
    1559           0 : void SfxLibraryContainer::implStoreLibraryIndexFile( SfxLibrary* pLib,
    1560             :                                                      const ::xmlscript::LibDescriptor& rLib,
    1561             :                                                      const uno::Reference< embed::XStorage >& xStorage )
    1562             : {
    1563           0 :     OUString aDummyLocation;
    1564           0 :     Reference< XSimpleFileAccess3 > xDummySFA;
    1565           0 :     implStoreLibraryIndexFile( pLib, rLib, xStorage, aDummyLocation, xDummySFA );
    1566           0 : }
    1567             : 
    1568             : // New variant for library export
    1569           0 : void SfxLibraryContainer::implStoreLibraryIndexFile( SfxLibrary* pLib,
    1570             :                                                      const ::xmlscript::LibDescriptor& rLib,
    1571             :                                                      const uno::Reference< embed::XStorage >& xStorage,
    1572             :                                                      const OUString& aTargetURL,
    1573             :                                                      Reference< XSimpleFileAccess3 > xToUseSFI )
    1574             : {
    1575             :     // Create sax writer
    1576           0 :     Reference< XWriter > xWriter = xml::sax::Writer::create(mxContext);
    1577             : 
    1578           0 :     sal_Bool bLink = pLib->mbLink;
    1579           0 :     bool bStorage = xStorage.is() && !bLink;
    1580             : 
    1581             :     // Write info file
    1582           0 :     uno::Reference< io::XOutputStream > xOut;
    1583           0 :     uno::Reference< io::XStream > xInfoStream;
    1584           0 :     if( bStorage )
    1585             :     {
    1586           0 :         OUString aStreamName( maInfoFileName );
    1587           0 :         aStreamName += "-lb.xml";
    1588             : 
    1589             :         try
    1590             :         {
    1591           0 :             xInfoStream = xStorage->openStreamElement( aStreamName, embed::ElementModes::READWRITE );
    1592             :             SAL_WARN_IF(!xInfoStream.is(), "basic", "No stream!");
    1593           0 :             uno::Reference< beans::XPropertySet > xProps( xInfoStream, uno::UNO_QUERY );
    1594             :             //    throw uno::RuntimeException(); // TODO
    1595             : 
    1596           0 :             if ( xProps.is() )
    1597             :             {
    1598           0 :                 OUString aMime("text/xml");
    1599           0 :                 xProps->setPropertyValue("MediaType", uno::makeAny( aMime ) );
    1600             : 
    1601             :                 // #87671 Allow encryption
    1602           0 :                 xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::makeAny( sal_True ) );
    1603             : 
    1604           0 :                 xOut = xInfoStream->getOutputStream();
    1605           0 :             }
    1606             :         }
    1607           0 :         catch(const uno::Exception& )
    1608             :         {
    1609             :             SAL_WARN("basic", "Problem during storing of library index file!");
    1610             :             // TODO: error handling?
    1611           0 :         }
    1612             :     }
    1613             :     else
    1614             :     {
    1615             :         // Export?
    1616           0 :         bool bExport = !aTargetURL.isEmpty();
    1617           0 :         Reference< XSimpleFileAccess3 > xSFI = mxSFI;
    1618           0 :         if( xToUseSFI.is() )
    1619             :         {
    1620           0 :             xSFI = xToUseSFI;
    1621             :         }
    1622           0 :         OUString aLibInfoPath;
    1623           0 :         if( bExport )
    1624             :         {
    1625           0 :             INetURLObject aInetObj( aTargetURL );
    1626           0 :             aInetObj.insertName( rLib.aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    1627           0 :             OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1628           0 :             if( !xSFI->isFolder( aLibDirPath ) )
    1629             :             {
    1630           0 :                 xSFI->createFolder( aLibDirPath );
    1631             :             }
    1632           0 :             aInetObj.insertName( maInfoFileName, sal_False, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    1633           0 :             aInetObj.setExtension( OUString( "xlb" ) );
    1634           0 :             aLibInfoPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
    1635             :         }
    1636             :         else
    1637             :         {
    1638           0 :             createAppLibraryFolder( pLib, rLib.aName );
    1639           0 :             aLibInfoPath = pLib->maLibInfoFileURL;
    1640             :         }
    1641             : 
    1642             :         try
    1643             :         {
    1644           0 :             if( xSFI->exists( aLibInfoPath ) )
    1645             :             {
    1646           0 :                 xSFI->kill( aLibInfoPath );
    1647             :             }
    1648           0 :             xOut = xSFI->openFileWrite( aLibInfoPath );
    1649             :         }
    1650           0 :         catch(const Exception& )
    1651             :         {
    1652           0 :             if( bExport )
    1653             :             {
    1654           0 :                 throw;
    1655             :             }
    1656           0 :             SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aLibInfoPath );
    1657           0 :             sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    1658           0 :             ErrorHandler::HandleError( nErrorCode );
    1659           0 :         }
    1660             :     }
    1661           0 :     if( !xOut.is() )
    1662             :     {
    1663             :         SAL_WARN("basic", "couldn't open output stream");
    1664           0 :         return;
    1665             :     }
    1666           0 :     xWriter->setOutputStream( xOut );
    1667           0 :     xmlscript::exportLibrary( xWriter, rLib );
    1668             : }
    1669             : 
    1670             : 
    1671         577 : bool SfxLibraryContainer::implLoadLibraryIndexFile(  SfxLibrary* pLib,
    1672             :                                                      ::xmlscript::LibDescriptor& rLib,
    1673             :                                                      const uno::Reference< embed::XStorage >& xStorage,
    1674             :                                                      const OUString& aIndexFileName )
    1675             : {
    1676         577 :     Reference< XParser > xParser = xml::sax::Parser::create(mxContext);
    1677             : 
    1678         577 :     sal_Bool bLink = sal_False;
    1679         577 :     bool bStorage = false;
    1680         577 :     if( pLib )
    1681             :     {
    1682         577 :         bLink = pLib->mbLink;
    1683         577 :         bStorage = xStorage.is() && !bLink;
    1684             :     }
    1685             : 
    1686             :     // Read info file
    1687        1154 :     uno::Reference< io::XInputStream > xInput;
    1688        1154 :     OUString aLibInfoPath;
    1689         577 :     if( bStorage )
    1690             :     {
    1691           3 :         aLibInfoPath = maInfoFileName;
    1692           3 :         aLibInfoPath += String( "-lb.xml" );
    1693             : 
    1694             :         try
    1695             :         {
    1696             :             uno::Reference< io::XStream > xInfoStream =
    1697           3 :                 xStorage->openStreamElement( aLibInfoPath, embed::ElementModes::READ );
    1698           3 :             xInput = xInfoStream->getInputStream();
    1699             :         }
    1700           0 :         catch(const uno::Exception& )
    1701             :         {}
    1702             :     }
    1703             :     else
    1704             :     {
    1705             :         // Create Input stream
    1706             :         //String aLibInfoPath; // attention: THIS PROBLEM MUST BE REVIEWED BY SCRIPTING OWNER!!!
    1707             : 
    1708         574 :         if( pLib )
    1709             :         {
    1710         574 :             createAppLibraryFolder( pLib, rLib.aName );
    1711         574 :             aLibInfoPath = pLib->maLibInfoFileURL;
    1712             :         }
    1713             :         else
    1714             :         {
    1715           0 :             aLibInfoPath = aIndexFileName;
    1716             :         }
    1717             :         try
    1718             :         {
    1719         574 :             xInput = mxSFI->openFileRead( aLibInfoPath );
    1720             :         }
    1721           0 :         catch(const Exception& )
    1722             :         {
    1723           0 :             xInput.clear();
    1724           0 :             if( !GbMigrationSuppressErrors )
    1725             :             {
    1726           0 :                 SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aLibInfoPath );
    1727           0 :                 sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    1728           0 :                 ErrorHandler::HandleError( nErrorCode );
    1729             :             }
    1730             :         }
    1731             :     }
    1732         577 :     if( !xInput.is() )
    1733             :     {
    1734           0 :         return false;
    1735             :     }
    1736             : 
    1737        1154 :     InputSource source;
    1738         577 :     source.aInputStream = xInput;
    1739         577 :     source.sSystemId    = aLibInfoPath;
    1740             : 
    1741             :     // start parsing
    1742             :     try
    1743             :     {
    1744         577 :         xParser->setDocumentHandler( ::xmlscript::importLibrary( rLib ) );
    1745         577 :         xParser->parseStream( source );
    1746             :     }
    1747           0 :     catch(const Exception& )
    1748             :     {
    1749             :         SAL_WARN("basic", "Parsing error");
    1750           0 :         SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aLibInfoPath );
    1751           0 :         sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    1752           0 :         ErrorHandler::HandleError( nErrorCode );
    1753           0 :         return false;
    1754             :     }
    1755             : 
    1756         577 :     if( !pLib )
    1757             :     {
    1758           0 :         Reference< XNameContainer > xLib = createLibrary( rLib.aName );
    1759           0 :         pLib = static_cast< SfxLibrary* >( xLib.get() );
    1760           0 :         pLib->mbLoaded = sal_False;
    1761           0 :         rLib.aStorageURL = aIndexFileName;
    1762             :         checkStorageURL( rLib.aStorageURL, pLib->maLibInfoFileURL, pLib->maStorageURL,
    1763           0 :                          pLib->maUnexpandedStorageURL );
    1764             : 
    1765           0 :         implImportLibDescriptor( pLib, rLib );
    1766             :     }
    1767             : 
    1768        1154 :     return true;
    1769             : }
    1770             : 
    1771        1089 : void SfxLibraryContainer::implImportLibDescriptor( SfxLibrary* pLib,
    1772             :                                                    ::xmlscript::LibDescriptor& rLib )
    1773             : {
    1774        1089 :     if( !pLib->mbInitialised )
    1775             :     {
    1776         577 :         sal_Int32 nElementCount = rLib.aElementNames.getLength();
    1777         577 :         const OUString* pElementNames = rLib.aElementNames.getConstArray();
    1778         577 :         Any aDummyElement = createEmptyLibraryElement();
    1779        2663 :         for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
    1780             :         {
    1781        2086 :             pLib->maNameContainer.insertByName( pElementNames[i], aDummyElement );
    1782             :         }
    1783         577 :         pLib->mbPasswordProtected = rLib.bPasswordProtected;
    1784         577 :         pLib->mbReadOnly = rLib.bReadOnly;
    1785         577 :         pLib->mbPreload  = rLib.bPreload;
    1786         577 :         pLib->implSetModified( sal_False );
    1787         577 :         pLib->mbInitialised = true;
    1788             :     }
    1789        1089 : }
    1790             : 
    1791             : 
    1792             : // Methods of new XLibraryStorage interface?
    1793         702 : void SfxLibraryContainer::storeLibraries_Impl( const uno::Reference< embed::XStorage >& i_rStorage,
    1794             :                                                bool bComplete )
    1795             : {
    1796         702 :     const Sequence< OUString > aNames = maNameContainer.getElementNames();
    1797         702 :     sal_Int32 nNameCount = aNames.getLength();
    1798         702 :     const OUString* pName = aNames.getConstArray();
    1799         702 :     const OUString* pNamesEnd = aNames.getConstArray() + nNameCount;
    1800             : 
    1801             :     // Don't count libs from shared index file
    1802         702 :     sal_Int32 nLibsToSave = nNameCount;
    1803        1900 :     for( ; pName != pNamesEnd; ++pName )
    1804             :     {
    1805        1198 :         SfxLibrary* pImplLib = getImplLib( *pName );
    1806        1198 :         if( pImplLib->mbSharedIndexFile || pImplLib->mbExtension )
    1807             :         {
    1808         496 :             nLibsToSave--;
    1809             :         }
    1810             :     }
    1811         702 :     if( !nLibsToSave )
    1812             :     {
    1813           0 :         return;
    1814             :     }
    1815         702 :     boost::scoped_ptr< ::xmlscript::LibDescriptorArray > pLibArray(new ::xmlscript::LibDescriptorArray(nLibsToSave));
    1816             : 
    1817             :     // Write to storage?
    1818         702 :     bool bStorage = i_rStorage.is();
    1819         702 :     uno::Reference< embed::XStorage > xSourceLibrariesStor;
    1820         702 :     uno::Reference< embed::XStorage > xTargetLibrariesStor;
    1821         702 :     OUString sTempTargetStorName;
    1822         702 :     const bool bInplaceStorage = bStorage && ( i_rStorage == mxStorage );
    1823         702 :     if ( bStorage )
    1824             :     {
    1825             :         // Don't write if only empty standard lib exists
    1826         640 :         if ( ( nNameCount == 1 ) && ( aNames[0] == "Standard" ) )
    1827             :         {
    1828         640 :             Any aLibAny = maNameContainer.getByName( aNames[0] );
    1829         640 :             Reference< XNameAccess > xNameAccess;
    1830         640 :             aLibAny >>= xNameAccess;
    1831         640 :             if ( ! ( xNameAccess->hasElements() || ( bInplaceStorage && isModified() ) ) )
    1832             :             {
    1833         640 :                 return;
    1834           0 :             }
    1835             :         }
    1836             : 
    1837             :         // create the empty target storage
    1838             :         try
    1839             :         {
    1840           0 :             OUString sTargetLibrariesStoreName;
    1841           0 :             if ( bInplaceStorage )
    1842             :             {
    1843             :                 // create a temporary target storage
    1844           0 :                 const OUStringBuffer aTempTargetNameBase = maLibrariesDir + OUString( "_temp_" );
    1845           0 :                 sal_Int32 index = 0;
    1846             :                 do
    1847             :                 {
    1848           0 :                     OUStringBuffer aTempTargetName( aTempTargetNameBase );
    1849           0 :                     aTempTargetName.append( index++ );
    1850             : 
    1851           0 :                     sTargetLibrariesStoreName = aTempTargetName.makeStringAndClear();
    1852           0 :                     if ( !i_rStorage->hasByName( sTargetLibrariesStoreName ) )
    1853             :                     {
    1854           0 :                         break;
    1855           0 :                     }
    1856             :                 }
    1857             :                 while ( true );
    1858           0 :                 sTempTargetStorName = sTargetLibrariesStoreName;
    1859             :             }
    1860             :             else
    1861             :             {
    1862           0 :                 sTargetLibrariesStoreName = maLibrariesDir;
    1863           0 :                 if ( i_rStorage->hasByName( sTargetLibrariesStoreName ) )
    1864             :                 {
    1865           0 :                     i_rStorage->removeElement( sTargetLibrariesStoreName );
    1866             :                 }
    1867             :             }
    1868             : 
    1869           0 :             xTargetLibrariesStor.set( i_rStorage->openStorageElement( sTargetLibrariesStoreName, embed::ElementModes::READWRITE ), UNO_QUERY_THROW );
    1870             :         }
    1871           0 :         catch( const uno::Exception& )
    1872             :         {
    1873             :             DBG_UNHANDLED_EXCEPTION();
    1874           0 :             return;
    1875             :         }
    1876             : 
    1877             :         // open the source storage which might be used to copy yet-unmodified libraries
    1878             :         try
    1879             :         {
    1880           0 :             if ( mxStorage->hasByName( maLibrariesDir ) || bInplaceStorage )
    1881             :             {
    1882           0 :                 xSourceLibrariesStor = mxStorage->openStorageElement( maLibrariesDir,
    1883           0 :                                                    bInplaceStorage ? embed::ElementModes::READWRITE : embed::ElementModes::READ );
    1884             :             }
    1885             :         }
    1886           0 :         catch( const uno::Exception& )
    1887             :         {
    1888             :             DBG_UNHANDLED_EXCEPTION();
    1889           0 :             return;
    1890             :         }
    1891             :     }
    1892             : 
    1893          62 :     int iArray = 0;
    1894          62 :     pName = aNames.getConstArray();
    1895          62 :     ::xmlscript::LibDescriptor aLibDescriptorForExtensionLibs;
    1896         620 :     for( ; pName != pNamesEnd; ++pName )
    1897             :     {
    1898         558 :         SfxLibrary* pImplLib = getImplLib( *pName );
    1899         558 :         if( pImplLib->mbSharedIndexFile )
    1900             :         {
    1901         496 :             continue;
    1902             :         }
    1903          62 :         const bool bExtensionLib = pImplLib->mbExtension;
    1904             :         ::xmlscript::LibDescriptor& rLib = bExtensionLib ?
    1905          62 :               aLibDescriptorForExtensionLibs : pLibArray->mpLibs[iArray];
    1906          62 :         if( !bExtensionLib )
    1907             :         {
    1908          62 :             iArray++;
    1909             :         }
    1910          62 :         rLib.aName = *pName;
    1911             : 
    1912          62 :         rLib.bLink = pImplLib->mbLink;
    1913          62 :         if( !bStorage || pImplLib->mbLink )
    1914             :         {
    1915          62 :             rLib.aStorageURL = ( pImplLib->maUnexpandedStorageURL.getLength() ) ?
    1916          62 :                 pImplLib->maUnexpandedStorageURL : pImplLib->maLibInfoFileURL;
    1917             :         }
    1918          62 :         rLib.bReadOnly = pImplLib->mbReadOnly;
    1919          62 :         rLib.bPreload = pImplLib->mbPreload;
    1920          62 :         rLib.bPasswordProtected = pImplLib->mbPasswordProtected;
    1921          62 :         rLib.aElementNames = pImplLib->getElementNames();
    1922             : 
    1923          62 :         if( pImplLib->implIsModified() || bComplete )
    1924             :         {
    1925             :             {
    1926           0 :             uno::Reference< embed::XStorage > xLibraryStor;
    1927           0 :             if( bStorage )
    1928             :             {
    1929             :                 try
    1930             :                 {
    1931           0 :                     xLibraryStor = xTargetLibrariesStor->openStorageElement(
    1932             :                                                                         rLib.aName,
    1933           0 :                                                                         embed::ElementModes::READWRITE );
    1934             :                 }
    1935           0 :                 catch(const uno::Exception& )
    1936             :                 {
    1937             :                     #if OSL_DEBUG_LEVEL > 0
    1938             :                     Any aError( ::cppu::getCaughtException() );
    1939             :                     SAL_WARN(
    1940             :                         "basic",
    1941             :                         "couldn't create sub storage for library \""
    1942             :                             << rLib.aName << "\". Exception: "
    1943             :                             << comphelper::anyToString(aError));
    1944             :                     #endif
    1945           0 :                     return;
    1946             :                 }
    1947             :             }
    1948             : 
    1949             :                 // Maybe lib is not loaded?!
    1950           0 :                 if( bComplete )
    1951             :                 {
    1952           0 :                     loadLibrary( rLib.aName );
    1953             :                 }
    1954           0 :                 if( pImplLib->mbPasswordProtected )
    1955             :                 {
    1956           0 :                     implStorePasswordLibrary( pImplLib, rLib.aName, xLibraryStor, uno::Reference< task::XInteractionHandler >() );
    1957             :                     // TODO: Check return value
    1958             :                 }
    1959             :                 else
    1960             :                 {
    1961           0 :                     implStoreLibrary( pImplLib, rLib.aName, xLibraryStor );
    1962             :                 }
    1963           0 :                 implStoreLibraryIndexFile( pImplLib, rLib, xLibraryStor );
    1964           0 :                 if( bStorage )
    1965             :                 {
    1966             :                     try
    1967             :                     {
    1968           0 :                         uno::Reference< embed::XTransactedObject > xTransact( xLibraryStor, uno::UNO_QUERY_THROW );
    1969           0 :                         xTransact->commit();
    1970             :                     }
    1971           0 :                     catch(const uno::Exception& )
    1972             :                     {
    1973             :                         DBG_UNHANDLED_EXCEPTION();
    1974             :                         // TODO: error handling
    1975             :                     }
    1976           0 :                 }
    1977             :             }
    1978           0 :             maModifiable.setModified( sal_True );
    1979           0 :             pImplLib->implSetModified( sal_False );
    1980             :         }
    1981             : 
    1982             :         // For container info ReadOnly refers to mbReadOnlyLink
    1983          62 :         rLib.bReadOnly = pImplLib->mbReadOnlyLink;
    1984             :     }
    1985             : 
    1986             :     // if we did an in-place save into a storage (i.e. a save into the storage we were already based on),
    1987             :     // then we need to clean up the temporary storage we used for this
    1988          62 :     if ( bInplaceStorage && !sTempTargetStorName.isEmpty() )
    1989             :     {
    1990             :         SAL_WARN_IF(
    1991             :             !xSourceLibrariesStor.is(), "basic",
    1992             :             ("SfxLibrariesContainer::storeLibraries_impl: unexpected: we should"
    1993             :              " have a source storage here!"));
    1994             :         try
    1995             :         {
    1996             :             // for this, we first remove everything from the source storage, then copy the complete content
    1997             :             // from the temporary target storage. From then on, what used to be the "source storage" becomes
    1998             :             // the "targt storage" for all subsequent operations.
    1999             : 
    2000             :             // (We cannot simply remove the storage, denoted by maLibrariesDir, from i_rStorage - there might be
    2001             :             // open references to it.)
    2002             : 
    2003           0 :             if ( xSourceLibrariesStor.is() )
    2004             :             {
    2005             :                 // remove
    2006           0 :                 const Sequence< OUString > aRemoveNames( xSourceLibrariesStor->getElementNames() );
    2007           0 :                 for ( const OUString* pRemoveName = aRemoveNames.getConstArray();
    2008           0 :                       pRemoveName != aRemoveNames.getConstArray() + aRemoveNames.getLength();
    2009             :                       ++pRemoveName
    2010             :                     )
    2011             :                 {
    2012           0 :                     xSourceLibrariesStor->removeElement( *pRemoveName );
    2013             :                 }
    2014             : 
    2015             :                 // copy
    2016           0 :                 const Sequence< OUString > aCopyNames( xTargetLibrariesStor->getElementNames() );
    2017           0 :                 for ( const OUString* pCopyName = aCopyNames.getConstArray();
    2018           0 :                       pCopyName != aCopyNames.getConstArray() + aCopyNames.getLength();
    2019             :                       ++pCopyName
    2020             :                     )
    2021             :                 {
    2022           0 :                     xTargetLibrariesStor->copyElementTo( *pCopyName, xSourceLibrariesStor, *pCopyName );
    2023           0 :                 }
    2024             :             }
    2025             : 
    2026             :             // close and remove temp target
    2027           0 :             xTargetLibrariesStor->dispose();
    2028           0 :             i_rStorage->removeElement( sTempTargetStorName );
    2029           0 :             xTargetLibrariesStor.clear();
    2030           0 :             sTempTargetStorName = OUString();
    2031             : 
    2032             :             // adjust target
    2033           0 :             xTargetLibrariesStor = xSourceLibrariesStor;
    2034           0 :             xSourceLibrariesStor.clear();
    2035             :         }
    2036           0 :         catch( const Exception& )
    2037             :         {
    2038             :             DBG_UNHANDLED_EXCEPTION();
    2039             :         }
    2040             :     }
    2041             : 
    2042          62 :     if( !mbOldInfoFormat && !maModifiable.isModified() )
    2043             :     {
    2044          62 :         return;
    2045             :     }
    2046           0 :     maModifiable.setModified( sal_False );
    2047           0 :     mbOldInfoFormat = false;
    2048             : 
    2049             :     // Write library container info
    2050             :     // Create sax writer
    2051           0 :     Reference< XWriter > xWriter = xml::sax::Writer::create(mxContext);
    2052             : 
    2053             :     // Write info file
    2054           0 :     uno::Reference< io::XOutputStream > xOut;
    2055           0 :     uno::Reference< io::XStream > xInfoStream;
    2056           0 :     if( bStorage )
    2057             :     {
    2058           0 :         OUString aStreamName( maInfoFileName );
    2059           0 :         aStreamName += "-lc.xml";
    2060             : 
    2061             :         try
    2062             :         {
    2063           0 :             xInfoStream = xTargetLibrariesStor->openStreamElement( aStreamName, embed::ElementModes::READWRITE );
    2064           0 :             uno::Reference< beans::XPropertySet > xProps( xInfoStream, uno::UNO_QUERY );
    2065             :             SAL_WARN_IF(
    2066             :                 !xProps.is(), "basic",
    2067             :                 "The stream must implement XPropertySet!");
    2068           0 :             if ( !xProps.is() )
    2069             :             {
    2070           0 :                 throw uno::RuntimeException();
    2071             :             }
    2072           0 :             OUString aMime( "text/xml" );
    2073           0 :             xProps->setPropertyValue("MediaType", uno::makeAny( aMime ) );
    2074             : 
    2075             :             // #87671 Allow encryption
    2076           0 :             xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::makeAny( sal_True ) );
    2077             : 
    2078           0 :             xOut = xInfoStream->getOutputStream();
    2079             :         }
    2080           0 :         catch(const uno::Exception& )
    2081             :         {
    2082           0 :             sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    2083           0 :             ErrorHandler::HandleError( nErrorCode );
    2084           0 :         }
    2085             :     }
    2086             :     else
    2087             :     {
    2088             :         // Create Output stream
    2089           0 :         INetURLObject aLibInfoInetObj( maLibraryPath.getToken(1, (sal_Unicode)';') );
    2090           0 :         aLibInfoInetObj.insertName( maInfoFileName, sal_False, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    2091           0 :         aLibInfoInetObj.setExtension( OUString("xlc") );
    2092           0 :         OUString aLibInfoPath( aLibInfoInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
    2093             : 
    2094             :         try
    2095             :         {
    2096           0 :             if( mxSFI->exists( aLibInfoPath ) )
    2097             :             {
    2098           0 :                 mxSFI->kill( aLibInfoPath );
    2099             :             }
    2100           0 :             xOut = mxSFI->openFileWrite( aLibInfoPath );
    2101             :         }
    2102           0 :         catch(const Exception& )
    2103             :         {
    2104           0 :             xOut.clear();
    2105           0 :             SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aLibInfoPath );
    2106           0 :             sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    2107           0 :             ErrorHandler::HandleError( nErrorCode );
    2108           0 :         }
    2109             : 
    2110             :     }
    2111           0 :     if( !xOut.is() )
    2112             :     {
    2113             :         SAL_WARN("basic", "couldn't open output stream");
    2114           0 :         return;
    2115             :     }
    2116             : 
    2117           0 :     xWriter->setOutputStream( xOut );
    2118             : 
    2119             :     try
    2120             :     {
    2121           0 :         xmlscript::exportLibraryContainer( xWriter, pLibArray.get() );
    2122           0 :         if ( bStorage )
    2123             :         {
    2124           0 :             uno::Reference< embed::XTransactedObject > xTransact( xTargetLibrariesStor, uno::UNO_QUERY );
    2125             :             SAL_WARN_IF(
    2126             :                 !xTransact.is(), "basic",
    2127             :                 "The storage must implement XTransactedObject!");
    2128           0 :             if ( !xTransact.is() )
    2129             :             {
    2130           0 :                 throw uno::RuntimeException();
    2131             :             }
    2132           0 :             xTransact->commit();
    2133             :         }
    2134             :     }
    2135           0 :     catch(const uno::Exception& )
    2136             :     {
    2137             :         SAL_WARN("basic", "Problem during storing of libraries!");
    2138           0 :         sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
    2139           0 :         ErrorHandler::HandleError( nErrorCode );
    2140           0 :     }
    2141             : }
    2142             : 
    2143             : 
    2144             : // Methods XElementAccess
    2145           0 : Type SAL_CALL SfxLibraryContainer::getElementType()
    2146             :     throw(RuntimeException)
    2147             : {
    2148           0 :     LibraryContainerMethodGuard aGuard( *this );
    2149           0 :     return maNameContainer.getElementType();
    2150             : }
    2151             : 
    2152        1601 : sal_Bool SfxLibraryContainer::hasElements()
    2153             :     throw(RuntimeException)
    2154             : {
    2155        1601 :     LibraryContainerMethodGuard aGuard( *this );
    2156        1601 :     sal_Bool bRet = maNameContainer.hasElements();
    2157        1601 :     return bRet;
    2158             : }
    2159             : 
    2160             : // Methods XNameAccess
    2161        1473 : Any SfxLibraryContainer::getByName( const OUString& aName )
    2162             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
    2163             : {
    2164        1473 :     LibraryContainerMethodGuard aGuard( *this );
    2165        1473 :     Any aRetAny = maNameContainer.getByName( aName ) ;
    2166        1473 :     return aRetAny;
    2167             : }
    2168             : 
    2169        1231 : Sequence< OUString > SfxLibraryContainer::getElementNames()
    2170             :     throw(RuntimeException)
    2171             : {
    2172        1231 :     LibraryContainerMethodGuard aGuard( *this );
    2173        1231 :     return maNameContainer.getElementNames();
    2174             : }
    2175             : 
    2176        5469 : sal_Bool SfxLibraryContainer::hasByName( const OUString& aName )
    2177             :     throw(RuntimeException)
    2178             : {
    2179        5469 :     LibraryContainerMethodGuard aGuard( *this );
    2180        5469 :     return maNameContainer.hasByName( aName ) ;
    2181             : }
    2182             : 
    2183             : // Methods XLibraryContainer
    2184         923 : Reference< XNameContainer > SAL_CALL SfxLibraryContainer::createLibrary( const OUString& Name )
    2185             :     throw(IllegalArgumentException, ElementExistException, RuntimeException)
    2186             : {
    2187         923 :     LibraryContainerMethodGuard aGuard( *this );
    2188         923 :     SfxLibrary* pNewLib = implCreateLibrary( Name );
    2189         923 :     pNewLib->maLibElementFileExtension = maLibElementFileExtension;
    2190             : 
    2191         923 :     createVariableURL( pNewLib->maUnexpandedStorageURL, Name, maInfoFileName, true );
    2192             : 
    2193        1846 :     Reference< XNameAccess > xNameAccess = static_cast< XNameAccess* >( pNewLib );
    2194        1846 :     Any aElement;
    2195         923 :     aElement <<= xNameAccess;
    2196         923 :     maNameContainer.insertByName( Name, aElement );
    2197         923 :     maModifiable.setModified( sal_True );
    2198         923 :     Reference< XNameContainer > xRet( xNameAccess, UNO_QUERY );
    2199        1846 :     return xRet;
    2200             : }
    2201             : 
    2202         512 : Reference< XNameAccess > SAL_CALL SfxLibraryContainer::createLibraryLink
    2203             :     ( const OUString& Name, const OUString& StorageURL, sal_Bool ReadOnly )
    2204             :     throw(IllegalArgumentException, ElementExistException, RuntimeException)
    2205             : {
    2206         512 :     LibraryContainerMethodGuard aGuard( *this );
    2207             :     // TODO: Check other reasons to force ReadOnly status
    2208             :     //if( !ReadOnly )
    2209             :     //{
    2210             :     //}
    2211             : 
    2212        1024 :     OUString aLibInfoFileURL;
    2213        1024 :     OUString aLibDirURL;
    2214        1024 :     OUString aUnexpandedStorageURL;
    2215         512 :     checkStorageURL( StorageURL, aLibInfoFileURL, aLibDirURL, aUnexpandedStorageURL );
    2216             : 
    2217             : 
    2218         512 :     SfxLibrary* pNewLib = implCreateLibraryLink( Name, aLibInfoFileURL, aLibDirURL, ReadOnly );
    2219         512 :     pNewLib->maLibElementFileExtension = maLibElementFileExtension;
    2220         512 :     pNewLib->maUnexpandedStorageURL = aUnexpandedStorageURL;
    2221         512 :     pNewLib->maOriginalStorageURL = StorageURL;
    2222             : 
    2223        1024 :     OUString aInitFileName;
    2224        1024 :     uno::Reference< embed::XStorage > xDummyStor;
    2225        1024 :     ::xmlscript::LibDescriptor aLibDesc;
    2226         512 :     implLoadLibraryIndexFile( pNewLib, aLibDesc, xDummyStor, aInitFileName );
    2227         512 :     implImportLibDescriptor( pNewLib, aLibDesc );
    2228             : 
    2229         512 :     Reference< XNameAccess > xRet = static_cast< XNameAccess* >( pNewLib );
    2230        1024 :     Any aElement;
    2231         512 :     aElement <<= xRet;
    2232         512 :     maNameContainer.insertByName( Name, aElement );
    2233         512 :     maModifiable.setModified( sal_True );
    2234             : 
    2235        1024 :     OUString aUserSearchStr("vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE");
    2236        1024 :     OUString aSharedSearchStr("vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE");
    2237        1024 :     OUString aBundledSearchStr("vnd.sun.star.expand:$BUNDLED_EXTENSIONS");
    2238         512 :     if( StorageURL.indexOf( aUserSearchStr ) != -1 )
    2239             :     {
    2240           0 :         pNewLib->mbExtension = true;
    2241             :     }
    2242         512 :     else if( StorageURL.indexOf( aSharedSearchStr ) != -1 || StorageURL.indexOf( aBundledSearchStr ) != -1 )
    2243             :     {
    2244           0 :         pNewLib->mbExtension = true;
    2245           0 :         pNewLib->mbReadOnly = sal_True;
    2246             :     }
    2247             : 
    2248        1024 :     return xRet;
    2249             : }
    2250             : 
    2251           0 : void SAL_CALL SfxLibraryContainer::removeLibrary( const OUString& Name )
    2252             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
    2253             : {
    2254           0 :     LibraryContainerMethodGuard aGuard( *this );
    2255             :     // Get and hold library before removing
    2256           0 :     Any aLibAny = maNameContainer.getByName( Name ) ;
    2257           0 :     Reference< XNameAccess > xNameAccess;
    2258           0 :     aLibAny >>= xNameAccess;
    2259           0 :     SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
    2260           0 :     if( pImplLib->mbReadOnly && !pImplLib->mbLink )
    2261             :     {
    2262           0 :         throw IllegalArgumentException();
    2263             :     }
    2264             :     // Remove from container
    2265           0 :     maNameContainer.removeByName( Name );
    2266           0 :     maModifiable.setModified( sal_True );
    2267             : 
    2268             :     // Delete library files, but not for linked libraries
    2269           0 :     if( !pImplLib->mbLink )
    2270             :     {
    2271           0 :         if( mxStorage.is() )
    2272             :         {
    2273           0 :             return;
    2274             :         }
    2275           0 :         if( xNameAccess->hasElements() )
    2276             :         {
    2277           0 :             Sequence< OUString > aNames = pImplLib->getElementNames();
    2278           0 :             sal_Int32 nNameCount = aNames.getLength();
    2279           0 :             const OUString* pNames = aNames.getConstArray();
    2280           0 :             for( sal_Int32 i = 0 ; i < nNameCount ; ++i, ++pNames )
    2281             :             {
    2282           0 :                 pImplLib->removeElementWithoutChecks( *pNames, SfxLibrary::LibraryContainerAccess() );
    2283           0 :             }
    2284             :         }
    2285             : 
    2286             :         // Delete index file
    2287           0 :         createAppLibraryFolder( pImplLib, Name );
    2288           0 :         OUString aLibInfoPath = pImplLib->maLibInfoFileURL;
    2289             :         try
    2290             :         {
    2291           0 :             if( mxSFI->exists( aLibInfoPath ) )
    2292             :             {
    2293           0 :                 mxSFI->kill( aLibInfoPath );
    2294             :             }
    2295             :         }
    2296           0 :         catch(const Exception& ) {}
    2297             : 
    2298             :         // Delete folder if empty
    2299           0 :         INetURLObject aInetObj( maLibraryPath.getToken(1, (sal_Unicode)';') );
    2300             :         aInetObj.insertName( Name, sal_True, INetURLObject::LAST_SEGMENT,
    2301           0 :                              sal_True, INetURLObject::ENCODE_ALL );
    2302           0 :         OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
    2303             : 
    2304             :         try
    2305             :         {
    2306           0 :             if( mxSFI->isFolder( aLibDirPath ) )
    2307             :             {
    2308           0 :                 Sequence< OUString > aContentSeq = mxSFI->getFolderContents( aLibDirPath, true );
    2309           0 :                 sal_Int32 nCount = aContentSeq.getLength();
    2310           0 :                 if( !nCount )
    2311             :                 {
    2312           0 :                     mxSFI->kill( aLibDirPath );
    2313           0 :                 }
    2314             :             }
    2315             :         }
    2316           0 :         catch(const Exception& )
    2317             :         {
    2318           0 :         }
    2319           0 :     }
    2320             : }
    2321             : 
    2322        4445 : sal_Bool SAL_CALL SfxLibraryContainer::isLibraryLoaded( const OUString& Name )
    2323             :     throw(NoSuchElementException, RuntimeException)
    2324             : {
    2325        4445 :     LibraryContainerMethodGuard aGuard( *this );
    2326        4445 :     SfxLibrary* pImplLib = getImplLib( Name );
    2327        4445 :     sal_Bool bRet = pImplLib->mbLoaded;
    2328        4445 :     return bRet;
    2329             : }
    2330             : 
    2331             : 
    2332         354 : void SAL_CALL SfxLibraryContainer::loadLibrary( const OUString& Name )
    2333             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
    2334             : {
    2335         354 :     LibraryContainerMethodGuard aGuard( *this );
    2336         708 :     Any aLibAny = maNameContainer.getByName( Name ) ;
    2337         708 :     Reference< XNameAccess > xNameAccess;
    2338         354 :     aLibAny >>= xNameAccess;
    2339         354 :     SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
    2340             : 
    2341         354 :     sal_Bool bLoaded = pImplLib->mbLoaded;
    2342         354 :     pImplLib->mbLoaded = sal_True;
    2343         354 :     if( !bLoaded && xNameAccess->hasElements() )
    2344             :     {
    2345          33 :         if( pImplLib->mbPasswordProtected )
    2346             :         {
    2347           0 :             implLoadPasswordLibrary( pImplLib, Name );
    2348           0 :             return;
    2349             :         }
    2350             : 
    2351          33 :         sal_Bool bLink = pImplLib->mbLink;
    2352          33 :         bool bStorage = mxStorage.is() && !bLink;
    2353             : 
    2354          33 :         uno::Reference< embed::XStorage > xLibrariesStor;
    2355          66 :         uno::Reference< embed::XStorage > xLibraryStor;
    2356          33 :         if( bStorage )
    2357             :         {
    2358             :             try
    2359             :             {
    2360           2 :                 xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
    2361             :                 SAL_WARN_IF(
    2362             :                     !xLibrariesStor.is(), "basic",
    2363             :                     ("The method must either throw exception or return a"
    2364             :                      " storage!"));
    2365           2 :                 if ( !xLibrariesStor.is() )
    2366             :                 {
    2367           0 :                     throw uno::RuntimeException();
    2368             :                 }
    2369             : 
    2370           2 :                 xLibraryStor = xLibrariesStor->openStorageElement( Name, embed::ElementModes::READ );
    2371             :                 SAL_WARN_IF(
    2372             :                     !xLibraryStor.is(), "basic",
    2373             :                     ("The method must either throw exception or return a"
    2374             :                      " storage!"));
    2375           2 :                 if ( !xLibrariesStor.is() )
    2376             :                 {
    2377           0 :                     throw uno::RuntimeException();
    2378             :                 }
    2379             :             }
    2380           0 :             catch(const uno::Exception& )
    2381             :             {
    2382             :             #if OSL_DEBUG_LEVEL > 0
    2383             :                 Any aError( ::cppu::getCaughtException() );
    2384             :                 SAL_WARN(
    2385             :                     "basic",
    2386             :                     "couldn't open sub storage for library \"" << Name
    2387             :                         << "\". Exception: "
    2388             :                         << comphelper::anyToString(aError));
    2389             :             #endif
    2390           0 :                 return;
    2391             :             }
    2392             :         }
    2393             : 
    2394          66 :         Sequence< OUString > aNames = pImplLib->getElementNames();
    2395          33 :         sal_Int32 nNameCount = aNames.getLength();
    2396          33 :         const OUString* pNames = aNames.getConstArray();
    2397          70 :         for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
    2398             :         {
    2399          37 :             OUString aElementName = pNames[ i ];
    2400             : 
    2401          74 :             OUString aFile;
    2402          74 :             uno::Reference< io::XInputStream > xInStream;
    2403             : 
    2404          37 :             if( bStorage )
    2405             :             {
    2406           6 :                 uno::Reference< io::XStream > xElementStream;
    2407             : 
    2408           6 :                 aFile = aElementName;
    2409           6 :                 aFile += ".xml";
    2410             : 
    2411             :                 try
    2412             :                 {
    2413           6 :                     xElementStream = xLibraryStor->openStreamElement( aFile, embed::ElementModes::READ );
    2414             :                 }
    2415           0 :                 catch(const uno::Exception& )
    2416             :                 {}
    2417             : 
    2418           6 :                 if( !xElementStream.is() )
    2419             :                 {
    2420             :                     // Check for EA2 document version with wrong extensions
    2421           0 :                     aFile = aElementName;
    2422           0 :                     aFile += ".";
    2423           0 :                     aFile += maLibElementFileExtension;
    2424             :                     try
    2425             :                     {
    2426           0 :                         xElementStream = xLibraryStor->openStreamElement( aFile, embed::ElementModes::READ );
    2427             :                     }
    2428           0 :                     catch(const uno::Exception& )
    2429             :                     {}
    2430             :                 }
    2431             : 
    2432           6 :                 if ( xElementStream.is() )
    2433             :                 {
    2434           6 :                     xInStream = xElementStream->getInputStream();
    2435             :                 }
    2436           6 :                 if ( !xInStream.is() )
    2437             :                 {
    2438             :                     SAL_WARN(
    2439             :                         "basic",
    2440             :                         "couldn't open library element stream - attempted to"
    2441             :                             " open library \"" << Name << '"');
    2442           0 :                     return;
    2443           6 :                 }
    2444             :             }
    2445             :             else
    2446             :             {
    2447          31 :                 OUString aLibDirPath = pImplLib->maStorageURL;
    2448          62 :                 INetURLObject aElementInetObj( aLibDirPath );
    2449             :                 aElementInetObj.insertName( aElementName, sal_False,
    2450             :                                             INetURLObject::LAST_SEGMENT, sal_True,
    2451          31 :                                             INetURLObject::ENCODE_ALL );
    2452          31 :                 aElementInetObj.setExtension( maLibElementFileExtension );
    2453          62 :                 aFile = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
    2454             :             }
    2455             : 
    2456          74 :             Reference< XNameContainer > xLib( pImplLib );
    2457             :             Any aAny = importLibraryElement( xLib, aElementName,
    2458          74 :                                              aFile, xInStream );
    2459          37 :             if( pImplLib->hasByName( aElementName ) )
    2460             :             {
    2461          37 :                 if( aAny.hasValue() )
    2462             :                 {
    2463          37 :                     pImplLib->maNameContainer.replaceByName( aElementName, aAny );
    2464             :                 }
    2465             :             }
    2466             :             else
    2467             :             {
    2468           0 :                 pImplLib->maNameContainer.insertByName( aElementName, aAny );
    2469             :             }
    2470          37 :         }
    2471          66 :         pImplLib->implSetModified( sal_False );
    2472         354 :     }
    2473             : }
    2474             : 
    2475             : // Methods XLibraryContainer2
    2476           0 : sal_Bool SAL_CALL SfxLibraryContainer::isLibraryLink( const OUString& Name )
    2477             :     throw (NoSuchElementException, RuntimeException)
    2478             : {
    2479           0 :     LibraryContainerMethodGuard aGuard( *this );
    2480           0 :     SfxLibrary* pImplLib = getImplLib( Name );
    2481           0 :     sal_Bool bRet = pImplLib->mbLink;
    2482           0 :     return bRet;
    2483             : }
    2484             : 
    2485           0 : OUString SAL_CALL SfxLibraryContainer::getLibraryLinkURL( const OUString& Name )
    2486             :     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
    2487             : {
    2488           0 :     LibraryContainerMethodGuard aGuard( *this );
    2489           0 :     SfxLibrary* pImplLib = getImplLib( Name );
    2490           0 :     sal_Bool bLink = pImplLib->mbLink;
    2491           0 :     if( !bLink )
    2492             :     {
    2493           0 :         throw IllegalArgumentException();
    2494             :     }
    2495           0 :     OUString aRetStr = pImplLib->maLibInfoFileURL;
    2496           0 :     return aRetStr;
    2497             : }
    2498             : 
    2499           0 : sal_Bool SAL_CALL SfxLibraryContainer::isLibraryReadOnly( const OUString& Name )
    2500             :     throw (NoSuchElementException, RuntimeException)
    2501             : {
    2502           0 :     LibraryContainerMethodGuard aGuard( *this );
    2503           0 :     SfxLibrary* pImplLib = getImplLib( Name );
    2504           0 :     sal_Bool bRet = pImplLib->mbReadOnly || (pImplLib->mbLink && pImplLib->mbReadOnlyLink);
    2505           0 :     return bRet;
    2506             : }
    2507             : 
    2508           0 : void SAL_CALL SfxLibraryContainer::setLibraryReadOnly( const OUString& Name, sal_Bool bReadOnly )
    2509             :     throw (NoSuchElementException, RuntimeException)
    2510             : {
    2511           0 :     LibraryContainerMethodGuard aGuard( *this );
    2512           0 :     SfxLibrary* pImplLib = getImplLib( Name );
    2513           0 :     if( pImplLib->mbLink )
    2514             :     {
    2515           0 :         if( pImplLib->mbReadOnlyLink != bReadOnly )
    2516             :         {
    2517           0 :             pImplLib->mbReadOnlyLink = bReadOnly;
    2518           0 :             pImplLib->implSetModified( sal_True );
    2519           0 :             maModifiable.setModified( sal_True );
    2520             :         }
    2521             :     }
    2522             :     else
    2523             :     {
    2524           0 :         if( pImplLib->mbReadOnly != bReadOnly )
    2525             :         {
    2526           0 :             pImplLib->mbReadOnly = bReadOnly;
    2527           0 :             pImplLib->implSetModified( sal_True );
    2528             :         }
    2529           0 :     }
    2530           0 : }
    2531             : 
    2532           0 : void SAL_CALL SfxLibraryContainer::renameLibrary( const OUString& Name, const OUString& NewName )
    2533             :     throw (NoSuchElementException, ElementExistException, RuntimeException)
    2534             : {
    2535           0 :     LibraryContainerMethodGuard aGuard( *this );
    2536           0 :     if( maNameContainer.hasByName( NewName ) )
    2537             :     {
    2538           0 :         throw ElementExistException();
    2539             :     }
    2540             :     // Get and hold library before removing
    2541           0 :     Any aLibAny = maNameContainer.getByName( Name ) ;
    2542             : 
    2543             :     // #i24094 Maybe lib is not loaded!
    2544           0 :     Reference< XNameAccess > xNameAccess;
    2545           0 :     aLibAny >>= xNameAccess;
    2546           0 :     SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
    2547           0 :     if( pImplLib->mbPasswordProtected && !pImplLib->mbPasswordVerified )
    2548             :     {
    2549           0 :         return;     // Lib with unverified password cannot be renamed
    2550             :     }
    2551           0 :     loadLibrary( Name );
    2552             : 
    2553             :     // Remove from container
    2554           0 :     maNameContainer.removeByName( Name );
    2555           0 :     maModifiable.setModified( sal_True );
    2556             : 
    2557             :     // Rename library folder, but not for linked libraries
    2558           0 :     bool bMovedSuccessful = true;
    2559             : 
    2560             :     // Rename files
    2561           0 :     bool bStorage = mxStorage.is();
    2562           0 :     if( !bStorage && !pImplLib->mbLink )
    2563             :     {
    2564           0 :         bMovedSuccessful = false;
    2565             : 
    2566           0 :         OUString aLibDirPath = pImplLib->maStorageURL;
    2567             : 
    2568           0 :         INetURLObject aDestInetObj( maLibraryPath.getToken(1, (sal_Unicode)';'));
    2569             :         aDestInetObj.insertName( NewName, sal_True, INetURLObject::LAST_SEGMENT,
    2570           0 :                                  sal_True, INetURLObject::ENCODE_ALL );
    2571           0 :         OUString aDestDirPath = aDestInetObj.GetMainURL( INetURLObject::NO_DECODE );
    2572             : 
    2573             :         // Store new URL
    2574           0 :         OUString aLibInfoFileURL = pImplLib->maLibInfoFileURL;
    2575             :         checkStorageURL( aDestDirPath, pImplLib->maLibInfoFileURL, pImplLib->maStorageURL,
    2576           0 :                          pImplLib->maUnexpandedStorageURL );
    2577             : 
    2578             :         try
    2579             :         {
    2580           0 :             if( mxSFI->isFolder( aLibDirPath ) )
    2581             :             {
    2582           0 :                 if( !mxSFI->isFolder( aDestDirPath ) )
    2583             :                 {
    2584           0 :                     mxSFI->createFolder( aDestDirPath );
    2585             :                 }
    2586             :                 // Move index file
    2587             :                 try
    2588             :                 {
    2589           0 :                     if( mxSFI->exists( pImplLib->maLibInfoFileURL ) )
    2590             :                     {
    2591           0 :                         mxSFI->kill( pImplLib->maLibInfoFileURL );
    2592             :                     }
    2593           0 :                     mxSFI->move( aLibInfoFileURL, pImplLib->maLibInfoFileURL );
    2594             :                 }
    2595           0 :                 catch(const Exception& )
    2596             :                 {
    2597             :                 }
    2598             : 
    2599           0 :                 Sequence< OUString > aElementNames = xNameAccess->getElementNames();
    2600           0 :                 sal_Int32 nNameCount = aElementNames.getLength();
    2601           0 :                 const OUString* pNames = aElementNames.getConstArray();
    2602           0 :                 for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
    2603             :                 {
    2604           0 :                     OUString aElementName = pNames[ i ];
    2605             : 
    2606           0 :                     INetURLObject aElementInetObj( aLibDirPath );
    2607             :                     aElementInetObj.insertName( aElementName, sal_False,
    2608           0 :                         INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
    2609           0 :                     aElementInetObj.setExtension( maLibElementFileExtension );
    2610           0 :                     OUString aElementPath( aElementInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
    2611             : 
    2612           0 :                     INetURLObject aElementDestInetObj( aDestDirPath );
    2613             :                     aElementDestInetObj.insertName( aElementName, sal_False,
    2614             :                                                     INetURLObject::LAST_SEGMENT, sal_True,
    2615           0 :                                                     INetURLObject::ENCODE_ALL );
    2616           0 :                     aElementDestInetObj.setExtension( maLibElementFileExtension );
    2617           0 :                     OUString aDestElementPath( aElementDestInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
    2618             : 
    2619             :                     try
    2620             :                     {
    2621           0 :                         if( mxSFI->exists( aDestElementPath ) )
    2622             :                         {
    2623           0 :                             mxSFI->kill( aDestElementPath );
    2624             :                         }
    2625           0 :                         mxSFI->move( aElementPath, aDestElementPath );
    2626             :                     }
    2627           0 :                     catch(const Exception& )
    2628             :                     {
    2629             :                     }
    2630           0 :                 }
    2631           0 :                 pImplLib->storeResourcesAsURL( aDestDirPath, NewName );
    2632             : 
    2633             :                 // Delete folder if empty
    2634           0 :                 Sequence< OUString > aContentSeq = mxSFI->getFolderContents( aLibDirPath, true );
    2635           0 :                 sal_Int32 nCount = aContentSeq.getLength();
    2636           0 :                 if( !nCount )
    2637             :                 {
    2638           0 :                     mxSFI->kill( aLibDirPath );
    2639             :                 }
    2640             : 
    2641           0 :                 bMovedSuccessful = true;
    2642           0 :                 pImplLib->implSetModified( sal_True );
    2643             :             }
    2644             :         }
    2645           0 :         catch(const Exception& )
    2646             :         {
    2647             :             // Restore old library
    2648           0 :             maNameContainer.insertByName( Name, aLibAny ) ;
    2649           0 :         }
    2650             :     }
    2651             : 
    2652           0 :     if( bStorage && !pImplLib->mbLink )
    2653             :     {
    2654           0 :         pImplLib->implSetModified( sal_True );
    2655             :     }
    2656           0 :     if( bMovedSuccessful )
    2657             :     {
    2658           0 :            maNameContainer.insertByName( NewName, aLibAny ) ;
    2659           0 :     }
    2660             : }
    2661             : 
    2662             : 
    2663             : // Methods XInitialization
    2664        1746 : void SAL_CALL SfxLibraryContainer::initialize( const Sequence< Any >& _rArguments )
    2665             :     throw (Exception, RuntimeException)
    2666             : {
    2667        1746 :     LibraryContainerMethodGuard aGuard( *this );
    2668        1746 :     sal_Int32 nArgCount = _rArguments.getLength();
    2669        1746 :     if ( nArgCount == 1 )
    2670             :     {
    2671        1746 :         OUString sInitialDocumentURL;
    2672        1794 :         Reference< XStorageBasedDocument > xDocument;
    2673        1746 :         if ( _rArguments[0] >>= sInitialDocumentURL )
    2674             :         {
    2675           0 :             initializeFromDocumentURL( sInitialDocumentURL );
    2676           0 :             return;
    2677             :         }
    2678             : 
    2679        1746 :         if ( _rArguments[0] >>= xDocument )
    2680             :         {
    2681        1746 :             initializeFromDocument( xDocument );
    2682        1698 :             return;
    2683          48 :         }
    2684             :     }
    2685             : 
    2686        1698 :     throw IllegalArgumentException();
    2687             : }
    2688             : 
    2689           0 : void SAL_CALL SfxLibraryContainer::initializeFromDocumentURL( const OUString& _rInitialDocumentURL )
    2690             : {
    2691           0 :     init( _rInitialDocumentURL, NULL );
    2692           0 : }
    2693             : 
    2694        1746 : void SAL_CALL SfxLibraryContainer::initializeFromDocument( const Reference< XStorageBasedDocument >& _rxDocument )
    2695             : {
    2696             :     // check whether this is a valid OfficeDocument, and obtain the document's root storage
    2697        1746 :     Reference< XStorage > xDocStorage;
    2698             :     try
    2699             :     {
    2700        1746 :         Reference< XServiceInfo > xSI( _rxDocument, UNO_QUERY_THROW );
    2701        1746 :         if ( xSI->supportsService("com.sun.star.document.OfficeDocument"))
    2702             :         {
    2703        1794 :             xDocStorage.set( _rxDocument->getDocumentStorage(), UNO_QUERY_THROW );
    2704             :         }
    2705        3396 :         Reference< XModel > xDocument( _rxDocument, UNO_QUERY_THROW );
    2706        3396 :         Reference< XComponent > xDocComponent( _rxDocument, UNO_QUERY_THROW );
    2707             : 
    2708        1698 :         mxOwnerDocument = xDocument;
    2709        3444 :         startComponentListening( xDocComponent );
    2710             :     }
    2711          48 :     catch( const Exception& ) { }
    2712             : 
    2713        1746 :     if ( !xDocStorage.is() )
    2714             :     {
    2715          48 :         throw IllegalArgumentException();
    2716             :     }
    2717        1698 :     init( OUString(), xDocStorage );
    2718        1698 : }
    2719             : 
    2720             : // OEventListenerAdapter
    2721        1690 : void SfxLibraryContainer::_disposing( const EventObject& _rSource )
    2722             : {
    2723             : #if OSL_DEBUG_LEVEL > 0
    2724             :     Reference< XModel > xDocument( mxOwnerDocument.get(), UNO_QUERY );
    2725             :     SAL_WARN_IF(
    2726             :         xDocument != _rSource.Source || !xDocument.is(), "basic",
    2727             :         "SfxLibraryContainer::_disposing: where does this come from?");
    2728             : #else
    2729             :     (void)_rSource;
    2730             : #endif
    2731        1690 :     dispose();
    2732        1690 : }
    2733             : 
    2734             : // OComponentHelper
    2735        1800 : void SAL_CALL SfxLibraryContainer::disposing()
    2736             : {
    2737        1800 :     Reference< XModel > xModel = mxOwnerDocument;
    2738        3600 :     EventObject aEvent( xModel.get() );
    2739        1800 :     maVBAScriptListeners.disposing( aEvent );
    2740        1800 :     stopAllComponentListening();
    2741        3600 :     mxOwnerDocument = WeakReference< XModel >();
    2742        1800 : }
    2743             : 
    2744             : // Methods XLibraryContainerPassword
    2745           0 : sal_Bool SAL_CALL SfxLibraryContainer::isLibraryPasswordProtected( const OUString& )
    2746             :     throw (NoSuchElementException, RuntimeException)
    2747             : {
    2748           0 :     LibraryContainerMethodGuard aGuard( *this );
    2749           0 :     return sal_False;
    2750             : }
    2751             : 
    2752           0 : sal_Bool SAL_CALL SfxLibraryContainer::isLibraryPasswordVerified( const OUString& )
    2753             :     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
    2754             : {
    2755           0 :     LibraryContainerMethodGuard aGuard( *this );
    2756           0 :     throw IllegalArgumentException();
    2757             : }
    2758             : 
    2759           0 : sal_Bool SAL_CALL SfxLibraryContainer::verifyLibraryPassword( const OUString&, const OUString& )
    2760             :     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
    2761             : {
    2762           0 :     LibraryContainerMethodGuard aGuard( *this );
    2763           0 :     throw IllegalArgumentException();
    2764             : }
    2765             : 
    2766           0 : void SAL_CALL SfxLibraryContainer::changeLibraryPassword(const OUString&, const OUString&, const OUString& )
    2767             :     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
    2768             : {
    2769           0 :     LibraryContainerMethodGuard aGuard( *this );
    2770           0 :     throw IllegalArgumentException();
    2771             : }
    2772             : 
    2773             : // Methods XContainer
    2774         463 : void SAL_CALL SfxLibraryContainer::addContainerListener( const Reference< XContainerListener >& xListener )
    2775             :     throw (RuntimeException)
    2776             : {
    2777         463 :     LibraryContainerMethodGuard aGuard( *this );
    2778         463 :     maNameContainer.setEventSource( static_cast< XInterface* >( (OWeakObject*)this ) );
    2779         463 :     maNameContainer.addContainerListener( xListener );
    2780         463 : }
    2781             : 
    2782           0 : void SAL_CALL SfxLibraryContainer::removeContainerListener( const Reference< XContainerListener >& xListener )
    2783             :     throw (RuntimeException)
    2784             : {
    2785           0 :     LibraryContainerMethodGuard aGuard( *this );
    2786           0 :     maNameContainer.removeContainerListener( xListener );
    2787           0 : }
    2788             : 
    2789             : // Methods XLibraryContainerExport
    2790           0 : void SAL_CALL SfxLibraryContainer::exportLibrary( const OUString& Name, const OUString& URL,
    2791             :     const Reference< XInteractionHandler >& Handler )
    2792             :     throw ( uno::Exception, NoSuchElementException, RuntimeException)
    2793             : {
    2794           0 :     LibraryContainerMethodGuard aGuard( *this );
    2795           0 :     SfxLibrary* pImplLib = getImplLib( Name );
    2796             : 
    2797           0 :     Reference< XSimpleFileAccess3 > xToUseSFI;
    2798           0 :     if( Handler.is() )
    2799             :     {
    2800           0 :         xToUseSFI = ucb::SimpleFileAccess::create( mxContext );
    2801           0 :         xToUseSFI->setInteractionHandler( Handler );
    2802             :     }
    2803             : 
    2804             :     // Maybe lib is not loaded?!
    2805           0 :     loadLibrary( Name );
    2806             : 
    2807           0 :     uno::Reference< ::com::sun::star::embed::XStorage > xDummyStor;
    2808           0 :     if( pImplLib->mbPasswordProtected )
    2809             :     {
    2810           0 :         implStorePasswordLibrary( pImplLib, Name, xDummyStor, URL, xToUseSFI, Handler );
    2811             :     }
    2812             :     else
    2813             :     {
    2814           0 :         implStoreLibrary( pImplLib, Name, xDummyStor, URL, xToUseSFI, Handler );
    2815             :     }
    2816           0 :     ::xmlscript::LibDescriptor aLibDesc;
    2817           0 :     aLibDesc.aName = Name;
    2818           0 :     aLibDesc.bLink = false;             // Link status gets lost?
    2819           0 :     aLibDesc.bReadOnly = pImplLib->mbReadOnly;
    2820           0 :     aLibDesc.bPreload = false;          // Preload status gets lost?
    2821           0 :     aLibDesc.bPasswordProtected = pImplLib->mbPasswordProtected;
    2822           0 :     aLibDesc.aElementNames = pImplLib->getElementNames();
    2823             : 
    2824           0 :     implStoreLibraryIndexFile( pImplLib, aLibDesc, xDummyStor, URL, xToUseSFI );
    2825           0 : }
    2826             : 
    2827         574 : OUString SfxLibraryContainer::expand_url( const OUString& url )
    2828             :     throw(::com::sun::star::uno::RuntimeException)
    2829             : {
    2830         574 :     if (url.startsWith( EXPAND_PROTOCOL ":" ))
    2831             :     {
    2832           0 :         if( !mxMacroExpander.is() )
    2833             :         {
    2834           0 :             Reference< util::XMacroExpander > xExpander = util::theMacroExpander::get(mxContext);
    2835           0 :             MutexGuard guard( Mutex::getGlobalMutex() );
    2836           0 :             if( !mxMacroExpander.is() )
    2837             :             {
    2838           0 :                 mxMacroExpander = xExpander;
    2839           0 :             }
    2840             :         }
    2841             : 
    2842           0 :         if( !mxMacroExpander.is() )
    2843             :         {
    2844           0 :             return url;
    2845             :         }
    2846             :         // cut protocol
    2847           0 :         OUString macro( url.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
    2848             :         // decode uric class chars
    2849           0 :         macro = Uri::decode( macro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
    2850             :         // expand macro string
    2851           0 :         OUString ret( mxMacroExpander->expandMacros( macro ) );
    2852           0 :         return ret;
    2853             :     }
    2854         574 :     else if( mxStringSubstitution.is() )
    2855             :     {
    2856         574 :         OUString ret( mxStringSubstitution->substituteVariables( url, false ) );
    2857         574 :         return ret;
    2858             :     }
    2859             :     else
    2860             :     {
    2861           0 :         return url;
    2862             :     }
    2863             : }
    2864             : 
    2865             : //XLibraryContainer3
    2866           0 : OUString SAL_CALL SfxLibraryContainer::getOriginalLibraryLinkURL( const OUString& Name )
    2867             :     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
    2868             : {
    2869           0 :     LibraryContainerMethodGuard aGuard( *this );
    2870           0 :     SfxLibrary* pImplLib = getImplLib( Name );
    2871           0 :     sal_Bool bLink = pImplLib->mbLink;
    2872           0 :     if( !bLink )
    2873             :     {
    2874           0 :         throw IllegalArgumentException();
    2875             :     }
    2876           0 :     OUString aRetStr = pImplLib->maOriginalStorageURL;
    2877           0 :     return aRetStr;
    2878             : }
    2879             : 
    2880             : 
    2881             : // XVBACompatibility
    2882         584 : sal_Bool SAL_CALL SfxLibraryContainer::getVBACompatibilityMode() throw (RuntimeException)
    2883             : {
    2884         584 :     return mbVBACompat;
    2885             : }
    2886             : 
    2887          29 : void SAL_CALL SfxLibraryContainer::setVBACompatibilityMode( ::sal_Bool _vbacompatmodeon ) throw (RuntimeException)
    2888             : {
    2889             :     /*  The member variable mbVBACompat must be set first, the following call
    2890             :         to getBasicManager() may call getVBACompatibilityMode() which returns
    2891             :         this value. */
    2892          29 :     mbVBACompat = _vbacompatmodeon;
    2893          29 :     if( BasicManager* pBasMgr = getBasicManager() )
    2894             :     {
    2895             :         // get the standard library
    2896          29 :         OUString aLibName = pBasMgr->GetName();
    2897          29 :         if ( aLibName.isEmpty())
    2898             :         {
    2899          28 :             aLibName = "Standard";
    2900             :         }
    2901          29 :         if( StarBASIC* pBasic = pBasMgr->GetLib( aLibName ) )
    2902             :         {
    2903          29 :             pBasic->SetVBAEnabled( _vbacompatmodeon );
    2904             :         }
    2905             :         /*  If in VBA compatibility mode, force creation of the VBA Globals
    2906             :             object. Each application will create an instance of its own
    2907             :             implementation and store it in its Basic manager. Implementations
    2908             :             will do all necessary additional initialization, such as
    2909             :             registering the global "This***Doc" UNO constant, starting the
    2910             :             document events processor etc.
    2911             :          */
    2912          29 :         if( mbVBACompat ) try
    2913             :         {
    2914          29 :             Reference< XModel > xModel( mxOwnerDocument );   // weak-ref -> ref
    2915          58 :             Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
    2916          61 :             xFactory->createInstance("ooo.vba.VBAGlobals");
    2917             :         }
    2918           3 :         catch(const Exception& )
    2919             :         {
    2920          29 :         }
    2921             :     }
    2922          29 : }
    2923             : 
    2924          28 : void SAL_CALL SfxLibraryContainer::setProjectName( const OUString& _projectname ) throw (RuntimeException)
    2925             : {
    2926          28 :     msProjectName = _projectname;
    2927          28 :     BasicManager* pBasMgr = getBasicManager();
    2928             :     // Temporary HACK
    2929             :     // Some parts of the VBA handling ( e.g. in core basic )
    2930             :     // code expect the name of the VBA project to be set as the name of
    2931             :     // the basic manager. Provide fail back here.
    2932          28 :     if( pBasMgr )
    2933             :     {
    2934          28 :         pBasMgr->SetName( msProjectName );
    2935             :     }
    2936          28 : }
    2937             : 
    2938           0 : sal_Int32 SAL_CALL SfxLibraryContainer::getRunningVBAScripts() throw (RuntimeException)
    2939             : {
    2940           0 :     LibraryContainerMethodGuard aGuard( *this );
    2941           0 :     return mnRunningVBAScripts;
    2942             : }
    2943             : 
    2944           0 : void SAL_CALL SfxLibraryContainer::addVBAScriptListener( const Reference< vba::XVBAScriptListener >& rxListener ) throw (RuntimeException)
    2945             : {
    2946           0 :     maVBAScriptListeners.addTypedListener( rxListener );
    2947           0 : }
    2948             : 
    2949           0 : void SAL_CALL SfxLibraryContainer::removeVBAScriptListener( const Reference< vba::XVBAScriptListener >& rxListener ) throw (RuntimeException)
    2950             : {
    2951           0 :     maVBAScriptListeners.removeTypedListener( rxListener );
    2952           0 : }
    2953             : 
    2954          52 : void SAL_CALL SfxLibraryContainer::broadcastVBAScriptEvent( sal_Int32 nIdentifier, const OUString& rModuleName ) throw (RuntimeException)
    2955             : {
    2956             :     // own lock for accessing the number of running scripts
    2957          52 :     enterMethod();
    2958          52 :     switch( nIdentifier )
    2959             :     {
    2960             :     case vba::VBAScriptEventId::SCRIPT_STARTED:
    2961          25 :         ++mnRunningVBAScripts;
    2962          25 :         break;
    2963             :     case vba::VBAScriptEventId::SCRIPT_STOPPED:
    2964          25 :         --mnRunningVBAScripts;
    2965          25 :         break;
    2966             :     }
    2967          52 :     leaveMethod();
    2968             : 
    2969          52 :     Reference< XModel > xModel = mxOwnerDocument;  // weak-ref -> ref
    2970         104 :     vba::VBAScriptEvent aEvent( xModel, nIdentifier, rModuleName );
    2971         104 :     maVBAScriptListeners.notify( aEvent );
    2972          52 : }
    2973             : 
    2974             : // Methods XServiceInfo
    2975           0 : sal_Bool SAL_CALL SfxLibraryContainer::supportsService( const OUString& _rServiceName )
    2976             :     throw (RuntimeException)
    2977             : {
    2978           0 :     LibraryContainerMethodGuard aGuard( *this );
    2979           0 :     Sequence< OUString > aSupportedServices( getSupportedServiceNames() );
    2980           0 :     const OUString* pSupportedServices = aSupportedServices.getConstArray();
    2981           0 :     for ( sal_Int32 i=0; i<aSupportedServices.getLength(); ++i, ++pSupportedServices )
    2982             :     {
    2983           0 :         if ( *pSupportedServices == _rServiceName )
    2984             :         {
    2985           0 :             return sal_True;
    2986             :         }
    2987             :     }
    2988           0 :     return sal_False;
    2989             : }
    2990             : 
    2991             : //============================================================================
    2992             : 
    2993             : // Implementation class SfxLibrary
    2994             : 
    2995             : // Ctor
    2996         923 : SfxLibrary::SfxLibrary( ModifiableHelper& _rModifiable, const Type& aType,
    2997             :     const Reference< XComponentContext >& xContext, const Reference< XSimpleFileAccess3 >& xSFI )
    2998             :         : OComponentHelper( m_aMutex )
    2999             :         , mxContext( xContext )
    3000             :         , mxSFI( xSFI )
    3001             :         , mrModifiable( _rModifiable )
    3002             :         , maNameContainer( aType )
    3003             :         , mbLoaded( sal_True )
    3004             :         , mbIsModified( sal_True )
    3005             :         , mbInitialised( false )
    3006             :         , mbLink( sal_False )
    3007             :         , mbReadOnly( sal_False )
    3008             :         , mbReadOnlyLink( sal_False )
    3009             :         , mbPreload( sal_False )
    3010             :         , mbPasswordProtected( sal_False )
    3011             :         , mbPasswordVerified( sal_False )
    3012             :         , mbDoc50Password( false )
    3013             :         , mbSharedIndexFile( false )
    3014         923 :         , mbExtension( false )
    3015             : {
    3016         923 : }
    3017             : 
    3018         512 : SfxLibrary::SfxLibrary( ModifiableHelper& _rModifiable, const Type& aType,
    3019             :     const Reference< XComponentContext >& xContext, const Reference< XSimpleFileAccess3 >& xSFI,
    3020             :     const OUString& aLibInfoFileURL, const OUString& aStorageURL, sal_Bool ReadOnly )
    3021             :         : OComponentHelper( m_aMutex )
    3022             :         , mxContext( xContext )
    3023             :         , mxSFI( xSFI )
    3024             :         , mrModifiable( _rModifiable )
    3025             :         , maNameContainer( aType )
    3026             :         , mbLoaded( sal_False )
    3027             :         , mbIsModified( sal_True )
    3028             :         , mbInitialised( false )
    3029             :         , maLibInfoFileURL( aLibInfoFileURL )
    3030             :         , maStorageURL( aStorageURL )
    3031             :         , mbLink( sal_True )
    3032             :         , mbReadOnly( sal_False )
    3033             :         , mbReadOnlyLink( ReadOnly )
    3034             :         , mbPreload( sal_False )
    3035             :         , mbPasswordProtected( sal_False )
    3036             :         , mbPasswordVerified( sal_False )
    3037             :         , mbDoc50Password( false )
    3038             :         , mbSharedIndexFile( false )
    3039         512 :         , mbExtension( false )
    3040             : {
    3041         512 : }
    3042             : 
    3043         759 : void SfxLibrary::implSetModified( sal_Bool _bIsModified )
    3044             : {
    3045         759 :     if ( mbIsModified == _bIsModified )
    3046             :     {
    3047         941 :         return;
    3048             :     }
    3049         577 :     mbIsModified = _bIsModified;
    3050         577 :     if ( mbIsModified )
    3051             :     {
    3052           0 :         mrModifiable.setModified( sal_True );
    3053             :     }
    3054             : }
    3055             : 
    3056             : // Methods XInterface
    3057        3395 : Any SAL_CALL SfxLibrary::queryInterface( const Type& rType )
    3058             :     throw( RuntimeException )
    3059             : {
    3060        3395 :     Any aRet;
    3061             : 
    3062        6790 :     aRet = Any(
    3063             :         ::cppu::queryInterface(
    3064             :             rType,
    3065             :             static_cast< XContainer * >( this ),
    3066             :             static_cast< XNameContainer * >( this ),
    3067             :             static_cast< XNameAccess * >( this ),
    3068             :             static_cast< XElementAccess * >( this ),
    3069        3395 :             static_cast< XChangesNotifier * >( this ) ) );
    3070        3395 :     if( !aRet.hasValue() )
    3071             :     {
    3072         918 :         aRet = OComponentHelper::queryInterface( rType );
    3073             :     }
    3074        3395 :     return aRet;
    3075             : }
    3076             : 
    3077             : // Methods XElementAccess
    3078           0 : Type SfxLibrary::getElementType()
    3079             :     throw(RuntimeException)
    3080             : {
    3081           0 :     return maNameContainer.getElementType();
    3082             : }
    3083             : 
    3084        1075 : sal_Bool SfxLibrary::hasElements()
    3085             :     throw(RuntimeException)
    3086             : {
    3087        1075 :     sal_Bool bRet = maNameContainer.hasElements();
    3088        1075 :     return bRet;
    3089             : }
    3090             : 
    3091             : // Methods XNameAccess
    3092          40 : Any SfxLibrary::getByName( const OUString& aName )
    3093             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
    3094             : {
    3095          40 :     impl_checkLoaded();
    3096             : 
    3097          40 :     Any aRetAny = maNameContainer.getByName( aName ) ;
    3098          40 :     return aRetAny;
    3099             : }
    3100             : 
    3101         909 : Sequence< OUString > SfxLibrary::getElementNames()
    3102             :     throw(RuntimeException)
    3103             : {
    3104         909 :     return maNameContainer.getElementNames();
    3105             : }
    3106             : 
    3107          43 : sal_Bool SfxLibrary::hasByName( const OUString& aName )
    3108             :     throw(RuntimeException)
    3109             : {
    3110          43 :     sal_Bool bRet = maNameContainer.hasByName( aName );
    3111          43 :     return bRet;
    3112             : }
    3113             : 
    3114         149 : void SfxLibrary::impl_checkReadOnly()
    3115             : {
    3116         149 :     if( mbReadOnly || (mbLink && mbReadOnlyLink) )
    3117             :     {
    3118             :         throw IllegalArgumentException(
    3119             :             OUString("Library is readonly."),
    3120             :             // TODO: resource
    3121             :             *this, 0
    3122           0 :         );
    3123             :     }
    3124         149 : }
    3125             : 
    3126         189 : void SfxLibrary::impl_checkLoaded()
    3127             : {
    3128         189 :     if ( !mbLoaded )
    3129             :     {
    3130             :         throw WrappedTargetException(
    3131             :             OUString(),
    3132             :             *this,
    3133             :             makeAny( LibraryNotLoadedException(
    3134             :                 OUString(),
    3135             :                 *this
    3136             :             ) )
    3137           0 :         );
    3138             :     }
    3139         189 : }
    3140             : 
    3141             : // Methods XNameReplace
    3142           0 : void SfxLibrary::replaceByName( const OUString& aName, const Any& aElement )
    3143             :     throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
    3144             : {
    3145           0 :     impl_checkReadOnly();
    3146           0 :     impl_checkLoaded();
    3147             : 
    3148             :     SAL_WARN_IF(
    3149             :         !isLibraryElementValid(aElement), "basic",
    3150             :         "SfxLibrary::replaceByName: replacing element is invalid!");
    3151             : 
    3152           0 :     maNameContainer.replaceByName( aName, aElement );
    3153           0 :     implSetModified( sal_True );
    3154           0 : }
    3155             : 
    3156             : 
    3157             : // Methods XNameContainer
    3158         149 : void SfxLibrary::insertByName( const OUString& aName, const Any& aElement )
    3159             :     throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
    3160             : {
    3161         149 :     impl_checkReadOnly();
    3162         149 :     impl_checkLoaded();
    3163             : 
    3164             :     SAL_WARN_IF(
    3165             :         !isLibraryElementValid(aElement), "basic",
    3166             :         "SfxLibrary::insertByName: to-be-inserted element is invalid!");
    3167             : 
    3168         149 :     maNameContainer.insertByName( aName, aElement );
    3169         149 :     implSetModified( sal_True );
    3170         149 : }
    3171             : 
    3172           0 : void SfxLibrary::impl_removeWithoutChecks( const OUString& _rElementName )
    3173             : {
    3174           0 :     maNameContainer.removeByName( _rElementName );
    3175           0 :     implSetModified( sal_True );
    3176             : 
    3177             :     // Remove element file
    3178           0 :     if( !maStorageURL.isEmpty() )
    3179             :     {
    3180           0 :         INetURLObject aElementInetObj( maStorageURL );
    3181             :         aElementInetObj.insertName( _rElementName, sal_False,
    3182             :                                     INetURLObject::LAST_SEGMENT, sal_True,
    3183           0 :                                     INetURLObject::ENCODE_ALL );
    3184           0 :         aElementInetObj.setExtension( maLibElementFileExtension );
    3185           0 :         OUString aFile = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
    3186             : 
    3187             :         try
    3188             :         {
    3189           0 :             if( mxSFI->exists( aFile ) )
    3190             :             {
    3191           0 :                 mxSFI->kill( aFile );
    3192             :             }
    3193             :         }
    3194           0 :         catch(const Exception& )
    3195             :         {
    3196             :             DBG_UNHANDLED_EXCEPTION();
    3197           0 :         }
    3198             :     }
    3199           0 : }
    3200             : 
    3201           0 : void SfxLibrary::removeByName( const OUString& Name )
    3202             :     throw(NoSuchElementException, WrappedTargetException, RuntimeException)
    3203             : {
    3204           0 :     impl_checkReadOnly();
    3205           0 :     impl_checkLoaded();
    3206           0 :     impl_removeWithoutChecks( Name );
    3207           0 : }
    3208             : 
    3209             : // XTypeProvider
    3210           0 : Sequence< Type > SfxLibrary::getTypes()
    3211             :     throw( RuntimeException )
    3212             : {
    3213             :     static OTypeCollection * s_pTypes_NameContainer = 0;
    3214             :     {
    3215           0 :         if( !s_pTypes_NameContainer )
    3216             :         {
    3217           0 :             MutexGuard aGuard( Mutex::getGlobalMutex() );
    3218           0 :             if( !s_pTypes_NameContainer )
    3219             :             {
    3220             :                 static OTypeCollection s_aTypes_NameContainer(
    3221           0 :                     ::getCppuType( (const Reference< XNameContainer > *)0 ),
    3222           0 :                     ::getCppuType( (const Reference< XContainer > *)0 ),
    3223           0 :                     ::getCppuType( (const Reference< XChangesNotifier > *)0 ),
    3224           0 :                     OComponentHelper::getTypes() );
    3225           0 :                 s_pTypes_NameContainer = &s_aTypes_NameContainer;
    3226           0 :             }
    3227             :         }
    3228           0 :         return s_pTypes_NameContainer->getTypes();
    3229             :     }
    3230             : }
    3231             : 
    3232             : 
    3233           0 : Sequence< sal_Int8 > SfxLibrary::getImplementationId()
    3234             :     throw( RuntimeException )
    3235             : {
    3236             :     static OImplementationId * s_pId_NameContainer = 0;
    3237             :     {
    3238           0 :         if( !s_pId_NameContainer )
    3239             :         {
    3240           0 :             MutexGuard aGuard( Mutex::getGlobalMutex() );
    3241           0 :             if( !s_pId_NameContainer )
    3242             :             {
    3243           0 :                 static OImplementationId s_aId_NameContainer;
    3244           0 :                 s_pId_NameContainer = &s_aId_NameContainer;
    3245           0 :             }
    3246             :         }
    3247           0 :         return s_pId_NameContainer->getImplementationId();
    3248             :     }
    3249             : }
    3250             : 
    3251             : // Methods XContainer
    3252         748 : void SAL_CALL SfxLibrary::addContainerListener( const Reference< XContainerListener >& xListener )
    3253             :     throw (RuntimeException)
    3254             : {
    3255         748 :     maNameContainer.setEventSource( static_cast< XInterface* >( (OWeakObject*)this ) );
    3256         748 :     maNameContainer.addContainerListener( xListener );
    3257         748 : }
    3258             : 
    3259           0 : void SAL_CALL SfxLibrary::removeContainerListener( const Reference< XContainerListener >& xListener )
    3260             :     throw (RuntimeException)
    3261             : {
    3262           0 :     maNameContainer.removeContainerListener( xListener );
    3263           0 : }
    3264             : 
    3265             : // Methods XChangesNotifier
    3266          26 : void SAL_CALL SfxLibrary::addChangesListener( const Reference< XChangesListener >& xListener )
    3267             :     throw (RuntimeException)
    3268             : {
    3269          26 :     maNameContainer.setEventSource( static_cast< XInterface* >( (OWeakObject*)this ) );
    3270          26 :     maNameContainer.addChangesListener( xListener );
    3271          26 : }
    3272             : 
    3273           0 : void SAL_CALL SfxLibrary::removeChangesListener( const Reference< XChangesListener >& xListener )
    3274             :     throw (RuntimeException)
    3275             : {
    3276           0 :     maNameContainer.removeChangesListener( xListener );
    3277           0 : }
    3278             : 
    3279             : //============================================================================
    3280             : // Implementation class ScriptExtensionIterator
    3281             : 
    3282             : #define sBasicLibMediaType "application/vnd.sun.star.basic-library"
    3283             : #define sDialogLibMediaType "application/vnd.sun.star.dialog-library"
    3284             : 
    3285         124 : ScriptExtensionIterator::ScriptExtensionIterator( void )
    3286             :     : m_xContext( comphelper::getProcessComponentContext() )
    3287             :     , m_eState( USER_EXTENSIONS )
    3288             :     , m_bUserPackagesLoaded( false )
    3289             :     , m_bSharedPackagesLoaded( false )
    3290             :     , m_bBundledPackagesLoaded( false )
    3291             :     , m_iUserPackage( 0 )
    3292             :     , m_iSharedPackage( 0 )
    3293             :        , m_iBundledPackage( 0 )
    3294         124 :     , m_pScriptSubPackageIterator( NULL )
    3295         124 : {}
    3296             : 
    3297         124 : OUString ScriptExtensionIterator::nextBasicOrDialogLibrary( bool& rbPureDialogLib )
    3298             : {
    3299         124 :     OUString aRetLib;
    3300             : 
    3301         556 :     while( aRetLib.isEmpty() && m_eState != END_REACHED )
    3302             :     {
    3303         310 :         switch( m_eState )
    3304             :         {
    3305             :             case USER_EXTENSIONS:
    3306             :             {
    3307             :                 Reference< deployment::XPackage > xScriptPackage =
    3308         124 :                     implGetNextUserScriptPackage( rbPureDialogLib );
    3309         122 :                 if( !xScriptPackage.is() )
    3310             :                 {
    3311         122 :                     break;
    3312             :                 }
    3313           0 :                 aRetLib = xScriptPackage->getURL();
    3314           0 :                 break;
    3315             :             }
    3316             : 
    3317             :             case SHARED_EXTENSIONS:
    3318             :             {
    3319             :                 Reference< deployment::XPackage > xScriptPackage =
    3320          62 :                     implGetNextSharedScriptPackage( rbPureDialogLib );
    3321          62 :                 if( !xScriptPackage.is() )
    3322             :                 {
    3323          62 :                     break;
    3324             :                 }
    3325           0 :                 aRetLib = xScriptPackage->getURL();
    3326           0 :                 break;
    3327             :             }
    3328             :             case BUNDLED_EXTENSIONS:
    3329             :             {
    3330             :                 Reference< deployment::XPackage > xScriptPackage =
    3331         124 :                     implGetNextBundledScriptPackage( rbPureDialogLib );
    3332         124 :                 if( !xScriptPackage.is() )
    3333             :                 {
    3334         124 :                     break;
    3335             :                 }
    3336           0 :                 aRetLib = xScriptPackage->getURL();
    3337           0 :                 break;
    3338             :             }
    3339             :             case END_REACHED:
    3340             :                 SAL_WARN(
    3341             :                     "basic",
    3342             :                     ("ScriptExtensionIterator::nextBasicOrDialogLibrary():"
    3343             :                      " Invalid case END_REACHED"));
    3344           0 :                 break;
    3345             :         }
    3346             :     }
    3347             : 
    3348         122 :     return aRetLib;
    3349             : }
    3350             : 
    3351          62 : ScriptSubPackageIterator::ScriptSubPackageIterator( Reference< deployment::XPackage > xMainPackage )
    3352             :     : m_xMainPackage( xMainPackage )
    3353             :     , m_bIsValid( false )
    3354             :     , m_bIsBundle( false )
    3355             :     , m_nSubPkgCount( 0 )
    3356          62 :     , m_iNextSubPkg( 0 )
    3357             : {
    3358          62 :     Reference< deployment::XPackage > xScriptPackage;
    3359          62 :     if( !m_xMainPackage.is() )
    3360             :     {
    3361          62 :         return;
    3362             :     }
    3363             :     // Check if parent package is registered
    3364          62 :     beans::Optional< beans::Ambiguous<sal_Bool> > option( m_xMainPackage->isRegistered
    3365          62 :         ( Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>() ) );
    3366          62 :     bool bRegistered = false;
    3367          62 :     if( option.IsPresent )
    3368             :     {
    3369          62 :         beans::Ambiguous<sal_Bool> const & reg = option.Value;
    3370          62 :         if( !reg.IsAmbiguous && reg.Value )
    3371             :         {
    3372          62 :             bRegistered = true;
    3373             :         }
    3374             :     }
    3375          62 :     if( bRegistered )
    3376             :     {
    3377          62 :         m_bIsValid = true;
    3378          62 :         if( m_xMainPackage->isBundle() )
    3379             :         {
    3380          62 :             m_bIsBundle = true;
    3381         186 :             m_aSubPkgSeq = m_xMainPackage->getBundle( Reference<task::XAbortChannel>(),
    3382         124 :                                                       Reference<ucb::XCommandEnvironment>() );
    3383          62 :             m_nSubPkgCount = m_aSubPkgSeq.getLength();
    3384             :         }
    3385          62 :     }
    3386             : }
    3387             : 
    3388          62 : Reference< deployment::XPackage > ScriptSubPackageIterator::getNextScriptSubPackage( bool& rbPureDialogLib )
    3389             : {
    3390          62 :     rbPureDialogLib = false;
    3391             : 
    3392          62 :     Reference< deployment::XPackage > xScriptPackage;
    3393          62 :     if( !m_bIsValid )
    3394             :     {
    3395           0 :         return xScriptPackage;
    3396             :     }
    3397          62 :     if( m_bIsBundle )
    3398             :     {
    3399          62 :         const Reference< deployment::XPackage >* pSeq = m_aSubPkgSeq.getConstArray();
    3400             :         sal_Int32 iPkg;
    3401         372 :         for( iPkg = m_iNextSubPkg ; iPkg < m_nSubPkgCount ; ++iPkg )
    3402             :         {
    3403         310 :             const Reference< deployment::XPackage > xSubPkg = pSeq[ iPkg ];
    3404         310 :             xScriptPackage = implDetectScriptPackage( xSubPkg, rbPureDialogLib );
    3405         310 :             if( xScriptPackage.is() )
    3406             :             {
    3407           0 :                 break;
    3408             :             }
    3409         310 :         }
    3410          62 :         m_iNextSubPkg = iPkg + 1;
    3411             :     }
    3412             :     else
    3413             :     {
    3414           0 :         xScriptPackage = implDetectScriptPackage( m_xMainPackage, rbPureDialogLib );
    3415           0 :         m_bIsValid = false;     // No more script packages
    3416             :     }
    3417             : 
    3418          62 :     return xScriptPackage;
    3419             : }
    3420             : 
    3421         310 : Reference< deployment::XPackage > ScriptSubPackageIterator::implDetectScriptPackage ( const Reference< deployment::XPackage > xPackage,
    3422             :                                                                                       bool& rbPureDialogLib )
    3423             : {
    3424         310 :     Reference< deployment::XPackage > xScriptPackage;
    3425             : 
    3426         310 :     if( xPackage.is() )
    3427             :     {
    3428         310 :         const Reference< deployment::XPackageTypeInfo > xPackageTypeInfo = xPackage->getPackageType();
    3429         620 :         OUString aMediaType = xPackageTypeInfo->getMediaType();
    3430         310 :         if ( aMediaType == sBasicLibMediaType )
    3431             :         {
    3432           0 :             xScriptPackage = xPackage;
    3433             :         }
    3434         310 :         else if ( aMediaType == sDialogLibMediaType )
    3435             :         {
    3436           0 :             rbPureDialogLib = true;
    3437           0 :             xScriptPackage = xPackage;
    3438         310 :         }
    3439             :     }
    3440             : 
    3441         310 :     return xScriptPackage;
    3442             : }
    3443             : 
    3444         124 : Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextUserScriptPackage( bool& rbPureDialogLib )
    3445             : {
    3446         124 :     Reference< deployment::XPackage > xScriptPackage;
    3447             : 
    3448         124 :     if( !m_bUserPackagesLoaded )
    3449             :     {
    3450             :         try
    3451             :         {
    3452         124 :             Reference< XExtensionManager > xManager = ExtensionManager::get( m_xContext );
    3453         190 :             m_aUserPackagesSeq = xManager->getDeployedExtensions(OUString("user"),
    3454             :                                                                  Reference< task::XAbortChannel >(),
    3455         190 :                                                                  Reference< ucb::XCommandEnvironment >() );
    3456             :         }
    3457         120 :         catch(const com::sun::star::uno::DeploymentException& )
    3458             :         {
    3459             :             // Special Office installations may not contain deployment code
    3460          60 :             m_eState = END_REACHED;
    3461          60 :             return xScriptPackage;
    3462             :         }
    3463             : 
    3464          62 :         m_bUserPackagesLoaded = true;
    3465             :     }
    3466             : 
    3467          62 :     if( m_iUserPackage == m_aUserPackagesSeq.getLength() )
    3468             :     {
    3469          62 :         m_eState = SHARED_EXTENSIONS;       // Later: SHARED_MODULE
    3470             :     }
    3471             :     else
    3472             :     {
    3473           0 :         if( m_pScriptSubPackageIterator == NULL )
    3474             :         {
    3475           0 :             const Reference< deployment::XPackage >* pUserPackages = m_aUserPackagesSeq.getConstArray();
    3476           0 :             Reference< deployment::XPackage > xPackage = pUserPackages[ m_iUserPackage ];
    3477             :             SAL_WARN_IF(
    3478             :                 !xPackage.is(), "basic",
    3479             :                 ("ScriptExtensionIterator::implGetNextUserScriptPackage():"
    3480             :                  " Invalid package"));
    3481           0 :             m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
    3482             :         }
    3483             : 
    3484           0 :         if( m_pScriptSubPackageIterator != NULL )
    3485             :         {
    3486           0 :             xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
    3487           0 :             if( !xScriptPackage.is() )
    3488             :             {
    3489           0 :                 delete m_pScriptSubPackageIterator;
    3490           0 :                 m_pScriptSubPackageIterator = NULL;
    3491           0 :                 m_iUserPackage++;
    3492             :             }
    3493             :         }
    3494             :     }
    3495             : 
    3496          62 :     return xScriptPackage;
    3497             : }
    3498             : 
    3499          62 : Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextSharedScriptPackage( bool& rbPureDialogLib )
    3500             : {
    3501          62 :     Reference< deployment::XPackage > xScriptPackage;
    3502             : 
    3503          62 :     if( !m_bSharedPackagesLoaded )
    3504             :     {
    3505             :         try
    3506             :         {
    3507          62 :             Reference< XExtensionManager > xSharedManager = ExtensionManager::get( m_xContext );
    3508         186 :             m_aSharedPackagesSeq = xSharedManager->getDeployedExtensions(OUString("shared"),
    3509             :                                                                          Reference< task::XAbortChannel >(),
    3510         186 :                                                                          Reference< ucb::XCommandEnvironment >() );
    3511             :         }
    3512           0 :         catch(const com::sun::star::uno::DeploymentException& )
    3513             :         {
    3514             :             // Special Office installations may not contain deployment code
    3515           0 :             return xScriptPackage;
    3516             :         }
    3517             : 
    3518          62 :         m_bSharedPackagesLoaded = true;
    3519             :     }
    3520             : 
    3521          62 :     if( m_iSharedPackage == m_aSharedPackagesSeq.getLength() )
    3522             :     {
    3523          62 :         m_eState = BUNDLED_EXTENSIONS;
    3524             :     }
    3525             :     else
    3526             :     {
    3527           0 :         if( m_pScriptSubPackageIterator == NULL )
    3528             :         {
    3529           0 :             const Reference< deployment::XPackage >* pSharedPackages = m_aSharedPackagesSeq.getConstArray();
    3530           0 :             Reference< deployment::XPackage > xPackage = pSharedPackages[ m_iSharedPackage ];
    3531             :             SAL_WARN_IF(
    3532             :                 !xPackage.is(), "basic",
    3533             :                 ("ScriptExtensionIterator::implGetNextSharedScriptPackage():"
    3534             :                  " Invalid package"));
    3535           0 :             m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
    3536             :         }
    3537             : 
    3538           0 :         if( m_pScriptSubPackageIterator != NULL )
    3539             :         {
    3540           0 :             xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
    3541           0 :             if( !xScriptPackage.is() )
    3542             :             {
    3543           0 :                 delete m_pScriptSubPackageIterator;
    3544           0 :                 m_pScriptSubPackageIterator = NULL;
    3545           0 :                 m_iSharedPackage++;
    3546             :             }
    3547             :         }
    3548             :     }
    3549             : 
    3550          62 :     return xScriptPackage;
    3551             : }
    3552             : 
    3553         124 : Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextBundledScriptPackage( bool& rbPureDialogLib )
    3554             : {
    3555         124 :     Reference< deployment::XPackage > xScriptPackage;
    3556             : 
    3557         124 :     if( !m_bBundledPackagesLoaded )
    3558             :     {
    3559             :         try
    3560             :         {
    3561          62 :             Reference< XExtensionManager > xManager = ExtensionManager::get( m_xContext );
    3562         186 :             m_aBundledPackagesSeq = xManager->getDeployedExtensions(OUString("bundled"),
    3563             :                                                                     Reference< task::XAbortChannel >(),
    3564         186 :                                                                     Reference< ucb::XCommandEnvironment >() );
    3565             :         }
    3566           0 :         catch(const com::sun::star::uno::DeploymentException& )
    3567             :         {
    3568             :             // Special Office installations may not contain deployment code
    3569           0 :             return xScriptPackage;
    3570             :         }
    3571             : 
    3572          62 :         m_bBundledPackagesLoaded = true;
    3573             :     }
    3574             : 
    3575         124 :     if( m_iBundledPackage == m_aBundledPackagesSeq.getLength() )
    3576             :     {
    3577          62 :         m_eState = END_REACHED;
    3578             :     }
    3579             :     else
    3580             :     {
    3581          62 :         if( m_pScriptSubPackageIterator == NULL )
    3582             :         {
    3583          62 :             const Reference< deployment::XPackage >* pBundledPackages = m_aBundledPackagesSeq.getConstArray();
    3584          62 :             Reference< deployment::XPackage > xPackage = pBundledPackages[ m_iBundledPackage ];
    3585             :             SAL_WARN_IF(
    3586             :                 !xPackage.is(), "basic",
    3587             :                 ("ScriptExtensionIterator::implGetNextBundledScriptPackage():"
    3588             :                  " Invalid package"));
    3589          62 :             m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
    3590             :         }
    3591             : 
    3592          62 :         if( m_pScriptSubPackageIterator != NULL )
    3593             :         {
    3594          62 :             xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
    3595          62 :             if( !xScriptPackage.is() )
    3596             :             {
    3597          62 :                 delete m_pScriptSubPackageIterator;
    3598          62 :                 m_pScriptSubPackageIterator = NULL;
    3599          62 :                 m_iBundledPackage++;
    3600             :             }
    3601             :         }
    3602             :     }
    3603             : 
    3604         124 :     return xScriptPackage;
    3605             : }
    3606             : 
    3607         429 : }   // namespace basic
    3608             : 
    3609             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10