xref: /AOO41X/main/basic/source/basmgr/basmgr.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir #include <tools/stream.hxx>
31*cdf0e10cSrcweir #include <sot/storage.hxx>
32*cdf0e10cSrcweir #include <tools/urlobj.hxx>
33*cdf0e10cSrcweir #include <svl/smplhint.hxx>
34*cdf0e10cSrcweir #include <vcl/svapp.hxx>
35*cdf0e10cSrcweir #include <vcl/window.hxx>
36*cdf0e10cSrcweir #include <vcl/wrkwin.hxx>
37*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
38*cdf0e10cSrcweir #include <basic/sbx.hxx>
39*cdf0e10cSrcweir #include <sot/storinfo.hxx>
40*cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
41*cdf0e10cSrcweir #include <tools/debug.hxx>
42*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
43*cdf0e10cSrcweir #include <basic/sbmod.hxx>
44*cdf0e10cSrcweir #include <basic/sbobjmod.hxx>
45*cdf0e10cSrcweir #include <unotools/intlwrapper.hxx>
46*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <basic/sbuno.hxx>
49*cdf0e10cSrcweir #include <basic/basmgr.hxx>
50*cdf0e10cSrcweir #include <sbunoobj.hxx>
51*cdf0e10cSrcweir #include "basrid.hxx"
52*cdf0e10cSrcweir #include "sbintern.hxx"
53*cdf0e10cSrcweir #include <sb.hrc>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #define LIB_SEP			0x01
57*cdf0e10cSrcweir #define LIBINFO_SEP		0x02
58*cdf0e10cSrcweir #define LIBINFO_ID		0x1491
59*cdf0e10cSrcweir #define PASSWORD_MARKER	0x31452134
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // Library API, implemented for XML import/export
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
65*cdf0e10cSrcweir #include <com/sun/star/container/XContainer.hpp>
66*cdf0e10cSrcweir #include <com/sun/star/script/XStarBasicAccess.hpp>
67*cdf0e10cSrcweir #include <com/sun/star/script/XStarBasicModuleInfo.hpp>
68*cdf0e10cSrcweir #include <com/sun/star/script/XStarBasicDialogInfo.hpp>
69*cdf0e10cSrcweir #include <com/sun/star/script/XStarBasicLibraryInfo.hpp>
70*cdf0e10cSrcweir #include <com/sun/star/script/XLibraryContainerPassword.hpp>
71*cdf0e10cSrcweir #include <com/sun/star/script/ModuleInfo.hpp>
72*cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBACompatibility.hpp>
73*cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir using com::sun::star::uno::Reference;
78*cdf0e10cSrcweir using namespace com::sun::star::container;
79*cdf0e10cSrcweir using namespace com::sun::star::uno;
80*cdf0e10cSrcweir using namespace com::sun::star::lang;
81*cdf0e10cSrcweir using namespace com::sun::star::script;
82*cdf0e10cSrcweir using namespace cppu;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir typedef WeakImplHelper1< XNameContainer > NameContainerHelper;
85*cdf0e10cSrcweir typedef WeakImplHelper1< XStarBasicModuleInfo > ModuleInfoHelper;
86*cdf0e10cSrcweir typedef WeakImplHelper1< XStarBasicDialogInfo > DialogInfoHelper;
87*cdf0e10cSrcweir typedef WeakImplHelper1< XStarBasicLibraryInfo > LibraryInfoHelper;
88*cdf0e10cSrcweir typedef WeakImplHelper1< XStarBasicAccess > StarBasicAccessHelper;
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir #define CURR_VER		2
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir // Version 1
95*cdf0e10cSrcweir //	  sal_uIntPtr 	nEndPos
96*cdf0e10cSrcweir //	  sal_uInt16 	nId
97*cdf0e10cSrcweir //	  sal_uInt16	nVer
98*cdf0e10cSrcweir //	  sal_Bool		bDoLoad
99*cdf0e10cSrcweir //	  String	LibName
100*cdf0e10cSrcweir //	  String	AbsStorageName
101*cdf0e10cSrcweir //	  String	RelStorageName
102*cdf0e10cSrcweir // Version 2
103*cdf0e10cSrcweir //	+ sal_Bool		bReference
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir static const char* szStdLibName = "Standard";
106*cdf0e10cSrcweir static const char szBasicStorage[] = "StarBASIC";
107*cdf0e10cSrcweir static const char* szOldManagerStream = "BasicManager";
108*cdf0e10cSrcweir static const char szManagerStream[] = "BasicManager2";
109*cdf0e10cSrcweir static const char* szImbedded = "LIBIMBEDDED";
110*cdf0e10cSrcweir static const char* szCryptingKey = "CryptedBasic";
111*cdf0e10cSrcweir static const char* szScriptLanguage = "StarBasic";
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir TYPEINIT1( BasicManager, SfxBroadcaster );
114*cdf0e10cSrcweir DBG_NAME( BasicManager );
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir StreamMode eStreamReadMode = STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYALL;
117*cdf0e10cSrcweir StreamMode eStorageReadMode = STREAM_READ | STREAM_SHARE_DENYWRITE;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir DECLARE_LIST( BasErrorLst, BasicError* )
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir //----------------------------------------------------------------------------
122*cdf0e10cSrcweir // BasicManager impl data
123*cdf0e10cSrcweir struct BasicManagerImpl
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir     LibraryContainerInfo    maContainerInfo;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     // Save stream data
128*cdf0e10cSrcweir     SvMemoryStream*  mpManagerStream;
129*cdf0e10cSrcweir     SvMemoryStream** mppLibStreams;
130*cdf0e10cSrcweir     sal_Int32        mnLibStreamCount;
131*cdf0e10cSrcweir     sal_Bool         mbModifiedByLibraryContainer;
132*cdf0e10cSrcweir     sal_Bool         mbError;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	BasicManagerImpl( void )
135*cdf0e10cSrcweir         : mpManagerStream( NULL )
136*cdf0e10cSrcweir         , mppLibStreams( NULL )
137*cdf0e10cSrcweir         , mnLibStreamCount( 0 )
138*cdf0e10cSrcweir         , mbModifiedByLibraryContainer( sal_False )
139*cdf0e10cSrcweir         , mbError( sal_False )
140*cdf0e10cSrcweir 	{}
141*cdf0e10cSrcweir     ~BasicManagerImpl();
142*cdf0e10cSrcweir };
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir BasicManagerImpl::~BasicManagerImpl()
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     delete mpManagerStream;
147*cdf0e10cSrcweir     if( mppLibStreams )
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         for( sal_Int32 i = 0 ; i < mnLibStreamCount ; i++ )
150*cdf0e10cSrcweir             delete mppLibStreams[i];
151*cdf0e10cSrcweir         delete[] mppLibStreams;
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir //============================================================================
156*cdf0e10cSrcweir // BasMgrContainerListenerImpl
157*cdf0e10cSrcweir //============================================================================
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< ::com::sun::star::container::XContainerListener > ContainerListenerHelper;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir class BasMgrContainerListenerImpl: public ContainerListenerHelper
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir 	BasicManager* mpMgr;
164*cdf0e10cSrcweir 	::rtl::OUString maLibName;		// empty -> no lib, but lib container
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir public:
167*cdf0e10cSrcweir 	BasMgrContainerListenerImpl( BasicManager* pMgr, ::rtl::OUString aLibName )
168*cdf0e10cSrcweir         : mpMgr( pMgr )
169*cdf0e10cSrcweir         , maLibName( aLibName ) {}
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 	static void insertLibraryImpl( const Reference< XLibraryContainer >& xScriptCont, BasicManager* pMgr,
172*cdf0e10cSrcweir 		Any aLibAny, ::rtl::OUString aLibName );
173*cdf0e10cSrcweir 	static void addLibraryModulesImpl( BasicManager* pMgr, Reference< XNameAccess > xLibNameAccess,
174*cdf0e10cSrcweir 		::rtl::OUString aLibName );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	// XEventListener
178*cdf0e10cSrcweir 	virtual void SAL_CALL disposing( const  ::com::sun::star::lang::EventObject& Source )
179*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	// XContainerListener
182*cdf0e10cSrcweir 	virtual void SAL_CALL elementInserted( const ::com::sun::star::container::ContainerEvent& Event )
183*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
184*cdf0e10cSrcweir 	virtual void SAL_CALL elementReplaced( const ::com::sun::star::container::ContainerEvent& Event )
185*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
186*cdf0e10cSrcweir 	virtual void SAL_CALL elementRemoved( const ::com::sun::star::container::ContainerEvent& Event )
187*cdf0e10cSrcweir 		throw(::com::sun::star::uno::RuntimeException);
188*cdf0e10cSrcweir };
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir //============================================================================
192*cdf0e10cSrcweir // BasMgrContainerListenerImpl
193*cdf0e10cSrcweir //============================================================================
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir void BasMgrContainerListenerImpl::insertLibraryImpl( const Reference< XLibraryContainer >& xScriptCont,
196*cdf0e10cSrcweir 	BasicManager* pMgr, Any aLibAny, ::rtl::OUString aLibName )
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir 	Reference< XNameAccess > xLibNameAccess;
199*cdf0e10cSrcweir 	aLibAny >>= xLibNameAccess;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	if( !pMgr->GetLib( aLibName ) )
202*cdf0e10cSrcweir 	{
203*cdf0e10cSrcweir         BasicManager* pBasMgr = static_cast< BasicManager* >( pMgr );
204*cdf0e10cSrcweir #ifdef DBG_UTIL
205*cdf0e10cSrcweir 		StarBASIC* pLib =
206*cdf0e10cSrcweir #endif
207*cdf0e10cSrcweir 		pBasMgr->CreateLibForLibContainer( aLibName, xScriptCont );
208*cdf0e10cSrcweir 		DBG_ASSERT( pLib, "XML Import: Basic library could not be created");
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	Reference< XContainer> xLibContainer( xLibNameAccess, UNO_QUERY );
212*cdf0e10cSrcweir 	if( xLibContainer.is() )
213*cdf0e10cSrcweir 	{
214*cdf0e10cSrcweir 		// Register listener for library
215*cdf0e10cSrcweir 		Reference< XContainerListener > xLibraryListener
216*cdf0e10cSrcweir 			= static_cast< XContainerListener* >
217*cdf0e10cSrcweir 				( new BasMgrContainerListenerImpl( pMgr, aLibName ) );
218*cdf0e10cSrcweir 		xLibContainer->addContainerListener( xLibraryListener );
219*cdf0e10cSrcweir 	}
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 	if( xScriptCont->isLibraryLoaded( aLibName ) )
222*cdf0e10cSrcweir 	{
223*cdf0e10cSrcweir 		addLibraryModulesImpl( pMgr, xLibNameAccess, aLibName );
224*cdf0e10cSrcweir 	}
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir void BasMgrContainerListenerImpl::addLibraryModulesImpl( BasicManager* pMgr,
229*cdf0e10cSrcweir 	Reference< XNameAccess > xLibNameAccess, ::rtl::OUString aLibName )
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aModuleNames = xLibNameAccess->getElementNames();
232*cdf0e10cSrcweir 	sal_Int32 nModuleCount = aModuleNames.getLength();
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	StarBASIC* pLib = pMgr->GetLib( aLibName );
235*cdf0e10cSrcweir 	DBG_ASSERT( pLib, "BasMgrContainerListenerImpl::addLibraryModulesImpl: Unknown lib!");
236*cdf0e10cSrcweir 	if( pLib )
237*cdf0e10cSrcweir 	{
238*cdf0e10cSrcweir 		const ::rtl::OUString* pNames = aModuleNames.getConstArray();
239*cdf0e10cSrcweir 		for( sal_Int32 j = 0 ; j < nModuleCount ; j++ )
240*cdf0e10cSrcweir 		{
241*cdf0e10cSrcweir 			::rtl::OUString aModuleName = pNames[ j ];
242*cdf0e10cSrcweir 			Any aElement = xLibNameAccess->getByName( aModuleName );
243*cdf0e10cSrcweir             ::rtl::OUString aMod;
244*cdf0e10cSrcweir             aElement >>= aMod;
245*cdf0e10cSrcweir             Reference< vba::XVBAModuleInfo > xVBAModuleInfo( xLibNameAccess, UNO_QUERY );
246*cdf0e10cSrcweir             if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( aModuleName ) )
247*cdf0e10cSrcweir 			{
248*cdf0e10cSrcweir                 ModuleInfo mInfo = xVBAModuleInfo->getModuleInfo( aModuleName );
249*cdf0e10cSrcweir 				OSL_TRACE("#addLibraryModulesImpl - aMod");
250*cdf0e10cSrcweir                 pLib->MakeModule32( aModuleName, mInfo, aMod );
251*cdf0e10cSrcweir 			}
252*cdf0e10cSrcweir 			else
253*cdf0e10cSrcweir 		pLib->MakeModule32( aModuleName, aMod );
254*cdf0e10cSrcweir 		}
255*cdf0e10cSrcweir 	}
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 	pLib->SetModified( sal_False );
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir // XEventListener
263*cdf0e10cSrcweir //----------------------------------------------------------------------------
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir void SAL_CALL BasMgrContainerListenerImpl::disposing( const  EventObject& Source )
266*cdf0e10cSrcweir 	throw( RuntimeException )
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir     (void)Source;
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir // XContainerListener
272*cdf0e10cSrcweir //----------------------------------------------------------------------------
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir void SAL_CALL BasMgrContainerListenerImpl::elementInserted( const ContainerEvent& Event )
275*cdf0e10cSrcweir 	throw( RuntimeException )
276*cdf0e10cSrcweir {
277*cdf0e10cSrcweir 	sal_Bool bLibContainer = ( maLibName.getLength() == 0 );
278*cdf0e10cSrcweir 	::rtl::OUString aName;
279*cdf0e10cSrcweir 	Event.Accessor >>= aName;
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     mpMgr->mpImpl->mbModifiedByLibraryContainer = sal_True;
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 	if( bLibContainer )
284*cdf0e10cSrcweir 	{
285*cdf0e10cSrcweir         Reference< XLibraryContainer > xScriptCont( Event.Source, UNO_QUERY );
286*cdf0e10cSrcweir 		insertLibraryImpl( xScriptCont, mpMgr, Event.Element, aName );
287*cdf0e10cSrcweir         StarBASIC* pLib = mpMgr->GetLib( aName );
288*cdf0e10cSrcweir         if ( pLib )
289*cdf0e10cSrcweir         {
290*cdf0e10cSrcweir             Reference< vba::XVBACompatibility > xVBACompat( xScriptCont, UNO_QUERY );
291*cdf0e10cSrcweir             if ( xVBACompat.is() )
292*cdf0e10cSrcweir                 pLib->SetVBAEnabled( xVBACompat->getVBACompatibilityMode() );
293*cdf0e10cSrcweir         }
294*cdf0e10cSrcweir 	}
295*cdf0e10cSrcweir 	else
296*cdf0e10cSrcweir 	{
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 		StarBASIC* pLib = mpMgr->GetLib( maLibName );
299*cdf0e10cSrcweir 		DBG_ASSERT( pLib, "BasMgrContainerListenerImpl::elementInserted: Unknown lib!");
300*cdf0e10cSrcweir 		if( pLib )
301*cdf0e10cSrcweir 		{
302*cdf0e10cSrcweir     		SbModule* pMod = pLib->FindModule( aName );
303*cdf0e10cSrcweir             if( !pMod )
304*cdf0e10cSrcweir             {
305*cdf0e10cSrcweir         	::rtl::OUString aMod;
306*cdf0e10cSrcweir         	Event.Element >>= aMod;
307*cdf0e10cSrcweir                 Reference< vba::XVBAModuleInfo > xVBAModuleInfo( Event.Source, UNO_QUERY );
308*cdf0e10cSrcweir                 if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( aName ) )
309*cdf0e10cSrcweir                 {
310*cdf0e10cSrcweir                     ModuleInfo mInfo = xVBAModuleInfo->getModuleInfo( aName );
311*cdf0e10cSrcweir                     pLib->MakeModule32( aName, mInfo, aMod );
312*cdf0e10cSrcweir                 }
313*cdf0e10cSrcweir                 else
314*cdf0e10cSrcweir 			        pLib->MakeModule32( aName, aMod );
315*cdf0e10cSrcweir 			    pLib->SetModified( sal_False );
316*cdf0e10cSrcweir             }
317*cdf0e10cSrcweir 		}
318*cdf0e10cSrcweir 	}
319*cdf0e10cSrcweir }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir //----------------------------------------------------------------------------
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir void SAL_CALL BasMgrContainerListenerImpl::elementReplaced( const ContainerEvent& Event )
324*cdf0e10cSrcweir 	throw( RuntimeException )
325*cdf0e10cSrcweir {
326*cdf0e10cSrcweir 	::rtl::OUString aName;
327*cdf0e10cSrcweir 	Event.Accessor >>= aName;
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     mpMgr->mpImpl->mbModifiedByLibraryContainer = sal_True;
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir 	// Replace not possible for library container
332*cdf0e10cSrcweir #ifdef DBG_UTIL
333*cdf0e10cSrcweir 	sal_Bool bLibContainer = ( maLibName.getLength() == 0 );
334*cdf0e10cSrcweir #endif
335*cdf0e10cSrcweir     DBG_ASSERT( !bLibContainer, "library container fired elementReplaced()");
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir 	StarBASIC* pLib = mpMgr->GetLib( maLibName );
338*cdf0e10cSrcweir 	if( pLib )
339*cdf0e10cSrcweir 	{
340*cdf0e10cSrcweir 		SbModule* pMod = pLib->FindModule( aName );
341*cdf0e10cSrcweir 		::rtl::OUString aMod;
342*cdf0e10cSrcweir         Event.Element >>= aMod;
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir 		if( pMod )
345*cdf0e10cSrcweir                 pMod->SetSource32( aMod );
346*cdf0e10cSrcweir 		else
347*cdf0e10cSrcweir 				pLib->MakeModule32( aName, aMod );
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir 		pLib->SetModified( sal_False );
350*cdf0e10cSrcweir 	}
351*cdf0e10cSrcweir }
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir //----------------------------------------------------------------------------
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir void SAL_CALL BasMgrContainerListenerImpl::elementRemoved( const ContainerEvent& Event )
356*cdf0e10cSrcweir 	throw( RuntimeException )
357*cdf0e10cSrcweir {
358*cdf0e10cSrcweir 	::rtl::OUString aName;
359*cdf0e10cSrcweir 	Event.Accessor >>= aName;
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     mpMgr->mpImpl->mbModifiedByLibraryContainer = sal_True;
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir 	sal_Bool bLibContainer = ( maLibName.getLength() == 0 );
364*cdf0e10cSrcweir 	if( bLibContainer )
365*cdf0e10cSrcweir 	{
366*cdf0e10cSrcweir 		StarBASIC* pLib = mpMgr->GetLib( aName );
367*cdf0e10cSrcweir 		if( pLib )
368*cdf0e10cSrcweir 		{
369*cdf0e10cSrcweir 			sal_uInt16 nLibId = mpMgr->GetLibId( aName );
370*cdf0e10cSrcweir 			mpMgr->RemoveLib( nLibId, sal_False );
371*cdf0e10cSrcweir 		}
372*cdf0e10cSrcweir 	}
373*cdf0e10cSrcweir 	else
374*cdf0e10cSrcweir 	{
375*cdf0e10cSrcweir 		StarBASIC* pLib = mpMgr->GetLib( maLibName );
376*cdf0e10cSrcweir 		SbModule* pMod = pLib ? pLib->FindModule( aName ) : NULL;
377*cdf0e10cSrcweir 		if( pMod )
378*cdf0e10cSrcweir 		{
379*cdf0e10cSrcweir 			pLib->Remove( pMod );
380*cdf0e10cSrcweir 			pLib->SetModified( sal_False );
381*cdf0e10cSrcweir 		}
382*cdf0e10cSrcweir 	}
383*cdf0e10cSrcweir }
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir //=====================================================================
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir class BasicErrorManager
389*cdf0e10cSrcweir {
390*cdf0e10cSrcweir private:
391*cdf0e10cSrcweir 	BasErrorLst	aErrorList;
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir public:
394*cdf0e10cSrcweir 				~BasicErrorManager();
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir 	void		Reset();
397*cdf0e10cSrcweir 	void		InsertError( const BasicError& rError );
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir 	sal_Bool		HasErrors()			{ return (sal_Bool)aErrorList.Count(); }
400*cdf0e10cSrcweir 	BasicError*	GetFirstError()		{ return aErrorList.First(); }
401*cdf0e10cSrcweir 	BasicError*	GetNextError()		{ return aErrorList.Next(); }
402*cdf0e10cSrcweir };
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir BasicErrorManager::~BasicErrorManager()
406*cdf0e10cSrcweir {
407*cdf0e10cSrcweir 	Reset();
408*cdf0e10cSrcweir }
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir void BasicErrorManager::Reset()
411*cdf0e10cSrcweir {
412*cdf0e10cSrcweir 	BasicError* pError = (BasicError*)aErrorList.First();
413*cdf0e10cSrcweir 	while ( pError )
414*cdf0e10cSrcweir 	{
415*cdf0e10cSrcweir 		delete pError;
416*cdf0e10cSrcweir 		pError = (BasicError*)aErrorList.Next();
417*cdf0e10cSrcweir 	}
418*cdf0e10cSrcweir 	aErrorList.Clear();
419*cdf0e10cSrcweir }
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir void BasicErrorManager::InsertError( const BasicError& rError )
422*cdf0e10cSrcweir {
423*cdf0e10cSrcweir 	aErrorList.Insert( new BasicError( rError ), LIST_APPEND );
424*cdf0e10cSrcweir }
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir BasicError::BasicError()
428*cdf0e10cSrcweir {
429*cdf0e10cSrcweir 	nErrorId	= 0;
430*cdf0e10cSrcweir 	nReason 	= 0;
431*cdf0e10cSrcweir }
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir BasicError::BasicError( sal_uIntPtr nId, sal_uInt16 nR, const String& rErrStr ) :
434*cdf0e10cSrcweir 	aErrStr( rErrStr )
435*cdf0e10cSrcweir {
436*cdf0e10cSrcweir 	nErrorId 	= nId;
437*cdf0e10cSrcweir 	nReason 	= nR;
438*cdf0e10cSrcweir }
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir BasicError::BasicError( const BasicError& rErr ) :
441*cdf0e10cSrcweir 	aErrStr( rErr.aErrStr )
442*cdf0e10cSrcweir {
443*cdf0e10cSrcweir 	nErrorId 	= rErr.nErrorId;
444*cdf0e10cSrcweir 	nReason		= rErr.nReason;
445*cdf0e10cSrcweir }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir class BasicLibInfo
449*cdf0e10cSrcweir {
450*cdf0e10cSrcweir private:
451*cdf0e10cSrcweir 	StarBASICRef	xLib;
452*cdf0e10cSrcweir 	String			aLibName;
453*cdf0e10cSrcweir 	String			aStorageName;	// String is sufficient, unique at runtime
454*cdf0e10cSrcweir 	String			aRelStorageName;
455*cdf0e10cSrcweir 	String			aPassword;
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 	sal_Bool			bDoLoad;
458*cdf0e10cSrcweir 	sal_Bool			bReference;
459*cdf0e10cSrcweir 	sal_Bool			bPasswordVerified;
460*cdf0e10cSrcweir 	sal_Bool			bFoundInPath;	// Must not relativated again!
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir     // Lib represents library in new UNO library container
463*cdf0e10cSrcweir     Reference< XLibraryContainer > mxScriptCont;
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir public:
466*cdf0e10cSrcweir 	BasicLibInfo();
467*cdf0e10cSrcweir 	BasicLibInfo( const String& rStorageName );
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir 	sal_Bool			IsReference() const		{ return bReference; }
470*cdf0e10cSrcweir 	sal_Bool&			IsReference()			{ return bReference; }
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir 	sal_Bool			IsExtern() const 		{ return ! aStorageName.EqualsAscii(szImbedded); }
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir 	void			SetStorageName( const String& rName )	{ aStorageName = rName; }
475*cdf0e10cSrcweir 	const String&	GetStorageName() const					{ return aStorageName; }
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir 	void			SetRelStorageName( const String& rN )	{ aRelStorageName = rN; }
478*cdf0e10cSrcweir 	const String&	GetRelStorageName()	const				{ return aRelStorageName; }
479*cdf0e10cSrcweir 	void			CalcRelStorageName( const String& rMgrStorageName );
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir 	StarBASICRef	GetLib() const
482*cdf0e10cSrcweir     {
483*cdf0e10cSrcweir         if( mxScriptCont.is() && mxScriptCont->hasByName( aLibName ) &&
484*cdf0e10cSrcweir             !mxScriptCont->isLibraryLoaded( aLibName ) )
485*cdf0e10cSrcweir                 return StarBASICRef();
486*cdf0e10cSrcweir         return xLib;
487*cdf0e10cSrcweir     }
488*cdf0e10cSrcweir 	StarBASICRef&	GetLibRef()							{ return xLib; }
489*cdf0e10cSrcweir 	void			SetLib( StarBASIC* pBasic )			{ xLib = pBasic; }
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir 	const String&	GetLibName() const					{ return aLibName; }
492*cdf0e10cSrcweir 	void			SetLibName( const String& rName )	{ aLibName = rName; }
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir 	// Only temporary for Load/Save
495*cdf0e10cSrcweir 	sal_Bool			DoLoad()							{ return bDoLoad; }
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir 	sal_Bool			HasPassword() const 				{ return aPassword.Len() != 0; }
498*cdf0e10cSrcweir 	const String&	GetPassword() const					{ return aPassword; }
499*cdf0e10cSrcweir 	void			SetPassword( const String& rNewPassword )
500*cdf0e10cSrcweir 														{ aPassword = rNewPassword;	}
501*cdf0e10cSrcweir 	sal_Bool			IsPasswordVerified() const			{ return bPasswordVerified; }
502*cdf0e10cSrcweir 	void			SetPasswordVerified()				{ bPasswordVerified = sal_True; }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir 	sal_Bool			IsFoundInPath() const				{ return bFoundInPath; }
505*cdf0e10cSrcweir 	void			SetFoundInPath( sal_Bool bInPath )		{ bFoundInPath = bInPath; }
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir 	void 					Store( SotStorageStream& rSStream, const String& rBasMgrStorageName, sal_Bool bUseOldReloadInfo );
508*cdf0e10cSrcweir 	static BasicLibInfo*	Create( SotStorageStream& rSStream );
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir     Reference< XLibraryContainer > GetLibraryContainer( void )
511*cdf0e10cSrcweir         { return mxScriptCont; }
512*cdf0e10cSrcweir     void SetLibraryContainer( const Reference< XLibraryContainer >& xScriptCont )
513*cdf0e10cSrcweir         { mxScriptCont = xScriptCont; }
514*cdf0e10cSrcweir };
515*cdf0e10cSrcweir 
516*cdf0e10cSrcweir DECLARE_LIST( BasicLibsBase, BasicLibInfo* )
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir class BasicLibs : public BasicLibsBase
519*cdf0e10cSrcweir {
520*cdf0e10cSrcweir public:
521*cdf0e10cSrcweir 	String	aBasicLibPath; // TODO: Should be member of manager, but currently not incompatible
522*cdf0e10cSrcweir };
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir BasicLibInfo::BasicLibInfo()
525*cdf0e10cSrcweir {
526*cdf0e10cSrcweir 	bReference 			= sal_False;
527*cdf0e10cSrcweir 	bPasswordVerified 	= sal_False;
528*cdf0e10cSrcweir 	bDoLoad 			= sal_False;
529*cdf0e10cSrcweir 	bFoundInPath		= sal_False;
530*cdf0e10cSrcweir     mxScriptCont    	= NULL;
531*cdf0e10cSrcweir 	aStorageName 		= String::CreateFromAscii(szImbedded);
532*cdf0e10cSrcweir 	aRelStorageName 	= String::CreateFromAscii(szImbedded);
533*cdf0e10cSrcweir }
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir BasicLibInfo::BasicLibInfo( const String& rStorageName )
536*cdf0e10cSrcweir {
537*cdf0e10cSrcweir 	bReference 			= sal_True;
538*cdf0e10cSrcweir 	bPasswordVerified 	= sal_False;
539*cdf0e10cSrcweir 	bDoLoad 			= sal_False;
540*cdf0e10cSrcweir     mxScriptCont    	= NULL;
541*cdf0e10cSrcweir 	aStorageName 		= rStorageName;
542*cdf0e10cSrcweir }
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir void BasicLibInfo::Store( SotStorageStream& rSStream, const String& rBasMgrStorageName, sal_Bool bUseOldReloadInfo )
545*cdf0e10cSrcweir {
546*cdf0e10cSrcweir 	sal_uIntPtr nStartPos = rSStream.Tell();
547*cdf0e10cSrcweir 	sal_uInt32 nEndPos = 0;
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 	sal_uInt16 nId = LIBINFO_ID;
550*cdf0e10cSrcweir 	sal_uInt16 nVer = CURR_VER;
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir 	rSStream << nEndPos;
553*cdf0e10cSrcweir 	rSStream << nId;
554*cdf0e10cSrcweir 	rSStream << nVer;
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir     String aCurStorageName = INetURLObject(rBasMgrStorageName, INET_PROT_FILE).GetMainURL( INetURLObject::NO_DECODE );
557*cdf0e10cSrcweir     DBG_ASSERT(aCurStorageName.Len() != 0, "Bad storage name");
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir     // If not set initialize StorageName
560*cdf0e10cSrcweir     if ( aStorageName.Len() == 0 )
561*cdf0e10cSrcweir         aStorageName = aCurStorageName;
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir     // Load again?
564*cdf0e10cSrcweir     sal_Bool bDoLoad_ = xLib.Is();
565*cdf0e10cSrcweir     if ( bUseOldReloadInfo )
566*cdf0e10cSrcweir         bDoLoad_ = DoLoad();
567*cdf0e10cSrcweir     rSStream << bDoLoad_;
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir     // The name of the lib...
570*cdf0e10cSrcweir     rSStream.WriteByteString(GetLibName());
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir     // Absolute path...
573*cdf0e10cSrcweir     if ( ! GetStorageName().EqualsAscii(szImbedded) )
574*cdf0e10cSrcweir     {
575*cdf0e10cSrcweir         String aSName = INetURLObject( GetStorageName(), INET_PROT_FILE).GetMainURL( INetURLObject::NO_DECODE );
576*cdf0e10cSrcweir         DBG_ASSERT(aSName.Len() != 0, "Bad storage name");
577*cdf0e10cSrcweir         rSStream.WriteByteString( aSName );
578*cdf0e10cSrcweir     }
579*cdf0e10cSrcweir     else
580*cdf0e10cSrcweir         rSStream.WriteByteString( szImbedded );
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir     // Relative path...
583*cdf0e10cSrcweir     if ( ( aStorageName == aCurStorageName ) || ( aStorageName.EqualsAscii(szImbedded) ) )
584*cdf0e10cSrcweir         rSStream.WriteByteString( szImbedded );
585*cdf0e10cSrcweir     else
586*cdf0e10cSrcweir     {
587*cdf0e10cSrcweir         // Do not determine the relative path if the file was only found in path:
588*cdf0e10cSrcweir         // because the relative path would change and after moving the lib the
589*cdf0e10cSrcweir         // the file cannot be found.
590*cdf0e10cSrcweir         if ( !IsFoundInPath() )
591*cdf0e10cSrcweir             CalcRelStorageName( aCurStorageName );
592*cdf0e10cSrcweir         rSStream.WriteByteString(aRelStorageName);
593*cdf0e10cSrcweir     }
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir 	// ------------------------------
596*cdf0e10cSrcweir 	// Version 2
597*cdf0e10cSrcweir 	// ------------------------------
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir     // reference...
600*cdf0e10cSrcweir     rSStream << bReference;
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir 	// ------------------------------
603*cdf0e10cSrcweir 	// End
604*cdf0e10cSrcweir 	// ------------------------------
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir 	nEndPos = rSStream.Tell();
607*cdf0e10cSrcweir 	rSStream.Seek( nStartPos );
608*cdf0e10cSrcweir 	rSStream << nEndPos;
609*cdf0e10cSrcweir 	rSStream.Seek( nEndPos );
610*cdf0e10cSrcweir }
611*cdf0e10cSrcweir 
612*cdf0e10cSrcweir BasicLibInfo* BasicLibInfo::Create( SotStorageStream& rSStream )
613*cdf0e10cSrcweir {
614*cdf0e10cSrcweir 	BasicLibInfo* pInfo = new BasicLibInfo;
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir 	sal_uInt32 nEndPos;
617*cdf0e10cSrcweir 	sal_uInt16 nId;
618*cdf0e10cSrcweir 	sal_uInt16 nVer;
619*cdf0e10cSrcweir 
620*cdf0e10cSrcweir 	rSStream >> nEndPos;
621*cdf0e10cSrcweir 	rSStream >> nId;
622*cdf0e10cSrcweir 	rSStream >> nVer;
623*cdf0e10cSrcweir 
624*cdf0e10cSrcweir 	DBG_ASSERT( nId == LIBINFO_ID, "Keine BasicLibInfo !?" );
625*cdf0e10cSrcweir     if( nId == LIBINFO_ID )
626*cdf0e10cSrcweir     {
627*cdf0e10cSrcweir         // Reload?
628*cdf0e10cSrcweir         sal_Bool bDoLoad;
629*cdf0e10cSrcweir         rSStream >> bDoLoad;
630*cdf0e10cSrcweir         pInfo->bDoLoad = bDoLoad;
631*cdf0e10cSrcweir 
632*cdf0e10cSrcweir         // The name of the lib...
633*cdf0e10cSrcweir         String aName;
634*cdf0e10cSrcweir         rSStream.ReadByteString(aName);
635*cdf0e10cSrcweir         pInfo->SetLibName( aName );
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir         // Absolute path...
638*cdf0e10cSrcweir         String aStorageName;
639*cdf0e10cSrcweir         rSStream.ReadByteString(aStorageName);
640*cdf0e10cSrcweir         pInfo->SetStorageName( aStorageName );
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir         // Relative path...
643*cdf0e10cSrcweir         String aRelStorageName;
644*cdf0e10cSrcweir         rSStream.ReadByteString(aRelStorageName);
645*cdf0e10cSrcweir         pInfo->SetRelStorageName( aRelStorageName );
646*cdf0e10cSrcweir 
647*cdf0e10cSrcweir         if ( nVer >= 2 )
648*cdf0e10cSrcweir         {
649*cdf0e10cSrcweir             sal_Bool bReferenz;
650*cdf0e10cSrcweir             rSStream >> bReferenz;
651*cdf0e10cSrcweir             pInfo->IsReference() = bReferenz;
652*cdf0e10cSrcweir         }
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir         rSStream.Seek( nEndPos );
655*cdf0e10cSrcweir     }
656*cdf0e10cSrcweir     return pInfo;
657*cdf0e10cSrcweir }
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir void BasicLibInfo::CalcRelStorageName( const String& rMgrStorageName )
660*cdf0e10cSrcweir {
661*cdf0e10cSrcweir 	if ( rMgrStorageName.Len() )
662*cdf0e10cSrcweir 	{
663*cdf0e10cSrcweir         INetURLObject aAbsURLObj( rMgrStorageName );
664*cdf0e10cSrcweir 		aAbsURLObj.removeSegment();
665*cdf0e10cSrcweir 		String aPath = aAbsURLObj.GetMainURL( INetURLObject::NO_DECODE );
666*cdf0e10cSrcweir 		UniString aRelURL = INetURLObject::GetRelURL( aPath, GetStorageName() );
667*cdf0e10cSrcweir         SetRelStorageName( aRelURL );
668*cdf0e10cSrcweir 	}
669*cdf0e10cSrcweir 	else
670*cdf0e10cSrcweir 		SetRelStorageName( String() );
671*cdf0e10cSrcweir }
672*cdf0e10cSrcweir BasicManager::BasicManager( SotStorage& rStorage, const String& rBaseURL, StarBASIC* pParentFromStdLib, String* pLibPath, sal_Bool bDocMgr ) : mbDocMgr( bDocMgr )
673*cdf0e10cSrcweir {
674*cdf0e10cSrcweir 	DBG_CTOR( BasicManager, 0 );
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir 	Init();
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir 	if( pLibPath )
679*cdf0e10cSrcweir 		pLibs->aBasicLibPath = *pLibPath;
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir     String aStorName( rStorage.GetName() );
682*cdf0e10cSrcweir     maStorageName = INetURLObject(aStorName, INET_PROT_FILE).GetMainURL( INetURLObject::NO_DECODE );
683*cdf0e10cSrcweir 
684*cdf0e10cSrcweir     // #91251: Storage name not longer available for documents < 5.0
685*cdf0e10cSrcweir     // Should be no real problem, because only relative storage names
686*cdf0e10cSrcweir     // (links) can be affected.
687*cdf0e10cSrcweir     // DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
688*cdf0e10cSrcweir     // DBG_ASSERT(aStorageName.Len() != 0, "Bad storage name");
689*cdf0e10cSrcweir 
690*cdf0e10cSrcweir     // If there is no Manager Stream, no further actions are necessary
691*cdf0e10cSrcweir     if ( rStorage.IsStream( String(RTL_CONSTASCII_USTRINGPARAM(szManagerStream)) ) )
692*cdf0e10cSrcweir     {
693*cdf0e10cSrcweir         LoadBasicManager( rStorage, rBaseURL );
694*cdf0e10cSrcweir         // StdLib contains Parent:
695*cdf0e10cSrcweir         StarBASIC* pStdLib = GetStdLib();
696*cdf0e10cSrcweir         DBG_ASSERT( pStdLib, "Standard-Lib not loaded?" );
697*cdf0e10cSrcweir         if ( !pStdLib )
698*cdf0e10cSrcweir         {
699*cdf0e10cSrcweir             // Should never happen, but if it happens we wont crash...
700*cdf0e10cSrcweir             pStdLib = new StarBASIC( NULL, mbDocMgr );
701*cdf0e10cSrcweir 			BasicLibInfo* pStdLibInfo = pLibs->GetObject( 0 );
702*cdf0e10cSrcweir 			if ( !pStdLibInfo )
703*cdf0e10cSrcweir 				pStdLibInfo = CreateLibInfo();
704*cdf0e10cSrcweir 			pStdLibInfo->SetLib( pStdLib );
705*cdf0e10cSrcweir             StarBASICRef xStdLib = pStdLibInfo->GetLib();
706*cdf0e10cSrcweir 			xStdLib->SetName( String::CreateFromAscii(szStdLibName) );
707*cdf0e10cSrcweir 			pStdLibInfo->SetLibName( String::CreateFromAscii(szStdLibName) );
708*cdf0e10cSrcweir 			xStdLib->SetFlag( SBX_DONTSTORE | SBX_EXTSEARCH );
709*cdf0e10cSrcweir 			xStdLib->SetModified( sal_False );
710*cdf0e10cSrcweir         }
711*cdf0e10cSrcweir         else
712*cdf0e10cSrcweir         {
713*cdf0e10cSrcweir 			pStdLib->SetParent( pParentFromStdLib );
714*cdf0e10cSrcweir             // The other get StdLib as parent:
715*cdf0e10cSrcweir             for ( sal_uInt16 nBasic = 1; nBasic < GetLibCount(); nBasic++ )
716*cdf0e10cSrcweir 			{
717*cdf0e10cSrcweir 				StarBASIC* pBasic = GetLib( nBasic );
718*cdf0e10cSrcweir 				if ( pBasic )
719*cdf0e10cSrcweir 				{
720*cdf0e10cSrcweir //					pBasic->SetParent( pStdLib );
721*cdf0e10cSrcweir 					pStdLib->Insert( pBasic );
722*cdf0e10cSrcweir 					pBasic->SetFlag( SBX_EXTSEARCH );
723*cdf0e10cSrcweir 				}
724*cdf0e10cSrcweir 			}
725*cdf0e10cSrcweir             // Modified through insert
726*cdf0e10cSrcweir             pStdLib->SetModified( sal_False );
727*cdf0e10cSrcweir         }
728*cdf0e10cSrcweir 
729*cdf0e10cSrcweir         // #91626 Save all stream data to save it unmodified if basic isn't modified
730*cdf0e10cSrcweir         // in an 6.0+ office. So also the old basic dialogs can be saved.
731*cdf0e10cSrcweir 	    SotStorageStreamRef xManagerStream = rStorage.OpenSotStream
732*cdf0e10cSrcweir 		    ( String(RTL_CONSTASCII_USTRINGPARAM(szManagerStream)), eStreamReadMode );
733*cdf0e10cSrcweir         mpImpl->mpManagerStream = new SvMemoryStream();
734*cdf0e10cSrcweir         *static_cast<SvStream*>(&xManagerStream) >> *mpImpl->mpManagerStream;
735*cdf0e10cSrcweir 
736*cdf0e10cSrcweir 	    SotStorageRef xBasicStorage = rStorage.OpenSotStorage
737*cdf0e10cSrcweir 							    ( String(RTL_CONSTASCII_USTRINGPARAM(szBasicStorage)), eStorageReadMode, sal_False );
738*cdf0e10cSrcweir 	    if( xBasicStorage.Is() && !xBasicStorage->GetError() )
739*cdf0e10cSrcweir 	    {
740*cdf0e10cSrcweir 	        sal_uInt16 nLibs = GetLibCount();
741*cdf0e10cSrcweir             mpImpl->mppLibStreams = new SvMemoryStream*[ nLibs ];
742*cdf0e10cSrcweir             for( sal_uInt16 nL = 0; nL < nLibs; nL++ )
743*cdf0e10cSrcweir 	        {
744*cdf0e10cSrcweir 		        BasicLibInfo* pInfo = pLibs->GetObject( nL );
745*cdf0e10cSrcweir 		        DBG_ASSERT( pInfo, "pInfo?!" );
746*cdf0e10cSrcweir     		    SotStorageStreamRef xBasicStream = xBasicStorage->OpenSotStream( pInfo->GetLibName(), eStreamReadMode );
747*cdf0e10cSrcweir                 mpImpl->mppLibStreams[nL] = new SvMemoryStream();
748*cdf0e10cSrcweir                 *static_cast<SvStream*>(&xBasicStream) >> *( mpImpl->mppLibStreams[nL] );
749*cdf0e10cSrcweir             }
750*cdf0e10cSrcweir         }
751*cdf0e10cSrcweir         else
752*cdf0e10cSrcweir             mpImpl->mbError = sal_True;
753*cdf0e10cSrcweir 	}
754*cdf0e10cSrcweir 	else
755*cdf0e10cSrcweir 	{
756*cdf0e10cSrcweir 		ImpCreateStdLib( pParentFromStdLib );
757*cdf0e10cSrcweir 		if ( rStorage.IsStream( String::CreateFromAscii(szOldManagerStream) ) )
758*cdf0e10cSrcweir             LoadOldBasicManager( rStorage );
759*cdf0e10cSrcweir 	}
760*cdf0e10cSrcweir 
761*cdf0e10cSrcweir 	bBasMgrModified = sal_False;
762*cdf0e10cSrcweir }
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir void copyToLibraryContainer( StarBASIC* pBasic, const LibraryContainerInfo& rInfo )
765*cdf0e10cSrcweir {
766*cdf0e10cSrcweir     Reference< XLibraryContainer > xScriptCont( rInfo.mxScriptCont.get() );
767*cdf0e10cSrcweir     if ( !xScriptCont.is() )
768*cdf0e10cSrcweir         return;
769*cdf0e10cSrcweir 
770*cdf0e10cSrcweir     String aLibName = pBasic->GetName();
771*cdf0e10cSrcweir 	if( !xScriptCont->hasByName( aLibName ) )
772*cdf0e10cSrcweir 		xScriptCont->createLibrary( aLibName );
773*cdf0e10cSrcweir 
774*cdf0e10cSrcweir 	Any aLibAny = xScriptCont->getByName( aLibName );
775*cdf0e10cSrcweir 	Reference< XNameContainer > xLib;
776*cdf0e10cSrcweir 	aLibAny >>= xLib;
777*cdf0e10cSrcweir     if ( !xLib.is() )
778*cdf0e10cSrcweir         return;
779*cdf0e10cSrcweir 
780*cdf0e10cSrcweir     sal_uInt16 nModCount = pBasic->GetModules()->Count();
781*cdf0e10cSrcweir 	for ( sal_uInt16 nMod = 0 ; nMod < nModCount ; nMod++ )
782*cdf0e10cSrcweir 	{
783*cdf0e10cSrcweir 		SbModule* pModule = (SbModule*)pBasic->GetModules()->Get( nMod );
784*cdf0e10cSrcweir 		DBG_ASSERT( pModule, "Modul nicht erhalten!" );
785*cdf0e10cSrcweir 
786*cdf0e10cSrcweir 		String aModName = pModule->GetName();
787*cdf0e10cSrcweir 		if( !xLib->hasByName( aModName ) )
788*cdf0e10cSrcweir 		{
789*cdf0e10cSrcweir 			::rtl::OUString aSource = pModule->GetSource32();
790*cdf0e10cSrcweir 			Any aSourceAny;
791*cdf0e10cSrcweir 			aSourceAny <<= aSource;
792*cdf0e10cSrcweir 			xLib->insertByName( aModName, aSourceAny );
793*cdf0e10cSrcweir 		}
794*cdf0e10cSrcweir 	}
795*cdf0e10cSrcweir }
796*cdf0e10cSrcweir 
797*cdf0e10cSrcweir const Reference< XPersistentLibraryContainer >& BasicManager::GetDialogLibraryContainer()  const
798*cdf0e10cSrcweir {
799*cdf0e10cSrcweir     return mpImpl->maContainerInfo.mxDialogCont;
800*cdf0e10cSrcweir }
801*cdf0e10cSrcweir 
802*cdf0e10cSrcweir const Reference< XPersistentLibraryContainer >& BasicManager::GetScriptLibraryContainer()  const
803*cdf0e10cSrcweir {
804*cdf0e10cSrcweir     return mpImpl->maContainerInfo.mxScriptCont;
805*cdf0e10cSrcweir }
806*cdf0e10cSrcweir 
807*cdf0e10cSrcweir void BasicManager::SetLibraryContainerInfo( const LibraryContainerInfo& rInfo )
808*cdf0e10cSrcweir {
809*cdf0e10cSrcweir 	mpImpl->maContainerInfo = rInfo;
810*cdf0e10cSrcweir 
811*cdf0e10cSrcweir 	Reference< XLibraryContainer > xScriptCont( mpImpl->maContainerInfo.mxScriptCont.get() );
812*cdf0e10cSrcweir 	StarBASIC* pStdLib = GetStdLib();
813*cdf0e10cSrcweir 	String aLibName = pStdLib->GetName();
814*cdf0e10cSrcweir 	if( xScriptCont.is() )
815*cdf0e10cSrcweir 	{
816*cdf0e10cSrcweir 		// Register listener for lib container
817*cdf0e10cSrcweir 		::rtl::OUString aEmptyLibName;
818*cdf0e10cSrcweir 		Reference< XContainerListener > xLibContainerListener
819*cdf0e10cSrcweir 			= static_cast< XContainerListener* >
820*cdf0e10cSrcweir 				( new BasMgrContainerListenerImpl( this, aEmptyLibName ) );
821*cdf0e10cSrcweir 
822*cdf0e10cSrcweir     	Reference< XContainer> xLibContainer( xScriptCont, UNO_QUERY );
823*cdf0e10cSrcweir 		xLibContainer->addContainerListener( xLibContainerListener );
824*cdf0e10cSrcweir 
825*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aScriptLibNames = xScriptCont->getElementNames();
826*cdf0e10cSrcweir 		const ::rtl::OUString* pScriptLibName = aScriptLibNames.getConstArray();
827*cdf0e10cSrcweir 		sal_Int32 i, nNameCount = aScriptLibNames.getLength();
828*cdf0e10cSrcweir 
829*cdf0e10cSrcweir         if( nNameCount )
830*cdf0e10cSrcweir         {
831*cdf0e10cSrcweir 		    for( i = 0 ; i < nNameCount ; ++i, ++pScriptLibName )
832*cdf0e10cSrcweir 		    {
833*cdf0e10cSrcweir 			    Any aLibAny = xScriptCont->getByName( *pScriptLibName );
834*cdf0e10cSrcweir 
835*cdf0e10cSrcweir                 if ( pScriptLibName->equalsAscii( "Standard" ) )
836*cdf0e10cSrcweir 				    xScriptCont->loadLibrary( *pScriptLibName );
837*cdf0e10cSrcweir 
838*cdf0e10cSrcweir 			    BasMgrContainerListenerImpl::insertLibraryImpl
839*cdf0e10cSrcweir 				    ( xScriptCont, this, aLibAny, *pScriptLibName );
840*cdf0e10cSrcweir 		    }
841*cdf0e10cSrcweir         }
842*cdf0e10cSrcweir         else
843*cdf0e10cSrcweir         {
844*cdf0e10cSrcweir             // No libs? Maybe an 5.2 document already loaded
845*cdf0e10cSrcweir 	        sal_uInt16 nLibs = GetLibCount();
846*cdf0e10cSrcweir 		    for( sal_uInt16 nL = 0; nL < nLibs; nL++ )
847*cdf0e10cSrcweir 		    {
848*cdf0e10cSrcweir 			    BasicLibInfo* pBasLibInfo = pLibs->GetObject( nL );
849*cdf0e10cSrcweir 			    StarBASIC* pLib = pBasLibInfo->GetLib();
850*cdf0e10cSrcweir 			    if( !pLib )
851*cdf0e10cSrcweir                 {
852*cdf0e10cSrcweir                     sal_Bool bLoaded = ImpLoadLibary( pBasLibInfo, NULL, sal_False );
853*cdf0e10cSrcweir                     if( bLoaded )
854*cdf0e10cSrcweir                         pLib = pBasLibInfo->GetLib();
855*cdf0e10cSrcweir                 }
856*cdf0e10cSrcweir 			    if( pLib )
857*cdf0e10cSrcweir                 {
858*cdf0e10cSrcweir                     copyToLibraryContainer( pLib, mpImpl->maContainerInfo );
859*cdf0e10cSrcweir                     if( pBasLibInfo->HasPassword() )
860*cdf0e10cSrcweir                     {
861*cdf0e10cSrcweir                         OldBasicPassword* pOldBasicPassword =
862*cdf0e10cSrcweir                             mpImpl->maContainerInfo.mpOldBasicPassword;
863*cdf0e10cSrcweir                         if( pOldBasicPassword )
864*cdf0e10cSrcweir                         {
865*cdf0e10cSrcweir                             pOldBasicPassword->setLibraryPassword
866*cdf0e10cSrcweir                                 ( pLib->GetName(), pBasLibInfo->GetPassword() );
867*cdf0e10cSrcweir                             pBasLibInfo->SetPasswordVerified();
868*cdf0e10cSrcweir                         }
869*cdf0e10cSrcweir                     }
870*cdf0e10cSrcweir                 }
871*cdf0e10cSrcweir 		    }
872*cdf0e10cSrcweir 
873*cdf0e10cSrcweir             mpImpl->mbModifiedByLibraryContainer = sal_False;
874*cdf0e10cSrcweir         }
875*cdf0e10cSrcweir 	}
876*cdf0e10cSrcweir 
877*cdf0e10cSrcweir     SetGlobalUNOConstant( "BasicLibraries", makeAny( mpImpl->maContainerInfo.mxScriptCont ) );
878*cdf0e10cSrcweir     SetGlobalUNOConstant( "DialogLibraries", makeAny( mpImpl->maContainerInfo.mxDialogCont ) );
879*cdf0e10cSrcweir }
880*cdf0e10cSrcweir 
881*cdf0e10cSrcweir BasicManager::BasicManager( StarBASIC* pSLib, String* pLibPath, sal_Bool bDocMgr ) : mbDocMgr( bDocMgr )
882*cdf0e10cSrcweir {
883*cdf0e10cSrcweir 	DBG_CTOR( BasicManager, 0 );
884*cdf0e10cSrcweir 	Init();
885*cdf0e10cSrcweir     DBG_ASSERT( pSLib, "BasicManager cannot be created with a NULL-Pointer!" );
886*cdf0e10cSrcweir 
887*cdf0e10cSrcweir 	if( pLibPath )
888*cdf0e10cSrcweir 		pLibs->aBasicLibPath = *pLibPath;
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir 	BasicLibInfo* pStdLibInfo = CreateLibInfo();
891*cdf0e10cSrcweir 	pStdLibInfo->SetLib( pSLib );
892*cdf0e10cSrcweir     StarBASICRef xStdLib = pStdLibInfo->GetLib();
893*cdf0e10cSrcweir 	xStdLib->SetName( String::CreateFromAscii(szStdLibName));
894*cdf0e10cSrcweir 	pStdLibInfo->SetLibName( String::CreateFromAscii(szStdLibName) );
895*cdf0e10cSrcweir 	pSLib->SetFlag( SBX_DONTSTORE | SBX_EXTSEARCH );
896*cdf0e10cSrcweir 
897*cdf0e10cSrcweir     // Save is only necessary if basic has changed
898*cdf0e10cSrcweir 	xStdLib->SetModified( sal_False );
899*cdf0e10cSrcweir 	bBasMgrModified = sal_False;
900*cdf0e10cSrcweir }
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir BasicManager::BasicManager()
903*cdf0e10cSrcweir {
904*cdf0e10cSrcweir     DBG_CTOR( BasicManager, 0 );
905*cdf0e10cSrcweir     // This ctor may only be used to adapt relative paths for 'Save As'.
906*cdf0e10cSrcweir     // There is no AppBasic so libs must not be loaded...
907*cdf0e10cSrcweir     Init();
908*cdf0e10cSrcweir }
909*cdf0e10cSrcweir 
910*cdf0e10cSrcweir void BasicManager::ImpMgrNotLoaded( const String& rStorageName )
911*cdf0e10cSrcweir {
912*cdf0e10cSrcweir     // pErrInf is only destroyed if the error os processed by an
913*cdf0e10cSrcweir     // ErrorHandler
914*cdf0e10cSrcweir     StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_MGROPEN, rStorageName, ERRCODE_BUTTON_OK );
915*cdf0e10cSrcweir     pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_OPENMGRSTREAM, rStorageName ) );
916*cdf0e10cSrcweir 
917*cdf0e10cSrcweir     // Create a stdlib otherwise we crash!
918*cdf0e10cSrcweir     BasicLibInfo* pStdLibInfo = CreateLibInfo();
919*cdf0e10cSrcweir     pStdLibInfo->SetLib( new StarBASIC( NULL, mbDocMgr ) );
920*cdf0e10cSrcweir     StarBASICRef xStdLib = pStdLibInfo->GetLib();
921*cdf0e10cSrcweir     xStdLib->SetName( String::CreateFromAscii(szStdLibName) );
922*cdf0e10cSrcweir     pStdLibInfo->SetLibName( String::CreateFromAscii(szStdLibName) );
923*cdf0e10cSrcweir     xStdLib->SetFlag( SBX_DONTSTORE | SBX_EXTSEARCH );
924*cdf0e10cSrcweir     xStdLib->SetModified( sal_False );
925*cdf0e10cSrcweir }
926*cdf0e10cSrcweir 
927*cdf0e10cSrcweir 
928*cdf0e10cSrcweir void BasicManager::ImpCreateStdLib( StarBASIC* pParentFromStdLib )
929*cdf0e10cSrcweir {
930*cdf0e10cSrcweir 	BasicLibInfo* pStdLibInfo = CreateLibInfo();
931*cdf0e10cSrcweir 	StarBASIC* pStdLib = new StarBASIC( pParentFromStdLib, mbDocMgr );
932*cdf0e10cSrcweir 	pStdLibInfo->SetLib( pStdLib );
933*cdf0e10cSrcweir 	pStdLib->SetName( String::CreateFromAscii(szStdLibName) );
934*cdf0e10cSrcweir 	pStdLibInfo->SetLibName( String::CreateFromAscii(szStdLibName) );
935*cdf0e10cSrcweir 	pStdLib->SetFlag( SBX_DONTSTORE | SBX_EXTSEARCH );
936*cdf0e10cSrcweir }
937*cdf0e10cSrcweir 
938*cdf0e10cSrcweir 
939*cdf0e10cSrcweir void BasicManager::LoadBasicManager( SotStorage& rStorage, const String& rBaseURL, sal_Bool bLoadLibs )
940*cdf0e10cSrcweir {
941*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
942*cdf0e10cSrcweir 
943*cdf0e10cSrcweir //	StreamMode eStreamMode = STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE;
944*cdf0e10cSrcweir 
945*cdf0e10cSrcweir 	SotStorageStreamRef xManagerStream = rStorage.OpenSotStream
946*cdf0e10cSrcweir 		( String(RTL_CONSTASCII_USTRINGPARAM(szManagerStream)), eStreamReadMode );
947*cdf0e10cSrcweir 
948*cdf0e10cSrcweir     String aStorName( rStorage.GetName() );
949*cdf0e10cSrcweir     // #i13114 removed, DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
950*cdf0e10cSrcweir 
951*cdf0e10cSrcweir 	if ( !xManagerStream.Is() || xManagerStream->GetError() || ( xManagerStream->Seek( STREAM_SEEK_TO_END ) == 0 ) )
952*cdf0e10cSrcweir 	{
953*cdf0e10cSrcweir         ImpMgrNotLoaded( aStorName );
954*cdf0e10cSrcweir 		return;
955*cdf0e10cSrcweir 	}
956*cdf0e10cSrcweir 
957*cdf0e10cSrcweir     maStorageName = INetURLObject(aStorName, INET_PROT_FILE).GetMainURL( INetURLObject::NO_DECODE );
958*cdf0e10cSrcweir     // #i13114 removed, DBG_ASSERT(aStorageName.Len() != 0, "Bad storage name");
959*cdf0e10cSrcweir 
960*cdf0e10cSrcweir     String aRealStorageName = maStorageName;  // for relative paths, can be modified through BaseURL
961*cdf0e10cSrcweir 
962*cdf0e10cSrcweir     // If loaded from template, only BaseURL is used:
963*cdf0e10cSrcweir     //String aBaseURL = INetURLObject::GetBaseURL();
964*cdf0e10cSrcweir     if ( rBaseURL.Len() )
965*cdf0e10cSrcweir 	{
966*cdf0e10cSrcweir         INetURLObject aObj( rBaseURL );
967*cdf0e10cSrcweir 		if ( aObj.GetProtocol() == INET_PROT_FILE )
968*cdf0e10cSrcweir 			aRealStorageName = aObj.PathToFileName();
969*cdf0e10cSrcweir 	}
970*cdf0e10cSrcweir 
971*cdf0e10cSrcweir 	xManagerStream->SetBufferSize( 1024 );
972*cdf0e10cSrcweir 	xManagerStream->Seek( STREAM_SEEK_TO_BEGIN );
973*cdf0e10cSrcweir 
974*cdf0e10cSrcweir 	sal_uInt32 nEndPos;
975*cdf0e10cSrcweir 	*xManagerStream >> nEndPos;
976*cdf0e10cSrcweir 
977*cdf0e10cSrcweir 	sal_uInt16 nLibs;
978*cdf0e10cSrcweir 	*xManagerStream >> nLibs;
979*cdf0e10cSrcweir 	// Plausi!
980*cdf0e10cSrcweir 	if( nLibs & 0xF000 )
981*cdf0e10cSrcweir 	{
982*cdf0e10cSrcweir 		DBG_ASSERT( !this, "BasicManager-Stream defect!" );
983*cdf0e10cSrcweir 		return;
984*cdf0e10cSrcweir 	}
985*cdf0e10cSrcweir 	for ( sal_uInt16 nL = 0; nL < nLibs; nL++ )
986*cdf0e10cSrcweir 	{
987*cdf0e10cSrcweir 		BasicLibInfo* pInfo = BasicLibInfo::Create( *xManagerStream );
988*cdf0e10cSrcweir 
989*cdf0e10cSrcweir         // Correct absolute pathname if relative is existing.
990*cdf0e10cSrcweir         // Always try relative first if there are two stands on disk
991*cdf0e10cSrcweir         if ( pInfo->GetRelStorageName().Len() && ( ! pInfo->GetRelStorageName().EqualsAscii(szImbedded) ) )
992*cdf0e10cSrcweir 		{
993*cdf0e10cSrcweir             INetURLObject aObj( aRealStorageName, INET_PROT_FILE );
994*cdf0e10cSrcweir             aObj.removeSegment();
995*cdf0e10cSrcweir             bool bWasAbsolute = sal_False;
996*cdf0e10cSrcweir             aObj = aObj.smartRel2Abs( pInfo->GetRelStorageName(), bWasAbsolute );
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir             //*** TODO: Replace if still necessary
999*cdf0e10cSrcweir             /* if ( SfxContentHelper::Exists( aObj.GetMainURL() ) )
1000*cdf0e10cSrcweir                 pInfo->SetStorageName( aObj.GetMainURL() );
1001*cdf0e10cSrcweir 			else */
1002*cdf0e10cSrcweir             //*** TODO-End
1003*cdf0e10cSrcweir             if ( pLibs->aBasicLibPath.Len() )
1004*cdf0e10cSrcweir 			{
1005*cdf0e10cSrcweir                 // Search lib in path
1006*cdf0e10cSrcweir 				String aSearchFile = pInfo->GetRelStorageName();
1007*cdf0e10cSrcweir 				SvtPathOptions aPathCFG;
1008*cdf0e10cSrcweir 				if( aPathCFG.SearchFile( aSearchFile, SvtPathOptions::PATH_BASIC ) )
1009*cdf0e10cSrcweir 				{
1010*cdf0e10cSrcweir                     pInfo->SetStorageName( aSearchFile );
1011*cdf0e10cSrcweir 					pInfo->SetFoundInPath( sal_True );
1012*cdf0e10cSrcweir 				}
1013*cdf0e10cSrcweir 			}
1014*cdf0e10cSrcweir 		}
1015*cdf0e10cSrcweir 
1016*cdf0e10cSrcweir 		pLibs->Insert( pInfo, LIST_APPEND );
1017*cdf0e10cSrcweir 		// Libs from external files should be loaded only when necessary.
1018*cdf0e10cSrcweir 		// But references are loaded at once, otherwise some big customers get into trouble
1019*cdf0e10cSrcweir 		if ( bLoadLibs && pInfo->DoLoad() &&
1020*cdf0e10cSrcweir 			( ( !pInfo->IsExtern() ) || ( pInfo->IsReference() ) ) )
1021*cdf0e10cSrcweir 		{
1022*cdf0e10cSrcweir             ImpLoadLibary( pInfo, &rStorage );
1023*cdf0e10cSrcweir 		}
1024*cdf0e10cSrcweir 	}
1025*cdf0e10cSrcweir 
1026*cdf0e10cSrcweir 	xManagerStream->Seek( nEndPos );
1027*cdf0e10cSrcweir 	xManagerStream->SetBufferSize( 0 );
1028*cdf0e10cSrcweir 	xManagerStream.Clear();
1029*cdf0e10cSrcweir }
1030*cdf0e10cSrcweir 
1031*cdf0e10cSrcweir void BasicManager::LoadOldBasicManager( SotStorage& rStorage )
1032*cdf0e10cSrcweir {
1033*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1034*cdf0e10cSrcweir 
1035*cdf0e10cSrcweir //	StreamMode eStreamMode = STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE;
1036*cdf0e10cSrcweir 
1037*cdf0e10cSrcweir 	SotStorageStreamRef xManagerStream = rStorage.OpenSotStream
1038*cdf0e10cSrcweir 		( String::CreateFromAscii(szOldManagerStream), eStreamReadMode );
1039*cdf0e10cSrcweir 
1040*cdf0e10cSrcweir     String aStorName( rStorage.GetName() );
1041*cdf0e10cSrcweir     DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
1042*cdf0e10cSrcweir 
1043*cdf0e10cSrcweir 	if ( !xManagerStream.Is() || xManagerStream->GetError() || ( xManagerStream->Seek( STREAM_SEEK_TO_END ) == 0 ) )
1044*cdf0e10cSrcweir 	{
1045*cdf0e10cSrcweir         ImpMgrNotLoaded( aStorName );
1046*cdf0e10cSrcweir 		return;
1047*cdf0e10cSrcweir 	}
1048*cdf0e10cSrcweir 
1049*cdf0e10cSrcweir 	xManagerStream->SetBufferSize( 1024 );
1050*cdf0e10cSrcweir 	xManagerStream->Seek( STREAM_SEEK_TO_BEGIN );
1051*cdf0e10cSrcweir 	sal_uInt32 nBasicStartOff, nBasicEndOff;
1052*cdf0e10cSrcweir 	*xManagerStream >> nBasicStartOff;
1053*cdf0e10cSrcweir 	*xManagerStream >> nBasicEndOff;
1054*cdf0e10cSrcweir 
1055*cdf0e10cSrcweir 	DBG_ASSERT( !xManagerStream->GetError(), "Ungueltiger Manager-Stream!" );
1056*cdf0e10cSrcweir 
1057*cdf0e10cSrcweir 	xManagerStream->Seek( nBasicStartOff );
1058*cdf0e10cSrcweir 	if( !ImplLoadBasic( *xManagerStream, pLibs->GetObject(0)->GetLibRef() ) )
1059*cdf0e10cSrcweir 	{
1060*cdf0e10cSrcweir //		String aErrorText( BasicResId( IDS_SBERR_MGROPEN ) );
1061*cdf0e10cSrcweir //      aErrorText.SearchAndReplace( "XX", aStorName );
1062*cdf0e10cSrcweir         StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_MGROPEN, aStorName, ERRCODE_BUTTON_OK );
1063*cdf0e10cSrcweir         pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_OPENMGRSTREAM, aStorName ) );
1064*cdf0e10cSrcweir 		// und es geht weiter...
1065*cdf0e10cSrcweir 	}
1066*cdf0e10cSrcweir 	xManagerStream->Seek( nBasicEndOff+1 );	// +1: 0x00 as separator
1067*cdf0e10cSrcweir 	String aLibs;
1068*cdf0e10cSrcweir 	xManagerStream->ReadByteString(aLibs);
1069*cdf0e10cSrcweir 	xManagerStream->SetBufferSize( 0 );
1070*cdf0e10cSrcweir 	xManagerStream.Clear();	// Close stream
1071*cdf0e10cSrcweir 
1072*cdf0e10cSrcweir 	if ( aLibs.Len() )
1073*cdf0e10cSrcweir 	{
1074*cdf0e10cSrcweir         String aCurStorageName( aStorName );
1075*cdf0e10cSrcweir         INetURLObject aCurStorage( aCurStorageName, INET_PROT_FILE );
1076*cdf0e10cSrcweir 		sal_uInt16 nLibs = aLibs.GetTokenCount( LIB_SEP );
1077*cdf0e10cSrcweir 		for ( sal_uInt16 nLib = 0; nLib < nLibs; nLib++ )
1078*cdf0e10cSrcweir 		{
1079*cdf0e10cSrcweir 			String aLibInfo( aLibs.GetToken( nLib, LIB_SEP ) );
1080*cdf0e10cSrcweir 			// TODO: Remove == 2
1081*cdf0e10cSrcweir 			DBG_ASSERT( ( aLibInfo.GetTokenCount( LIBINFO_SEP ) == 2 ) || ( aLibInfo.GetTokenCount( LIBINFO_SEP ) == 3 ), "Ungueltige Lib-Info!" );
1082*cdf0e10cSrcweir 			String aLibName( aLibInfo.GetToken( 0, LIBINFO_SEP ) );
1083*cdf0e10cSrcweir 			String aLibAbsStorageName( aLibInfo.GetToken( 1, LIBINFO_SEP ) );
1084*cdf0e10cSrcweir 			String aLibRelStorageName( aLibInfo.GetToken( 2, LIBINFO_SEP ) );
1085*cdf0e10cSrcweir             INetURLObject aLibAbsStorage( aLibAbsStorageName, INET_PROT_FILE );
1086*cdf0e10cSrcweir 
1087*cdf0e10cSrcweir             INetURLObject aLibRelStorage( aStorName );
1088*cdf0e10cSrcweir             aLibRelStorage.removeSegment();
1089*cdf0e10cSrcweir             bool bWasAbsolute = sal_False;
1090*cdf0e10cSrcweir             aLibRelStorage = aLibRelStorage.smartRel2Abs( aLibRelStorageName, bWasAbsolute);
1091*cdf0e10cSrcweir             DBG_ASSERT(!bWasAbsolute, "RelStorageName was absolute!" );
1092*cdf0e10cSrcweir 
1093*cdf0e10cSrcweir 			SotStorageRef xStorageRef;
1094*cdf0e10cSrcweir 			if ( ( aLibAbsStorage == aCurStorage ) || ( aLibRelStorageName.EqualsAscii(szImbedded) ) )
1095*cdf0e10cSrcweir 				xStorageRef = &rStorage;
1096*cdf0e10cSrcweir 			else
1097*cdf0e10cSrcweir 			{
1098*cdf0e10cSrcweir                 xStorageRef = new SotStorage( sal_False, aLibAbsStorage.GetMainURL
1099*cdf0e10cSrcweir                     ( INetURLObject::NO_DECODE ), eStorageReadMode, sal_True );
1100*cdf0e10cSrcweir                 if ( xStorageRef->GetError() != ERRCODE_NONE )
1101*cdf0e10cSrcweir                     xStorageRef = new SotStorage( sal_False, aLibRelStorage.
1102*cdf0e10cSrcweir                     GetMainURL( INetURLObject::NO_DECODE ), eStorageReadMode, sal_True );
1103*cdf0e10cSrcweir 			}
1104*cdf0e10cSrcweir 			if ( xStorageRef.Is() )
1105*cdf0e10cSrcweir                 AddLib( *xStorageRef, aLibName, sal_False );
1106*cdf0e10cSrcweir 			else
1107*cdf0e10cSrcweir 			{
1108*cdf0e10cSrcweir //				String aErrorText( BasicResId( IDS_SBERR_LIBLOAD ) );
1109*cdf0e10cSrcweir //				aErrorText.SearchAndReplace( "XX", aLibName );
1110*cdf0e10cSrcweir                 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD, aStorName, ERRCODE_BUTTON_OK );
1111*cdf0e10cSrcweir                 pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_STORAGENOTFOUND, aStorName ) );
1112*cdf0e10cSrcweir 			}
1113*cdf0e10cSrcweir 		}
1114*cdf0e10cSrcweir 	}
1115*cdf0e10cSrcweir }
1116*cdf0e10cSrcweir 
1117*cdf0e10cSrcweir BasicManager::~BasicManager()
1118*cdf0e10cSrcweir {
1119*cdf0e10cSrcweir 	DBG_DTOR( BasicManager, 0 );
1120*cdf0e10cSrcweir 
1121*cdf0e10cSrcweir     // Notify listener if something needs to be saved
1122*cdf0e10cSrcweir 	Broadcast( SfxSimpleHint( SFX_HINT_DYING) );
1123*cdf0e10cSrcweir 
1124*cdf0e10cSrcweir     // Destroy Basic-Infos...
1125*cdf0e10cSrcweir     // In reverse order
1126*cdf0e10cSrcweir 	BasicLibInfo* pInf = pLibs->Last();
1127*cdf0e10cSrcweir 	while ( pInf )
1128*cdf0e10cSrcweir 	{
1129*cdf0e10cSrcweir 		delete pInf;
1130*cdf0e10cSrcweir 		pInf = pLibs->Prev();
1131*cdf0e10cSrcweir 	}
1132*cdf0e10cSrcweir 	pLibs->Clear();
1133*cdf0e10cSrcweir 	delete pLibs;
1134*cdf0e10cSrcweir 	delete pErrorMgr;
1135*cdf0e10cSrcweir 	delete mpImpl;
1136*cdf0e10cSrcweir }
1137*cdf0e10cSrcweir 
1138*cdf0e10cSrcweir void BasicManager::LegacyDeleteBasicManager( BasicManager*& _rpManager )
1139*cdf0e10cSrcweir {
1140*cdf0e10cSrcweir     delete _rpManager;
1141*cdf0e10cSrcweir     _rpManager = NULL;
1142*cdf0e10cSrcweir }
1143*cdf0e10cSrcweir 
1144*cdf0e10cSrcweir void BasicManager::Init()
1145*cdf0e10cSrcweir {
1146*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1147*cdf0e10cSrcweir 
1148*cdf0e10cSrcweir 	bBasMgrModified = sal_False;
1149*cdf0e10cSrcweir 	pErrorMgr = new BasicErrorManager;
1150*cdf0e10cSrcweir 	pLibs = new BasicLibs;
1151*cdf0e10cSrcweir 	mpImpl = new BasicManagerImpl();
1152*cdf0e10cSrcweir }
1153*cdf0e10cSrcweir 
1154*cdf0e10cSrcweir BasicLibInfo* BasicManager::CreateLibInfo()
1155*cdf0e10cSrcweir {
1156*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1157*cdf0e10cSrcweir 
1158*cdf0e10cSrcweir 	BasicLibInfo* pInf = new BasicLibInfo;
1159*cdf0e10cSrcweir 	pLibs->Insert( pInf, LIST_APPEND );
1160*cdf0e10cSrcweir 	return pInf;
1161*cdf0e10cSrcweir }
1162*cdf0e10cSrcweir 
1163*cdf0e10cSrcweir sal_Bool BasicManager::ImpLoadLibary( BasicLibInfo* pLibInfo, SotStorage* pCurStorage, sal_Bool bInfosOnly ) const
1164*cdf0e10cSrcweir {
1165*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1166*cdf0e10cSrcweir 
1167*cdf0e10cSrcweir 	DBG_ASSERT( pLibInfo, "LibInfo!?" );
1168*cdf0e10cSrcweir 
1169*cdf0e10cSrcweir 	String aStorageName( pLibInfo->GetStorageName() );
1170*cdf0e10cSrcweir 	if ( !aStorageName.Len() || ( aStorageName.EqualsAscii(szImbedded) ) )
1171*cdf0e10cSrcweir 		aStorageName = GetStorageName();
1172*cdf0e10cSrcweir 
1173*cdf0e10cSrcweir 	SotStorageRef xStorage;
1174*cdf0e10cSrcweir 	// The current must not be opened again...
1175*cdf0e10cSrcweir 	if ( pCurStorage )
1176*cdf0e10cSrcweir 	{
1177*cdf0e10cSrcweir         String aStorName( pCurStorage->GetName() );
1178*cdf0e10cSrcweir         // #i13114 removed, DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
1179*cdf0e10cSrcweir 
1180*cdf0e10cSrcweir         INetURLObject aCurStorageEntry(aStorName, INET_PROT_FILE);
1181*cdf0e10cSrcweir         // #i13114 removed, DBG_ASSERT(aCurStorageEntry.GetMainURL( INetURLObject::NO_DECODE ).Len() != 0, "Bad storage name");
1182*cdf0e10cSrcweir 
1183*cdf0e10cSrcweir         INetURLObject aStorageEntry(aStorageName, INET_PROT_FILE);
1184*cdf0e10cSrcweir         // #i13114 removed, DBG_ASSERT(aCurStorageEntry.GetMainURL( INetURLObject::NO_DECODE ).Len() != 0, "Bad storage name");
1185*cdf0e10cSrcweir 
1186*cdf0e10cSrcweir 		if ( aCurStorageEntry == aStorageEntry )
1187*cdf0e10cSrcweir 			xStorage = pCurStorage;
1188*cdf0e10cSrcweir 	}
1189*cdf0e10cSrcweir 
1190*cdf0e10cSrcweir 	if ( !xStorage.Is() )
1191*cdf0e10cSrcweir 		xStorage = new SotStorage( sal_False, aStorageName, eStorageReadMode );
1192*cdf0e10cSrcweir 
1193*cdf0e10cSrcweir 	SotStorageRef xBasicStorage = xStorage->OpenSotStorage
1194*cdf0e10cSrcweir 							( String(RTL_CONSTASCII_USTRINGPARAM(szBasicStorage)), eStorageReadMode, sal_False );
1195*cdf0e10cSrcweir 
1196*cdf0e10cSrcweir 	if ( !xBasicStorage.Is() || xBasicStorage->GetError() )
1197*cdf0e10cSrcweir 	{
1198*cdf0e10cSrcweir 		StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_MGROPEN,	xStorage->GetName(), ERRCODE_BUTTON_OK );
1199*cdf0e10cSrcweir 		pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_OPENLIBSTORAGE, pLibInfo->GetLibName() ) );
1200*cdf0e10cSrcweir 	}
1201*cdf0e10cSrcweir 	else
1202*cdf0e10cSrcweir 	{
1203*cdf0e10cSrcweir 		// In the Basic-Storage every lib is in a Stream...
1204*cdf0e10cSrcweir 		SotStorageStreamRef xBasicStream = xBasicStorage->OpenSotStream( pLibInfo->GetLibName(), eStreamReadMode );
1205*cdf0e10cSrcweir 		if ( !xBasicStream.Is() || xBasicStream->GetError() )
1206*cdf0e10cSrcweir 		{
1207*cdf0e10cSrcweir 			StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD , pLibInfo->GetLibName(), ERRCODE_BUTTON_OK );
1208*cdf0e10cSrcweir 			pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_OPENLIBSTREAM, pLibInfo->GetLibName() ) );
1209*cdf0e10cSrcweir 		}
1210*cdf0e10cSrcweir 		else
1211*cdf0e10cSrcweir 		{
1212*cdf0e10cSrcweir 			sal_Bool bLoaded = sal_False;
1213*cdf0e10cSrcweir 			if ( xBasicStream->Seek( STREAM_SEEK_TO_END ) != 0 )
1214*cdf0e10cSrcweir 			{
1215*cdf0e10cSrcweir 				if ( !bInfosOnly )
1216*cdf0e10cSrcweir 				{
1217*cdf0e10cSrcweir 					if ( !pLibInfo->GetLib().Is() )
1218*cdf0e10cSrcweir 						pLibInfo->SetLib( new StarBASIC( GetStdLib(), mbDocMgr ) );
1219*cdf0e10cSrcweir 					xBasicStream->SetBufferSize( 1024 );
1220*cdf0e10cSrcweir 					xBasicStream->Seek( STREAM_SEEK_TO_BEGIN );
1221*cdf0e10cSrcweir 					bLoaded = ImplLoadBasic( *xBasicStream, pLibInfo->GetLibRef() );
1222*cdf0e10cSrcweir 					xBasicStream->SetBufferSize( 0 );
1223*cdf0e10cSrcweir                     StarBASICRef xStdLib = pLibInfo->GetLib();
1224*cdf0e10cSrcweir 					xStdLib->SetName( pLibInfo->GetLibName() );
1225*cdf0e10cSrcweir 					xStdLib->SetModified( sal_False );
1226*cdf0e10cSrcweir 					xStdLib->SetFlag( SBX_DONTSTORE );
1227*cdf0e10cSrcweir 				}
1228*cdf0e10cSrcweir 				else
1229*cdf0e10cSrcweir 				{
1230*cdf0e10cSrcweir 					// Skip Basic...
1231*cdf0e10cSrcweir 					xBasicStream->Seek( STREAM_SEEK_TO_BEGIN );
1232*cdf0e10cSrcweir 					ImplEncryptStream( *xBasicStream );
1233*cdf0e10cSrcweir 					SbxBase::Skip( *xBasicStream );
1234*cdf0e10cSrcweir 					bLoaded = sal_True;
1235*cdf0e10cSrcweir 				}
1236*cdf0e10cSrcweir 			}
1237*cdf0e10cSrcweir 			if ( !bLoaded )
1238*cdf0e10cSrcweir 			{
1239*cdf0e10cSrcweir 				StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD,	pLibInfo->GetLibName(), ERRCODE_BUTTON_OK );
1240*cdf0e10cSrcweir 				pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_BASICLOADERROR, pLibInfo->GetLibName() ) );
1241*cdf0e10cSrcweir 			}
1242*cdf0e10cSrcweir 			else
1243*cdf0e10cSrcweir 			{
1244*cdf0e10cSrcweir 				// Perhaps there are additional information in the stream...
1245*cdf0e10cSrcweir 				xBasicStream->SetKey( szCryptingKey );
1246*cdf0e10cSrcweir 				xBasicStream->RefreshBuffer();
1247*cdf0e10cSrcweir 				sal_uInt32 nPasswordMarker = 0;
1248*cdf0e10cSrcweir 				*xBasicStream >> nPasswordMarker;
1249*cdf0e10cSrcweir 				if ( ( nPasswordMarker == PASSWORD_MARKER ) && !xBasicStream->IsEof() )
1250*cdf0e10cSrcweir 				{
1251*cdf0e10cSrcweir 					String aPassword;
1252*cdf0e10cSrcweir 					xBasicStream->ReadByteString(aPassword);
1253*cdf0e10cSrcweir 					pLibInfo->SetPassword( aPassword );
1254*cdf0e10cSrcweir 				}
1255*cdf0e10cSrcweir 				xBasicStream->SetKey( ByteString() );
1256*cdf0e10cSrcweir 				CheckModules( pLibInfo->GetLib(), pLibInfo->IsReference() );
1257*cdf0e10cSrcweir 			}
1258*cdf0e10cSrcweir 			return bLoaded;
1259*cdf0e10cSrcweir 		}
1260*cdf0e10cSrcweir 	}
1261*cdf0e10cSrcweir 	return sal_False;
1262*cdf0e10cSrcweir }
1263*cdf0e10cSrcweir 
1264*cdf0e10cSrcweir sal_Bool BasicManager::ImplEncryptStream( SvStream& rStrm ) const
1265*cdf0e10cSrcweir {
1266*cdf0e10cSrcweir 	sal_uIntPtr nPos = rStrm.Tell();
1267*cdf0e10cSrcweir 	sal_uInt32 nCreator;
1268*cdf0e10cSrcweir 	rStrm >> nCreator;
1269*cdf0e10cSrcweir 	rStrm.Seek( nPos );
1270*cdf0e10cSrcweir 	sal_Bool bProtected = sal_False;
1271*cdf0e10cSrcweir 	if ( nCreator != SBXCR_SBX )
1272*cdf0e10cSrcweir 	{
1273*cdf0e10cSrcweir 		// Should only be the case for encrypted Streams
1274*cdf0e10cSrcweir 		bProtected = sal_True;
1275*cdf0e10cSrcweir 		rStrm.SetKey( szCryptingKey );
1276*cdf0e10cSrcweir 		rStrm.RefreshBuffer();
1277*cdf0e10cSrcweir 	}
1278*cdf0e10cSrcweir 	return bProtected;
1279*cdf0e10cSrcweir }
1280*cdf0e10cSrcweir 
1281*cdf0e10cSrcweir // This code is necessary to load the BASIC of Beta 1
1282*cdf0e10cSrcweir // TODO: Which Beta 1?
1283*cdf0e10cSrcweir sal_Bool BasicManager::ImplLoadBasic( SvStream& rStrm, StarBASICRef& rOldBasic ) const
1284*cdf0e10cSrcweir {
1285*cdf0e10cSrcweir 	sal_Bool bProtected = ImplEncryptStream( rStrm );
1286*cdf0e10cSrcweir 	SbxBaseRef xNew = SbxBase::Load( rStrm );
1287*cdf0e10cSrcweir 	sal_Bool bLoaded = sal_False;
1288*cdf0e10cSrcweir 	if( xNew.Is() )
1289*cdf0e10cSrcweir 	{
1290*cdf0e10cSrcweir 		if( xNew->IsA( TYPE(StarBASIC) ) )
1291*cdf0e10cSrcweir 		{
1292*cdf0e10cSrcweir 			StarBASIC* pNew = (StarBASIC*)(SbxBase*) xNew;
1293*cdf0e10cSrcweir 			// Use the Parent of the old BASICs
1294*cdf0e10cSrcweir 			if( rOldBasic.Is() )
1295*cdf0e10cSrcweir 			{
1296*cdf0e10cSrcweir 				pNew->SetParent( rOldBasic->GetParent() );
1297*cdf0e10cSrcweir 				if( pNew->GetParent() )
1298*cdf0e10cSrcweir 					pNew->GetParent()->Insert( pNew );
1299*cdf0e10cSrcweir 				pNew->SetFlag( SBX_EXTSEARCH );
1300*cdf0e10cSrcweir 			}
1301*cdf0e10cSrcweir 			rOldBasic = pNew;
1302*cdf0e10cSrcweir 
1303*cdf0e10cSrcweir 			// Fill new libray container (5.2 -> 6.0)
1304*cdf0e10cSrcweir             copyToLibraryContainer( pNew, mpImpl->maContainerInfo );
1305*cdf0e10cSrcweir 
1306*cdf0e10cSrcweir /*
1307*cdf0e10cSrcweir 			if( rOldBasic->GetParent() )
1308*cdf0e10cSrcweir 			{
1309*cdf0e10cSrcweir 				rOldBasic->GetParent()->Insert( rOldBasic );
1310*cdf0e10cSrcweir 				rOldBasic->SetFlag( SBX_EXTSEARCH );
1311*cdf0e10cSrcweir 			}
1312*cdf0e10cSrcweir */
1313*cdf0e10cSrcweir 			pNew->SetModified( sal_False );
1314*cdf0e10cSrcweir 			bLoaded = sal_True;
1315*cdf0e10cSrcweir 		}
1316*cdf0e10cSrcweir 	}
1317*cdf0e10cSrcweir 	if ( bProtected )
1318*cdf0e10cSrcweir 		rStrm.SetKey( ByteString() );
1319*cdf0e10cSrcweir 	return bLoaded;
1320*cdf0e10cSrcweir }
1321*cdf0e10cSrcweir 
1322*cdf0e10cSrcweir void BasicManager::CheckModules( StarBASIC* pLib, sal_Bool bReference ) const
1323*cdf0e10cSrcweir {
1324*cdf0e10cSrcweir 	if ( !pLib )
1325*cdf0e10cSrcweir 		return;
1326*cdf0e10cSrcweir 
1327*cdf0e10cSrcweir 	sal_Bool bModified = pLib->IsModified();
1328*cdf0e10cSrcweir 
1329*cdf0e10cSrcweir 	for ( sal_uInt16 nMod = 0; nMod < pLib->GetModules()->Count(); nMod++ )
1330*cdf0e10cSrcweir 	{
1331*cdf0e10cSrcweir 		SbModule* pModule = (SbModule*)pLib->GetModules()->Get( nMod );
1332*cdf0e10cSrcweir 		DBG_ASSERT( pModule, "Modul nicht erhalten!" );
1333*cdf0e10cSrcweir 		if ( !pModule->IsCompiled() && !StarBASIC::GetErrorCode() )
1334*cdf0e10cSrcweir 			pLib->Compile( pModule );
1335*cdf0e10cSrcweir 	}
1336*cdf0e10cSrcweir 
1337*cdf0e10cSrcweir 	// #67477, AB 8.12.99 On demand compile in referenced libs should not
1338*cdf0e10cSrcweir 	// cause modified
1339*cdf0e10cSrcweir 	if( !bModified && bReference )
1340*cdf0e10cSrcweir 	{
1341*cdf0e10cSrcweir 		DBG_ERROR( "Per Reference eingebundene Basic-Library ist nicht compiliert!" );
1342*cdf0e10cSrcweir 		pLib->SetModified( sal_False );
1343*cdf0e10cSrcweir 	}
1344*cdf0e10cSrcweir }
1345*cdf0e10cSrcweir 
1346*cdf0e10cSrcweir StarBASIC* BasicManager::AddLib( SotStorage& rStorage, const String& rLibName, sal_Bool bReference )
1347*cdf0e10cSrcweir {
1348*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1349*cdf0e10cSrcweir 
1350*cdf0e10cSrcweir     String aStorName( rStorage.GetName() );
1351*cdf0e10cSrcweir     DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
1352*cdf0e10cSrcweir 
1353*cdf0e10cSrcweir     String aStorageName = INetURLObject(aStorName, INET_PROT_FILE).GetMainURL( INetURLObject::NO_DECODE );
1354*cdf0e10cSrcweir     DBG_ASSERT(aStorageName.Len() != 0, "Bad storage name");
1355*cdf0e10cSrcweir 
1356*cdf0e10cSrcweir 	String aNewLibName( rLibName );
1357*cdf0e10cSrcweir 	while ( HasLib( aNewLibName ) )
1358*cdf0e10cSrcweir 		aNewLibName += '_';
1359*cdf0e10cSrcweir 
1360*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = CreateLibInfo();
1361*cdf0e10cSrcweir 	// Use original name otherwise ImpLoadLibary failes...
1362*cdf0e10cSrcweir 	pLibInfo->SetLibName( rLibName );
1363*cdf0e10cSrcweir 	// Funktioniert so aber nicht, wenn Name doppelt
1364*cdf0e10cSrcweir //	sal_uInt16 nLibId = GetLibId( rLibName );
1365*cdf0e10cSrcweir 	sal_uInt16 nLibId = (sal_uInt16) pLibs->GetPos( pLibInfo );
1366*cdf0e10cSrcweir 
1367*cdf0e10cSrcweir 	// Set StorageName before load because it is compared with pCurStorage
1368*cdf0e10cSrcweir 	pLibInfo->SetStorageName( aStorageName );
1369*cdf0e10cSrcweir     sal_Bool bLoaded = ImpLoadLibary( pLibInfo, &rStorage );
1370*cdf0e10cSrcweir 
1371*cdf0e10cSrcweir 	if ( bLoaded )
1372*cdf0e10cSrcweir 	{
1373*cdf0e10cSrcweir 		if ( aNewLibName != rLibName )
1374*cdf0e10cSrcweir 			SetLibName( nLibId, aNewLibName );
1375*cdf0e10cSrcweir 
1376*cdf0e10cSrcweir 		if ( bReference )
1377*cdf0e10cSrcweir 		{
1378*cdf0e10cSrcweir 			pLibInfo->GetLib()->SetModified( sal_False );	// Don't save in this case
1379*cdf0e10cSrcweir 			pLibInfo->SetRelStorageName( String() );
1380*cdf0e10cSrcweir //			pLibInfo->CalcRelStorageName( GetStorageName() );
1381*cdf0e10cSrcweir 			pLibInfo->IsReference() = sal_True;
1382*cdf0e10cSrcweir 		}
1383*cdf0e10cSrcweir 		else
1384*cdf0e10cSrcweir 		{
1385*cdf0e10cSrcweir 			pLibInfo->GetLib()->SetModified( sal_True ); // Must be saved after Add!
1386*cdf0e10cSrcweir 			pLibInfo->SetStorageName( String::CreateFromAscii(szImbedded) ); // Save in BasicManager-Storage
1387*cdf0e10cSrcweir 		}
1388*cdf0e10cSrcweir 		bBasMgrModified = sal_True;
1389*cdf0e10cSrcweir 	}
1390*cdf0e10cSrcweir 	else
1391*cdf0e10cSrcweir 	{
1392*cdf0e10cSrcweir 		RemoveLib( nLibId, sal_False );
1393*cdf0e10cSrcweir 		pLibInfo = 0;
1394*cdf0e10cSrcweir 	}
1395*cdf0e10cSrcweir 
1396*cdf0e10cSrcweir 	if( pLibInfo )
1397*cdf0e10cSrcweir 		return &*pLibInfo->GetLib() ;
1398*cdf0e10cSrcweir 	else
1399*cdf0e10cSrcweir 		return 0;
1400*cdf0e10cSrcweir }
1401*cdf0e10cSrcweir 
1402*cdf0e10cSrcweir sal_Bool BasicManager::IsReference( sal_uInt16 nLib )
1403*cdf0e10cSrcweir {
1404*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1405*cdf0e10cSrcweir 
1406*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = pLibs->GetObject( nLib );
1407*cdf0e10cSrcweir 	DBG_ASSERT( pLibInfo, "Lib?!" );
1408*cdf0e10cSrcweir 	if ( pLibInfo )
1409*cdf0e10cSrcweir 		return pLibInfo->IsReference();
1410*cdf0e10cSrcweir 
1411*cdf0e10cSrcweir 	return sal_False;
1412*cdf0e10cSrcweir }
1413*cdf0e10cSrcweir 
1414*cdf0e10cSrcweir sal_Bool BasicManager::RemoveLib( sal_uInt16 nLib )
1415*cdf0e10cSrcweir {
1416*cdf0e10cSrcweir 	// Only pyhsical deletion if no reference
1417*cdf0e10cSrcweir 	return RemoveLib( nLib, !IsReference( nLib ) );
1418*cdf0e10cSrcweir }
1419*cdf0e10cSrcweir 
1420*cdf0e10cSrcweir sal_Bool BasicManager::RemoveLib( sal_uInt16 nLib, sal_Bool bDelBasicFromStorage )
1421*cdf0e10cSrcweir {
1422*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1423*cdf0e10cSrcweir 	DBG_ASSERT( nLib, "Standard-Lib cannot be removed!" );
1424*cdf0e10cSrcweir 
1425*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = pLibs->GetObject( nLib );
1426*cdf0e10cSrcweir 	DBG_ASSERT( pLibInfo, "Lib not found!" );
1427*cdf0e10cSrcweir 
1428*cdf0e10cSrcweir 	if ( !pLibInfo || !nLib )
1429*cdf0e10cSrcweir 	{
1430*cdf0e10cSrcweir //		String aErrorText( BasicResId( IDS_SBERR_REMOVELIB ) );
1431*cdf0e10cSrcweir 		StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_REMOVELIB, String(), ERRCODE_BUTTON_OK );
1432*cdf0e10cSrcweir 		pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_STDLIB, pLibInfo->GetLibName() ) );
1433*cdf0e10cSrcweir 		return sal_False;
1434*cdf0e10cSrcweir 	}
1435*cdf0e10cSrcweir 
1436*cdf0e10cSrcweir     // If one of the streams cannot be opened, this is not an error,
1437*cdf0e10cSrcweir     // because BASIC was never written before...
1438*cdf0e10cSrcweir 	if ( bDelBasicFromStorage && !pLibInfo->IsReference() &&
1439*cdf0e10cSrcweir 			( !pLibInfo->IsExtern() || SotStorage::IsStorageFile( pLibInfo->GetStorageName() ) ) )
1440*cdf0e10cSrcweir 	{
1441*cdf0e10cSrcweir 		SotStorageRef xStorage;
1442*cdf0e10cSrcweir 		if ( !pLibInfo->IsExtern() )
1443*cdf0e10cSrcweir 			xStorage = new SotStorage( sal_False, GetStorageName() );
1444*cdf0e10cSrcweir 		else
1445*cdf0e10cSrcweir 			xStorage = new SotStorage( sal_False, pLibInfo->GetStorageName() );
1446*cdf0e10cSrcweir 
1447*cdf0e10cSrcweir 		if ( xStorage->IsStorage( String(RTL_CONSTASCII_USTRINGPARAM(szBasicStorage)) ) )
1448*cdf0e10cSrcweir 		{
1449*cdf0e10cSrcweir 			SotStorageRef xBasicStorage = xStorage->OpenSotStorage
1450*cdf0e10cSrcweir 							( String(RTL_CONSTASCII_USTRINGPARAM(szBasicStorage)), STREAM_STD_READWRITE, sal_False );
1451*cdf0e10cSrcweir 
1452*cdf0e10cSrcweir 			if ( !xBasicStorage.Is() || xBasicStorage->GetError() )
1453*cdf0e10cSrcweir 			{
1454*cdf0e10cSrcweir //				String aErrorText( BasicResId( IDS_SBERR_REMOVELIB ) );
1455*cdf0e10cSrcweir 				StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_REMOVELIB, String(), ERRCODE_BUTTON_OK );
1456*cdf0e10cSrcweir 				pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_OPENLIBSTORAGE, pLibInfo->GetLibName() ) );
1457*cdf0e10cSrcweir 			}
1458*cdf0e10cSrcweir 			else if ( xBasicStorage->IsStream( pLibInfo->GetLibName() ) )
1459*cdf0e10cSrcweir 			{
1460*cdf0e10cSrcweir 				xBasicStorage->Remove( pLibInfo->GetLibName() );
1461*cdf0e10cSrcweir 				xBasicStorage->Commit();
1462*cdf0e10cSrcweir 
1463*cdf0e10cSrcweir 				// If no further stream available,
1464*cdf0e10cSrcweir 				// delete the SubStorage.
1465*cdf0e10cSrcweir 				SvStorageInfoList aInfoList( 0, 4 );
1466*cdf0e10cSrcweir 				xBasicStorage->FillInfoList( &aInfoList );
1467*cdf0e10cSrcweir 				if ( !aInfoList.Count() )
1468*cdf0e10cSrcweir 				{
1469*cdf0e10cSrcweir 					xBasicStorage.Clear();
1470*cdf0e10cSrcweir 					xStorage->Remove( String(RTL_CONSTASCII_USTRINGPARAM(szBasicStorage)) );
1471*cdf0e10cSrcweir 					xStorage->Commit();
1472*cdf0e10cSrcweir 					// If no further Streams or SubStorages available,
1473*cdf0e10cSrcweir 					// delete the Storage, too.
1474*cdf0e10cSrcweir 					aInfoList.Clear();
1475*cdf0e10cSrcweir 					xStorage->FillInfoList( &aInfoList );
1476*cdf0e10cSrcweir 					if ( !aInfoList.Count() )
1477*cdf0e10cSrcweir 					{
1478*cdf0e10cSrcweir                         String aName_( xStorage->GetName() );
1479*cdf0e10cSrcweir 						xStorage.Clear();
1480*cdf0e10cSrcweir                         //*** TODO: Replace if still necessary
1481*cdf0e10cSrcweir                         //SfxContentHelper::Kill( aName );
1482*cdf0e10cSrcweir                         //*** TODO-End
1483*cdf0e10cSrcweir 					}
1484*cdf0e10cSrcweir 				}
1485*cdf0e10cSrcweir 			}
1486*cdf0e10cSrcweir 		}
1487*cdf0e10cSrcweir 	}
1488*cdf0e10cSrcweir 	bBasMgrModified = sal_True;
1489*cdf0e10cSrcweir 	if ( pLibInfo->GetLib().Is() )
1490*cdf0e10cSrcweir 		GetStdLib()->Remove( pLibInfo->GetLib() );
1491*cdf0e10cSrcweir 	delete pLibs->Remove( pLibInfo );
1492*cdf0e10cSrcweir 	return sal_True;	// Remove was successful, del unimportant
1493*cdf0e10cSrcweir }
1494*cdf0e10cSrcweir 
1495*cdf0e10cSrcweir sal_uInt16 BasicManager::GetLibCount() const
1496*cdf0e10cSrcweir {
1497*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1498*cdf0e10cSrcweir 	return (sal_uInt16)pLibs->Count();
1499*cdf0e10cSrcweir }
1500*cdf0e10cSrcweir 
1501*cdf0e10cSrcweir StarBASIC* BasicManager::GetLib( sal_uInt16 nLib ) const
1502*cdf0e10cSrcweir {
1503*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1504*cdf0e10cSrcweir 	BasicLibInfo* pInf = pLibs->GetObject( nLib );
1505*cdf0e10cSrcweir 	DBG_ASSERT( pInf, "Lib existiert nicht!" );
1506*cdf0e10cSrcweir 	if ( pInf )
1507*cdf0e10cSrcweir 		return pInf->GetLib();
1508*cdf0e10cSrcweir 	return 0;
1509*cdf0e10cSrcweir }
1510*cdf0e10cSrcweir 
1511*cdf0e10cSrcweir StarBASIC* BasicManager::GetStdLib() const
1512*cdf0e10cSrcweir {
1513*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1514*cdf0e10cSrcweir 	StarBASIC* pLib = GetLib( 0 );
1515*cdf0e10cSrcweir 	return pLib;
1516*cdf0e10cSrcweir }
1517*cdf0e10cSrcweir 
1518*cdf0e10cSrcweir StarBASIC* BasicManager::GetLib( const String& rName ) const
1519*cdf0e10cSrcweir {
1520*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1521*cdf0e10cSrcweir 
1522*cdf0e10cSrcweir 	BasicLibInfo* pInf = pLibs->First();
1523*cdf0e10cSrcweir 	while ( pInf )
1524*cdf0e10cSrcweir 	{
1525*cdf0e10cSrcweir 		if ( pInf->GetLibName().CompareIgnoreCaseToAscii( rName ) == COMPARE_EQUAL )// Check if available...
1526*cdf0e10cSrcweir 			return pInf->GetLib();
1527*cdf0e10cSrcweir 
1528*cdf0e10cSrcweir 		pInf = pLibs->Next();
1529*cdf0e10cSrcweir 	}
1530*cdf0e10cSrcweir 	return 0;
1531*cdf0e10cSrcweir }
1532*cdf0e10cSrcweir 
1533*cdf0e10cSrcweir sal_uInt16 BasicManager::GetLibId( const String& rName ) const
1534*cdf0e10cSrcweir {
1535*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1536*cdf0e10cSrcweir 
1537*cdf0e10cSrcweir 	BasicLibInfo* pInf = pLibs->First();
1538*cdf0e10cSrcweir 	while ( pInf )
1539*cdf0e10cSrcweir 	{
1540*cdf0e10cSrcweir 		if ( pInf->GetLibName().CompareIgnoreCaseToAscii( rName ) == COMPARE_EQUAL )
1541*cdf0e10cSrcweir 			return (sal_uInt16)pLibs->GetCurPos();
1542*cdf0e10cSrcweir 
1543*cdf0e10cSrcweir 		pInf = pLibs->Next();
1544*cdf0e10cSrcweir 	}
1545*cdf0e10cSrcweir 	return LIB_NOTFOUND;
1546*cdf0e10cSrcweir }
1547*cdf0e10cSrcweir 
1548*cdf0e10cSrcweir sal_Bool BasicManager::HasLib( const String& rName ) const
1549*cdf0e10cSrcweir {
1550*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1551*cdf0e10cSrcweir 
1552*cdf0e10cSrcweir 	BasicLibInfo* pInf = pLibs->First();
1553*cdf0e10cSrcweir 	while ( pInf )
1554*cdf0e10cSrcweir 	{
1555*cdf0e10cSrcweir 		if ( pInf->GetLibName().CompareIgnoreCaseToAscii( rName ) == COMPARE_EQUAL )
1556*cdf0e10cSrcweir 			return sal_True;
1557*cdf0e10cSrcweir 
1558*cdf0e10cSrcweir 		pInf = pLibs->Next();
1559*cdf0e10cSrcweir 	}
1560*cdf0e10cSrcweir 	return sal_False;
1561*cdf0e10cSrcweir }
1562*cdf0e10cSrcweir 
1563*cdf0e10cSrcweir sal_Bool BasicManager::SetLibName( sal_uInt16 nLib, const String& rName )
1564*cdf0e10cSrcweir {
1565*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1566*cdf0e10cSrcweir 
1567*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = pLibs->GetObject( nLib );
1568*cdf0e10cSrcweir 	DBG_ASSERT( pLibInfo, "Lib?!" );
1569*cdf0e10cSrcweir 	if ( pLibInfo )
1570*cdf0e10cSrcweir 	{
1571*cdf0e10cSrcweir 		pLibInfo->SetLibName( rName );
1572*cdf0e10cSrcweir 		if ( pLibInfo->GetLib().Is() )
1573*cdf0e10cSrcweir 		{
1574*cdf0e10cSrcweir             StarBASICRef xStdLib = pLibInfo->GetLib();
1575*cdf0e10cSrcweir 			xStdLib->SetName( rName );
1576*cdf0e10cSrcweir 			xStdLib->SetModified( sal_True );
1577*cdf0e10cSrcweir 		}
1578*cdf0e10cSrcweir 		bBasMgrModified = sal_True;
1579*cdf0e10cSrcweir 		return sal_True;
1580*cdf0e10cSrcweir 	}
1581*cdf0e10cSrcweir 	return sal_False;
1582*cdf0e10cSrcweir }
1583*cdf0e10cSrcweir 
1584*cdf0e10cSrcweir String BasicManager::GetLibName( sal_uInt16 nLib )
1585*cdf0e10cSrcweir {
1586*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1587*cdf0e10cSrcweir 
1588*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = pLibs->GetObject( nLib );
1589*cdf0e10cSrcweir 	DBG_ASSERT( pLibInfo, "Lib?!" );
1590*cdf0e10cSrcweir 	if ( pLibInfo )
1591*cdf0e10cSrcweir 		return pLibInfo->GetLibName();
1592*cdf0e10cSrcweir 	return String();
1593*cdf0e10cSrcweir }
1594*cdf0e10cSrcweir 
1595*cdf0e10cSrcweir sal_Bool BasicManager::LoadLib( sal_uInt16 nLib )
1596*cdf0e10cSrcweir {
1597*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1598*cdf0e10cSrcweir 
1599*cdf0e10cSrcweir 	sal_Bool bDone = sal_False;
1600*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = pLibs->GetObject( nLib );
1601*cdf0e10cSrcweir 	DBG_ASSERT( pLibInfo, "Lib?!" );
1602*cdf0e10cSrcweir 	if ( pLibInfo )
1603*cdf0e10cSrcweir 	{
1604*cdf0e10cSrcweir         Reference< XLibraryContainer > xLibContainer = pLibInfo->GetLibraryContainer();
1605*cdf0e10cSrcweir         if( xLibContainer.is() )
1606*cdf0e10cSrcweir         {
1607*cdf0e10cSrcweir             String aLibName = pLibInfo->GetLibName();
1608*cdf0e10cSrcweir             xLibContainer->loadLibrary( aLibName );
1609*cdf0e10cSrcweir             bDone = xLibContainer->isLibraryLoaded( aLibName );;
1610*cdf0e10cSrcweir         }
1611*cdf0e10cSrcweir         else
1612*cdf0e10cSrcweir         {
1613*cdf0e10cSrcweir             bDone = ImpLoadLibary( pLibInfo, NULL, sal_False );
1614*cdf0e10cSrcweir 		    StarBASIC* pLib = GetLib( nLib );
1615*cdf0e10cSrcweir 		    if ( pLib )
1616*cdf0e10cSrcweir 		    {
1617*cdf0e10cSrcweir     //			pLib->SetParent( GetStdLib() );
1618*cdf0e10cSrcweir 			    GetStdLib()->Insert( pLib );
1619*cdf0e10cSrcweir 			    pLib->SetFlag( SBX_EXTSEARCH );
1620*cdf0e10cSrcweir 		    }
1621*cdf0e10cSrcweir         }
1622*cdf0e10cSrcweir 	}
1623*cdf0e10cSrcweir 	else
1624*cdf0e10cSrcweir 	{
1625*cdf0e10cSrcweir //		String aErrorText( BasicResId( IDS_SBERR_LIBLOAD ) );
1626*cdf0e10cSrcweir //		aErrorText.SearchAndReplace( "XX", "" );
1627*cdf0e10cSrcweir 		StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD,	String(), ERRCODE_BUTTON_OK );
1628*cdf0e10cSrcweir 		pErrorMgr->InsertError( BasicError( *pErrInf, BASERR_REASON_LIBNOTFOUND, String::CreateFromInt32(nLib) ) );
1629*cdf0e10cSrcweir 	}
1630*cdf0e10cSrcweir 	return bDone;
1631*cdf0e10cSrcweir }
1632*cdf0e10cSrcweir 
1633*cdf0e10cSrcweir StarBASIC* BasicManager::CreateLib( const String& rLibName )
1634*cdf0e10cSrcweir {
1635*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1636*cdf0e10cSrcweir 	if ( GetLib( rLibName ) )
1637*cdf0e10cSrcweir 		return 0;
1638*cdf0e10cSrcweir 
1639*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = CreateLibInfo();
1640*cdf0e10cSrcweir 	StarBASIC* pNew = new StarBASIC( GetStdLib(), mbDocMgr );
1641*cdf0e10cSrcweir 	GetStdLib()->Insert( pNew );
1642*cdf0e10cSrcweir 	pNew->SetFlag( SBX_EXTSEARCH | SBX_DONTSTORE );
1643*cdf0e10cSrcweir 	pLibInfo->SetLib( pNew );
1644*cdf0e10cSrcweir 	pLibInfo->SetLibName( rLibName );
1645*cdf0e10cSrcweir 	pLibInfo->GetLib()->SetName( rLibName );
1646*cdf0e10cSrcweir 	return pLibInfo->GetLib();
1647*cdf0e10cSrcweir }
1648*cdf0e10cSrcweir 
1649*cdf0e10cSrcweir // For XML import/export:
1650*cdf0e10cSrcweir StarBASIC* BasicManager::CreateLib
1651*cdf0e10cSrcweir     ( const String& rLibName, const String& Password, const String& LinkTargetURL )
1652*cdf0e10cSrcweir {
1653*cdf0e10cSrcweir 	// Ask if lib exists because standard lib is always there
1654*cdf0e10cSrcweir 	StarBASIC* pLib = GetLib( rLibName );
1655*cdf0e10cSrcweir 	if( !pLib )
1656*cdf0e10cSrcweir 	{
1657*cdf0e10cSrcweir 	    if( LinkTargetURL.Len() != 0 )
1658*cdf0e10cSrcweir 		{
1659*cdf0e10cSrcweir 			SotStorageRef xStorage = new SotStorage( sal_False, LinkTargetURL, STREAM_READ | STREAM_SHARE_DENYWRITE );
1660*cdf0e10cSrcweir 			if( !xStorage->GetError() )
1661*cdf0e10cSrcweir 			{
1662*cdf0e10cSrcweir 				pLib = AddLib( *xStorage, rLibName, sal_True );
1663*cdf0e10cSrcweir 
1664*cdf0e10cSrcweir 				//if( !pLibInfo )
1665*cdf0e10cSrcweir 					//pLibInfo = FindLibInfo( pLib );
1666*cdf0e10cSrcweir 				//pLibInfo->SetStorageName( LinkTargetURL );
1667*cdf0e10cSrcweir 				//pLibInfo->GetLib()->SetModified( sal_False );	// Dann nicht speichern
1668*cdf0e10cSrcweir 				//pLibInfo->SetRelStorageName( String() );
1669*cdf0e10cSrcweir 				//pLibInfo->IsReference() = sal_True;
1670*cdf0e10cSrcweir 			}
1671*cdf0e10cSrcweir 			//else
1672*cdf0e10cSrcweir 				//Message?
1673*cdf0e10cSrcweir 
1674*cdf0e10cSrcweir 			DBG_ASSERT( pLib, "XML Import: Linked basic library could not be loaded");
1675*cdf0e10cSrcweir 		}
1676*cdf0e10cSrcweir 		else
1677*cdf0e10cSrcweir 		{
1678*cdf0e10cSrcweir 			pLib = CreateLib( rLibName );
1679*cdf0e10cSrcweir 			if( Password.Len() != 0 )
1680*cdf0e10cSrcweir 			{
1681*cdf0e10cSrcweir 				BasicLibInfo* pLibInfo = FindLibInfo( pLib );
1682*cdf0e10cSrcweir 				pLibInfo ->SetPassword( Password );
1683*cdf0e10cSrcweir 			}
1684*cdf0e10cSrcweir 		}
1685*cdf0e10cSrcweir 		//ExternalSourceURL ?
1686*cdf0e10cSrcweir 	}
1687*cdf0e10cSrcweir 	return pLib;
1688*cdf0e10cSrcweir }
1689*cdf0e10cSrcweir 
1690*cdf0e10cSrcweir StarBASIC* BasicManager::CreateLibForLibContainer( const String& rLibName,
1691*cdf0e10cSrcweir     const Reference< XLibraryContainer >& xScriptCont )
1692*cdf0e10cSrcweir {
1693*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1694*cdf0e10cSrcweir 	if ( GetLib( rLibName ) )
1695*cdf0e10cSrcweir 		return 0;
1696*cdf0e10cSrcweir 
1697*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = CreateLibInfo();
1698*cdf0e10cSrcweir 	StarBASIC* pNew = new StarBASIC( GetStdLib(), mbDocMgr );
1699*cdf0e10cSrcweir 	GetStdLib()->Insert( pNew );
1700*cdf0e10cSrcweir 	pNew->SetFlag( SBX_EXTSEARCH | SBX_DONTSTORE );
1701*cdf0e10cSrcweir 	pLibInfo->SetLib( pNew );
1702*cdf0e10cSrcweir 	pLibInfo->SetLibName( rLibName );
1703*cdf0e10cSrcweir 	pLibInfo->GetLib()->SetName( rLibName );
1704*cdf0e10cSrcweir     pLibInfo->SetLibraryContainer( xScriptCont );
1705*cdf0e10cSrcweir 	return pNew;
1706*cdf0e10cSrcweir }
1707*cdf0e10cSrcweir 
1708*cdf0e10cSrcweir 
1709*cdf0e10cSrcweir BasicLibInfo* BasicManager::FindLibInfo( StarBASIC* pBasic ) const
1710*cdf0e10cSrcweir {
1711*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1712*cdf0e10cSrcweir 
1713*cdf0e10cSrcweir 	BasicLibInfo* pInf = ((BasicManager*)this)->pLibs->First();
1714*cdf0e10cSrcweir 	while ( pInf )
1715*cdf0e10cSrcweir 	{
1716*cdf0e10cSrcweir 		if ( pInf->GetLib() == pBasic )
1717*cdf0e10cSrcweir 			return pInf;
1718*cdf0e10cSrcweir 
1719*cdf0e10cSrcweir 		pInf = ((BasicManager*)this)->pLibs->Next();
1720*cdf0e10cSrcweir 	}
1721*cdf0e10cSrcweir 	return 0;
1722*cdf0e10cSrcweir }
1723*cdf0e10cSrcweir 
1724*cdf0e10cSrcweir 
1725*cdf0e10cSrcweir sal_Bool BasicManager::IsModified() const
1726*cdf0e10cSrcweir {
1727*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1728*cdf0e10cSrcweir 
1729*cdf0e10cSrcweir 	if ( bBasMgrModified )
1730*cdf0e10cSrcweir 		return sal_True;
1731*cdf0e10cSrcweir 	return IsBasicModified();
1732*cdf0e10cSrcweir }
1733*cdf0e10cSrcweir 
1734*cdf0e10cSrcweir sal_Bool BasicManager::IsBasicModified() const
1735*cdf0e10cSrcweir {
1736*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1737*cdf0e10cSrcweir 
1738*cdf0e10cSrcweir 	BasicLibInfo* pInf = pLibs->First();
1739*cdf0e10cSrcweir 	while ( pInf )
1740*cdf0e10cSrcweir 	{
1741*cdf0e10cSrcweir 		if ( pInf->GetLib().Is() && pInf->GetLib()->IsModified() )
1742*cdf0e10cSrcweir 			return sal_True;
1743*cdf0e10cSrcweir 
1744*cdf0e10cSrcweir 		pInf = pLibs->Next();
1745*cdf0e10cSrcweir 	}
1746*cdf0e10cSrcweir 	return sal_False;
1747*cdf0e10cSrcweir }
1748*cdf0e10cSrcweir 
1749*cdf0e10cSrcweir void BasicManager::SetFlagToAllLibs( short nFlag, sal_Bool bSet ) const
1750*cdf0e10cSrcweir {
1751*cdf0e10cSrcweir 	sal_uInt16 nLibs = GetLibCount();
1752*cdf0e10cSrcweir 	for ( sal_uInt16 nL = 0; nL < nLibs; nL++ )
1753*cdf0e10cSrcweir 	{
1754*cdf0e10cSrcweir 		BasicLibInfo* pInfo = pLibs->GetObject( nL );
1755*cdf0e10cSrcweir 		DBG_ASSERT( pInfo, "Info?!" );
1756*cdf0e10cSrcweir 		StarBASIC* pLib = pInfo->GetLib();
1757*cdf0e10cSrcweir 		if ( pLib )
1758*cdf0e10cSrcweir 		{
1759*cdf0e10cSrcweir 			if ( bSet )
1760*cdf0e10cSrcweir 				pLib->SetFlag( nFlag );
1761*cdf0e10cSrcweir 			else
1762*cdf0e10cSrcweir 				pLib->ResetFlag( nFlag );
1763*cdf0e10cSrcweir 		}
1764*cdf0e10cSrcweir 	}
1765*cdf0e10cSrcweir }
1766*cdf0e10cSrcweir 
1767*cdf0e10cSrcweir sal_Bool BasicManager::HasErrors()
1768*cdf0e10cSrcweir {
1769*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1770*cdf0e10cSrcweir 	return pErrorMgr->HasErrors();
1771*cdf0e10cSrcweir }
1772*cdf0e10cSrcweir 
1773*cdf0e10cSrcweir void BasicManager::ClearErrors()
1774*cdf0e10cSrcweir {
1775*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1776*cdf0e10cSrcweir 	pErrorMgr->Reset();
1777*cdf0e10cSrcweir }
1778*cdf0e10cSrcweir 
1779*cdf0e10cSrcweir BasicError* BasicManager::GetFirstError()
1780*cdf0e10cSrcweir {
1781*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1782*cdf0e10cSrcweir 	return pErrorMgr->GetFirstError();
1783*cdf0e10cSrcweir }
1784*cdf0e10cSrcweir 
1785*cdf0e10cSrcweir BasicError* BasicManager::GetNextError()
1786*cdf0e10cSrcweir {
1787*cdf0e10cSrcweir 	DBG_CHKTHIS( BasicManager, 0 );
1788*cdf0e10cSrcweir 	return pErrorMgr->GetNextError();
1789*cdf0e10cSrcweir }
1790*cdf0e10cSrcweir bool BasicManager::GetGlobalUNOConstant( const sal_Char* _pAsciiName, ::com::sun::star::uno::Any& aOut )
1791*cdf0e10cSrcweir {
1792*cdf0e10cSrcweir     bool bRes = false;
1793*cdf0e10cSrcweir     StarBASIC* pStandardLib = GetStdLib();
1794*cdf0e10cSrcweir     OSL_PRECOND( pStandardLib, "BasicManager::GetGlobalUNOConstant: no lib to read from!" );
1795*cdf0e10cSrcweir     if ( pStandardLib )
1796*cdf0e10cSrcweir         bRes = pStandardLib->GetUNOConstant( _pAsciiName, aOut );
1797*cdf0e10cSrcweir     return bRes;
1798*cdf0e10cSrcweir }
1799*cdf0e10cSrcweir 
1800*cdf0e10cSrcweir Any BasicManager::SetGlobalUNOConstant( const sal_Char* _pAsciiName, const Any& _rValue )
1801*cdf0e10cSrcweir {
1802*cdf0e10cSrcweir     Any aOldValue;
1803*cdf0e10cSrcweir 
1804*cdf0e10cSrcweir     StarBASIC* pStandardLib = GetStdLib();
1805*cdf0e10cSrcweir     OSL_PRECOND( pStandardLib, "BasicManager::SetGlobalUNOConstant: no lib to insert into!" );
1806*cdf0e10cSrcweir     if ( !pStandardLib )
1807*cdf0e10cSrcweir         return aOldValue;
1808*cdf0e10cSrcweir 
1809*cdf0e10cSrcweir     ::rtl::OUString sVarName( ::rtl::OUString::createFromAscii( _pAsciiName ) );
1810*cdf0e10cSrcweir 
1811*cdf0e10cSrcweir     // obtain the old value
1812*cdf0e10cSrcweir     SbxVariable* pVariable = pStandardLib->Find( sVarName, SbxCLASS_OBJECT );
1813*cdf0e10cSrcweir     if ( pVariable )
1814*cdf0e10cSrcweir         aOldValue = sbxToUnoValue( pVariable );
1815*cdf0e10cSrcweir 
1816*cdf0e10cSrcweir     SbxObjectRef xUnoObj = GetSbUnoObject( sVarName, _rValue );
1817*cdf0e10cSrcweir     xUnoObj->SetFlag( SBX_DONTSTORE );
1818*cdf0e10cSrcweir     pStandardLib->Insert( xUnoObj );
1819*cdf0e10cSrcweir 
1820*cdf0e10cSrcweir     return aOldValue;
1821*cdf0e10cSrcweir }
1822*cdf0e10cSrcweir 
1823*cdf0e10cSrcweir bool BasicManager::LegacyPsswdBinaryLimitExceeded( ::com::sun::star::uno::Sequence< rtl::OUString >& _out_rModuleNames )
1824*cdf0e10cSrcweir {
1825*cdf0e10cSrcweir     try
1826*cdf0e10cSrcweir     {
1827*cdf0e10cSrcweir         Reference< XNameAccess > xScripts( GetScriptLibraryContainer(), UNO_QUERY_THROW );
1828*cdf0e10cSrcweir         Reference< XLibraryContainerPassword > xPassword( GetScriptLibraryContainer(), UNO_QUERY_THROW );
1829*cdf0e10cSrcweir 
1830*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aNames( xScripts->getElementNames() );
1831*cdf0e10cSrcweir         const ::rtl::OUString* pNames = aNames.getConstArray();
1832*cdf0e10cSrcweir         const ::rtl::OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
1833*cdf0e10cSrcweir         for ( ; pNames != pNamesEnd; ++pNames )
1834*cdf0e10cSrcweir         {
1835*cdf0e10cSrcweir             if( /*pLib->mbSharedIndexFile ||*/ !xPassword->isLibraryPasswordProtected( *pNames ) )
1836*cdf0e10cSrcweir                 continue;
1837*cdf0e10cSrcweir 
1838*cdf0e10cSrcweir             StarBASIC* pBasicLib = GetLib( *pNames );
1839*cdf0e10cSrcweir             if ( !pBasicLib )
1840*cdf0e10cSrcweir                 continue;
1841*cdf0e10cSrcweir 
1842*cdf0e10cSrcweir             Reference< XNameAccess > xScriptLibrary( xScripts->getByName( *pNames ), UNO_QUERY_THROW );
1843*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aElementNames( xScriptLibrary->getElementNames() );
1844*cdf0e10cSrcweir             sal_Int32 nLen = aElementNames.getLength();
1845*cdf0e10cSrcweir 
1846*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aBigModules( nLen );
1847*cdf0e10cSrcweir             sal_Int32 nBigModules = 0;
1848*cdf0e10cSrcweir 
1849*cdf0e10cSrcweir             const ::rtl::OUString* pElementNames = aElementNames.getConstArray();
1850*cdf0e10cSrcweir             const ::rtl::OUString* pElementNamesEnd = aElementNames.getConstArray() + aElementNames.getLength();
1851*cdf0e10cSrcweir             for ( ; pElementNames != pElementNamesEnd; ++pElementNames )
1852*cdf0e10cSrcweir             {
1853*cdf0e10cSrcweir                 SbModule* pMod = pBasicLib->FindModule( *pElementNames );
1854*cdf0e10cSrcweir                 if ( pMod && pMod->ExceedsLegacyModuleSize() )
1855*cdf0e10cSrcweir                     aBigModules[ nBigModules++ ] = *pElementNames;
1856*cdf0e10cSrcweir             }
1857*cdf0e10cSrcweir 
1858*cdf0e10cSrcweir             if ( nBigModules )
1859*cdf0e10cSrcweir             {
1860*cdf0e10cSrcweir                 aBigModules.realloc( nBigModules );
1861*cdf0e10cSrcweir                 _out_rModuleNames = aBigModules;
1862*cdf0e10cSrcweir                 return true;
1863*cdf0e10cSrcweir             }
1864*cdf0e10cSrcweir         }
1865*cdf0e10cSrcweir     }
1866*cdf0e10cSrcweir     catch( const Exception& )
1867*cdf0e10cSrcweir     {
1868*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
1869*cdf0e10cSrcweir     }
1870*cdf0e10cSrcweir     return false;
1871*cdf0e10cSrcweir }
1872*cdf0e10cSrcweir 
1873*cdf0e10cSrcweir 
1874*cdf0e10cSrcweir namespace
1875*cdf0e10cSrcweir {
1876*cdf0e10cSrcweir     SbMethod* lcl_queryMacro( BasicManager* i_manager, String const& i_fullyQualifiedName )
1877*cdf0e10cSrcweir     {
1878*cdf0e10cSrcweir         sal_uInt16 nLast = 0;
1879*cdf0e10cSrcweir         String sMacro = i_fullyQualifiedName;
1880*cdf0e10cSrcweir         String sLibName = sMacro.GetToken( 0, '.', nLast );
1881*cdf0e10cSrcweir         String sModule = sMacro.GetToken( 0, '.', nLast );
1882*cdf0e10cSrcweir         sMacro.Erase( 0, nLast );
1883*cdf0e10cSrcweir 
1884*cdf0e10cSrcweir 	    IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
1885*cdf0e10cSrcweir 	    const CollatorWrapper* pCollator = aIntlWrapper.getCollator();
1886*cdf0e10cSrcweir 	    sal_uInt16 nLibCount = i_manager->GetLibCount();
1887*cdf0e10cSrcweir 	    for ( sal_uInt16 nLib = 0; nLib < nLibCount; ++nLib )
1888*cdf0e10cSrcweir 	    {
1889*cdf0e10cSrcweir 		    if ( COMPARE_EQUAL == pCollator->compareString( i_manager->GetLibName( nLib ), sLibName ) )
1890*cdf0e10cSrcweir 		    {
1891*cdf0e10cSrcweir 			    StarBASIC* pLib = i_manager->GetLib( nLib );
1892*cdf0e10cSrcweir 			    if( !pLib )
1893*cdf0e10cSrcweir 			    {
1894*cdf0e10cSrcweir 				    i_manager->LoadLib( nLib );
1895*cdf0e10cSrcweir 				    pLib = i_manager->GetLib( nLib );
1896*cdf0e10cSrcweir 			    }
1897*cdf0e10cSrcweir 
1898*cdf0e10cSrcweir 			    if( pLib )
1899*cdf0e10cSrcweir 			    {
1900*cdf0e10cSrcweir 				    sal_uInt16 nModCount = pLib->GetModules()->Count();
1901*cdf0e10cSrcweir 				    for( sal_uInt16 nMod = 0; nMod < nModCount; ++nMod )
1902*cdf0e10cSrcweir 				    {
1903*cdf0e10cSrcweir 					    SbModule* pMod = (SbModule*)pLib->GetModules()->Get( nMod );
1904*cdf0e10cSrcweir 					    if ( pMod && COMPARE_EQUAL == pCollator->compareString( pMod->GetName(), sModule ) )
1905*cdf0e10cSrcweir 					    {
1906*cdf0e10cSrcweir 						    SbMethod* pMethod = (SbMethod*)pMod->Find( sMacro, SbxCLASS_METHOD );
1907*cdf0e10cSrcweir 						    if( pMethod )
1908*cdf0e10cSrcweir 							    return pMethod;
1909*cdf0e10cSrcweir 					    }
1910*cdf0e10cSrcweir 				    }
1911*cdf0e10cSrcweir 			    }
1912*cdf0e10cSrcweir 		    }
1913*cdf0e10cSrcweir 	    }
1914*cdf0e10cSrcweir 	    return 0;
1915*cdf0e10cSrcweir     }
1916*cdf0e10cSrcweir }
1917*cdf0e10cSrcweir 
1918*cdf0e10cSrcweir bool BasicManager::HasMacro( String const& i_fullyQualifiedName ) const
1919*cdf0e10cSrcweir {
1920*cdf0e10cSrcweir     return ( NULL != lcl_queryMacro( const_cast< BasicManager* >( this ), i_fullyQualifiedName ) );
1921*cdf0e10cSrcweir }
1922*cdf0e10cSrcweir 
1923*cdf0e10cSrcweir ErrCode BasicManager::ExecuteMacro( String const& i_fullyQualifiedName, SbxArray* i_arguments, SbxValue* i_retValue )
1924*cdf0e10cSrcweir {
1925*cdf0e10cSrcweir     SbMethod* pMethod = lcl_queryMacro( this, i_fullyQualifiedName );
1926*cdf0e10cSrcweir     ErrCode nError = 0;
1927*cdf0e10cSrcweir     if ( pMethod )
1928*cdf0e10cSrcweir     {
1929*cdf0e10cSrcweir         if ( i_arguments )
1930*cdf0e10cSrcweir             pMethod->SetParameters( i_arguments );
1931*cdf0e10cSrcweir         nError = pMethod->Call( i_retValue );
1932*cdf0e10cSrcweir     }
1933*cdf0e10cSrcweir     else
1934*cdf0e10cSrcweir         nError = ERRCODE_BASIC_PROC_UNDEFINED;
1935*cdf0e10cSrcweir     return nError;
1936*cdf0e10cSrcweir }
1937*cdf0e10cSrcweir 
1938*cdf0e10cSrcweir ErrCode BasicManager::ExecuteMacro( String const& i_fullyQualifiedName, String const& i_commaSeparatedArgs, SbxValue* i_retValue )
1939*cdf0e10cSrcweir {
1940*cdf0e10cSrcweir     SbMethod* pMethod = lcl_queryMacro( this, i_fullyQualifiedName );
1941*cdf0e10cSrcweir     if ( !pMethod )
1942*cdf0e10cSrcweir         return ERRCODE_BASIC_PROC_UNDEFINED;
1943*cdf0e10cSrcweir 
1944*cdf0e10cSrcweir     // arguments must be quoted
1945*cdf0e10cSrcweir     String sQuotedArgs;
1946*cdf0e10cSrcweir     String sArgs( i_commaSeparatedArgs );
1947*cdf0e10cSrcweir     if ( sArgs.Len()<2 || sArgs.GetBuffer()[1] == '\"')
1948*cdf0e10cSrcweir         // no args or already quoted args
1949*cdf0e10cSrcweir         sQuotedArgs = sArgs;
1950*cdf0e10cSrcweir     else
1951*cdf0e10cSrcweir     {
1952*cdf0e10cSrcweir         // quote parameters
1953*cdf0e10cSrcweir         sArgs.Erase( 0, 1 );
1954*cdf0e10cSrcweir         sArgs.Erase( sArgs.Len()-1, 1 );
1955*cdf0e10cSrcweir 
1956*cdf0e10cSrcweir         sQuotedArgs = '(';
1957*cdf0e10cSrcweir 
1958*cdf0e10cSrcweir         sal_uInt16 nCount = sArgs.GetTokenCount(',');
1959*cdf0e10cSrcweir         for ( sal_uInt16 n=0; n<nCount; ++n )
1960*cdf0e10cSrcweir         {
1961*cdf0e10cSrcweir             sQuotedArgs += '\"';
1962*cdf0e10cSrcweir             sQuotedArgs += sArgs.GetToken( n, ',' );
1963*cdf0e10cSrcweir             sQuotedArgs += '\"';
1964*cdf0e10cSrcweir             if ( n<nCount-1 )
1965*cdf0e10cSrcweir                 sQuotedArgs += ',';
1966*cdf0e10cSrcweir         }
1967*cdf0e10cSrcweir 
1968*cdf0e10cSrcweir         sQuotedArgs += ')';
1969*cdf0e10cSrcweir     }
1970*cdf0e10cSrcweir 
1971*cdf0e10cSrcweir     // add quoted arguments and do the call
1972*cdf0e10cSrcweir     String sCall( '[' );
1973*cdf0e10cSrcweir     sCall += pMethod->GetName();
1974*cdf0e10cSrcweir     sCall += sQuotedArgs;
1975*cdf0e10cSrcweir     sCall += ']';
1976*cdf0e10cSrcweir 
1977*cdf0e10cSrcweir 	SbxVariable* pRet = pMethod->GetParent()->Execute( sCall );
1978*cdf0e10cSrcweir     if ( pRet && ( pRet != pMethod ) )
1979*cdf0e10cSrcweir         *i_retValue = *pRet;
1980*cdf0e10cSrcweir     return SbxBase::GetError();
1981*cdf0e10cSrcweir }
1982*cdf0e10cSrcweir 
1983*cdf0e10cSrcweir //=====================================================================
1984*cdf0e10cSrcweir 
1985*cdf0e10cSrcweir class ModuleInfo_Impl : public ModuleInfoHelper
1986*cdf0e10cSrcweir {
1987*cdf0e10cSrcweir 	::rtl::OUString maName;
1988*cdf0e10cSrcweir 	::rtl::OUString maLanguage;
1989*cdf0e10cSrcweir 	::rtl::OUString maSource;
1990*cdf0e10cSrcweir 
1991*cdf0e10cSrcweir public:
1992*cdf0e10cSrcweir 	ModuleInfo_Impl( const ::rtl::OUString& aName, const ::rtl::OUString& aLanguage, const ::rtl::OUString& aSource )
1993*cdf0e10cSrcweir 		: maName( aName ), maLanguage( aLanguage), maSource( aSource ) {}
1994*cdf0e10cSrcweir 
1995*cdf0e10cSrcweir     // Methods XStarBasicModuleInfo
1996*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getName() throw(RuntimeException)
1997*cdf0e10cSrcweir 		{ return maName; }
1998*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getLanguage() throw(RuntimeException)
1999*cdf0e10cSrcweir 		{ return maLanguage; }
2000*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getSource() throw(RuntimeException)
2001*cdf0e10cSrcweir 		{ return maSource; }
2002*cdf0e10cSrcweir };
2003*cdf0e10cSrcweir 
2004*cdf0e10cSrcweir 
2005*cdf0e10cSrcweir //=====================================================================
2006*cdf0e10cSrcweir 
2007*cdf0e10cSrcweir class DialogInfo_Impl : public DialogInfoHelper
2008*cdf0e10cSrcweir {
2009*cdf0e10cSrcweir 	::rtl::OUString maName;
2010*cdf0e10cSrcweir 	Sequence< sal_Int8 > mData;
2011*cdf0e10cSrcweir 
2012*cdf0e10cSrcweir public:
2013*cdf0e10cSrcweir 	DialogInfo_Impl( const ::rtl::OUString& aName, Sequence< sal_Int8 > Data )
2014*cdf0e10cSrcweir 		: maName( aName ), mData( Data ) {}
2015*cdf0e10cSrcweir 
2016*cdf0e10cSrcweir     // Methods XStarBasicDialogInfo
2017*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getName() throw(RuntimeException)
2018*cdf0e10cSrcweir 		{ return maName; }
2019*cdf0e10cSrcweir     virtual Sequence< sal_Int8 > SAL_CALL getData() throw(RuntimeException)
2020*cdf0e10cSrcweir 		{ return mData; }
2021*cdf0e10cSrcweir };
2022*cdf0e10cSrcweir 
2023*cdf0e10cSrcweir 
2024*cdf0e10cSrcweir //=====================================================================
2025*cdf0e10cSrcweir 
2026*cdf0e10cSrcweir class LibraryInfo_Impl : public LibraryInfoHelper
2027*cdf0e10cSrcweir {
2028*cdf0e10cSrcweir 	::rtl::OUString maName;
2029*cdf0e10cSrcweir 	Reference< XNameContainer > mxModuleContainer;
2030*cdf0e10cSrcweir 	Reference< XNameContainer > mxDialogContainer;
2031*cdf0e10cSrcweir 	::rtl::OUString maPassword;
2032*cdf0e10cSrcweir 	::rtl::OUString maExternaleSourceURL;
2033*cdf0e10cSrcweir 	::rtl::OUString maLinkTargetURL;
2034*cdf0e10cSrcweir 
2035*cdf0e10cSrcweir public:
2036*cdf0e10cSrcweir 	LibraryInfo_Impl
2037*cdf0e10cSrcweir 	(
2038*cdf0e10cSrcweir 		const ::rtl::OUString& aName,
2039*cdf0e10cSrcweir 		Reference< XNameContainer > xModuleContainer,
2040*cdf0e10cSrcweir 		Reference< XNameContainer > xDialogContainer,
2041*cdf0e10cSrcweir 		const ::rtl::OUString& aPassword,
2042*cdf0e10cSrcweir 		const ::rtl::OUString& aExternaleSourceURL,
2043*cdf0e10cSrcweir 		const ::rtl::OUString& aLinkTargetURL
2044*cdf0e10cSrcweir 	)
2045*cdf0e10cSrcweir 		: maName( aName )
2046*cdf0e10cSrcweir 		, mxModuleContainer( xModuleContainer )
2047*cdf0e10cSrcweir 		, mxDialogContainer( xDialogContainer )
2048*cdf0e10cSrcweir 		, maPassword( aPassword )
2049*cdf0e10cSrcweir 		, maExternaleSourceURL( aExternaleSourceURL )
2050*cdf0e10cSrcweir 		, maLinkTargetURL( aLinkTargetURL )
2051*cdf0e10cSrcweir 	{}
2052*cdf0e10cSrcweir 
2053*cdf0e10cSrcweir     // Methods XStarBasicLibraryInfo
2054*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getName() throw(RuntimeException)
2055*cdf0e10cSrcweir 		{ return maName; }
2056*cdf0e10cSrcweir     virtual Reference< XNameContainer > SAL_CALL getModuleContainer() throw(RuntimeException)
2057*cdf0e10cSrcweir 		{ return mxModuleContainer; }
2058*cdf0e10cSrcweir     virtual Reference< XNameContainer > SAL_CALL getDialogContainer() throw(RuntimeException)
2059*cdf0e10cSrcweir 		{ return mxDialogContainer; }
2060*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getPassword() throw(RuntimeException)
2061*cdf0e10cSrcweir 		{ return maPassword; }
2062*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getExternalSourceURL() throw(RuntimeException)
2063*cdf0e10cSrcweir 		{ return maExternaleSourceURL; }
2064*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getLinkTargetURL() throw(RuntimeException)
2065*cdf0e10cSrcweir 		{ return maLinkTargetURL; }
2066*cdf0e10cSrcweir };
2067*cdf0e10cSrcweir 
2068*cdf0e10cSrcweir //=====================================================================
2069*cdf0e10cSrcweir 
2070*cdf0e10cSrcweir class ModuleContainer_Impl : public NameContainerHelper
2071*cdf0e10cSrcweir {
2072*cdf0e10cSrcweir 	StarBASIC* mpLib;
2073*cdf0e10cSrcweir 
2074*cdf0e10cSrcweir public:
2075*cdf0e10cSrcweir 	ModuleContainer_Impl( StarBASIC* pLib )
2076*cdf0e10cSrcweir 		:mpLib( pLib ) {}
2077*cdf0e10cSrcweir 
2078*cdf0e10cSrcweir     // Methods XElementAccess
2079*cdf0e10cSrcweir     virtual Type SAL_CALL getElementType()
2080*cdf0e10cSrcweir 		throw(RuntimeException);
2081*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasElements()
2082*cdf0e10cSrcweir 		throw(RuntimeException);
2083*cdf0e10cSrcweir 
2084*cdf0e10cSrcweir     // Methods XNameAccess
2085*cdf0e10cSrcweir     virtual Any SAL_CALL getByName( const ::rtl::OUString& aName )
2086*cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, RuntimeException);
2087*cdf0e10cSrcweir     virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames()
2088*cdf0e10cSrcweir 		throw(RuntimeException);
2089*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
2090*cdf0e10cSrcweir 		throw(RuntimeException);
2091*cdf0e10cSrcweir 
2092*cdf0e10cSrcweir     // Methods XNameReplace
2093*cdf0e10cSrcweir     virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const Any& aElement )
2094*cdf0e10cSrcweir 		throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException);
2095*cdf0e10cSrcweir 
2096*cdf0e10cSrcweir     // Methods XNameContainer
2097*cdf0e10cSrcweir     virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const Any& aElement )
2098*cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException);
2099*cdf0e10cSrcweir     virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
2100*cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, RuntimeException);
2101*cdf0e10cSrcweir };
2102*cdf0e10cSrcweir 
2103*cdf0e10cSrcweir // Methods XElementAccess
2104*cdf0e10cSrcweir Type ModuleContainer_Impl::getElementType()
2105*cdf0e10cSrcweir 	throw(RuntimeException)
2106*cdf0e10cSrcweir {
2107*cdf0e10cSrcweir 	Type aModuleType = ::getCppuType( (const Reference< XStarBasicModuleInfo > *)0 );
2108*cdf0e10cSrcweir 	return aModuleType;
2109*cdf0e10cSrcweir }
2110*cdf0e10cSrcweir 
2111*cdf0e10cSrcweir sal_Bool ModuleContainer_Impl::hasElements()
2112*cdf0e10cSrcweir 	throw(RuntimeException)
2113*cdf0e10cSrcweir {
2114*cdf0e10cSrcweir     SbxArray* pMods = mpLib ? mpLib->GetModules() : NULL;
2115*cdf0e10cSrcweir     return pMods && pMods->Count() > 0;
2116*cdf0e10cSrcweir }
2117*cdf0e10cSrcweir 
2118*cdf0e10cSrcweir // Methods XNameAccess
2119*cdf0e10cSrcweir Any ModuleContainer_Impl::getByName( const ::rtl::OUString& aName )
2120*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2121*cdf0e10cSrcweir {
2122*cdf0e10cSrcweir     SbModule* pMod = mpLib ? mpLib->FindModule( aName ) : NULL;
2123*cdf0e10cSrcweir 	if( !pMod )
2124*cdf0e10cSrcweir 		throw NoSuchElementException();
2125*cdf0e10cSrcweir 	Reference< XStarBasicModuleInfo > xMod = (XStarBasicModuleInfo*)new ModuleInfo_Impl
2126*cdf0e10cSrcweir 		( aName, ::rtl::OUString::createFromAscii( szScriptLanguage ), pMod->GetSource32() );
2127*cdf0e10cSrcweir 	Any aRetAny;
2128*cdf0e10cSrcweir 	aRetAny <<= xMod;
2129*cdf0e10cSrcweir 	return aRetAny;
2130*cdf0e10cSrcweir }
2131*cdf0e10cSrcweir 
2132*cdf0e10cSrcweir Sequence< ::rtl::OUString > ModuleContainer_Impl::getElementNames()
2133*cdf0e10cSrcweir 	throw(RuntimeException)
2134*cdf0e10cSrcweir {
2135*cdf0e10cSrcweir     SbxArray* pMods = mpLib ? mpLib->GetModules() : NULL;
2136*cdf0e10cSrcweir     sal_uInt16 nMods = pMods ? pMods->Count() : 0;
2137*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aRetSeq( nMods );
2138*cdf0e10cSrcweir 	::rtl::OUString* pRetSeq = aRetSeq.getArray();
2139*cdf0e10cSrcweir 	for( sal_uInt16 i = 0 ; i < nMods ; i++ )
2140*cdf0e10cSrcweir 	{
2141*cdf0e10cSrcweir 		SbxVariable* pMod = pMods->Get( i );
2142*cdf0e10cSrcweir 		pRetSeq[i] = ::rtl::OUString( pMod->GetName() );
2143*cdf0e10cSrcweir 	}
2144*cdf0e10cSrcweir 	return aRetSeq;
2145*cdf0e10cSrcweir }
2146*cdf0e10cSrcweir 
2147*cdf0e10cSrcweir sal_Bool ModuleContainer_Impl::hasByName( const ::rtl::OUString& aName )
2148*cdf0e10cSrcweir 	throw(RuntimeException)
2149*cdf0e10cSrcweir {
2150*cdf0e10cSrcweir     SbModule* pMod = mpLib ? mpLib->FindModule( aName ) : NULL;
2151*cdf0e10cSrcweir 	sal_Bool bRet = (pMod != NULL);
2152*cdf0e10cSrcweir 	return bRet;
2153*cdf0e10cSrcweir }
2154*cdf0e10cSrcweir 
2155*cdf0e10cSrcweir 
2156*cdf0e10cSrcweir // Methods XNameReplace
2157*cdf0e10cSrcweir void ModuleContainer_Impl::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
2158*cdf0e10cSrcweir 	throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
2159*cdf0e10cSrcweir {
2160*cdf0e10cSrcweir 	removeByName( aName );
2161*cdf0e10cSrcweir 	insertByName( aName, aElement );
2162*cdf0e10cSrcweir }
2163*cdf0e10cSrcweir 
2164*cdf0e10cSrcweir 
2165*cdf0e10cSrcweir // Methods XNameContainer
2166*cdf0e10cSrcweir void ModuleContainer_Impl::insertByName( const ::rtl::OUString& aName, const Any& aElement )
2167*cdf0e10cSrcweir 	throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
2168*cdf0e10cSrcweir {
2169*cdf0e10cSrcweir 	Type aModuleType = ::getCppuType( (const Reference< XStarBasicModuleInfo > *)0 );
2170*cdf0e10cSrcweir 	Type aAnyType = aElement.getValueType();
2171*cdf0e10cSrcweir 	if( aModuleType != aAnyType )
2172*cdf0e10cSrcweir 		throw IllegalArgumentException();
2173*cdf0e10cSrcweir 	Reference< XStarBasicModuleInfo > xMod;
2174*cdf0e10cSrcweir 	aElement >>= xMod;
2175*cdf0e10cSrcweir 	mpLib->MakeModule32( aName, xMod->getSource() );
2176*cdf0e10cSrcweir }
2177*cdf0e10cSrcweir 
2178*cdf0e10cSrcweir void ModuleContainer_Impl::removeByName( const ::rtl::OUString& Name )
2179*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2180*cdf0e10cSrcweir {
2181*cdf0e10cSrcweir     SbModule* pMod = mpLib ? mpLib->FindModule( Name ) : NULL;
2182*cdf0e10cSrcweir 	if( !pMod )
2183*cdf0e10cSrcweir 		throw NoSuchElementException();
2184*cdf0e10cSrcweir 	mpLib->Remove( pMod );
2185*cdf0e10cSrcweir }
2186*cdf0e10cSrcweir 
2187*cdf0e10cSrcweir 
2188*cdf0e10cSrcweir //=====================================================================
2189*cdf0e10cSrcweir 
2190*cdf0e10cSrcweir Sequence< sal_Int8 > implGetDialogData( SbxObject* pDialog )
2191*cdf0e10cSrcweir {
2192*cdf0e10cSrcweir 	SvMemoryStream aMemStream;
2193*cdf0e10cSrcweir 	pDialog->Store( aMemStream );
2194*cdf0e10cSrcweir 	sal_Int32 nLen = aMemStream.Tell();
2195*cdf0e10cSrcweir 	Sequence< sal_Int8 > aData( nLen );
2196*cdf0e10cSrcweir 	sal_Int8* pDestData = aData.getArray();
2197*cdf0e10cSrcweir 	const sal_Int8* pSrcData = (const sal_Int8*)aMemStream.GetData();
2198*cdf0e10cSrcweir 	rtl_copyMemory( pDestData, pSrcData, nLen );
2199*cdf0e10cSrcweir 	return aData;
2200*cdf0e10cSrcweir }
2201*cdf0e10cSrcweir 
2202*cdf0e10cSrcweir SbxObject* implCreateDialog( Sequence< sal_Int8 > aData )
2203*cdf0e10cSrcweir {
2204*cdf0e10cSrcweir 	sal_Int8* pData = aData.getArray();
2205*cdf0e10cSrcweir 	SvMemoryStream aMemStream( pData, aData.getLength(), STREAM_READ );
2206*cdf0e10cSrcweir 	SbxObject* pDialog = (SbxObject*)SbxBase::Load( aMemStream );
2207*cdf0e10cSrcweir 	return pDialog;
2208*cdf0e10cSrcweir }
2209*cdf0e10cSrcweir 
2210*cdf0e10cSrcweir // HACK! Because this value is defined in basctl/inc/vcsbxdef.hxx
2211*cdf0e10cSrcweir // which we can't include here, we have to use the value directly
2212*cdf0e10cSrcweir #define SBXID_DIALOG		101
2213*cdf0e10cSrcweir 
2214*cdf0e10cSrcweir 
2215*cdf0e10cSrcweir class DialogContainer_Impl : public NameContainerHelper
2216*cdf0e10cSrcweir {
2217*cdf0e10cSrcweir 	StarBASIC* mpLib;
2218*cdf0e10cSrcweir 
2219*cdf0e10cSrcweir public:
2220*cdf0e10cSrcweir 	DialogContainer_Impl( StarBASIC* pLib )
2221*cdf0e10cSrcweir 		:mpLib( pLib ) {}
2222*cdf0e10cSrcweir 
2223*cdf0e10cSrcweir     // Methods XElementAccess
2224*cdf0e10cSrcweir     virtual Type SAL_CALL getElementType()
2225*cdf0e10cSrcweir 		throw(RuntimeException);
2226*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasElements()
2227*cdf0e10cSrcweir 		throw(RuntimeException);
2228*cdf0e10cSrcweir 
2229*cdf0e10cSrcweir     // Methods XNameAccess
2230*cdf0e10cSrcweir     virtual Any SAL_CALL getByName( const ::rtl::OUString& aName )
2231*cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, RuntimeException);
2232*cdf0e10cSrcweir     virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames()
2233*cdf0e10cSrcweir 		throw(RuntimeException);
2234*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
2235*cdf0e10cSrcweir 		throw(RuntimeException);
2236*cdf0e10cSrcweir 
2237*cdf0e10cSrcweir     // Methods XNameReplace
2238*cdf0e10cSrcweir     virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const Any& aElement )
2239*cdf0e10cSrcweir 		throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException);
2240*cdf0e10cSrcweir 
2241*cdf0e10cSrcweir     // Methods XNameContainer
2242*cdf0e10cSrcweir     virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const Any& aElement )
2243*cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException);
2244*cdf0e10cSrcweir     virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
2245*cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, RuntimeException);
2246*cdf0e10cSrcweir };
2247*cdf0e10cSrcweir 
2248*cdf0e10cSrcweir // Methods XElementAccess
2249*cdf0e10cSrcweir Type DialogContainer_Impl::getElementType()
2250*cdf0e10cSrcweir 	throw(RuntimeException)
2251*cdf0e10cSrcweir {
2252*cdf0e10cSrcweir 	Type aModuleType = ::getCppuType( (const Reference< XStarBasicDialogInfo > *)0 );
2253*cdf0e10cSrcweir 	return aModuleType;
2254*cdf0e10cSrcweir }
2255*cdf0e10cSrcweir 
2256*cdf0e10cSrcweir sal_Bool DialogContainer_Impl::hasElements()
2257*cdf0e10cSrcweir 	throw(RuntimeException)
2258*cdf0e10cSrcweir {
2259*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
2260*cdf0e10cSrcweir 
2261*cdf0e10cSrcweir 	mpLib->GetAll( SbxCLASS_OBJECT );
2262*cdf0e10cSrcweir 	sal_Int16 nCount = mpLib->GetObjects()->Count();
2263*cdf0e10cSrcweir 	for( sal_Int16 nObj = 0; nObj < nCount ; nObj++ )
2264*cdf0e10cSrcweir 	{
2265*cdf0e10cSrcweir 		SbxVariable* pVar = mpLib->GetObjects()->Get( nObj );
2266*cdf0e10cSrcweir 		if ( pVar->ISA( SbxObject ) && ( ((SbxObject*)pVar)->GetSbxId() == SBXID_DIALOG ) )
2267*cdf0e10cSrcweir 		{
2268*cdf0e10cSrcweir 			bRet = sal_True;
2269*cdf0e10cSrcweir 			break;
2270*cdf0e10cSrcweir 		}
2271*cdf0e10cSrcweir 	}
2272*cdf0e10cSrcweir 	return bRet;
2273*cdf0e10cSrcweir }
2274*cdf0e10cSrcweir 
2275*cdf0e10cSrcweir // Methods XNameAccess
2276*cdf0e10cSrcweir Any DialogContainer_Impl::getByName( const ::rtl::OUString& aName )
2277*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2278*cdf0e10cSrcweir {
2279*cdf0e10cSrcweir 	SbxVariable* pVar = mpLib->GetObjects()->Find( aName, SbxCLASS_DONTCARE );
2280*cdf0e10cSrcweir 	if( !( pVar && pVar->ISA( SbxObject ) &&
2281*cdf0e10cSrcweir 		   ( ((SbxObject*)pVar)->GetSbxId() == SBXID_DIALOG ) ) )
2282*cdf0e10cSrcweir 	{
2283*cdf0e10cSrcweir 		throw NoSuchElementException();
2284*cdf0e10cSrcweir 	}
2285*cdf0e10cSrcweir 
2286*cdf0e10cSrcweir 	Reference< XStarBasicDialogInfo > xDialog =
2287*cdf0e10cSrcweir 		(XStarBasicDialogInfo*)new DialogInfo_Impl
2288*cdf0e10cSrcweir 			( aName, implGetDialogData( (SbxObject*)pVar ) );
2289*cdf0e10cSrcweir 
2290*cdf0e10cSrcweir 	Any aRetAny;
2291*cdf0e10cSrcweir 	aRetAny <<= xDialog;
2292*cdf0e10cSrcweir 	return aRetAny;
2293*cdf0e10cSrcweir }
2294*cdf0e10cSrcweir 
2295*cdf0e10cSrcweir Sequence< ::rtl::OUString > DialogContainer_Impl::getElementNames()
2296*cdf0e10cSrcweir 	throw(RuntimeException)
2297*cdf0e10cSrcweir {
2298*cdf0e10cSrcweir 	mpLib->GetAll( SbxCLASS_OBJECT );
2299*cdf0e10cSrcweir 	sal_Int16 nCount = mpLib->GetObjects()->Count();
2300*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aRetSeq( nCount );
2301*cdf0e10cSrcweir 	::rtl::OUString* pRetSeq = aRetSeq.getArray();
2302*cdf0e10cSrcweir 	sal_Int32 nDialogCounter = 0;
2303*cdf0e10cSrcweir 
2304*cdf0e10cSrcweir 	for( sal_Int16 nObj = 0; nObj < nCount ; nObj++ )
2305*cdf0e10cSrcweir 	{
2306*cdf0e10cSrcweir 		SbxVariable* pVar = mpLib->GetObjects()->Get( nObj );
2307*cdf0e10cSrcweir 		if ( pVar->ISA( SbxObject ) && ( ((SbxObject*)pVar)->GetSbxId() == SBXID_DIALOG ) )
2308*cdf0e10cSrcweir 		{
2309*cdf0e10cSrcweir 			pRetSeq[ nDialogCounter ] = ::rtl::OUString( pVar->GetName() );
2310*cdf0e10cSrcweir 			nDialogCounter++;
2311*cdf0e10cSrcweir 		}
2312*cdf0e10cSrcweir 	}
2313*cdf0e10cSrcweir 	aRetSeq.realloc( nDialogCounter );
2314*cdf0e10cSrcweir 	return aRetSeq;
2315*cdf0e10cSrcweir }
2316*cdf0e10cSrcweir 
2317*cdf0e10cSrcweir sal_Bool DialogContainer_Impl::hasByName( const ::rtl::OUString& aName )
2318*cdf0e10cSrcweir 	throw(RuntimeException)
2319*cdf0e10cSrcweir {
2320*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
2321*cdf0e10cSrcweir 	SbxVariable* pVar = mpLib->GetObjects()->Find( aName, SbxCLASS_DONTCARE );
2322*cdf0e10cSrcweir 	if( pVar && pVar->ISA( SbxObject ) &&
2323*cdf0e10cSrcweir 		   ( ((SbxObject*)pVar)->GetSbxId() == SBXID_DIALOG ) )
2324*cdf0e10cSrcweir 	{
2325*cdf0e10cSrcweir 		bRet = sal_True;
2326*cdf0e10cSrcweir 	}
2327*cdf0e10cSrcweir 	return bRet;
2328*cdf0e10cSrcweir }
2329*cdf0e10cSrcweir 
2330*cdf0e10cSrcweir 
2331*cdf0e10cSrcweir // Methods XNameReplace
2332*cdf0e10cSrcweir void DialogContainer_Impl::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
2333*cdf0e10cSrcweir 	throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
2334*cdf0e10cSrcweir {
2335*cdf0e10cSrcweir 	removeByName( aName );
2336*cdf0e10cSrcweir 	insertByName( aName, aElement );
2337*cdf0e10cSrcweir }
2338*cdf0e10cSrcweir 
2339*cdf0e10cSrcweir 
2340*cdf0e10cSrcweir // Methods XNameContainer
2341*cdf0e10cSrcweir void DialogContainer_Impl::insertByName( const ::rtl::OUString& aName, const Any& aElement )
2342*cdf0e10cSrcweir 	throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
2343*cdf0e10cSrcweir {
2344*cdf0e10cSrcweir     (void)aName;
2345*cdf0e10cSrcweir 	Type aModuleType = ::getCppuType( (const Reference< XStarBasicDialogInfo > *)0 );
2346*cdf0e10cSrcweir 	Type aAnyType = aElement.getValueType();
2347*cdf0e10cSrcweir 	if( aModuleType != aAnyType )
2348*cdf0e10cSrcweir 		throw IllegalArgumentException();
2349*cdf0e10cSrcweir 	Reference< XStarBasicDialogInfo > xMod;
2350*cdf0e10cSrcweir 	aElement >>= xMod;
2351*cdf0e10cSrcweir 	SbxObjectRef xDialog = implCreateDialog( xMod->getData() );
2352*cdf0e10cSrcweir 	mpLib->Insert( xDialog );
2353*cdf0e10cSrcweir }
2354*cdf0e10cSrcweir 
2355*cdf0e10cSrcweir void DialogContainer_Impl::removeByName( const ::rtl::OUString& Name )
2356*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2357*cdf0e10cSrcweir {
2358*cdf0e10cSrcweir     (void)Name;
2359*cdf0e10cSrcweir 	SbxVariable* pVar = mpLib->GetObjects()->Find( Name, SbxCLASS_DONTCARE );
2360*cdf0e10cSrcweir 	if( !( pVar && pVar->ISA( SbxObject ) &&
2361*cdf0e10cSrcweir 		   ( ((SbxObject*)pVar)->GetSbxId() == SBXID_DIALOG ) ) )
2362*cdf0e10cSrcweir 	{
2363*cdf0e10cSrcweir 		throw NoSuchElementException();
2364*cdf0e10cSrcweir 	}
2365*cdf0e10cSrcweir 	mpLib->Remove( pVar );
2366*cdf0e10cSrcweir }
2367*cdf0e10cSrcweir 
2368*cdf0e10cSrcweir 
2369*cdf0e10cSrcweir //=====================================================================
2370*cdf0e10cSrcweir 
2371*cdf0e10cSrcweir 
2372*cdf0e10cSrcweir class LibraryContainer_Impl : public NameContainerHelper
2373*cdf0e10cSrcweir {
2374*cdf0e10cSrcweir 	BasicManager* mpMgr;
2375*cdf0e10cSrcweir 
2376*cdf0e10cSrcweir public:
2377*cdf0e10cSrcweir 	LibraryContainer_Impl( BasicManager* pMgr )
2378*cdf0e10cSrcweir 		:mpMgr( pMgr ) {}
2379*cdf0e10cSrcweir 
2380*cdf0e10cSrcweir     // Methods XElementAccess
2381*cdf0e10cSrcweir     virtual Type SAL_CALL getElementType()
2382*cdf0e10cSrcweir 		throw(RuntimeException);
2383*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasElements()
2384*cdf0e10cSrcweir 		throw(RuntimeException);
2385*cdf0e10cSrcweir 
2386*cdf0e10cSrcweir     // Methods XNameAccess
2387*cdf0e10cSrcweir     virtual Any SAL_CALL getByName( const ::rtl::OUString& aName )
2388*cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, RuntimeException);
2389*cdf0e10cSrcweir     virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames()
2390*cdf0e10cSrcweir 		throw(RuntimeException);
2391*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
2392*cdf0e10cSrcweir 		throw(RuntimeException);
2393*cdf0e10cSrcweir 
2394*cdf0e10cSrcweir     // Methods XNameReplace
2395*cdf0e10cSrcweir     virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const Any& aElement )
2396*cdf0e10cSrcweir 		throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException);
2397*cdf0e10cSrcweir 
2398*cdf0e10cSrcweir     // Methods XNameContainer
2399*cdf0e10cSrcweir     virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const Any& aElement )
2400*cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException);
2401*cdf0e10cSrcweir     virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
2402*cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, RuntimeException);
2403*cdf0e10cSrcweir };
2404*cdf0e10cSrcweir 
2405*cdf0e10cSrcweir 
2406*cdf0e10cSrcweir // Methods XElementAccess
2407*cdf0e10cSrcweir Type LibraryContainer_Impl::getElementType()
2408*cdf0e10cSrcweir 	throw(RuntimeException)
2409*cdf0e10cSrcweir {
2410*cdf0e10cSrcweir 	Type aType = ::getCppuType( (const Reference< XStarBasicLibraryInfo > *)0 );
2411*cdf0e10cSrcweir 	return aType;
2412*cdf0e10cSrcweir }
2413*cdf0e10cSrcweir 
2414*cdf0e10cSrcweir sal_Bool LibraryContainer_Impl::hasElements()
2415*cdf0e10cSrcweir 	throw(RuntimeException)
2416*cdf0e10cSrcweir {
2417*cdf0e10cSrcweir 	sal_Int32 nLibs = mpMgr->GetLibCount();
2418*cdf0e10cSrcweir 	sal_Bool bRet = (nLibs > 0);
2419*cdf0e10cSrcweir 	return bRet;
2420*cdf0e10cSrcweir }
2421*cdf0e10cSrcweir 
2422*cdf0e10cSrcweir // Methods XNameAccess
2423*cdf0e10cSrcweir Any LibraryContainer_Impl::getByName( const ::rtl::OUString& aName )
2424*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2425*cdf0e10cSrcweir {
2426*cdf0e10cSrcweir 	Any aRetAny;
2427*cdf0e10cSrcweir 	if( !mpMgr->HasLib( aName ) )
2428*cdf0e10cSrcweir 		throw NoSuchElementException();
2429*cdf0e10cSrcweir 	StarBASIC* pLib = mpMgr->GetLib( aName );
2430*cdf0e10cSrcweir 
2431*cdf0e10cSrcweir 	Reference< XNameContainer > xModuleContainer =
2432*cdf0e10cSrcweir 		(XNameContainer*)new ModuleContainer_Impl( pLib );
2433*cdf0e10cSrcweir 
2434*cdf0e10cSrcweir 	Reference< XNameContainer > xDialogContainer;
2435*cdf0e10cSrcweir 		(XNameContainer*)new DialogContainer_Impl( pLib );
2436*cdf0e10cSrcweir 
2437*cdf0e10cSrcweir 	BasicLibInfo* pLibInfo = mpMgr->FindLibInfo( pLib );
2438*cdf0e10cSrcweir 
2439*cdf0e10cSrcweir 	::rtl::OUString aPassword = pLibInfo->GetPassword();
2440*cdf0e10cSrcweir 
2441*cdf0e10cSrcweir 	// TODO Only provide extern info!
2442*cdf0e10cSrcweir 	::rtl::OUString aExternaleSourceURL;
2443*cdf0e10cSrcweir 	::rtl::OUString aLinkTargetURL;
2444*cdf0e10cSrcweir 	if( pLibInfo->IsReference() )
2445*cdf0e10cSrcweir 		aLinkTargetURL = pLibInfo->GetStorageName();
2446*cdf0e10cSrcweir 	else if( pLibInfo->IsExtern() )
2447*cdf0e10cSrcweir 		aExternaleSourceURL = pLibInfo->GetStorageName();
2448*cdf0e10cSrcweir 
2449*cdf0e10cSrcweir 	Reference< XStarBasicLibraryInfo > xLibInfo = new LibraryInfo_Impl
2450*cdf0e10cSrcweir 	(
2451*cdf0e10cSrcweir 		aName,
2452*cdf0e10cSrcweir 		xModuleContainer,
2453*cdf0e10cSrcweir 		xDialogContainer,
2454*cdf0e10cSrcweir 		aPassword,
2455*cdf0e10cSrcweir 		aExternaleSourceURL,
2456*cdf0e10cSrcweir 		aLinkTargetURL
2457*cdf0e10cSrcweir 	);
2458*cdf0e10cSrcweir 
2459*cdf0e10cSrcweir 	aRetAny <<= xLibInfo;
2460*cdf0e10cSrcweir 	return aRetAny;
2461*cdf0e10cSrcweir }
2462*cdf0e10cSrcweir 
2463*cdf0e10cSrcweir Sequence< ::rtl::OUString > LibraryContainer_Impl::getElementNames()
2464*cdf0e10cSrcweir 	throw(RuntimeException)
2465*cdf0e10cSrcweir {
2466*cdf0e10cSrcweir 	sal_uInt16 nLibs = mpMgr->GetLibCount();
2467*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aRetSeq( nLibs );
2468*cdf0e10cSrcweir 	::rtl::OUString* pRetSeq = aRetSeq.getArray();
2469*cdf0e10cSrcweir 	for( sal_uInt16 i = 0 ; i < nLibs ; i++ )
2470*cdf0e10cSrcweir 	{
2471*cdf0e10cSrcweir 		pRetSeq[i] = ::rtl::OUString( mpMgr->GetLibName( i ) );
2472*cdf0e10cSrcweir 	}
2473*cdf0e10cSrcweir 	return aRetSeq;
2474*cdf0e10cSrcweir }
2475*cdf0e10cSrcweir 
2476*cdf0e10cSrcweir sal_Bool LibraryContainer_Impl::hasByName( const ::rtl::OUString& aName )
2477*cdf0e10cSrcweir 	throw(RuntimeException)
2478*cdf0e10cSrcweir {
2479*cdf0e10cSrcweir 	sal_Bool bRet = mpMgr->HasLib( aName );
2480*cdf0e10cSrcweir 	return bRet;
2481*cdf0e10cSrcweir }
2482*cdf0e10cSrcweir 
2483*cdf0e10cSrcweir // Methods XNameReplace
2484*cdf0e10cSrcweir void LibraryContainer_Impl::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
2485*cdf0e10cSrcweir 	throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
2486*cdf0e10cSrcweir {
2487*cdf0e10cSrcweir 	removeByName( aName );
2488*cdf0e10cSrcweir 	insertByName( aName, aElement );
2489*cdf0e10cSrcweir }
2490*cdf0e10cSrcweir 
2491*cdf0e10cSrcweir // Methods XNameContainer
2492*cdf0e10cSrcweir void LibraryContainer_Impl::insertByName( const ::rtl::OUString& aName, const Any& aElement )
2493*cdf0e10cSrcweir 	throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
2494*cdf0e10cSrcweir {
2495*cdf0e10cSrcweir     (void)aName;
2496*cdf0e10cSrcweir     (void)aElement;
2497*cdf0e10cSrcweir 	// TODO: Insert a complete Library?!
2498*cdf0e10cSrcweir }
2499*cdf0e10cSrcweir 
2500*cdf0e10cSrcweir void LibraryContainer_Impl::removeByName( const ::rtl::OUString& Name )
2501*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2502*cdf0e10cSrcweir {
2503*cdf0e10cSrcweir 	StarBASIC* pLib = mpMgr->GetLib( Name );
2504*cdf0e10cSrcweir 	if( !pLib )
2505*cdf0e10cSrcweir 		throw NoSuchElementException();
2506*cdf0e10cSrcweir 	sal_uInt16 nLibId = mpMgr->GetLibId( Name );
2507*cdf0e10cSrcweir 	mpMgr->RemoveLib( nLibId );
2508*cdf0e10cSrcweir }
2509*cdf0e10cSrcweir 
2510*cdf0e10cSrcweir //=====================================================================
2511*cdf0e10cSrcweir 
2512*cdf0e10cSrcweir typedef WeakImplHelper1< XStarBasicAccess > StarBasicAccessHelper;
2513*cdf0e10cSrcweir 
2514*cdf0e10cSrcweir 
2515*cdf0e10cSrcweir class StarBasicAccess_Impl : public StarBasicAccessHelper
2516*cdf0e10cSrcweir {
2517*cdf0e10cSrcweir 	BasicManager* mpMgr;
2518*cdf0e10cSrcweir 	Reference< XNameContainer > mxLibContainer;
2519*cdf0e10cSrcweir 
2520*cdf0e10cSrcweir public:
2521*cdf0e10cSrcweir 	StarBasicAccess_Impl( BasicManager* pMgr )
2522*cdf0e10cSrcweir 		:mpMgr( pMgr ) {}
2523*cdf0e10cSrcweir 
2524*cdf0e10cSrcweir public:
2525*cdf0e10cSrcweir 
2526*cdf0e10cSrcweir     // Methods
2527*cdf0e10cSrcweir     virtual Reference< XNameContainer > SAL_CALL getLibraryContainer()
2528*cdf0e10cSrcweir 		throw(RuntimeException);
2529*cdf0e10cSrcweir     virtual void SAL_CALL createLibrary( const ::rtl::OUString& LibName, const ::rtl::OUString& Password,
2530*cdf0e10cSrcweir 		const ::rtl::OUString& ExternalSourceURL, const ::rtl::OUString& LinkTargetURL )
2531*cdf0e10cSrcweir 			throw(ElementExistException, RuntimeException);
2532*cdf0e10cSrcweir     virtual void SAL_CALL addModule( const ::rtl::OUString& LibraryName, const ::rtl::OUString& ModuleName,
2533*cdf0e10cSrcweir 		const ::rtl::OUString& Language, const ::rtl::OUString& Source )
2534*cdf0e10cSrcweir 			throw(NoSuchElementException, RuntimeException);
2535*cdf0e10cSrcweir     virtual void SAL_CALL addDialog( const ::rtl::OUString& LibraryName, const ::rtl::OUString& DialogName,
2536*cdf0e10cSrcweir 		const Sequence< sal_Int8 >& Data )
2537*cdf0e10cSrcweir 			throw(NoSuchElementException, RuntimeException);
2538*cdf0e10cSrcweir 
2539*cdf0e10cSrcweir };
2540*cdf0e10cSrcweir 
2541*cdf0e10cSrcweir Reference< XNameContainer > SAL_CALL StarBasicAccess_Impl::getLibraryContainer()
2542*cdf0e10cSrcweir 	throw(RuntimeException)
2543*cdf0e10cSrcweir {
2544*cdf0e10cSrcweir 	if( !mxLibContainer.is() )
2545*cdf0e10cSrcweir 		mxLibContainer = (XNameContainer*)new LibraryContainer_Impl( mpMgr );
2546*cdf0e10cSrcweir 	return mxLibContainer;
2547*cdf0e10cSrcweir }
2548*cdf0e10cSrcweir 
2549*cdf0e10cSrcweir void SAL_CALL StarBasicAccess_Impl::createLibrary
2550*cdf0e10cSrcweir (
2551*cdf0e10cSrcweir 	const ::rtl::OUString& LibName,
2552*cdf0e10cSrcweir 	const ::rtl::OUString& Password,
2553*cdf0e10cSrcweir 	const ::rtl::OUString& ExternalSourceURL,
2554*cdf0e10cSrcweir 	const ::rtl::OUString& LinkTargetURL
2555*cdf0e10cSrcweir )
2556*cdf0e10cSrcweir 	throw(ElementExistException, RuntimeException)
2557*cdf0e10cSrcweir {
2558*cdf0e10cSrcweir     (void)ExternalSourceURL;
2559*cdf0e10cSrcweir #ifdef DBG_UTIL
2560*cdf0e10cSrcweir 	StarBASIC* pLib =
2561*cdf0e10cSrcweir #endif
2562*cdf0e10cSrcweir 	mpMgr->CreateLib( LibName, Password, LinkTargetURL );
2563*cdf0e10cSrcweir 	DBG_ASSERT( pLib, "XML Import: Basic library could not be created");
2564*cdf0e10cSrcweir }
2565*cdf0e10cSrcweir 
2566*cdf0e10cSrcweir void SAL_CALL StarBasicAccess_Impl::addModule
2567*cdf0e10cSrcweir (
2568*cdf0e10cSrcweir 	const ::rtl::OUString& LibraryName,
2569*cdf0e10cSrcweir 	const ::rtl::OUString& ModuleName,
2570*cdf0e10cSrcweir 	const ::rtl::OUString& Language,
2571*cdf0e10cSrcweir 	const ::rtl::OUString& Source
2572*cdf0e10cSrcweir )
2573*cdf0e10cSrcweir 	throw(NoSuchElementException, RuntimeException)
2574*cdf0e10cSrcweir {
2575*cdf0e10cSrcweir     (void)Language;
2576*cdf0e10cSrcweir 	StarBASIC* pLib = mpMgr->GetLib( LibraryName );
2577*cdf0e10cSrcweir 	DBG_ASSERT( pLib, "XML Import: Lib for module unknown");
2578*cdf0e10cSrcweir 	if( pLib )
2579*cdf0e10cSrcweir 		pLib->MakeModule32( ModuleName, Source );
2580*cdf0e10cSrcweir }
2581*cdf0e10cSrcweir 
2582*cdf0e10cSrcweir void SAL_CALL StarBasicAccess_Impl::addDialog
2583*cdf0e10cSrcweir (
2584*cdf0e10cSrcweir 	const ::rtl::OUString& LibraryName,
2585*cdf0e10cSrcweir 	const ::rtl::OUString& DialogName,
2586*cdf0e10cSrcweir 	const Sequence< sal_Int8 >& Data
2587*cdf0e10cSrcweir )
2588*cdf0e10cSrcweir 	throw(NoSuchElementException, RuntimeException)
2589*cdf0e10cSrcweir {
2590*cdf0e10cSrcweir 	(void)LibraryName;
2591*cdf0e10cSrcweir 	(void)DialogName;
2592*cdf0e10cSrcweir 	(void)Data;
2593*cdf0e10cSrcweir }
2594*cdf0e10cSrcweir 
2595*cdf0e10cSrcweir // Basic XML Import/Export
2596*cdf0e10cSrcweir Reference< XStarBasicAccess > getStarBasicAccess( BasicManager* pMgr )
2597*cdf0e10cSrcweir {
2598*cdf0e10cSrcweir 	Reference< XStarBasicAccess > xRet =
2599*cdf0e10cSrcweir 		new StarBasicAccess_Impl( (BasicManager*)pMgr );
2600*cdf0e10cSrcweir 	return xRet;
2601*cdf0e10cSrcweir }
2602*cdf0e10cSrcweir 
2603