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