LCOV - code coverage report
Current view: top level - libreoffice/basic/source/basmgr - basicmanagerrepository.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 154 178 86.5 %
Date: 2012-12-17 Functions: 24 27 88.9 %
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 <basic/basicmanagerrepository.hxx>
      21             : #include <basic/basmgr.hxx>
      22             : #include "scriptcont.hxx"
      23             : #include "dlgcont.hxx"
      24             : #include <basic/sbuno.hxx>
      25             : #include "sbintern.hxx"
      26             : 
      27             : #include <com/sun/star/beans/XPropertySet.hpp>
      28             : #include <com/sun/star/document/XStorageBasedDocument.hpp>
      29             : #include <com/sun/star/document/XEmbeddedScripts.hpp>
      30             : #include <svtools/ehdl.hxx>
      31             : #include <svtools/sfxecode.hxx>
      32             : #include <unotools/pathoptions.hxx>
      33             : #include <svl/smplhint.hxx>
      34             : #include <vcl/svapp.hxx>
      35             : #include <tools/debug.hxx>
      36             : #include <tools/diagnose_ex.h>
      37             : #include <tools/urlobj.hxx>
      38             : #include <comphelper/stl_types.hxx>
      39             : #include <comphelper/processfactory.hxx>
      40             : #include <comphelper/documentinfo.hxx>
      41             : #include <unotools/eventlisteneradapter.hxx>
      42             : 
      43             : #include <rtl/instance.hxx>
      44             : #include <rtl/strbuf.hxx>
      45             : 
      46             : #include <map>
      47             : 
      48             : //........................................................................
      49             : namespace basic
      50             : {
      51             : //........................................................................
      52             : 
      53             :     /** === begin UNO using === **/
      54             :     using ::com::sun::star::uno::Reference;
      55             :     using ::com::sun::star::frame::XModel;
      56             :     using ::com::sun::star::uno::XInterface;
      57             :     using ::com::sun::star::uno::UNO_QUERY;
      58             :     using ::com::sun::star::embed::XStorage;
      59             :     using ::com::sun::star::script::XPersistentLibraryContainer;
      60             :     using ::com::sun::star::uno::Any;
      61             :     using ::com::sun::star::lang::XMultiServiceFactory;
      62             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      63             :     using ::com::sun::star::beans::XPropertySet;
      64             :     using ::com::sun::star::uno::Exception;
      65             :     using ::com::sun::star::document::XStorageBasedDocument;
      66             :     using ::com::sun::star::lang::XComponent;
      67             :     using ::com::sun::star::document::XEmbeddedScripts;
      68             :     /** === end UNO using === **/
      69             : 
      70             :     typedef BasicManager*   BasicManagerPointer;
      71             :     typedef ::std::map< Reference< XInterface >, BasicManagerPointer, ::comphelper::OInterfaceCompare< XInterface > > BasicManagerStore;
      72             : 
      73             :     typedef ::std::vector< BasicManagerCreationListener* >  CreationListeners;
      74             : 
      75             :     //====================================================================
      76             :     //= BasicManagerCleaner
      77             :     //====================================================================
      78             :     /// is the only instance which is allowed to delete a BasicManager instance
      79             :     class BasicManagerCleaner
      80             :     {
      81             :     public:
      82         330 :         static void deleteBasicManager( BasicManager*& _rpManager )
      83             :         {
      84         330 :             delete _rpManager;
      85         330 :             _rpManager = NULL;
      86         330 :         }
      87             :     };
      88             : 
      89             :     //====================================================================
      90             :     //= ImplRepository
      91             :     //====================================================================
      92           0 :     class ImplRepository : public ::utl::OEventListenerAdapter, public SfxListener
      93             :     {
      94             :     private:
      95             :         friend struct CreateImplRepository;
      96             :         ImplRepository();
      97             : 
      98             :     private:
      99             :         ::osl::Mutex        m_aMutex;
     100             :         BasicManagerStore   m_aStore;
     101             :         CreationListeners   m_aCreationListeners;
     102             : 
     103             :     public:
     104             :         static ImplRepository& Instance();
     105             : 
     106             :         BasicManager*   getDocumentBasicManager( const Reference< XModel >& _rxDocumentModel );
     107             :         BasicManager*   getApplicationBasicManager( bool _bCreate );
     108             :         void            setApplicationBasicManager( BasicManager* _pBasicManager );
     109             :         void    registerCreationListener( BasicManagerCreationListener& _rListener );
     110             :         void    revokeCreationListener( BasicManagerCreationListener& _rListener );
     111             : 
     112             :     private:
     113             :         /** retrieves the location at which the BasicManager for the given model
     114             :             is stored.
     115             : 
     116             :             If previously, the BasicManager for this model has never been requested,
     117             :             then the model is added to the map, with an initial NULL BasicManager.
     118             : 
     119             :             @param _rxDocumentModel
     120             :                 the model whose BasicManager's location is to be retrieved. Must not be <NULL/>.
     121             : 
     122             :             @precond
     123             :                 our mutex is locked
     124             :         */
     125             :         BasicManagerPointer&
     126             :                 impl_getLocationForModel( const Reference< XModel >& _rxDocumentModel );
     127             : 
     128             :         /** creates a new BasicManager instance for the given model
     129             : 
     130             :             @param _out_rpBasicManager
     131             :                 reference to the pointer variable that will hold the new
     132             :                 BasicManager.
     133             : 
     134             :             @param _rxDocumentModel
     135             :                 the model whose BasicManager will be created. Must not be <NULL/>.
     136             :         */
     137             :         void impl_createManagerForModel(
     138             :                     BasicManagerPointer& _out_rpBasicManager,
     139             :                     const Reference< XModel >& _rxDocumentModel );
     140             : 
     141             :         /** creates the application-wide BasicManager
     142             :         */
     143             :         BasicManagerPointer impl_createApplicationBasicManager();
     144             : 
     145             :         /** notifies all listeners which expressed interest in the creation of BasicManager instances.
     146             :         */
     147             :         void    impl_notifyCreationListeners(
     148             :                     const Reference< XModel >& _rxDocumentModel,
     149             :                     BasicManager& _rManager
     150             :                  );
     151             : 
     152             :         /** retrieves the current storage of a given document
     153             : 
     154             :             @param  _rxDocument
     155             :                 the document whose storage is to be retrieved.
     156             : 
     157             :             @param  _out_rStorage
     158             :                 takes the storage upon successful return. Note that this might be <NULL/> even
     159             :                 if <TRUE/> is returned. In this case, the document has not yet been saved.
     160             : 
     161             :             @return
     162             :                 <TRUE/> if the storage could be successfully retrieved (in which case
     163             :                 <arg>_out_rStorage</arg> might or might not be <NULL/>), <FALSE/> otherwise.
     164             :                 In the latter case, processing this document should stop.
     165             :         */
     166             :         bool    impl_getDocumentStorage_nothrow( const Reference< XModel >& _rxDocument, Reference< XStorage >& _out_rStorage );
     167             : 
     168             :         /** retrieves the containers for Basic and Dialog libraries for a given document
     169             : 
     170             :             @param  _rxDocument
     171             :                 the document whose containers are to be retrieved.
     172             : 
     173             :             @param _out_rxBasicLibraries
     174             :                 takes the basic library container upon successful return
     175             : 
     176             :             @param _out_rxDialogLibraries
     177             :                 takes the dialog library container upon successful return
     178             : 
     179             :             @return
     180             :                 <TRUE/> if and only if both containers exist, and could successfully be retrieved
     181             :         */
     182             :         bool    impl_getDocumentLibraryContainers_nothrow(
     183             :                     const Reference< XModel >& _rxDocument,
     184             :                     Reference< XPersistentLibraryContainer >& _out_rxBasicLibraries,
     185             :                     Reference< XPersistentLibraryContainer >& _out_rxDialogLibraries
     186             :                 );
     187             : 
     188             :         /** initializes the given library containers, which belong to a document
     189             :         */
     190             :         void    impl_initDocLibraryContainers_nothrow(
     191             :                     const Reference< XPersistentLibraryContainer >& _rxBasicLibraries,
     192             :                     const Reference< XPersistentLibraryContainer >& _rxDialogLibraries
     193             :                 );
     194             : 
     195             :         // OEventListenerAdapter overridables
     196             :         virtual void _disposing( const ::com::sun::star::lang::EventObject& _rSource );
     197             : 
     198             :         // SfxListener overridables
     199             :         virtual void Notify( SfxBroadcaster& _rBC, const SfxHint& _rHint );
     200             : 
     201             :         /** removes the Model/BasicManager pair given by iterator from our store
     202             :         */
     203             :         void impl_removeFromRepository( BasicManagerStore::iterator _pos );
     204             : 
     205             :     private:
     206             :         StarBASIC* impl_getDefaultAppBasicLibrary();
     207             :     };
     208             : 
     209             :     //====================================================================
     210             :     //= CreateImplRepository
     211             :     //====================================================================
     212             :     struct CreateImplRepository
     213             :     {
     214          72 :         ImplRepository* operator()()
     215             :         {
     216          72 :             static ImplRepository* pRepository = new ImplRepository;
     217          72 :             return pRepository;
     218             :         }
     219             :     };
     220             : 
     221             : 
     222             :     //====================================================================
     223             :     //= ImplRepository
     224             :     //====================================================================
     225             :     //--------------------------------------------------------------------
     226          72 :     ImplRepository::ImplRepository()
     227             :     {
     228          72 :     }
     229             : 
     230             :     //--------------------------------------------------------------------
     231        1761 :     ImplRepository& ImplRepository::Instance()
     232             :     {
     233             :         return *rtl_Instance< ImplRepository, CreateImplRepository, ::osl::MutexGuard, ::osl::GetGlobalMutex >::
     234        1761 :             create( CreateImplRepository(), ::osl::GetGlobalMutex() );
     235             :     }
     236             : 
     237             :     //--------------------------------------------------------------------
     238         580 :     BasicManager* ImplRepository::getDocumentBasicManager( const Reference< XModel >& _rxDocumentModel )
     239             :     {
     240         580 :         ::osl::MutexGuard aGuard( m_aMutex );
     241             : 
     242             :         /*  #163556# (DR) - This function may be called recursively while
     243             :             constructing the Basic manager and loading the Basic storage. By
     244             :             passing the map entry received from impl_getLocationForModel() to
     245             :             the function impl_createManagerForModel(), the new Basic manager
     246             :             will be put immediately into the map of existing Basic managers,
     247             :             thus a recursive call of this function will find and return it
     248             :             without creating another instance.
     249             :          */
     250         580 :         BasicManagerPointer& pBasicManager = impl_getLocationForModel( _rxDocumentModel );
     251         580 :         if ( pBasicManager == NULL )
     252         564 :             impl_createManagerForModel( pBasicManager, _rxDocumentModel );
     253             : 
     254         578 :         return pBasicManager;
     255             :     }
     256             : 
     257             :     //--------------------------------------------------------------------
     258        1717 :     BasicManager* ImplRepository::getApplicationBasicManager( bool _bCreate )
     259             :     {
     260        1717 :         ::osl::MutexGuard aGuard( m_aMutex );
     261             : 
     262        1717 :         BasicManager* pAppManager = GetSbData()->pAppBasMgr;
     263        1717 :         if ( ( pAppManager == NULL ) && _bCreate )
     264          52 :             pAppManager = impl_createApplicationBasicManager();
     265             : 
     266        1715 :         return pAppManager;
     267             :     }
     268             : 
     269             :     //--------------------------------------------------------------------
     270          60 :     void ImplRepository::setApplicationBasicManager( BasicManager* _pBasicManager )
     271             :     {
     272          60 :         ::osl::MutexGuard aGuard( m_aMutex );
     273             : 
     274          60 :         BasicManager* pPreviousManager = getApplicationBasicManager( false );
     275          60 :         BasicManagerCleaner::deleteBasicManager( pPreviousManager );
     276             : 
     277          60 :         GetSbData()->pAppBasMgr = _pBasicManager;
     278          60 :     }
     279             : 
     280             :     //--------------------------------------------------------------------
     281          52 :     BasicManager* ImplRepository::impl_createApplicationBasicManager()
     282             :     {
     283          52 :         ::osl::MutexGuard aGuard( m_aMutex );
     284             :         OSL_PRECOND( getApplicationBasicManager( false ) == NULL, "ImplRepository::impl_createApplicationBasicManager: there already is one!" );
     285             : 
     286             :         // Determine Directory
     287          52 :         SvtPathOptions aPathCFG;
     288          52 :         OUString aAppBasicDir( aPathCFG.GetBasicPath() );
     289          52 :         if ( aAppBasicDir.isEmpty() )
     290             :         {
     291           0 :             aPathCFG.SetBasicPath(rtl::OUString("$(prog)"));
     292             :         }
     293             :         // soffice.new search only in user dir => first dir
     294          52 :         OUString aAppFirstBasicDir = aAppBasicDir.getToken(1, ';');
     295             : 
     296             :         // Create basic and load it
     297             :         // AppBasicDir is now a PATH
     298          52 :         INetURLObject aAppBasic( SvtPathOptions().SubstituteVariable(rtl::OUString("$(progurl)")) );
     299          52 :         aAppBasic.insertName( Application::GetAppName() );
     300             : 
     301          52 :         BasicManager* pBasicManager = new BasicManager( new StarBASIC, &aAppBasicDir );
     302          52 :         setApplicationBasicManager( pBasicManager );
     303             : 
     304             :         // The first dir in the path as destination:
     305          52 :         OUString aFileName( aAppBasic.getName() );
     306          52 :         aAppBasic = INetURLObject( aAppBasicDir.getToken(1, ';') );
     307             :         DBG_ASSERT(aAppBasic.GetProtocol() != INET_PROT_NOT_VALID,
     308             :             rtl::OStringBuffer(RTL_CONSTASCII_STRINGPARAM("Invalid URL: \"")).
     309             :             append(rtl::OUStringToOString(aAppBasicDir,
     310             :                 osl_getThreadTextEncoding())).
     311             :             append('"').getStr()
     312             :         );
     313          52 :         aAppBasic.insertName( aFileName );
     314          52 :         pBasicManager->SetStorageName( aAppBasic.PathToFileName() );
     315             : 
     316             :         // Basic container
     317          54 :         SfxScriptLibraryContainer* pBasicCont = new SfxScriptLibraryContainer( Reference< XStorage >() );
     318          50 :         Reference< XPersistentLibraryContainer > xBasicCont( pBasicCont );
     319          50 :         pBasicCont->setBasicManager( pBasicManager );
     320             : 
     321             :         // Dialog container
     322          50 :         SfxDialogLibraryContainer* pDialogCont = new SfxDialogLibraryContainer( Reference< XStorage >() );
     323          50 :         Reference< XPersistentLibraryContainer > xDialogCont( pDialogCont );
     324             : 
     325          50 :         LibraryContainerInfo aInfo( xBasicCont, xDialogCont, static_cast< OldBasicPassword* >( pBasicCont ) );
     326          50 :         pBasicManager->SetLibraryContainerInfo( aInfo );
     327             : 
     328             :         // global constants
     329             : 
     330             :         // StarDesktop
     331          50 :         Reference< XMultiServiceFactory > xSMgr = ::comphelper::getProcessServiceFactory();
     332             :         pBasicManager->SetGlobalUNOConstant( "StarDesktop",
     333          50 :                                              makeAny( xSMgr->createInstance("com.sun.star.frame.Desktop")));
     334             : 
     335             :         // (BasicLibraries and DialogLibraries have automatically been added in SetLibraryContainerInfo)
     336             : 
     337             :         // notify
     338          50 :         impl_notifyCreationListeners( NULL, *pBasicManager );
     339             : 
     340             :         // outta here
     341          50 :         return pBasicManager;
     342             :     }
     343             : 
     344             :     //--------------------------------------------------------------------
     345          72 :     void ImplRepository::registerCreationListener( BasicManagerCreationListener& _rListener )
     346             :     {
     347          72 :         ::osl::MutexGuard aGuard( m_aMutex );
     348          72 :         m_aCreationListeners.push_back( &_rListener );
     349          72 :     }
     350             : 
     351             :     //--------------------------------------------------------------------
     352           8 :     void ImplRepository::revokeCreationListener( BasicManagerCreationListener& _rListener )
     353             :     {
     354           8 :         ::osl::MutexGuard aGuard( m_aMutex );
     355           8 :         CreationListeners::iterator pos = ::std::find( m_aCreationListeners.begin(), m_aCreationListeners.end(), &_rListener );
     356           8 :         if ( pos != m_aCreationListeners.end() )
     357           8 :             m_aCreationListeners.erase( pos );
     358             :         else {
     359             :             OSL_FAIL( "ImplRepository::revokeCreationListener: listener is not registered!" );
     360           8 :         }
     361           8 :     }
     362             : 
     363             :     //--------------------------------------------------------------------
     364         550 :     void ImplRepository::impl_notifyCreationListeners( const Reference< XModel >& _rxDocumentModel, BasicManager& _rManager )
     365             :     {
     366        3300 :         for (   CreationListeners::const_iterator loop = m_aCreationListeners.begin();
     367        2200 :                 loop != m_aCreationListeners.end();
     368             :                 ++loop
     369             :             )
     370             :         {
     371         550 :             (*loop)->onBasicManagerCreated( _rxDocumentModel, _rManager );
     372             :         }
     373         550 :     }
     374             : 
     375             :     //--------------------------------------------------------------------
     376         564 :     StarBASIC* ImplRepository::impl_getDefaultAppBasicLibrary()
     377             :     {
     378         564 :         BasicManager* pAppManager = getApplicationBasicManager( true );
     379             : 
     380         562 :         StarBASIC* pAppBasic = pAppManager ? pAppManager->GetLib(0) : NULL;
     381             :         DBG_ASSERT( pAppBasic != NULL, "impl_getApplicationBasic: unable to determine the default application's Basic library!" );
     382         562 :         return pAppBasic;
     383             :     }
     384             : 
     385             :     //--------------------------------------------------------------------
     386         580 :     BasicManagerPointer& ImplRepository::impl_getLocationForModel( const Reference< XModel >& _rxDocumentModel )
     387             :     {
     388         580 :         Reference< XInterface > xNormalized( _rxDocumentModel, UNO_QUERY );
     389             :         DBG_ASSERT( xNormalized.is(), "ImplRepository::impl_getLocationForModel: invalid model!" );
     390             : 
     391         580 :         BasicManagerPointer& location = m_aStore[ xNormalized ];
     392         580 :         return location;
     393             :     }
     394             : 
     395             :     //--------------------------------------------------------------------
     396         500 :     void ImplRepository::impl_initDocLibraryContainers_nothrow( const Reference< XPersistentLibraryContainer >& _rxBasicLibraries, const Reference< XPersistentLibraryContainer >& _rxDialogLibraries )
     397             :     {
     398             :         OSL_PRECOND( _rxBasicLibraries.is() && _rxDialogLibraries.is(),
     399             :             "ImplRepository::impl_initDocLibraryContainers_nothrow: illegal library containers, this will crash!" );
     400             : 
     401             :         try
     402             :         {
     403             :             // ensure there's a standard library in the basic container
     404         500 :             OUString aStdLibName( "Standard" );
     405         500 :             if ( !_rxBasicLibraries->hasByName( aStdLibName ) )
     406             :             {
     407          10 :                 _rxBasicLibraries->createLibrary( aStdLibName );
     408             :             }
     409             :             // as well as in the dialog container
     410         500 :             if ( !_rxDialogLibraries->hasByName( aStdLibName ) )
     411             :             {
     412         500 :                 _rxDialogLibraries->createLibrary( aStdLibName );
     413         500 :             }
     414             :         }
     415           0 :         catch( const Exception& )
     416             :         {
     417             :             DBG_UNHANDLED_EXCEPTION();
     418             :         }
     419         500 :     }
     420             : 
     421             :     //--------------------------------------------------------------------
     422         564 :     void ImplRepository::impl_createManagerForModel( BasicManagerPointer& _out_rpBasicManager, const Reference< XModel >& _rxDocumentModel )
     423             :     {
     424         564 :         StarBASIC* pAppBasic = impl_getDefaultAppBasicLibrary();
     425             : 
     426         562 :         _out_rpBasicManager = 0;
     427         562 :         Reference< XStorage > xStorage;
     428         562 :         if ( !impl_getDocumentStorage_nothrow( _rxDocumentModel, xStorage ) )
     429             :         {
     430             :             // the document is not able to provide the storage it is based on.
     431             :             return;
     432             :         }
     433         562 :         Reference< XPersistentLibraryContainer > xBasicLibs;
     434         562 :         Reference< XPersistentLibraryContainer > xDialogLibs;
     435         562 :         if ( !impl_getDocumentLibraryContainers_nothrow( _rxDocumentModel, xBasicLibs, xDialogLibs ) )
     436             :             // the document does not have BasicLibraries and DialogLibraries
     437             :             return;
     438             : 
     439         500 :         if ( xStorage.is() )
     440             :         {
     441             :             // load BASIC-manager
     442             :             SfxErrorContext aErrContext( ERRCTX_SFX_LOADBASIC,
     443         500 :                 ::comphelper::DocumentInfo::getDocumentTitle( _rxDocumentModel ) );
     444         500 :             OUString aAppBasicDir = SvtPathOptions().GetBasicPath();
     445             : 
     446             :             // Storage and BaseURL are only needed by binary documents!
     447         500 :             SotStorageRef xDummyStor = new SotStorage( OUString() );
     448             :             _out_rpBasicManager = new BasicManager( *xDummyStor, OUString() /* TODO/LATER: xStorage */,
     449             :                                                                 pAppBasic,
     450         500 :                                                                 &aAppBasicDir, sal_True );
     451         500 :             if ( !_out_rpBasicManager->GetErrors().empty() )
     452             :             {
     453             :                 // handle errors
     454           0 :                 std::vector<BasicError>& aErrors = _out_rpBasicManager->GetErrors();
     455           0 :                 for(std::vector<BasicError>::const_iterator i = aErrors.begin(); i != aErrors.end(); ++i)
     456             :                 {
     457             :                     // show message to user
     458           0 :                     if ( ERRCODE_BUTTON_CANCEL == ErrorHandler::HandleError( i->GetErrorId() ) )
     459             :                     {
     460             :                         // user wants to break loading of BASIC-manager
     461           0 :                         BasicManagerCleaner::deleteBasicManager( _out_rpBasicManager );
     462           0 :                         xStorage.clear();
     463           0 :                         break;
     464             :                     }
     465             :                 }
     466         500 :             }
     467             :         }
     468             : 
     469             :         // not loaded?
     470         500 :         if ( !xStorage.is() )
     471             :         {
     472             :             // create new BASIC-manager
     473           0 :             StarBASIC* pBasic = new StarBASIC( pAppBasic );
     474           0 :             pBasic->SetFlag( SBX_EXTSEARCH );
     475           0 :             _out_rpBasicManager = new BasicManager( pBasic, NULL, sal_True );
     476             :         }
     477             : 
     478             :         // knit the containers with the BasicManager
     479         500 :         LibraryContainerInfo aInfo( xBasicLibs, xDialogLibs, dynamic_cast< OldBasicPassword* >( xBasicLibs.get() ) );
     480             :         OSL_ENSURE( aInfo.mpOldBasicPassword, "ImplRepository::impl_createManagerForModel: wrong BasicLibraries implementation!" );
     481         500 :         _out_rpBasicManager->SetLibraryContainerInfo( aInfo );
     482             : 
     483             :         // initialize the containers
     484         500 :         impl_initDocLibraryContainers_nothrow( xBasicLibs, xDialogLibs );
     485             : 
     486             :         // so that also dialogs etc. could be 'qualified' addressed
     487         500 :         _out_rpBasicManager->GetLib(0)->SetParent( pAppBasic );
     488             : 
     489             :         // global properties in the document's Basic
     490         500 :         _out_rpBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( _rxDocumentModel ) );
     491             : 
     492             :         // notify
     493         500 :         impl_notifyCreationListeners( _rxDocumentModel, *_out_rpBasicManager );
     494             : 
     495             :         // register as listener for this model being disposed/closed
     496         500 :         Reference< XComponent > xDocumentComponent( _rxDocumentModel, UNO_QUERY );
     497             :         OSL_ENSURE( xDocumentComponent.is(), "ImplRepository::impl_createManagerForModel: the document must be an XComponent!" );
     498         500 :         startComponentListening( xDocumentComponent );
     499             : 
     500             :         // register as listener for the BasicManager being destroyed
     501         500 :         StartListening( *_out_rpBasicManager );
     502             : 
     503             :         // #i104876: Library container must not be modified just after
     504             :         // creation. This happens as side effect when creating default
     505             :         // "Standard" libraries and needs to be corrected here
     506         500 :         xBasicLibs->setModified( sal_False );
     507         500 :         xDialogLibs->setModified( sal_False );
     508             : 
     509             :     }
     510             : 
     511             :     //--------------------------------------------------------------------
     512         562 :     bool ImplRepository::impl_getDocumentStorage_nothrow( const Reference< XModel >& _rxDocument, Reference< XStorage >& _out_rStorage )
     513             :     {
     514         562 :         _out_rStorage.clear();
     515             :         try
     516             :         {
     517         562 :             Reference< XStorageBasedDocument > xStorDoc( _rxDocument, UNO_QUERY_THROW );
     518         562 :             _out_rStorage.set( xStorDoc->getDocumentStorage() );
     519             :         }
     520           0 :         catch( const Exception& )
     521             :         {
     522             :             DBG_UNHANDLED_EXCEPTION();
     523           0 :             return false;
     524             :         }
     525         562 :         return true;
     526             :     }
     527             : 
     528             :     //--------------------------------------------------------------------
     529         562 :     bool ImplRepository::impl_getDocumentLibraryContainers_nothrow( const Reference< XModel >& _rxDocument,
     530             :         Reference< XPersistentLibraryContainer >& _out_rxBasicLibraries, Reference< XPersistentLibraryContainer >& _out_rxDialogLibraries )
     531             :     {
     532         562 :         _out_rxBasicLibraries.clear();
     533         562 :         _out_rxDialogLibraries.clear();
     534             :         try
     535             :         {
     536         562 :             Reference< XEmbeddedScripts > xScripts( _rxDocument, UNO_QUERY_THROW );
     537         562 :             _out_rxBasicLibraries.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW );
     538         500 :             _out_rxDialogLibraries.set( xScripts->getDialogLibraries(), UNO_QUERY_THROW );
     539             :         }
     540          62 :         catch( const Exception& )
     541             :         {
     542             :             DBG_UNHANDLED_EXCEPTION();
     543             :         }
     544         562 :         return _out_rxBasicLibraries.is() && _out_rxDialogLibraries.is();
     545             :     }
     546             : 
     547             :     //--------------------------------------------------------------------
     548         270 :     void ImplRepository::impl_removeFromRepository( BasicManagerStore::iterator _pos )
     549             :     {
     550             :         OSL_PRECOND( _pos != m_aStore.end(), "ImplRepository::impl_removeFromRepository: invalid position!" );
     551             : 
     552         270 :         BasicManager* pManager = _pos->second;
     553             : 
     554             :         // *first* remove from map (else Notify won't work properly)
     555         270 :         m_aStore.erase( _pos );
     556             : 
     557             :         // *then* delete the BasicManager
     558         270 :         EndListening( *pManager );
     559         270 :         BasicManagerCleaner::deleteBasicManager( pManager );
     560         270 :     }
     561             : 
     562             :     //--------------------------------------------------------------------
     563         270 :     void ImplRepository::_disposing( const ::com::sun::star::lang::EventObject& _rSource )
     564             :     {
     565         270 :         ::osl::MutexGuard aGuard( m_aMutex );
     566             : 
     567         270 :         Reference< XInterface > xNormalizedSource( _rSource.Source, UNO_QUERY );
     568             :     #if OSL_DEBUG_LEVEL > 0
     569             :         bool bFound = false;
     570             :     #endif
     571             : 
     572       11280 :         for (   BasicManagerStore::iterator loop = m_aStore.begin();
     573        7520 :                 loop != m_aStore.end();
     574             :                 ++loop
     575             :             )
     576             :         {
     577        3760 :             if ( loop->first.get() == xNormalizedSource.get() )
     578             :             {
     579         270 :                 impl_removeFromRepository( loop );
     580             :             #if OSL_DEBUG_LEVEL > 0
     581             :                 bFound = true;
     582             :             #endif
     583         270 :                 break;
     584             :             }
     585             :         }
     586             : 
     587         270 :         OSL_ENSURE( bFound, "ImplRepository::_disposing: where does this come from?" );
     588         270 :     }
     589             : 
     590             :     //--------------------------------------------------------------------
     591           0 :     void ImplRepository::Notify( SfxBroadcaster& _rBC, const SfxHint& _rHint )
     592             :     {
     593           0 :         const SfxSimpleHint* pSimpleHint = dynamic_cast< const SfxSimpleHint* >( &_rHint );
     594           0 :         if ( !pSimpleHint || ( pSimpleHint->GetId() != SFX_HINT_DYING ) )
     595             :             // not interested in
     596           0 :             return;
     597             : 
     598           0 :         BasicManager* pManager = dynamic_cast< BasicManager* >( &_rBC );
     599             :         OSL_ENSURE( pManager, "ImplRepository::Notify: where does this come from?" );
     600             : 
     601           0 :         for (   BasicManagerStore::iterator loop = m_aStore.begin();
     602           0 :                 loop != m_aStore.end();
     603             :                 ++loop
     604             :             )
     605             :         {
     606           0 :             if ( loop->second == pManager )
     607             :             {
     608             :                 // a BasicManager which is still in our repository is being deleted.
     609             :                 // That's bad, since by definition, we *own* all instances in our
     610             :                 // repository.
     611             :                 OSL_FAIL( "ImplRepository::Notify: nobody should tamper with the managers, except ourself!" );
     612           0 :                 m_aStore.erase( loop );
     613           0 :                 break;
     614             :             }
     615             :         }
     616             :     }
     617             : 
     618             :     //====================================================================
     619             :     //= BasicManagerRepository
     620             :     //====================================================================
     621             :     //--------------------------------------------------------------------
     622         580 :     BasicManager* BasicManagerRepository::getDocumentBasicManager( const Reference< XModel >& _rxDocumentModel )
     623             :     {
     624         580 :         return ImplRepository::Instance().getDocumentBasicManager( _rxDocumentModel );
     625             :     }
     626             : 
     627             :     //--------------------------------------------------------------------
     628        1093 :     BasicManager* BasicManagerRepository::getApplicationBasicManager( bool _bCreate )
     629             :     {
     630        1093 :         return ImplRepository::Instance().getApplicationBasicManager( _bCreate );
     631             :     }
     632             : 
     633             :     //--------------------------------------------------------------------
     634           8 :     void BasicManagerRepository::resetApplicationBasicManager()
     635             :     {
     636           8 :         return ImplRepository::Instance().setApplicationBasicManager( NULL );
     637             :     }
     638             : 
     639             :     //--------------------------------------------------------------------
     640          72 :     void BasicManagerRepository::registerCreationListener( BasicManagerCreationListener& _rListener )
     641             :     {
     642          72 :         ImplRepository::Instance().registerCreationListener( _rListener );
     643          72 :     }
     644             : 
     645             :     //--------------------------------------------------------------------
     646           8 :     void BasicManagerRepository::revokeCreationListener( BasicManagerCreationListener& _rListener )
     647             :     {
     648           8 :         ImplRepository::Instance().revokeCreationListener( _rListener );
     649           8 :     }
     650             : 
     651             : //........................................................................
     652             : } // namespace basic
     653             : //........................................................................
     654             : 
     655             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10