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