Debian Codegraph @

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