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 | #include "scriptdocument.hxx" |
21 | #include "basobj.hxx" |
22 | #include "basidesh.hrc" |
23 | #include "iderid.hxx" |
24 | #include "dlgeddef.hxx" |
25 | #include "doceventnotifier.hxx" |
26 | #include "documentenumeration.hxx" |
27 | |
28 | #include <com/sun/star/uri/UriReferenceFactory.hpp> |
29 | #include <com/sun/star/util/theMacroExpander.hpp> |
30 | #include <com/sun/star/document/MacroExecMode.hpp> |
31 | #include <com/sun/star/frame/XStorable.hpp> |
32 | #include <com/sun/star/frame/FrameSearchFlag.hpp> |
33 | #include <com/sun/star/frame/XDesktop.hpp> |
34 | #include <com/sun/star/frame/XModel2.hpp> |
35 | #include <com/sun/star/awt/XWindow2.hpp> |
36 | #include <com/sun/star/document/XEmbeddedScripts.hpp> |
37 | #include <com/sun/star/script/vba/XVBACompatibility.hpp> |
38 | #include <com/sun/star/script/vba/XVBAModuleInfo.hpp> |
39 | |
40 | #include <sfx2/objsh.hxx> |
41 | #include <sfx2/app.hxx> |
42 | #include <sfx2/viewfrm.hxx> |
43 | #include <sfx2/bindings.hxx> |
44 | #include <sfx2/docfile.hxx> |
45 | |
46 | #include <vcl/svapp.hxx> |
47 | |
48 | #include <basic/basicmanagerrepository.hxx> |
49 | |
50 | #include <xmlscript/xmldlg_imexp.hxx> |
51 | |
52 | #include <unotools/syslocale.hxx> |
53 | |
54 | #include <unotools/collatorwrapper.hxx> |
55 | |
56 | #include <tools/diagnose_ex.h> |
57 | |
58 | #include <comphelper/processfactory.hxx> |
59 | #include <comphelper/documentinfo.hxx> |
60 | |
61 | #include <osl/mutex.hxx> |
62 | |
63 | #include <cppuhelper/implbase1.hxx> |
64 | |
65 | #include <rtl/uri.hxx> |
66 | #include <rtl/bootstrap.hxx> |
67 | |
68 | #include <osl/process.h> |
69 | #include <osl/file.hxx> |
70 | |
71 | #include <set> |
72 | |
73 | namespace basctl |
74 | { |
75 | using ::com::sun::star::uno::Sequence; |
76 | using ::com::sun::star::uno::Reference; |
77 | using ::com::sun::star::frame::XModel; |
78 | using ::com::sun::star::beans::XPropertySet; |
79 | using ::com::sun::star::script::XLibraryContainer; |
80 | using ::com::sun::star::uno::UNO_QUERY_THROW; |
81 | using ::com::sun::star::uno::UNO_SET_THROW; |
82 | using ::com::sun::star::beans::XPropertySetInfo; |
83 | using ::com::sun::star::uno::Exception; |
84 | using ::com::sun::star::container::XNameContainer; |
85 | using ::com::sun::star::container::NoSuchElementException; |
86 | using ::com::sun::star::uno::UNO_QUERY; |
87 | using ::com::sun::star::task::XStatusIndicator; |
88 | using ::com::sun::star::uno::makeAny; |
89 | using ::com::sun::star::script::XLibraryContainer2; |
90 | using ::com::sun::star::lang::XMultiServiceFactory; |
91 | using ::com::sun::star::uri::UriReferenceFactory; |
92 | using ::com::sun::star::uri::XUriReferenceFactory; |
93 | using ::com::sun::star::uri::XUriReference; |
94 | using ::com::sun::star::uno::XComponentContext; |
95 | using ::com::sun::star::util::XMacroExpander; |
96 | using ::com::sun::star::util::theMacroExpander; |
97 | using ::com::sun::star::io::XInputStreamProvider; |
98 | using ::com::sun::star::uno::Any; |
99 | using ::com::sun::star::io::XInputStream; |
100 | using ::com::sun::star::frame::XStorable; |
101 | using ::com::sun::star::util::XModifiable; |
102 | using ::com::sun::star::frame::XController; |
103 | using ::com::sun::star::frame::XFrame; |
104 | using ::com::sun::star::util::URL; |
105 | using ::com::sun::star::frame::XDispatchProvider; |
106 | using ::com::sun::star::frame::XDispatch; |
107 | using ::com::sun::star::beans::PropertyValue; |
108 | using ::com::sun::star::frame::XDesktop; |
109 | using ::com::sun::star::container::XEnumerationAccess; |
110 | using ::com::sun::star::container::XEnumeration; |
111 | using ::com::sun::star::frame::XModel2; |
112 | using ::com::sun::star::awt::XWindow2; |
113 | using ::com::sun::star::document::XEventListener; |
114 | using ::com::sun::star::lang::EventObject; |
115 | using ::com::sun::star::uno::RuntimeException; |
116 | using ::com::sun::star::document::XEventBroadcaster; |
117 | using ::com::sun::star::document::XEmbeddedScripts; |
118 | using ::com::sun::star::script::ModuleInfo; |
119 | using ::com::sun::star::script::vba::XVBACompatibility; |
120 | using ::com::sun::star::script::vba::XVBAModuleInfo; |
121 | |
122 | namespace MacroExecMode = ::com::sun::star::document::MacroExecMode; |
123 | namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag; |
124 | |
125 | //==================================================================== |
126 | //= helper |
127 | //==================================================================== |
128 | namespace |
129 | { |
130 | static bool StringCompareLessThan( const OUString& lhs, const OUString& rhs ) |
131 | { |
132 | return lhs.compareToIgnoreAsciiCase( rhs ) < 0; |
133 | } |
134 | |
135 | class FilterDocuments : public docs::IDocumentDescriptorFilter |
136 | { |
137 | public: |
138 | FilterDocuments( bool _bFilterInvisible ) : m_bFilterInvisible( _bFilterInvisible ) { } |
139 | |
140 | virtual ~FilterDocuments() {} |
141 | |
142 | virtual bool includeDocument( const docs::DocumentDescriptor& _rDocument ) const; |
143 | |
144 | private: |
145 | bool impl_isDocumentVisible_nothrow( const docs::DocumentDescriptor& _rDocument ) const; |
146 | |
147 | private: |
148 | bool m_bFilterInvisible; |
149 | }; |
150 | |
151 | bool FilterDocuments::impl_isDocumentVisible_nothrow( const docs::DocumentDescriptor& _rDocument ) const |
152 | { |
153 | try |
154 | { |
155 | for ( docs::Controllers::const_iterator controller = _rDocument.aControllers.begin(); |
156 | controller != _rDocument.aControllers.end(); |
157 | ++controller |
158 | ) |
159 | { |
160 | Reference< XFrame > xFrame( (*controller)->getFrame(), UNO_SET_THROW ); |
161 | Reference< XWindow2 > xContainer( xFrame->getContainerWindow(), UNO_QUERY_THROW ); |
162 | if ( xContainer->isVisible() ) |
163 | return true; |
164 | } |
165 | } |
166 | catch( const Exception& ) |
167 | { |
168 | DBG_UNHANDLED_EXCEPTION(); |
169 | } |
170 | return false; |
171 | } |
172 | |
173 | bool FilterDocuments::includeDocument( const docs::DocumentDescriptor& _rDocument ) const |
174 | { |
175 | Reference< XEmbeddedScripts > xScripts( _rDocument.xModel, UNO_QUERY ); |
176 | if ( !xScripts.is() ) |
177 | return false; |
178 | if ( !m_bFilterInvisible || impl_isDocumentVisible_nothrow( _rDocument ) ) |
179 | return true; |
180 | return false; |
181 | } |
182 | |
183 | void lcl_getAllModels_throw( docs::Documents& _out_rModels, bool _bVisibleOnly ) |
184 | { |
185 | _out_rModels.clear(); |
186 | |
187 | FilterDocuments aFilter( _bVisibleOnly ); |
188 | docs::DocumentEnumeration aEnum( |
189 | comphelper::getProcessComponentContext(), &aFilter ); |
190 | |
191 | aEnum.getDocuments( _out_rModels ); |
192 | } |
193 | } |
194 | |
195 | class ScriptDocument::Impl : public DocumentEventListener |
196 | { |
197 | private: |
198 | bool m_bIsApplication; |
199 | bool m_bValid; |
200 | bool m_bDocumentClosed; |
201 | Reference< XModel > m_xDocument; |
202 | Reference< XModifiable > m_xDocModify; |
203 | Reference< XEmbeddedScripts > m_xScriptAccess; |
204 | ::std::auto_ptr< DocumentEventNotifier > |
205 | m_pDocListener; |
206 | |
207 | public: |
208 | Impl (); |
209 | Impl (Reference<XModel> const& rxDocument); |
210 | ~Impl (); |
211 | |
212 | /** determines whether the instance refers to a valid "document" with script and |
213 | dialog libraries |
214 | */ |
215 | inline bool isValid() const { return m_bValid; } |
216 | /** determines whether the instance refers to a non-closed document |
217 | */ |
218 | inline bool isAlive() const { return m_bValid ? ( m_bIsApplication ? true : !m_bDocumentClosed ) : false; } |
219 | /// determines whether the "document" refers to the application in real |
220 | inline bool isApplication() const { return m_bValid && m_bIsApplication; } |
221 | /// determines whether the document refers to a real document (instead of the application) |
222 | inline bool isDocument() const { return m_bValid && !m_bIsApplication; } |
223 | |
224 | /** invalidates the instance |
225 | */ |
226 | void invalidate(); |
227 | |
228 | const Reference< XModel >& |
229 | getDocumentRef() const { return m_xDocument; } |
230 | |
231 | /// returns a library container belonging to the document |
232 | Reference< XLibraryContainer > |
233 | getLibraryContainer( LibraryContainerType _eType ) const; |
234 | |
235 | /// determines whether a given library is part of the shared installation |
236 | bool isLibraryShared( const OUString& _rLibName, LibraryContainerType _eType ); |
237 | |
238 | /** returns the current frame of the document |
239 | |
240 | To be called for documents only, not for the application. |
241 | |
242 | If <FALSE/> is returned, an assertion will be raised in non-product builds. |
243 | */ |
244 | bool getCurrentFrame( Reference< XFrame >& _out_rxFrame ) const; |
245 | |
246 | // versions with the same signature/semantics as in ScriptDocument itself |
247 | bool isReadOnly() const; |
248 | bool isInVBAMode() const; |
249 | BasicManager* |
250 | getBasicManager() const; |
251 | Reference< XModel > |
252 | getDocument() const; |
253 | void setDocumentModified() const; |
254 | bool isDocumentModified() const; |
255 | bool saveDocument( const Reference< XStatusIndicator >& _rxStatusIndicator ) const; |
256 | |
257 | OUString getTitle() const; |
258 | OUString getURL() const; |
259 | |
260 | bool allowMacros() const; |
261 | |
262 | Reference< XNameContainer > |
263 | getLibrary( LibraryContainerType _eType, const OUString& _rLibName, bool _bLoadLibrary ) const |
264 | SAL_THROW((NoSuchElementException)); |
265 | bool hasLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const; |
266 | Reference< XNameContainer > |
267 | getOrCreateLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const; |
268 | |
269 | void loadLibraryIfExists( LibraryContainerType _eType, const OUString& _rLibrary ); |
270 | |
271 | bool removeModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rModuleName ); |
272 | bool hasModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rModName ) const; |
273 | bool getModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rObjectName, Any& _out_rModuleOrDialog ); |
274 | bool renameModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rOldName, const OUString& _rNewName, const Reference< XNameContainer >& _rxExistingDialogModel ); |
275 | bool createModule( const OUString& _rLibName, const OUString& _rModName, bool _bCreateMain, OUString& _out_rNewModuleCode ) const; |
276 | bool insertModuleOrDialog( LibraryContainerType _eType, const OUString& _rObjectName, const OUString& _rModName, const Any& _rElement ) const; |
277 | bool updateModule( const OUString& _rLibName, const OUString& _rModName, const OUString& _rModuleCode ) const; |
278 | bool createDialog( const OUString& _rLibName, const OUString& _rDialogName, Reference< XInputStreamProvider >& _out_rDialogProvider ) const; |
279 | |
280 | protected: |
281 | // DocumentEventListener |
282 | virtual void onDocumentCreated( const ScriptDocument& _rDocument ); |
283 | virtual void onDocumentOpened( const ScriptDocument& _rDocument ); |
284 | virtual void onDocumentSave( const ScriptDocument& _rDocument ); |
285 | virtual void onDocumentSaveDone( const ScriptDocument& _rDocument ); |
286 | virtual void onDocumentSaveAs( const ScriptDocument& _rDocument ); |
287 | virtual void onDocumentSaveAsDone( const ScriptDocument& _rDocument ); |
288 | virtual void onDocumentClosed( const ScriptDocument& _rDocument ); |
289 | virtual void onDocumentTitleChanged( const ScriptDocument& _rDocument ); |
290 | virtual void onDocumentModeChanged( const ScriptDocument& _rDocument ); |
291 | |
292 | private: |
293 | bool impl_initDocument_nothrow( const Reference< XModel >& _rxModel ); |
294 | }; |
295 | |
296 | //==================================================================== |
297 | //= ScriptDocument::Impl - implementation |
298 | //==================================================================== |
299 | ScriptDocument::Impl::Impl() |
300 | :m_bIsApplication( true ) |
301 | ,m_bValid( true ) |
302 | ,m_bDocumentClosed( false ) |
303 | { |
304 | } |
305 | |
306 | ScriptDocument::Impl::Impl( const Reference< XModel >& _rxDocument ) |
307 | :m_bIsApplication( false ) |
308 | ,m_bValid( false ) |
309 | ,m_bDocumentClosed( false ) |
310 | { |
311 | if ( _rxDocument.is() ) |
312 | { |
313 | if ( impl_initDocument_nothrow( _rxDocument ) ) |
314 | { |
315 | } |
316 | } |
317 | } |
318 | |
319 | ScriptDocument::Impl::~Impl() |
320 | { |
321 | invalidate(); |
322 | } |
323 | |
324 | void ScriptDocument::Impl::invalidate() |
325 | { |
326 | m_bIsApplication = false; |
327 | m_bValid = false; |
328 | m_bDocumentClosed = false; |
329 | |
330 | m_xDocument.clear(); |
331 | m_xDocModify.clear(); |
332 | m_xScriptAccess.clear(); |
333 | |
334 | if ( m_pDocListener.get() ) |
335 | m_pDocListener->dispose(); |
336 | } |
337 | |
338 | bool ScriptDocument::Impl::impl_initDocument_nothrow( const Reference< XModel >& _rxModel ) |
339 | { |
340 | try |
341 | { |
342 | m_xDocument.set ( _rxModel, UNO_SET_THROW ); |
343 | m_xDocModify.set ( _rxModel, UNO_QUERY_THROW ); |
344 | m_xScriptAccess.set ( _rxModel, UNO_QUERY ); |
345 | |
346 | m_bValid = m_xScriptAccess.is(); |
347 | |
348 | if ( m_bValid ) |
349 | m_pDocListener.reset( new DocumentEventNotifier( *this, _rxModel ) ); |
350 | } |
351 | catch( const Exception& ) |
352 | { |
353 | DBG_UNHANDLED_EXCEPTION(); |
354 | m_bValid = false; |
355 | } |
356 | |
357 | if ( !m_bValid ) |
358 | { |
359 | invalidate(); |
360 | } |
361 | |
362 | return m_bValid; |
363 | } |
364 | |
365 | Reference< XLibraryContainer > ScriptDocument::Impl::getLibraryContainer( LibraryContainerType _eType ) const |
366 | { |
367 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::getLibraryContainer: invalid!" ); |
368 | |
369 | Reference< XLibraryContainer > xContainer; |
370 | if ( !isValid() ) |
371 | return xContainer; |
372 | |
373 | try |
374 | { |
375 | if ( isApplication() ) |
376 | xContainer.set( _eType == E_SCRIPTS ? SFX_APP()->GetBasicContainer() : SFX_APP()->GetDialogContainer(), UNO_QUERY_THROW ); |
377 | else |
378 | { |
379 | xContainer.set( |
380 | _eType == E_SCRIPTS ? m_xScriptAccess->getBasicLibraries() : m_xScriptAccess->getDialogLibraries(), |
381 | UNO_QUERY_THROW ); |
382 | } |
383 | } |
384 | catch( const Exception& ) |
385 | { |
386 | DBG_UNHANDLED_EXCEPTION(); |
387 | } |
388 | return xContainer; |
389 | } |
390 | |
391 | bool ScriptDocument::Impl::isReadOnly() const |
392 | { |
393 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::isReadOnly: invalid state!" ); |
394 | OSL_ENSURE( !isApplication(), "ScriptDocument::Impl::isReadOnly: not allowed to be called for the application!" ); |
395 | |
396 | bool bIsReadOnly = true; |
397 | if ( isValid() && !isApplication() ) |
398 | { |
399 | try |
400 | { |
401 | // note that XStorable is required by the OfficeDocument service |
402 | Reference< XStorable > xDocStorable( m_xDocument, UNO_QUERY_THROW ); |
403 | bIsReadOnly = xDocStorable->isReadonly(); |
404 | } |
405 | catch( const Exception& ) |
406 | { |
407 | DBG_UNHANDLED_EXCEPTION(); |
408 | } |
409 | } |
410 | return bIsReadOnly; |
411 | } |
412 | |
413 | bool ScriptDocument::Impl::isInVBAMode() const |
414 | { |
415 | bool bResult = false; |
416 | if ( !isApplication() ) |
417 | { |
418 | Reference< XVBACompatibility > xVBACompat( getLibraryContainer( E_SCRIPTS ), UNO_QUERY ); |
419 | if ( xVBACompat.is() ) |
420 | bResult = xVBACompat->getVBACompatibilityMode(); |
421 | } |
422 | return bResult; |
423 | } |
424 | |
425 | |
426 | BasicManager* ScriptDocument::Impl::getBasicManager() const |
427 | { |
428 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::getBasicManager: invalid state!" ); |
429 | if ( !isValid() ) |
430 | return NULL; |
431 | |
432 | if ( isApplication() ) |
433 | return SFX_APP()->GetBasicManager(); |
434 | |
435 | return ::basic::BasicManagerRepository::getDocumentBasicManager( m_xDocument ); |
436 | } |
437 | |
438 | |
439 | Reference< XModel > ScriptDocument::Impl::getDocument() const |
440 | { |
441 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::getDocument: invalid state!" ); |
442 | OSL_ENSURE( isDocument(), "ScriptDocument::Impl::getDocument: for documents only!" ); |
443 | if ( !isValid() || !isDocument() ) |
444 | return NULL; |
445 | |
446 | return m_xDocument; |
447 | } |
448 | |
449 | |
450 | Reference< XNameContainer > ScriptDocument::Impl::getLibrary( LibraryContainerType _eType, const OUString& _rLibName, bool _bLoadLibrary ) const |
451 | SAL_THROW((NoSuchElementException)) |
452 | { |
453 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::getLibrary: invalid state!" ); |
454 | |
455 | Reference< XNameContainer > xContainer; |
456 | try |
457 | { |
458 | Reference< XLibraryContainer > xLibContainer = getLibraryContainer( _eType ); |
459 | if ( isValid() ) |
460 | { |
461 | if ( xLibContainer.is() ) |
462 | xContainer.set( xLibContainer->getByName( _rLibName ), UNO_QUERY_THROW ); |
463 | } |
464 | |
465 | if ( !xContainer.is() ) |
466 | throw NoSuchElementException(); |
467 | |
468 | // load library |
469 | if ( _bLoadLibrary && !xLibContainer->isLibraryLoaded( _rLibName ) ) |
470 | xLibContainer->loadLibrary( _rLibName ); |
471 | } |
472 | catch( const NoSuchElementException& ) |
473 | { |
474 | throw; // allowed to leave |
475 | } |
476 | catch( const Exception& ) |
477 | { |
478 | DBG_UNHANDLED_EXCEPTION(); |
479 | } |
480 | |
481 | return xContainer; |
482 | } |
483 | |
484 | |
485 | bool ScriptDocument::Impl::hasLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const |
486 | { |
487 | bool bHas = false; |
488 | try |
489 | { |
490 | Reference< XLibraryContainer > xLibContainer = getLibraryContainer( _eType ); |
491 | bHas = xLibContainer.is() && xLibContainer->hasByName( _rLibName ); |
492 | } |
493 | catch( const Exception& ) |
494 | { |
495 | DBG_UNHANDLED_EXCEPTION(); |
496 | } |
497 | return bHas; |
498 | } |
499 | |
500 | |
501 | Reference< XNameContainer > ScriptDocument::Impl::getOrCreateLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const |
502 | { |
503 | Reference< XNameContainer > xLibrary; |
504 | try |
505 | { |
506 | Reference< XLibraryContainer > xLibContainer( getLibraryContainer( _eType ), UNO_QUERY_THROW ); |
507 | if ( xLibContainer->hasByName( _rLibName ) ) |
508 | xLibrary.set( xLibContainer->getByName( _rLibName ), UNO_QUERY_THROW ); |
509 | else |
510 | xLibrary.set( xLibContainer->createLibrary( _rLibName ), UNO_QUERY_THROW ); |
511 | |
512 | if ( !xLibContainer->isLibraryLoaded( _rLibName ) ) |
513 | xLibContainer->loadLibrary( _rLibName ); |
514 | } |
515 | catch( const Exception& ) |
516 | { |
517 | DBG_UNHANDLED_EXCEPTION(); |
518 | } |
519 | return xLibrary; |
520 | } |
521 | |
522 | |
523 | void ScriptDocument::Impl::loadLibraryIfExists( LibraryContainerType _eType, const OUString& _rLibrary ) |
524 | { |
525 | try |
526 | { |
527 | Reference< XLibraryContainer > xLibContainer( getLibraryContainer( _eType ) ); |
528 | if ( xLibContainer.is() && xLibContainer->hasByName( _rLibrary ) && !xLibContainer->isLibraryLoaded( _rLibrary ) ) |
529 | xLibContainer->loadLibrary( _rLibrary ); |
530 | } |
531 | catch( const Exception& ) |
532 | { |
533 | DBG_UNHANDLED_EXCEPTION(); |
534 | } |
535 | } |
536 | |
537 | |
538 | bool ScriptDocument::Impl::removeModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rModuleName ) |
539 | { |
540 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::removeModuleOrDialog: invalid!" ); |
541 | if ( isValid() ) |
542 | { |
543 | try |
544 | { |
545 | Reference< XNameContainer > xLib( getLibrary( _eType, _rLibName, true ) ); |
546 | if ( xLib.is() ) |
547 | { |
548 | xLib->removeByName( _rModuleName ); |
549 | return true; |
550 | } |
551 | } |
552 | catch( const Exception& ) |
553 | { |
554 | DBG_UNHANDLED_EXCEPTION(); |
555 | } |
556 | } |
557 | return false; |
558 | } |
559 | |
560 | |
561 | bool ScriptDocument::Impl::hasModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rModName ) const |
562 | { |
563 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::hasModuleOrDialog: invalid!" ); |
564 | if ( !isValid() ) |
565 | return false; |
566 | |
567 | try |
568 | { |
569 | Reference< XNameContainer > xLib( getLibrary( _eType, _rLibName, true ) ); |
570 | if ( xLib.is() ) |
571 | return xLib->hasByName( _rModName ); |
572 | } |
573 | catch( const Exception& ) |
574 | { |
575 | DBG_UNHANDLED_EXCEPTION(); |
576 | } |
577 | return false; |
578 | } |
579 | |
580 | |
581 | bool ScriptDocument::Impl::getModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rObjectName, Any& _out_rModuleOrDialog ) |
582 | { |
583 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::getModuleOrDialog: invalid!" ); |
584 | if ( !isValid() ) |
585 | return false; |
586 | |
587 | _out_rModuleOrDialog.clear(); |
588 | try |
589 | { |
590 | Reference< XNameContainer > xLib( getLibrary( _eType, _rLibName, true ), UNO_QUERY_THROW ); |
591 | if ( xLib->hasByName( _rObjectName ) ) |
592 | { |
593 | _out_rModuleOrDialog = xLib->getByName( _rObjectName ); |
594 | return true; |
595 | } |
596 | } |
597 | catch( const Exception& ) |
598 | { |
599 | DBG_UNHANDLED_EXCEPTION(); |
600 | } |
601 | return false; |
602 | } |
603 | |
604 | |
605 | bool ScriptDocument::Impl::renameModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, |
606 | const OUString& _rOldName, const OUString& _rNewName, const Reference< XNameContainer >& _rxExistingDialogModel ) |
607 | { |
608 | OSL_ENSURE( isValid(), "ScriptDocument::Impl::renameModuleOrDialog: invalid!" ); |
609 | if ( !isValid() ) |
610 | return false; |
611 | |
612 | try |
613 | { |
614 | Reference< XNameContainer > xLib( getLibrary( _eType, _rLibName, true ), UNO_QUERY_THROW ); |
615 | |
616 | // get element |
617 | Any aElement( xLib->getByName( _rOldName ) ); |
618 | |
619 | // remove element from container |
620 | xLib->removeByName( _rOldName ); |
621 | |
622 | // if it's a dialog, import and export, to reflect the new name |
623 | if ( _eType == E_DIALOGS ) |
624 | { |
625 | // create dialog model |
626 | Reference< XComponentContext > aContext( |
627 | comphelper::getProcessComponentContext() ); |
628 | Reference< XNameContainer > xDialogModel; |
629 | if ( _rxExistingDialogModel.is() ) |
630 | xDialogModel = _rxExistingDialogModel; |
631 | else |
632 | xDialogModel.set( |
633 | ( aContext->getServiceManager()-> |
634 | createInstanceWithContext( |
635 | "com.sun.star.awt.UnoControlDialogModel" , |
636 | aContext ) ), |
637 | UNO_QUERY_THROW ); |
638 | |
639 | // import dialog model |
640 | Reference< XInputStreamProvider > xISP( aElement, UNO_QUERY_THROW ); |
641 | if ( !_rxExistingDialogModel.is() ) |
642 | { |
643 | Reference< XInputStream > xInput( xISP->createInputStream(), UNO_QUERY_THROW ); |
644 | ::xmlscript::importDialogModel( xInput, xDialogModel, aContext, isDocument() ? getDocument() : Reference< XModel >() ); |
645 | } |
646 | |
647 | // set new name as property |
648 | Reference< XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY_THROW ); |
649 | xDlgPSet->setPropertyValue( DLGED_PROP_NAME, makeAny( _rNewName ) ); |
650 | |
651 | // export dialog model |
652 | xISP = ::xmlscript::exportDialogModel( xDialogModel, aContext, isDocument() ? getDocument() : Reference< XModel >() ); |
653 | aElement <<= xISP; |
654 | } |
655 | |
656 | // insert element by new name in container |
657 | if ( _eType == E_SCRIPTS ) |
658 | { |
659 | Reference< XVBAModuleInfo > xVBAModuleInfo( xLib, UNO_QUERY ); |
660 | if ( xVBAModuleInfo->hasModuleInfo( _rOldName ) ) |
661 | { |
662 | ModuleInfo sModuleInfo = xVBAModuleInfo->getModuleInfo( _rOldName ); |
663 | xVBAModuleInfo->removeModuleInfo( _rOldName ); |
664 | xVBAModuleInfo->insertModuleInfo( _rNewName, sModuleInfo ); |
665 | } |
666 | } |
667 | xLib->insertByName( _rNewName, aElement ); |
668 | return true; |
669 | } |
670 | catch( const Exception& ) |
671 | { |
672 | DBG_UNHANDLED_EXCEPTION(); |
673 | } |
674 | return false; |
675 | } |
676 | |
677 | |
678 | bool ScriptDocument::Impl::createModule( const OUString& _rLibName, const OUString& _rModName, bool _bCreateMain, OUString& _out_rNewModuleCode ) const |
679 | { |
680 | _out_rNewModuleCode = OUString(); |
681 | try |
682 | { |
683 | Reference< XNameContainer > xLib( getLibrary( E_SCRIPTS, _rLibName, true ) ); |
684 | if ( !xLib.is() || xLib->hasByName( _rModName ) ) |
685 | return false; |
686 | |
687 | // create new module |
688 | _out_rNewModuleCode = "REM ***** BASIC *****\n\n" ; |
689 | if ( _bCreateMain ) |
690 | _out_rNewModuleCode += "Sub Main\n\nEnd Sub\n" ; |
691 | |
692 | // insert module into library |
693 | xLib->insertByName( _rModName, makeAny( _out_rNewModuleCode ) ); |
694 | } |
695 | catch( const Exception& ) |
696 | { |
697 | DBG_UNHANDLED_EXCEPTION(); |
698 | return false; |
699 | } |
700 | |
701 | return true; |
702 | } |
703 | |
704 | |
705 | bool ScriptDocument::Impl::insertModuleOrDialog( LibraryContainerType _eType, const OUString& _rLibName, const OUString& _rObjectName, const Any& _rElement ) const |
706 | { |
707 | try |
708 | { |
709 | Reference< XNameContainer > xLib( getOrCreateLibrary( _eType, _rLibName ), UNO_QUERY_THROW ); |
710 | if ( xLib->hasByName( _rObjectName ) ) |
711 | return false; |
712 | |
713 | xLib->insertByName( _rObjectName, _rElement ); |
714 | return true; |
715 | } |
716 | catch( const Exception& ) |
717 | { |
718 | DBG_UNHANDLED_EXCEPTION(); |
719 | } |
720 | return false; |
721 | } |
722 | |
723 | |
724 | bool ScriptDocument::Impl::updateModule( const OUString& _rLibName, const OUString& _rModName, const OUString& _rModuleCode ) const |
725 | { |
726 | try |
727 | { |
728 | Reference< XNameContainer > xLib( getOrCreateLibrary( E_SCRIPTS, _rLibName ), UNO_QUERY_THROW ); |
729 | if ( !xLib->hasByName( _rModName ) ) |
730 | return false; |
731 | xLib->replaceByName( _rModName, makeAny( _rModuleCode ) ); |
732 | return true; |
733 | } |
734 | catch( const Exception& ) |
735 | { |
736 | DBG_UNHANDLED_EXCEPTION(); |
737 | } |
738 | return false; |
739 | } |
740 | |
741 | |
742 | bool ScriptDocument::Impl::createDialog( const OUString& _rLibName, const OUString& _rDialogName, Reference< XInputStreamProvider >& _out_rDialogProvider ) const |
743 | { |
744 | try |
745 | { |
746 | Reference< XNameContainer > xLib( getLibrary( E_DIALOGS, _rLibName, true ), UNO_QUERY_THROW ); |
747 | |
748 | // create dialog |
749 | _out_rDialogProvider.clear(); |
750 | if ( xLib->hasByName( _rDialogName ) ) |
751 | return false; |
752 | |
753 | // create new dialog model |
754 | Reference< XComponentContext > aContext( |
755 | comphelper::getProcessComponentContext() ); |
756 | Reference< XNameContainer > xDialogModel( |
757 | aContext->getServiceManager()->createInstanceWithContext( |
758 | "com.sun.star.awt.UnoControlDialogModel" , aContext ), |
759 | UNO_QUERY_THROW ); |
760 | |
761 | // set name property |
762 | Reference< XPropertySet > xDlgPSet( xDialogModel, UNO_QUERY_THROW ); |
763 | xDlgPSet->setPropertyValue( DLGED_PROP_NAME, makeAny( _rDialogName ) ); |
764 | |
765 | // export dialog model |
766 | _out_rDialogProvider = ::xmlscript::exportDialogModel( xDialogModel, aContext, isDocument() ? getDocument() : Reference< XModel >() ); |
767 | |
768 | // insert dialog into library |
769 | xLib->insertByName( _rDialogName, makeAny( _out_rDialogProvider ) ); |
770 | } |
771 | catch( const Exception& ) |
772 | { |
773 | DBG_UNHANDLED_EXCEPTION(); |
774 | } |
775 | |
776 | return _out_rDialogProvider.is(); |
777 | } |
778 | |
779 | |
780 | void ScriptDocument::Impl::setDocumentModified() const |
781 | { |
782 | OSL_ENSURE( isValid() && isDocument(), "ScriptDocument::Impl::setDocumentModified: only to be called for real documents!" ); |
783 | if ( isValid() && isDocument() ) |
784 | { |
785 | try |
786 | { |
787 | m_xDocModify->setModified( true ); |
788 | } |
789 | catch( const Exception& ) |
790 | { |
791 | DBG_UNHANDLED_EXCEPTION(); |
792 | } |
793 | } |
794 | } |
795 | |
796 | |
797 | bool ScriptDocument::Impl::isDocumentModified() const |
798 | { |
799 | OSL_ENSURE( isValid() && isDocument(), "ScriptDocument::Impl::isDocumentModified: only to be called for real documents!" ); |
800 | bool bIsModified = false; |
801 | if ( isValid() && isDocument() ) |
802 | { |
803 | try |
804 | { |
805 | bIsModified = m_xDocModify->isModified(); |
806 | } |
807 | catch( const Exception& ) |
808 | { |
809 | DBG_UNHANDLED_EXCEPTION(); |
810 | } |
811 | } |
812 | return bIsModified; |
813 | } |
814 | |
815 | |
816 | bool ScriptDocument::Impl::saveDocument( const Reference< XStatusIndicator >& _rxStatusIndicator ) const |
817 | { |
818 | Reference< XFrame > xFrame; |
819 | if ( !getCurrentFrame( xFrame ) ) |
820 | return false; |
821 | |
822 | Sequence< PropertyValue > aArgs; |
823 | if ( _rxStatusIndicator.is() ) |
824 | { |
825 | aArgs.realloc(1); |
826 | aArgs[0].Name = "StatusIndicator" ; |
827 | aArgs[0].Value <<= _rxStatusIndicator; |
828 | } |
829 | |
830 | try |
831 | { |
832 | URL aURL; |
833 | aURL.Complete = ".uno:Save" ; |
834 | aURL.Main = aURL.Complete; |
835 | aURL.Protocol = ".uno:" ; |
836 | aURL.Path = "Save" ; |
837 | |
838 | Reference< XDispatchProvider > xDispProv( xFrame, UNO_QUERY_THROW ); |
839 | Reference< XDispatch > xDispatch( |
840 | xDispProv->queryDispatch( aURL, "_self" , FrameSearchFlag::AUTO ), |
841 | UNO_SET_THROW ); |
842 | |
843 | xDispatch->dispatch( aURL, aArgs ); |
844 | } |
845 | catch( const Exception& ) |
846 | { |
847 | DBG_UNHANDLED_EXCEPTION(); |
848 | return false; |
849 | } |
850 | |
851 | return true; |
852 | } |
853 | |
854 | |
855 | OUString ScriptDocument::Impl::getTitle() const |
856 | { |
857 | OSL_PRECOND( isValid() && isDocument(), "ScriptDocument::Impl::getTitle: for documents only!" ); |
858 | |
859 | OUString sTitle; |
860 | if ( isValid() && isDocument() ) |
861 | { |
862 | sTitle = ::comphelper::DocumentInfo::getDocumentTitle( m_xDocument ); |
863 | } |
864 | return sTitle; |
865 | } |
866 | |
867 | |
868 | OUString ScriptDocument::Impl::getURL() const |
869 | { |
870 | OSL_PRECOND( isValid() && isDocument(), "ScriptDocument::Impl::getURL: for documents only!" ); |
871 | |
872 | OUString sURL; |
873 | if ( isValid() && isDocument() ) |
874 | { |
875 | try |
876 | { |
877 | sURL = m_xDocument->getURL(); |
878 | } |
879 | catch( const Exception& ) |
880 | { |
881 | DBG_UNHANDLED_EXCEPTION(); |
882 | } |
883 | } |
884 | return sURL; |
885 | } |
886 | |
887 | |
888 | bool ScriptDocument::Impl::allowMacros() const |
889 | { |
890 | OSL_ENSURE( isValid() && isDocument(), "ScriptDocument::Impl::allowMacros: for documents only!" ); |
891 | bool bAllow = false; |
892 | if ( isValid() && isDocument() ) |
893 | { |
894 | try |
895 | { |
896 | bAllow = m_xScriptAccess->getAllowMacroExecution(); |
897 | } |
898 | catch( const Exception& ) |
899 | { |
900 | DBG_UNHANDLED_EXCEPTION(); |
901 | } |
902 | } |
903 | return bAllow; |
904 | } |
905 | |
906 | |
907 | bool ScriptDocument::Impl::getCurrentFrame( Reference< XFrame >& _out_rxFrame ) const |
908 | { |
909 | _out_rxFrame.clear(); |
910 | OSL_PRECOND( isValid() && isDocument(), "ScriptDocument::Impl::getCurrentFrame: documents only!" ); |
911 | if ( !isValid() || !isDocument() ) |
912 | return false; |
913 | |
914 | try |
915 | { |
916 | Reference< XModel > xDocument( m_xDocument, UNO_SET_THROW ); |
917 | Reference< XController > xController( xDocument->getCurrentController(), UNO_SET_THROW ); |
918 | _out_rxFrame.set( xController->getFrame(), UNO_SET_THROW ); |
919 | } |
920 | catch( const Exception& ) |
921 | { |
922 | DBG_UNHANDLED_EXCEPTION(); |
923 | } |
924 | |
925 | return _out_rxFrame.is(); |
926 | } |
927 | |
928 | |
929 | bool ScriptDocument::Impl::isLibraryShared( const OUString& _rLibName, LibraryContainerType _eType ) |
930 | { |
931 | bool bIsShared = false; |
932 | try |
933 | { |
934 | Reference< XLibraryContainer2 > xLibContainer( getLibraryContainer( _eType ), UNO_QUERY_THROW ); |
935 | |
936 | if ( !xLibContainer->hasByName( _rLibName ) || !xLibContainer->isLibraryLink( _rLibName ) ) |
937 | return false; |
938 | OUString aFileURL; |
939 | Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() ); |
940 | Reference< XUriReferenceFactory > xUriFac = UriReferenceFactory::create(xContext); |
941 | |
942 | OUString aLinkURL( xLibContainer->getLibraryLinkURL( _rLibName ) ); |
943 | Reference< XUriReference > xUriRef( xUriFac->parse( aLinkURL ), UNO_QUERY_THROW ); |
944 | |
945 | OUString aScheme = xUriRef->getScheme(); |
946 | if ( aScheme.equalsIgnoreAsciiCase("file" ) ) |
947 | { |
948 | aFileURL = aLinkURL; |
949 | } |
950 | else if ( aScheme.equalsIgnoreAsciiCase("vnd.sun.star.pkg" ) ) |
951 | { |
952 | OUString aAuthority = xUriRef->getAuthority(); |
953 | if ( aAuthority.matchIgnoreAsciiCase("vnd.sun.star.expand:" ) ) |
954 | { |
955 | OUString aDecodedURL( aAuthority.copy( sizeof ( "vnd.sun.star.expand:" ) - 1 ) ); |
956 | aDecodedURL = ::rtl::Uri::decode( aDecodedURL, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 ); |
957 | Reference< XMacroExpander > xMacroExpander = theMacroExpander::get(xContext); |
958 | aFileURL = xMacroExpander->expandMacros( aDecodedURL ); |
959 | } |
960 | } |
961 | |
962 | if ( !aFileURL.isEmpty() ) |
963 | { |
964 | ::osl::DirectoryItem aFileItem; |
965 | ::osl::FileStatus aFileStatus( osl_FileStatus_Mask_FileURL ); |
966 | OSL_VERIFY( ::osl::DirectoryItem::get( aFileURL, aFileItem ) == ::osl::FileBase::E_None ); |
967 | OSL_VERIFY( aFileItem.getFileStatus( aFileStatus ) == ::osl::FileBase::E_None ); |
968 | OUString aCanonicalFileURL( aFileStatus.getFileURL() ); |
969 | |
970 | OUString aSearchURL1( "share/basic" ); |
971 | OUString aSearchURL2( "share/uno_packages" ); |
972 | OUString aSearchURL3( "share/extensions" ); |
973 | if( aCanonicalFileURL.indexOf( aSearchURL1 ) >= 0 || |
974 | aCanonicalFileURL.indexOf( aSearchURL2 ) >= 0 || |
975 | aCanonicalFileURL.indexOf( aSearchURL3 ) >= 0 ) |
976 | bIsShared = true; |
977 | } |
978 | } |
979 | catch( const Exception& ) |
980 | { |
981 | DBG_UNHANDLED_EXCEPTION(); |
982 | } |
983 | |
984 | return bIsShared; |
985 | } |
986 | |
987 | |
988 | void ScriptDocument::Impl::onDocumentCreated( const ScriptDocument& /*_rDocument*/ ) |
989 | { |
990 | // not interested in |
991 | } |
992 | |
993 | void ScriptDocument::Impl::onDocumentOpened( const ScriptDocument& /*_rDocument*/ ) |
994 | { |
995 | // not interested in |
996 | } |
997 | |
998 | void ScriptDocument::Impl::onDocumentSave( const ScriptDocument& /*_rDocument*/ ) |
999 | { |
1000 | // not interested in |
1001 | } |
1002 | |
1003 | void ScriptDocument::Impl::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ ) |
1004 | { |
1005 | // not interested in |
1006 | } |
1007 | |
1008 | void ScriptDocument::Impl::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ ) |
1009 | { |
1010 | // not interested in |
1011 | } |
1012 | |
1013 | void ScriptDocument::Impl::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ ) |
1014 | { |
1015 | // not interested in |
1016 | } |
1017 | |
1018 | void ScriptDocument::Impl::onDocumentClosed( const ScriptDocument& _rDocument ) |
1019 | { |
1020 | DBG_TESTSOLARMUTEX(); |
1021 | OSL_PRECOND( isValid(), "ScriptDocument::Impl::onDocumentClosed: should not be listening if I'm not valid!" ); |
1022 | |
1023 | bool bMyDocument = m_xDocument == _rDocument.getDocument(); |
1024 | OSL_PRECOND( bMyDocument, "ScriptDocument::Impl::onDocumentClosed: didn't want to know *this*!" ); |
1025 | if ( bMyDocument ) |
1026 | { |
1027 | m_bDocumentClosed = true; |
1028 | } |
1029 | } |
1030 | |
1031 | |
1032 | void ScriptDocument::Impl::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ ) |
1033 | { |
1034 | // not interested in |
1035 | } |
1036 | |
1037 | void ScriptDocument::Impl::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ ) |
1038 | { |
1039 | // not interested in |
1040 | } |
1041 | |
1042 | //==================================================================== |
1043 | //= ScriptDocument |
1044 | //==================================================================== |
1045 | ScriptDocument::ScriptDocument() |
1046 | :m_pImpl(new Impl) |
1047 | { } |
1048 | |
1049 | |
1050 | ScriptDocument::ScriptDocument( ScriptDocument::SpecialDocument _eType ) |
1051 | :m_pImpl( new Impl( Reference< XModel >() ) ) |
1052 | { |
1053 | OSL_ENSURE( _eType == NoDocument, "ScriptDocument::ScriptDocument: unknown SpecialDocument type!" ); |
1054 | (void)_eType; |
1055 | } |
1056 | |
1057 | |
1058 | ScriptDocument::ScriptDocument( const Reference< XModel >& _rxDocument ) |
1059 | :m_pImpl( new Impl( _rxDocument ) ) |
1060 | { |
1061 | OSL_ENSURE( _rxDocument.is(), "ScriptDocument::ScriptDocument: document must not be NULL!" ); |
1062 | // a NULL document results in an uninitialized instance, and for this |
1063 | // purpose, there is a dedicated constructor |
1064 | } |
1065 | |
1066 | |
1067 | ScriptDocument::ScriptDocument( const ScriptDocument& _rSource ) |
1068 | :m_pImpl( _rSource.m_pImpl ) |
1069 | { |
1070 | } |
1071 | |
1072 | |
1073 | ScriptDocument::~ScriptDocument() |
1074 | { |
1075 | } |
1076 | |
1077 | |
1078 | const ScriptDocument& ScriptDocument::getApplicationScriptDocument() |
1079 | { |
1080 | static ScriptDocument s_aApplicationScripts; |
1081 | return s_aApplicationScripts; |
1082 | } |
1083 | |
1084 | |
1085 | ScriptDocument ScriptDocument::getDocumentForBasicManager( const BasicManager* _pManager ) |
1086 | { |
1087 | if ( _pManager == SFX_APP()->GetBasicManager() ) |
1088 | return getApplicationScriptDocument(); |
1089 | |
1090 | docs::Documents aDocuments; |
1091 | lcl_getAllModels_throw( aDocuments, false ); |
1092 | |
1093 | for ( docs::Documents::const_iterator doc = aDocuments.begin(); |
1094 | doc != aDocuments.end(); |
1095 | ++doc |
1096 | ) |
1097 | { |
1098 | const BasicManager* pDocBasicManager = ::basic::BasicManagerRepository::getDocumentBasicManager( doc->xModel ); |
1099 | if ( ( pDocBasicManager != SFX_APP()->GetBasicManager() ) |
1100 | && ( pDocBasicManager == _pManager ) |
1101 | ) |
1102 | { |
1103 | return ScriptDocument( doc->xModel ); |
1104 | } |
1105 | } |
1106 | |
1107 | OSL_FAIL( "ScriptDocument::getDocumentForBasicManager: did not find a document for this manager!" ); |
1108 | return ScriptDocument( NoDocument ); |
1109 | } |
1110 | |
1111 | |
1112 | ScriptDocument ScriptDocument::getDocumentWithURLOrCaption( const OUString& _rUrlOrCaption ) |
1113 | { |
1114 | ScriptDocument aDocument( getApplicationScriptDocument() ); |
1115 | if ( _rUrlOrCaption.isEmpty() ) |
1116 | return aDocument; |
1117 | |
1118 | docs::Documents aDocuments; |
1119 | lcl_getAllModels_throw( aDocuments, false ); |
1120 | |
1121 | for ( docs::Documents::const_iterator doc = aDocuments.begin(); |
1122 | doc != aDocuments.end(); |
1123 | ++doc |
1124 | ) |
1125 | { |
1126 | const ScriptDocument aCheck = ScriptDocument( doc->xModel ); |
1127 | if ( _rUrlOrCaption == aCheck.getTitle() |
1128 | || _rUrlOrCaption == aCheck.getURL() |
1129 | ) |
1130 | { |
1131 | aDocument = aCheck; |
1132 | break; |
1133 | } |
1134 | } |
1135 | |
1136 | return aDocument; |
1137 | } |
1138 | |
1139 | |
1140 | namespace |
1141 | { |
1142 | struct DocumentTitleLess : public ::std::binary_function< ScriptDocument, ScriptDocument, bool > |
1143 | { |
1144 | DocumentTitleLess( const CollatorWrapper& _rCollator ) |
1145 | :m_aCollator( _rCollator ) |
1146 | { |
1147 | } |
1148 | |
1149 | bool operator()( const ScriptDocument& _lhs, const ScriptDocument& _rhs ) const |
1150 | { |
1151 | return m_aCollator.compareString( _lhs.getTitle(), _rhs.getTitle() ) < 0; |
1152 | } |
1153 | private: |
1154 | const CollatorWrapper m_aCollator; |
1155 | }; |
1156 | } |
1157 | |
1158 | |
1159 | ScriptDocuments ScriptDocument::getAllScriptDocuments( ScriptDocument::ScriptDocumentList _eListType ) |
1160 | { |
1161 | ScriptDocuments aScriptDocs; |
1162 | |
1163 | // include application? |
1164 | if ( _eListType == AllWithApplication ) |
1165 | aScriptDocs.push_back( getApplicationScriptDocument() ); |
1166 | |
1167 | // obtain documents |
1168 | try |
1169 | { |
1170 | docs::Documents aDocuments; |
1171 | lcl_getAllModels_throw( aDocuments, true /* exclude invisible */ ); |
1172 | |
1173 | for ( docs::Documents::const_iterator doc = aDocuments.begin(); |
1174 | doc != aDocuments.end(); |
1175 | ++doc |
1176 | ) |
1177 | { |
1178 | // exclude documents without script/library containers |
1179 | ScriptDocument aDoc( doc->xModel ); |
1180 | if ( !aDoc.isValid() ) |
1181 | continue; |
1182 | |
1183 | aScriptDocs.push_back( aDoc ); |
1184 | } |
1185 | } |
1186 | catch( const Exception& ) |
1187 | { |
1188 | DBG_UNHANDLED_EXCEPTION(); |
1189 | } |
1190 | |
1191 | // sort document list by doc title? |
1192 | if ( _eListType == DocumentsSorted ) |
1193 | { |
1194 | CollatorWrapper aCollator( ::comphelper::getProcessComponentContext() ); |
1195 | aCollator.loadDefaultCollator( SvtSysLocale().GetLanguageTag().getLocale(), 0 ); |
1196 | ::std::sort( aScriptDocs.begin(), aScriptDocs.end(), DocumentTitleLess( aCollator ) ); |
1197 | } |
1198 | |
1199 | return aScriptDocs; |
1200 | } |
1201 | |
1202 | |
1203 | bool ScriptDocument::operator==( const ScriptDocument& _rhs ) const |
1204 | { |
1205 | return m_pImpl->getDocumentRef() == _rhs.m_pImpl->getDocumentRef(); |
1206 | } |
1207 | |
1208 | |
1209 | sal_Int32 ScriptDocument::hashCode() const |
1210 | { |
1211 | return sal::static_int_cast<sal_Int32>(reinterpret_cast< sal_IntPtr >( m_pImpl->getDocumentRef().get() )); |
1212 | } |
1213 | |
1214 | |
1215 | bool ScriptDocument::isValid() const |
1216 | { |
1217 | return m_pImpl->isValid(); |
1218 | } |
1219 | |
1220 | |
1221 | bool ScriptDocument::isAlive() const |
1222 | { |
1223 | return m_pImpl->isAlive(); |
1224 | } |
1225 | |
1226 | |
1227 | Reference< XLibraryContainer > ScriptDocument::getLibraryContainer( LibraryContainerType _eType ) const |
1228 | { |
1229 | return m_pImpl->getLibraryContainer( _eType ); |
1230 | } |
1231 | |
1232 | |
1233 | Reference< XNameContainer > ScriptDocument::getLibrary( LibraryContainerType _eType, const OUString& _rLibName, bool _bLoadLibrary ) const |
1234 | SAL_THROW((NoSuchElementException)) |
1235 | { |
1236 | return m_pImpl->getLibrary( _eType, _rLibName, _bLoadLibrary ); |
1237 | } |
1238 | |
1239 | |
1240 | bool ScriptDocument::hasLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const |
1241 | { |
1242 | return m_pImpl->hasLibrary( _eType, _rLibName ); |
1243 | } |
1244 | |
1245 | |
1246 | Reference< XNameContainer > ScriptDocument::getOrCreateLibrary( LibraryContainerType _eType, const OUString& _rLibName ) const |
1247 | { |
1248 | return m_pImpl->getOrCreateLibrary( _eType, _rLibName ); |
1249 | } |
1250 | |
1251 | |
1252 | void ScriptDocument::loadLibraryIfExists( LibraryContainerType _eType, const OUString& _rLibrary ) |
1253 | { |
1254 | m_pImpl->loadLibraryIfExists( _eType, _rLibrary ); |
1255 | } |
1256 | |
1257 | |
1258 | Sequence< OUString > ScriptDocument::getObjectNames( LibraryContainerType _eType, const OUString& _rLibName ) const |
1259 | { |
1260 | Sequence< OUString > aModuleNames; |
1261 | |
1262 | try |
1263 | { |
1264 | if ( hasLibrary( _eType, _rLibName ) ) |
1265 | { |
1266 | Reference< XNameContainer > xLib( getLibrary( _eType, _rLibName, false ) ); |
1267 | if ( xLib.is() ) |
1268 | aModuleNames = xLib->getElementNames(); |
1269 | } |
1270 | } |
1271 | catch( const Exception& ) |
1272 | { |
1273 | DBG_UNHANDLED_EXCEPTION(); |
1274 | } |
1275 | |
1276 | // sort |
1277 | ::std::sort( aModuleNames.getArray() , aModuleNames.getArray() + aModuleNames.getLength() , StringCompareLessThan ); |
1278 | |
1279 | return aModuleNames; |
1280 | } |
1281 | |
1282 | |
1283 | OUString ScriptDocument::createObjectName( LibraryContainerType _eType, const OUString& _rLibName ) const |
1284 | { |
1285 | OUString aObjectName; |
1286 | |
1287 | OUString aBaseName = _eType == E_SCRIPTS ? OUString("Module" ) : OUString("Dialog" ); |
1288 | |
1289 | Sequence< OUString > aUsedNames( getObjectNames( _eType, _rLibName ) ); |
1290 | ::std::set< OUString > aUsedNamesCheck; |
1291 | ::std::copy( aUsedNames.getConstArray(), aUsedNames.getConstArray() + aUsedNames.getLength(), |
1292 | ::std::insert_iterator< ::std::set< OUString > >( aUsedNamesCheck, aUsedNamesCheck.begin() ) ); |
1293 | |
1294 | bool bValid = false; |
1295 | sal_Int32 i = 1; |
1296 | while ( !bValid ) |
1297 | { |
1298 | aObjectName = aBaseName; |
1299 | aObjectName += OUString::number( i ); |
1300 | |
1301 | if ( aUsedNamesCheck.find( aObjectName ) == aUsedNamesCheck.end() ) |
1302 | bValid = true; |
1303 | |
1304 | ++i; |
1305 | } |
1306 | |
1307 | return aObjectName; |
1308 | } |
1309 | |
1310 | |
1311 | Sequence< OUString > ScriptDocument::getLibraryNames() const |
1312 | { |
1313 | return GetMergedLibraryNames( getLibraryContainer( E_SCRIPTS ), getLibraryContainer( E_DIALOGS ) ); |
1314 | } |
1315 | |
1316 | |
1317 | bool ScriptDocument::isReadOnly() const |
1318 | { |
1319 | return m_pImpl->isReadOnly(); |
1320 | } |
1321 | |
1322 | |
1323 | bool ScriptDocument::isApplication() const |
1324 | { |
1325 | return m_pImpl->isApplication(); |
1326 | } |
1327 | |
1328 | bool ScriptDocument::isInVBAMode() const |
1329 | { |
1330 | return m_pImpl->isInVBAMode(); |
1331 | } |
1332 | |
1333 | |
1334 | BasicManager* ScriptDocument::getBasicManager() const |
1335 | { |
1336 | return m_pImpl->getBasicManager(); |
1337 | } |
1338 | |
1339 | |
1340 | Reference< XModel > ScriptDocument::getDocument() const |
1341 | { |
1342 | return m_pImpl->getDocument(); |
1343 | } |
1344 | |
1345 | |
1346 | Reference< XModel > ScriptDocument::getDocumentOrNull() const |
1347 | { |
1348 | if ( isDocument() ) |
1349 | return m_pImpl->getDocument(); |
1350 | return NULL; |
1351 | } |
1352 | |
1353 | |
1354 | bool ScriptDocument::removeModule( const OUString& _rLibName, const OUString& _rModuleName ) const |
1355 | { |
1356 | return m_pImpl->removeModuleOrDialog( E_SCRIPTS, _rLibName, _rModuleName ); |
1357 | } |
1358 | |
1359 | |
1360 | bool ScriptDocument::hasModule( const OUString& _rLibName, const OUString& _rModuleName ) const |
1361 | { |
1362 | return m_pImpl->hasModuleOrDialog( E_SCRIPTS, _rLibName, _rModuleName ); |
1363 | } |
1364 | |
1365 | |
1366 | bool ScriptDocument::getModule( const OUString& _rLibName, const OUString& _rModName, OUString& _out_rModuleSource ) const |
1367 | { |
1368 | Any aCode; |
1369 | if ( !m_pImpl->getModuleOrDialog( E_SCRIPTS, _rLibName, _rModName, aCode ) ) |
1370 | return false; |
1371 | OSL_VERIFY( aCode >>= _out_rModuleSource ); |
1372 | return true; |
1373 | } |
1374 | |
1375 | |
1376 | bool ScriptDocument::renameModule( const OUString& _rLibName, const OUString& _rOldName, const OUString& _rNewName ) const |
1377 | { |
1378 | return m_pImpl->renameModuleOrDialog( E_SCRIPTS, _rLibName, _rOldName, _rNewName, NULL ); |
1379 | } |
1380 | |
1381 | |
1382 | bool ScriptDocument::createModule( const OUString& _rLibName, const OUString& _rModName, bool _bCreateMain, OUString& _out_rNewModuleCode ) const |
1383 | { |
1384 | if ( !m_pImpl->createModule( _rLibName, _rModName, _bCreateMain, _out_rNewModuleCode ) ) |
1385 | return false; |
1386 | |
1387 | // doc shell modified |
1388 | MarkDocumentModified( *const_cast< ScriptDocument* >( this ) ); // here? |
1389 | return true; |
1390 | } |
1391 | |
1392 | |
1393 | bool ScriptDocument::insertModule( const OUString& _rLibName, const OUString& _rModName, const OUString& _rModuleCode ) const |
1394 | { |
1395 | return m_pImpl->insertModuleOrDialog( E_SCRIPTS, _rLibName, _rModName, makeAny( _rModuleCode ) ); |
1396 | } |
1397 | |
1398 | |
1399 | bool ScriptDocument::updateModule( const OUString& _rLibName, const OUString& _rModName, const OUString& _rModuleCode ) const |
1400 | { |
1401 | return m_pImpl->updateModule( _rLibName, _rModName, _rModuleCode ); |
1402 | } |
1403 | |
1404 | |
1405 | bool ScriptDocument::removeDialog( const OUString& _rLibName, const OUString& _rDialogName ) const |
1406 | { |
1407 | return m_pImpl->removeModuleOrDialog( E_DIALOGS, _rLibName, _rDialogName ); |
1408 | } |
1409 | |
1410 | |
1411 | bool ScriptDocument::hasDialog( const OUString& _rLibName, const OUString& _rDialogName ) const |
1412 | { |
1413 | return m_pImpl->hasModuleOrDialog( E_DIALOGS, _rLibName, _rDialogName ); |
1414 | } |
1415 | |
1416 | |
1417 | bool ScriptDocument::getDialog( const OUString& _rLibName, const OUString& _rDialogName, Reference< XInputStreamProvider >& _out_rDialogProvider ) const |
1418 | { |
1419 | Any aCode; |
1420 | if ( !m_pImpl->getModuleOrDialog( E_DIALOGS, _rLibName, _rDialogName, aCode ) ) |
1421 | return false; |
1422 | OSL_VERIFY( aCode >>= _out_rDialogProvider ); |
1423 | return _out_rDialogProvider.is(); |
1424 | } |
1425 | |
1426 | |
1427 | bool ScriptDocument::renameDialog( const OUString& _rLibName, const OUString& _rOldName, const OUString& _rNewName, const Reference< XNameContainer >& _rxExistingDialogModel ) const |
1428 | { |
1429 | return m_pImpl->renameModuleOrDialog( E_DIALOGS, _rLibName, _rOldName, _rNewName, _rxExistingDialogModel ); |
1430 | } |
1431 | |
1432 | |
1433 | bool ScriptDocument::createDialog( const OUString& _rLibName, const OUString& _rDialogName, Reference< XInputStreamProvider >& _out_rDialogProvider ) const |
1434 | { |
1435 | if ( !m_pImpl->createDialog( _rLibName, _rDialogName, _out_rDialogProvider ) ) |
1436 | return false; |
1437 | |
1438 | MarkDocumentModified( *const_cast< ScriptDocument* >( this ) ); // here? |
1439 | return true; |
1440 | } |
1441 | |
1442 | |
1443 | bool ScriptDocument::insertDialog( const OUString& _rLibName, const OUString& _rDialogName, const Reference< XInputStreamProvider >& _rxDialogProvider ) const |
1444 | { |
1445 | return m_pImpl->insertModuleOrDialog( E_DIALOGS, _rLibName, _rDialogName, makeAny( _rxDialogProvider ) ); |
1446 | } |
1447 | |
1448 | |
1449 | void ScriptDocument::setDocumentModified() const |
1450 | { |
1451 | m_pImpl->setDocumentModified(); |
1452 | } |
1453 | |
1454 | |
1455 | bool ScriptDocument::isDocumentModified() const |
1456 | { |
1457 | return m_pImpl->isDocumentModified(); |
1458 | } |
1459 | |
1460 | |
1461 | bool ScriptDocument::saveDocument( const Reference< XStatusIndicator >& _rxStatusIndicator ) const |
1462 | { |
1463 | return m_pImpl->saveDocument( _rxStatusIndicator ); |
1464 | } |
1465 | |
1466 | |
1467 | LibraryLocation ScriptDocument::getLibraryLocation( const OUString& _rLibName ) const |
1468 | { |
1469 | LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN; |
1470 | if ( !_rLibName.isEmpty() ) |
1471 | { |
1472 | if ( isDocument() ) |
1473 | { |
1474 | eLocation = LIBRARY_LOCATION_DOCUMENT; |
1475 | } |
1476 | else |
1477 | { |
1478 | if ( ( hasLibrary( E_SCRIPTS, _rLibName ) && !m_pImpl->isLibraryShared( _rLibName, E_SCRIPTS ) ) |
1479 | || ( hasLibrary( E_DIALOGS, _rLibName ) && !m_pImpl->isLibraryShared( _rLibName, E_DIALOGS ) ) |
1480 | ) |
1481 | { |
1482 | eLocation = LIBRARY_LOCATION_USER; |
1483 | } |
1484 | else |
1485 | { |
1486 | eLocation = LIBRARY_LOCATION_SHARE; |
1487 | } |
1488 | } |
1489 | } |
1490 | |
1491 | return eLocation; |
1492 | } |
1493 | |
1494 | |
1495 | OUString ScriptDocument::getTitle( LibraryLocation _eLocation, LibraryType _eType ) const |
1496 | { |
1497 | OUString aTitle; |
1498 | |
1499 | switch ( _eLocation ) |
1500 | { |
1501 | case LIBRARY_LOCATION_USER: |
1502 | { |
1503 | switch ( _eType ) |
1504 | { |
1505 | case LIBRARY_TYPE_MODULE: aTitle = IDE_RESSTR(RID_STR_USERMACROS); break; |
1506 | case LIBRARY_TYPE_DIALOG: aTitle = IDE_RESSTR(RID_STR_USERDIALOGS); break; |
1507 | case LIBRARY_TYPE_ALL: aTitle = IDE_RESSTR(RID_STR_USERMACROSDIALOGS); break; |
1508 | default: |
1509 | break; |
1510 | } |
1511 | break; |
1512 | case LIBRARY_LOCATION_SHARE: |
1513 | { |
1514 | switch ( _eType ) |
1515 | { |
1516 | case LIBRARY_TYPE_MODULE: aTitle = IDE_RESSTR(RID_STR_SHAREMACROS); break; |
1517 | case LIBRARY_TYPE_DIALOG: aTitle = IDE_RESSTR(RID_STR_SHAREDIALOGS); break; |
1518 | case LIBRARY_TYPE_ALL: aTitle = IDE_RESSTR(RID_STR_SHAREMACROSDIALOGS); break; |
1519 | default: |
1520 | break; |
1521 | } |
1522 | } |
1523 | break; |
1524 | case LIBRARY_LOCATION_DOCUMENT: |
1525 | aTitle = getTitle(); |
1526 | break; |
1527 | default: |
1528 | break; |
1529 | } |
1530 | } |
1531 | |
1532 | return aTitle; |
1533 | } |
1534 | |
1535 | |
1536 | OUString ScriptDocument::getTitle() const |
1537 | { |
1538 | return m_pImpl->getTitle(); |
1539 | } |
1540 | |
1541 | |
1542 | OUString ScriptDocument::getURL() const |
1543 | { |
1544 | return m_pImpl->getURL(); |
1545 | } |
1546 | |
1547 | |
1548 | bool ScriptDocument::isActive() const |
1549 | { |
1550 | bool bIsActive( false ); |
1551 | try |
1552 | { |
1553 | Reference< XFrame > xFrame; |
1554 | if ( m_pImpl->getCurrentFrame( xFrame ) ) |
1555 | bIsActive = xFrame->isActive(); |
1556 | } |
1557 | catch( const Exception& ) |
1558 | { |
1559 | DBG_UNHANDLED_EXCEPTION(); |
1560 | } |
1561 | return bIsActive; |
1562 | } |
1563 | |
1564 | |
1565 | bool ScriptDocument::allowMacros() const |
1566 | { |
1567 | return m_pImpl->allowMacros(); |
1568 | } |
1569 | |
1570 | } // namespace basctl |
1571 | |
1572 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
1573 | |