Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_BASCTL_SOURCE_INC_SCRIPTDOCUMENT_HXX
21 : #define INCLUDED_BASCTL_SOURCE_INC_SCRIPTDOCUMENT_HXX
22 :
23 : #include <com/sun/star/script/XLibraryContainer.hpp>
24 : #include <com/sun/star/frame/XModel.hpp>
25 : #include <com/sun/star/task/XStatusIndicator.hpp>
26 : #include <com/sun/star/io/XInputStreamProvider.hpp>
27 :
28 : #include <boost/shared_ptr.hpp>
29 : #include <vector>
30 :
31 : class SfxListener;
32 :
33 : class BasicManager;
34 :
35 :
36 : namespace basctl
37 : {
38 :
39 : enum LibraryContainerType
40 : {
41 : E_SCRIPTS,
42 : E_DIALOGS
43 : };
44 :
45 : enum LibraryLocation
46 : {
47 : LIBRARY_LOCATION_UNKNOWN,
48 : LIBRARY_LOCATION_USER,
49 : LIBRARY_LOCATION_SHARE,
50 : LIBRARY_LOCATION_DOCUMENT
51 : };
52 :
53 : enum LibraryType
54 : {
55 : LIBRARY_TYPE_UNKNOWN,
56 : LIBRARY_TYPE_MODULE,
57 : LIBRARY_TYPE_DIALOG,
58 : LIBRARY_TYPE_ALL
59 : };
60 :
61 : class ScriptDocument;
62 : typedef ::std::vector< ScriptDocument > ScriptDocuments;
63 :
64 : /** encapsulates a document which contains Basic scripts and dialogs
65 : */
66 0 : class ScriptDocument
67 : {
68 : private:
69 : class Impl;
70 : boost::shared_ptr<Impl> m_pImpl;
71 :
72 : private:
73 : /** creates a ScriptDocument instance which operates on the application-wide
74 : scripts and dialogs
75 : */
76 : ScriptDocument();
77 :
78 : public:
79 : enum SpecialDocument { NoDocument };
80 : /** creates a ScriptDocument instance which does refers to neither the application-wide,
81 : nor a specific real document's scripts.
82 :
83 : This constructor might come handy when you need some kind of uninitialized
84 : ScriptDocument, which you do not want to operate on (yet), but initialize later
85 : by assignment.
86 :
87 : <member>isValid</member> will return <FALSE/> for a ScriptDocument constructed
88 : this way.
89 : */
90 : explicit ScriptDocument( SpecialDocument _eType );
91 :
92 : /** creates a ScriptDocument instance which refers to a document given as
93 : XModel
94 :
95 : @param _rxDocument
96 : the document. Must not be <NULL/>.
97 : */
98 : explicit ScriptDocument( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& _rxDocument );
99 :
100 : /// copy constructor
101 : ScriptDocument( const ScriptDocument& _rSource );
102 :
103 : /// destructor
104 : ~ScriptDocument();
105 :
106 : /** returns a reference to a shared ScriptDocument instance which
107 : operates on the application-wide scripts and dialogs
108 : */
109 : static const ScriptDocument&
110 : getApplicationScriptDocument();
111 :
112 : /** returns a (newly created) ScriptDocument instance for the document to
113 : which a given BasicManager belongs
114 :
115 : If the basic manager is the application's basic manager, then the (shared)
116 : ScriptDocument instance which is responsible for the application is returned.
117 :
118 : @see getApplicationScriptDocument
119 : */
120 : static ScriptDocument
121 : getDocumentForBasicManager( const BasicManager* _pManager );
122 :
123 : /** returns a (newly created) ScriptDocument instance for the document
124 : with a given caption or URL
125 :
126 : If there is no document with the given caption, then the (shared)
127 : ScriptDocument instance which is responsible for the application is returned.
128 :
129 : @see getApplicationScriptDocument
130 : */
131 : static ScriptDocument
132 : getDocumentWithURLOrCaption( const OUString& _rUrlOrCaption );
133 :
134 : /** operation mode for getAllScriptDocuments
135 : */
136 : enum ScriptDocumentList
137 : {
138 : /** all ScriptDocuments, including the dedicated one which represents
139 : the application-wide scripts/dialogs.
140 : */
141 : AllWithApplication,
142 : /** real documents only
143 : */
144 : DocumentsOnly,
145 : /** real documents only, sorted lexicographically by their title (using the sys locale's default
146 : collator)
147 : */
148 : DocumentsSorted
149 : };
150 :
151 : /** returns the set of ScriptDocument instances, one for each open document which
152 : contains Basic/Dialog containers; plus an additional instance for
153 : the application, if desired
154 :
155 : Documents which are not visible - i.e. do not have a visible frame.
156 :
157 : @param _bIncludingApplication
158 : <TRUE/> if the application-wide scripts/dialogs should also be represented
159 : by a ScriptDocument
160 : */
161 : static ScriptDocuments
162 : getAllScriptDocuments( ScriptDocumentList _eListType );
163 :
164 : // comparison
165 : bool operator==( const ScriptDocument& _rhs ) const;
166 0 : inline bool operator!=( const ScriptDocument& _rhs ) const { return !( *this == _rhs ); }
167 :
168 : /// retrieves a (pretty simple) hash code for the document
169 : sal_Int32 hashCode() const;
170 :
171 : /** determines whether the document is actually able to contain Basic/Dialog libraries
172 :
173 : Note that validity does not automatically imply the document can be used for active
174 : work. Instead, it is possible the document is closed already (or being closed currently).
175 : In this case, isValid will return <TRUE/>, but isAlive will return <FALSE/>.
176 :
177 : @return
178 : <TRUE/> if the instance refers to a document which contains Basic/Dialog libraries,
179 : or the application as a whole, <FALSE/> otherwise.
180 :
181 : @see isAlive
182 : */
183 : bool isValid() const;
184 :
185 : /** determines whether the document instance is alive
186 :
187 : If the instance is not valid, <FALSE/> is returned.
188 :
189 : If the instance refers to a real document, which is already closed, or just being closed,
190 : the method returns <FALSE/>.
191 :
192 : If the instance refers to the application, <TRUE/> is returned.
193 :
194 : @see isValid
195 : */
196 : bool isAlive() const;
197 :
198 : bool isInVBAMode() const;
199 : /// returns the BasicManager associated with this instance
200 : BasicManager*
201 : getBasicManager() const;
202 :
203 : /** returns the UNO component representing the document which the instance operates on
204 :
205 : Must not be used when the instance operates on the application-wide
206 : Basic/Dialog libraries.
207 : */
208 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >
209 : getDocument() const;
210 :
211 : /** returns the UNO component representing the document which the instance operates on
212 :
213 : May be used when the instance operates on the application-wide
214 : Basic/Dialog libraries, in this case it returns <NULL/>.
215 : */
216 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >
217 : getDocumentOrNull() const;
218 :
219 : /** returns the Basic or Dialog library container of the document
220 :
221 : If the document is not valid, <NULL/> is returned.
222 : */
223 : ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer >
224 : getLibraryContainer( LibraryContainerType _eType ) const;
225 :
226 : /** determines whether there exists a library of the given type, with the given name
227 : */
228 : bool hasLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const;
229 :
230 : /** returns a script or dialog library given by name
231 :
232 : @param _eType
233 : the type of library to load
234 : @param _rLibName
235 : the name of the script library
236 : @param _bLoadLibrary
237 : <TRUE/> if and only if the library should be loaded.
238 :
239 : @throws NoSuchElementException
240 : if there is no script library with the given name
241 : */
242 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
243 : getLibrary( LibraryContainerType _eType, const OUString& _rLibName, bool _bLoadLibrary ) const;
244 :
245 : /** creates a script or dialog library in the document, or returns an existing one
246 :
247 : If <code>_rLibName</code> denotes an existing library which does not need to be created,
248 : then this library will automatically be loaded, and then returned.
249 : */
250 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
251 : getOrCreateLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const;
252 :
253 : /** returns the names of the modules in a given script or dialog library of the document
254 : */
255 : ::com::sun::star::uno::Sequence< OUString >
256 : getObjectNames( LibraryContainerType _eType, const OUString& _rLibName ) const;
257 :
258 : /** retrieves a name for a newly to be created module or dialog
259 : */
260 : OUString createObjectName( LibraryContainerType _eType, const OUString& _rLibName ) const;
261 :
262 : /** loads a script or dialog library given by name, if there is such a library
263 : */
264 : void loadLibraryIfExists( LibraryContainerType _eType, const OUString& _rLibrary );
265 :
266 : /// retrieves the (combined) names of all script and dialog libraries
267 : ::com::sun::star::uno::Sequence< OUString >
268 : getLibraryNames() const;
269 :
270 : /** removes a given script module from the document
271 :
272 : @return
273 : <TRUE/> if and only if the removal was successful. When <FALSE/> is returned,
274 : this will reported as assertion in a non-product build.
275 : */
276 : bool removeModule( const OUString& _rLibName, const OUString& _rModuleName ) const;
277 :
278 : /** creates a module with the given name in the given library
279 : @param _rLibName
280 : the library name
281 : @param _rModName
282 : the name of the to-be-created module
283 : @param _bCreateMain
284 : determines whether or not a function Main should be created
285 : @param _out_rNewModuleCode
286 : the source code of the newly created module
287 : @return
288 : <TRUE/> if and only if the creation was successful
289 : */
290 : bool createModule( const OUString& _rLibName, const OUString& _rModName, bool _bCreateMain, OUString& _out_rNewModuleCode ) const;
291 :
292 : /** inserts a given piece as code as module
293 : @param _rLibName
294 : the name of the library to insert the module into. If a library with this name does
295 : not yet exist, it will be created.
296 : @param _rModName
297 : the name of the module to insert the code as. Must denote a name which is not yet
298 : used in the module library.
299 : @param _rModuleCode
300 : the code of the new module
301 : @return
302 : <TRUE/> if and only if the insertion was successful.
303 : */
304 : bool insertModule( const OUString& _rLibName, const OUString& _rModName, const OUString& _rModuleCode ) const;
305 :
306 : /** updates a given module with new code
307 : @param _rLibName
308 : the name of the library the modules lives in. Must denote an existing module library.
309 : @param _rModName
310 : the name of the module to update. Must denote an existing module in the given library.
311 : @param _rModuleCode
312 : the new module code.
313 : @return
314 : <TRUE/> if and only if the insertion was successful.
315 : */
316 : bool updateModule( const OUString& _rLibName, const OUString& _rModName, const OUString& _rModuleCode ) const;
317 :
318 : /// determines whether a module with the given name exists in the given library
319 : bool hasModule( const OUString& _rLibName, const OUString& _rModName ) const;
320 :
321 : /** retrieves a module's source
322 : @param _rLibName
323 : the library name where the module is located
324 : @param _rModName
325 : the module name
326 : @param _out_rModuleSource
327 : takes the module's source upon successful return
328 : @return
329 : <TRUE/> if and only if the code could be successfully retrieved, <FALSE/> otherwise
330 : */
331 : bool getModule( const OUString& _rLibName, const OUString& _rModName, OUString& _rModuleSource ) const;
332 :
333 : /** renames a module
334 : @param _rLibName
335 : the library where the module lives in. Must denote an existing library.
336 : @param _rOldName
337 : the old module name. Must denote an existing module.
338 : @param _rNewName
339 : the new module name
340 : @return
341 : <TRUE/> if and only if renaming was successful.
342 : */
343 : bool renameModule( const OUString& _rLibName, const OUString& _rOldName, const OUString& _rNewName ) const;
344 :
345 : /** removes a given dialog from the document
346 :
347 : @return
348 : <TRUE/> if and only if the removal was successful. When <FALSE/> is returned,
349 : this will reported as assertion in a non-product build.
350 : */
351 : bool removeDialog( const OUString& _rLibName, const OUString& _rDialogName ) const;
352 :
353 : /// determines whether a dialog with the given name exists in the given library
354 : bool hasDialog( const OUString& _rLibName, const OUString& _rDialogName ) const;
355 :
356 : /** retrieves a dialog
357 : @param _rLibName
358 : the library name where the module is located
359 : @param _rDialogName
360 : the dialog's name
361 : @param _out_rDialogSource
362 : takes the provider for the dialog's desription, upon successful return
363 : @return
364 : <TRUE/> if and only if the dialog could be successfully retrieved, <FALSE/> otherwise
365 : */
366 : bool getDialog(
367 : const OUString& _rLibName,
368 : const OUString& _rDialogName,
369 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStreamProvider >& _out_rDialogProvider
370 : ) const;
371 :
372 : /** renames a dialog
373 : @param _rLibName
374 : the library where the dialog lives in. Must denote an existing library.
375 : @param _rOldName
376 : the old dialog name. Must denote an existing dialog.
377 : @param _rNewName
378 : the new dialog name
379 : @param _rxExistingDialogModel
380 : the existing model of the dialog, if already loaded in the IDE
381 : @return
382 : <TRUE/> if and only if renaming was successful.
383 : */
384 : bool renameDialog(
385 : const OUString& _rLibName,
386 : const OUString& _rOldName,
387 : const OUString& _rNewName,
388 : const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& _rxExistingDialogModel
389 : ) const;
390 :
391 : /** create a dialog
392 : @param _rLibName
393 : the library name where the module is located
394 : @param _rDialogName
395 : the dialog's name
396 : @param _out_rDialogSource
397 : takes the provider for the dialog's desription, upon successful return
398 : @return
399 : <TRUE/> if and only if the dialog could be successfully retrieved, <FALSE/> otherwise
400 : */
401 : bool createDialog(
402 : const OUString& _rLibName,
403 : const OUString& _rDialogName,
404 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStreamProvider >& _out_rDialogProvider
405 : ) const;
406 :
407 : /** inserts a given dialog into a given library
408 :
409 : @param _rLibName
410 : the name of the library to insert the dialog into. If a library with this name does
411 : not yet exist, it will be created.
412 : @param _rModName
413 : the name of the dialog to insert. Must denote a name which is not yet
414 : used in the dialog library.
415 : @param _rDialogProvider
416 : the provider of the dialog's description
417 : @return
418 : <TRUE/> if and only if the insertion was successful.
419 : */
420 : bool insertDialog(
421 : const OUString& _rLibName,
422 : const OUString& _rDialogName,
423 : const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStreamProvider >& _rDialogProvider
424 : ) const;
425 :
426 : /** determines whether the document is read-only
427 :
428 : cannot be called if the document operates on the application-wide scripts
429 : */
430 : bool isReadOnly() const;
431 :
432 : /** determines whether the ScriptDocument instance operates on the whole application,
433 : as opposed to a real document
434 : */
435 : bool isApplication() const;
436 :
437 : /** determines whether the ScriptDocument instance operates on a real document,
438 : as opposed to the whole application
439 : */
440 0 : bool isDocument() const { return isValid() && !isApplication(); }
441 :
442 : /** marks the document as modified
443 : @precond
444 : the instance operates on a real document, not on the application
445 : @see isDocument
446 : */
447 : void setDocumentModified() const;
448 :
449 : /** determines whether the document is modified
450 : @precond
451 : the instance operates on a real document, not on the application
452 : @see isDocument
453 : */
454 : bool isDocumentModified() const;
455 :
456 : /** saves the document, if the instance refers to a real document
457 : @precond
458 : <code>isApplication</code> returns <FALSE/>
459 : */
460 : bool saveDocument(
461 : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XStatusIndicator >& _rxStatusIndicator
462 : ) const;
463 :
464 : /// returns the location of a library given by name
465 : LibraryLocation
466 : getLibraryLocation( const OUString& _rLibName ) const;
467 :
468 : /// returns the title for the document
469 : OUString getTitle( LibraryLocation _eLocation, LibraryType _eType = LIBRARY_TYPE_ALL ) const;
470 :
471 : /** returns the title of the document
472 :
473 : to be used for valid documents only
474 : */
475 : OUString getTitle() const;
476 :
477 : /** returns the URL of the document
478 :
479 : to be used for valid documents only
480 : */
481 : OUString getURL() const;
482 :
483 : /** determines whether the document is currently the one-and-only application-wide active document
484 : */
485 : bool isActive() const;
486 :
487 : /** determines whether macro execution for this document is allowed
488 :
489 : only to be called for real documents (->isDocument)
490 : */
491 : bool allowMacros() const;
492 : };
493 :
494 :
495 : } // namespace basctl
496 :
497 :
498 : #endif // INCLUDED_BASCTL_SOURCE_INC_SCRIPTDOCUMENT_HXX
499 :
500 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|