LibreOffice Module basic (master) 1
basmgr.cxx
Go to the documentation of this file.
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 <utility>
21#include <vcl/errinf.hxx>
22#include <tools/stream.hxx>
23#include <sot/storage.hxx>
24#include <tools/urlobj.hxx>
25#include <svl/hint.hxx>
26#include <basic/sbx.hxx>
27#include <basic/sbmeth.hxx>
28#include <sot/storinfo.hxx>
30#include <tools/debug.hxx>
32#include <basic/sbmod.hxx>
34#include <sal/log.hxx>
35#include <o3tl/string_view.hxx>
36
37#include <basic/sberrors.hxx>
38#include <basic/sbuno.hxx>
39#include <basic/basmgr.hxx>
40#include <global.hxx>
41#include <com/sun/star/script/XLibraryContainer.hpp>
42#include <com/sun/star/script/XPersistentLibraryContainer.hpp>
43
44#include <scriptcont.hxx>
45
46#include <memory>
47#include <vector>
48
49#define LIB_SEP 0x01
50#define LIBINFO_SEP 0x02
51#define LIBINFO_ID 0x1491
52#define PASSWORD_MARKER 0x31452134
53
54
55// Library API, implemented for XML import/export
56
57#include <com/sun/star/container/XNameContainer.hpp>
58#include <com/sun/star/container/XContainer.hpp>
59#include <com/sun/star/script/XStarBasicAccess.hpp>
60#include <com/sun/star/script/XStarBasicModuleInfo.hpp>
61#include <com/sun/star/script/XStarBasicDialogInfo.hpp>
62#include <com/sun/star/script/XStarBasicLibraryInfo.hpp>
63#include <com/sun/star/script/XLibraryContainerPassword.hpp>
64#include <com/sun/star/script/ModuleInfo.hpp>
65#include <com/sun/star/script/vba/XVBACompatibility.hpp>
66#include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
67#include <com/sun/star/ucb/ContentCreationException.hpp>
68
70
72using namespace com::sun::star;
73using namespace com::sun::star::script;
74using namespace cppu;
75
76typedef WeakImplHelper< container::XNameContainer > NameContainerHelper;
77typedef WeakImplHelper< script::XStarBasicModuleInfo > ModuleInfoHelper;
78typedef WeakImplHelper< script::XStarBasicAccess > StarBasicAccessHelper;
79
80// Version 1
81// sal_uInt32 nEndPos
82// sal_uInt16 nId
83// sal_uInt16 nVer
84// bool bDoLoad
85// String LibName
86// String AbsStorageName
87// String RelStorageName
88// Version 2
89// + bool bReference
90
91constexpr OUStringLiteral szStdLibName = u"Standard";
92constexpr OUStringLiteral szBasicStorage = u"StarBASIC";
93constexpr OUStringLiteral szOldManagerStream = u"BasicManager";
94constexpr OUStringLiteral szManagerStream = u"BasicManager2";
95constexpr OUStringLiteral szImbedded = u"LIBIMBEDDED";
96constexpr OStringLiteral szCryptingKey = "CryptedBasic";
97
98
99const StreamMode eStreamReadMode = StreamMode::READ | StreamMode::NOCREATE | StreamMode::SHARE_DENYALL;
100const StreamMode eStorageReadMode = StreamMode::READ | StreamMode::SHARE_DENYWRITE;
101
102// BasMgrContainerListenerImpl
103
104
105typedef ::cppu::WeakImplHelper< container::XContainerListener > ContainerListenerHelper;
106
108{
110 OUString maLibName; // empty -> no lib, but lib container
111
112public:
113 BasMgrContainerListenerImpl( BasicManager* pMgr, OUString aLibName )
114 : mpMgr( pMgr )
115 , maLibName(std::move( aLibName )) {}
116
117 static void insertLibraryImpl( const uno::Reference< script::XLibraryContainer >& xScriptCont, BasicManager* pMgr,
118 const uno::Any& aLibAny, const OUString& aLibName );
119 static void addLibraryModulesImpl( BasicManager const * pMgr, const uno::Reference< container::XNameAccess >& xLibNameAccess,
120 std::u16string_view aLibName );
121
122
123 // XEventListener
124 virtual void SAL_CALL disposing( const lang::EventObject& Source ) override;
125
126 // XContainerListener
127 virtual void SAL_CALL elementInserted( const container::ContainerEvent& Event ) override;
128 virtual void SAL_CALL elementReplaced( const container::ContainerEvent& Event ) override;
129 virtual void SAL_CALL elementRemoved( const container::ContainerEvent& Event ) override;
130};
131
132
133// BasMgrContainerListenerImpl
134
135
136void BasMgrContainerListenerImpl::insertLibraryImpl( const uno::Reference< script::XLibraryContainer >& xScriptCont,
137 BasicManager* pMgr, const uno::Any& aLibAny, const OUString& aLibName )
138{
140 aLibAny >>= xLibNameAccess;
141
142 if( !pMgr->GetLib( aLibName ) )
143 {
144 StarBASIC* pLib =
145 pMgr->CreateLibForLibContainer( aLibName, xScriptCont );
146 DBG_ASSERT( pLib, "XML Import: Basic library could not be created");
147 }
148
149 uno::Reference< container::XContainer> xLibContainer( xLibNameAccess, uno::UNO_QUERY );
150 if( xLibContainer.is() )
151 {
152 // Register listener for library
154 = new BasMgrContainerListenerImpl( pMgr, aLibName );
155 xLibContainer->addContainerListener( xLibraryListener );
156 }
157
158 if( xScriptCont->isLibraryLoaded( aLibName ) )
159 {
160 addLibraryModulesImpl( pMgr, xLibNameAccess, aLibName );
161 }
162}
163
164
166 const uno::Reference< container::XNameAccess >& xLibNameAccess, std::u16string_view aLibName )
167{
168 uno::Sequence< OUString > aModuleNames = xLibNameAccess->getElementNames();
169 sal_Int32 nModuleCount = aModuleNames.getLength();
170
171 StarBASIC* pLib = pMgr->GetLib( aLibName );
172 DBG_ASSERT( pLib, "BasMgrContainerListenerImpl::addLibraryModulesImpl: Unknown lib!");
173 if( !pLib )
174 return;
175
176 const OUString* pNames = aModuleNames.getConstArray();
177 for( sal_Int32 j = 0 ; j < nModuleCount ; j++ )
178 {
179 OUString aModuleName = pNames[ j ];
180 uno::Any aElement = xLibNameAccess->getByName( aModuleName );
181 OUString aMod;
182 aElement >>= aMod;
183 uno::Reference< vba::XVBAModuleInfo > xVBAModuleInfo( xLibNameAccess, uno::UNO_QUERY );
184 if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( aModuleName ) )
185 {
186 ModuleInfo aInfo = xVBAModuleInfo->getModuleInfo( aModuleName );
187 pLib->MakeModule( aModuleName, aInfo, aMod );
188 }
189 else
190 pLib->MakeModule( aModuleName, aMod );
191 }
192
193 pLib->SetModified( false );
194}
195
196
197// XEventListener
198
199
200void SAL_CALL BasMgrContainerListenerImpl::disposing( const lang::EventObject& ) {}
201
202// XContainerListener
203
204
205void SAL_CALL BasMgrContainerListenerImpl::elementInserted( const container::ContainerEvent& Event )
206{
207 bool bLibContainer = maLibName.isEmpty();
208 OUString aName;
209 Event.Accessor >>= aName;
210
211 if( bLibContainer )
212 {
213 uno::Reference< script::XLibraryContainer > xScriptCont( Event.Source, uno::UNO_QUERY );
214 if (xScriptCont.is())
215 insertLibraryImpl(xScriptCont, mpMgr, Event.Element, aName);
217 if ( pLib )
218 {
219 uno::Reference< vba::XVBACompatibility > xVBACompat( xScriptCont, uno::UNO_QUERY );
220 if ( xVBACompat.is() )
221 pLib->SetVBAEnabled( xVBACompat->getVBACompatibilityMode() );
222 }
223 }
224 else
225 {
226
228 DBG_ASSERT( pLib, "BasMgrContainerListenerImpl::elementInserted: Unknown lib!");
229 if( pLib )
230 {
231 SbModule* pMod = pLib->FindModule( aName );
232 if( !pMod )
233 {
234 OUString aMod;
235 Event.Element >>= aMod;
236 uno::Reference< vba::XVBAModuleInfo > xVBAModuleInfo( Event.Source, uno::UNO_QUERY );
237 if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( aName ) )
238 {
239 ModuleInfo aInfo = xVBAModuleInfo->getModuleInfo( aName );
240 pLib->MakeModule( aName, aInfo, aMod );
241 }
242 else
243 pLib->MakeModule( aName, aMod );
244 pLib->SetModified( false );
245 }
246 }
247 }
248}
249
250
251void SAL_CALL BasMgrContainerListenerImpl::elementReplaced( const container::ContainerEvent& Event )
252{
253 OUString aName;
254 Event.Accessor >>= aName;
255
256 // Replace not possible for library container
257 DBG_ASSERT( !maLibName.isEmpty(), "library container fired elementReplaced()");
258
260 if( !pLib )
261 return;
262
263 SbModule* pMod = pLib->FindModule( aName );
264 OUString aMod;
265 Event.Element >>= aMod;
266
267 if( pMod )
268 pMod->SetSource32( aMod );
269 else
270 pLib->MakeModule( aName, aMod );
271
272 pLib->SetModified( false );
273}
274
275
276void SAL_CALL BasMgrContainerListenerImpl::elementRemoved( const container::ContainerEvent& Event )
277{
278 OUString aName;
279 Event.Accessor >>= aName;
280
281 bool bLibContainer = maLibName.isEmpty();
282 if( bLibContainer )
283 {
285 if( pLib )
286 {
287 sal_uInt16 nLibId = mpMgr->GetLibId( aName );
288 mpMgr->RemoveLib( nLibId, false );
289 }
290 }
291 else
292 {
294 SbModule* pMod = pLib ? pLib->FindModule( aName ) : nullptr;
295 if( pMod )
296 {
297 pLib->Remove( pMod );
298 pLib->SetModified( false );
299 }
300 }
301}
302
304{
305 nErrorId = nId;
306 nReason = nR;
307}
308
310{
311 nErrorId = rErr.nErrorId;
312 nReason = rErr.nReason;
313}
314
315
317{
318private:
320 OUString aLibName;
321 OUString aStorageName; // string is sufficient, unique at runtime
323 OUString aPassword;
324
327
328 // Lib represents library in new UNO library container
329 uno::Reference< script::XLibraryContainer > mxScriptCont;
330
331public:
332 BasicLibInfo();
333
334 bool IsReference() const { return bReference; }
335 void SetReference(bool b) { bReference = b; }
336
337 bool IsExtern() const { return aStorageName != szImbedded; }
338
339 void SetStorageName( const OUString& rName ) { aStorageName = rName; }
340 const OUString& GetStorageName() const { return aStorageName; }
341
342 void SetRelStorageName( const OUString& rN ) { aRelStorageName = rN; }
343 const OUString& GetRelStorageName() const { return aRelStorageName; }
344
346 {
347 if( mxScriptCont.is() && mxScriptCont->hasByName( aLibName ) &&
348 !mxScriptCont->isLibraryLoaded( aLibName ) )
349 return StarBASICRef();
350 return xLib;
351 }
353 void SetLib( StarBASIC* pBasic ) { xLib = pBasic; }
354
355 const OUString& GetLibName() const { return aLibName; }
356 void SetLibName( const OUString& rName ) { aLibName = rName; }
357
358 // Only temporary for Load/Save
359 bool DoLoad() const { return bDoLoad; }
360
361 bool HasPassword() const { return !aPassword.isEmpty(); }
362 const OUString& GetPassword() const { return aPassword; }
363 void SetPassword( const OUString& rNewPassword )
364 { aPassword = rNewPassword; }
365
366 static BasicLibInfo* Create( SotStorageStream& rSStream );
367
368 const uno::Reference< script::XLibraryContainer >& GetLibraryContainer() const
369 { return mxScriptCont; }
370 void SetLibraryContainer( const uno::Reference< script::XLibraryContainer >& xScriptCont )
371 { mxScriptCont = xScriptCont; }
372};
373
374
376 : aStorageName(szImbedded)
377 , aRelStorageName(szImbedded)
378 , bDoLoad(false)
379 , bReference(false)
380{
381}
382
384{
385 BasicLibInfo* pInfo = new BasicLibInfo;
386
387 sal_uInt32 nEndPos;
388 sal_uInt16 nId;
389 sal_uInt16 nVer;
390
391 rSStream.ReadUInt32( nEndPos );
392 rSStream.ReadUInt16( nId );
393 rSStream.ReadUInt16( nVer );
394
395 DBG_ASSERT( nId == LIBINFO_ID, "No BasicLibInfo?!" );
396 if( nId == LIBINFO_ID )
397 {
398 // Reload?
399 bool bDoLoad;
400 rSStream.ReadCharAsBool( bDoLoad );
401 pInfo->bDoLoad = bDoLoad;
402
403 // The name of the lib...
404 OUString aName = rSStream.ReadUniOrByteString(rSStream.GetStreamCharSet());
405 pInfo->SetLibName( aName );
406
407 // Absolute path...
408 OUString aStorageName = rSStream.ReadUniOrByteString(rSStream.GetStreamCharSet());
410
411 // Relative path...
412 OUString aRelStorageName = rSStream.ReadUniOrByteString(rSStream.GetStreamCharSet());
414
415 if ( nVer >= 2 )
416 {
417 bool bReferenz;
418 rSStream.ReadCharAsBool( bReferenz );
419 pInfo->SetReference(bReferenz);
420 }
421
422 rSStream.Seek( nEndPos );
423 }
424 return pInfo;
425}
426
427BasicManager::BasicManager( SotStorage& rStorage, std::u16string_view rBaseURL, StarBASIC* pParentFromStdLib, OUString const * pLibPath, bool bDocMgr ) : mbDocMgr( bDocMgr )
428{
429 if( pLibPath )
430 {
431 aBasicLibPath = *pLibPath;
432 }
433 OUString aStorName( rStorage.GetName() );
435
436
437 // If there is no Manager Stream, no further actions are necessary
438 if ( rStorage.IsStream( szManagerStream ) )
439 {
440 LoadBasicManager( rStorage, rBaseURL );
441 // StdLib contains Parent:
442 StarBASIC* pStdLib = GetStdLib();
443 DBG_ASSERT( pStdLib, "Standard-Lib not loaded?" );
444 if ( !pStdLib )
445 {
446 // Should never happen, but if it happens we won't crash...
447 pStdLib = new StarBASIC( nullptr, mbDocMgr );
448
449 if (maLibs.empty())
451
452 BasicLibInfo& rStdLibInfo = *maLibs.front();
453
454 rStdLibInfo.SetLib( pStdLib );
455 StarBASICRef xStdLib = rStdLibInfo.GetLib();
456 xStdLib->SetName( szStdLibName );
457 rStdLibInfo.SetLibName( szStdLibName );
459 xStdLib->SetModified( false );
460 }
461 else
462 {
463 pStdLib->SetParent( pParentFromStdLib );
464 // The other get StdLib as parent:
465
466 for ( sal_uInt16 nBasic = 1; nBasic < GetLibCount(); nBasic++ )
467 {
468 StarBASIC* pBasic = GetLib( nBasic );
469 if ( pBasic )
470 {
471 pStdLib->Insert( pBasic );
473 }
474 }
475 // Modified through insert
476 pStdLib->SetModified( false );
477 }
478 }
479 else
480 {
481 ImpCreateStdLib( pParentFromStdLib );
482 if ( rStorage.IsStream( szOldManagerStream ) )
483 LoadOldBasicManager( rStorage );
484 }
485}
486
487static void copyToLibraryContainer( StarBASIC* pBasic, const LibraryContainerInfo& rInfo )
488{
489 uno::Reference< script::XLibraryContainer > xScriptCont( rInfo.mxScriptCont );
490 if ( !xScriptCont.is() )
491 return;
492
493 OUString aLibName = pBasic->GetName();
494 if( !xScriptCont->hasByName( aLibName ) )
495 xScriptCont->createLibrary( aLibName );
496
497 uno::Any aLibAny = xScriptCont->getByName( aLibName );
498 uno::Reference< container::XNameContainer > xLib;
499 aLibAny >>= xLib;
500 if ( !xLib.is() )
501 return;
502
503 for ( const auto& pModule: pBasic->GetModules() )
504 {
505 OUString aModName = pModule->GetName();
506 if( !xLib->hasByName( aModName ) )
507 {
508 OUString aSource = pModule->GetSource32();
509 uno::Any aSourceAny;
510 aSourceAny <<= aSource;
511 xLib->insertByName( aModName, aSourceAny );
512 }
513 }
514}
515
516const uno::Reference< script::XPersistentLibraryContainer >& BasicManager::GetDialogLibraryContainer() const
517{
519}
520
521const uno::Reference< script::XPersistentLibraryContainer >& BasicManager::GetScriptLibraryContainer() const
522{
524}
525
527{
528 maContainerInfo = rInfo;
529
530 uno::Reference< script::XLibraryContainer > xScriptCont( maContainerInfo.mxScriptCont );
531 if( xScriptCont.is() )
532 {
533 // Register listener for lib container
534 uno::Reference< container::XContainerListener > xLibContainerListener
535 = new BasMgrContainerListenerImpl( this, "" );
536
537 uno::Reference< container::XContainer> xLibContainer( xScriptCont, uno::UNO_QUERY );
538 xLibContainer->addContainerListener( xLibContainerListener );
539
540 const uno::Sequence< OUString > aScriptLibNames = xScriptCont->getElementNames();
541
542 if( aScriptLibNames.hasElements() )
543 {
544 for(const auto& rScriptLibName : aScriptLibNames)
545 {
546 uno::Any aLibAny = xScriptCont->getByName( rScriptLibName );
547
548 if ( rScriptLibName == "Standard" || rScriptLibName == "VBAProject")
549 xScriptCont->loadLibrary( rScriptLibName );
550
552 ( xScriptCont, this, aLibAny, rScriptLibName );
553 }
554 }
555 else
556 {
557 // No libs? Maybe an 5.2 document already loaded
558 for (auto const& rpBasLibInfo : maLibs)
559 {
560 StarBASIC* pLib = rpBasLibInfo->GetLib().get();
561 if( !pLib )
562 {
563 bool bLoaded = ImpLoadLibrary( rpBasLibInfo.get(), nullptr );
564 if( bLoaded )
565 pLib = rpBasLibInfo->GetLib().get();
566 }
567 if( pLib )
568 {
570 if (rpBasLibInfo->HasPassword())
571 {
572 basic::SfxScriptLibraryContainer* pOldBasicPassword =
574 if( pOldBasicPassword )
575 {
576 pOldBasicPassword->setLibraryPassword(
577 pLib->GetName(), rpBasLibInfo->GetPassword() );
578 }
579 }
580 }
581 }
582 }
583 }
584
587}
588
589BasicManager::BasicManager( StarBASIC* pSLib, OUString const * pLibPath, bool bDocMgr ) : mbDocMgr( bDocMgr )
590{
591 DBG_ASSERT( pSLib, "BasicManager cannot be created with a NULL-Pointer!" );
592
593 if( pLibPath )
594 {
595 aBasicLibPath = *pLibPath;
596 }
597 BasicLibInfo* pStdLibInfo = CreateLibInfo();
598 pStdLibInfo->SetLib( pSLib );
599 StarBASICRef xStdLib = pStdLibInfo->GetLib();
600 xStdLib->SetName(szStdLibName);
601 pStdLibInfo->SetLibName(szStdLibName );
603
604 // Save is only necessary if basic has changed
605 xStdLib->SetModified( false );
606}
607
608void BasicManager::ImpMgrNotLoaded( const OUString& rStorageName )
609{
610 // pErrInf is only destroyed if the error os processed by an
611 // ErrorHandler
612 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_MGROPEN, rStorageName, DialogMask::ButtonsOk );
613 aErrors.emplace_back(*pErrInf, BasicErrorReason::OPENMGRSTREAM);
614
615 // Create a stdlib otherwise we crash!
616 BasicLibInfo* pStdLibInfo = CreateLibInfo();
617 pStdLibInfo->SetLib( new StarBASIC( nullptr, mbDocMgr ) );
618 StarBASICRef xStdLib = pStdLibInfo->GetLib();
619 xStdLib->SetName( szStdLibName );
620 pStdLibInfo->SetLibName( szStdLibName );
622 xStdLib->SetModified( false );
623}
624
625
626void BasicManager::ImpCreateStdLib( StarBASIC* pParentFromStdLib )
627{
628 BasicLibInfo* pStdLibInfo = CreateLibInfo();
629 StarBASIC* pStdLib = new StarBASIC( pParentFromStdLib, mbDocMgr );
630 pStdLibInfo->SetLib( pStdLib );
631 pStdLib->SetName( szStdLibName );
632 pStdLibInfo->SetLibName( szStdLibName );
634}
635
636void BasicManager::LoadBasicManager( SotStorage& rStorage, std::u16string_view rBaseURL )
637{
639
640 OUString aStorName( rStorage.GetName() );
641 // #i13114 removed, DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
642
643 if ( !xManagerStream.is() || xManagerStream->GetError() || ( xManagerStream->TellEnd() == 0 ) )
644 {
645 ImpMgrNotLoaded( aStorName );
646 return;
647 }
648
650 // #i13114 removed, DBG_ASSERT(aStorageName.Len() != 0, "Bad storage name");
651
652 OUString aRealStorageName = maStorageName; // for relative paths, can be modified through BaseURL
653
654 if ( !rBaseURL.empty() )
655 {
656 INetURLObject aObj( rBaseURL );
657 if ( aObj.GetProtocol() == INetProtocol::File )
658 {
659 aRealStorageName = aObj.PathToFileName();
660 }
661 }
662
663 xManagerStream->SetBufferSize( 1024 );
664 xManagerStream->Seek( STREAM_SEEK_TO_BEGIN );
665
666 sal_uInt32 nEndPos;
667 xManagerStream->ReadUInt32( nEndPos );
668
669 sal_uInt16 nLibs;
670 xManagerStream->ReadUInt16( nLibs );
671 // Plausibility!
672 if( nLibs & 0xF000 )
673 {
674 SAL_WARN( "basic", "BasicManager-Stream defect!" );
675 return;
676 }
677 const size_t nMinBasicLibSize(8);
678 const size_t nMaxPossibleLibs = xManagerStream->remainingSize() / nMinBasicLibSize;
679 if (nLibs > nMaxPossibleLibs)
680 {
681 SAL_WARN("basic", "Parsing error: " << nMaxPossibleLibs <<
682 " max possible entries, but " << nLibs << " claimed, truncating");
683 nLibs = nMaxPossibleLibs;
684 }
685 for (sal_uInt16 nL = 0; nL < nLibs; ++nL)
686 {
687 BasicLibInfo* pInfo = BasicLibInfo::Create( *xManagerStream );
688
689 // Correct absolute pathname if relative is existing.
690 // Always try relative first if there are two stands on disk
691 if ( !pInfo->GetRelStorageName().isEmpty() && pInfo->GetRelStorageName() != szImbedded )
692 {
693 INetURLObject aObj( aRealStorageName, INetProtocol::File );
694 aObj.removeSegment();
695 bool bWasAbsolute = false;
696 aObj = aObj.smartRel2Abs( pInfo->GetRelStorageName(), bWasAbsolute );
697
698 //*** TODO: Replace if still necessary
699 //*** TODO-End
700 if ( ! aBasicLibPath.isEmpty() )
701 {
702 // Search lib in path
703 OUString aSearchFile = pInfo->GetRelStorageName();
704 OUString aSearchFileOldFormat(aSearchFile);
705 SvtPathOptions aPathCFG;
706 if( aPathCFG.SearchFile( aSearchFileOldFormat, SvtPathOptions::Paths::Basic ) )
707 {
708 pInfo->SetStorageName( aSearchFile );
709 }
710 }
711 }
712
713 maLibs.push_back(std::unique_ptr<BasicLibInfo>(pInfo));
714 // Libs from external files should be loaded only when necessary.
715 // But references are loaded at once, otherwise some big customers get into trouble
716 if ( pInfo->DoLoad() &&
717 ( !pInfo->IsExtern() || pInfo->IsReference()))
718 {
719 ImpLoadLibrary( pInfo, &rStorage );
720 }
721 }
722
723 xManagerStream->Seek( nEndPos );
724 xManagerStream->SetBufferSize( 0 );
725 xManagerStream.clear();
726}
727
729{
731
732 OUString aStorName( rStorage.GetName() );
733 DBG_ASSERT( aStorName.getLength(), "No Storage Name!" );
734
735 if ( !xManagerStream.is() || xManagerStream->GetError() || ( xManagerStream->TellEnd() == 0 ) )
736 {
737 ImpMgrNotLoaded( aStorName );
738 return;
739 }
740
741 xManagerStream->SetBufferSize( 1024 );
742 xManagerStream->Seek( STREAM_SEEK_TO_BEGIN );
743 sal_uInt32 nBasicStartOff, nBasicEndOff;
744 xManagerStream->ReadUInt32( nBasicStartOff );
745 xManagerStream->ReadUInt32( nBasicEndOff );
746
747 DBG_ASSERT( !xManagerStream->GetError(), "Invalid Manager-Stream!" );
748
749 xManagerStream->Seek( nBasicStartOff );
750 if (!ImplLoadBasic( *xManagerStream, maLibs.front()->GetLibRef() ))
751 {
752 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_MGROPEN, aStorName, DialogMask::ButtonsOk );
753 aErrors.emplace_back(*pErrInf, BasicErrorReason::OPENMGRSTREAM);
754 // and it proceeds ...
755 }
756 xManagerStream->Seek( nBasicEndOff+1 ); // +1: 0x00 as separator
757 OUString aLibs = xManagerStream->ReadUniOrByteString(xManagerStream->GetStreamCharSet());
758 xManagerStream->SetBufferSize( 0 );
759 xManagerStream.clear(); // Close stream
760
761 if ( aLibs.isEmpty() )
762 return;
763
764 INetURLObject aCurStorage( aStorName, INetProtocol::File );
765 sal_Int32 nLibPos {0};
766 do {
767 const OUString aLibInfo(aLibs.getToken(0, LIB_SEP, nLibPos));
768 sal_Int32 nInfoPos {0};
769 const OUString aLibName( aLibInfo.getToken( 0, LIBINFO_SEP, nInfoPos ) );
770 DBG_ASSERT( nInfoPos >= 0, "Invalid Lib-Info!" );
771 const OUString aLibAbsStorageName( aLibInfo.getToken( 0, LIBINFO_SEP, nInfoPos ) );
772 // TODO: fail also here if there are no more tokens?
773 const OUString aLibRelStorageName( aLibInfo.getToken( 0, LIBINFO_SEP, nInfoPos ) );
774 DBG_ASSERT( nInfoPos < 0, "Invalid Lib-Info!" );
775 INetURLObject aLibAbsStorage( aLibAbsStorageName, INetProtocol::File );
776
777 INetURLObject aLibRelStorage( aStorName );
778 aLibRelStorage.removeSegment();
779 bool bWasAbsolute = false;
780 aLibRelStorage = aLibRelStorage.smartRel2Abs( aLibRelStorageName, bWasAbsolute);
781 DBG_ASSERT(!bWasAbsolute, "RelStorageName was absolute!" );
782
783 tools::SvRef<SotStorage> xStorageRef;
784 if ( aLibAbsStorage == aCurStorage || aLibRelStorageName == szImbedded )
785 {
786 xStorageRef = &rStorage;
787 }
788 else
789 {
790 xStorageRef = new SotStorage( false, aLibAbsStorage.GetMainURL
792 if ( xStorageRef->GetError() != ERRCODE_NONE )
793 xStorageRef = new SotStorage( false, aLibRelStorage.
795 }
796 if ( xStorageRef.is() )
797 {
798 AddLib( *xStorageRef, aLibName, false );
799 }
800 else
801 {
802 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD, aStorName, DialogMask::ButtonsOk );
803 aErrors.emplace_back(*pErrInf, BasicErrorReason::STORAGENOTFOUND);
804 }
805 } while (nLibPos>=0);
806}
807
809{
810 // Notify listener if something needs to be saved
811 Broadcast( SfxHint( SfxHintId::Dying) );
812}
813
814bool BasicManager::HasExeCode( std::u16string_view sLib )
815{
816 StarBASIC* pLib = GetLib(sLib);
817 if ( pLib )
818 {
819 for (const auto& pModule: pLib->GetModules())
820 {
821 if (pModule->HasExeCode())
822 return true;
823 }
824 }
825 return false;
826}
827
829{
830 maLibs.push_back(std::make_unique<BasicLibInfo>());
831 return maLibs.back().get();
832}
833
835{
836 try {
837 DBG_ASSERT( pLibInfo, "LibInfo!?" );
838
839 OUString aStorageName( pLibInfo->GetStorageName() );
840 if ( aStorageName.isEmpty() || aStorageName == szImbedded )
841 {
842 aStorageName = GetStorageName();
843 }
845 // The current must not be opened again...
846 if ( pCurStorage )
847 {
848 OUString aStorName( pCurStorage->GetName() );
849 // #i13114 removed, DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
850
851 INetURLObject aCurStorageEntry(aStorName, INetProtocol::File);
852 // #i13114 removed, DBG_ASSERT(aCurStorageEntry.GetMainURL( INetURLObject::DecodeMechanism::NONE ).Len() != 0, "Bad storage name");
853
854 INetURLObject aStorageEntry(aStorageName, INetProtocol::File);
855 // #i13114 removed, DBG_ASSERT(aCurStorageEntry.GetMainURL( INetURLObject::DecodeMechanism::NONE ).Len() != 0, "Bad storage name");
856
857 if ( aCurStorageEntry == aStorageEntry )
858 {
859 xStorage = pCurStorage;
860 }
861 }
862
863 if ( !xStorage.is() )
864 {
865 xStorage = new SotStorage( false, aStorageName, eStorageReadMode );
866 }
867 tools::SvRef<SotStorage> xBasicStorage = xStorage->OpenSotStorage( szBasicStorage, eStorageReadMode, false );
868
869 if ( !xBasicStorage.is() || xBasicStorage->GetError() )
870 {
871 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_MGROPEN, xStorage->GetName(), DialogMask::ButtonsOk );
872 aErrors.emplace_back(*pErrInf, BasicErrorReason::OPENLIBSTORAGE);
873 }
874 else
875 {
876 // In the Basic-Storage every lib is in a Stream...
877 tools::SvRef<SotStorageStream> xBasicStream = xBasicStorage->OpenSotStream( pLibInfo->GetLibName(), eStreamReadMode );
878 if ( !xBasicStream.is() || xBasicStream->GetError() )
879 {
880 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD , pLibInfo->GetLibName(), DialogMask::ButtonsOk );
881 aErrors.emplace_back(*pErrInf, BasicErrorReason::OPENLIBSTREAM);
882 }
883 else
884 {
885 bool bLoaded = false;
886 if ( xBasicStream->TellEnd() != 0 )
887 {
888 if ( !pLibInfo->GetLib().is() )
889 {
890 pLibInfo->SetLib( new StarBASIC( GetStdLib(), mbDocMgr ) );
891 }
892 xBasicStream->SetBufferSize( 1024 );
893 xBasicStream->Seek( STREAM_SEEK_TO_BEGIN );
894 bLoaded = ImplLoadBasic( *xBasicStream, pLibInfo->GetLibRef() );
895 xBasicStream->SetBufferSize( 0 );
896 StarBASICRef xStdLib = pLibInfo->GetLib();
897 xStdLib->SetName( pLibInfo->GetLibName() );
898 xStdLib->SetModified( false );
899 xStdLib->SetFlag( SbxFlagBits::DontStore );
900 }
901 if ( !bLoaded )
902 {
903 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD, pLibInfo->GetLibName(), DialogMask::ButtonsOk );
904 aErrors.emplace_back(*pErrInf, BasicErrorReason::BASICLOADERROR);
905 }
906 else
907 {
908 // Perhaps there are additional information in the stream...
909 xBasicStream->SetCryptMaskKey(szCryptingKey);
910 xBasicStream->RefreshBuffer();
911 sal_uInt32 nPasswordMarker = 0;
912 xBasicStream->ReadUInt32( nPasswordMarker );
913 if ( ( nPasswordMarker == PASSWORD_MARKER ) && !xBasicStream->eof() )
914 {
915 OUString aPassword = xBasicStream->ReadUniOrByteString(
916 xBasicStream->GetStreamCharSet());
917 pLibInfo->SetPassword( aPassword );
918 }
919 xBasicStream->SetCryptMaskKey(OString());
920 CheckModules( pLibInfo->GetLib().get(), pLibInfo->IsReference() );
921 }
922 return bLoaded;
923 }
924 }
925 }
926 catch (const css::ucb::ContentCreationException&)
927 {
928 }
929 return false;
930}
931
933{
934 sal_uInt64 const nPos = rStrm.Tell();
935 sal_uInt32 nCreator;
936 rStrm.ReadUInt32( nCreator );
937 rStrm.Seek( nPos );
938 bool bProtected = false;
939 if ( nCreator != SBXCR_SBX )
940 {
941 // Should only be the case for encrypted Streams
942 bProtected = true;
945 }
946 return bProtected;
947}
948
949// This code is necessary to load the BASIC of Beta 1
950// TODO: Which Beta 1?
951bool BasicManager::ImplLoadBasic( SvStream& rStrm, StarBASICRef& rOldBasic ) const
952{
953 bool bProtected = ImplEncryptStream( rStrm );
955 bool bLoaded = false;
956 if( xNew.is() )
957 {
958 if( auto pNew = dynamic_cast<StarBASIC*>( xNew.get() ) )
959 {
960 // Use the Parent of the old BASICs
961 if( rOldBasic.is() )
962 {
963 pNew->SetParent( rOldBasic->GetParent() );
964 if( pNew->GetParent() )
965 {
966 pNew->GetParent()->Insert( pNew );
967 }
968 pNew->SetFlag( SbxFlagBits::ExtSearch );
969 }
970 rOldBasic = pNew;
971
972 // Fill new library container (5.2 -> 6.0)
974
975 pNew->SetModified( false );
976 bLoaded = true;
977 }
978 }
979 if ( bProtected )
980 {
981 rStrm.SetCryptMaskKey(OString());
982 }
983 return bLoaded;
984}
985
986void BasicManager::CheckModules( StarBASIC* pLib, bool bReference )
987{
988 if ( !pLib )
989 {
990 return;
991 }
992 bool bModified = pLib->IsModified();
993
994 for ( const auto& pModule: pLib->GetModules() )
995 {
996 DBG_ASSERT(pModule, "Module not received!");
997 if ( !pModule->IsCompiled() && !StarBASIC::GetErrorCode() )
998 {
999 pModule->Compile();
1000 }
1001 }
1002
1003 // #67477, AB 8.12.99 On demand compile in referenced libs should not
1004 // cause modified
1005 if( !bModified && bReference )
1006 {
1007 OSL_FAIL( "Referenced basic library is not compiled!" );
1008 pLib->SetModified( false );
1009 }
1010}
1011
1012StarBASIC* BasicManager::AddLib( SotStorage& rStorage, const OUString& rLibName, bool bReference )
1013{
1014 OUString aStorName( rStorage.GetName() );
1015 DBG_ASSERT( !aStorName.isEmpty(), "No Storage Name!" );
1016
1017 OUString aStorageName = INetURLObject(aStorName, INetProtocol::File).GetMainURL( INetURLObject::DecodeMechanism::NONE );
1018 DBG_ASSERT(!aStorageName.isEmpty(), "Bad storage name");
1019
1020 OUString aNewLibName( rLibName );
1021 while ( HasLib( aNewLibName ) )
1022 {
1023 aNewLibName += "_";
1024 }
1025 BasicLibInfo* pLibInfo = CreateLibInfo();
1026 // Use original name otherwise ImpLoadLibrary fails...
1027 pLibInfo->SetLibName( rLibName );
1028 // but doesn't work this way if name exists twice
1029 sal_uInt16 nLibId = static_cast<sal_uInt16>(maLibs.size()) - 1;
1030
1031 // Set StorageName before load because it is compared with pCurStorage
1032 pLibInfo->SetStorageName( aStorageName );
1033 bool bLoaded = ImpLoadLibrary( pLibInfo, &rStorage );
1034
1035 if ( bLoaded )
1036 {
1037 if ( aNewLibName != rLibName )
1038 {
1039 pLibInfo->SetLibName(aNewLibName);
1040 }
1041 if ( bReference )
1042 {
1043 pLibInfo->GetLib()->SetModified( false ); // Don't save in this case
1044 pLibInfo->SetRelStorageName( OUString() );
1045 pLibInfo->SetReference(true);
1046 }
1047 else
1048 {
1049 pLibInfo->GetLib()->SetModified( true ); // Must be saved after Add!
1050 pLibInfo->SetStorageName( szImbedded ); // Save in BasicManager-Storage
1051 }
1052 }
1053 else
1054 {
1055 RemoveLib( nLibId, false );
1056 pLibInfo = nullptr;
1057 }
1058
1059 return pLibInfo ? &*pLibInfo->GetLib() : nullptr;
1060
1061}
1062
1063bool BasicManager::IsReference( sal_uInt16 nLib )
1064{
1065 DBG_ASSERT( nLib < maLibs.size(), "Lib does not exist!" );
1066 if ( nLib < maLibs.size() )
1067 {
1068 return maLibs[nLib]->IsReference();
1069 }
1070 return false;
1071}
1072
1073void BasicManager::RemoveLib( sal_uInt16 nLib )
1074{
1075 // Only physical deletion if no reference
1076 RemoveLib( nLib, !IsReference( nLib ) );
1077}
1078
1079bool BasicManager::RemoveLib( sal_uInt16 nLib, bool bDelBasicFromStorage )
1080{
1081 DBG_ASSERT( nLib, "Standard-Lib cannot be removed!" );
1082
1083 DBG_ASSERT( !nLib || nLib < maLibs.size(), "Lib not found!" );
1084
1085 if( !nLib || nLib < maLibs.size() )
1086 {
1087 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_REMOVELIB, OUString(), DialogMask::ButtonsOk );
1088 aErrors.emplace_back(*pErrInf, BasicErrorReason::STDLIB);
1089 return false;
1090 }
1091
1092 auto const itLibInfo = maLibs.begin() + nLib;
1093
1094 // If one of the streams cannot be opened, this is not an error,
1095 // because BASIC was never written before...
1096 if (bDelBasicFromStorage && !(*itLibInfo)->IsReference() &&
1097 (!(*itLibInfo)->IsExtern() || SotStorage::IsStorageFile((*itLibInfo)->GetStorageName())))
1098 {
1099 tools::SvRef<SotStorage> xStorage;
1100 try
1101 {
1102 if (!(*itLibInfo)->IsExtern())
1103 {
1104 xStorage = new SotStorage(false, GetStorageName());
1105 }
1106 else
1107 {
1108 xStorage = new SotStorage(false, (*itLibInfo)->GetStorageName());
1109 }
1110 }
1111 catch (const css::ucb::ContentCreationException&)
1112 {
1113 TOOLS_WARN_EXCEPTION("basic", "BasicManager::RemoveLib:");
1114 }
1115
1116 if (xStorage.is() && xStorage->IsStorage(szBasicStorage))
1117 {
1118 tools::SvRef<SotStorage> xBasicStorage = xStorage->OpenSotStorage
1119 ( szBasicStorage, StreamMode::STD_READWRITE, false );
1120
1121 if ( !xBasicStorage.is() || xBasicStorage->GetError() )
1122 {
1123 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_REMOVELIB, OUString(), DialogMask::ButtonsOk );
1124 aErrors.emplace_back(*pErrInf, BasicErrorReason::OPENLIBSTORAGE);
1125 }
1126 else if (xBasicStorage->IsStream((*itLibInfo)->GetLibName()))
1127 {
1128 xBasicStorage->Remove((*itLibInfo)->GetLibName());
1129 xBasicStorage->Commit();
1130
1131 // If no further stream available,
1132 // delete the SubStorage.
1133 SvStorageInfoList aInfoList;
1134 xBasicStorage->FillInfoList( &aInfoList );
1135 if ( aInfoList.empty() )
1136 {
1137 xBasicStorage.clear();
1138 xStorage->Remove( szBasicStorage );
1139 xStorage->Commit();
1140 // If no further Streams or SubStorages available,
1141 // delete the Storage, too.
1142 aInfoList.clear();
1143 xStorage->FillInfoList( &aInfoList );
1144 if ( aInfoList.empty() )
1145 {
1146 //OUString aName_( xStorage->GetName() );
1147 xStorage.clear();
1148 //*** TODO: Replace if still necessary
1149 //SfxContentHelper::Kill( aName );
1150 //*** TODO-End
1151 }
1152 }
1153 }
1154 }
1155 }
1156 if ((*itLibInfo)->GetLib().is())
1157 {
1158 GetStdLib()->Remove( (*itLibInfo)->GetLib().get() );
1159 }
1160 maLibs.erase(itLibInfo);
1161 return true; // Remove was successful, del unimportant
1162}
1163
1165{
1166 return static_cast<sal_uInt16>(maLibs.size());
1167}
1168
1169StarBASIC* BasicManager::GetLib( sal_uInt16 nLib ) const
1170{
1171 DBG_ASSERT( nLib < maLibs.size(), "Lib does not exist!" );
1172 if ( nLib < maLibs.size() )
1173 {
1174 return maLibs[nLib]->GetLib().get();
1175 }
1176 return nullptr;
1177}
1178
1180{
1181 StarBASIC* pLib = GetLib( 0 );
1182 return pLib;
1183}
1184
1185StarBASIC* BasicManager::GetLib( std::u16string_view rName ) const
1186{
1187 for (auto const& rpLib : maLibs)
1188 {
1189 if (rpLib->GetLibName().equalsIgnoreAsciiCase(rName)) // Check if available...
1190 {
1191 return rpLib->GetLib().get();
1192 }
1193 }
1194 return nullptr;
1195}
1196
1197sal_uInt16 BasicManager::GetLibId( std::u16string_view rName ) const
1198{
1199 for (size_t i = 0; i < maLibs.size(); i++)
1200 {
1201 if (maLibs[i]->GetLibName().equalsIgnoreAsciiCase( rName ))
1202 {
1203 return static_cast<sal_uInt16>(i);
1204 }
1205 }
1206 return LIB_NOTFOUND;
1207}
1208
1209bool BasicManager::HasLib( std::u16string_view rName ) const
1210{
1211 for (const auto& rpLib : maLibs)
1212 {
1213 if (rpLib->GetLibName().equalsIgnoreAsciiCase(rName)) // Check if available...
1214 {
1215 return true;
1216 }
1217 }
1218 return false;
1219}
1220
1221OUString BasicManager::GetLibName( sal_uInt16 nLib )
1222{
1223 DBG_ASSERT( nLib < maLibs.size(), "Lib?!" );
1224 if ( nLib < maLibs.size() )
1225 {
1226 return maLibs[nLib]->GetLibName();
1227 }
1228 return OUString();
1229}
1230
1231bool BasicManager::LoadLib( sal_uInt16 nLib )
1232{
1233 bool bDone = false;
1234 DBG_ASSERT( nLib < maLibs.size() , "Lib?!" );
1235 if ( nLib < maLibs.size() )
1236 {
1237 BasicLibInfo& rLibInfo = *maLibs[nLib];
1238 uno::Reference< script::XLibraryContainer > xLibContainer = rLibInfo.GetLibraryContainer();
1239 if( xLibContainer.is() )
1240 {
1241 OUString aLibName = rLibInfo.GetLibName();
1242 xLibContainer->loadLibrary( aLibName );
1243 bDone = xLibContainer->isLibraryLoaded( aLibName );
1244 }
1245 else
1246 {
1247 bDone = ImpLoadLibrary( &rLibInfo, nullptr );
1248 StarBASIC* pLib = GetLib( nLib );
1249 if ( pLib )
1250 {
1251 GetStdLib()->Insert( pLib );
1252 pLib->SetFlag( SbxFlagBits::ExtSearch );
1253 }
1254 }
1255 }
1256 else
1257 {
1258 StringErrorInfo* pErrInf = new StringErrorInfo( ERRCODE_BASMGR_LIBLOAD, OUString(), DialogMask::ButtonsOk );
1259 aErrors.emplace_back(*pErrInf, BasicErrorReason::LIBNOTFOUND);
1260 }
1261 return bDone;
1262}
1263
1264StarBASIC* BasicManager::CreateLib( const OUString& rLibName )
1265{
1266 if ( GetLib( rLibName ) )
1267 {
1268 return nullptr;
1269 }
1270 BasicLibInfo* pLibInfo = CreateLibInfo();
1271 StarBASIC* pNew = new StarBASIC( GetStdLib(), mbDocMgr );
1272 GetStdLib()->Insert( pNew );
1274 pLibInfo->SetLib( pNew );
1275 pLibInfo->SetLibName( rLibName );
1276 pLibInfo->GetLib()->SetName( rLibName );
1277 return pLibInfo->GetLib().get();
1278}
1279
1280// For XML import/export:
1281StarBASIC* BasicManager::CreateLib( const OUString& rLibName, const OUString& Password,
1282 const OUString& LinkTargetURL )
1283{
1284 // Ask if lib exists because standard lib is always there
1285 StarBASIC* pLib = GetLib( rLibName );
1286 if( !pLib )
1287 {
1288 if( !LinkTargetURL.isEmpty())
1289 {
1290 try
1291 {
1292 tools::SvRef<SotStorage> xStorage = new SotStorage(false, LinkTargetURL, StreamMode::READ | StreamMode::SHARE_DENYWRITE);
1293 if (!xStorage->GetError())
1294 {
1295 pLib = AddLib(*xStorage, rLibName, true);
1296 }
1297 }
1298 catch (const css::ucb::ContentCreationException&)
1299 {
1300 TOOLS_WARN_EXCEPTION("basic", "BasicManager::RemoveLib:");
1301 }
1302 DBG_ASSERT( pLib, "XML Import: Linked basic library could not be loaded");
1303 }
1304 else
1305 {
1306 pLib = CreateLib( rLibName );
1307 if( Password.isEmpty())
1308 {
1309 BasicLibInfo* pLibInfo = FindLibInfo( pLib );
1310 pLibInfo ->SetPassword( Password );
1311 }
1312 }
1313 //ExternalSourceURL ?
1314 }
1315 return pLib;
1316}
1317
1319 const uno::Reference< script::XLibraryContainer >& xScriptCont )
1320{
1321 if ( GetLib( rLibName ) )
1322 {
1323 return nullptr;
1324 }
1325 BasicLibInfo* pLibInfo = CreateLibInfo();
1326 StarBASIC* pNew = new StarBASIC( GetStdLib(), mbDocMgr );
1327 GetStdLib()->Insert( pNew );
1329 pLibInfo->SetLib( pNew );
1330 pLibInfo->SetLibName( rLibName );
1331 pLibInfo->GetLib()->SetName( rLibName );
1332 pLibInfo->SetLibraryContainer( xScriptCont );
1333 return pNew;
1334}
1335
1336
1338{
1339 for (auto const& rpLib : maLibs)
1340 {
1341 if (rpLib->GetLib().get() == pBasic)
1342 {
1343 return rpLib.get();
1344 }
1345 }
1346 return nullptr;
1347}
1348
1349
1351{
1352 for (auto const& rpLib : maLibs)
1353 {
1354 if (rpLib->GetLib().is() && rpLib->GetLib()->IsModified())
1355 {
1356 return true;
1357 }
1358 }
1359 return false;
1360}
1361
1362
1363bool BasicManager::GetGlobalUNOConstant( const OUString& rName, uno::Any& aOut )
1364{
1365 bool bRes = false;
1366 StarBASIC* pStandardLib = GetStdLib();
1367 OSL_PRECOND( pStandardLib, "BasicManager::GetGlobalUNOConstant: no lib to read from!" );
1368 if ( pStandardLib )
1369 bRes = pStandardLib->GetUNOConstant( rName, aOut );
1370 return bRes;
1371}
1372
1373void BasicManager::SetGlobalUNOConstant( const OUString& rName, const uno::Any& _rValue, css::uno::Any* pOldValue )
1374{
1375 StarBASIC* pStandardLib = GetStdLib();
1376 OSL_PRECOND( pStandardLib, "BasicManager::SetGlobalUNOConstant: no lib to insert into!" );
1377 if ( !pStandardLib )
1378 return;
1379
1380 if (pOldValue)
1381 {
1382 // obtain the old value
1383 SbxVariable* pVariable = pStandardLib->Find( rName, SbxClassType::Object );
1384 if ( pVariable )
1385 *pOldValue = sbxToUnoValue( pVariable );
1386 }
1387 SbxObjectRef xUnoObj = GetSbUnoObject( _rValue.getValueType ().getTypeName () , _rValue );
1388 xUnoObj->SetName(rName);
1389 xUnoObj->SetFlag( SbxFlagBits::DontStore );
1390 pStandardLib->Insert( xUnoObj.get() );
1391}
1392
1393bool BasicManager::ImgVersion12PsswdBinaryLimitExceeded( std::vector< OUString >& _out_rModuleNames )
1394{
1395 try
1396 {
1397 uno::Reference< container::XNameAccess > xScripts( GetScriptLibraryContainer(), uno::UNO_QUERY_THROW );
1398 uno::Reference< script::XLibraryContainerPassword > xPassword( GetScriptLibraryContainer(), uno::UNO_QUERY_THROW );
1399
1400 const uno::Sequence< OUString > aNames( xScripts->getElementNames() );
1401 for ( auto const & scriptElementName : aNames )
1402 {
1403 if( !xPassword->isLibraryPasswordProtected( scriptElementName ) )
1404 continue;
1405
1406 StarBASIC* pBasicLib = GetLib( scriptElementName );
1407 if ( !pBasicLib )
1408 continue;
1409
1410 uno::Reference< container::XNameAccess > xScriptLibrary( xScripts->getByName( scriptElementName ), uno::UNO_QUERY_THROW );
1411 const uno::Sequence< OUString > aElementNames( xScriptLibrary->getElementNames() );
1412
1413 std::vector<OUString> aBigModules;
1414 for ( auto const & libraryElementName : aElementNames )
1415 {
1416 SbModule* pMod = pBasicLib->FindModule( libraryElementName );
1417 if ( pMod && pMod->ExceedsImgVersion12ModuleSize() )
1418 aBigModules.push_back(libraryElementName);
1419 }
1420
1421 if (!aBigModules.empty())
1422 {
1423 _out_rModuleNames.swap(aBigModules);
1424 return true;
1425 }
1426 }
1427 }
1428 catch( const uno::Exception& )
1429 {
1430 DBG_UNHANDLED_EXCEPTION("basic");
1431 }
1432 return false;
1433}
1434
1435
1436namespace
1437{
1438 SbMethod* lcl_queryMacro( BasicManager* i_manager, OUString const& i_fullyQualifiedName )
1439 {
1440 sal_Int32 nLast = 0;
1441 const OUString sLibName {i_fullyQualifiedName.getToken( 0, '.', nLast )};
1442 const OUString sModule {i_fullyQualifiedName.getToken( 0, '.', nLast )};
1443 OUString sMacro;
1444 if(nLast >= 0)
1445 {
1446 sMacro = i_fullyQualifiedName.copy(nLast);
1447 }
1448 else
1449 {
1450 sMacro = i_fullyQualifiedName;
1451 }
1452
1454 sal_uInt16 nLibCount = i_manager->GetLibCount();
1455 for ( sal_uInt16 nLib = 0; nLib < nLibCount; ++nLib )
1456 {
1457 if ( rTransliteration.isEqual( i_manager->GetLibName( nLib ), sLibName ) )
1458 {
1459 StarBASIC* pLib = i_manager->GetLib( nLib );
1460 if( !pLib )
1461 {
1462 bool const bLoaded = i_manager->LoadLib( nLib );
1463 if (bLoaded)
1464 {
1465 pLib = i_manager->GetLib( nLib );
1466 }
1467 }
1468
1469 if( pLib )
1470 {
1471 for ( const auto& pMod: pLib->GetModules() )
1472 {
1473 if ( rTransliteration.isEqual( pMod->GetName(), sModule ) )
1474 {
1475 SbMethod* pMethod = static_cast<SbMethod*>(pMod->Find( sMacro, SbxClassType::Method ));
1476 if( pMethod )
1477 {
1478 return pMethod;
1479 }
1480 }
1481 }
1482 }
1483 }
1484 }
1485 return nullptr;
1486 }
1487}
1488
1489bool BasicManager::HasMacro( OUString const& i_fullyQualifiedName ) const
1490{
1491 return ( lcl_queryMacro( const_cast< BasicManager* >( this ), i_fullyQualifiedName ) != nullptr );
1492}
1493
1494ErrCode BasicManager::ExecuteMacro( OUString const& i_fullyQualifiedName, SbxArray* i_arguments, SbxValue* i_retValue )
1495{
1496 SbMethod* pMethod = lcl_queryMacro( this, i_fullyQualifiedName );
1497 ErrCode nError = ERRCODE_NONE;
1498 if ( pMethod )
1499 {
1500 if ( i_arguments )
1501 pMethod->SetParameters( i_arguments );
1502 nError = pMethod->Call( i_retValue );
1503 }
1504 else
1506 return nError;
1507}
1508
1509ErrCode BasicManager::ExecuteMacro( OUString const& i_fullyQualifiedName, std::u16string_view i_commaSeparatedArgs, SbxValue* i_retValue )
1510{
1511 SbMethod* pMethod = lcl_queryMacro( this, i_fullyQualifiedName );
1512 if ( !pMethod )
1513 {
1515 }
1516 // arguments must be quoted
1517 OUString sQuotedArgs;
1518 OUStringBuffer sArgs( i_commaSeparatedArgs );
1519 if ( sArgs.getLength()<2 || sArgs[1] == '\"')
1520 {
1521 // no args or already quoted args
1522 sQuotedArgs = sArgs.makeStringAndClear();
1523 }
1524 else
1525 {
1526 // quote parameters
1527 sArgs.remove( 0, 1 );
1528 sArgs.remove( sArgs.getLength() - 1, 1 );
1529
1530 OUStringBuffer aBuff;
1531 OUString sArgs2 = sArgs.makeStringAndClear();
1532
1533 aBuff.append("(");
1534 if (!sArgs2.isEmpty())
1535 {
1536
1537 sal_Int32 nPos {0};
1538 for (;;)
1539 {
1540 aBuff.append( OUString::Concat("\"")
1541 + o3tl::getToken(sArgs2, 0, ',', nPos)
1542 + "\"" );
1543 if (nPos<0)
1544 break;
1545 aBuff.append( "," );
1546 }
1547 }
1548 aBuff.append( ")" );
1549
1550 sQuotedArgs = aBuff.makeStringAndClear();
1551 }
1552
1553 // add quoted arguments and do the call
1554 OUString sCall = "["
1555 + pMethod->GetName()
1556 + sQuotedArgs
1557 + "]";
1558
1559 SbxVariable* pRet = pMethod->GetParent()->Execute( sCall );
1560 if ( pRet && ( pRet != pMethod ) )
1561 {
1562 *i_retValue = *pRet;
1563 }
1564 return SbxBase::GetError();
1565}
1566
1567namespace {
1568
1569class ModuleInfo_Impl : public ModuleInfoHelper
1570{
1571 OUString maName;
1572 OUString maLanguage;
1573 OUString maSource;
1574
1575public:
1576 ModuleInfo_Impl( OUString aName, OUString aLanguage, OUString aSource )
1577 : maName(std::move( aName )), maLanguage(std::move( aLanguage)), maSource(std::move( aSource )) {}
1578
1579 // Methods XStarBasicModuleInfo
1580 virtual OUString SAL_CALL getName() override
1581 { return maName; }
1582 virtual OUString SAL_CALL getLanguage() override
1583 { return maLanguage; }
1584 virtual OUString SAL_CALL getSource() override
1585 { return maSource; }
1586};
1587
1588
1589class DialogInfo_Impl : public WeakImplHelper< script::XStarBasicDialogInfo >
1590{
1591 OUString maName;
1592 uno::Sequence< sal_Int8 > mData;
1593
1594public:
1595 DialogInfo_Impl( OUString aName, const uno::Sequence< sal_Int8 >& Data )
1596 : maName(std::move( aName )), mData( Data ) {}
1597
1598 // Methods XStarBasicDialogInfo
1599 virtual OUString SAL_CALL getName() override
1600 { return maName; }
1601 virtual uno::Sequence< sal_Int8 > SAL_CALL getData() override
1602 { return mData; }
1603};
1604
1605
1606class LibraryInfo_Impl : public WeakImplHelper< script::XStarBasicLibraryInfo >
1607{
1608 OUString maName;
1609 uno::Reference< container::XNameContainer > mxModuleContainer;
1610 uno::Reference< container::XNameContainer > mxDialogContainer;
1611 OUString maPassword;
1612 OUString maExternaleSourceURL;
1613 OUString maLinkTargetURL;
1614
1615public:
1616 LibraryInfo_Impl
1617 (
1618 OUString aName,
1619 uno::Reference< container::XNameContainer > xModuleContainer,
1620 uno::Reference< container::XNameContainer > xDialogContainer,
1621 OUString aPassword,
1622 OUString aExternaleSourceURL,
1623 OUString aLinkTargetURL
1624 )
1625 : maName(std::move( aName ))
1626 , mxModuleContainer(std::move( xModuleContainer ))
1627 , mxDialogContainer(std::move( xDialogContainer ))
1628 , maPassword(std::move( aPassword ))
1629 , maExternaleSourceURL(std::move( aExternaleSourceURL ))
1630 , maLinkTargetURL(std::move( aLinkTargetURL ))
1631 {}
1632
1633 // Methods XStarBasicLibraryInfo
1634 virtual OUString SAL_CALL getName() override
1635 { return maName; }
1636 virtual uno::Reference< container::XNameContainer > SAL_CALL getModuleContainer() override
1637 { return mxModuleContainer; }
1638 virtual uno::Reference< container::XNameContainer > SAL_CALL getDialogContainer() override
1639 { return mxDialogContainer; }
1640 virtual OUString SAL_CALL getPassword() override
1641 { return maPassword; }
1642 virtual OUString SAL_CALL getExternalSourceURL() override
1643 { return maExternaleSourceURL; }
1644 virtual OUString SAL_CALL getLinkTargetURL() override
1645 { return maLinkTargetURL; }
1646};
1647
1648
1649class ModuleContainer_Impl : public NameContainerHelper
1650{
1651 StarBASIC* mpLib;
1652
1653public:
1654 explicit ModuleContainer_Impl( StarBASIC* pLib )
1655 :mpLib( pLib ) {}
1656
1657 // Methods XElementAccess
1658 virtual uno::Type SAL_CALL getElementType() override;
1659 virtual sal_Bool SAL_CALL hasElements() override;
1660
1661 // Methods XNameAccess
1662 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
1663 virtual uno::Sequence< OUString > SAL_CALL getElementNames() override;
1664 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
1665
1666 // Methods XNameReplace
1667 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
1668
1669 // Methods XNameContainer
1670 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
1671 virtual void SAL_CALL removeByName( const OUString& Name ) override;
1672};
1673
1674}
1675
1676// Methods XElementAccess
1677uno::Type ModuleContainer_Impl::getElementType()
1678{
1680 return aModuleType;
1681}
1682
1683sal_Bool ModuleContainer_Impl::hasElements()
1684{
1685 return mpLib && !mpLib->GetModules().empty();
1686}
1687
1688// Methods XNameAccess
1689uno::Any ModuleContainer_Impl::getByName( const OUString& aName )
1690{
1691 SbModule* pMod = mpLib ? mpLib->FindModule( aName ) : nullptr;
1692 if( !pMod )
1693 throw container::NoSuchElementException();
1694 uno::Reference< script::XStarBasicModuleInfo > xMod = new ModuleInfo_Impl( aName, "StarBasic", pMod->GetSource32() );
1695 uno::Any aRetAny;
1696 aRetAny <<= xMod;
1697 return aRetAny;
1698}
1699
1700uno::Sequence< OUString > ModuleContainer_Impl::getElementNames()
1701{
1702 sal_uInt16 nMods = mpLib ? mpLib->GetModules().size() : 0;
1703 uno::Sequence< OUString > aRetSeq( nMods );
1704 OUString* pRetSeq = aRetSeq.getArray();
1705 for( sal_uInt16 i = 0 ; i < nMods ; i++ )
1706 {
1707 pRetSeq[i] = mpLib->GetModules()[i]->GetName();
1708 }
1709 return aRetSeq;
1710}
1711
1712sal_Bool ModuleContainer_Impl::hasByName( const OUString& aName )
1713{
1714 SbModule* pMod = mpLib ? mpLib->FindModule( aName ) : nullptr;
1715 bool bRet = (pMod != nullptr);
1716 return bRet;
1717}
1718
1719
1720// Methods XNameReplace
1721void ModuleContainer_Impl::replaceByName( const OUString& aName, const uno::Any& aElement )
1722{
1723 removeByName( aName );
1724 insertByName( aName, aElement );
1725}
1726
1727
1728// Methods XNameContainer
1729void ModuleContainer_Impl::insertByName( const OUString& aName, const uno::Any& aElement )
1730{
1732 const uno::Type& aAnyType = aElement.getValueType();
1733 if( aModuleType != aAnyType )
1734 {
1735 throw lang::IllegalArgumentException("types do not match", getXWeak(), 2);
1736 }
1737 uno::Reference< script::XStarBasicModuleInfo > xMod;
1738 aElement >>= xMod;
1739 mpLib->MakeModule( aName, xMod->getSource() );
1740}
1741
1742void ModuleContainer_Impl::removeByName( const OUString& Name )
1743{
1744 SbModule* pMod = mpLib ? mpLib->FindModule( Name ) : nullptr;
1745 if( !pMod )
1746 {
1747 throw container::NoSuchElementException();
1748 }
1749 mpLib->Remove( pMod );
1750}
1751
1752
1753static uno::Sequence< sal_Int8 > implGetDialogData( SbxObject* pDialog )
1754{
1755 SvMemoryStream aMemStream;
1756 pDialog->Store( aMemStream );
1757 sal_Int32 nLen = aMemStream.Tell();
1758 if (nLen < 0) { abort(); }
1759 uno::Sequence< sal_Int8 > aData( nLen );
1760 sal_Int8* pDestData = aData.getArray();
1761 const sal_Int8* pSrcData = static_cast<const sal_Int8*>(aMemStream.GetData());
1762 memcpy( pDestData, pSrcData, nLen );
1763 return aData;
1764}
1765
1766static SbxObjectRef implCreateDialog( const uno::Sequence< sal_Int8 >& aData )
1767{
1768 sal_Int8* pData = const_cast< uno::Sequence< sal_Int8 >& >(aData).getArray();
1769 SvMemoryStream aMemStream( pData, aData.getLength(), StreamMode::READ );
1770 SbxBaseRef pBase = SbxBase::Load( aMemStream );
1771 return dynamic_cast<SbxObject*>(pBase.get());
1772}
1773
1774// HACK! Because this value is defined in basctl/inc/vcsbxdef.hxx
1775// which we can't include here, we have to use the value directly
1776#define SBXID_DIALOG 101
1777
1778namespace {
1779
1780class DialogContainer_Impl : public NameContainerHelper
1781{
1782 StarBASIC* mpLib;
1783
1784public:
1785 explicit DialogContainer_Impl( StarBASIC* pLib )
1786 :mpLib( pLib ) {}
1787
1788 // Methods XElementAccess
1789 virtual uno::Type SAL_CALL getElementType() override;
1790 virtual sal_Bool SAL_CALL hasElements() override;
1791
1792 // Methods XNameAccess
1793 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
1794 virtual uno::Sequence< OUString > SAL_CALL getElementNames() override;
1795 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
1796
1797 // Methods XNameReplace
1798 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
1799
1800 // Methods XNameContainer
1801 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
1802 virtual void SAL_CALL removeByName( const OUString& Name ) override;
1803};
1804
1805}
1806
1807// Methods XElementAccess
1808uno::Type DialogContainer_Impl::getElementType()
1809{
1811 return aModuleType;
1812}
1813
1814sal_Bool DialogContainer_Impl::hasElements()
1815{
1816 bool bRet = false;
1817
1818 sal_Int32 nCount = mpLib->GetObjects()->Count();
1819 for( sal_Int32 nObj = 0; nObj < nCount ; nObj++ )
1820 {
1821 SbxVariable* pVar = mpLib->GetObjects()->Get( nObj );
1822 SbxObject* pObj = dynamic_cast<SbxObject*>(pVar);
1823 if ( pObj && (pObj->GetSbxId() == SBXID_DIALOG ) )
1824 {
1825 bRet = true;
1826 break;
1827 }
1828 }
1829 return bRet;
1830}
1831
1832// Methods XNameAccess
1833uno::Any DialogContainer_Impl::getByName( const OUString& aName )
1834{
1835 SbxVariable* pVar = mpLib->GetObjects()->Find( aName, SbxClassType::DontCare );
1836 SbxObject* pObj = dynamic_cast<SbxObject*>(pVar);
1837 if( !( pObj && pObj->GetSbxId() == SBXID_DIALOG ) )
1838 {
1839 throw container::NoSuchElementException();
1840 }
1841
1842 uno::Reference< script::XStarBasicDialogInfo > xDialog =
1843 new DialogInfo_Impl(aName, implGetDialogData(pObj));
1844
1845 uno::Any aRetAny;
1846 aRetAny <<= xDialog;
1847 return aRetAny;
1848}
1849
1850uno::Sequence< OUString > DialogContainer_Impl::getElementNames()
1851{
1852 sal_Int32 nCount = mpLib->GetObjects()->Count();
1853 uno::Sequence< OUString > aRetSeq( nCount );
1854 OUString* pRetSeq = aRetSeq.getArray();
1855 sal_Int32 nDialogCounter = 0;
1856
1857 for( sal_Int32 nObj = 0; nObj < nCount ; nObj++ )
1858 {
1859 SbxVariable* pVar = mpLib->GetObjects()->Get( nObj );
1860 SbxObject* pObj = dynamic_cast<SbxObject*> (pVar);
1861 if ( pObj && ( pObj->GetSbxId() == SBXID_DIALOG ) )
1862 {
1863 pRetSeq[ nDialogCounter ] = pVar->GetName();
1864 nDialogCounter++;
1865 }
1866 }
1867 aRetSeq.realloc( nDialogCounter );
1868 return aRetSeq;
1869}
1870
1871sal_Bool DialogContainer_Impl::hasByName( const OUString& aName )
1872{
1873 bool bRet = false;
1874 SbxVariable* pVar = mpLib->GetObjects()->Find( aName, SbxClassType::DontCare );
1875 SbxObject* pObj = dynamic_cast<SbxObject*>(pVar);
1876 if( pObj && ( pObj->GetSbxId() == SBXID_DIALOG ) )
1877 {
1878 bRet = true;
1879 }
1880 return bRet;
1881}
1882
1883
1884// Methods XNameReplace
1885void DialogContainer_Impl::replaceByName( const OUString& aName, const uno::Any& aElement )
1886{
1887 removeByName( aName );
1888 insertByName( aName, aElement );
1889}
1890
1891
1892// Methods XNameContainer
1893void DialogContainer_Impl::insertByName( const OUString&, const uno::Any& aElement )
1894{
1896 const uno::Type& aAnyType = aElement.getValueType();
1897 if( aModuleType != aAnyType )
1898 {
1899 throw lang::IllegalArgumentException("types do not match", getXWeak(), 2);
1900 }
1901 uno::Reference< script::XStarBasicDialogInfo > xMod;
1902 aElement >>= xMod;
1903 SbxObjectRef xDialog = implCreateDialog( xMod->getData() );
1904 mpLib->Insert( xDialog.get() );
1905}
1906
1907void DialogContainer_Impl::removeByName( const OUString& Name )
1908{
1909 SbxVariable* pVar = mpLib->GetObjects()->Find( Name, SbxClassType::DontCare );
1910 SbxObject* pObj = dynamic_cast<SbxObject*>(pVar);
1911 if( !( pObj && ( pObj->GetSbxId() == SBXID_DIALOG ) ) )
1912 {
1913 throw container::NoSuchElementException();
1914 }
1915 mpLib->Remove( pVar );
1916}
1917
1918
1920{
1922
1923public:
1925 :mpMgr( pMgr ) {}
1926
1927 // Methods XElementAccess
1928 virtual uno::Type SAL_CALL getElementType() override;
1929 virtual sal_Bool SAL_CALL hasElements() override;
1930
1931 // Methods XNameAccess
1932 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
1933 virtual uno::Sequence< OUString > SAL_CALL getElementNames() override;
1934 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
1935
1936 // Methods XNameReplace
1937 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
1938
1939 // Methods XNameContainer
1940 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
1941 virtual void SAL_CALL removeByName( const OUString& Name ) override;
1942};
1943
1944
1945// Methods XElementAccess
1947{
1949 return aType;
1950}
1951
1953{
1954 sal_Int32 nLibs = mpMgr->GetLibCount();
1955 bool bRet = (nLibs > 0);
1956 return bRet;
1957}
1958
1959// Methods XNameAccess
1961{
1962 uno::Any aRetAny;
1963 if( !mpMgr->HasLib( aName ) )
1964 throw container::NoSuchElementException();
1966
1967 uno::Reference< container::XNameContainer > xModuleContainer =
1968 new ModuleContainer_Impl( pLib );
1969
1970 uno::Reference< container::XNameContainer > xDialogContainer =
1971 new DialogContainer_Impl( pLib );
1972
1973 BasicLibInfo* pLibInfo = mpMgr->FindLibInfo( pLib );
1974
1975 OUString aPassword = pLibInfo->GetPassword();
1976
1977 // TODO Only provide extern info!
1978 OUString aExternaleSourceURL;
1979 OUString aLinkTargetURL;
1980 if( pLibInfo->IsReference() )
1981 {
1982 aLinkTargetURL = pLibInfo->GetStorageName();
1983 }
1984 else if( pLibInfo->IsExtern() )
1985 {
1986 aExternaleSourceURL = pLibInfo->GetStorageName();
1987 }
1988 uno::Reference< script::XStarBasicLibraryInfo > xLibInfo = new LibraryInfo_Impl
1989 (
1990 aName,
1991 xModuleContainer,
1992 xDialogContainer,
1993 aPassword,
1994 aExternaleSourceURL,
1995 aLinkTargetURL
1996 );
1997
1998 aRetAny <<= xLibInfo;
1999 return aRetAny;
2000}
2001
2003{
2004 sal_uInt16 nLibs = mpMgr->GetLibCount();
2005 uno::Sequence< OUString > aRetSeq( nLibs );
2006 OUString* pRetSeq = aRetSeq.getArray();
2007 for( sal_uInt16 i = 0 ; i < nLibs ; i++ )
2008 {
2009 pRetSeq[i] = mpMgr->GetLibName( i );
2010 }
2011 return aRetSeq;
2012}
2013
2015{
2016 bool bRet = mpMgr->HasLib( aName );
2017 return bRet;
2018}
2019
2020// Methods XNameReplace
2021void LibraryContainer_Impl::replaceByName( const OUString& aName, const uno::Any& aElement )
2022{
2024 insertByName( aName, aElement );
2025}
2026
2027// Methods XNameContainer
2028void LibraryContainer_Impl::insertByName( const OUString&, const uno::Any& )
2029{
2030 // TODO: Insert a complete Library?!
2031}
2032
2033void LibraryContainer_Impl::removeByName( const OUString& Name )
2034{
2036 if( !pLib )
2037 {
2038 throw container::NoSuchElementException();
2039 }
2040 sal_uInt16 nLibId = mpMgr->GetLibId( Name );
2041 mpMgr->RemoveLib( nLibId );
2042}
2043
2044
2045typedef WeakImplHelper< script::XStarBasicAccess > StarBasicAccessHelper;
2046
2047
2049{
2051 uno::Reference< container::XNameContainer > mxLibContainer;
2052
2053public:
2055 :mpMgr( pMgr ) {}
2056
2057public:
2058 // Methods
2059 virtual uno::Reference< container::XNameContainer > SAL_CALL getLibraryContainer() override;
2060 virtual void SAL_CALL createLibrary( const OUString& LibName, const OUString& Password,
2061 const OUString& ExternalSourceURL, const OUString& LinkTargetURL ) override;
2062 virtual void SAL_CALL addModule( const OUString& LibraryName, const OUString& ModuleName,
2063 const OUString& Language, const OUString& Source ) override;
2064 virtual void SAL_CALL addDialog( const OUString& LibraryName, const OUString& DialogName,
2065 const uno::Sequence< sal_Int8 >& Data ) override;
2066};
2067
2068uno::Reference< container::XNameContainer > SAL_CALL StarBasicAccess_Impl::getLibraryContainer()
2069{
2070 if( !mxLibContainer.is() )
2072 return mxLibContainer;
2073}
2074
2076(
2077 const OUString& LibName,
2078 const OUString& Password,
2079 const OUString&,
2080 const OUString& LinkTargetURL
2081)
2082{
2083 StarBASIC* pLib = mpMgr->CreateLib( LibName, Password, LinkTargetURL );
2084 DBG_ASSERT( pLib, "XML Import: Basic library could not be created");
2085}
2086
2088(
2089 const OUString& LibraryName,
2090 const OUString& ModuleName,
2091 const OUString&,
2092 const OUString& Source
2093)
2094{
2095 StarBASIC* pLib = mpMgr->GetLib( LibraryName );
2096 DBG_ASSERT( pLib, "XML Import: Lib for module unknown");
2097 if( pLib )
2098 {
2099 pLib->MakeModule( ModuleName, Source );
2100 }
2101}
2102
2104(
2105 const OUString&,
2106 const OUString&,
2107 const uno::Sequence< sal_Int8 >&
2108)
2109{}
2110
2111// Basic XML Import/Export
2112uno::Reference< script::XStarBasicAccess > getStarBasicAccess( BasicManager* pMgr )
2113{
2114 uno::Reference< script::XStarBasicAccess > xRet =
2115 new StarBasicAccess_Impl( pMgr );
2116 return xRet;
2117}
2118
2119/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString maName
constexpr OUStringLiteral szBasicStorage
Definition: basmgr.cxx:92
#define LIBINFO_ID
Definition: basmgr.cxx:51
WeakImplHelper< script::XStarBasicAccess > StarBasicAccessHelper
Definition: basmgr.cxx:78
const StreamMode eStreamReadMode
Definition: basmgr.cxx:99
#define LIBINFO_SEP
Definition: basmgr.cxx:50
constexpr OStringLiteral szCryptingKey
Definition: basmgr.cxx:96
uno::Reference< script::XStarBasicAccess > getStarBasicAccess(BasicManager *pMgr)
Definition: basmgr.cxx:2112
WeakImplHelper< script::XStarBasicModuleInfo > ModuleInfoHelper
Definition: basmgr.cxx:77
WeakImplHelper< container::XNameContainer > NameContainerHelper
Definition: basmgr.cxx:76
static void copyToLibraryContainer(StarBASIC *pBasic, const LibraryContainerInfo &rInfo)
Definition: basmgr.cxx:487
constexpr OUStringLiteral szStdLibName
Definition: basmgr.cxx:91
const StreamMode eStorageReadMode
Definition: basmgr.cxx:100
#define LIB_SEP
Definition: basmgr.cxx:49
constexpr OUStringLiteral szManagerStream
Definition: basmgr.cxx:94
constexpr OUStringLiteral szOldManagerStream
Definition: basmgr.cxx:93
::cppu::WeakImplHelper< container::XContainerListener > ContainerListenerHelper
Definition: basmgr.cxx:105
constexpr OUStringLiteral szImbedded
Definition: basmgr.cxx:95
#define SBXID_DIALOG
Definition: basmgr.cxx:1776
static SbxObjectRef implCreateDialog(const uno::Sequence< sal_Int8 > &aData)
Definition: basmgr.cxx:1766
static uno::Sequence< sal_Int8 > implGetDialogData(SbxObject *pDialog)
Definition: basmgr.cxx:1753
#define PASSWORD_MARKER
Definition: basmgr.cxx:52
BasicErrorReason
Definition: basmgr.hxx:45
#define LIB_NOTFOUND
Definition: basmgr.hxx:95
BasicManager * mpMgr
Definition: basmgr.cxx:109
virtual void SAL_CALL elementRemoved(const container::ContainerEvent &Event) override
Definition: basmgr.cxx:276
static void addLibraryModulesImpl(BasicManager const *pMgr, const uno::Reference< container::XNameAccess > &xLibNameAccess, std::u16string_view aLibName)
Definition: basmgr.cxx:165
virtual void SAL_CALL elementInserted(const container::ContainerEvent &Event) override
Definition: basmgr.cxx:205
virtual void SAL_CALL elementReplaced(const container::ContainerEvent &Event) override
Definition: basmgr.cxx:251
BasMgrContainerListenerImpl(BasicManager *pMgr, OUString aLibName)
Definition: basmgr.cxx:113
virtual void SAL_CALL disposing(const lang::EventObject &Source) override
Definition: basmgr.cxx:200
static void insertLibraryImpl(const uno::Reference< script::XLibraryContainer > &xScriptCont, BasicManager *pMgr, const uno::Any &aLibAny, const OUString &aLibName)
Definition: basmgr.cxx:136
ErrCode nErrorId
Definition: basmgr.hxx:58
BasicErrorReason nReason
Definition: basmgr.hxx:59
BasicError(const BasicError &rErr)
Definition: basmgr.cxx:309
StarBASICRef GetLib() const
Definition: basmgr.cxx:345
void SetLibraryContainer(const uno::Reference< script::XLibraryContainer > &xScriptCont)
Definition: basmgr.cxx:370
void SetLib(StarBASIC *pBasic)
Definition: basmgr.cxx:353
void SetStorageName(const OUString &rName)
Definition: basmgr.cxx:339
static BasicLibInfo * Create(SotStorageStream &rSStream)
Definition: basmgr.cxx:383
const uno::Reference< script::XLibraryContainer > & GetLibraryContainer() const
Definition: basmgr.cxx:368
StarBASICRef xLib
Definition: basmgr.cxx:319
void SetReference(bool b)
Definition: basmgr.cxx:335
bool bDoLoad
Definition: basmgr.cxx:325
uno::Reference< script::XLibraryContainer > mxScriptCont
Definition: basmgr.cxx:329
const OUString & GetStorageName() const
Definition: basmgr.cxx:340
bool IsReference() const
Definition: basmgr.cxx:334
void SetPassword(const OUString &rNewPassword)
Definition: basmgr.cxx:363
bool bReference
Definition: basmgr.cxx:326
bool IsExtern() const
Definition: basmgr.cxx:337
const OUString & GetRelStorageName() const
Definition: basmgr.cxx:343
const OUString & GetLibName() const
Definition: basmgr.cxx:355
OUString aRelStorageName
Definition: basmgr.cxx:322
void SetLibName(const OUString &rName)
Definition: basmgr.cxx:356
bool HasPassword() const
Definition: basmgr.cxx:361
void SetRelStorageName(const OUString &rN)
Definition: basmgr.cxx:342
OUString aStorageName
Definition: basmgr.cxx:321
OUString aPassword
Definition: basmgr.cxx:323
bool DoLoad() const
Definition: basmgr.cxx:359
OUString aLibName
Definition: basmgr.cxx:320
const OUString & GetPassword() const
Definition: basmgr.cxx:362
StarBASICRef & GetLibRef()
Definition: basmgr.cxx:352
BASIC_DLLPRIVATE StarBASIC * AddLib(SotStorage &rStorage, const OUString &rLibName, bool bReference)
Definition: basmgr.cxx:1012
StarBASIC * GetLib(sal_uInt16 nLib) const
Definition: basmgr.cxx:1169
const OUString & GetStorageName() const
Definition: basmgr.hxx:133
void SetLibraryContainerInfo(const LibraryContainerInfo &rInfo)
announces the library containers which belong to this BasicManager
Definition: basmgr.cxx:526
bool ImplLoadBasic(SvStream &rStrm, StarBASICRef &rOldBasic) const
Definition: basmgr.cxx:951
bool ImpLoadLibrary(BasicLibInfo *pLibInfo, SotStorage *pCurStorage)
Definition: basmgr.cxx:834
OUString GetLibName(sal_uInt16 nLib)
Definition: basmgr.cxx:1221
bool HasMacro(OUString const &i_fullyQualifiedName) const
determines whether the Basic Manager has a given macro, given by fully qualified name
Definition: basmgr.cxx:1489
ErrCode ExecuteMacro(OUString const &i_fullyQualifiedName, SbxArray *i_arguments, SbxValue *i_retValue)
executes a given macro
Definition: basmgr.cxx:1494
bool HasExeCode(std::u16string_view)
Definition: basmgr.cxx:814
const css::uno::Reference< css::script::XPersistentLibraryContainer > & GetScriptLibraryContainer() const
Definition: basmgr.cxx:521
BASIC_DLLPRIVATE bool HasLib(std::u16string_view rName) const
Definition: basmgr.cxx:1209
BASIC_DLLPRIVATE StarBASIC * CreateLibForLibContainer(const OUString &rLibName, const css::uno::Reference< css::script::XLibraryContainer > &xScriptCont)
Definition: basmgr.cxx:1318
void LoadOldBasicManager(SotStorage &rStorage)
Definition: basmgr.cxx:728
virtual ~BasicManager() override
Definition: basmgr.cxx:808
BASIC_DLLPRIVATE bool IsReference(sal_uInt16 nLib)
Definition: basmgr.cxx:1063
OUString aBasicLibPath
Definition: basmgr.hxx:113
OUString maStorageName
Definition: basmgr.hxx:108
void ImpCreateStdLib(StarBASIC *pParentFromStdLib)
Definition: basmgr.cxx:626
void SetGlobalUNOConstant(const OUString &rName, const css::uno::Any &_rValue, css::uno::Any *pOldValue=nullptr)
sets a global constant in the basic library, referring to some UNO object, to a new value.
Definition: basmgr.cxx:1373
bool mbDocMgr
Definition: basmgr.hxx:109
bool IsBasicModified() const
Definition: basmgr.cxx:1350
LibraryContainerInfo maContainerInfo
Definition: basmgr.hxx:111
BasicLibInfo * FindLibInfo(StarBASIC const *pBasic)
Definition: basmgr.cxx:1337
std::vector< BasicError > aErrors
Definition: basmgr.hxx:105
const css::uno::Reference< css::script::XPersistentLibraryContainer > & GetDialogLibraryContainer() const
Definition: basmgr.cxx:516
BasicManager(SotStorage &rStorage, std::u16string_view rBaseURL, StarBASIC *pParentFromStdLib=nullptr, OUString const *pLibPath=nullptr, bool bDocMgr=false)
Definition: basmgr.cxx:427
static void CheckModules(StarBASIC *pBasic, bool bReference)
Definition: basmgr.cxx:986
sal_uInt16 GetLibCount() const
Definition: basmgr.cxx:1164
bool RemoveLib(sal_uInt16 nLib, bool bDelBasicFromStorage)
Definition: basmgr.cxx:1079
void LoadBasicManager(SotStorage &rStorage, std::u16string_view rBaseURL)
Definition: basmgr.cxx:636
bool GetGlobalUNOConstant(const OUString &rName, css::uno::Any &aOut)
retrieves a global constant in the basic library, referring to some UNO object, returns true if a val...
Definition: basmgr.cxx:1363
bool ImgVersion12PsswdBinaryLimitExceeded(std::vector< OUString > &_out_rModuleNames)
determines whether there are password-protected modules whose size exceeds the B_IMG_VERSION_12 modul...
Definition: basmgr.cxx:1393
bool LoadLib(sal_uInt16 nLib)
Definition: basmgr.cxx:1231
BASIC_DLLPRIVATE StarBASIC * CreateLib(const OUString &rLibName)
Definition: basmgr.cxx:1264
std::vector< std::unique_ptr< BasicLibInfo > > maLibs
Definition: basmgr.hxx:112
sal_uInt16 GetLibId(std::u16string_view rName) const
Definition: basmgr.cxx:1197
BASIC_DLLPRIVATE StarBASIC * GetStdLib() const
Definition: basmgr.cxx:1179
static bool ImplEncryptStream(SvStream &rStream)
Definition: basmgr.cxx:932
BasicLibInfo * CreateLibInfo()
Definition: basmgr.cxx:828
friend class BasMgrContainerListenerImpl
Definition: basmgr.hxx:101
void ImpMgrNotLoaded(const OUString &rStorageName)
Definition: basmgr.cxx:608
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
INetURLObject smartRel2Abs(OUString const &rTheRelURIRef, bool &rWasAbsolute, bool bIgnoreFragment=false, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, bool bRelativeNonURIs=false, FSysStyle eStyle=FSysStyle::Detect) const
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
OUString PathToFileName() const
INetProtocol GetProtocol() const
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: basmgr.cxx:2014
virtual sal_Bool SAL_CALL hasElements() override
Definition: basmgr.cxx:1952
virtual void SAL_CALL insertByName(const OUString &aName, const uno::Any &aElement) override
Definition: basmgr.cxx:2028
virtual uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: basmgr.cxx:1960
BasicManager * mpMgr
Definition: basmgr.cxx:1921
virtual void SAL_CALL replaceByName(const OUString &aName, const uno::Any &aElement) override
Definition: basmgr.cxx:2021
virtual void SAL_CALL removeByName(const OUString &Name) override
Definition: basmgr.cxx:2033
LibraryContainer_Impl(BasicManager *pMgr)
Definition: basmgr.cxx:1924
virtual uno::Type SAL_CALL getElementType() override
Definition: basmgr.cxx:1946
virtual uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: basmgr.cxx:2002
static utl::TransliterationWrapper & GetTransliteration()
Definition: global.cxx:39
ErrCode Call(SbxValue *pRet, SbxVariable *pCaller=nullptr)
Definition: sbxmod.cxx:2054
SAL_DLLPRIVATE bool ExceedsImgVersion12ModuleSize()
Definition: sbxmod.cxx:1670
const OUString & GetSource32() const
Definition: sbmod.hxx:107
void SetSource32(const OUString &r)
Definition: sbxmod.cxx:802
Definition: sbx.hxx:95
static ErrCode const & GetError()
Definition: sbxbase.cxx:96
void SetFlag(SbxFlagBits n)
Definition: sbxcore.hxx:108
virtual sal_uInt16 GetSbxId() const =0
std::pair< bool, sal_uInt32 > Store(SvStream &)
Definition: sbxbase.cxx:252
static SbxBaseRef Load(SvStream &)
Definition: sbxbase.cxx:212
SbxVariable * Execute(const OUString &)
Definition: sbxexec.cxx:337
bool Get(SbxValues &) const
Definition: sbxvalue.cxx:270
const SbxObject * GetParent() const
Definition: sbxvar.hxx:296
void SetName(const OUString &)
Definition: sbxvar.cxx:192
void SetParameters(SbxArray *p)
Definition: sbxvar.cxx:178
const OUString & GetName(SbxNameType=SbxNameType::NONE) const
Definition: sbxvar.cxx:199
virtual void SetParent(SbxObject *)
Definition: sbxvar.cxx:355
void Broadcast(const SfxHint &rHint)
bool IsStream(const OUString &rEleName) const
const OUString & GetName() const
tools::SvRef< SotStorageStream > OpenSotStream(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE)
static bool IsStorageFile(OUString const &rFileName)
SbModules & GetModules()
Definition: sbstar.hxx:102
virtual SbxVariable * Find(const OUString &, SbxClassType) override
Definition: sb.cxx:1248
bool GetUNOConstant(const OUString &rName, css::uno::Any &aOut)
Definition: sb.cxx:1874
static ErrCode const & GetErrorCode()
Definition: sb.cxx:1432
SbModule * FindModule(std::u16string_view)
Definition: sb.cxx:1092
virtual void Remove(SbxVariable *) override
Definition: sb.cxx:1070
virtual void Insert(SbxVariable *) override
Definition: sb.cxx:1051
virtual void SetModified(bool) override
Definition: sb.cxx:931
virtual uno::Reference< container::XNameContainer > SAL_CALL getLibraryContainer() override
Definition: basmgr.cxx:2068
StarBasicAccess_Impl(BasicManager *pMgr)
Definition: basmgr.cxx:2054
virtual void SAL_CALL createLibrary(const OUString &LibName, const OUString &Password, const OUString &ExternalSourceURL, const OUString &LinkTargetURL) override
Definition: basmgr.cxx:2076
virtual void SAL_CALL addModule(const OUString &LibraryName, const OUString &ModuleName, const OUString &Language, const OUString &Source) override
Definition: basmgr.cxx:2088
virtual void SAL_CALL addDialog(const OUString &LibraryName, const OUString &DialogName, const uno::Sequence< sal_Int8 > &Data) override
Definition: basmgr.cxx:2104
uno::Reference< container::XNameContainer > mxLibContainer
Definition: basmgr.cxx:2051
BasicManager * mpMgr
Definition: basmgr.cxx:2050
const void * GetData()
sal_uInt64 Tell() const
void SetCryptMaskKey(const OString &rCryptMaskKey)
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
sal_uInt64 Seek(sal_uInt64 nPos)
void RefreshBuffer()
bool SearchFile(OUString &rIniFile, Paths ePath=Paths::UserConfig)
void setLibraryPassword(const OUString &rLibraryName, const OUString &rPassword)
Definition: scriptcont.cxx:90
css::uno::Type const & get()
T * get() const
bool is() const
bool isEqual(const OUString &rStr1, const OUString &rStr2) const
int nCount
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
float u
#define ERRCODE_NONE
OUString aName
sal_uInt16 nPos
const char * pLib
#define SAL_WARN(area, stream)
::cppu::WeakImplHelper< css::container::XContainerListener > ContainerListenerHelper
std::unique_ptr< sal_Int32[]> pData
constexpr OUStringLiteral aData
int i
void SvStream & rStrm
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
bool getPassword(const css::uno::Reference< css::task::XInteractionHandler > &xHandler, OUString &rOutPwd, bool bFirstTry, const OUString &rDocName)
sal_Int16 nId
#define ERRCODE_BASIC_PROC_UNDEFINED
Definition: sberrors.hxx:32
#define ERRCODE_BASMGR_REMOVELIB
Definition: sberrors.hxx:163
#define ERRCODE_BASMGR_LIBLOAD
Definition: sberrors.hxx:157
#define ERRCODE_BASMGR_MGROPEN
Definition: sberrors.hxx:161
tools::SvRef< StarBASIC > StarBASICRef
Definition: sbstar.hxx:153
SbxObjectRef GetSbUnoObject(const OUString &aName, const Any &aUnoObj_)
Definition: sbunoobj.cxx:2882
Any sbxToUnoValue(const SbxValue *pVar)
Definition: sbunoobj.cxx:1137
constexpr auto SBXCR_SBX
Definition: sbxdef.hxx:164
std::vector< SvStorageInfo > SvStorageInfoList
StreamMode
#define STREAM_SEEK_TO_BEGIN
css::uno::Reference< css::script::XPersistentLibraryContainer > mxDialogCont
Definition: basmgr.hxx:75
basic::SfxScriptLibraryContainer * mpOldBasicPassword
Definition: basmgr.hxx:76
css::uno::Reference< css::script::XPersistentLibraryContainer > mxScriptCont
Definition: basmgr.hxx:74
OUString Name
unsigned char sal_Bool
signed char sal_Int8