xref: /trunk/main/basic/inc/basic/basmgr.hxx (revision 7fef15a0)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 //
24 #ifndef _BASMGR_HXX
25 #define _BASMGR_HXX
26 
27 #include <tools/string.hxx>
28 #include <svl/brdcst.hxx>
29 #include "basic/basicdllapi.h"
30 #include <basic/sbstar.hxx>
31 #include <com/sun/star/script/XStorageBasedLibraryContainer.hpp>
32 #include <com/sun/star/script/XStarBasicAccess.hpp>
33 
34 
35 // Basic XML Import/Export
36 BASIC_DLLPUBLIC com::sun::star::uno::Reference< com::sun::star::script::XStarBasicAccess >
37 	getStarBasicAccess( BasicManager* pMgr );
38 
39 
40 
41 class SotStorage;
42 
43 #define BASERR_ID_STDLIBOPEN			ERRCODE_BASMGR_STDLIBOPEN
44 #define BASERR_ID_STDLIBSAVE            ERRCODE_BASMGR_STDLIBSAVE
45 #define BASERR_ID_LIBLOAD				ERRCODE_BASMGR_LIBLOAD
46 #define BASERR_ID_LIBCREATE				ERRCODE_BASMGR_LIBCREATE
47 #define BASERR_ID_LIBSAVE               ERRCODE_BASMGR_LIBSAVE
48 #define BASERR_ID_LIBDEL				ERRCODE_BASMGR_LIBDEL
49 #define BASERR_ID_MGROPEN               ERRCODE_BASMGR_MGROPEN
50 #define BASERR_ID_MGRSAVE				ERRCODE_BASMGR_MGRSAVE
51 #define BASERR_ID_REMOVELIB				ERRCODE_BASMGR_REMOVELIB
52 #define BASERR_ID_UNLOADLIB				ERRCODE_BASMGR_UNLOADLIB
53 
54 #define BASERR_REASON_OPENSTORAGE       0x0001
55 #define BASERR_REASON_OPENLIBSTORAGE    0x0002
56 #define BASERR_REASON_OPENMGRSTREAM     0x0004
57 #define BASERR_REASON_OPENLIBSTREAM     0x0008
58 #define BASERR_REASON_LIBNOTFOUND		0x0010
59 #define BASERR_REASON_STORAGENOTFOUND	0x0020
60 #define BASERR_REASON_BASICLOADERROR	0x0040
61 #define BASERR_REASON_NOSTORAGENAME		0x0080
62 
63 #define BASERR_REASON_STDLIB			0x0100
64 
65 class BASIC_DLLPUBLIC BasicError
66 {
67 private:
68 	sal_uIntPtr	nErrorId;
69 	sal_uInt16	nReason;
70 	String	aErrStr;
71 
72 public:
73 			BasicError();
74 			BasicError( const BasicError& rErr );
75 			BasicError( sal_uIntPtr nId, sal_uInt16 nR, const String& rErrStr );
76 
GetErrorId() const77 	sal_uIntPtr 	GetErrorId() const					{ return nErrorId; }
GetReason() const78 	sal_uInt16	GetReason() const					{ return nReason; }
GetErrorStr()79 	String	GetErrorStr()						{ return aErrStr; }
80 
SetErrorId(sal_uIntPtr n)81 	void	SetErrorId( sal_uIntPtr n )				{ nErrorId = n; }
SetReason(sal_uInt16 n)82 	void	SetReason( sal_uInt16 n )				{ nReason = n; }
SetErrorStr(const String & rStr)83 	void	SetErrorStr( const String& rStr)	{ aErrStr = rStr; }
84 };
85 
86 
87 //
88 
89 class BasicLibs;
90 class ErrorManager;
91 class BasicLibInfo;
92 class BasicErrorManager;
93 namespace basic { class BasicManagerCleaner; }
94 
95 // Library password handling for 5.0 documents
96 class BASIC_DLLPUBLIC OldBasicPassword
97 {
98 public:
99     virtual void setLibraryPassword( const String& rLibraryName, const String& rPassword ) = 0;
100     virtual String getLibraryPassword( const String& rLibraryName ) = 0;
101     virtual void clearLibraryPassword( const String& rLibraryName ) = 0;
102     virtual sal_Bool hasLibraryPassword( const String& rLibraryName ) = 0;
103 };
104 
105 struct LibraryContainerInfo
106 {
107     ::com::sun::star::uno::Reference< com::sun::star::script::XPersistentLibraryContainer > mxScriptCont;
108     ::com::sun::star::uno::Reference< com::sun::star::script::XPersistentLibraryContainer > mxDialogCont;
109     OldBasicPassword* mpOldBasicPassword;
110 
LibraryContainerInfoLibraryContainerInfo111     LibraryContainerInfo()
112         :mpOldBasicPassword( NULL )
113     {
114     }
115 
LibraryContainerInfoLibraryContainerInfo116 	LibraryContainerInfo
117     (
118         com::sun::star::uno::Reference< com::sun::star::script::XPersistentLibraryContainer > xScriptCont,
119 	    com::sun::star::uno::Reference< com::sun::star::script::XPersistentLibraryContainer > xDialogCont,
120         OldBasicPassword* pOldBasicPassword
121     )
122         : mxScriptCont( xScriptCont )
123         , mxDialogCont( xDialogCont )
124         , mpOldBasicPassword( pOldBasicPassword )
125 	{}
126 };
127 
128 struct BasicManagerImpl;
129 
130 
131 #define LIB_NOTFOUND	0xFFFF
132 
133 class BASIC_DLLPUBLIC BasicManager : public SfxBroadcaster
134 {
135     friend class LibraryContainer_Impl;
136     friend class StarBasicAccess_Impl;
137     friend class BasMgrContainerListenerImpl;
138     friend class ::basic::BasicManagerCleaner;
139 
140 private:
141 	BasicLibs*			pLibs;
142 	BasicErrorManager*	pErrorMgr;
143 
144 	String				aName;
145 	String				maStorageName;
146 	sal_Bool				bBasMgrModified;
147 	sal_Bool				mbDocMgr;
148 
149 	BasicManagerImpl*	mpImpl;
150 
151 	void				Init();
152 
153 protected:
154 	sal_Bool			ImpLoadLibary( BasicLibInfo* pLibInfo ) const;
155 	sal_Bool			ImpLoadLibary( BasicLibInfo* pLibInfo, SotStorage* pCurStorage, sal_Bool bInfosOnly = sal_False ) const;
156 	void			ImpCreateStdLib( StarBASIC* pParentFromStdLib );
157 	void			ImpMgrNotLoaded(  const String& rStorageName  );
158 	BasicLibInfo*	CreateLibInfo();
159     void            LoadBasicManager( SotStorage& rStorage, const String& rBaseURL, sal_Bool bLoadBasics = sal_True );
160 	void			LoadOldBasicManager( SotStorage& rStorage );
161 	sal_Bool 			ImplLoadBasic( SvStream& rStrm, StarBASICRef& rOldBasic ) const;
162 	sal_Bool			ImplEncryptStream( SvStream& rStream ) const;
163 	BasicLibInfo*	FindLibInfo( StarBASIC* pBasic ) const;
164 	void			CheckModules( StarBASIC* pBasic, sal_Bool bReference ) const;
165 	void			SetFlagToAllLibs( short nFlag, sal_Bool bSet ) const;
166 					BasicManager();	// Nur zum anpassen von Pfaden bei 'Speichern unter'.
167                     ~BasicManager();
168 
169 public:
170 					TYPEINFO();
171                     BasicManager( SotStorage& rStorage, const String& rBaseURL, StarBASIC* pParentFromStdLib = NULL, String* pLibPath = NULL, sal_Bool bDocMgr = sal_False );
172 					BasicManager( StarBASIC* pStdLib, String* pLibPath = NULL, sal_Bool bDocMgr = sal_False );
173 
174     /** deletes the given BasicManager instance
175 
176         This method is necessary since normally, BasicManager instances are owned by the BasicManagerRepository,
177         and expected to be deleted by the repository only. However, there exists quite some legacy code,
178         which needs to explicitly delete a BasicManager itself. This code must not use the (protected)
179         destructor, but LegacyDeleteBasicManager.
180     */
181     static void     LegacyDeleteBasicManager( BasicManager*& _rpManager );
182 
SetStorageName(const String & rName)183 	void			SetStorageName( const String& rName )	{ maStorageName = rName; }
GetStorageName() const184 	String			GetStorageName() const 					{ return maStorageName; }
SetName(const String & rName)185 	void			SetName( const String& rName ) 			{ aName = rName; }
GetName() const186 	String			GetName() const							{ return aName; }
187 
188 
189 	sal_uInt16		    GetLibCount() const;
190 	StarBASIC*	    GetLib( sal_uInt16 nLib ) const;
191 	StarBASIC*	    GetLib( const String& rName ) const;
192 	sal_uInt16		    GetLibId( const String& rName ) const;
193 
194 	String		    GetLibName( sal_uInt16 nLib );
195 
196     /** announces the library containers which belong to this BasicManager
197 
198         The method will automatically add two global constants, BasicLibraries and DialogLibraries,
199         to the BasicManager.
200     */
201 	void			SetLibraryContainerInfo( const LibraryContainerInfo& rInfo );
202 
203     const ::com::sun::star::uno::Reference< com::sun::star::script::XPersistentLibraryContainer >&
204                     GetDialogLibraryContainer()  const;
205     const ::com::sun::star::uno::Reference< com::sun::star::script::XPersistentLibraryContainer >&
206                     GetScriptLibraryContainer()  const;
207 
208 	sal_Bool		    LoadLib( sal_uInt16 nLib );
209 	sal_Bool		    RemoveLib( sal_uInt16 nLib, sal_Bool bDelBasicFromStorage );
210 
211 	// Modify-Flag wird nur beim Speichern zurueckgesetzt.
212 	sal_Bool		    IsModified() const;
213 	sal_Bool		    IsBasicModified() const;
214 
215 	sal_Bool		    HasErrors();
216 	void		    ClearErrors();
217 	BasicError*     GetFirstError();
218 	BasicError*     GetNextError();
219 
220     /** sets a global constant in the basic library, referring to some UNO object, to a new value.
221 
222         If a constant with this name already existed before, its value is changed, and the old constant is
223         returned. If it does not yet exist, it is newly created, and inserted into the basic library.
224     */
225     ::com::sun::star::uno::Any
226                     SetGlobalUNOConstant( const sal_Char* _pAsciiName, const ::com::sun::star::uno::Any& _rValue );
227 
228     /** retrieves a global constant in the basic library, referring to some UNO object, returns true if a value is found ( value is in aOut ) false otherwise. */
229                     bool GetGlobalUNOConstant( const sal_Char* _pAsciiName, ::com::sun::star::uno::Any& aOut );
230     /** determines whether there are password-protected modules whose size exceedes the
231         legacy module size
232         @param _out_rModuleNames
233             takes the names of modules whose size exceeds the legacy limit
234     */
235     bool            LegacyPsswdBinaryLimitExceeded( ::com::sun::star::uno::Sequence< rtl::OUString >& _out_rModuleNames );
236 
237     /// determines whether the Basic Manager has a given macro, given by fully qualified name
238     bool            HasMacro( String const& i_fullyQualifiedName ) const;
239     /// executes a given macro
240     ErrCode         ExecuteMacro( String const& i_fullyQualifiedName, SbxArray* i_arguments, SbxValue* i_retValue );
241     /// executes a given macro
242     ErrCode         ExecuteMacro( String const& i_fullyQualifiedName, String const& i_commaSeparatedArgs, SbxValue* i_retValue );
243 
244 private:
245 	sal_Bool		    IsReference( sal_uInt16 nLib );
246 
247 	sal_Bool		    SetLibName( sal_uInt16 nLib, const String& rName );
248 
249 	StarBASIC*  	GetStdLib() const;
250 	StarBASIC*      AddLib( SotStorage& rStorage, const String& rLibName, sal_Bool bReference );
251 	sal_Bool		    RemoveLib( sal_uInt16 nLib );
252     sal_Bool	    	HasLib( const String& rName ) const;
253 
254     StarBASIC*	    CreateLibForLibContainer( const String& rLibName,
255                         const com::sun::star::uno::Reference< com::sun::star::script::XLibraryContainer >&
256                             xScriptCont );
257 	// For XML import/export:
258 	StarBASIC*      CreateLib( const String& rLibName );
259 	StarBASIC*      CreateLib( const String& rLibName, const String& Password,
260 							   const String& LinkTargetURL );
261 };
262 
263 BASIC_DLLPUBLIC void SetAppBasicManager( BasicManager* pBasMgr );
264 
265 #endif	//_BASMGR_HXX
266