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