xref: /AOO41X/main/basic/source/uno/scriptcont.cxx (revision 0848378beb0d0fcd9a9bf3cafa6204dbc20d39f7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basic.hxx"
26 #include "scriptcont.hxx"
27 #include "sbmodule.hxx"
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <com/sun/star/xml/sax/XParser.hpp>
30 #include <com/sun/star/xml/sax/InputSource.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <com/sun/star/io/XInputStream.hpp>
33 #include <com/sun/star/io/XActiveDataSource.hpp>
34 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/embed/XEncryptionProtectedSource.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/embed/XTransactedObject.hpp>
39 #include <com/sun/star/task/ErrorCodeIOException.hpp>
40 #include <com/sun/star/script/ModuleType.hpp>
41 #include <comphelper/processfactory.hxx>
42 #ifndef _COMPHELPER_STORAGEHELPER_HXX_
43 #include <comphelper/storagehelper.hxx>
44 #endif
45 #include <unotools/streamwrap.hxx>
46 #include <unotools/ucbstreamhelper.hxx>
47 #include <osl/mutex.hxx>
48 #include <rtl/digest.h>
49 #include <rtl/strbuf.hxx>
50 
51 // For password functionality
52 #include <tools/urlobj.hxx>
53 
54 
55 #include <unotools/pathoptions.hxx>
56 #include <svtools/sfxecode.hxx>
57 #include <svtools/ehdl.hxx>
58 #include <basic/basmgr.hxx>
59 #include <basic/sbmod.hxx>
60 #include <basic/basicmanagerrepository.hxx>
61 #include "basic/modsizeexceeded.hxx"
62 #include <xmlscript/xmlmod_imexp.hxx>
63 #include <cppuhelper/factory.hxx>
64 #include <com/sun/star/util/VetoException.hpp>
65 
66 namespace basic
67 {
68 
69 using namespace com::sun::star::document;
70 using namespace com::sun::star::container;
71 using namespace com::sun::star::io;
72 using namespace com::sun::star::uno;
73 using namespace com::sun::star::ucb;
74 using namespace com::sun::star::lang;
75 using namespace com::sun::star::script;
76 using namespace com::sun::star::xml::sax;
77 using namespace com::sun::star;
78 using namespace cppu;
79 using namespace osl;
80 
81 using ::rtl::OUString;
82 
83 //============================================================================
84 // Implementation class SfxScriptLibraryContainer
85 
getInfoFileName() const86 const sal_Char* SAL_CALL SfxScriptLibraryContainer::getInfoFileName() const { return "script"; }
getOldInfoFileName() const87 const sal_Char* SAL_CALL SfxScriptLibraryContainer::getOldInfoFileName() const { return "script"; }
getLibElementFileExtension() const88 const sal_Char* SAL_CALL SfxScriptLibraryContainer::getLibElementFileExtension() const { return "xba"; }
getLibrariesDir() const89 const sal_Char* SAL_CALL SfxScriptLibraryContainer::getLibrariesDir() const { return "Basic"; }
90 
91 // OldBasicPassword interface
setLibraryPassword(const String & rLibraryName,const String & rPassword)92 void SfxScriptLibraryContainer::setLibraryPassword
93     ( const String& rLibraryName, const String& rPassword )
94 {
95     try
96     {
97         SfxLibrary* pImplLib = getImplLib( rLibraryName );
98         if( rPassword.Len() )
99         {
100             pImplLib->mbDoc50Password = sal_True;
101             pImplLib->mbPasswordProtected = sal_True;
102             pImplLib->maPassword = rPassword;
103         }
104     }
105     catch( NoSuchElementException& ) {}
106 }
107 
getLibraryPassword(const String & rLibraryName)108 String SfxScriptLibraryContainer::getLibraryPassword( const String& rLibraryName )
109 {
110     SfxLibrary* pImplLib = getImplLib( rLibraryName );
111     String aPassword;
112     if( pImplLib->mbPasswordVerified )
113         aPassword = pImplLib->maPassword;
114     return aPassword;
115 }
116 
clearLibraryPassword(const String & rLibraryName)117 void SfxScriptLibraryContainer::clearLibraryPassword( const String& rLibraryName )
118 {
119     try
120     {
121         SfxLibrary* pImplLib = getImplLib( rLibraryName );
122         pImplLib->mbDoc50Password = sal_False;
123         pImplLib->mbPasswordProtected = sal_False;
124         pImplLib->maPassword = OUString();
125     }
126     catch( NoSuchElementException& ) {}
127 }
128 
hasLibraryPassword(const String & rLibraryName)129 sal_Bool SfxScriptLibraryContainer::hasLibraryPassword( const String& rLibraryName )
130 {
131     SfxLibrary* pImplLib = getImplLib( rLibraryName );
132     return pImplLib->mbPasswordProtected;
133 }
134 
135 
136 // Ctor for service
SfxScriptLibraryContainer(void)137 SfxScriptLibraryContainer::SfxScriptLibraryContainer( void )
138     :maScriptLanguage( RTL_CONSTASCII_USTRINGPARAM( "StarBasic" ) )
139 {
140     // all initialisation has to be done
141     // by calling XInitialization::initialize
142 }
143 
SfxScriptLibraryContainer(const uno::Reference<embed::XStorage> & xStorage)144 SfxScriptLibraryContainer::SfxScriptLibraryContainer( const uno::Reference< embed::XStorage >& xStorage )
145     :maScriptLanguage( RTL_CONSTASCII_USTRINGPARAM( "StarBasic" ) )
146 {
147     init( OUString(), xStorage );
148 }
149 
150 // Methods to get library instances of the correct type
implCreateLibrary(const OUString & aName)151 SfxLibrary* SfxScriptLibraryContainer::implCreateLibrary( const OUString& aName )
152 {
153     (void)aName;    // Only needed for SfxDialogLibrary
154     SfxLibrary* pRet = new SfxScriptLibrary( maModifiable, mxMSF, mxSFI );
155     return pRet;
156 }
157 
implCreateLibraryLink(const OUString & aName,const OUString & aLibInfoFileURL,const OUString & StorageURL,sal_Bool ReadOnly)158 SfxLibrary* SfxScriptLibraryContainer::implCreateLibraryLink
159     ( const OUString& aName, const OUString& aLibInfoFileURL,
160       const OUString& StorageURL, sal_Bool ReadOnly )
161 {
162     (void)aName;    // Only needed for SfxDialogLibrary
163     SfxLibrary* pRet =
164         new SfxScriptLibrary
165             ( maModifiable, mxMSF, mxSFI, aLibInfoFileURL, StorageURL, ReadOnly );
166     return pRet;
167 }
168 
createEmptyLibraryElement(void)169 Any SAL_CALL SfxScriptLibraryContainer::createEmptyLibraryElement( void )
170 {
171     OUString aMod;
172     Any aRetAny;
173     aRetAny <<= aMod;
174     return aRetAny;
175 }
176 
isLibraryElementValid(Any aElement) const177 bool SAL_CALL SfxScriptLibraryContainer::isLibraryElementValid( Any aElement ) const
178 {
179     return SfxScriptLibrary::containsValidModule( aElement );
180 }
181 
writeLibraryElement(const Reference<XNameContainer> & xLib,const OUString & aElementName,const Reference<XOutputStream> & xOutput)182 void SAL_CALL SfxScriptLibraryContainer::writeLibraryElement
183 (
184     const Reference < XNameContainer >& xLib,
185     const OUString& aElementName,
186     const Reference< XOutputStream >& xOutput
187 )
188     throw(Exception)
189 {
190     // Create sax writer
191     Reference< XExtendedDocumentHandler > xHandler(
192         mxMSF->createInstance(
193             OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") ) ), UNO_QUERY );
194     if( !xHandler.is() )
195     {
196         OSL_ENSURE( 0, "### couln't create sax-writer component\n" );
197         return;
198     }
199 
200     Reference< XTruncate > xTruncate( xOutput, UNO_QUERY );
201     OSL_ENSURE( xTruncate.is(), "Currently only the streams that can be truncated are expected!" );
202     if ( xTruncate.is() )
203         xTruncate->truncate();
204 
205     Reference< XActiveDataSource > xSource( xHandler, UNO_QUERY );
206     xSource->setOutputStream( xOutput );
207 
208     xmlscript::ModuleDescriptor aMod;
209     aMod.aName = aElementName;
210     aMod.aLanguage = maScriptLanguage;
211     Any aElement = xLib->getByName( aElementName );
212     aElement >>= aMod.aCode;
213 
214     Reference< script::vba::XVBAModuleInfo > xModInfo( xLib, UNO_QUERY );
215     if( xModInfo.is() && xModInfo->hasModuleInfo( aElementName ) )
216     {
217         script::ModuleInfo aModInfo = xModInfo->getModuleInfo( aElementName );
218         switch( aModInfo.ModuleType )
219         {
220         case ModuleType::NORMAL:
221             aMod.aModuleType = OUString( RTL_CONSTASCII_USTRINGPARAM("normal") );
222             break;
223         case ModuleType::CLASS:
224             aMod.aModuleType = OUString( RTL_CONSTASCII_USTRINGPARAM("class") );
225             break;
226         case ModuleType::FORM:
227             aMod.aModuleType = OUString( RTL_CONSTASCII_USTRINGPARAM("form") );
228             break;
229         case ModuleType::DOCUMENT:
230             aMod.aModuleType = OUString( RTL_CONSTASCII_USTRINGPARAM("document") );
231             break;
232         case ModuleType::UNKNOWN:
233             // nothing
234             break;
235         }
236     }
237 
238     xmlscript::exportScriptModule( xHandler, aMod );
239 }
240 
241 
importLibraryElement(const Reference<XNameContainer> & xLib,const OUString & aElementName,const OUString & aFile,const uno::Reference<io::XInputStream> & xInStream)242 Any SAL_CALL SfxScriptLibraryContainer::importLibraryElement
243     ( const Reference < XNameContainer >& xLib,
244       const OUString& aElementName, const OUString& aFile,
245       const uno::Reference< io::XInputStream >& xInStream )
246 {
247     Any aRetAny;
248 
249     Reference< XParser > xParser( mxMSF->createInstance(
250         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ) ), UNO_QUERY );
251     if( !xParser.is() )
252     {
253         OSL_ENSURE( 0, "### couln't create sax parser component\n" );
254         return aRetAny;
255     }
256 
257 
258     // Read from storage?
259     sal_Bool bStorage = xInStream.is();
260     Reference< XInputStream > xInput;
261 
262     if( bStorage )
263     {
264         xInput = xInStream;
265     }
266     else
267     {
268         try
269         {
270             xInput = mxSFI->openFileRead( aFile );
271         }
272         catch( Exception& )
273         //catch( Exception& e )
274         {
275             // TODO:
276             //throw WrappedTargetException( e );
277         }
278     }
279 
280     if( !xInput.is() )
281         return aRetAny;
282 
283     InputSource source;
284     source.aInputStream = xInput;
285     source.sSystemId    = aFile;
286 
287     // start parsing
288     xmlscript::ModuleDescriptor aMod;
289 
290     try
291     {
292         xParser->setDocumentHandler( ::xmlscript::importScriptModule( aMod ) );
293         xParser->parseStream( source );
294     }
295     catch( Exception& )
296     {
297         SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aFile );
298         sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
299         ErrorHandler::HandleError( nErrorCode );
300     }
301 
302     aRetAny <<= aMod.aCode;
303 
304     // TODO: Check language
305     // aMod.aLanguage
306     // aMod.aName ignored
307     if( !aMod.aModuleType.isEmpty() )
308     {
309         /*  If in VBA compatibility mode, force creation of the VBA Globals
310             object. Each application will create an instance of its own
311             implementation and store it in its Basic manager. Implementations
312             will do all necessary additional initialization, such as
313             registering the global "This***Doc" UNO constant, starting the
314             document events processor etc.
315          */
316         if( getVBACompatibilityMode() ) try
317         {
318             Reference< frame::XModel > xModel( mxOwnerDocument );   // weak-ref -> ref
319             Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
320             xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.VBAGlobals" ) ) );
321         }
322         catch( Exception& )
323         {
324         }
325 
326         script::ModuleInfo aModInfo;
327         aModInfo.ModuleType = ModuleType::UNKNOWN;
328         if( aMod.aModuleType.equalsAsciiL(
329                     RTL_CONSTASCII_STRINGPARAM("normal") ))
330         {
331             aModInfo.ModuleType = ModuleType::NORMAL;
332         }
333         else if( aMod.aModuleType.equalsAsciiL(
334                     RTL_CONSTASCII_STRINGPARAM("class") ))
335         {
336             aModInfo.ModuleType = ModuleType::CLASS;
337         }
338         else if( aMod.aModuleType.equalsAsciiL(
339                     RTL_CONSTASCII_STRINGPARAM("form") ))
340         {
341             aModInfo.ModuleType = ModuleType::FORM;
342             aModInfo.ModuleObject = mxOwnerDocument;
343         }
344         else if( aMod.aModuleType.equalsAsciiL(
345                     RTL_CONSTASCII_STRINGPARAM("document") ))
346         {
347             aModInfo.ModuleType = ModuleType::DOCUMENT;
348 
349             // #163691# use the same codename access instance for all document modules
350             if( !mxCodeNameAccess.is() ) try
351             {
352                 Reference<frame::XModel > xModel( mxOwnerDocument );
353                 Reference< XMultiServiceFactory> xSF( xModel, UNO_QUERY_THROW );
354                 mxCodeNameAccess.set( xSF->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.VBAObjectModuleObjectProvider" ) ) ), UNO_QUERY );
355             }
356             catch( Exception& ) {}
357 
358             if( mxCodeNameAccess.is() )
359             {
360                 try
361                 {
362                     aModInfo.ModuleObject.set( mxCodeNameAccess->getByName( aElementName), uno::UNO_QUERY );
363                 }
364                 catch(uno::Exception&)
365                 {
366                     OSL_TRACE("Failed to get documument object for %s", rtl::OUStringToOString( aElementName, RTL_TEXTENCODING_UTF8 ).getStr() );
367                 }
368             }
369         }
370 
371         Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( xLib, UNO_QUERY );
372         if( xVBAModuleInfo.is() )
373         {
374             if( xVBAModuleInfo->hasModuleInfo( aElementName ) )
375                 xVBAModuleInfo->removeModuleInfo( aElementName );
376             xVBAModuleInfo->insertModuleInfo( aElementName, aModInfo );
377         }
378     }
379 
380     return aRetAny;
381 }
382 
createInstanceImpl(void)383 SfxLibraryContainer* SfxScriptLibraryContainer::createInstanceImpl( void )
384 {
385     return new SfxScriptLibraryContainer();
386 }
387 
importFromOldStorage(const::rtl::OUString & aFile)388 void SAL_CALL SfxScriptLibraryContainer::importFromOldStorage( const ::rtl::OUString& aFile )
389 {
390     // TODO: move loading from old storage to binary filters?
391     SotStorageRef xStorage = new SotStorage( sal_False, aFile );
392     if( xStorage.Is() && xStorage->GetError() == ERRCODE_NONE )
393     {
394         // We need a BasicManager to avoid problems
395         // StarBASIC* pBas = new StarBASIC();
396         BasicManager* pBasicManager = new BasicManager( *(SotStorage*)xStorage, aFile );
397 
398         // Set info
399         LibraryContainerInfo aInfo( this, NULL, static_cast< OldBasicPassword* >( this ) );
400         pBasicManager->SetLibraryContainerInfo( aInfo );
401 
402         // Now the libraries should be copied to this SfxScriptLibraryContainer
403         BasicManager::LegacyDeleteBasicManager( pBasicManager );
404     }
405 }
406 
407 
408 // Storing with password encryption
409 
410 // Methods XLibraryContainerPassword
isLibraryPasswordProtected(const OUString & Name)411 sal_Bool SAL_CALL SfxScriptLibraryContainer::isLibraryPasswordProtected( const OUString& Name )
412     throw (NoSuchElementException, RuntimeException)
413 {
414     LibraryContainerMethodGuard aGuard( *this );
415     SfxLibrary* pImplLib = getImplLib( Name );
416     sal_Bool bRet = pImplLib->mbPasswordProtected;
417     return bRet;
418 }
419 
isLibraryPasswordVerified(const OUString & Name)420 sal_Bool SAL_CALL SfxScriptLibraryContainer::isLibraryPasswordVerified( const OUString& Name )
421     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
422 {
423     LibraryContainerMethodGuard aGuard( *this );
424     SfxLibrary* pImplLib = getImplLib( Name );
425     if( !pImplLib->mbPasswordProtected )
426         throw IllegalArgumentException();
427     sal_Bool bRet = pImplLib->mbPasswordVerified;
428     return bRet;
429 }
430 
verifyLibraryPassword(const OUString & Name,const OUString & Password)431 sal_Bool SAL_CALL SfxScriptLibraryContainer::verifyLibraryPassword
432     ( const OUString& Name, const OUString& Password )
433         throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
434 {
435     LibraryContainerMethodGuard aGuard( *this );
436     SfxLibrary* pImplLib = getImplLib( Name );
437     if( !pImplLib->mbPasswordProtected || pImplLib->mbPasswordVerified )
438         throw IllegalArgumentException();
439 
440     // Test password
441     sal_Bool bSuccess = sal_False;
442     if( pImplLib->mbDoc50Password )
443     {
444         bSuccess = ( Password == pImplLib->maPassword );
445         if( bSuccess )
446             pImplLib->mbPasswordVerified = sal_True;
447     }
448     else
449     {
450         pImplLib->maPassword = Password;
451         bSuccess = implLoadPasswordLibrary( pImplLib, Name, sal_True );
452         if( bSuccess )
453         {
454             // The library gets modified by verifiying the password, because other-
455             // wise for saving the storage would be copied and that doesn't work
456             // with mtg's storages when the password is verified
457             pImplLib->implSetModified( sal_True );
458             pImplLib->mbPasswordVerified = sal_True;
459 
460             // Reload library to get source
461             if( pImplLib->mbLoaded )
462                 implLoadPasswordLibrary( pImplLib, Name );
463         }
464     }
465     return bSuccess;
466 }
467 
changeLibraryPassword(const OUString & Name,const OUString & OldPassword,const OUString & NewPassword)468 void SAL_CALL SfxScriptLibraryContainer::changeLibraryPassword( const OUString& Name,
469     const OUString& OldPassword, const OUString& NewPassword )
470         throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
471 {
472     LibraryContainerMethodGuard aGuard( *this );
473     SfxLibrary* pImplLib = getImplLib( Name );
474     if( OldPassword == NewPassword )
475         return;
476 
477     sal_Bool bOldPassword = !OldPassword.isEmpty();
478     sal_Bool bNewPassword = !NewPassword.isEmpty();
479     sal_Bool bStorage = mxStorage.is() && !pImplLib->mbLink;
480 
481     if( pImplLib->mbReadOnly || (bOldPassword && !pImplLib->mbPasswordProtected) )
482         throw IllegalArgumentException();
483 
484     // Library must be loaded
485     loadLibrary( Name );
486 
487     sal_Bool bKillCryptedFiles = sal_False;
488     sal_Bool bKillUncryptedFiles = sal_False;
489 
490     // Remove or change password?
491     if( bOldPassword )
492     {
493         if( isLibraryPasswordVerified( Name ) )
494         {
495             if( pImplLib->maPassword != OldPassword )
496                 throw IllegalArgumentException();
497         }
498         else
499         {
500             if( !verifyLibraryPassword( Name, OldPassword ) )
501                 throw IllegalArgumentException();
502 
503             // Reload library to get source
504             // Should be done in verifyLibraryPassword loadLibrary( Name );
505         }
506 
507         if( !bNewPassword )
508         {
509             pImplLib->mbPasswordProtected = sal_False;
510             pImplLib->mbPasswordVerified = sal_False;
511             pImplLib->maPassword = OUString();
512 
513             maModifiable.setModified( sal_True );
514             pImplLib->implSetModified( sal_True );
515 
516             if( !bStorage && !pImplLib->mbDoc50Password )
517             {
518                 // Store application basic uncrypted
519                 uno::Reference< embed::XStorage > xStorage;
520                 storeLibraries_Impl( xStorage, sal_False );
521                 bKillCryptedFiles = sal_True;
522             }
523         }
524     }
525 
526     // Set new password?
527     if( bNewPassword )
528     {
529         pImplLib->mbPasswordProtected = sal_True;
530         pImplLib->mbPasswordVerified = sal_True;
531         pImplLib->maPassword = NewPassword;
532 
533         maModifiable.setModified( sal_True );
534         pImplLib->implSetModified( sal_True );
535 
536         if( !bStorage && !pImplLib->mbDoc50Password )
537         {
538             // Store applictaion basic crypted
539             uno::Reference< embed::XStorage > xStorage;
540             storeLibraries_Impl( xStorage, sal_False );
541             bKillUncryptedFiles = sal_True;
542         }
543     }
544 
545     if( bKillCryptedFiles || bKillUncryptedFiles )
546     {
547         Sequence< OUString > aElementNames = pImplLib->getElementNames();
548         sal_Int32 nNameCount = aElementNames.getLength();
549         const OUString* pNames = aElementNames.getConstArray();
550         OUString aLibDirPath = createAppLibraryFolder( pImplLib, Name );
551         try
552         {
553             for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
554             {
555                 OUString aElementName = pNames[ i ];
556 
557                 INetURLObject aElementInetObj( aLibDirPath );
558                 aElementInetObj.insertName( aElementName, sal_False,
559                     INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
560                 if( bKillUncryptedFiles )
561                     aElementInetObj.setExtension( maLibElementFileExtension );
562                 else
563                     aElementInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("pba") ) );
564                 String aElementPath( aElementInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
565 
566                 if( mxSFI->exists( aElementPath ) )
567                     mxSFI->kill( aElementPath );
568             }
569         }
570         catch( Exception& ) {}
571     }
572 }
573 
574 
setStreamKey(uno::Reference<io::XStream> xStream,const::rtl::OUString & aPass)575 void setStreamKey( uno::Reference< io::XStream > xStream, const ::rtl::OUString& aPass )
576 {
577     uno::Reference< embed::XEncryptionProtectedSource > xEncrStream( xStream, uno::UNO_QUERY );
578     if ( xEncrStream.is() )
579         xEncrStream->setEncryptionPassword( aPass );
580 }
581 
582 
583 // Impl methods
implStorePasswordLibrary(SfxLibrary * pLib,const::rtl::OUString & aName,const uno::Reference<embed::XStorage> & xStorage,const::com::sun::star::uno::Reference<::com::sun::star::task::XInteractionHandler> & xHandler)584 sal_Bool SfxScriptLibraryContainer::implStorePasswordLibrary( SfxLibrary* pLib,
585     const ::rtl::OUString& aName, const uno::Reference< embed::XStorage >& xStorage, const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler )
586 {
587     OUString aDummyLocation;
588     Reference< XSimpleFileAccess > xDummySFA;
589     return implStorePasswordLibrary( pLib, aName, xStorage, aDummyLocation, xDummySFA, xHandler );
590 }
591 
implStorePasswordLibrary(SfxLibrary * pLib,const::rtl::OUString & aName,const::com::sun::star::uno::Reference<::com::sun::star::embed::XStorage> & xStorage,const::rtl::OUString & aTargetURL,const Reference<XSimpleFileAccess> xToUseSFI,const::com::sun::star::uno::Reference<::com::sun::star::task::XInteractionHandler> & xHandler)592 sal_Bool SfxScriptLibraryContainer::implStorePasswordLibrary( SfxLibrary* pLib, const ::rtl::OUString& aName,
593                         const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage,
594                         const ::rtl::OUString& aTargetURL, const Reference< XSimpleFileAccess > xToUseSFI, const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler )
595 {
596     bool bExport = aTargetURL.getLength();
597 
598     BasicManager* pBasicMgr = getBasicManager();
599     OSL_ENSURE( pBasicMgr, "SfxScriptLibraryContainer::implStorePasswordLibrary: cannot do this without a BasicManager!" );
600     if ( !pBasicMgr )
601         return sal_False;
602 
603     // Only need to handle the export case here,
604     // save/saveas etc are handled in sfxbasemodel::storeSelf &
605     // sfxbasemodel::impl_store
606     uno::Sequence<rtl::OUString> aNames;
607     if ( bExport && pBasicMgr->LegacyPsswdBinaryLimitExceeded(aNames) )
608     {
609         if ( xHandler.is() )
610         {
611             ModuleSizeExceeded* pReq =  new ModuleSizeExceeded( aNames );
612             uno::Reference< task::XInteractionRequest > xReq( pReq );
613             xHandler->handle( xReq );
614             if ( pReq->isAbort() )
615                 throw util::VetoException();
616         }
617     }
618 
619     StarBASIC* pBasicLib = pBasicMgr->GetLib( aName );
620     if( !pBasicLib )
621         return sal_False;
622 
623     Sequence< OUString > aElementNames = pLib->getElementNames();
624     sal_Int32 nNameCount = aElementNames.getLength();
625     const OUString* pNames = aElementNames.getConstArray();
626 
627     sal_Bool bLink = pLib->mbLink;
628     sal_Bool bStorage = xStorage.is() && !bLink;
629     if( bStorage )
630     {
631         for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
632         {
633             OUString aElementName = pNames[ i ];
634 
635             // Write binary image stream
636             SbModule* pMod = pBasicLib->FindModule( aElementName );
637             if( pMod )
638             {
639                 //OUString aCodeStreamName( RTL_CONSTASCII_USTRINGPARAM("code.bin") );
640                 OUString aCodeStreamName = aElementName;
641                 aCodeStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".bin") );
642 
643                 try {
644                     uno::Reference< io::XStream > xCodeStream = xStorage->openStreamElement(
645                                         aCodeStreamName,
646                                         embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
647 
648                     if ( !xCodeStream.is() )
649                         throw uno::RuntimeException();
650 
651                     SvMemoryStream aMemStream;
652                     /*sal_Bool bStore = */pMod->StoreBinaryData( aMemStream );
653 
654                     sal_Int32 nSize = (sal_Int32)aMemStream.Tell();
655                     Sequence< sal_Int8 > aBinSeq( nSize );
656                     sal_Int8* pData = aBinSeq.getArray();
657                     ::rtl_copyMemory( pData, aMemStream.GetData(), nSize );
658 
659                     Reference< XOutputStream > xOut = xCodeStream->getOutputStream();
660                     if ( !xOut.is() )
661                         throw io::IOException(); // access denied because the stream is readonly
662 
663                     xOut->writeBytes( aBinSeq );
664                     xOut->closeOutput();
665                 }
666                 catch( uno::Exception& )
667                 {
668                     // TODO: handle error
669                 }
670             }
671 
672             if( pLib->mbPasswordVerified || pLib->mbDoc50Password )
673             {
674                 /*Any aElement = pLib->getByName( aElementName );*/
675                 if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
676                 {
677                 #if OSL_DEBUG_LEVEL > 0
678                     ::rtl::OStringBuffer aMessage;
679                     aMessage.append( "invalid library element '" );
680                     aMessage.append( ::rtl::OUStringToOString( aElementName, osl_getThreadTextEncoding() ) );
681                     aMessage.append( "'." );
682                     OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
683                 #endif
684                     continue;
685                 }
686 
687                 OUString aSourceStreamName = aElementName;
688                 aSourceStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
689 
690                 try {
691                     uno::Reference< io::XStream > xSourceStream = xStorage->openStreamElement(
692                                                                 aSourceStreamName,
693                                                                 embed::ElementModes::READWRITE );
694                     uno::Reference< beans::XPropertySet > xProps( xSourceStream, uno::UNO_QUERY );
695                     if ( !xProps.is() )
696                         throw uno::RuntimeException();
697 
698                     String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
699                     OUString aMime( RTL_CONSTASCII_USTRINGPARAM("text/xml") );
700                     xProps->setPropertyValue( aPropName, uno::makeAny( aMime ) );
701 
702                     // Set encryption key
703                     setStreamKey( xSourceStream, pLib->maPassword );
704 
705                     Reference< XOutputStream > xOutput = xSourceStream->getOutputStream();
706                     Reference< XNameContainer > xLib( pLib );
707                     writeLibraryElement( xLib, aElementName, xOutput );
708                     // writeLibraryElement should have the stream already closed
709                     // xOutput->closeOutput();
710                 }
711                 catch( uno::Exception& )
712                 {
713                     OSL_ENSURE( sal_False, "Problem on storing of password library!\n" );
714                     // TODO: error handling
715                 }
716             }
717             else    // !mbPasswordVerified
718             {
719                 // TODO
720                 // What to do if not verified?! In any case it's already loaded here
721             }
722         }
723 
724     }
725     // Application libraries have only to be saved if the password
726     // is verified because otherwise they can't be modified
727     else if( pLib->mbPasswordVerified || bExport )
728     {
729         try
730         {
731             Reference< XSimpleFileAccess > xSFI = mxSFI;
732             if( xToUseSFI.is() )
733                 xSFI = xToUseSFI;
734 
735             OUString aLibDirPath;
736             if( bExport )
737             {
738                 INetURLObject aInetObj( aTargetURL );
739                 aInetObj.insertName( aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
740                 aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
741 
742                 if( !xSFI->isFolder( aLibDirPath ) )
743                     xSFI->createFolder( aLibDirPath );
744             }
745             else
746             {
747                 aLibDirPath = createAppLibraryFolder( pLib, aName );
748             }
749 
750             for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
751             {
752                 OUString aElementName = pNames[ i ];
753 
754                 INetURLObject aElementInetObj( aLibDirPath );
755                 aElementInetObj.insertName( aElementName, sal_False,
756                     INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
757                 aElementInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("pba") ) );
758                 String aElementPath = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
759 
760                 /*Any aElement = pLib->getByName( aElementName );*/
761                 if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
762                 {
763                 #if OSL_DEBUG_LEVEL > 0
764                     ::rtl::OStringBuffer aMessage;
765                     aMessage.append( "invalid library element '" );
766                     aMessage.append( ::rtl::OUStringToOString( aElementName, osl_getThreadTextEncoding() ) );
767                     aMessage.append( "'." );
768                     OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
769                 #endif
770                     continue;
771                 }
772 
773                 try
774                 {
775                     uno::Reference< embed::XStorage > xElementRootStorage =
776                                                             ::comphelper::OStorageHelper::GetStorageFromURL(
777                                                                     aElementPath,
778                                                                     embed::ElementModes::READWRITE );
779                     if ( !xElementRootStorage.is() )
780                         throw uno::RuntimeException();
781 
782                     // Write binary image stream
783                     SbModule* pMod = pBasicLib->FindModule( aElementName );
784                     if( pMod )
785                     {
786                         OUString aCodeStreamName( RTL_CONSTASCII_USTRINGPARAM("code.bin") );
787 
788                         uno::Reference< io::XStream > xCodeStream = xElementRootStorage->openStreamElement(
789                                             aCodeStreamName,
790                                             embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
791 
792                         SvMemoryStream aMemStream;
793                         /*sal_Bool bStore = */pMod->StoreBinaryData( aMemStream );
794 
795                         sal_Int32 nSize = (sal_Int32)aMemStream.Tell();
796                         Sequence< sal_Int8 > aBinSeq( nSize );
797                         sal_Int8* pData = aBinSeq.getArray();
798                         ::rtl_copyMemory( pData, aMemStream.GetData(), nSize );
799 
800                         Reference< XOutputStream > xOut = xCodeStream->getOutputStream();
801                         if ( xOut.is() )
802                         {
803                             xOut->writeBytes( aBinSeq );
804                             xOut->closeOutput();
805                         }
806                     }
807 
808                     // Write encrypted source stream
809                     OUString aSourceStreamName( RTL_CONSTASCII_USTRINGPARAM("source.xml") );
810 
811                     uno::Reference< io::XStream > xSourceStream;
812                     try
813                     {
814                         xSourceStream = xElementRootStorage->openStreamElement(
815                             aSourceStreamName,
816                             embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
817 
818                         // #87671 Allow encryption
819                         uno::Reference< embed::XEncryptionProtectedSource > xEncr( xSourceStream, uno::UNO_QUERY );
820                         OSL_ENSURE( xEncr.is(),
821                                     "StorageStream opened for writing must implement XEncryptionProtectedSource!\n" );
822                         if ( !xEncr.is() )
823                             throw uno::RuntimeException();
824                         xEncr->setEncryptionPassword( pLib->maPassword );
825                     }
826                     catch( ::com::sun::star::packages::WrongPasswordException& )
827                     {
828                         xSourceStream = xElementRootStorage->openEncryptedStreamElement(
829                             aSourceStreamName,
830                             embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE,
831                             pLib->maPassword );
832                     }
833 
834                     uno::Reference< beans::XPropertySet > xProps( xSourceStream, uno::UNO_QUERY );
835                     if ( !xProps.is() )
836                         throw uno::RuntimeException();
837                     String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
838                     OUString aMime( RTL_CONSTASCII_USTRINGPARAM("text/xml") );
839                     xProps->setPropertyValue( aPropName, uno::makeAny( aMime ) );
840 
841                     Reference< XOutputStream > xOut = xSourceStream->getOutputStream();
842                     Reference< XNameContainer > xLib( pLib );
843                     writeLibraryElement( xLib, aElementName, xOut );
844                     // i50568: sax writer already closes stream
845                     // xOut->closeOutput();
846 
847                     uno::Reference< embed::XTransactedObject > xTransact( xElementRootStorage, uno::UNO_QUERY );
848                     OSL_ENSURE( xTransact.is(), "The storage must implement XTransactedObject!\n" );
849                     if ( !xTransact.is() )
850                         throw uno::RuntimeException();
851 
852                     xTransact->commit();
853                 }
854                 catch( uno::Exception& )
855                 {
856                     // TODO: handle error
857                 }
858 
859                 // Storage Dtor commits too, that makes problems
860                 // xElementRootStorage->Commit();
861             }
862         }
863         catch( Exception& )
864         {
865             //throw e;
866         }
867     }
868     return sal_True;
869 }
870 
implLoadPasswordLibrary(SfxLibrary * pLib,const OUString & Name,sal_Bool bVerifyPasswordOnly)871 sal_Bool SfxScriptLibraryContainer::implLoadPasswordLibrary
872     ( SfxLibrary* pLib, const OUString& Name, sal_Bool bVerifyPasswordOnly )
873         throw(WrappedTargetException, RuntimeException)
874 {
875     sal_Bool bRet = sal_True;
876 
877     sal_Bool bLink = pLib->mbLink;
878     sal_Bool bStorage = mxStorage.is() && !bLink;
879 
880     // Already loaded? Then only verifiedPassword can change something
881     SfxScriptLibrary* pScriptLib = static_cast< SfxScriptLibrary* >( pLib );
882     if( pScriptLib->mbLoaded )
883     {
884         if( pScriptLib->mbLoadedBinary && !bVerifyPasswordOnly &&
885             (pScriptLib->mbLoadedSource || !pLib->mbPasswordVerified) )
886                 return sal_False;
887     }
888 
889     StarBASIC* pBasicLib = NULL;
890     sal_Bool bLoadBinary = sal_False;
891     if( !pScriptLib->mbLoadedBinary && !bVerifyPasswordOnly && !pLib->mbPasswordVerified )
892     {
893         BasicManager* pBasicMgr = getBasicManager();
894         OSL_ENSURE( pBasicMgr, "SfxScriptLibraryContainer::implLoadPasswordLibrary: cannot do this without a BasicManager!" );
895         sal_Bool bLoaded = pScriptLib->mbLoaded;
896         pScriptLib->mbLoaded = sal_True;        // Necessary to get lib
897         pBasicLib = pBasicMgr ? pBasicMgr->GetLib( Name ) : NULL;
898         pScriptLib->mbLoaded = bLoaded;    // Restore flag
899         if( !pBasicLib )
900             return sal_False;
901 
902         bLoadBinary = sal_True;
903         pScriptLib->mbLoadedBinary = sal_True;
904     }
905 
906     sal_Bool bLoadSource = sal_False;
907     if( !pScriptLib->mbLoadedSource && pLib->mbPasswordVerified && !bVerifyPasswordOnly )
908     {
909         bLoadSource = sal_True;
910         pScriptLib->mbLoadedSource = sal_True;
911     }
912 
913     Sequence< OUString > aElementNames = pLib->getElementNames();
914     sal_Int32 nNameCount = aElementNames.getLength();
915     const OUString* pNames = aElementNames.getConstArray();
916 
917     if( bStorage )
918     {
919         uno::Reference< embed::XStorage > xLibrariesStor;
920         uno::Reference< embed::XStorage > xLibraryStor;
921         if( bStorage )
922         {
923             try {
924                 xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
925                 if ( !xLibrariesStor.is() )
926                     throw uno::RuntimeException();
927 
928                 xLibraryStor = xLibrariesStor->openStorageElement( Name, embed::ElementModes::READ );
929                 if ( !xLibraryStor.is() )
930                     throw uno::RuntimeException();
931             }
932             catch( uno::Exception& )
933             {
934                 OSL_ENSURE( 0, "### couln't open sub storage for library\n" );
935                 return sal_False;
936             }
937         }
938 
939         for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
940         {
941             OUString aElementName = pNames[ i ];
942 
943             // Load binary
944             if( bLoadBinary )
945             {
946                 SbModule* pMod = pBasicLib->FindModule( aElementName );
947                 if( !pMod )
948                 {
949                     pMod = pBasicLib->MakeModule( aElementName, String() );
950                     pBasicLib->SetModified( sal_False );
951                 }
952 
953                 //OUString aCodeStreamName( RTL_CONSTASCII_USTRINGPARAM("code.bin") );
954                 OUString aCodeStreamName= aElementName;
955                 aCodeStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".bin") );
956 
957                 try {
958                     uno::Reference< io::XStream > xCodeStream = xLibraryStor->openStreamElement(
959                                                                                         aCodeStreamName,
960                                                                                         embed::ElementModes::READ );
961                     if ( !xCodeStream.is() )
962                         throw uno::RuntimeException();
963 
964                     SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xCodeStream );
965                     if ( !pStream || pStream->GetError() )
966                     {
967                         sal_Int32 nError = pStream ? pStream->GetError() : ERRCODE_IO_GENERAL;
968                         delete pStream;
969                         throw task::ErrorCodeIOException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), nError );
970                     }
971 
972                     /*sal_Bool bRet = */pMod->LoadBinaryData( *pStream );
973                     // TODO: Check return value
974 
975                     delete pStream;
976                 }
977                 catch( uno::Exception& )
978                 {
979                     // TODO: error handling
980                 }
981             }
982 
983             // Load source
984             if( bLoadSource || bVerifyPasswordOnly )
985             {
986                 // Access encrypted source stream
987                 OUString aSourceStreamName = aElementName;
988                 aSourceStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
989 
990                 try {
991                     uno::Reference< io::XStream > xSourceStream = xLibraryStor->openEncryptedStreamElement(
992                                                                     aSourceStreamName,
993                                                                     embed::ElementModes::READ,
994                                                                     pLib->maPassword );
995                     if ( !xSourceStream.is() )
996                         throw uno::RuntimeException();
997 
998                     // if this point is reached then the password is correct
999                     if ( !bVerifyPasswordOnly )
1000                     {
1001                         uno::Reference< io::XInputStream > xInStream = xSourceStream->getInputStream();
1002                         if ( !xInStream.is() )
1003                             throw io::IOException(); // read access denied, seems to be impossible
1004 
1005                         Reference< XNameContainer > xLib( pLib );
1006                         Any aAny = importLibraryElement( xLib,
1007                                         aElementName, aSourceStreamName,
1008                                         xInStream );
1009                         if( pLib->hasByName( aElementName ) )
1010                         {
1011                             if( aAny.hasValue() )
1012                                 pLib->maNameContainer.replaceByName( aElementName, aAny );
1013                         }
1014                         else
1015                         {
1016                             pLib->maNameContainer.insertByName( aElementName, aAny );
1017                         }
1018                     }
1019                 }
1020                 catch( uno::Exception& )
1021                 {
1022                     bRet = sal_False;
1023                 }
1024             }
1025         }
1026     }
1027     else
1028     {
1029         try
1030         {
1031             OUString aLibDirPath = createAppLibraryFolder( pLib, Name );
1032 
1033             for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
1034             {
1035                 OUString aElementName = pNames[ i ];
1036 
1037                 INetURLObject aElementInetObj( aLibDirPath );
1038                 aElementInetObj.insertName( aElementName, sal_False,
1039                     INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1040                 aElementInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("pba") ) );
1041                 String aElementPath = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
1042 
1043                 uno::Reference< embed::XStorage > xElementRootStorage;
1044                 try {
1045                     xElementRootStorage = ::comphelper::OStorageHelper::GetStorageFromURL(
1046                                                                     aElementPath,
1047                                                                     embed::ElementModes::READ );
1048                 } catch( uno::Exception& )
1049                 {
1050                     // TODO: error handling
1051                 }
1052 
1053                 if ( xElementRootStorage.is() )
1054                 {
1055                     // Load binary
1056                     if( bLoadBinary )
1057                     {
1058                         SbModule* pMod = pBasicLib->FindModule( aElementName );
1059                         if( !pMod )
1060                         {
1061                             pMod = pBasicLib->MakeModule( aElementName, String() );
1062                             pBasicLib->SetModified( sal_False );
1063                         }
1064 
1065                         try {
1066                             OUString aCodeStreamName( RTL_CONSTASCII_USTRINGPARAM("code.bin") );
1067                             uno::Reference< io::XStream > xCodeStream = xElementRootStorage->openStreamElement(
1068                                                                         aCodeStreamName,
1069                                                                         embed::ElementModes::READ );
1070 
1071                             SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xCodeStream );
1072                             if ( !pStream || pStream->GetError() )
1073                             {
1074                                 sal_Int32 nError = pStream ? pStream->GetError() : ERRCODE_IO_GENERAL;
1075                                 delete pStream;
1076                                 throw task::ErrorCodeIOException( ::rtl::OUString(),
1077                                                                     uno::Reference< uno::XInterface >(),
1078                                                                     nError );
1079                             }
1080 
1081                             /*sal_Bool bRet = */pMod->LoadBinaryData( *pStream );
1082                             // TODO: Check return value
1083 
1084                             delete pStream;
1085                         }
1086                         catch( uno::Exception& )
1087                         {
1088                             // TODO: error handling
1089                         }
1090                     }
1091 
1092                     // Load source
1093                     if( bLoadSource || bVerifyPasswordOnly )
1094                     {
1095                         // Access encrypted source stream
1096                         OUString aSourceStreamName( RTL_CONSTASCII_USTRINGPARAM("source.xml") );
1097                         try {
1098                             uno::Reference< io::XStream > xSourceStream = xElementRootStorage->openEncryptedStreamElement(
1099                                                                     aSourceStreamName,
1100                                                                     embed::ElementModes::READ,
1101                                                                     pLib->maPassword );
1102                             if ( !xSourceStream.is() )
1103                                 throw uno::RuntimeException();
1104 
1105                             if ( !bVerifyPasswordOnly )
1106                             {
1107                                 uno::Reference< io::XInputStream > xInStream = xSourceStream->getInputStream();
1108                                 if ( !xInStream.is() )
1109                                     throw io::IOException(); // read access denied, seems to be impossible
1110 
1111                                 Reference< XNameContainer > xLib( pLib );
1112                                 Any aAny = importLibraryElement( xLib,
1113                                                 aElementName,
1114                                                 aSourceStreamName,
1115                                                 xInStream );
1116                                 if( pLib->hasByName( aElementName ) )
1117                                 {
1118                                     if( aAny.hasValue() )
1119                                         pLib->maNameContainer.replaceByName( aElementName, aAny );
1120                                 }
1121                                 else
1122                                 {
1123                                     pLib->maNameContainer.insertByName( aElementName, aAny );
1124                                 }
1125                             }
1126                         }
1127                         catch ( uno::Exception& )
1128                         {
1129                             bRet = sal_False;
1130                         }
1131                     }
1132                 }
1133             }
1134 
1135         }
1136         catch( Exception& )
1137         {
1138             // TODO
1139             //throw e;
1140         }
1141     }
1142 
1143 //REMOVE        // If the password is verified the library must remain modified, because
1144 //REMOVE        // otherwise for saving the storage would be copied and that doesn't work
1145 //REMOVE        // with mtg's storages when the password is verified
1146 //REMOVE        if( !pLib->mbPasswordVerified )
1147 //REMOVE            pLib->mbModified = sal_False;
1148     return bRet;
1149 }
1150 
1151 
onNewRootStorage()1152 void SfxScriptLibraryContainer::onNewRootStorage()
1153 {
1154 }
1155 
1156 //============================================================================
1157 // Service
createRegistryInfo_SfxScriptLibraryContainer()1158 void createRegistryInfo_SfxScriptLibraryContainer()
1159 {
1160     static OAutoRegistration< SfxScriptLibraryContainer > aAutoRegistration;
1161 }
1162 
getImplementationName()1163 ::rtl::OUString SAL_CALL SfxScriptLibraryContainer::getImplementationName( ) throw (RuntimeException)
1164 {
1165     return getImplementationName_static();
1166 }
1167 
getSupportedServiceNames()1168 Sequence< ::rtl::OUString > SAL_CALL SfxScriptLibraryContainer::getSupportedServiceNames( ) throw (RuntimeException)
1169 {
1170     return getSupportedServiceNames_static();
1171 }
1172 
getSupportedServiceNames_static()1173 Sequence< OUString > SfxScriptLibraryContainer::getSupportedServiceNames_static()
1174 {
1175     Sequence< OUString > aServiceNames( 2 );
1176     aServiceNames[0] = OUString::createFromAscii( "com.sun.star.script.DocumentScriptLibraryContainer" );
1177     // plus, for compatibility:
1178     aServiceNames[1] = OUString::createFromAscii( "com.sun.star.script.ScriptLibraryContainer" );
1179     return aServiceNames;
1180 }
1181 
getImplementationName_static()1182 OUString SfxScriptLibraryContainer::getImplementationName_static()
1183 {
1184     static OUString aImplName;
1185     static sal_Bool bNeedsInit = sal_True;
1186 
1187     MutexGuard aGuard( Mutex::getGlobalMutex() );
1188     if( bNeedsInit )
1189     {
1190         aImplName = OUString::createFromAscii( "com.sun.star.comp.sfx2.ScriptLibraryContainer" );
1191         bNeedsInit = sal_False;
1192     }
1193     return aImplName;
1194 }
1195 
Create(const Reference<XComponentContext> &)1196 Reference< XInterface > SAL_CALL SfxScriptLibraryContainer::Create
1197     ( const Reference< XComponentContext >& )
1198         throw( Exception )
1199 {
1200     Reference< XInterface > xRet =
1201         static_cast< XInterface* >( static_cast< OWeakObject* >(new SfxScriptLibraryContainer()) );
1202     return xRet;
1203 }
1204 
1205 //============================================================================
1206 // Implementation class SfxScriptLibrary
1207 
1208 // Ctor
SfxScriptLibrary(ModifiableHelper & _rModifiable,const Reference<XMultiServiceFactory> & xMSF,const Reference<XSimpleFileAccess> & xSFI)1209 SfxScriptLibrary::SfxScriptLibrary( ModifiableHelper& _rModifiable,
1210                                     const Reference< XMultiServiceFactory >& xMSF,
1211                                     const Reference< XSimpleFileAccess >& xSFI )
1212     : SfxLibrary( _rModifiable, getCppuType( (const OUString *)0 ), xMSF, xSFI )
1213     , mbLoadedSource( sal_False )
1214     , mbLoadedBinary( sal_False )
1215 {
1216 }
1217 
SfxScriptLibrary(ModifiableHelper & _rModifiable,const Reference<XMultiServiceFactory> & xMSF,const Reference<XSimpleFileAccess> & xSFI,const OUString & aLibInfoFileURL,const OUString & aStorageURL,sal_Bool ReadOnly)1218 SfxScriptLibrary::SfxScriptLibrary( ModifiableHelper& _rModifiable,
1219                                     const Reference< XMultiServiceFactory >& xMSF,
1220                                     const Reference< XSimpleFileAccess >& xSFI,
1221                                     const OUString& aLibInfoFileURL,
1222                                     const OUString& aStorageURL,
1223                                     sal_Bool ReadOnly )
1224     : SfxLibrary( _rModifiable, getCppuType( (const OUString *)0 ), xMSF, xSFI,
1225                         aLibInfoFileURL, aStorageURL, ReadOnly)
1226     , mbLoadedSource( sal_False )
1227     , mbLoadedBinary( sal_False )
1228 {
1229 }
1230 
1231 // Provide modify state including resources
isModified(void)1232 sal_Bool SfxScriptLibrary::isModified( void )
1233 {
1234     return implIsModified();    // No resources
1235 }
1236 
storeResources(void)1237 void SfxScriptLibrary::storeResources( void )
1238 {
1239     // No resources
1240 }
1241 
storeResourcesToURL(const::rtl::OUString & URL,const Reference<task::XInteractionHandler> & Handler)1242 void SfxScriptLibrary::storeResourcesToURL( const ::rtl::OUString& URL,
1243     const Reference< task::XInteractionHandler >& Handler )
1244 {
1245     (void)URL;
1246     (void)Handler;
1247 }
1248 
storeResourcesAsURL(const::rtl::OUString & URL,const::rtl::OUString & NewName)1249 void SfxScriptLibrary::storeResourcesAsURL
1250     ( const ::rtl::OUString& URL, const ::rtl::OUString& NewName )
1251 {
1252     (void)URL;
1253     (void)NewName;
1254 }
1255 
storeResourcesToStorage(const::com::sun::star::uno::Reference<::com::sun::star::embed::XStorage> & xStorage)1256 void SfxScriptLibrary::storeResourcesToStorage( const ::com::sun::star::uno::Reference
1257     < ::com::sun::star::embed::XStorage >& xStorage )
1258 {
1259     // No resources
1260     (void)xStorage;
1261 }
1262 
containsValidModule(const Any & aElement)1263 bool SfxScriptLibrary::containsValidModule( const Any& aElement )
1264 {
1265     OUString sModuleText;
1266     aElement >>= sModuleText;
1267     return ( !sModuleText.isEmpty() );
1268 }
1269 
isLibraryElementValid(::com::sun::star::uno::Any aElement) const1270 bool SAL_CALL SfxScriptLibrary::isLibraryElementValid( ::com::sun::star::uno::Any aElement ) const
1271 {
1272     return SfxScriptLibrary::containsValidModule( aElement );
1273 }
1274 
1275 IMPLEMENT_FORWARD_XINTERFACE2( SfxScriptLibrary, SfxLibrary, SfxScriptLibrary_BASE );
1276 IMPLEMENT_FORWARD_XTYPEPROVIDER2( SfxScriptLibrary, SfxLibrary, SfxScriptLibrary_BASE );
1277 
1278 script::ModuleInfo SAL_CALL
getModuleInfo(const::rtl::OUString & ModuleName)1279 SfxScriptLibrary::getModuleInfo( const ::rtl::OUString& ModuleName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
1280 {
1281     if ( !hasModuleInfo( ModuleName ) )
1282         throw NoSuchElementException();
1283     return mModuleInfos[ ModuleName ];
1284 }
1285 
1286 sal_Bool SAL_CALL
hasModuleInfo(const::rtl::OUString & ModuleName)1287 SfxScriptLibrary::hasModuleInfo( const ::rtl::OUString& ModuleName ) throw (RuntimeException)
1288 {
1289     sal_Bool bRes = sal_False;
1290     ModuleInfoMap::iterator it = mModuleInfos.find( ModuleName );
1291 
1292     if ( it != mModuleInfos.end() )
1293         bRes = sal_True;
1294 
1295     return bRes;
1296 }
1297 
insertModuleInfo(const::rtl::OUString & ModuleName,const script::ModuleInfo & ModuleInfo)1298 void SAL_CALL SfxScriptLibrary::insertModuleInfo( const ::rtl::OUString& ModuleName, const script::ModuleInfo& ModuleInfo ) throw (IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
1299 {
1300     if ( hasModuleInfo( ModuleName ) )
1301         throw ElementExistException();
1302     mModuleInfos[ ModuleName ] = ModuleInfo;
1303 }
1304 
removeModuleInfo(const::rtl::OUString & ModuleName)1305 void SAL_CALL SfxScriptLibrary::removeModuleInfo( const ::rtl::OUString& ModuleName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
1306 {
1307         // #FIXME add NoSuchElementException to the spec
1308     if ( !hasModuleInfo( ModuleName ) )
1309         throw NoSuchElementException();
1310     mModuleInfos.erase( mModuleInfos.find( ModuleName ) );
1311 }
1312 
1313 
1314 //============================================================================
1315 
1316 }   // namespace basic
1317