Debian Codegraph @

basic/source/uno/namecont.cxx

raw
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 <config_extensions.h>
21 #include <config_folders.h>
22
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <com/sun/star/embed/XTransactedObject.hpp>
26 #include <com/sun/star/io/IOException.hpp>
27 #include <com/sun/star/lang/NoSupportException.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/ucb/ContentCreationException.hpp>
31 #include <com/sun/star/xml/sax/SAXException.hpp>
32 #include <utility>
33 #include <vcl/svapp.hxx>
34 #include <o3tl/string_view.hxx>
35 #include <o3tl/temporary.hxx>
36 #include <vcl/errinf.hxx>
37 #include <rtl/ustring.hxx>
38 #include <sal/log.hxx>
39 #include <sot/storage.hxx>
40 #include <comphelper/getexpandeduri.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/sequence.hxx>
43
44 #include <namecont.hxx>
45 #include <basic/basicmanagerrepository.hxx>
46 #include <comphelper/diagnose_ex.hxx>
47 #include <tools/urlobj.hxx>
48 #include <unotools/pathoptions.hxx>
49 #include <svtools/sfxecode.hxx>
50 #include <svtools/ehdl.hxx>
51 #include <basic/basmgr.hxx>
52 #include <com/sun/star/xml/sax/Parser.hpp>
53 #include <com/sun/star/xml/sax/InputSource.hpp>
54 #include <com/sun/star/io/XOutputStream.hpp>
55 #include <com/sun/star/xml/sax/Writer.hpp>
56 #include <com/sun/star/io/XInputStream.hpp>
57 #include <com/sun/star/beans/XPropertySet.hpp>
58 #include <com/sun/star/uno/DeploymentException.hpp>
59 #include <com/sun/star/lang/DisposedException.hpp>
60 #include <com/sun/star/script/LibraryNotLoadedException.hpp>
61 #include <com/sun/star/script/vba/VBAScriptEventId.hpp>
62 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
63 #include <com/sun/star/util/PathSubstitution.hpp>
64 #include <com/sun/star/deployment/ExtensionManager.hpp>
65 #include <comphelper/storagehelper.hxx>
66 #include <cppuhelper/exc_hlp.hxx>
67 #include <cppuhelper/supportsservice.hxx>
68 #include <memory>
69 #include <string_view>
70
71 namespace basic
72 {
73
74 using namespace com::sun::star::document;
75 using namespace com::sun::star::container;
76 using namespace com::sun::star::uno;
77 using namespace com::sun::star::lang;
78 using namespace com::sun::star::io;
79 using namespace com::sun::star::ucb;
80 using namespace com::sun::star::script;
81 using namespace com::sun::star::beans;
82 using namespace com::sun::star::xml::sax;
83 using namespace com::sun::star::util;
84 using namespace com::sun::star::task;
85 using namespace com::sun::star::embed;
86 using namespace com::sun::star::frame;
87 using namespace com::sun::star::deployment;
88 using namespace com::sun::star;
89 using namespace cppu;
90
91 using com::sun::star::uno::Reference;
92
93 // #i34411: Flag for error handling during migration
94 static bool GbMigrationSuppressErrors = false;
95
96
97 // Implementation class NameContainer
98
99 // Methods XElementAccess
100 Type NameContainer::getElementType()
101 {
102 return mType;
103 }
104
105 sal_Bool NameContainer::hasElements()
106 {
107 return !maMap.empty();
108 }
109
110 // Methods XNameAccess
111 Any NameContainer::getByName( const OUString& aName )
112 {
113 auto aIt = maMap.find(aName);
114 if (aIt == maMap.end())
115 {
116 throw NoSuchElementException(aName);
117 }
118 return aIt->second;
119 }
120
121 Sequence< OUString > NameContainer::getElementNames()
122 {
123 return comphelper::mapKeysToSequence(maMap);
124 }
125
126 sal_Bool NameContainer::hasByName( const OUString& aName )
127 {
128 return maMap.contains(aName);
129 }
130
131
132 // Methods XNameReplace
133 void NameContainer::replaceByName(const OUString& aName, const Any& aElement,
134 std::unique_lock<std::mutex>& guard)
135 {
136 const Type& aAnyType = aElement.getValueType();
137 if( mType != aAnyType )
138 {
139 throw IllegalArgumentException(u"types do not match"_ustr, rOwner, 2);
140 }
141 auto aIt = maMap.find(aName);
142 if (aIt == maMap.end())
143 {
144 throw NoSuchElementException(aName);
145 }
146 Any aOldElement = aIt->second;
147 aIt->second = aElement;
148
149 // Fire event
150 if (maContainerListeners.getLength(guard) > 0)
151 {
152 ContainerEvent aEvent;
153 aEvent.Source = mpxEventSource;
154 aEvent.Accessor <<= aName;
155 aEvent.Element = aElement;
156 aEvent.ReplacedElement = aOldElement;
157 maContainerListeners.notifyEach(guard, &XContainerListener::elementReplaced, aEvent);
158 }
159
160 /* After the container event has been fired (one listener will update the
161 core Basic manager), fire change event. Listeners can rely on that the
162 Basic source code of the core Basic manager is up-to-date. */
163 if (maChangesListeners.getLength(guard) > 0)
164 {
165 ChangesEvent aEvent;
166 aEvent.Source = mpxEventSource;
167 aEvent.Base <<= aEvent.Source;
168 aEvent.Changes = { { Any(aName), aElement, aOldElement } };
169 maChangesListeners.notifyEach(guard, &XChangesListener::changesOccurred, aEvent);
170 }
171 }
172
173 void NameContainer::insertNoCheck(const OUString& aName, const Any& aElement,
174 std::unique_lock<std::mutex>& guard)
175 {
176 const Type& aAnyType = aElement.getValueType();
177 if( mType != aAnyType )
178 {
179 throw IllegalArgumentException(u"types do not match"_ustr, rOwner, 2);
180 }
181
182 maMap[aName] = aElement;
183
184 // Fire event
185 if (maContainerListeners.getLength(guard) > 0)
186 {
187 ContainerEvent aEvent;
188 aEvent.Source = mpxEventSource;
189 aEvent.Accessor <<= aName;
190 aEvent.Element = aElement;
191 maContainerListeners.notifyEach(guard, &XContainerListener::elementInserted, aEvent);
192 }
193
194 /* After the container event has been fired (one listener will update the
195 core Basic manager), fire change event. Listeners can rely on that the
196 Basic source code of the core Basic manager is up-to-date. */
197 if (maChangesListeners.getLength(guard) > 0)
198 {
199 ChangesEvent aEvent;
200 aEvent.Source = mpxEventSource;
201 aEvent.Base <<= aEvent.Source;
202 aEvent.Changes = { { Any(aName), aElement, {} } };
203 maChangesListeners.notifyEach(guard, &XChangesListener::changesOccurred, aEvent);
204 }
205 }
206
207 // Methods XNameContainer
208 void NameContainer::insertByName(const OUString& aName, const Any& aElement,
209 std::unique_lock<std::mutex>& guard)
210 {
211 if (hasByName(aName))
212 throw ElementExistException(aName);
213 insertNoCheck(aName, aElement, guard);
214 }
215
216 void NameContainer::removeByName(const OUString& aName, std::unique_lock<std::mutex>& guard)
217 {
218 auto aIt = maMap.find(aName);
219 if (aIt == maMap.end())
220 {
221 throw NoSuchElementException("\"" + aName + "\" not found");
222 }
223
224 Any aOldElement = aIt->second;
225 maMap.erase(aIt);
226
227 // Fire event
228 if (maContainerListeners.getLength(guard) > 0)
229 {
230 ContainerEvent aEvent;
231 aEvent.Source = mpxEventSource;
232 aEvent.Accessor <<= aName;
233 aEvent.Element = aOldElement;
234 maContainerListeners.notifyEach(guard, &XContainerListener::elementRemoved, aEvent);
235 }
236
237 /* After the container event has been fired (one listener will update the
238 core Basic manager), fire change event. Listeners can rely on that the
239 Basic source code of the core Basic manager is up-to-date. */
240 if (maChangesListeners.getLength(guard) > 0)
241 {
242 ChangesEvent aEvent;
243 aEvent.Source = mpxEventSource;
244 aEvent.Base <<= aEvent.Source;
245 aEvent.Changes = { { Any(aName),
246 {}, // Element remains empty (meaning "replaced with nothing")
247 aOldElement } };
248 maChangesListeners.notifyEach(guard, &XChangesListener::changesOccurred, aEvent);
249 }
250 }
251
252
253 // Methods XContainer
254 void NameContainer::addContainerListener(const Reference<XContainerListener>& xListener,
255 std::unique_lock<std::mutex>& guard)
256 {
257 if( !xListener.is() )
258 {
259 throw RuntimeException(u"addContainerListener called with null xListener"_ustr,rOwner);
260 }
261 maContainerListeners.addInterface(guard, xListener);
262 }
263
264 void NameContainer::removeContainerListener(const Reference<XContainerListener>& xListener,
265 std::unique_lock<std::mutex>& guard)
266 {
267 if( !xListener.is() )
268 {
269 throw RuntimeException(u"removeContainerListener called with null xListener"_ustr,rOwner);
270 }
271 maContainerListeners.removeInterface(guard, xListener);
272 }
273
274 // Methods XChangesNotifier
275 void NameContainer::addChangesListener(const Reference<XChangesListener>& xListener,
276 std::unique_lock<std::mutex>& guard)
277 {
278 if( !xListener.is() )
279 {
280 throw RuntimeException(u"addChangesListener called with null xListener"_ustr,rOwner);
281 }
282 maChangesListeners.addInterface(guard, xListener);
283 }
284
285 void NameContainer::removeChangesListener(const Reference<XChangesListener>& xListener,
286 std::unique_lock<std::mutex>& guard)
287 {
288 if( !xListener.is() )
289 {
290 throw RuntimeException(u"removeChangesListener called with null xListener"_ustr,rOwner);
291 }
292 maChangesListeners.removeInterface(guard, xListener);
293 }
294
295
296 // ModifiableHelper
297
298 void ModifiableHelper::setModified(bool _bModified, std::unique_lock<std::mutex>& guard)
299 {
300 if ( _bModified == mbModified )
301 {
302 return;
303 }
304 mbModified = _bModified;
305
306 if (m_aModifyListeners.getLength(guard) == 0)
307 {
308 return;
309 }
310 EventObject aModifyEvent( m_rEventSource );
311 m_aModifyListeners.notifyEach(guard, &XModifyListener::modified, aModifyEvent);
312 }
313
314
315 // Ctor
316 SfxLibraryContainer::SfxLibraryContainer()
317 : mnRunningVBAScripts( 0 )
318 , mbVBACompat( false )
319 , meVBATextEncoding( RTL_TEXTENCODING_DONTKNOW )
320 , maModifiable( *this )
321 , maNameContainer( cppu::UnoType<XNameAccess>::get(), *this )
322 , mpBasMgr( nullptr )
323 , mbOwnBasMgr( false )
324 , mbOldInfoFormat( false )
325 , mbOasis2OOoFormat( false )
326 , meInitMode(DEFAULT)
327 {
328 mxContext = comphelper::getProcessComponentContext();
329
330 mxSFI = ucb::SimpleFileAccess::create( mxContext );
331
332 mxStringSubstitution = util::PathSubstitution::create( mxContext );
333 }
334
335 SfxLibraryContainer::~SfxLibraryContainer()
336 {
337 if( mbOwnBasMgr )
338 {
339 delete mpBasMgr;
340 }
341 }
342
343 void SfxLibraryContainer::enterMethod()
344 {
345 Application::GetSolarMutex().acquire();
346 if (m_bDisposed)
347 {
348 throw DisposedException( OUString(), *this );
349 }
350 }
351
352 void SfxLibraryContainer::leaveMethod()
353 {
354 Application::GetSolarMutex().release();
355 }
356
357 BasicManager* SfxLibraryContainer::getBasicManager()
358 {
359 try
360 {
361 if ( mpBasMgr )
362 {
363 return mpBasMgr;
364 }
365 Reference< XModel > xDocument( mxOwnerDocument.get(), UNO_QUERY );
366 SAL_WARN_IF(
367 !xDocument.is(), "basic",
368 ("SfxLibraryContainer::getBasicManager: cannot obtain a BasicManager"
369 " without document!"));
370 if ( xDocument.is() )
371 {
372 mpBasMgr = BasicManagerRepository::getDocumentBasicManager( xDocument );
373 }
374 }
375 catch (const css::ucb::ContentCreationException&)
376 {
377 TOOLS_WARN_EXCEPTION( "basic", "SfxLibraryContainer::getBasicManager:" );
378 }
379 return mpBasMgr;
380 }
381
382 // Methods XStorageBasedLibraryContainer
383 Reference< XStorage > SAL_CALL SfxLibraryContainer::getRootStorage()
384 {
385 LibraryContainerMethodGuard aGuard( *this );
386 return mxStorage;
387 }
388
389 void SAL_CALL SfxLibraryContainer::setRootStorage( const Reference< XStorage >& _rxRootStorage )
390 {
391 LibraryContainerMethodGuard aGuard( *this );
392 if ( !_rxRootStorage.is() )
393 {
394 throw IllegalArgumentException(u"no root storage"_ustr, getXWeak(), 1);
395 }
396 mxStorage = _rxRootStorage;
397 onNewRootStorage();
398 }
399
400 void SAL_CALL SfxLibraryContainer::storeLibrariesToStorage( const Reference< XStorage >& _rxRootStorage )
401 {
402 LibraryContainerMethodGuard aGuard( *this );
403 if ( !_rxRootStorage.is() )
404 {
405 throw IllegalArgumentException(u"no root storage"_ustr, getXWeak(), 1);
406 }
407 try
408 {
409 storeLibraries_Impl(_rxRootStorage, true, o3tl::temporary(std::unique_lock(m_aMutex)));
410 }
411 catch( const Exception& )
412 {
413 throw WrappedTargetException( OUString(),
414 *this, ::cppu::getCaughtException() );
415 }
416 }
417
418
419 // Methods XModifiable
420 sal_Bool SfxLibraryContainer::isModified()
421 {
422 LibraryContainerMethodGuard aGuard( *this );
423 if ( maModifiable.isModified() )
424 {
425 return true;
426 }
427 // the library container is not modified, go through the libraries and check whether they are modified
428 for (auto& aName : maNameContainer.getElementNames())
429 {
430 try
431 {
432 SfxLibrary* pImplLib = getImplLib( aName );
433 if( pImplLib->isModified() )
434 {
435 if ( aName == "Standard" )
436 {
437 // this is a workaround that has to be implemented because
438 // empty standard library should stay marked as modified
439 // but should not be treated as modified while it is empty
440 if ( pImplLib->hasElements() )
441 return true;
442 }
443 else
444 {
445 return true;
446 }
447 }
448 }
449 catch(const css::container::NoSuchElementException&)
450 {
451 }
452 }
453
454 return false;
455 }
456
457 void SAL_CALL SfxLibraryContainer::setModified( sal_Bool _bModified )
458 {
459 LibraryContainerMethodGuard aGuard( *this );
460 maModifiable.setModified(_bModified, o3tl::temporary(std::unique_lock(m_aMutex)));
461 }
462
463 void SAL_CALL SfxLibraryContainer::addModifyListener( const Reference< XModifyListener >& _rxListener )
464 {
465 LibraryContainerMethodGuard aGuard( *this );
466 maModifiable.addModifyListener(_rxListener, o3tl::temporary(std::unique_lock(m_aMutex)));
467 }
468
469 void SAL_CALL SfxLibraryContainer::removeModifyListener( const Reference< XModifyListener >& _rxListener )
470 {
471 LibraryContainerMethodGuard aGuard( *this );
472 maModifiable.removeModifyListener(_rxListener, o3tl::temporary(std::unique_lock(m_aMutex)));
473 }
474
475 // Methods XPersistentLibraryContainer
476 Any SAL_CALL SfxLibraryContainer::getRootLocation()
477 {
478 LibraryContainerMethodGuard aGuard( *this );
479 return Any( getRootStorage() );
480 }
481
482 OUString SAL_CALL SfxLibraryContainer::getContainerLocationName()
483 {
484 LibraryContainerMethodGuard aGuard( *this );
485 return maLibrariesDir;
486 }
487
488 void SAL_CALL SfxLibraryContainer::storeLibraries( )
489 {
490 LibraryContainerMethodGuard aGuard( *this );
491 try
492 {
493 storeLibraries_Impl(mxStorage, mxStorage.is(), o3tl::temporary(std::unique_lock(m_aMutex)));
494 // we need to store *all* libraries if and only if we are based on a storage:
495 // in this case, storeLibraries_Impl will remove the source storage, after loading
496 // all libraries, so we need to force them to be stored, again
497 }
498 catch( const Exception& )
499 {
500 throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
501 }
502 }
503
504 namespace
505 {
506 void checkAndCopyFileImpl( const INetURLObject& rSourceFolderInetObj,
507 const INetURLObject& rTargetFolderInetObj,
508 std::u16string_view rCheckFileName,
509 std::u16string_view rCheckExtension,
510 const Reference< XSimpleFileAccess3 >& xSFI )
511 {
512 INetURLObject aTargetFolderInetObj( rTargetFolderInetObj );
513 aTargetFolderInetObj.insertName( rCheckFileName, true, INetURLObject::LAST_SEGMENT,
514 INetURLObject::EncodeMechanism::All );
515 aTargetFolderInetObj.setExtension( rCheckExtension );
516 OUString aTargetFile = aTargetFolderInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
517 if( !xSFI->exists( aTargetFile ) )
518 {
519 INetURLObject aSourceFolderInetObj( rSourceFolderInetObj );
520 aSourceFolderInetObj.insertName( rCheckFileName, true, INetURLObject::LAST_SEGMENT,
521 INetURLObject::EncodeMechanism::All );
522 aSourceFolderInetObj.setExtension( rCheckExtension );
523 OUString aSourceFile = aSourceFolderInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
524 xSFI->copy( aSourceFile, aTargetFile );
525 }
526 }
527
528 constexpr OUString sUserBasicVariablePrefix = u"$(USER)/basic/"_ustr;
529 constexpr OUString sInstBasicVariablePrefix = u"$(INST)/" LIBO_SHARE_FOLDER "/basic/"_ustr;
530
531 void createVariableURL( OUString& rStr, std::u16string_view rLibName,
532 std::u16string_view rInfoFileName, bool bUser )
533 {
534 if( bUser )
535 {
536 rStr = sUserBasicVariablePrefix;
537 }
538 else
539 {
540 rStr = sInstBasicVariablePrefix;
541 }
542 rStr += OUString::Concat(rLibName) + "/" + rInfoFileName + ".xlb/";
543 }
544 }
545
546 void SfxLibraryContainer::init(const OUString& rInitialDocumentURL,
547 const uno::Reference<embed::XStorage>& rxInitialStorage,
548 std::unique_lock<std::mutex>& guard)
549 {
550 // this might be called from within the ctor, and the impl_init might (indirectly) create
551 // a UNO reference to ourself.
552 // Ensure that we're not destroyed while we're in here
553 osl_atomic_increment( &m_refCount );
554 init_Impl(rInitialDocumentURL, rxInitialStorage, guard);
555 osl_atomic_decrement( &m_refCount );
556 }
557
558 void SfxLibraryContainer::init_Impl( const OUString& rInitialDocumentURL,
559 const uno::Reference< embed::XStorage >& rxInitialStorage,
560 std::unique_lock<std::mutex>& guard )
561 {
562 uno::Reference< embed::XStorage > xStorage = rxInitialStorage;
563
564 maInitialDocumentURL = rInitialDocumentURL;
565 maInfoFileName = getInfoFileName();
566 maOldInfoFileName = getOldInfoFileName();
567 maLibElementFileExtension = getLibElementFileExtension();
568 maLibrariesDir = getLibrariesDir();
569
570 meInitMode = DEFAULT;
571 INetURLObject aInitUrlInetObj( maInitialDocumentURL );
572 OUString aInitFileName = aInitUrlInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
573 if( !aInitFileName.isEmpty() )
574 {
575 // We need a BasicManager to avoid problems
576 StarBASIC* pBas = new StarBASIC();
577 mpBasMgr = new BasicManager( pBas );
578 mbOwnBasMgr = true;
579
580 OUString aExtension = aInitUrlInetObj.getExtension();
581 if( aExtension == "xlc" )
582 {
583 meInitMode = CONTAINER_INIT_FILE;
584 INetURLObject aLibPathInetObj( std::move(aInitUrlInetObj) );
585 aLibPathInetObj.removeSegment();
586 maLibraryPath = aLibPathInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
587 }
588 else if( aExtension == "xlb" )
589 {
590 meInitMode = LIBRARY_INIT_FILE;
591 uno::Reference< embed::XStorage > xDummyStor;
592 ::xmlscript::LibDescriptor aLibDesc;
593 implLoadLibraryIndexFile(nullptr, aLibDesc, xDummyStor, aInitFileName, guard);
594 return;
595 }
596 else
597 {
598 // Decide between old and new document
599 bool bOldStorage = SotStorage::IsOLEStorage( aInitFileName );
600 if ( bOldStorage )
601 {
602 meInitMode = OLD_BASIC_STORAGE;
603 importFromOldStorage( aInitFileName );
604 return;
605 }
606 else
607 {
608 meInitMode = OFFICE_DOCUMENT;
609 try
610 {
611 xStorage = ::comphelper::OStorageHelper::GetStorageFromURL( aInitFileName, embed::ElementModes::READ );
612 }
613 catch (const uno::Exception& )
614 {
615 // TODO: error handling
616 }
617 }
618 }
619 }
620 else
621 {
622 // Default paths
623 maLibraryPath = SvtPathOptions().GetBasicPath();
624 }
625
626 uno::Reference< io::XInputStream > xInput;
627
628 mxStorage = xStorage;
629 bool bStorage = mxStorage.is();
630
631
632 // #110009: Scope to force the StorageRefs to be destructed and
633 // so the streams to be closed before the preload operation
634 {
635
636 uno::Reference< embed::XStorage > xLibrariesStor;
637 OUString aFileName;
638
639 int nPassCount = 1;
640 if( !bStorage && meInitMode == DEFAULT )
641 {
642 nPassCount = 2;
643 }
644 for( int nPass = 0 ; nPass < nPassCount ; nPass++ )
645 {
646 if( bStorage )
647 {
648 SAL_WARN_IF(
649 meInitMode != DEFAULT && meInitMode != OFFICE_DOCUMENT, "basic",
650 "Wrong InitMode for document");
651 try
652 {
653 uno::Reference< io::XStream > xStream;
654 xLibrariesStor = xStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
655
656 if ( xLibrariesStor.is() )
657 {
658 aFileName = maInfoFileName + "-lc.xml";
659 try
660 {
661 xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
662 }
663 catch(const uno::Exception& )
664 {}
665
666 if( !xStream.is() )
667 {
668 mbOldInfoFormat = true;
669
670 // Check old version
671 aFileName = maOldInfoFileName + ".xml";
672 try
673 {
674 xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
675 }
676 catch(const uno::Exception& )
677 {}
678
679 if( !xStream.is() )
680 {
681 // Check for EA2 document version with wrong extensions
682 aFileName = maOldInfoFileName + ".xli";
683 xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
684 }
685 }
686 }
687
688 if ( xStream.is() )
689 {
690 xInput = xStream->getInputStream();
691 }
692 }
693 catch(const uno::Exception& )
694 {
695 // TODO: error handling?
696 }
697 }
698 else
699 {
700 std::unique_ptr<INetURLObject> pLibInfoInetObj;
701 if( meInitMode == CONTAINER_INIT_FILE )
702 {
703 aFileName = aInitFileName;
704 }
705 else
706 {
707 if( nPass == 1 )
708 {
709 pLibInfoInetObj.reset(new INetURLObject( o3tl::getToken(maLibraryPath, 0, ';') ));
710 }
711 else
712 {
713 pLibInfoInetObj.reset(new INetURLObject( o3tl::getToken(maLibraryPath, 1, ';') ));
714 }
715 pLibInfoInetObj->insertName( maInfoFileName, false, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
716 pLibInfoInetObj->setExtension( u"xlc" );
717 aFileName = pLibInfoInetObj->GetMainURL( INetURLObject::DecodeMechanism::NONE );
718 }
719
720 try
721 {
722 xInput = mxSFI->openFileRead( aFileName );
723 }
724 catch(const Exception& )
725 {
726 // Silently tolerate empty or missing files
727 xInput.clear();
728 }
729
730 // Old variant?
731 if( !xInput.is() && nPass == 0 )
732 {
733 INetURLObject aLibInfoInetObj( o3tl::getToken(maLibraryPath, 1, ';') );
734 aLibInfoInetObj.insertName( maOldInfoFileName, false, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
735 aLibInfoInetObj.setExtension( u"xli" );
736 aFileName = aLibInfoInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
737
738 try
739 {
740 xInput = mxSFI->openFileRead( aFileName );
741 mbOldInfoFormat = true;
742 }
743 catch(const Exception& )
744 {
745 xInput.clear();
746 }
747 }
748 }
749
750 if( xInput.is() )
751 {
752 InputSource source;
753 source.aInputStream = xInput;
754 source.sSystemId = aFileName;
755
756 // start parsing
757 ::xmlscript::LibDescriptorArray aLibArray;
758
759 Reference< XParser > xParser = xml::sax::Parser::create(mxContext);
760 try
761 {
762 xParser->setDocumentHandler( ::xmlscript::importLibraryContainer( &aLibArray ) );
763 xParser->parseStream( source );
764 }
765 catch ( const xml::sax::SAXException& )
766 {
767 TOOLS_WARN_EXCEPTION( "basic", "" );
768 return;
769 }
770 catch ( const io::IOException& )
771 {
772 TOOLS_WARN_EXCEPTION( "basic", "" );
773 return;
774 }
775
776 sal_Int32 nLibCount = aLibArray.mnLibCount;
777 for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
778 {
779 ::xmlscript::LibDescriptor& rLib = aLibArray.mpLibs[i];
780
781 // Check storage URL
782 OUString aStorageURL = rLib.aStorageURL;
783 if( !bStorage && aStorageURL.isEmpty() && nPass == 0 )
784 {
785 OUString aLibraryPath;
786 if( meInitMode == CONTAINER_INIT_FILE )
787 {
788 aLibraryPath = maLibraryPath;
789 }
790 else
791 {
792 aLibraryPath = maLibraryPath.getToken(1, ';');
793 }
794 INetURLObject aInetObj( aLibraryPath );
795
796 aInetObj.insertName( rLib.aName, true, INetURLObject::LAST_SEGMENT,
797 INetURLObject::EncodeMechanism::All );
798 OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
799 if( mxSFI->isFolder( aLibDirPath ) )
800 {
801 createVariableURL( rLib.aStorageURL, rLib.aName, maInfoFileName, true );
802 maModifiable.setModified(true, guard);
803 }
804 else if( rLib.bLink )
805 {
806 // Check "share" path
807 INetURLObject aShareInetObj( o3tl::getToken(maLibraryPath, 0, ';') );
808 aShareInetObj.insertName( rLib.aName, true, INetURLObject::LAST_SEGMENT,
809 INetURLObject::EncodeMechanism::All );
810 OUString aShareLibDirPath = aShareInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
811 if( mxSFI->isFolder( aShareLibDirPath ) )
812 {
813 createVariableURL( rLib.aStorageURL, rLib.aName, maInfoFileName, false );
814 maModifiable.setModified(true, guard);
815 }
816 else
817 {
818 // #i25537: Ignore lib if library folder does not really exist
819 continue;
820 }
821 }
822 }
823
824 OUString aLibName = rLib.aName;
825
826 // If the same library name is used by the shared and the
827 // user lib container index files the user file wins
828 if( nPass == 1 && hasByName( aLibName ) )
829 {
830 continue;
831 }
832 SfxLibrary* pImplLib;
833 if( rLib.bLink )
834 {
835 Reference< XNameAccess > xLib =
836 createLibraryLink_Impl(aLibName, rLib.aStorageURL, rLib.bReadOnly, guard);
837 pImplLib = static_cast< SfxLibrary* >( xLib.get() );
838 }
839 else
840 {
841 Reference<XNameContainer> xLib = createLibrary_Impl(aLibName, guard);
842 pImplLib = static_cast< SfxLibrary* >( xLib.get() );
843 pImplLib->mbLoaded = false;
844 pImplLib->mbReadOnly = rLib.bReadOnly;
845 if( !bStorage )
846 {
847 checkStorageURL( rLib.aStorageURL, pImplLib->maLibInfoFileURL,
848 pImplLib->maStorageURL, pImplLib->maUnexpandedStorageURL );
849 }
850 }
851 maModifiable.setModified(false, guard);
852
853 // Read library info files
854 if( !mbOldInfoFormat )
855 {
856 uno::Reference< embed::XStorage > xLibraryStor;
857 if( !pImplLib->mbInitialised && bStorage )
858 {
859 try
860 {
861 xLibraryStor = xLibrariesStor->openStorageElement( rLib.aName,
862 embed::ElementModes::READ );
863 }
864 catch(const uno::Exception& )
865 {
866 #if OSL_DEBUG_LEVEL > 0
867 TOOLS_WARN_EXCEPTION(
868 "basic",
869 "couldn't open sub storage for library \"" << rLib.aName << "\"");
870 #endif
871 }
872 }
873
874 // Link is already initialised in createLibraryLink_Impl()
875 if( !pImplLib->mbInitialised && (!bStorage || xLibraryStor.is()) )
876 {
877 bool bLoaded = implLoadLibraryIndexFile( pImplLib, rLib, xLibraryStor, OUString(), guard );
878 SAL_WARN_IF(
879 bLoaded && aLibName != rLib.aName, "basic",
880 ("Different library names in library container and"
881 " library info files!"));
882 if( GbMigrationSuppressErrors && !bLoaded )
883 {
884 removeLibrary( aLibName );
885 }
886 }
887 }
888 else if( !bStorage )
889 {
890 // Write new index file immediately because otherwise
891 // the library elements will be lost when storing into
892 // the new info format
893 uno::Reference< embed::XStorage > xTmpStorage;
894 implStoreLibraryIndexFile( pImplLib, rLib, xTmpStorage );
895 }
896
897 implImportLibDescriptor(pImplLib, rLib, guard);
898
899 if( nPass == 1 )
900 {
901 pImplLib->mbSharedIndexFile = true;
902 pImplLib->mbReadOnly = true;
903 }
904 }
905
906 // Keep flag for documents to force writing the new index files
907 if( !bStorage )
908 {
909 mbOldInfoFormat = false;
910 }
911 }
912 }
913
914 // #110009: END Scope to force the StorageRefs to be destructed
915 }
916
917 if( !bStorage && meInitMode == DEFAULT )
918 {
919 try
920 {
921 implScanExtensions(guard);
922 }
923 catch(const uno::Exception& )
924 {
925 // TODO: error handling?
926 SAL_WARN("basic", "Cannot access extensions!");
927 }
928 }
929
930 // Preload?
931 {
932 for (auto& aName : maNameContainer.getElementNames())
933 {
934 SfxLibrary* pImplLib = getImplLib( aName );
935 if( pImplLib->mbPreload )
936 {
937 loadLibrary_Impl(aName, guard);
938 }
939 }
940 }
941
942 if( meInitMode != DEFAULT )
943 return;
944
945 // tdf#121740 speed up loading documents with lots of embedded documents by avoid the UCB work of updating non-existent VBA libraries
946 if (rInitialDocumentURL.isEmpty())
947 return;
948
949 INetURLObject aUserBasicInetObj( o3tl::getToken(maLibraryPath, 1, ';') );
950 OUString aStandardStr(u"Standard"_ustr);
951
952 INetURLObject aPrevUserBasicInetObj_1( aUserBasicInetObj );
953 aPrevUserBasicInetObj_1.removeSegment();
954 INetURLObject aPrevUserBasicInetObj_2 = aPrevUserBasicInetObj_1;
955 aPrevUserBasicInetObj_1.Append( u"__basic_80" );
956 aPrevUserBasicInetObj_2.Append( u"__basic_80_2" );
957
958 // #i93163
959 bool bCleanUp = false;
960 try
961 {
962 INetURLObject aPrevUserBasicInetObj = aPrevUserBasicInetObj_1;
963 OUString aPrevFolder = aPrevUserBasicInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
964 if( mxSFI->isFolder( aPrevFolder ) )
965 {
966 // Check if Standard folder exists and is complete
967 INetURLObject aUserBasicStandardInetObj( aUserBasicInetObj );
968 aUserBasicStandardInetObj.insertName( aStandardStr, true, INetURLObject::LAST_SEGMENT,
969 INetURLObject::EncodeMechanism::All );
970 INetURLObject aPrevUserBasicStandardInetObj( aPrevUserBasicInetObj );
971 aPrevUserBasicStandardInetObj.insertName( aStandardStr, true, INetURLObject::LAST_SEGMENT,
972 INetURLObject::EncodeMechanism::All );
973 OUString aPrevStandardFolder = aPrevUserBasicStandardInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
974 if( mxSFI->isFolder( aPrevStandardFolder ) )
975 {
976 OUString aXlbExtension( u"xlb"_ustr );
977 OUString aCheckFileName;
978
979 // Check if script.xlb exists
980 aCheckFileName = "script";
981 checkAndCopyFileImpl( aUserBasicStandardInetObj,
982 aPrevUserBasicStandardInetObj,
983 aCheckFileName, aXlbExtension, mxSFI );
984
985 // Check if dialog.xlb exists
986 aCheckFileName = "dialog";
987 checkAndCopyFileImpl( aUserBasicStandardInetObj,
988 aPrevUserBasicStandardInetObj,
989 aCheckFileName, aXlbExtension, mxSFI );
990
991 // Check if module1.xba exists
992 aCheckFileName = "Module1";
993 checkAndCopyFileImpl( aUserBasicStandardInetObj,
994 aPrevUserBasicStandardInetObj,
995 aCheckFileName, u"xba", mxSFI );
996 }
997 else
998 {
999 OUString aStandardFolder = aUserBasicStandardInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1000 mxSFI->copy( aStandardFolder, aPrevStandardFolder );
1001 }
1002
1003 OUString aPrevCopyToFolder = aPrevUserBasicInetObj_2.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1004 mxSFI->copy( aPrevFolder, aPrevCopyToFolder );
1005 }
1006 else
1007 {
1008 aPrevUserBasicInetObj = aPrevUserBasicInetObj_2;
1009 aPrevFolder = aPrevUserBasicInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1010 }
1011 if( mxSFI->isFolder( aPrevFolder ) )
1012 {
1013 rtl::Reference<SfxLibraryContainer> pPrevCont = createInstanceImpl();
1014
1015 // Rename previous basic folder to make storage URLs correct during initialisation
1016 OUString aFolderUserBasic = aUserBasicInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1017 INetURLObject aUserBasicTmpInetObj( aUserBasicInetObj );
1018 aUserBasicTmpInetObj.removeSegment();
1019 aUserBasicTmpInetObj.Append( u"__basic_tmp" );
1020 OUString aFolderTmp = aUserBasicTmpInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1021
1022 mxSFI->move( aFolderUserBasic, aFolderTmp );
1023 try
1024 {
1025 mxSFI->move( aPrevFolder, aFolderUserBasic );
1026 }
1027 catch(const Exception& )
1028 {
1029 // Move back user/basic folder
1030 try
1031 {
1032 mxSFI->kill( aFolderUserBasic );
1033 }
1034 catch(const Exception& )
1035 {}
1036 mxSFI->move( aFolderTmp, aFolderUserBasic );
1037 throw;
1038 }
1039
1040 INetURLObject aPrevUserBasicLibInfoInetObj( aUserBasicInetObj );
1041 aPrevUserBasicLibInfoInetObj.insertName( maInfoFileName, false, INetURLObject::LAST_SEGMENT,
1042 INetURLObject::EncodeMechanism::All );
1043 aPrevUserBasicLibInfoInetObj.setExtension( u"xlc");
1044 OUString aLibInfoFileName = aPrevUserBasicLibInfoInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1045 Sequence<Any> aInitSeq( 1 );
1046 aInitSeq.getArray()[0] <<= aLibInfoFileName;
1047 GbMigrationSuppressErrors = true;
1048 pPrevCont->initialize( aInitSeq );
1049 GbMigrationSuppressErrors = false;
1050
1051 // Rename folders back
1052 mxSFI->move( aFolderUserBasic, aPrevFolder );
1053 mxSFI->move( aFolderTmp, aFolderUserBasic );
1054
1055 for (auto& aLibName : pPrevCont->getElementNames())
1056 {
1057 if( hasByName( aLibName ) )
1058 {
1059 if( aLibName == aStandardStr )
1060 {
1061 SfxLibrary* pImplLib = getImplLib( aStandardStr );
1062 OUString aStandardFolder = pImplLib->maStorageURL;
1063 mxSFI->kill( aStandardFolder );
1064 }
1065 else
1066 {
1067 continue;
1068 }
1069 }
1070
1071 SfxLibrary* pImplLib = pPrevCont->getImplLib( aLibName );
1072 if( pImplLib->mbLink )
1073 {
1074 OUString aStorageURL = pImplLib->maUnexpandedStorageURL;
1075 bool bCreateLink = true;
1076 if( aStorageURL.indexOf( "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE" ) != -1 ||
1077 aStorageURL.indexOf( "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE" ) != -1 ||
1078 aStorageURL.indexOf( "vnd.sun.star.expand:$BUNDLED_EXTENSIONS" ) != -1 ||
1079 aStorageURL.indexOf( "$(INST)" ) != -1 )
1080 {
1081 bCreateLink = false;
1082 }
1083 if( bCreateLink )
1084 {
1085 createLibraryLink_Impl( aLibName, pImplLib->maStorageURL, pImplLib->mbReadOnly, guard );
1086 }
1087 }
1088 else
1089 {
1090 // Move folder if not already done
1091 INetURLObject aUserBasicLibFolderInetObj( aUserBasicInetObj );
1092 aUserBasicLibFolderInetObj.Append( aLibName );
1093 OUString aLibFolder = aUserBasicLibFolderInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1094
1095 INetURLObject aPrevUserBasicLibFolderInetObj( aPrevUserBasicInetObj );
1096 aPrevUserBasicLibFolderInetObj.Append( aLibName );
1097 OUString aPrevLibFolder = aPrevUserBasicLibFolderInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1098
1099 if( mxSFI->isFolder( aPrevLibFolder ) && !mxSFI->isFolder( aLibFolder ) )
1100 {
1101 mxSFI->move( aPrevLibFolder, aLibFolder );
1102 }
1103
1104 if( aLibName == aStandardStr )
1105 {
1106 maNameContainer.removeByName(aLibName, guard);
1107 }
1108
1109 // Create library
1110 Reference<XNameContainer> xLib = createLibrary_Impl(aLibName, guard);
1111 SfxLibrary* pNewLib = static_cast< SfxLibrary* >( xLib.get() );
1112 pNewLib->mbLoaded = false;
1113 pNewLib->implSetModified(false, guard);
1114 checkStorageURL( aLibFolder, pNewLib->maLibInfoFileURL,
1115 pNewLib->maStorageURL, pNewLib->maUnexpandedStorageURL );
1116
1117 uno::Reference< embed::XStorage > xDummyStor;
1118 ::xmlscript::LibDescriptor aLibDesc;
1119 implLoadLibraryIndexFile( pNewLib, aLibDesc, xDummyStor, pNewLib->maLibInfoFileURL, guard );
1120 implImportLibDescriptor(pNewLib, aLibDesc, guard);
1121 }
1122 }
1123 mxSFI->kill( aPrevFolder );
1124 }
1125 }
1126 catch(const Exception&)
1127 {
1128 TOOLS_WARN_EXCEPTION("basic", "Upgrade of Basic installation failed somehow" );
1129 bCleanUp = true;
1130 }
1131
1132 // #i93163
1133 if( !bCleanUp )
1134 return;
1135
1136 INetURLObject aPrevUserBasicInetObj_Err(std::move(aUserBasicInetObj));
1137 aPrevUserBasicInetObj_Err.removeSegment();
1138 aPrevUserBasicInetObj_Err.Append( u"__basic_80_err" );
1139 OUString aPrevFolder_Err = aPrevUserBasicInetObj_Err.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1140
1141 bool bSaved = false;
1142 try
1143 {
1144 OUString aPrevFolder_1 = aPrevUserBasicInetObj_1.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1145 if( mxSFI->isFolder( aPrevFolder_1 ) )
1146 {
1147 mxSFI->move( aPrevFolder_1, aPrevFolder_Err );
1148 bSaved = true;
1149 }
1150 }
1151 catch(const Exception& )
1152 {}
1153 try
1154 {
1155 OUString aPrevFolder_2 = aPrevUserBasicInetObj_2.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1156 if( !bSaved && mxSFI->isFolder( aPrevFolder_2 ) )
1157 {
1158 mxSFI->move( aPrevFolder_2, aPrevFolder_Err );
1159 }
1160 else
1161 {
1162 mxSFI->kill( aPrevFolder_2 );
1163 }
1164 }
1165 catch(const Exception& )
1166 {}
1167 }
1168
1169 void SfxLibraryContainer::implScanExtensions(std::unique_lock<std::mutex>& guard)
1170 {
1171 #if HAVE_FEATURE_EXTENSIONS
1172 ScriptExtensionIterator aScriptIt;
1173
1174 bool bPureDialogLib = false;
1175 for (;;)
1176 {
1177 OUString aLibURL = aScriptIt.nextBasicOrDialogLibrary( bPureDialogLib );
1178 if (aLibURL.isEmpty())
1179 break;
1180 if( bPureDialogLib && maInfoFileName == "script" )
1181 {
1182 continue;
1183 }
1184 // Extract lib name
1185 sal_Int32 nLen = aLibURL.getLength();
1186 sal_Int32 indexLastSlash = aLibURL.lastIndexOf( '/' );
1187 sal_Int32 nReduceCopy = 0;
1188 if( indexLastSlash == nLen - 1 )
1189 {
1190 nReduceCopy = 1;
1191 indexLastSlash = aLibURL.lastIndexOf( '/', nLen - 1 );
1192 }
1193
1194 OUString aLibName = aLibURL.copy( indexLastSlash + 1, nLen - indexLastSlash - nReduceCopy - 1 );
1195
1196 // If a library of the same exists the existing library wins
1197 if( hasByName( aLibName ) )
1198 {
1199 continue;
1200 }
1201 // Add index file to URL
1202 OUString aIndexFileURL = aLibURL;
1203 if( nReduceCopy == 0 )
1204 {
1205 aIndexFileURL += "/";
1206 }
1207 aIndexFileURL += maInfoFileName + ".xlb";
1208
1209 // Create link
1210 const bool bReadOnly = false;
1211 createLibraryLink_Impl(aLibName, aIndexFileURL, bReadOnly, guard);
1212 }
1213 #else
1214 (void)guard;
1215 #endif
1216 }
1217
1218 // Handle maLibInfoFileURL and maStorageURL correctly
1219 void SfxLibraryContainer::checkStorageURL( const OUString& aSourceURL,
1220 OUString& aLibInfoFileURL, OUString& aStorageURL,
1221 OUString& aUnexpandedStorageURL )
1222 {
1223 OUString aExpandedSourceURL = expand_url( aSourceURL );
1224 if( aExpandedSourceURL != aSourceURL )
1225 {
1226 aUnexpandedStorageURL = aSourceURL;
1227 }
1228 else
1229 {
1230 // try to re-create the variable URL: helps moving the profile
1231 if (std::u16string_view aRest; aSourceURL.startsWith(expand_url(sUserBasicVariablePrefix), &aRest))
1232 aUnexpandedStorageURL = sUserBasicVariablePrefix + aRest;
1233 else if (aSourceURL.startsWith(expand_url(sInstBasicVariablePrefix), &aRest))
1234 aUnexpandedStorageURL = sInstBasicVariablePrefix + aRest;
1235 else
1236 aUnexpandedStorageURL.clear(); // This will use eventual value of aLibInfoFileURL
1237 }
1238
1239 INetURLObject aInetObj( aExpandedSourceURL );
1240 OUString aExtension = aInetObj.getExtension();
1241 if( aExtension == "xlb" )
1242 {
1243 // URL to xlb file
1244 aLibInfoFileURL = aExpandedSourceURL;
1245 aInetObj.removeSegment();
1246 aStorageURL = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1247 }
1248 else
1249 {
1250 // URL to library folder
1251 aStorageURL = aExpandedSourceURL;
1252 aInetObj.insertName( maInfoFileName, false, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
1253 aInetObj.setExtension( u"xlb" );
1254 aLibInfoFileURL = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1255 }
1256 }
1257
1258 SfxLibrary* SfxLibraryContainer::getImplLib( const OUString& rLibraryName )
1259 {
1260 auto xNameAccess = maNameContainer.getByName(rLibraryName).query<XNameAccess>();
1261 return static_cast<SfxLibrary*>(xNameAccess.get());
1262 }
1263
1264
1265 // Storing with password encryption
1266
1267 // Empty implementation, avoids unnecessary implementation in dlgcont.cxx
1268 bool SfxLibraryContainer::implStorePasswordLibrary( SfxLibrary*,
1269 const OUString&,
1270 const uno::Reference< embed::XStorage >&,
1271 const uno::Reference< task::XInteractionHandler >& )
1272 {
1273 return false;
1274 }
1275
1276 bool SfxLibraryContainer::implStorePasswordLibrary(
1277 SfxLibrary* /*pLib*/,
1278 const OUString& /*aName*/,
1279 const css::uno::Reference< css::embed::XStorage >& /*xStorage*/,
1280 const OUString& /*aTargetURL*/,
1281 const Reference< XSimpleFileAccess3 >& /*xToUseSFI*/,
1282 const uno::Reference< task::XInteractionHandler >& )
1283 {
1284 return false;
1285 }
1286
1287 bool SfxLibraryContainer::implLoadPasswordLibrary(
1288 SfxLibrary* /*pLib*/,
1289 const OUString& /*Name*/,
1290 bool /*bVerifyPasswordOnly*/,
1291 std::unique_lock<std::mutex>& /*guard*/ )
1292 {
1293 return true;
1294 }
1295
1296 OUString SfxLibraryContainer::createAppLibraryFolder( SfxLibrary* pLib, std::u16string_view aName )
1297 {
1298 OUString aLibDirPath = pLib->maStorageURL;
1299 if( aLibDirPath.isEmpty() )
1300 {
1301 INetURLObject aInetObj( o3tl::getToken(maLibraryPath, 1, ';') );
1302 aInetObj.insertName( aName, true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
1303 checkStorageURL( aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), pLib->maLibInfoFileURL,
1304 pLib->maStorageURL, pLib->maUnexpandedStorageURL );
1305 aLibDirPath = pLib->maStorageURL;
1306 }
1307
1308 if( !mxSFI->isFolder( aLibDirPath ) )
1309 {
1310 try
1311 {
1312 mxSFI->createFolder( aLibDirPath );
1313 }
1314 catch(const Exception& )
1315 {}
1316 }
1317
1318 return aLibDirPath;
1319 }
1320
1321 // Storing
1322 void SfxLibraryContainer::implStoreLibrary( SfxLibrary* pLib,
1323 std::u16string_view aName,
1324 const uno::Reference< embed::XStorage >& xStorage )
1325 {
1326 Reference< XSimpleFileAccess3 > xDummySFA;
1327 Reference< XInteractionHandler > xDummyHandler;
1328 implStoreLibrary( pLib, aName, xStorage, u"", xDummySFA, xDummyHandler );
1329 }
1330
1331 // New variant for library export
1332 void SfxLibraryContainer::implStoreLibrary( SfxLibrary* pLib,
1333 std::u16string_view aName,
1334 const uno::Reference< embed::XStorage >& xStorage,
1335 std::u16string_view aTargetURL,
1336 const Reference< XSimpleFileAccess3 >& rToUseSFI,
1337 const Reference< XInteractionHandler >& xHandler )
1338 {
1339 bool bLink = pLib->mbLink;
1340 bool bStorage = xStorage.is() && !bLink;
1341
1342 if( bStorage )
1343 {
1344 for (auto& aElementName : pLib->getElementNames())
1345 {
1346 OUString aStreamName = aElementName + ".xml";
1347
1348 if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
1349 {
1350 SAL_WARN(
1351 "basic",
1352 "invalid library element \"" << aElementName << '"');
1353 continue;
1354 }
1355 try
1356 {
1357 uno::Reference< io::XStream > xElementStream = xStorage->openStreamElement(
1358 aStreamName,
1359 embed::ElementModes::READWRITE );
1360 // throw uno::RuntimeException(); // TODO: method must either return the stream or throw an exception
1361
1362 uno::Reference< beans::XPropertySet > xProps( xElementStream, uno::UNO_QUERY );
1363 SAL_WARN_IF(
1364 !xProps.is(), "basic",
1365 "The StorageStream must implement XPropertySet interface!");
1366 //if ( !xProps.is() ) //TODO
1367
1368 if ( xProps.is() )
1369 {
1370 xProps->setPropertyValue(u"MediaType"_ustr, uno::Any( u"text/xml"_ustr ) );
1371
1372 // #87671 Allow encryption
1373 xProps->setPropertyValue(u"UseCommonStoragePasswordEncryption"_ustr, uno::Any( true ) );
1374
1375 Reference< XOutputStream > xOutput = xElementStream->getOutputStream();
1376 Reference< XNameContainer > xLib( pLib );
1377 writeLibraryElement( xLib, aElementName, xOutput );
1378 }
1379 }
1380 catch(const uno::Exception& )
1381 {
1382 SAL_WARN("basic", "Problem during storing of library!");
1383 // TODO: error handling?
1384 }
1385 }
1386 pLib->storeResourcesToStorage( xStorage );
1387 }
1388 else
1389 {
1390 // Export?
1391 bool bExport = !aTargetURL.empty();
1392 try
1393 {
1394 Reference< XSimpleFileAccess3 > xSFI = mxSFI;
1395 if( rToUseSFI.is() )
1396 {
1397 xSFI = rToUseSFI;
1398 }
1399 OUString aLibDirPath;
1400 if( bExport )
1401 {
1402 INetURLObject aInetObj( aTargetURL );
1403 aInetObj.insertName( aName, true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
1404 aLibDirPath = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1405
1406 if( !xSFI->isFolder( aLibDirPath ) )
1407 {
1408 xSFI->createFolder( aLibDirPath );
1409 }
1410 pLib->storeResourcesToURL( aLibDirPath, xHandler );
1411 }
1412 else
1413 {
1414 aLibDirPath = createAppLibraryFolder( pLib, aName );
1415 pLib->storeResources();
1416 }
1417
1418 for (auto& aElementName : pLib->getElementNames())
1419 {
1420 INetURLObject aElementInetObj( aLibDirPath );
1421 aElementInetObj.insertName( aElementName, false,
1422 INetURLObject::LAST_SEGMENT,
1423 INetURLObject::EncodeMechanism::All );
1424 aElementInetObj.setExtension( maLibElementFileExtension );
1425 OUString aElementPath( aElementInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
1426
1427 if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
1428 {
1429 SAL_WARN(
1430 "basic",
1431 "invalid library element \"" << aElementName << '"');
1432 continue;
1433 }
1434
1435 // TODO: Check modified
1436 try
1437 {
1438 if( xSFI->exists( aElementPath ) )
1439 {
1440 xSFI->kill( aElementPath );
1441 }
1442 Reference< XOutputStream > xOutput = xSFI->openFileWrite( aElementPath );
1443 Reference< XNameContainer > xLib( pLib );
1444 writeLibraryElement( xLib, aElementName, xOutput );
1445 xOutput->closeOutput();
1446 }
1447 catch(const Exception& )
1448 {
1449 if( bExport )
1450 {
1451 throw;
1452 }
1453 SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aElementPath );
1454 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
1455 }
1456 }
1457 }
1458 catch(const Exception& )
1459 {
1460 if( bExport )
1461 {
1462 throw;
1463 }
1464 }
1465 }
1466 }
1467
1468 void SfxLibraryContainer::implStoreLibraryIndexFile( SfxLibrary* pLib,
1469 const ::xmlscript::LibDescriptor& rLib,
1470 const uno::Reference< embed::XStorage >& xStorage )
1471 {
1472 Reference< XSimpleFileAccess3 > xDummySFA;
1473 implStoreLibraryIndexFile( pLib, rLib, xStorage, u"", xDummySFA );
1474 }
1475
1476 // New variant for library export
1477 void SfxLibraryContainer::implStoreLibraryIndexFile( SfxLibrary* pLib,
1478 const ::xmlscript::LibDescriptor& rLib,
1479 const uno::Reference< embed::XStorage >& xStorage,
1480 std::u16string_view aTargetURL,
1481 const Reference< XSimpleFileAccess3 >& rToUseSFI )
1482 {
1483 // Create sax writer
1484 Reference< XWriter > xWriter = xml::sax::Writer::create(mxContext);
1485
1486 bool bLink = pLib->mbLink;
1487 bool bStorage = xStorage.is() && !bLink;
1488
1489 // Write info file
1490 uno::Reference< io::XOutputStream > xOut;
1491 uno::Reference< io::XStream > xInfoStream;
1492 if( bStorage )
1493 {
1494 OUString aStreamName = maInfoFileName + "-lb.xml";
1495
1496 try
1497 {
1498 xInfoStream = xStorage->openStreamElement( aStreamName, embed::ElementModes::READWRITE );
1499 SAL_WARN_IF(!xInfoStream.is(), "basic", "No stream!");
1500 uno::Reference< beans::XPropertySet > xProps( xInfoStream, uno::UNO_QUERY );
1501 // throw uno::RuntimeException(); // TODO
1502
1503 if ( xProps.is() )
1504 {
1505 xProps->setPropertyValue(u"MediaType"_ustr, uno::Any( u"text/xml"_ustr ) );
1506
1507 // #87671 Allow encryption
1508 xProps->setPropertyValue(u"UseCommonStoragePasswordEncryption"_ustr, uno::Any( true ) );
1509
1510 xOut = xInfoStream->getOutputStream();
1511 }
1512 }
1513 catch(const uno::Exception& )
1514 {
1515 SAL_WARN("basic", "Problem during storing of library index file!");
1516 // TODO: error handling?
1517 }
1518 }
1519 else
1520 {
1521 // Export?
1522 bool bExport = !aTargetURL.empty();
1523 Reference< XSimpleFileAccess3 > xSFI = mxSFI;
1524 if( rToUseSFI.is() )
1525 {
1526 xSFI = rToUseSFI;
1527 }
1528 OUString aLibInfoPath;
1529 if( bExport )
1530 {
1531 INetURLObject aInetObj( aTargetURL );
1532 aInetObj.insertName( rLib.aName, true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
1533 OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1534 if( !xSFI->isFolder( aLibDirPath ) )
1535 {
1536 xSFI->createFolder( aLibDirPath );
1537 }
1538 aInetObj.insertName( maInfoFileName, false, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
1539 aInetObj.setExtension( u"xlb" );
1540 aLibInfoPath = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
1541 }
1542 else
1543 {
1544 createAppLibraryFolder( pLib, rLib.aName );
1545 aLibInfoPath = pLib->maLibInfoFileURL;
1546 }
1547
1548 try
1549 {
1550 if( xSFI->exists( aLibInfoPath ) )
1551 {
1552 xSFI->kill( aLibInfoPath );
1553 }
1554 xOut = xSFI->openFileWrite( aLibInfoPath );
1555 }
1556 catch(const Exception& )
1557 {
1558 if( bExport )
1559 {
1560 throw;
1561 }
1562 SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aLibInfoPath );
1563 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
1564 }
1565 }
1566 if( !xOut.is() )
1567 {
1568 SAL_WARN("basic", "couldn't open output stream");
1569 return;
1570 }
1571 xWriter->setOutputStream( xOut );
1572 xmlscript::exportLibrary( xWriter, rLib );
1573 }
1574
1575
1576 bool SfxLibraryContainer::implLoadLibraryIndexFile( SfxLibrary* pLib,
1577 ::xmlscript::LibDescriptor& rLib,
1578 const uno::Reference< embed::XStorage >& xStorage,
1579 const OUString& aIndexFileName,
1580 std::unique_lock<std::mutex>& guard )
1581 {
1582 Reference< XParser > xParser = xml::sax::Parser::create(mxContext);
1583
1584 bool bStorage = false;
1585 if( pLib )
1586 {
1587 bool bLink = pLib->mbLink;
1588 bStorage = xStorage.is() && !bLink;
1589 }
1590
1591 // Read info file
1592 uno::Reference< io::XInputStream > xInput;
1593 OUString aLibInfoPath;
1594 if( bStorage )
1595 {
1596 aLibInfoPath = maInfoFileName + "-lb.xml";
1597
1598 try
1599 {
1600 uno::Reference< io::XStream > xInfoStream =
1601 xStorage->openStreamElement( aLibInfoPath, embed::ElementModes::READ );
1602 xInput = xInfoStream->getInputStream();
1603 }
1604 catch(const uno::Exception& )
1605 {}
1606 }
1607 else
1608 {
1609 // Create Input stream
1610 //String aLibInfoPath; // attention: THIS PROBLEM MUST BE REVIEWED BY SCRIPTING OWNER!!!
1611
1612 if( pLib )
1613 {
1614 createAppLibraryFolder( pLib, rLib.aName );
1615 aLibInfoPath = pLib->maLibInfoFileURL;
1616 }
1617 else
1618 {
1619 aLibInfoPath = aIndexFileName;
1620 }
1621 try
1622 {
1623 xInput = mxSFI->openFileRead( aLibInfoPath );
1624 }
1625 catch(const Exception& )
1626 {
1627 xInput.clear();
1628 if( !GbMigrationSuppressErrors )
1629 {
1630 SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aLibInfoPath );
1631 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
1632 }
1633 }
1634 }
1635 if( !xInput.is() )
1636 {
1637 return false;
1638 }
1639
1640 InputSource source;
1641 source.aInputStream = std::move(xInput);
1642 source.sSystemId = aLibInfoPath;
1643
1644 // start parsing
1645 try
1646 {
1647 xParser->setDocumentHandler( ::xmlscript::importLibrary( rLib ) );
1648 xParser->parseStream( source );
1649 }
1650 catch(const Exception& )
1651 {
1652 SAL_WARN("basic", "Parsing error");
1653 SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aLibInfoPath );
1654 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
1655 return false;
1656 }
1657
1658 if( !pLib )
1659 {
1660 Reference<XNameContainer> xLib = createLibrary_Impl(rLib.aName, guard);
1661 pLib = static_cast< SfxLibrary* >( xLib.get() );
1662 pLib->mbLoaded = false;
1663 rLib.aStorageURL = aIndexFileName;
1664 checkStorageURL( rLib.aStorageURL, pLib->maLibInfoFileURL, pLib->maStorageURL,
1665 pLib->maUnexpandedStorageURL );
1666
1667 implImportLibDescriptor(pLib, rLib, guard);
1668 }
1669
1670 return true;
1671 }
1672
1673 void SfxLibraryContainer::implImportLibDescriptor( SfxLibrary* pLib,
1674 ::xmlscript::LibDescriptor const& rLib,
1675 std::unique_lock<std::mutex>& guard)
1676 {
1677 if( pLib->mbInitialised )
1678 return;
1679 Any aDummyElement = createEmptyLibraryElement();
1680 for (auto& name : rLib.aElementNames)
1681 {
1682 pLib->maNameContainer.insertByName(name, aDummyElement, guard);
1683 }
1684 pLib->mbPasswordProtected = rLib.bPasswordProtected;
1685 pLib->mbReadOnly = rLib.bReadOnly;
1686 pLib->mbPreload = rLib.bPreload;
1687 pLib->implSetModified(false, guard);
1688 pLib->mbInitialised = true;
1689 }
1690
1691
1692 // Methods of new XLibraryStorage interface?
1693 void SfxLibraryContainer::storeLibraries_Impl( const uno::Reference< embed::XStorage >& i_rStorage,
1694 bool bComplete, std::unique_lock<std::mutex>& guard )
1695 {
1696 const Sequence< OUString > aNames = maNameContainer.getElementNames();
1697
1698 // Don't count libs from shared index file
1699 sal_Int32 nLibsToSave
1700 = std::count_if(aNames.begin(), aNames.end(),
1701 [this](const OUString& name) {
1702 SfxLibrary* pImplLib = getImplLib(name);
1703 return !pImplLib->mbSharedIndexFile && !pImplLib->mbExtension;
1704 });
1705 // Write to storage?
1706 bool bStorage = i_rStorage.is();
1707 uno::Reference< embed::XStorage > xSourceLibrariesStor;
1708 uno::Reference< embed::XStorage > xTargetLibrariesStor;
1709 OUString sTempTargetStorName;
1710 const bool bInplaceStorage = bStorage && ( i_rStorage == mxStorage );
1711
1712 if( nLibsToSave == 0 )
1713 {
1714 if ( bInplaceStorage && mxStorage->hasByName(maLibrariesDir) )
1715 {
1716 mxStorage->removeElement(maLibrariesDir);
1717 }
1718 return;
1719 }
1720
1721 if ( bStorage )
1722 {
1723 // Don't write if only empty standard lib exists
1724 if ( ( nLibsToSave == 1 ) && ( aNames[0] == "Standard" ) )
1725 {
1726 if (!getImplLib(aNames[0])->hasElements())
1727 {
1728 if ( bInplaceStorage && mxStorage->hasByName(maLibrariesDir) )
1729 {
1730 mxStorage->removeElement(maLibrariesDir);
1731 }
1732 return;
1733 }
1734 }
1735
1736 // create the empty target storage
1737 try
1738 {
1739 OUString sTargetLibrariesStoreName;
1740 if ( bInplaceStorage )
1741 {
1742 // create a temporary target storage
1743 const OUString aTempTargetNameBase = maLibrariesDir + "_temp_";
1744 sal_Int32 index = 0;
1745 do
1746 {
1747 sTargetLibrariesStoreName = aTempTargetNameBase + OUString::number( index++ );
1748 if ( !i_rStorage->hasByName( sTargetLibrariesStoreName ) )
1749 {
1750 break;
1751 }
1752 }
1753 while ( true );
1754 sTempTargetStorName = sTargetLibrariesStoreName;
1755 }
1756 else
1757 {
1758 sTargetLibrariesStoreName = maLibrariesDir;
1759 if ( i_rStorage->hasByName( sTargetLibrariesStoreName ) )
1760 {
1761 i_rStorage->removeElement( sTargetLibrariesStoreName );
1762 }
1763 }
1764
1765 xTargetLibrariesStor.set( i_rStorage->openStorageElement( sTargetLibrariesStoreName, embed::ElementModes::READWRITE ), UNO_SET_THROW );
1766 }
1767 catch( const uno::Exception& )
1768 {
1769 DBG_UNHANDLED_EXCEPTION("basic");
1770 return;
1771 }
1772
1773 // open the source storage which might be used to copy yet-unmodified libraries
1774 try
1775 {
1776 if ( mxStorage->hasByName( maLibrariesDir ) || bInplaceStorage )
1777 {
1778 xSourceLibrariesStor = mxStorage->openStorageElement( maLibrariesDir,
1779 bInplaceStorage ? embed::ElementModes::READWRITE : embed::ElementModes::READ );
1780 }
1781 }
1782 catch( const uno::Exception& )
1783 {
1784 DBG_UNHANDLED_EXCEPTION("basic");
1785 return;
1786 }
1787 }
1788
1789 int iArray = 0;
1790 ::xmlscript::LibDescriptor aLibDescriptorForExtensionLibs;
1791 ::xmlscript::LibDescriptorArray aLibArray( nLibsToSave );
1792 for (auto& name : aNames)
1793 {
1794 SfxLibrary* pImplLib = getImplLib(name);
1795 if( pImplLib->mbSharedIndexFile )
1796 {
1797 continue;
1798 }
1799 const bool bExtensionLib = pImplLib->mbExtension;
1800 ::xmlscript::LibDescriptor& rLib = bExtensionLib ?
1801 aLibDescriptorForExtensionLibs : aLibArray.mpLibs[iArray];
1802 if( !bExtensionLib )
1803 {
1804 iArray++;
1805 }
1806 rLib.aName = name;
1807
1808 rLib.bLink = pImplLib->mbLink;
1809 if( !bStorage || pImplLib->mbLink )
1810 {
1811 rLib.aStorageURL = ( pImplLib->maUnexpandedStorageURL.getLength() ) ?
1812 pImplLib->maUnexpandedStorageURL : pImplLib->maLibInfoFileURL;
1813 }
1814 rLib.bReadOnly = pImplLib->mbReadOnly;
1815 rLib.bPreload = pImplLib->mbPreload;
1816 rLib.bPasswordProtected = pImplLib->mbPasswordProtected;
1817 rLib.aElementNames = pImplLib->getElementNames();
1818
1819 if( pImplLib->implIsModified() || bComplete )
1820 {
1821 // Testing pImplLib->implIsModified() is not reliable,
1822 // IMHO the value of pImplLib->implIsModified() should
1823 // reflect whether the library ( in-memory ) model
1824 // is in sync with the library container's own storage. Currently
1825 // whenever the library model is written to *any* storage
1826 // pImplLib->implSetModified( sal_False ) is called
1827 // The way the code works, especially the way that sfx uses
1828 // temp storage when saving ( and later sets the root storage of the
1829 // library container ) and similar madness in dbaccess means some surgery
1830 // is required to make it possible to successfully use this optimisation
1831 // It would be possible to do the implSetModified() call below only
1832 // conditionally, but that would require an additional boolean to be
1833 // passed in via the XStorageBasedDocument::storeLibrariesToStorage()...
1834 // fdo#68983: If there's a password and the password is not known, only
1835 // copying the storage works!
1836 // Can we simply copy the storage?
1837 bool isCopyStorage = !mbOldInfoFormat && !mbOasis2OOoFormat
1838 && !pImplLib->isLoadedStorable()
1839 && xSourceLibrariesStor.is() /* null for user profile */;
1840 if (isCopyStorage)
1841 {
1842 try
1843 {
1844 (void)xSourceLibrariesStor->isStorageElement(rLib.aName);
1845 }
1846 catch (container::NoSuchElementException const&)
1847 {
1848 isCopyStorage = false;
1849 }
1850 }
1851 if (isCopyStorage)
1852 {
1853 try
1854 {
1855 xSourceLibrariesStor->copyElementTo( rLib.aName, xTargetLibrariesStor, rLib.aName );
1856 }
1857 catch( const uno::Exception& )
1858 {
1859 DBG_UNHANDLED_EXCEPTION("basic");
1860 // TODO: error handling?
1861 }
1862 }
1863 else
1864 {
1865 uno::Reference< embed::XStorage > xLibraryStor;
1866 if( bStorage )
1867 {
1868 #if OSL_DEBUG_LEVEL > 0
1869 try
1870 {
1871 #endif
1872 xLibraryStor = xTargetLibrariesStor->openStorageElement(
1873 rLib.aName,
1874 embed::ElementModes::READWRITE );
1875 #if OSL_DEBUG_LEVEL > 0
1876 }
1877 catch(const uno::Exception& )
1878 {
1879 TOOLS_WARN_EXCEPTION(
1880 "basic",
1881 "couldn't create sub storage for library \"" << rLib.aName << "\"");
1882 throw;
1883 }
1884 #endif
1885 }
1886
1887 // Maybe lib is not loaded?!
1888 if( bComplete )
1889 {
1890 loadLibrary_Impl(rLib.aName, guard);
1891 }
1892 if( pImplLib->mbPasswordProtected )
1893 {
1894 implStorePasswordLibrary( pImplLib, rLib.aName, xLibraryStor, uno::Reference< task::XInteractionHandler >() );
1895 // TODO: Check return value
1896 }
1897 else
1898 {
1899 implStoreLibrary( pImplLib, rLib.aName, xLibraryStor );
1900 }
1901 implStoreLibraryIndexFile( pImplLib, rLib, xLibraryStor );
1902 if( bStorage )
1903 {
1904 try
1905 {
1906 uno::Reference< embed::XTransactedObject > xTransact( xLibraryStor, uno::UNO_QUERY_THROW );
1907 xTransact->commit();
1908 }
1909 catch(const uno::Exception& )
1910 {
1911 DBG_UNHANDLED_EXCEPTION("basic");
1912 // TODO: error handling
1913 throw;
1914 }
1915 }
1916 }
1917 maModifiable.setModified(true, guard);
1918 pImplLib->implSetModified(false, guard);
1919 }
1920
1921 // For container info ReadOnly refers to mbReadOnlyLink
1922 rLib.bReadOnly = pImplLib->mbReadOnlyLink;
1923 }
1924
1925 // if we did an in-place save into a storage (i.e. a save into the storage we were already based on),
1926 // then we need to clean up the temporary storage we used for this
1927 if ( bInplaceStorage && !sTempTargetStorName.isEmpty() )
1928 {
1929 SAL_WARN_IF(
1930 !xSourceLibrariesStor.is(), "basic",
1931 ("SfxLibrariesContainer::storeLibraries_impl: unexpected: we should"
1932 " have a source storage here!"));
1933 try
1934 {
1935 // for this, we first remove everything from the source storage, then copy the complete content
1936 // from the temporary target storage. From then on, what used to be the "source storage" becomes
1937 // the "target storage" for all subsequent operations.
1938
1939 // (We cannot simply remove the storage, denoted by maLibrariesDir, from i_rStorage - there might be
1940 // open references to it.)
1941
1942 if ( xSourceLibrariesStor.is() )
1943 {
1944 // remove
1945 const Sequence< OUString > aRemoveNames( xSourceLibrariesStor->getElementNames() );
1946 for ( auto const & removeName : aRemoveNames )
1947 {
1948 xSourceLibrariesStor->removeElement( removeName );
1949 }
1950
1951 // copy
1952 const Sequence< OUString > aCopyNames( xTargetLibrariesStor->getElementNames() );
1953 for ( auto const & copyName : aCopyNames )
1954 {
1955 xTargetLibrariesStor->copyElementTo( copyName, xSourceLibrariesStor, copyName );
1956 }
1957 }
1958
1959 // close and remove temp target
1960 xTargetLibrariesStor->dispose();
1961 i_rStorage->removeElement( sTempTargetStorName );
1962 xTargetLibrariesStor.clear();
1963 sTempTargetStorName.clear();
1964
1965 // adjust target
1966 xTargetLibrariesStor = xSourceLibrariesStor;
1967 xSourceLibrariesStor.clear();
1968 }
1969 catch( const Exception& )
1970 {
1971 DBG_UNHANDLED_EXCEPTION("basic");
1972 throw;
1973 }
1974 }
1975
1976 if( !mbOldInfoFormat && !maModifiable.isModified() )
1977 {
1978 return;
1979 }
1980 maModifiable.setModified(false, guard);
1981 mbOldInfoFormat = false;
1982
1983 // Write library container info
1984 // Create sax writer
1985 Reference< XWriter > xWriter = xml::sax::Writer::create(mxContext);
1986
1987 // Write info file
1988 uno::Reference< io::XOutputStream > xOut;
1989 uno::Reference< io::XStream > xInfoStream;
1990 if( bStorage )
1991 {
1992 OUString aStreamName = maInfoFileName + "-lc.xml";
1993
1994 try
1995 {
1996 xInfoStream = xTargetLibrariesStor->openStreamElement( aStreamName, embed::ElementModes::READWRITE );
1997 uno::Reference< beans::XPropertySet > xProps( xInfoStream, uno::UNO_QUERY_THROW );
1998 xProps->setPropertyValue(u"MediaType"_ustr, uno::Any( u"text/xml"_ustr ) );
1999
2000 // #87671 Allow encryption
2001 xProps->setPropertyValue(u"UseCommonStoragePasswordEncryption"_ustr, uno::Any( true ) );
2002
2003 xOut = xInfoStream->getOutputStream();
2004 }
2005 catch(const uno::Exception& )
2006 {
2007 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
2008 }
2009 }
2010 else
2011 {
2012 // Create Output stream
2013 INetURLObject aLibInfoInetObj( o3tl::getToken(maLibraryPath, 1, ';') );
2014 aLibInfoInetObj.insertName( maInfoFileName, false, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
2015 aLibInfoInetObj.setExtension( u"xlc" );
2016 OUString aLibInfoPath( aLibInfoInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
2017
2018 try
2019 {
2020 if( mxSFI->exists( aLibInfoPath ) )
2021 {
2022 mxSFI->kill( aLibInfoPath );
2023 }
2024 xOut = mxSFI->openFileWrite( aLibInfoPath );
2025 }
2026 catch(const Exception& )
2027 {
2028 xOut.clear();
2029 SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aLibInfoPath );
2030 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
2031 }
2032
2033 }
2034 if( !xOut.is() )
2035 {
2036 SAL_WARN("basic", "couldn't open output stream");
2037 return;
2038 }
2039
2040 xWriter->setOutputStream( xOut );
2041
2042 try
2043 {
2044 xmlscript::exportLibraryContainer( xWriter, &aLibArray );
2045 if ( bStorage )
2046 {
2047 uno::Reference< embed::XTransactedObject > xTransact( xTargetLibrariesStor, uno::UNO_QUERY_THROW );
2048 xTransact->commit();
2049 }
2050 }
2051 catch(const uno::Exception& )
2052 {
2053 SAL_WARN("basic", "Problem during storing of libraries!");
2054 ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
2055 }
2056 }
2057
2058
2059 // Methods XElementAccess
2060 Type SAL_CALL SfxLibraryContainer::getElementType()
2061 {
2062 LibraryContainerMethodGuard aGuard( *this );
2063 return maNameContainer.getElementType();
2064 }
2065
2066 sal_Bool SfxLibraryContainer::hasElements()
2067 {
2068 LibraryContainerMethodGuard aGuard( *this );
2069 return maNameContainer.hasElements();
2070 }
2071
2072 // Methods XNameAccess
2073 Any SfxLibraryContainer::getByName( const OUString& aName )
2074 {
2075 LibraryContainerMethodGuard aGuard( *this );
2076 return maNameContainer.getByName(aName);
2077 }
2078
2079 Sequence< OUString > SfxLibraryContainer::getElementNames()
2080 {
2081 LibraryContainerMethodGuard aGuard( *this );
2082 return maNameContainer.getElementNames();
2083 }
2084
2085 sal_Bool SfxLibraryContainer::hasByName( const OUString& aName )
2086 {
2087 LibraryContainerMethodGuard aGuard( *this );
2088 return maNameContainer.hasByName( aName ) ;
2089 }
2090
2091 // Methods XLibraryContainer
2092 Reference< XNameContainer > SAL_CALL SfxLibraryContainer::createLibrary( const OUString& Name )
2093 {
2094 LibraryContainerMethodGuard aGuard( *this );
2095 return createLibrary_Impl(Name, o3tl::temporary(std::unique_lock(m_aMutex)));
2096 }
2097
2098 css::uno::Reference<css::container::XNameContainer>
2099 SfxLibraryContainer::createLibrary_Impl(const OUString& Name, std::unique_lock<std::mutex>& guard)
2100 {
2101 rtl::Reference<SfxLibrary> pNewLib = implCreateLibrary( Name );
2102 pNewLib->maLibElementFileExtension = maLibElementFileExtension;
2103
2104 createVariableURL( pNewLib->maUnexpandedStorageURL, Name, maInfoFileName, true );
2105 // tdf#151741 - fill various storage URLs for the newly created library
2106 checkStorageURL(pNewLib->maUnexpandedStorageURL, pNewLib->maLibInfoFileURL,
2107 pNewLib->maStorageURL, pNewLib->maUnexpandedStorageURL);
2108
2109 Any aElement;
2110 aElement <<= Reference< XNameAccess >( pNewLib );
2111 maNameContainer.insertByName(Name, aElement, guard);
2112 maModifiable.setModified(true, guard);
2113 return pNewLib;
2114 }
2115
2116 Reference< XNameAccess > SAL_CALL SfxLibraryContainer::createLibraryLink
2117 ( const OUString& Name, const OUString& StorageURL, sal_Bool ReadOnly )
2118 {
2119 LibraryContainerMethodGuard aGuard( *this );
2120 return createLibraryLink_Impl(Name, StorageURL, ReadOnly, o3tl::temporary(std::unique_lock(m_aMutex)));
2121 }
2122
2123 css::uno::Reference<css::container::XNameAccess>
2124 SfxLibraryContainer::createLibraryLink_Impl(const OUString& Name, const OUString& StorageURL,
2125 sal_Bool ReadOnly, std::unique_lock<std::mutex>& guard)
2126 {
2127 // TODO: Check other reasons to force ReadOnly status
2128 //if( !ReadOnly )
2129 //{
2130 //}
2131
2132 OUString aLibInfoFileURL;
2133 OUString aLibDirURL;
2134 OUString aUnexpandedStorageURL;
2135 checkStorageURL( StorageURL, aLibInfoFileURL, aLibDirURL, aUnexpandedStorageURL );
2136
2137
2138 rtl::Reference<SfxLibrary> pNewLib = implCreateLibraryLink( Name, aLibInfoFileURL, aLibDirURL, ReadOnly );
2139 pNewLib->maLibElementFileExtension = maLibElementFileExtension;
2140 pNewLib->maUnexpandedStorageURL = aUnexpandedStorageURL;
2141 pNewLib->maOriginalStorageURL = StorageURL;
2142
2143 uno::Reference< embed::XStorage > xDummyStor;
2144 ::xmlscript::LibDescriptor aLibDesc;
2145 implLoadLibraryIndexFile(pNewLib.get(), aLibDesc, xDummyStor, OUString(), guard);
2146 implImportLibDescriptor(pNewLib.get(), aLibDesc, guard);
2147
2148 Any aElement;
2149 aElement <<= Reference< XNameAccess >( pNewLib );
2150 maNameContainer.insertByName(Name, aElement, guard);
2151 maModifiable.setModified(true, guard);
2152
2153 if( StorageURL.indexOf( "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE" ) != -1 )
2154 {
2155 pNewLib->mbExtension = true;
2156 }
2157 else if( StorageURL.indexOf( "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE" ) != -1
2158 || StorageURL.indexOf( "vnd.sun.star.expand:$BUNDLED_EXTENSIONS" ) != -1 )
2159 {
2160 pNewLib->mbExtension = true;
2161 pNewLib->mbReadOnly = true;
2162 }
2163
2164 return pNewLib;
2165 }
2166
2167 void SAL_CALL SfxLibraryContainer::removeLibrary( const OUString& Name )
2168 {
2169 LibraryContainerMethodGuard aGuard( *this );
2170 std::unique_lock guard(m_aMutex);
2171 // Get and hold library before removing
2172 rtl::Reference pImplLib(getImplLib(Name));
2173 if( pImplLib->mbReadOnly && !pImplLib->mbLink )
2174 {
2175 throw IllegalArgumentException(u"readonly && !link"_ustr, getXWeak(), 1);
2176 }
2177 // Remove from container
2178 maNameContainer.removeByName(Name, guard);
2179 maModifiable.setModified(true, guard);
2180
2181 // Delete library files, but not for linked libraries
2182 if( pImplLib->mbLink )
2183 return;
2184
2185 if( mxStorage.is() )
2186 {
2187 return;
2188 }
2189 if (pImplLib->hasElements())
2190 {
2191 for (auto& name : pImplLib->getElementNames())
2192 pImplLib->impl_removeWithoutChecks(name, guard);
2193 }
2194
2195 // Delete index file
2196 createAppLibraryFolder(pImplLib.get(), Name);
2197 OUString aLibInfoPath = pImplLib->maLibInfoFileURL;
2198 try
2199 {
2200 if( mxSFI->exists( aLibInfoPath ) )
2201 {
2202 mxSFI->kill( aLibInfoPath );
2203 }
2204 }
2205 catch(const Exception& ) {}
2206
2207 // Delete folder if empty
2208 INetURLObject aInetObj( o3tl::getToken(maLibraryPath, 1, ';') );
2209 aInetObj.insertName( Name, true, INetURLObject::LAST_SEGMENT,
2210 INetURLObject::EncodeMechanism::All );
2211 OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
2212
2213 try
2214 {
2215 if( mxSFI->isFolder( aLibDirPath ) )
2216 {
2217 Sequence< OUString > aContentSeq = mxSFI->getFolderContents( aLibDirPath, true );
2218 sal_Int32 nCount = aContentSeq.getLength();
2219 if( !nCount )
2220 {
2221 mxSFI->kill( aLibDirPath );
2222 }
2223 }
2224 }
2225 catch(const Exception& )
2226 {
2227 }
2228 }
2229
2230 sal_Bool SAL_CALL SfxLibraryContainer::isLibraryLoaded( const OUString& Name )
2231 {
2232 LibraryContainerMethodGuard aGuard( *this );
2233 SfxLibrary* pImplLib = getImplLib( Name );
2234 bool bRet = pImplLib->mbLoaded;
2235 return bRet;
2236 }
2237
2238
2239 void SAL_CALL SfxLibraryContainer::loadLibrary( const OUString& Name )
2240 {
2241 LibraryContainerMethodGuard aGuard( *this );
2242 return loadLibrary_Impl(Name, o3tl::temporary(std::unique_lock(m_aMutex)));
2243 }
2244
2245 void SfxLibraryContainer::loadLibrary_Impl(const OUString& Name,
2246 std::unique_lock<std::mutex>& guard)
2247 {
2248 SfxLibrary* pImplLib = getImplLib(Name);
2249
2250 bool bLoaded = pImplLib->mbLoaded;
2251 pImplLib->mbLoaded = true;
2252 if (bLoaded || !pImplLib->hasElements())
2253 return;
2254
2255 if( pImplLib->mbPasswordProtected )
2256 {
2257 implLoadPasswordLibrary(pImplLib, Name, false, guard);
2258 return;
2259 }
2260
2261 bool bLink = pImplLib->mbLink;
2262 bool bStorage = mxStorage.is() && !bLink;
2263
2264 uno::Reference< embed::XStorage > xLibrariesStor;
2265 uno::Reference< embed::XStorage > xLibraryStor;
2266 if( bStorage )
2267 {
2268 #if OSL_DEBUG_LEVEL > 0
2269 try
2270 {
2271 #endif
2272 xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
2273 SAL_WARN_IF(
2274 !xLibrariesStor.is(), "basic",
2275 ("The method must either throw exception or return a"
2276 " storage!"));
2277 if ( !xLibrariesStor.is() )
2278 {
2279 throw uno::RuntimeException(u"null returned from openStorageElement"_ustr,getXWeak());
2280 }
2281
2282 xLibraryStor = xLibrariesStor->openStorageElement( Name, embed::ElementModes::READ );
2283 SAL_WARN_IF(
2284 !xLibraryStor.is(), "basic",
2285 ("The method must either throw exception or return a"
2286 " storage!"));
2287 if ( !xLibrariesStor.is() )
2288 {
2289 throw uno::RuntimeException(u"null returned from openStorageElement"_ustr,getXWeak());
2290 }
2291 #if OSL_DEBUG_LEVEL > 0
2292 }
2293 catch(const uno::Exception& )
2294 {
2295 TOOLS_WARN_EXCEPTION(
2296 "basic",
2297 "couldn't open sub storage for library \"" << Name << "\"");
2298 throw;
2299 }
2300 #endif
2301 }
2302
2303 // tdf#167255 workaround: sort library elements to establish at least some predictable order.
2304 // FIXME: the order of modules must not affect their inner names visibility. Modules must load
2305 // their content first (and so the names of e.g. global constants / variables must be known),
2306 // and only then their elements' values must be resolved.
2307 auto elements = pImplLib->getElementNames();
2308 {
2309 auto range = asNonConstRange(elements);
2310 std::sort(range.begin(), range.end());
2311 }
2312
2313 for (auto& aElementName : elements)
2314 {
2315 OUString aFile;
2316 uno::Reference< io::XInputStream > xInStream;
2317
2318 if( bStorage )
2319 {
2320 uno::Reference< io::XStream > xElementStream;
2321
2322 aFile = aElementName + ".xml";
2323
2324 try
2325 {
2326 xElementStream = xLibraryStor->openStreamElement( aFile, embed::ElementModes::READ );
2327 }
2328 catch(const uno::Exception& )
2329 {}
2330
2331 if( !xElementStream.is() )
2332 {
2333 // Check for EA2 document version with wrong extensions
2334 aFile = aElementName + "." + maLibElementFileExtension;
2335 try
2336 {
2337 xElementStream = xLibraryStor->openStreamElement( aFile, embed::ElementModes::READ );
2338 }
2339 catch(const uno::Exception& )
2340 {}
2341 }
2342
2343 if ( xElementStream.is() )
2344 {
2345 xInStream = xElementStream->getInputStream();
2346 }
2347 if ( !xInStream.is() )
2348 {
2349 SAL_WARN(
2350 "basic",
2351 "couldn't open library element stream - attempted to"
2352 " open library \"" << Name << '"');
2353 throw RuntimeException(u"couldn't open library element stream"_ustr, *this);
2354 }
2355 }
2356 else
2357 {
2358 OUString aLibDirPath = pImplLib->maStorageURL;
2359 INetURLObject aElementInetObj( aLibDirPath );
2360 aElementInetObj.insertName( aElementName, false,
2361 INetURLObject::LAST_SEGMENT,
2362 INetURLObject::EncodeMechanism::All );
2363 aElementInetObj.setExtension( maLibElementFileExtension );
2364 aFile = aElementInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
2365 }
2366
2367 Reference< XNameContainer > xLib( pImplLib );
2368 Any aAny = importLibraryElement( xLib, aElementName,
2369 aFile, xInStream );
2370 if( pImplLib->hasByName( aElementName ) )
2371 {
2372 if( aAny.hasValue() )
2373 {
2374 pImplLib->maNameContainer.replaceByName(aElementName, aAny, guard);
2375 }
2376 }
2377 else
2378 {
2379 pImplLib->maNameContainer.insertNoCheck(aElementName, aAny, guard);
2380 }
2381 }
2382 pImplLib->implSetModified(false, guard);
2383 }
2384
2385 // Methods XLibraryContainer2
2386 sal_Bool SAL_CALL SfxLibraryContainer::isLibraryLink( const OUString& Name )
2387 {
2388 LibraryContainerMethodGuard aGuard( *this );
2389 SfxLibrary* pImplLib = getImplLib( Name );
2390 bool bRet = pImplLib->mbLink;
2391 return bRet;
2392 }
2393
2394 OUString SAL_CALL SfxLibraryContainer::getLibraryLinkURL( const OUString& Name )
2395 {
2396 LibraryContainerMethodGuard aGuard( *this );
2397 SfxLibrary* pImplLib = getImplLib( Name );
2398 bool bLink = pImplLib->mbLink;
2399 if( !bLink )
2400 {
2401 throw IllegalArgumentException(u"!link"_ustr, getXWeak(), 1);
2402 }
2403 OUString aRetStr = pImplLib->maLibInfoFileURL;
2404 return aRetStr;
2405 }
2406
2407 sal_Bool SAL_CALL SfxLibraryContainer::isLibraryReadOnly( const OUString& Name )
2408 {
2409 LibraryContainerMethodGuard aGuard( *this );
2410 SfxLibrary* pImplLib = getImplLib( Name );
2411 bool bRet = pImplLib->mbReadOnly || (pImplLib->mbLink && pImplLib->mbReadOnlyLink);
2412 return bRet;
2413 }
2414
2415 void SAL_CALL SfxLibraryContainer::setLibraryReadOnly( const OUString& Name, sal_Bool bReadOnly )
2416 {
2417 LibraryContainerMethodGuard aGuard( *this );
2418 std::unique_lock guard(m_aMutex);
2419 SfxLibrary* pImplLib = getImplLib( Name );
2420 if( pImplLib->mbLink )
2421 {
2422 if( pImplLib->mbReadOnlyLink != bool(bReadOnly) )
2423 {
2424 pImplLib->mbReadOnlyLink = bReadOnly;
2425 pImplLib->implSetModified(true, guard);
2426 maModifiable.setModified(true, guard);
2427 }
2428 }
2429 else
2430 {
2431 if( pImplLib->mbReadOnly != bool(bReadOnly) )
2432 {
2433 pImplLib->mbReadOnly = bReadOnly;
2434 pImplLib->implSetModified(true, guard);
2435 }
2436 }
2437 }
2438
2439 void SAL_CALL SfxLibraryContainer::renameLibrary( const OUString& Name, const OUString& NewName )
2440 {
2441 LibraryContainerMethodGuard aGuard( *this );
2442 std::unique_lock guard(m_aMutex);
2443 if( maNameContainer.hasByName( NewName ) )
2444 {
2445 throw ElementExistException();
2446 }
2447 // Get and hold library before removing
2448 rtl::Reference pImplLib(getImplLib(Name));
2449
2450 // #i24094 Maybe lib is not loaded!
2451 if( pImplLib->mbPasswordProtected && !pImplLib->mbPasswordVerified )
2452 {
2453 return; // Lib with unverified password cannot be renamed
2454 }
2455 loadLibrary_Impl(Name, guard);
2456
2457 // Rename library folder, but not for linked libraries
2458 bool bMovedSuccessful = true;
2459
2460 // Rename files
2461 bool bStorage = mxStorage.is();
2462 if( !bStorage && !pImplLib->mbLink )
2463 {
2464 bMovedSuccessful = false;
2465
2466 OUString aLibDirPath = pImplLib->maStorageURL;
2467 // tdf#151741 - fill various storage URLs for the library
2468 // These URLs should not be empty for newly created libraries after
2469 // the change in SfxLibraryContainer::createLibrary_Impl.
2470 if (aLibDirPath.isEmpty())
2471 {
2472 checkStorageURL(pImplLib->maUnexpandedStorageURL, pImplLib->maLibInfoFileURL,
2473 pImplLib->maStorageURL, pImplLib->maUnexpandedStorageURL);
2474 }
2475
2476 INetURLObject aDestInetObj( o3tl::getToken(maLibraryPath, 1, ';'));
2477 aDestInetObj.insertName( NewName, true, INetURLObject::LAST_SEGMENT,
2478 INetURLObject::EncodeMechanism::All );
2479 OUString aDestDirPath = aDestInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
2480
2481 // Store new URL
2482 OUString aLibInfoFileURL = pImplLib->maLibInfoFileURL;
2483 checkStorageURL(aDestDirPath, pImplLib->maLibInfoFileURL, pImplLib->maStorageURL,
2484 pImplLib->maUnexpandedStorageURL );
2485
2486 try
2487 {
2488 if( mxSFI->isFolder( aLibDirPath ) )
2489 {
2490 if( !mxSFI->isFolder( aDestDirPath ) )
2491 {
2492 mxSFI->createFolder( aDestDirPath );
2493 }
2494 // Move index file
2495 try
2496 {
2497 if( mxSFI->exists( pImplLib->maLibInfoFileURL ) )
2498 {
2499 mxSFI->kill( pImplLib->maLibInfoFileURL );
2500 }
2501 mxSFI->move( aLibInfoFileURL, pImplLib->maLibInfoFileURL );
2502 }
2503 catch(const Exception& )
2504 {
2505 }
2506
2507 for (auto& aElementName : pImplLib->getElementNames())
2508 {
2509 INetURLObject aElementInetObj( aLibDirPath );
2510 aElementInetObj.insertName( aElementName, false,
2511 INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
2512 aElementInetObj.setExtension( maLibElementFileExtension );
2513 OUString aElementPath( aElementInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
2514
2515 INetURLObject aElementDestInetObj( aDestDirPath );
2516 aElementDestInetObj.insertName( aElementName, false,
2517 INetURLObject::LAST_SEGMENT,
2518 INetURLObject::EncodeMechanism::All );
2519 aElementDestInetObj.setExtension( maLibElementFileExtension );
2520 OUString aDestElementPath( aElementDestInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
2521
2522 try
2523 {
2524 if( mxSFI->exists( aDestElementPath ) )
2525 {
2526 mxSFI->kill( aDestElementPath );
2527 }
2528 mxSFI->move( aElementPath, aDestElementPath );
2529 }
2530 catch(const Exception& )
2531 {
2532 }
2533 }
2534 pImplLib->storeResourcesAsURL( aDestDirPath, NewName );
2535
2536 // Delete folder if empty
2537 Sequence< OUString > aContentSeq = mxSFI->getFolderContents( aLibDirPath, true );
2538 sal_Int32 nCount = aContentSeq.getLength();
2539 if( !nCount )
2540 {
2541 mxSFI->kill( aLibDirPath );
2542 }
2543
2544 bMovedSuccessful = true;
2545 pImplLib->implSetModified(true, guard);
2546 }
2547 }
2548 catch(const Exception& )
2549 {
2550 }
2551 }
2552
2553 if( bStorage && !pImplLib->mbLink )
2554 {
2555 pImplLib->implSetModified(true, guard);
2556 }
2557 if( bMovedSuccessful )
2558 {
2559 // Remove the old library from the container and insert it back with the new name
2560 maNameContainer.removeByName(Name, guard);
2561 maNameContainer.insertByName(NewName, Any(Reference<XNameAccess>(pImplLib)), guard);
2562 maModifiable.setModified(true, guard);
2563 }
2564 }
2565
2566
2567 // Methods XInitialization
2568 void SAL_CALL SfxLibraryContainer::initialize( const Sequence< Any >& _rArguments )
2569 {
2570 LibraryContainerMethodGuard aGuard( *this );
2571 std::unique_lock guard(m_aMutex);
2572 sal_Int32 nArgCount = _rArguments.getLength();
2573 if ( nArgCount != 1 )
2574 throw IllegalArgumentException(u"too many args"_ustr, getXWeak(), -1);
2575
2576 OUString sInitialDocumentURL;
2577 Reference< XStorageBasedDocument > xDocument;
2578 if ( _rArguments[0] >>= sInitialDocumentURL )
2579 {
2580 init(sInitialDocumentURL, nullptr, guard);
2581 return;
2582 }
2583
2584 if ( _rArguments[0] >>= xDocument )
2585 {
2586 initializeFromDocument(xDocument, guard);
2587 return;
2588 }
2589 throw IllegalArgumentException(u"arg1 unknown type"_ustr, getXWeak(), 1);
2590
2591 }
2592
2593 void SfxLibraryContainer::initializeFromDocument( const Reference< XStorageBasedDocument >& _rxDocument,
2594 std::unique_lock<std::mutex>& guard )
2595 {
2596 // check whether this is a valid OfficeDocument, and obtain the document's root storage
2597 Reference< XStorage > xDocStorage;
2598 try
2599 {
2600 Reference< XServiceInfo > xSI( _rxDocument, UNO_QUERY_THROW );
2601 if ( xSI->supportsService(u"com.sun.star.document.OfficeDocument"_ustr))
2602 {
2603 xDocStorage.set( _rxDocument->getDocumentStorage(), UNO_SET_THROW );
2604 }
2605 Reference< XModel > xDocument( _rxDocument, UNO_QUERY_THROW );
2606 Reference< XComponent > xDocComponent( _rxDocument, UNO_QUERY_THROW );
2607
2608 mxOwnerDocument = xDocument;
2609 startComponentListening( xDocComponent );
2610 }
2611 catch( const Exception& ) { }
2612
2613 if ( !xDocStorage.is() )
2614 {
2615 throw IllegalArgumentException(u"no doc storage"_ustr, getXWeak(), 1);
2616 }
2617 init(OUString(), xDocStorage, guard);
2618 }
2619
2620 // OEventListenerAdapter
2621 void SfxLibraryContainer::_disposing( const EventObject& _rSource )
2622 {
2623 #if OSL_DEBUG_LEVEL > 0
2624 Reference< XModel > xDocument( mxOwnerDocument.get(), UNO_QUERY );
2625 SAL_WARN_IF(
2626 xDocument != _rSource.Source || !xDocument.is(), "basic",
2627 "SfxLibraryContainer::_disposing: where does this come from?");
2628 #else
2629 (void)_rSource;
2630 #endif
2631 dispose();
2632 }
2633
2634 // OComponentHelper
2635 void SfxLibraryContainer::disposing(std::unique_lock<std::mutex>& guard)
2636 {
2637 Reference< XModel > xModel = mxOwnerDocument;
2638 EventObject aEvent( xModel );
2639 maVBAScriptListeners.disposeAndClear(guard, aEvent);
2640 stopAllComponentListening();
2641 mxOwnerDocument.clear();
2642 }
2643
2644 // Methods XLibraryContainerPassword
2645 sal_Bool SAL_CALL SfxLibraryContainer::isLibraryPasswordProtected( const OUString& )
2646 {
2647 return false;
2648 }
2649
2650 sal_Bool SAL_CALL SfxLibraryContainer::isLibraryPasswordVerified( const OUString& )
2651 {
2652 throw IllegalArgumentException();
2653 }
2654
2655 sal_Bool SAL_CALL SfxLibraryContainer::verifyLibraryPassword( const OUString&, const OUString& )
2656 {
2657 throw IllegalArgumentException();
2658 }
2659
2660 void SAL_CALL SfxLibraryContainer::changeLibraryPassword(const OUString&, const OUString&, const OUString& )
2661 {
2662 throw IllegalArgumentException();
2663 }
2664
2665 // Methods XContainer
2666 void SAL_CALL SfxLibraryContainer::addContainerListener( const Reference< XContainerListener >& xListener )
2667 {
2668 LibraryContainerMethodGuard aGuard( *this );
2669 std::unique_lock guard(m_aMutex);
2670 maNameContainer.setEventSource( getXWeak() );
2671 maNameContainer.addContainerListener(xListener, guard);
2672 }
2673
2674 void SAL_CALL SfxLibraryContainer::removeContainerListener( const Reference< XContainerListener >& xListener )
2675 {
2676 LibraryContainerMethodGuard aGuard( *this );
2677 std::unique_lock guard(m_aMutex);
2678 maNameContainer.removeContainerListener(xListener, guard);
2679 }
2680
2681 // Methods XLibraryContainerExport
2682 void SAL_CALL SfxLibraryContainer::exportLibrary( const OUString& Name, const OUString& URL,
2683 const Reference< XInteractionHandler >& Handler )
2684 {
2685 LibraryContainerMethodGuard aGuard( *this );
2686 std::unique_lock guard(m_aMutex);
2687 SfxLibrary* pImplLib = getImplLib( Name );
2688
2689 Reference< XSimpleFileAccess3 > xToUseSFI;
2690 if( Handler.is() )
2691 {
2692 xToUseSFI = ucb::SimpleFileAccess::create( mxContext );
2693 xToUseSFI->setInteractionHandler( Handler );
2694 }
2695
2696 // Maybe lib is not loaded?!
2697 loadLibrary_Impl(Name, guard);
2698
2699 uno::Reference< css::embed::XStorage > xDummyStor;
2700 if( pImplLib->mbPasswordProtected )
2701 {
2702 implStorePasswordLibrary( pImplLib, Name, xDummyStor, URL, xToUseSFI, Handler );
2703 }
2704 else
2705 {
2706 implStoreLibrary( pImplLib, Name, xDummyStor, URL, xToUseSFI, Handler );
2707 }
2708 ::xmlscript::LibDescriptor aLibDesc;
2709 aLibDesc.aName = Name;
2710 aLibDesc.bLink = false; // Link status gets lost?
2711 aLibDesc.bReadOnly = pImplLib->mbReadOnly;
2712 aLibDesc.bPreload = false; // Preload status gets lost?
2713 aLibDesc.bPasswordProtected = pImplLib->mbPasswordProtected;
2714 aLibDesc.aElementNames = pImplLib->getElementNames();
2715
2716 implStoreLibraryIndexFile( pImplLib, aLibDesc, xDummyStor, URL, xToUseSFI );
2717 }
2718
2719 OUString SfxLibraryContainer::expand_url( const OUString& url )
2720 {
2721 if (url.startsWithIgnoreAsciiCase( "vnd.sun.star.expand:" ))
2722 {
2723 return comphelper::getExpandedUri(mxContext, url);
2724 }
2725 else if( mxStringSubstitution.is() )
2726 {
2727 OUString ret( mxStringSubstitution->substituteVariables( url, false ) );
2728 return ret;
2729 }
2730 else
2731 {
2732 return url;
2733 }
2734 }
2735
2736 //XLibraryContainer3
2737 OUString SAL_CALL SfxLibraryContainer::getOriginalLibraryLinkURL( const OUString& Name )
2738 {
2739 LibraryContainerMethodGuard aGuard( *this );
2740 SfxLibrary* pImplLib = getImplLib( Name );
2741 bool bLink = pImplLib->mbLink;
2742 if( !bLink )
2743 {
2744 throw IllegalArgumentException(u"!link"_ustr, getXWeak(), 1);
2745 }
2746 OUString aRetStr = pImplLib->maOriginalStorageURL;
2747 return aRetStr;
2748 }
2749
2750
2751 // XVBACompatibility
2752 sal_Bool SAL_CALL SfxLibraryContainer::getVBACompatibilityMode()
2753 {
2754 return mbVBACompat;
2755 }
2756
2757 void SAL_CALL SfxLibraryContainer::setVBACompatibilityMode( sal_Bool _vbacompatmodeon )
2758 {
2759 /* The member variable mbVBACompat must be set first, the following call
2760 to getBasicManager() may call getVBACompatibilityMode() which returns
2761 this value. */
2762 mbVBACompat = _vbacompatmodeon;
2763 BasicManager* pBasMgr = getBasicManager();
2764 if( !pBasMgr )
2765 return;
2766
2767 // get the standard library
2768 OUString aLibName = pBasMgr->GetName();
2769 if ( aLibName.isEmpty())
2770 {
2771 aLibName = "Standard";
2772 }
2773 if( StarBASIC* pBasic = pBasMgr->GetLib( aLibName ) )
2774 {
2775 pBasic->SetVBAEnabled( _vbacompatmodeon );
2776 }
2777 /* If in VBA compatibility mode, force creation of the VBA Globals
2778 object. Each application will create an instance of its own
2779 implementation and store it in its Basic manager. Implementations
2780 will do all necessary additional initialization, such as
2781 registering the global "This***Doc" UNO constant, starting the
2782 document events processor etc.
2783 */
2784 if( mbVBACompat ) try
2785 {
2786 Reference< XModel > xModel( mxOwnerDocument ); // weak-ref -> ref
2787 Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
2788 xFactory->createInstance(u"ooo.vba.VBAGlobals"_ustr);
2789 }
2790 catch(const Exception& )
2791 {
2792 }
2793 }
2794
2795 void SAL_CALL SfxLibraryContainer::setProjectName( const OUString& _projectname )
2796 {
2797 msProjectName = _projectname;
2798 BasicManager* pBasMgr = getBasicManager();
2799 // Temporary HACK
2800 // Some parts of the VBA handling ( e.g. in core basic )
2801 // code expect the name of the VBA project to be set as the name of
2802 // the basic manager. Provide fail back here.
2803 if( pBasMgr )
2804 {
2805 pBasMgr->SetName( msProjectName );
2806 }
2807 }
2808
2809 sal_Int32 SAL_CALL SfxLibraryContainer::getRunningVBAScripts()
2810 {
2811 LibraryContainerMethodGuard aGuard( *this );
2812 return mnRunningVBAScripts;
2813 }
2814
2815 void SAL_CALL SfxLibraryContainer::addVBAScriptListener( const Reference< vba::XVBAScriptListener >& rxListener )
2816 {
2817 maVBAScriptListeners.addInterface(o3tl::temporary(std::unique_lock(m_aMutex)), rxListener);
2818 }
2819
2820 void SAL_CALL SfxLibraryContainer::removeVBAScriptListener( const Reference< vba::XVBAScriptListener >& rxListener )
2821 {
2822 maVBAScriptListeners.removeInterface(o3tl::temporary(std::unique_lock(m_aMutex)), rxListener);
2823 }
2824
2825 void SAL_CALL SfxLibraryContainer::broadcastVBAScriptEvent( sal_Int32 nIdentifier, const OUString& rModuleName )
2826 {
2827 // own lock for accessing the number of running scripts
2828 enterMethod();
2829 switch( nIdentifier )
2830 {
2831 case vba::VBAScriptEventId::SCRIPT_STARTED:
2832 ++mnRunningVBAScripts;
2833 break;
2834 case vba::VBAScriptEventId::SCRIPT_STOPPED:
2835 --mnRunningVBAScripts;
2836 break;
2837 }
2838 leaveMethod();
2839
2840 Reference< XModel > xModel = mxOwnerDocument; // weak-ref -> ref
2841 vba::VBAScriptEvent aEvent( Reference<XInterface>(xModel, UNO_QUERY), nIdentifier, rModuleName );
2842 maVBAScriptListeners.notifyEach(o3tl::temporary(std::unique_lock(m_aMutex)),
2843 &css::script::vba::XVBAScriptListener::notifyVBAScriptEvent,
2844 aEvent);
2845 }
2846
2847 // Methods XPropertySet
2848 css::uno::Reference<css::beans::XPropertySetInfo> SAL_CALL SfxLibraryContainer::getPropertySetInfo()
2849 {
2850 return uno::Reference<beans::XPropertySetInfo>();
2851 }
2852
2853 void SAL_CALL SfxLibraryContainer::setPropertyValue(const OUString& aPropertyName,
2854 const uno::Any& aValue)
2855 {
2856 if (aPropertyName != sVBATextEncodingPropName)
2857 throw UnknownPropertyException(aPropertyName, getXWeak());
2858 aValue >>= meVBATextEncoding;
2859 }
2860
2861 css::uno::Any SAL_CALL SfxLibraryContainer::getPropertyValue(const OUString& aPropertyName)
2862 {
2863 if (aPropertyName == sVBATextEncodingPropName)
2864 return uno::Any(meVBATextEncoding);
2865 throw UnknownPropertyException(aPropertyName, getXWeak());
2866 }
2867
2868 void SAL_CALL SfxLibraryContainer::addPropertyChangeListener(
2869 const OUString& /* aPropertyName */, const Reference<XPropertyChangeListener>& /* xListener */)
2870 {
2871 throw NoSupportException();
2872 }
2873
2874 void SAL_CALL SfxLibraryContainer::removePropertyChangeListener(
2875 const OUString& /* aPropertyName */, const Reference<XPropertyChangeListener>& /* aListener */)
2876 {
2877 throw NoSupportException();
2878 }
2879
2880 void SAL_CALL SfxLibraryContainer::addVetoableChangeListener(
2881 const OUString& /* PropertyName */, const Reference<XVetoableChangeListener>& /* aListener */)
2882 {
2883 throw NoSupportException();
2884 }
2885
2886 void SAL_CALL SfxLibraryContainer::removeVetoableChangeListener(
2887 const OUString& /* PropertyName */, const Reference<XVetoableChangeListener>& /* aListener */)
2888 {
2889 throw NoSupportException();
2890 }
2891
2892 // Methods XServiceInfo
2893 sal_Bool SAL_CALL SfxLibraryContainer::supportsService( const OUString& _rServiceName )
2894 {
2895 return cppu::supportsService(this, _rServiceName);
2896 }
2897
2898 // Implementation class SfxLibrary
2899
2900 // Ctor
2901 SfxLibrary::SfxLibrary( ModifiableHelper& _rModifiable, const Type& aType,
2902 const Reference< XSimpleFileAccess3 >& xSFI )
2903 : mxSFI( xSFI )
2904 , mrModifiable( _rModifiable )
2905 , maNameContainer( aType, *this )
2906 , mbLoaded( true )
2907 , mbIsModified( true )
2908 , mbInitialised( false )
2909 , mbLink( false )
2910 , mbReadOnly( false )
2911 , mbReadOnlyLink( false )
2912 , mbPreload( false )
2913 , mbPasswordProtected( false )
2914 , mbPasswordVerified( false )
2915 , mbDoc50Password( false )
2916 , mbSharedIndexFile( false )
2917 , mbExtension( false )
2918 {
2919 }
2920
2921 SfxLibrary::SfxLibrary( ModifiableHelper& _rModifiable, const Type& aType,
2922 const Reference< XSimpleFileAccess3 >& xSFI,
2923 OUString aLibInfoFileURL, OUString aStorageURL, bool ReadOnly )
2924 : mxSFI( xSFI )
2925 , mrModifiable( _rModifiable )
2926 , maNameContainer( aType, *this )
2927 , mbLoaded( false )
2928 , mbIsModified( true )
2929 , mbInitialised( false )
2930 , mbLink( true )
2931 , mbReadOnly( false )
2932 , mbReadOnlyLink( ReadOnly )
2933 , mbPreload( false )
2934 , mbPasswordProtected( false )
2935 , mbPasswordVerified( false )
2936 , mbDoc50Password( false )
2937 , mbSharedIndexFile( false )
2938 , mbExtension( false )
2939 , maLibInfoFileURL(std::move( aLibInfoFileURL ))
2940 , maStorageURL(std::move( aStorageURL ))
2941 {
2942 }
2943
2944 bool SfxLibrary::isLoadedStorable()
2945 {
2946 return mbLoaded && (!mbPasswordProtected || mbPasswordVerified);
2947 }
2948
2949 void SfxLibrary::implSetModified(bool _bIsModified, std::unique_lock<std::mutex>& guard)
2950 {
2951 if ( mbIsModified == _bIsModified )
2952 {
2953 return;
2954 }
2955 mbIsModified = _bIsModified;
2956 if ( mbIsModified )
2957 {
2958 mrModifiable.setModified(true, guard);
2959 }
2960 }
2961
2962 // Methods XElementAccess
2963 Type SfxLibrary::getElementType()
2964 {
2965 return maNameContainer.getElementType();
2966 }
2967
2968 sal_Bool SfxLibrary::hasElements()
2969 {
2970 return maNameContainer.hasElements();
2971 }
2972
2973 // Methods XNameAccess
2974 Any SfxLibrary::getByName( const OUString& aName )
2975 {
2976 impl_checkLoaded();
2977
2978 return maNameContainer.getByName(aName);
2979 }
2980
2981 Sequence< OUString > SfxLibrary::getElementNames()
2982 {
2983 return maNameContainer.getElementNames();
2984 }
2985
2986 sal_Bool SfxLibrary::hasByName( const OUString& aName )
2987 {
2988 bool bRet = maNameContainer.hasByName( aName );
2989 return bRet;
2990 }
2991
2992 void SfxLibrary::impl_checkReadOnly()
2993 {
2994 if( mbReadOnly || (mbLink && mbReadOnlyLink) )
2995 {
2996 throw IllegalArgumentException(
2997 u"Library is readonly."_ustr,
2998 // TODO: resource
2999 *this, 0
3000 );
3001 }
3002 }
3003
3004 void SfxLibrary::impl_checkLoaded()
3005 {
3006 if ( !mbLoaded )
3007 {
3008 throw WrappedTargetException(
3009 OUString(),
3010 *this,
3011 Any( LibraryNotLoadedException(
3012 OUString(),
3013 *this
3014 ) )
3015 );
3016 }
3017 }
3018
3019 // Methods XNameReplace
3020 void SfxLibrary::replaceByName( const OUString& aName, const Any& aElement )
3021 {
3022 impl_checkReadOnly();
3023 impl_checkLoaded();
3024
3025 SAL_WARN_IF(
3026 !isLibraryElementValid(aElement), "basic",
3027 "SfxLibrary::replaceByName: replacing element is invalid!");
3028
3029 std::unique_lock guard(m_aMutex);
3030 maNameContainer.replaceByName(aName, aElement, guard);
3031 implSetModified(true, guard);
3032 }
3033
3034
3035 // Methods XNameContainer
3036 void SfxLibrary::insertByName( const OUString& aName, const Any& aElement )
3037 {
3038 impl_checkReadOnly();
3039 impl_checkLoaded();
3040
3041 SAL_WARN_IF(
3042 !isLibraryElementValid(aElement), "basic",
3043 "SfxLibrary::insertByName: to-be-inserted element is invalid!");
3044
3045 std::unique_lock guard(m_aMutex);
3046 maNameContainer.insertByName(aName, aElement, guard);
3047 implSetModified(true, guard);
3048 }
3049
3050 void SfxLibrary::impl_removeWithoutChecks(const OUString& _rElementName,
3051 std::unique_lock<std::mutex>& guard)
3052 {
3053 maNameContainer.removeByName(_rElementName, guard);
3054 implSetModified(true, guard);
3055
3056 // Remove element file
3057 if( maStorageURL.isEmpty() )
3058 return;
3059
3060 INetURLObject aElementInetObj( maStorageURL );
3061 aElementInetObj.insertName( _rElementName, false,
3062 INetURLObject::LAST_SEGMENT,
3063 INetURLObject::EncodeMechanism::All );
3064 aElementInetObj.setExtension( maLibElementFileExtension );
3065 OUString aFile = aElementInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
3066
3067 try
3068 {
3069 if( mxSFI->exists( aFile ) )
3070 {
3071 mxSFI->kill( aFile );
3072 }
3073 }
3074 catch(const Exception& )
3075 {
3076 DBG_UNHANDLED_EXCEPTION("basic");
3077 }
3078 }
3079
3080 void SfxLibrary::removeByName( const OUString& Name )
3081 {
3082 impl_checkReadOnly();
3083 impl_checkLoaded();
3084 impl_removeWithoutChecks(Name, o3tl::temporary(std::unique_lock(m_aMutex)));
3085 }
3086
3087 // Methods XContainer
3088 void SAL_CALL SfxLibrary::addContainerListener( const Reference< XContainerListener >& xListener )
3089 {
3090 maNameContainer.setEventSource( getXWeak() );
3091 maNameContainer.addContainerListener(xListener, o3tl::temporary(std::unique_lock(m_aMutex)));
3092 }
3093
3094 void SAL_CALL SfxLibrary::removeContainerListener( const Reference< XContainerListener >& xListener )
3095 {
3096 maNameContainer.removeContainerListener(xListener, o3tl::temporary(std::unique_lock(m_aMutex)));
3097 }
3098
3099 // Methods XChangesNotifier
3100 void SAL_CALL SfxLibrary::addChangesListener( const Reference< XChangesListener >& xListener )
3101 {
3102 maNameContainer.setEventSource( getXWeak() );
3103 maNameContainer.addChangesListener(xListener, o3tl::temporary(std::unique_lock(m_aMutex)));
3104 }
3105
3106 void SAL_CALL SfxLibrary::removeChangesListener( const Reference< XChangesListener >& xListener )
3107 {
3108 maNameContainer.removeChangesListener(xListener, o3tl::temporary(std::unique_lock(m_aMutex)));
3109 }
3110
3111
3112 // Implementation class ScriptExtensionIterator
3113
3114 constexpr OUStringLiteral sBasicLibMediaType = u"application/vnd.sun.star.basic-library";
3115 constexpr OUStringLiteral sDialogLibMediaType = u"application/vnd.sun.star.dialog-library";
3116
3117 ScriptExtensionIterator::ScriptExtensionIterator()
3118 : m_xContext( comphelper::getProcessComponentContext() )
3119 , m_eState( USER_EXTENSIONS )
3120 , m_bUserPackagesLoaded( false )
3121 , m_bSharedPackagesLoaded( false )
3122 , m_bBundledPackagesLoaded( false )
3123 , m_iUserPackage( 0 )
3124 , m_iSharedPackage( 0 )
3125 , m_iBundledPackage( 0 )
3126 , m_pScriptSubPackageIterator( nullptr )
3127 {}
3128
3129 OUString ScriptExtensionIterator::nextBasicOrDialogLibrary( bool& rbPureDialogLib )
3130 {
3131 OUString aRetLib;
3132
3133 while( aRetLib.isEmpty() && m_eState != END_REACHED )
3134 {
3135 switch( m_eState )
3136 {
3137 case USER_EXTENSIONS:
3138 {
3139 Reference< deployment::XPackage > xScriptPackage =
3140 implGetNextUserScriptPackage( rbPureDialogLib );
3141 if( !xScriptPackage.is() )
3142 {
3143 break;
3144 }
3145 aRetLib = xScriptPackage->getURL();
3146 break;
3147 }
3148
3149 case SHARED_EXTENSIONS:
3150 {
3151 Reference< deployment::XPackage > xScriptPackage =
3152 implGetNextSharedScriptPackage( rbPureDialogLib );
3153 if( !xScriptPackage.is() )
3154 {
3155 break;
3156 }
3157 aRetLib = xScriptPackage->getURL();
3158 break;
3159 }
3160 case BUNDLED_EXTENSIONS:
3161 {
3162 Reference< deployment::XPackage > xScriptPackage =
3163 implGetNextBundledScriptPackage( rbPureDialogLib );
3164 if( !xScriptPackage.is() )
3165 {
3166 break;
3167 }
3168 aRetLib = xScriptPackage->getURL();
3169 break;
3170 }
3171 case END_REACHED:
3172 SAL_WARN(
3173 "basic",
3174 ("ScriptExtensionIterator::nextBasicOrDialogLibrary():"
3175 " Invalid case END_REACHED"));
3176 break;
3177 }
3178 }
3179
3180 return aRetLib;
3181 }
3182
3183 ScriptSubPackageIterator::ScriptSubPackageIterator( Reference< deployment::XPackage > const & xMainPackage )
3184 : m_xMainPackage( xMainPackage )
3185 , m_bIsValid( false )
3186 , m_bIsBundle( false )
3187 , m_nSubPkgCount( 0 )
3188 , m_iNextSubPkg( 0 )
3189 {
3190 if( !m_xMainPackage.is() )
3191 {
3192 return;
3193 }
3194 // Check if parent package is registered
3195 beans::Optional< beans::Ambiguous<sal_Bool> > option( m_xMainPackage->isRegistered
3196 ( Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>() ) );
3197 bool bRegistered = false;
3198 if( option.IsPresent )
3199 {
3200 beans::Ambiguous<sal_Bool> const & reg = option.Value;
3201 if( !reg.IsAmbiguous && reg.Value )
3202 {
3203 bRegistered = true;
3204 }
3205 }
3206 if( bRegistered )
3207 {
3208 m_bIsValid = true;
3209 if( m_xMainPackage->isBundle() )
3210 {
3211 m_bIsBundle = true;
3212 m_aSubPkgSeq = m_xMainPackage->getBundle( Reference<task::XAbortChannel>(),
3213 Reference<ucb::XCommandEnvironment>() );
3214 m_nSubPkgCount = m_aSubPkgSeq.getLength();
3215 }
3216 }
3217 }
3218
3219 Reference< deployment::XPackage > ScriptSubPackageIterator::getNextScriptSubPackage( bool& rbPureDialogLib )
3220 {
3221 rbPureDialogLib = false;
3222
3223 Reference< deployment::XPackage > xScriptPackage;
3224 if( !m_bIsValid )
3225 {
3226 return xScriptPackage;
3227 }
3228 if( m_bIsBundle )
3229 {
3230 sal_Int32 iPkg;
3231 for( iPkg = m_iNextSubPkg ; iPkg < m_nSubPkgCount ; ++iPkg )
3232 {
3233 const Reference<deployment::XPackage> xSubPkg = m_aSubPkgSeq[iPkg];
3234 xScriptPackage = implDetectScriptPackage( xSubPkg, rbPureDialogLib );
3235 if( xScriptPackage.is() )
3236 {
3237 break;
3238 }
3239 }
3240 m_iNextSubPkg = iPkg + 1;
3241 }
3242 else
3243 {
3244 xScriptPackage = implDetectScriptPackage( m_xMainPackage, rbPureDialogLib );
3245 m_bIsValid = false; // No more script packages
3246 }
3247
3248 return xScriptPackage;
3249 }
3250
3251 Reference< deployment::XPackage > ScriptSubPackageIterator::implDetectScriptPackage ( const Reference< deployment::XPackage >& rPackage,
3252 bool& rbPureDialogLib )
3253 {
3254 Reference< deployment::XPackage > xScriptPackage;
3255
3256 if( rPackage.is() )
3257 {
3258 const Reference< deployment::XPackageTypeInfo > xPackageTypeInfo = rPackage->getPackageType();
3259 OUString aMediaType = xPackageTypeInfo->getMediaType();
3260 if ( aMediaType == sBasicLibMediaType )
3261 {
3262 xScriptPackage = rPackage;
3263 }
3264 else if ( aMediaType == sDialogLibMediaType )
3265 {
3266 rbPureDialogLib = true;
3267 xScriptPackage = rPackage;
3268 }
3269 }
3270
3271 return xScriptPackage;
3272 }
3273
3274 Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextUserScriptPackage( bool& rbPureDialogLib )
3275 {
3276 Reference< deployment::XPackage > xScriptPackage;
3277
3278 if( !m_bUserPackagesLoaded )
3279 {
3280 try
3281 {
3282 Reference< XExtensionManager > xManager = ExtensionManager::get( m_xContext );
3283 m_aUserPackagesSeq = xManager->getDeployedExtensions(u"user"_ustr,
3284 Reference< task::XAbortChannel >(),
3285 Reference< ucb::XCommandEnvironment >() );
3286 }
3287 catch(const css::uno::DeploymentException& )
3288 {
3289 // Special Office installations may not contain deployment code
3290 m_eState = END_REACHED;
3291 return xScriptPackage;
3292 }
3293
3294 m_bUserPackagesLoaded = true;
3295 }
3296
3297 if( m_iUserPackage == m_aUserPackagesSeq.getLength() )
3298 {
3299 m_eState = SHARED_EXTENSIONS; // Later: SHARED_MODULE
3300 }
3301 else
3302 {
3303 if( m_pScriptSubPackageIterator == nullptr )
3304 {
3305 Reference<deployment::XPackage> xPackage = m_aUserPackagesSeq[m_iUserPackage];
3306 SAL_WARN_IF(
3307 !xPackage.is(), "basic",
3308 ("ScriptExtensionIterator::implGetNextUserScriptPackage():"
3309 " Invalid package"));
3310 m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
3311 }
3312
3313 xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
3314 if( !xScriptPackage.is() )
3315 {
3316 delete m_pScriptSubPackageIterator;
3317 m_pScriptSubPackageIterator = nullptr;
3318 m_iUserPackage++;
3319 }
3320 }
3321
3322 return xScriptPackage;
3323 }
3324
3325 Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextSharedScriptPackage( bool& rbPureDialogLib )
3326 {
3327 Reference< deployment::XPackage > xScriptPackage;
3328
3329 if( !m_bSharedPackagesLoaded )
3330 {
3331 try
3332 {
3333 Reference< XExtensionManager > xSharedManager = ExtensionManager::get( m_xContext );
3334 m_aSharedPackagesSeq = xSharedManager->getDeployedExtensions(u"shared"_ustr,
3335 Reference< task::XAbortChannel >(),
3336 Reference< ucb::XCommandEnvironment >() );
3337 }
3338 catch(const css::uno::DeploymentException& )
3339 {
3340 // Special Office installations may not contain deployment code
3341 return xScriptPackage;
3342 }
3343
3344 m_bSharedPackagesLoaded = true;
3345 }
3346
3347 if( m_iSharedPackage == m_aSharedPackagesSeq.getLength() )
3348 {
3349 m_eState = BUNDLED_EXTENSIONS;
3350 }
3351 else
3352 {
3353 if( m_pScriptSubPackageIterator == nullptr )
3354 {
3355 Reference<deployment::XPackage> xPackage = m_aSharedPackagesSeq[m_iSharedPackage];
3356 SAL_WARN_IF(
3357 !xPackage.is(), "basic",
3358 ("ScriptExtensionIterator::implGetNextSharedScriptPackage():"
3359 " Invalid package"));
3360 m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
3361 }
3362
3363 xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
3364 if( !xScriptPackage.is() )
3365 {
3366 delete m_pScriptSubPackageIterator;
3367 m_pScriptSubPackageIterator = nullptr;
3368 m_iSharedPackage++;
3369 }
3370 }
3371
3372 return xScriptPackage;
3373 }
3374
3375 Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextBundledScriptPackage( bool& rbPureDialogLib )
3376 {
3377 Reference< deployment::XPackage > xScriptPackage;
3378
3379 if( !m_bBundledPackagesLoaded )
3380 {
3381 try
3382 {
3383 Reference< XExtensionManager > xManager = ExtensionManager::get( m_xContext );
3384 m_aBundledPackagesSeq = xManager->getDeployedExtensions(u"bundled"_ustr,
3385 Reference< task::XAbortChannel >(),
3386 Reference< ucb::XCommandEnvironment >() );
3387 }
3388 catch(const css::uno::DeploymentException& )
3389 {
3390 // Special Office installations may not contain deployment code
3391 return xScriptPackage;
3392 }
3393
3394 m_bBundledPackagesLoaded = true;
3395 }
3396
3397 if( m_iBundledPackage == m_aBundledPackagesSeq.getLength() )
3398 {
3399 m_eState = END_REACHED;
3400 }
3401 else
3402 {
3403 if( m_pScriptSubPackageIterator == nullptr )
3404 {
3405 Reference<deployment::XPackage> xPackage = m_aBundledPackagesSeq[m_iBundledPackage];
3406 SAL_WARN_IF(
3407 !xPackage.is(), "basic",
3408 ("ScriptExtensionIterator::implGetNextBundledScriptPackage():"
3409 " Invalid package"));
3410 m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
3411 }
3412
3413 xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
3414 if( !xScriptPackage.is() )
3415 {
3416 delete m_pScriptSubPackageIterator;
3417 m_pScriptSubPackageIterator = nullptr;
3418 m_iBundledPackage++;
3419 }
3420 }
3421
3422 return xScriptPackage;
3423 }
3424
3425 } // namespace basic
3426
3427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */