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