xref: /AOO41X/main/basic/source/uno/namecont.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 <com/sun/star/container/XNameContainer.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/container/XContainer.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
35*cdf0e10cSrcweir #include <vcl/svapp.hxx>
36*cdf0e10cSrcweir #include <vos/mutex.hxx>
37*cdf0e10cSrcweir #include <tools/errinf.hxx>
38*cdf0e10cSrcweir #include <osl/mutex.hxx>
39*cdf0e10cSrcweir #include <vos/diagnose.hxx>
40*cdf0e10cSrcweir #include <rtl/uri.hxx>
41*cdf0e10cSrcweir #include <rtl/strbuf.hxx>
42*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
43*cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include "namecont.hxx"
46*cdf0e10cSrcweir #include <basic/basicmanagerrepository.hxx>
47*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
48*cdf0e10cSrcweir #include <tools/urlobj.hxx>
49*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
50*cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
51*cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
52*cdf0e10cSrcweir #include <svtools/ehdl.hxx>
53*cdf0e10cSrcweir #include <basic/basmgr.hxx>
54*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/uno/DeploymentException.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/script/LibraryNotLoadedException.hpp>
64*cdf0e10cSrcweir #include <com/sun/star/script/vba/VBAScriptEventId.hpp>
65*cdf0e10cSrcweir #include <com/sun/star/deployment/ExtensionManager.hpp>
66*cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
67*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
68*cdf0e10cSrcweir #include <basic/sbmod.hxx>
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::uno;
76*cdf0e10cSrcweir using namespace com::sun::star::lang;
77*cdf0e10cSrcweir using namespace com::sun::star::io;
78*cdf0e10cSrcweir using namespace com::sun::star::ucb;
79*cdf0e10cSrcweir using namespace com::sun::star::script;
80*cdf0e10cSrcweir using namespace com::sun::star::beans;
81*cdf0e10cSrcweir using namespace com::sun::star::xml::sax;
82*cdf0e10cSrcweir using namespace com::sun::star::util;
83*cdf0e10cSrcweir using namespace com::sun::star::task;
84*cdf0e10cSrcweir using namespace com::sun::star::embed;
85*cdf0e10cSrcweir using namespace com::sun::star::frame;
86*cdf0e10cSrcweir using namespace com::sun::star::deployment;
87*cdf0e10cSrcweir using namespace com::sun::star;
88*cdf0e10cSrcweir using namespace cppu;
89*cdf0e10cSrcweir using namespace rtl;
90*cdf0e10cSrcweir using namespace osl;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir using com::sun::star::uno::Reference;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir // #i34411: Flag for error handling during migration
95*cdf0e10cSrcweir static bool GbMigrationSuppressErrors = false;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir //============================================================================
98*cdf0e10cSrcweir // Implementation class NameContainer
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir // Methods XElementAccess
101*cdf0e10cSrcweir Type NameContainer::getElementType()
102*cdf0e10cSrcweir 	throw(RuntimeException)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	return mType;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir sal_Bool NameContainer::hasElements()
108*cdf0e10cSrcweir 	throw(RuntimeException)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	sal_Bool bRet = (mnElementCount > 0);
111*cdf0e10cSrcweir 	return bRet;
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // Methods XNameAccess
115*cdf0e10cSrcweir Any NameContainer::getByName( const OUString& aName )
116*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir     NameContainerNameMap::iterator aIt = mHashMap.find( aName );
119*cdf0e10cSrcweir 	if( aIt == mHashMap.end() )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		throw NoSuchElementException();
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 	sal_Int32 iHashResult = (*aIt).second;
124*cdf0e10cSrcweir 	Any aRetAny = mValues.getConstArray()[ iHashResult ];
125*cdf0e10cSrcweir 	return aRetAny;
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir Sequence< OUString > NameContainer::getElementNames()
129*cdf0e10cSrcweir 	throw(RuntimeException)
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	return mNames;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir sal_Bool NameContainer::hasByName( const OUString& aName )
135*cdf0e10cSrcweir 	throw(RuntimeException)
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
138*cdf0e10cSrcweir 	sal_Bool bRet = ( aIt != mHashMap.end() );
139*cdf0e10cSrcweir 	return bRet;
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir // Methods XNameReplace
144*cdf0e10cSrcweir void NameContainer::replaceByName( const OUString& aName, const Any& aElement )
145*cdf0e10cSrcweir 	throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	Type aAnyType = aElement.getValueType();
148*cdf0e10cSrcweir     if( mType != aAnyType )
149*cdf0e10cSrcweir 		throw IllegalArgumentException();
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
152*cdf0e10cSrcweir 	if( aIt == mHashMap.end() )
153*cdf0e10cSrcweir 	{
154*cdf0e10cSrcweir 		throw NoSuchElementException();
155*cdf0e10cSrcweir 	}
156*cdf0e10cSrcweir 	sal_Int32 iHashResult = (*aIt).second;
157*cdf0e10cSrcweir 	Any aOldElement = mValues.getConstArray()[ iHashResult ];
158*cdf0e10cSrcweir 	mValues.getArray()[ iHashResult ] = aElement;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	// Fire event
162*cdf0e10cSrcweir 	if( maContainerListeners.getLength() > 0 )
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir     	ContainerEvent aEvent;
165*cdf0e10cSrcweir     	aEvent.Source = mpxEventSource;
166*cdf0e10cSrcweir     	aEvent.Accessor <<= aName;
167*cdf0e10cSrcweir     	aEvent.Element = aElement;
168*cdf0e10cSrcweir     	aEvent.ReplacedElement = aOldElement;
169*cdf0e10cSrcweir         maContainerListeners.notifyEach( &XContainerListener::elementReplaced, aEvent );
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     /*  After the container event has been fired (one listener will update the
173*cdf0e10cSrcweir         core Basic manager), fire change event. Listeners can rely that the
174*cdf0e10cSrcweir         Basic source code of the core Basic manager is up-to-date. */
175*cdf0e10cSrcweir     if( maChangesListeners.getLength() > 0 )
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         ChangesEvent aEvent;
178*cdf0e10cSrcweir         aEvent.Source = mpxEventSource;
179*cdf0e10cSrcweir         aEvent.Base <<= aEvent.Source;
180*cdf0e10cSrcweir         aEvent.Changes.realloc( 1 );
181*cdf0e10cSrcweir         aEvent.Changes[ 0 ].Accessor <<= aName;
182*cdf0e10cSrcweir         aEvent.Changes[ 0 ].Element <<= aElement;
183*cdf0e10cSrcweir     	aEvent.Changes[ 0 ].ReplacedElement = aOldElement;
184*cdf0e10cSrcweir         maChangesListeners.notifyEach( &XChangesListener::changesOccurred, aEvent );
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir // Methods XNameContainer
190*cdf0e10cSrcweir void NameContainer::insertByName( const OUString& aName, const Any& aElement )
191*cdf0e10cSrcweir 	throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir 	Type aAnyType = aElement.getValueType();
194*cdf0e10cSrcweir     if( mType != aAnyType )
195*cdf0e10cSrcweir 		throw IllegalArgumentException();
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
198*cdf0e10cSrcweir 	if( aIt != mHashMap.end() )
199*cdf0e10cSrcweir 	{
200*cdf0e10cSrcweir 		throw ElementExistException();
201*cdf0e10cSrcweir 	}
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	sal_Int32 nCount = mNames.getLength();
204*cdf0e10cSrcweir 	mNames.realloc( nCount + 1 );
205*cdf0e10cSrcweir 	mValues.realloc( nCount + 1 );
206*cdf0e10cSrcweir 	mNames.getArray()[ nCount ] = aName;
207*cdf0e10cSrcweir 	mValues.getArray()[ nCount ] = aElement;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	mHashMap[ aName ] = nCount;
210*cdf0e10cSrcweir 	mnElementCount++;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	// Fire event
213*cdf0e10cSrcweir 	if( maContainerListeners.getLength() > 0 )
214*cdf0e10cSrcweir 	{
215*cdf0e10cSrcweir     	ContainerEvent aEvent;
216*cdf0e10cSrcweir     	aEvent.Source = mpxEventSource;
217*cdf0e10cSrcweir     	aEvent.Accessor <<= aName;
218*cdf0e10cSrcweir     	aEvent.Element = aElement;
219*cdf0e10cSrcweir         maContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
220*cdf0e10cSrcweir 	}
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     /*  After the container event has been fired (one listener will update the
223*cdf0e10cSrcweir         core Basic manager), fire change event. Listeners can rely that the
224*cdf0e10cSrcweir         Basic source code of the core Basic manager is up-to-date. */
225*cdf0e10cSrcweir     if( maChangesListeners.getLength() > 0 )
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         ChangesEvent aEvent;
228*cdf0e10cSrcweir         aEvent.Source = mpxEventSource;
229*cdf0e10cSrcweir         aEvent.Base <<= aEvent.Source;
230*cdf0e10cSrcweir         aEvent.Changes.realloc( 1 );
231*cdf0e10cSrcweir         aEvent.Changes[ 0 ].Accessor <<= aName;
232*cdf0e10cSrcweir         aEvent.Changes[ 0 ].Element <<= aElement;
233*cdf0e10cSrcweir         maChangesListeners.notifyEach( &XChangesListener::changesOccurred, aEvent );
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir void NameContainer::removeByName( const OUString& aName )
238*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir 	NameContainerNameMap::iterator aIt = mHashMap.find( aName );
241*cdf0e10cSrcweir 	if( aIt == mHashMap.end() )
242*cdf0e10cSrcweir 	{
243*cdf0e10cSrcweir 		throw NoSuchElementException();
244*cdf0e10cSrcweir 	}
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 	sal_Int32 iHashResult = (*aIt).second;
247*cdf0e10cSrcweir 	Any aOldElement = mValues.getConstArray()[ iHashResult ];
248*cdf0e10cSrcweir 	mHashMap.erase( aIt );
249*cdf0e10cSrcweir 	sal_Int32 iLast = mNames.getLength() - 1;
250*cdf0e10cSrcweir 	if( iLast != iHashResult )
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		OUString* pNames = mNames.getArray();
253*cdf0e10cSrcweir 		Any* pValues = mValues.getArray();
254*cdf0e10cSrcweir 		pNames[ iHashResult ] = pNames[ iLast ];
255*cdf0e10cSrcweir 		pValues[ iHashResult ] = pValues[ iLast ];
256*cdf0e10cSrcweir 		mHashMap[ pNames[ iHashResult ] ] = iHashResult;
257*cdf0e10cSrcweir 	}
258*cdf0e10cSrcweir 	mNames.realloc( iLast );
259*cdf0e10cSrcweir 	mValues.realloc( iLast );
260*cdf0e10cSrcweir 	mnElementCount--;
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	// Fire event
263*cdf0e10cSrcweir 	if( maContainerListeners.getLength() > 0 )
264*cdf0e10cSrcweir 	{
265*cdf0e10cSrcweir     	ContainerEvent aEvent;
266*cdf0e10cSrcweir     	aEvent.Source = mpxEventSource;
267*cdf0e10cSrcweir     	aEvent.Accessor <<= aName;
268*cdf0e10cSrcweir     	aEvent.Element = aOldElement;
269*cdf0e10cSrcweir         maContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
270*cdf0e10cSrcweir 	}
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir     /*  After the container event has been fired (one listener will update the
273*cdf0e10cSrcweir         core Basic manager), fire change event. Listeners can rely that the
274*cdf0e10cSrcweir         Basic source code of the core Basic manager is up-to-date. */
275*cdf0e10cSrcweir     if( maChangesListeners.getLength() > 0 )
276*cdf0e10cSrcweir     {
277*cdf0e10cSrcweir         ChangesEvent aEvent;
278*cdf0e10cSrcweir         aEvent.Source = mpxEventSource;
279*cdf0e10cSrcweir         aEvent.Base <<= aEvent.Source;
280*cdf0e10cSrcweir         aEvent.Changes.realloc( 1 );
281*cdf0e10cSrcweir         aEvent.Changes[ 0 ].Accessor <<= aName;
282*cdf0e10cSrcweir         // aEvent.Changes[ 0 ].Element remains empty (meaning "replaced with nothing")
283*cdf0e10cSrcweir         aEvent.Changes[ 0 ].ReplacedElement = aOldElement;
284*cdf0e10cSrcweir         maChangesListeners.notifyEach( &XChangesListener::changesOccurred, aEvent );
285*cdf0e10cSrcweir     }
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir // Methods XContainer
290*cdf0e10cSrcweir void SAL_CALL NameContainer::addContainerListener( const Reference< XContainerListener >& xListener )
291*cdf0e10cSrcweir 	throw (RuntimeException)
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir 	if( !xListener.is() )
294*cdf0e10cSrcweir 		throw RuntimeException();
295*cdf0e10cSrcweir 	Reference< XInterface > xIface( xListener, UNO_QUERY );
296*cdf0e10cSrcweir 	maContainerListeners.addInterface( xIface );
297*cdf0e10cSrcweir }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir void SAL_CALL NameContainer::removeContainerListener( const Reference< XContainerListener >& xListener )
300*cdf0e10cSrcweir 	throw (RuntimeException)
301*cdf0e10cSrcweir {
302*cdf0e10cSrcweir 	if( !xListener.is() )
303*cdf0e10cSrcweir 		throw RuntimeException();
304*cdf0e10cSrcweir 	Reference< XInterface > xIface( xListener, UNO_QUERY );
305*cdf0e10cSrcweir 	maContainerListeners.removeInterface( xIface );
306*cdf0e10cSrcweir }
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir // Methods XChangesNotifier
309*cdf0e10cSrcweir void SAL_CALL NameContainer::addChangesListener( const Reference< XChangesListener >& xListener )
310*cdf0e10cSrcweir     throw (RuntimeException)
311*cdf0e10cSrcweir {
312*cdf0e10cSrcweir 	if( !xListener.is() )
313*cdf0e10cSrcweir 		throw RuntimeException();
314*cdf0e10cSrcweir 	Reference< XInterface > xIface( xListener, UNO_QUERY );
315*cdf0e10cSrcweir 	maChangesListeners.addInterface( xIface );
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir void SAL_CALL NameContainer::removeChangesListener( const Reference< XChangesListener >& xListener )
319*cdf0e10cSrcweir     throw (RuntimeException)
320*cdf0e10cSrcweir {
321*cdf0e10cSrcweir 	if( !xListener.is() )
322*cdf0e10cSrcweir 		throw RuntimeException();
323*cdf0e10cSrcweir 	Reference< XInterface > xIface( xListener, UNO_QUERY );
324*cdf0e10cSrcweir 	maChangesListeners.removeInterface( xIface );
325*cdf0e10cSrcweir }
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir //============================================================================
328*cdf0e10cSrcweir // ModifiableHelper
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir void ModifiableHelper::setModified( sal_Bool _bModified )
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir     if ( _bModified == mbModified )
333*cdf0e10cSrcweir         return;
334*cdf0e10cSrcweir     mbModified = _bModified;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir     if ( m_aModifyListeners.getLength() == 0 )
337*cdf0e10cSrcweir         return;
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir     EventObject aModifyEvent( m_rEventSource );
340*cdf0e10cSrcweir     m_aModifyListeners.notifyEach( &XModifyListener::modified, aModifyEvent );
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir //============================================================================
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir VBAScriptListenerContainer::VBAScriptListenerContainer( ::osl::Mutex& rMutex ) :
346*cdf0e10cSrcweir     VBAScriptListenerContainer_BASE( rMutex )
347*cdf0e10cSrcweir {
348*cdf0e10cSrcweir }
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir bool VBAScriptListenerContainer::implTypedNotify( const Reference< vba::XVBAScriptListener >& rxListener, const vba::VBAScriptEvent& rEvent ) throw (Exception)
351*cdf0e10cSrcweir {
352*cdf0e10cSrcweir     rxListener->notifyVBAScriptEvent( rEvent );
353*cdf0e10cSrcweir     return true;    // notify all other listeners too
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir //============================================================================
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir // Implementation class SfxLibraryContainer
359*cdf0e10cSrcweir DBG_NAME( SfxLibraryContainer )
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir // Ctor
362*cdf0e10cSrcweir SfxLibraryContainer::SfxLibraryContainer( void )
363*cdf0e10cSrcweir 	: SfxLibraryContainer_BASE( maMutex )
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir     , maVBAScriptListeners( maMutex )
366*cdf0e10cSrcweir     , mnRunningVBAScripts( 0 )
367*cdf0e10cSrcweir     , mbVBACompat( sal_False )
368*cdf0e10cSrcweir     , maModifiable( *this, maMutex )
369*cdf0e10cSrcweir     , maNameContainer( getCppuType( (Reference< XNameAccess >*) NULL ) )
370*cdf0e10cSrcweir     , mbOldInfoFormat( sal_False )
371*cdf0e10cSrcweir     , mbOasis2OOoFormat( sal_False )
372*cdf0e10cSrcweir     , mpBasMgr( NULL )
373*cdf0e10cSrcweir     , mbOwnBasMgr( sal_False )
374*cdf0e10cSrcweir {
375*cdf0e10cSrcweir     DBG_CTOR( SfxLibraryContainer, NULL );
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 	mxMSF = comphelper::getProcessServiceFactory();
378*cdf0e10cSrcweir 	if( !mxMSF.is() )
379*cdf0e10cSrcweir 	{
380*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't get ProcessServiceFactory\n" );
381*cdf0e10cSrcweir 	}
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir 	mxSFI = Reference< XSimpleFileAccess >( mxMSF->createInstance
384*cdf0e10cSrcweir 		( OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), UNO_QUERY );
385*cdf0e10cSrcweir 	if( !mxSFI.is() )
386*cdf0e10cSrcweir 	{
387*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create SimpleFileAccess component\n" );
388*cdf0e10cSrcweir 	}
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir 	mxStringSubstitution = Reference< XStringSubstitution >( mxMSF->createInstance
391*cdf0e10cSrcweir 		( OUString::createFromAscii( "com.sun.star.util.PathSubstitution" ) ), UNO_QUERY );
392*cdf0e10cSrcweir 	if( !mxStringSubstitution.is() )
393*cdf0e10cSrcweir 	{
394*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create PathSubstitution component\n" );
395*cdf0e10cSrcweir 	}
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir SfxLibraryContainer::~SfxLibraryContainer()
399*cdf0e10cSrcweir {
400*cdf0e10cSrcweir     if( mbOwnBasMgr )
401*cdf0e10cSrcweir         BasicManager::LegacyDeleteBasicManager( mpBasMgr );
402*cdf0e10cSrcweir     DBG_DTOR( SfxLibraryContainer, NULL );
403*cdf0e10cSrcweir }
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir void SfxLibraryContainer::checkDisposed() const
406*cdf0e10cSrcweir {
407*cdf0e10cSrcweir     if ( isDisposed() )
408*cdf0e10cSrcweir         throw DisposedException( ::rtl::OUString(), *const_cast< SfxLibraryContainer* >( this ) );
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir void SfxLibraryContainer::enterMethod()
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir     maMutex.acquire();
414*cdf0e10cSrcweir     checkDisposed();
415*cdf0e10cSrcweir }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir void SfxLibraryContainer::leaveMethod()
418*cdf0e10cSrcweir {
419*cdf0e10cSrcweir     maMutex.release();
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir BasicManager* SfxLibraryContainer::getBasicManager( void )
423*cdf0e10cSrcweir {
424*cdf0e10cSrcweir     if ( mpBasMgr )
425*cdf0e10cSrcweir         return mpBasMgr;
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir     Reference< XModel > xDocument( mxOwnerDocument.get(), UNO_QUERY );
428*cdf0e10cSrcweir     OSL_ENSURE( xDocument.is(), "SfxLibraryContainer::getBasicManager: cannot obtain a BasicManager without document!" );
429*cdf0e10cSrcweir     if ( xDocument.is() )
430*cdf0e10cSrcweir         mpBasMgr = BasicManagerRepository::getDocumentBasicManager( xDocument );
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir     return mpBasMgr;
433*cdf0e10cSrcweir }
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir // Methods XStorageBasedLibraryContainer
436*cdf0e10cSrcweir Reference< XStorage > SAL_CALL SfxLibraryContainer::getRootStorage() throw (RuntimeException)
437*cdf0e10cSrcweir {
438*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
439*cdf0e10cSrcweir     return mxStorage;
440*cdf0e10cSrcweir }
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::setRootStorage( const Reference< XStorage >& _rxRootStorage ) throw (IllegalArgumentException, RuntimeException)
443*cdf0e10cSrcweir {
444*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
445*cdf0e10cSrcweir     if ( !_rxRootStorage.is() )
446*cdf0e10cSrcweir         throw IllegalArgumentException();
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir 	mxStorage = _rxRootStorage;
449*cdf0e10cSrcweir 	onNewRootStorage();
450*cdf0e10cSrcweir }
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::storeLibrariesToStorage( const Reference< XStorage >& _rxRootStorage ) throw (IllegalArgumentException, WrappedTargetException, RuntimeException)
453*cdf0e10cSrcweir {
454*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
455*cdf0e10cSrcweir     if ( !_rxRootStorage.is() )
456*cdf0e10cSrcweir         throw IllegalArgumentException();
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir     try
459*cdf0e10cSrcweir     {
460*cdf0e10cSrcweir         storeLibraries_Impl( _rxRootStorage, sal_True );
461*cdf0e10cSrcweir     }
462*cdf0e10cSrcweir     catch( const Exception& )
463*cdf0e10cSrcweir     {
464*cdf0e10cSrcweir         throw WrappedTargetException( ::rtl::OUString(), *this, ::cppu::getCaughtException() );
465*cdf0e10cSrcweir     }
466*cdf0e10cSrcweir }
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir // Methods XModifiable
470*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::isModified() throw (RuntimeException)
471*cdf0e10cSrcweir {
472*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
473*cdf0e10cSrcweir 	if ( maModifiable.isModified() )
474*cdf0e10cSrcweir 		return sal_True;
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir 	// the library container is not modified, go through the libraries and check whether they are modified
477*cdf0e10cSrcweir 	Sequence< OUString > aNames = maNameContainer.getElementNames();
478*cdf0e10cSrcweir 	const OUString* pNames = aNames.getConstArray();
479*cdf0e10cSrcweir 	sal_Int32 nNameCount = aNames.getLength();
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir 	for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
482*cdf0e10cSrcweir 	{
483*cdf0e10cSrcweir 		OUString aName = pNames[ i ];
484*cdf0e10cSrcweir         SfxLibrary* pImplLib = getImplLib( aName );
485*cdf0e10cSrcweir 		if( pImplLib->isModified() )
486*cdf0e10cSrcweir 		{
487*cdf0e10cSrcweir 			if ( aName.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Standard") ) ) )
488*cdf0e10cSrcweir 			{
489*cdf0e10cSrcweir 				// this is a workaround that has to be implemented because
490*cdf0e10cSrcweir 				// empty standard library should stay marked as modified
491*cdf0e10cSrcweir 				// but should not be treated as modified while it is empty
492*cdf0e10cSrcweir 				if ( pImplLib->hasElements() )
493*cdf0e10cSrcweir 					return sal_True;
494*cdf0e10cSrcweir 			}
495*cdf0e10cSrcweir 			else
496*cdf0e10cSrcweir 				return sal_True;
497*cdf0e10cSrcweir 		}
498*cdf0e10cSrcweir 	}
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir 	return sal_False;
501*cdf0e10cSrcweir }
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::setModified( sal_Bool _bModified ) throw (PropertyVetoException, RuntimeException)
504*cdf0e10cSrcweir {
505*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
506*cdf0e10cSrcweir     maModifiable.setModified( _bModified );
507*cdf0e10cSrcweir }
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::addModifyListener( const Reference< XModifyListener >& _rxListener ) throw (RuntimeException)
510*cdf0e10cSrcweir {
511*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
512*cdf0e10cSrcweir     maModifiable.addModifyListener( _rxListener );
513*cdf0e10cSrcweir }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::removeModifyListener( const Reference< XModifyListener >& _rxListener ) throw (RuntimeException)
516*cdf0e10cSrcweir {
517*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
518*cdf0e10cSrcweir     maModifiable.removeModifyListener( _rxListener );
519*cdf0e10cSrcweir }
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir // Methods XPersistentLibraryContainer
522*cdf0e10cSrcweir Any SAL_CALL SfxLibraryContainer::getRootLocation() throw (RuntimeException)
523*cdf0e10cSrcweir {
524*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
525*cdf0e10cSrcweir     return makeAny( getRootStorage() );
526*cdf0e10cSrcweir }
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SfxLibraryContainer::getContainerLocationName() throw (RuntimeException)
529*cdf0e10cSrcweir {
530*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
531*cdf0e10cSrcweir     return maLibrariesDir;
532*cdf0e10cSrcweir }
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::storeLibraries(  ) throw (WrappedTargetException, RuntimeException)
535*cdf0e10cSrcweir {
536*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
537*cdf0e10cSrcweir     try
538*cdf0e10cSrcweir     {
539*cdf0e10cSrcweir 	    storeLibraries_Impl( mxStorage, mxStorage.is()  );
540*cdf0e10cSrcweir         // we need to store *all* libraries if and only if we are based on a storage:
541*cdf0e10cSrcweir         // in this case, storeLibraries_Impl will remove the source storage, after loading
542*cdf0e10cSrcweir         // all libraries, so we need to force them to be stored, again
543*cdf0e10cSrcweir     }
544*cdf0e10cSrcweir     catch( const Exception& )
545*cdf0e10cSrcweir     {
546*cdf0e10cSrcweir         throw WrappedTargetException( ::rtl::OUString(), *this, ::cppu::getCaughtException() );
547*cdf0e10cSrcweir     }
548*cdf0e10cSrcweir }
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir static void checkAndCopyFileImpl( const INetURLObject& rSourceFolderInetObj,
551*cdf0e10cSrcweir 								  const INetURLObject& rTargetFolderInetObj,
552*cdf0e10cSrcweir 								  const OUString& rCheckFileName,
553*cdf0e10cSrcweir 								  const OUString& rCheckExtension,
554*cdf0e10cSrcweir 								  Reference< XSimpleFileAccess > xSFI )
555*cdf0e10cSrcweir {
556*cdf0e10cSrcweir 	INetURLObject aTargetFolderInetObj( rTargetFolderInetObj );
557*cdf0e10cSrcweir 	aTargetFolderInetObj.insertName( rCheckFileName, sal_True, INetURLObject::LAST_SEGMENT,
558*cdf0e10cSrcweir 									 sal_True, INetURLObject::ENCODE_ALL );
559*cdf0e10cSrcweir 	aTargetFolderInetObj.setExtension( rCheckExtension );
560*cdf0e10cSrcweir 	OUString aTargetFile = aTargetFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
561*cdf0e10cSrcweir 	if( !xSFI->exists( aTargetFile ) )
562*cdf0e10cSrcweir 	{
563*cdf0e10cSrcweir 		INetURLObject aSourceFolderInetObj( rSourceFolderInetObj );
564*cdf0e10cSrcweir 		aSourceFolderInetObj.insertName( rCheckFileName, sal_True, INetURLObject::LAST_SEGMENT,
565*cdf0e10cSrcweir 										 sal_True, INetURLObject::ENCODE_ALL );
566*cdf0e10cSrcweir 		aSourceFolderInetObj.setExtension( rCheckExtension );
567*cdf0e10cSrcweir 		OUString aSourceFile = aSourceFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
568*cdf0e10cSrcweir 		xSFI->copy( aSourceFile, aTargetFile );
569*cdf0e10cSrcweir 	}
570*cdf0e10cSrcweir }
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir static void createVariableURL( OUString& rStr, const OUString& rLibName,
573*cdf0e10cSrcweir 							   const OUString& rInfoFileName, bool bUser )
574*cdf0e10cSrcweir {
575*cdf0e10cSrcweir 	if( bUser )
576*cdf0e10cSrcweir 		rStr = OUString::createFromAscii( "$(USER)/basic/" );
577*cdf0e10cSrcweir 	else
578*cdf0e10cSrcweir 		rStr = OUString::createFromAscii( "$(INST)/share/basic/" );
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir 	rStr += rLibName;
581*cdf0e10cSrcweir 	rStr += OUString::createFromAscii( "/" );
582*cdf0e10cSrcweir 	rStr += rInfoFileName;
583*cdf0e10cSrcweir 	rStr += OUString::createFromAscii( ".xlb/" );
584*cdf0e10cSrcweir }
585*cdf0e10cSrcweir 
586*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::init( const OUString& rInitialDocumentURL, const uno::Reference< embed::XStorage >& rxInitialStorage )
587*cdf0e10cSrcweir {
588*cdf0e10cSrcweir     // this might be called from within the ctor, and the impl_init might (indirectly) create
589*cdf0e10cSrcweir     // an UNO reference to ourself.
590*cdf0e10cSrcweir     // Ensure that we're not destroyed while we're in here
591*cdf0e10cSrcweir     osl_incrementInterlockedCount( &m_refCount );
592*cdf0e10cSrcweir     sal_Bool bSuccess = init_Impl( rInitialDocumentURL, rxInitialStorage );
593*cdf0e10cSrcweir     osl_decrementInterlockedCount( &m_refCount );
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir     return bSuccess;
596*cdf0e10cSrcweir }
597*cdf0e10cSrcweir 
598*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::init_Impl(
599*cdf0e10cSrcweir     const OUString& rInitialDocumentURL, const uno::Reference< embed::XStorage >& rxInitialStorage )
600*cdf0e10cSrcweir {
601*cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xStorage = rxInitialStorage;
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir     maInitialDocumentURL = rInitialDocumentURL;
604*cdf0e10cSrcweir     maInfoFileName = OUString::createFromAscii( getInfoFileName() );
605*cdf0e10cSrcweir 	maOldInfoFileName = OUString::createFromAscii( getOldInfoFileName() );
606*cdf0e10cSrcweir 	maLibElementFileExtension = OUString::createFromAscii( getLibElementFileExtension() );
607*cdf0e10cSrcweir 	maLibrariesDir = OUString::createFromAscii( getLibrariesDir() );
608*cdf0e10cSrcweir 
609*cdf0e10cSrcweir     meInitMode = DEFAULT;
610*cdf0e10cSrcweir     INetURLObject aInitUrlInetObj( maInitialDocumentURL );
611*cdf0e10cSrcweir     OUString aInitFileName = aInitUrlInetObj.GetMainURL( INetURLObject::NO_DECODE );
612*cdf0e10cSrcweir     if( aInitFileName.getLength() )
613*cdf0e10cSrcweir     {
614*cdf0e10cSrcweir         // We need a BasicManager to avoid problems
615*cdf0e10cSrcweir         StarBASIC* pBas = new StarBASIC();
616*cdf0e10cSrcweir         mpBasMgr = new BasicManager( pBas );
617*cdf0e10cSrcweir         mbOwnBasMgr = sal_True;
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir         OUString aExtension = aInitUrlInetObj.getExtension();
620*cdf0e10cSrcweir         if( aExtension.compareToAscii( "xlc" ) == COMPARE_EQUAL )
621*cdf0e10cSrcweir         {
622*cdf0e10cSrcweir             meInitMode = CONTAINER_INIT_FILE;
623*cdf0e10cSrcweir 	        INetURLObject aLibPathInetObj( aInitUrlInetObj );
624*cdf0e10cSrcweir 			aLibPathInetObj.removeSegment();
625*cdf0e10cSrcweir 	        maLibraryPath = aLibPathInetObj.GetMainURL( INetURLObject::NO_DECODE );
626*cdf0e10cSrcweir         }
627*cdf0e10cSrcweir         else if( aExtension.compareToAscii( "xlb" ) == COMPARE_EQUAL )
628*cdf0e10cSrcweir         {
629*cdf0e10cSrcweir             meInitMode = LIBRARY_INIT_FILE;
630*cdf0e10cSrcweir         	uno::Reference< embed::XStorage > xDummyStor;
631*cdf0e10cSrcweir             ::xmlscript::LibDescriptor aLibDesc;
632*cdf0e10cSrcweir             sal_Bool bReadIndexFile = implLoadLibraryIndexFile( NULL, aLibDesc, xDummyStor, aInitFileName );
633*cdf0e10cSrcweir            	return bReadIndexFile;
634*cdf0e10cSrcweir         }
635*cdf0e10cSrcweir         else
636*cdf0e10cSrcweir         {
637*cdf0e10cSrcweir             // Decide between old and new document
638*cdf0e10cSrcweir             sal_Bool bOldStorage = SotStorage::IsOLEStorage( aInitFileName );
639*cdf0e10cSrcweir             if ( bOldStorage )
640*cdf0e10cSrcweir             {
641*cdf0e10cSrcweir                 meInitMode = OLD_BASIC_STORAGE;
642*cdf0e10cSrcweir                 importFromOldStorage( aInitFileName );
643*cdf0e10cSrcweir                 return sal_True;
644*cdf0e10cSrcweir             }
645*cdf0e10cSrcweir             else
646*cdf0e10cSrcweir             {
647*cdf0e10cSrcweir                 meInitMode = OFFICE_DOCUMENT;
648*cdf0e10cSrcweir                 try
649*cdf0e10cSrcweir                 {
650*cdf0e10cSrcweir                     xStorage = ::comphelper::OStorageHelper::GetStorageFromURL( aInitFileName, embed::ElementModes::READ );
651*cdf0e10cSrcweir                 }
652*cdf0e10cSrcweir                 catch ( uno::Exception& )
653*cdf0e10cSrcweir                 {
654*cdf0e10cSrcweir                     // TODO: error handling
655*cdf0e10cSrcweir                 }
656*cdf0e10cSrcweir             }
657*cdf0e10cSrcweir         }
658*cdf0e10cSrcweir     }
659*cdf0e10cSrcweir     else
660*cdf0e10cSrcweir     {
661*cdf0e10cSrcweir         // Default pathes
662*cdf0e10cSrcweir         maLibraryPath = SvtPathOptions().GetBasicPath();
663*cdf0e10cSrcweir     }
664*cdf0e10cSrcweir 
665*cdf0e10cSrcweir 	Reference< XParser > xParser( mxMSF->createInstance(
666*cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ) ), UNO_QUERY );
667*cdf0e10cSrcweir 	if( !xParser.is() )
668*cdf0e10cSrcweir 	{
669*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create sax parser component\n" );
670*cdf0e10cSrcweir 		return sal_False;
671*cdf0e10cSrcweir 	}
672*cdf0e10cSrcweir 
673*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInput;
674*cdf0e10cSrcweir 
675*cdf0e10cSrcweir 	mxStorage = xStorage;
676*cdf0e10cSrcweir 	sal_Bool bStorage = mxStorage.is();
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir 
679*cdf0e10cSrcweir 	// #110009: Scope to force the StorageRefs to be destructed and
680*cdf0e10cSrcweir 	// so the streams to be closed before the preload operation
681*cdf0e10cSrcweir 	{
682*cdf0e10cSrcweir 	// #110009
683*cdf0e10cSrcweir 
684*cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xLibrariesStor;
685*cdf0e10cSrcweir 	String aFileName;
686*cdf0e10cSrcweir 
687*cdf0e10cSrcweir 	int nPassCount = 1;
688*cdf0e10cSrcweir 	if( !bStorage && meInitMode == DEFAULT )
689*cdf0e10cSrcweir 		nPassCount = 2;
690*cdf0e10cSrcweir 	for( int nPass = 0 ; nPass < nPassCount ; nPass++ )
691*cdf0e10cSrcweir 	{
692*cdf0e10cSrcweir 		if( bStorage )
693*cdf0e10cSrcweir 		{
694*cdf0e10cSrcweir 			OSL_ENSURE( meInitMode == DEFAULT || meInitMode == OFFICE_DOCUMENT,
695*cdf0e10cSrcweir 				"### Wrong InitMode for document\n" );
696*cdf0e10cSrcweir 			try
697*cdf0e10cSrcweir 			{
698*cdf0e10cSrcweir 				uno::Reference< io::XStream > xStream;
699*cdf0e10cSrcweir 				xLibrariesStor = xStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
700*cdf0e10cSrcweir                 //if ( !xLibrariesStor.is() )
701*cdf0e10cSrcweir                     // TODO: the method must either return a storage or throw an exception
702*cdf0e10cSrcweir                     //throw uno::RuntimeException();
703*cdf0e10cSrcweir 
704*cdf0e10cSrcweir                 if ( xLibrariesStor.is() )
705*cdf0e10cSrcweir                 {
706*cdf0e10cSrcweir                     aFileName = maInfoFileName;
707*cdf0e10cSrcweir                     aFileName += String( RTL_CONSTASCII_USTRINGPARAM("-lc.xml") );
708*cdf0e10cSrcweir 
709*cdf0e10cSrcweir                     try
710*cdf0e10cSrcweir                     {
711*cdf0e10cSrcweir                         xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
712*cdf0e10cSrcweir                     }
713*cdf0e10cSrcweir                     catch( uno::Exception& )
714*cdf0e10cSrcweir                     {}
715*cdf0e10cSrcweir 
716*cdf0e10cSrcweir                     if( !xStream.is() )
717*cdf0e10cSrcweir                     {
718*cdf0e10cSrcweir                         mbOldInfoFormat = true;
719*cdf0e10cSrcweir 
720*cdf0e10cSrcweir                         // Check old version
721*cdf0e10cSrcweir                         aFileName = maOldInfoFileName;
722*cdf0e10cSrcweir                         aFileName += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
723*cdf0e10cSrcweir 
724*cdf0e10cSrcweir                         try
725*cdf0e10cSrcweir                         {
726*cdf0e10cSrcweir                             xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
727*cdf0e10cSrcweir                         }
728*cdf0e10cSrcweir                         catch( uno::Exception& )
729*cdf0e10cSrcweir                         {}
730*cdf0e10cSrcweir 
731*cdf0e10cSrcweir                         if( !xStream.is() )
732*cdf0e10cSrcweir                         {
733*cdf0e10cSrcweir                             // Check for EA2 document version with wrong extensions
734*cdf0e10cSrcweir                             aFileName = maOldInfoFileName;
735*cdf0e10cSrcweir                             aFileName += String( RTL_CONSTASCII_USTRINGPARAM(".xli") );
736*cdf0e10cSrcweir                             xStream = xLibrariesStor->openStreamElement( aFileName, embed::ElementModes::READ );
737*cdf0e10cSrcweir                         }
738*cdf0e10cSrcweir                     }
739*cdf0e10cSrcweir 				}
740*cdf0e10cSrcweir 
741*cdf0e10cSrcweir 				if ( xStream.is() )
742*cdf0e10cSrcweir 					xInput = xStream->getInputStream();
743*cdf0e10cSrcweir 			}
744*cdf0e10cSrcweir 			catch( uno::Exception& )
745*cdf0e10cSrcweir 			{
746*cdf0e10cSrcweir                 // TODO: error handling?
747*cdf0e10cSrcweir 			}
748*cdf0e10cSrcweir 		}
749*cdf0e10cSrcweir 		else
750*cdf0e10cSrcweir 		{
751*cdf0e10cSrcweir 			INetURLObject* pLibInfoInetObj = NULL;
752*cdf0e10cSrcweir 			if( meInitMode == CONTAINER_INIT_FILE )
753*cdf0e10cSrcweir 			{
754*cdf0e10cSrcweir 				aFileName = aInitFileName;
755*cdf0e10cSrcweir 			}
756*cdf0e10cSrcweir 			else
757*cdf0e10cSrcweir 			{
758*cdf0e10cSrcweir 				if( nPass == 1 )
759*cdf0e10cSrcweir 					pLibInfoInetObj = new INetURLObject( String(maLibraryPath).GetToken(0) );
760*cdf0e10cSrcweir 				else
761*cdf0e10cSrcweir 					pLibInfoInetObj = new INetURLObject( String(maLibraryPath).GetToken(1) );
762*cdf0e10cSrcweir 				pLibInfoInetObj->insertName( maInfoFileName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
763*cdf0e10cSrcweir 				pLibInfoInetObj->setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xlc") ) );
764*cdf0e10cSrcweir 				aFileName = pLibInfoInetObj->GetMainURL( INetURLObject::NO_DECODE );
765*cdf0e10cSrcweir 			}
766*cdf0e10cSrcweir 
767*cdf0e10cSrcweir 			try
768*cdf0e10cSrcweir 			{
769*cdf0e10cSrcweir 				xInput = mxSFI->openFileRead( aFileName );
770*cdf0e10cSrcweir 			}
771*cdf0e10cSrcweir 			catch( Exception& )
772*cdf0e10cSrcweir 			{
773*cdf0e10cSrcweir 				xInput.clear();
774*cdf0e10cSrcweir                 if( nPass == 0 )
775*cdf0e10cSrcweir                 {
776*cdf0e10cSrcweir 		            SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aFileName );
777*cdf0e10cSrcweir                     sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
778*cdf0e10cSrcweir                     ErrorHandler::HandleError( nErrorCode );
779*cdf0e10cSrcweir                 }
780*cdf0e10cSrcweir 			}
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir 			// Old variant?
783*cdf0e10cSrcweir 			if( !xInput.is() && nPass == 0 )
784*cdf0e10cSrcweir 			{
785*cdf0e10cSrcweir 				INetURLObject aLibInfoInetObj( String(maLibraryPath).GetToken(1) );
786*cdf0e10cSrcweir 				aLibInfoInetObj.insertName( maOldInfoFileName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
787*cdf0e10cSrcweir 				aLibInfoInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xli") ) );
788*cdf0e10cSrcweir 				aFileName = aLibInfoInetObj.GetMainURL( INetURLObject::NO_DECODE );
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir 				try
791*cdf0e10cSrcweir 				{
792*cdf0e10cSrcweir 					xInput = mxSFI->openFileRead( aFileName );
793*cdf0e10cSrcweir 					mbOldInfoFormat = true;
794*cdf0e10cSrcweir 				}
795*cdf0e10cSrcweir 				catch( Exception& )
796*cdf0e10cSrcweir 				{
797*cdf0e10cSrcweir     				xInput.clear();
798*cdf0e10cSrcweir 		            SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aFileName );
799*cdf0e10cSrcweir                     sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
800*cdf0e10cSrcweir                     ErrorHandler::HandleError( nErrorCode );
801*cdf0e10cSrcweir 				}
802*cdf0e10cSrcweir 			}
803*cdf0e10cSrcweir 
804*cdf0e10cSrcweir 			delete pLibInfoInetObj;
805*cdf0e10cSrcweir 		}
806*cdf0e10cSrcweir 
807*cdf0e10cSrcweir 		if( xInput.is() )
808*cdf0e10cSrcweir         {
809*cdf0e10cSrcweir 		    InputSource source;
810*cdf0e10cSrcweir 		    source.aInputStream = xInput;
811*cdf0e10cSrcweir 		    source.sSystemId 	= aFileName;
812*cdf0e10cSrcweir 
813*cdf0e10cSrcweir 		    // start parsing
814*cdf0e10cSrcweir 		    ::xmlscript::LibDescriptorArray* pLibArray = new ::xmlscript::LibDescriptorArray();
815*cdf0e10cSrcweir 
816*cdf0e10cSrcweir             try
817*cdf0e10cSrcweir             {
818*cdf0e10cSrcweir                 xParser->setDocumentHandler( ::xmlscript::importLibraryContainer( pLibArray ) );
819*cdf0e10cSrcweir                 xParser->parseStream( source );
820*cdf0e10cSrcweir             }
821*cdf0e10cSrcweir             catch ( xml::sax::SAXException& e )
822*cdf0e10cSrcweir             {
823*cdf0e10cSrcweir                 (void) e; // avoid warning
824*cdf0e10cSrcweir                 OSL_ENSURE( 0, OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
825*cdf0e10cSrcweir                 return sal_False;
826*cdf0e10cSrcweir             }
827*cdf0e10cSrcweir             catch ( io::IOException& e )
828*cdf0e10cSrcweir             {
829*cdf0e10cSrcweir                 (void) e; // avoid warning
830*cdf0e10cSrcweir                 OSL_ENSURE( 0, OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
831*cdf0e10cSrcweir                 return sal_False;
832*cdf0e10cSrcweir             }
833*cdf0e10cSrcweir 
834*cdf0e10cSrcweir 		    sal_Int32 nLibCount = pLibArray->mnLibCount;
835*cdf0e10cSrcweir 		    for( sal_Int32 i = 0 ; i < nLibCount ; i++ )
836*cdf0e10cSrcweir 		    {
837*cdf0e10cSrcweir 			    ::xmlscript::LibDescriptor& rLib = pLibArray->mpLibs[i];
838*cdf0e10cSrcweir 
839*cdf0e10cSrcweir 			    // Check storage URL
840*cdf0e10cSrcweir 			    OUString aStorageURL = rLib.aStorageURL;
841*cdf0e10cSrcweir 			    if( !bStorage && !aStorageURL.getLength() && nPass == 0 )
842*cdf0e10cSrcweir 			    {
843*cdf0e10cSrcweir 					String aLibraryPath;
844*cdf0e10cSrcweir 					if( meInitMode == CONTAINER_INIT_FILE )
845*cdf0e10cSrcweir 						aLibraryPath = maLibraryPath;
846*cdf0e10cSrcweir 					else
847*cdf0e10cSrcweir 						aLibraryPath = String(maLibraryPath).GetToken(1);
848*cdf0e10cSrcweir 					INetURLObject aInetObj( aLibraryPath );
849*cdf0e10cSrcweir 
850*cdf0e10cSrcweir 				    aInetObj.insertName( rLib.aName, sal_True, INetURLObject::LAST_SEGMENT,
851*cdf0e10cSrcweir 					    sal_True, INetURLObject::ENCODE_ALL );
852*cdf0e10cSrcweir 				    OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
853*cdf0e10cSrcweir 				    if( mxSFI->isFolder( aLibDirPath ) )
854*cdf0e10cSrcweir 				    {
855*cdf0e10cSrcweir 						createVariableURL( rLib.aStorageURL, rLib.aName, maInfoFileName, true );
856*cdf0e10cSrcweir                         maModifiable.setModified( sal_True );
857*cdf0e10cSrcweir 				    }
858*cdf0e10cSrcweir 				    else if( rLib.bLink )
859*cdf0e10cSrcweir 				    {
860*cdf0e10cSrcweir 					    // Check "share" path
861*cdf0e10cSrcweir 					    INetURLObject aShareInetObj( String(maLibraryPath).GetToken(0) );
862*cdf0e10cSrcweir 					    aShareInetObj.insertName( rLib.aName, sal_True, INetURLObject::LAST_SEGMENT,
863*cdf0e10cSrcweir 						    sal_True, INetURLObject::ENCODE_ALL );
864*cdf0e10cSrcweir 					    OUString aShareLibDirPath = aShareInetObj.GetMainURL( INetURLObject::NO_DECODE );
865*cdf0e10cSrcweir 					    if( mxSFI->isFolder( aShareLibDirPath ) )
866*cdf0e10cSrcweir 					    {
867*cdf0e10cSrcweir 							createVariableURL( rLib.aStorageURL, rLib.aName, maInfoFileName, false );
868*cdf0e10cSrcweir                             maModifiable.setModified( sal_True );
869*cdf0e10cSrcweir 					    }
870*cdf0e10cSrcweir 						else
871*cdf0e10cSrcweir 						{
872*cdf0e10cSrcweir 							// #i25537: Ignore lib if library folder does not really exist
873*cdf0e10cSrcweir 							continue;
874*cdf0e10cSrcweir 						}
875*cdf0e10cSrcweir 				    }
876*cdf0e10cSrcweir 			    }
877*cdf0e10cSrcweir 
878*cdf0e10cSrcweir 			    OUString aLibName = rLib.aName;
879*cdf0e10cSrcweir 
880*cdf0e10cSrcweir 			    // If the same library name is used by the shared and the
881*cdf0e10cSrcweir 			    // user lib container index files the user file wins
882*cdf0e10cSrcweir 			    if( nPass == 1 && hasByName( aLibName ) )
883*cdf0e10cSrcweir 				    continue;
884*cdf0e10cSrcweir 
885*cdf0e10cSrcweir 			    SfxLibrary* pImplLib;
886*cdf0e10cSrcweir 			    if( rLib.bLink )
887*cdf0e10cSrcweir 			    {
888*cdf0e10cSrcweir 				    Reference< XNameAccess > xLib =
889*cdf0e10cSrcweir 					    createLibraryLink( aLibName, rLib.aStorageURL, rLib.bReadOnly );
890*cdf0e10cSrcweir 				    pImplLib = static_cast< SfxLibrary* >( xLib.get() );
891*cdf0e10cSrcweir 			    }
892*cdf0e10cSrcweir 			    else
893*cdf0e10cSrcweir 			    {
894*cdf0e10cSrcweir 				    Reference< XNameContainer > xLib = createLibrary( aLibName );
895*cdf0e10cSrcweir 				    pImplLib = static_cast< SfxLibrary* >( xLib.get() );
896*cdf0e10cSrcweir 				    pImplLib->mbLoaded = sal_False;
897*cdf0e10cSrcweir 				    pImplLib->mbReadOnly = rLib.bReadOnly;
898*cdf0e10cSrcweir 				    if( !bStorage )
899*cdf0e10cSrcweir 					    checkStorageURL( rLib.aStorageURL, pImplLib->maLibInfoFileURL,
900*cdf0e10cSrcweir                             pImplLib->maStorageURL, pImplLib->maUnexpandedStorageURL );
901*cdf0e10cSrcweir 			    }
902*cdf0e10cSrcweir 				maModifiable.setModified( sal_False );
903*cdf0e10cSrcweir 
904*cdf0e10cSrcweir 			    // Read library info files
905*cdf0e10cSrcweir 			    if( !mbOldInfoFormat )
906*cdf0e10cSrcweir 			    {
907*cdf0e10cSrcweir         		    uno::Reference< embed::XStorage > xLibraryStor;
908*cdf0e10cSrcweir           		    if( !pImplLib->mbInitialised && bStorage )
909*cdf0e10cSrcweir 				    {
910*cdf0e10cSrcweir 						try {
911*cdf0e10cSrcweir 							xLibraryStor = xLibrariesStor->openStorageElement( rLib.aName,
912*cdf0e10cSrcweir 																				embed::ElementModes::READ );
913*cdf0e10cSrcweir 						}
914*cdf0e10cSrcweir 						catch( uno::Exception& )
915*cdf0e10cSrcweir 						{
916*cdf0e10cSrcweir                         #if OSL_DEBUG_LEVEL > 0
917*cdf0e10cSrcweir                             Any aError( ::cppu::getCaughtException() );
918*cdf0e10cSrcweir                             ::rtl::OStringBuffer aMessage;
919*cdf0e10cSrcweir                             aMessage.append( "couln't open sub storage for library '" );
920*cdf0e10cSrcweir                             aMessage.append( ::rtl::OUStringToOString( rLib.aName, osl_getThreadTextEncoding() ) );
921*cdf0e10cSrcweir                             aMessage.append( "'.\n\nException:" );
922*cdf0e10cSrcweir                             aMessage.append( ::rtl::OUStringToOString( ::comphelper::anyToString( aError ), osl_getThreadTextEncoding() ) );
923*cdf0e10cSrcweir 			                OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
924*cdf0e10cSrcweir                         #endif
925*cdf0e10cSrcweir 						}
926*cdf0e10cSrcweir 				    }
927*cdf0e10cSrcweir 
928*cdf0e10cSrcweir 				    // Link is already initialised in createLibraryLink()
929*cdf0e10cSrcweir 				    if( !pImplLib->mbInitialised && (!bStorage || xLibraryStor.is()) )
930*cdf0e10cSrcweir 				    {
931*cdf0e10cSrcweir 					    OUString aIndexFileName;
932*cdf0e10cSrcweir 					    sal_Bool bLoaded = implLoadLibraryIndexFile( pImplLib, rLib, xLibraryStor, aIndexFileName );
933*cdf0e10cSrcweir 					    if( bLoaded && aLibName != rLib.aName )
934*cdf0e10cSrcweir 					    {
935*cdf0e10cSrcweir 						    OSL_ENSURE( 0, "Different library names in library"
936*cdf0e10cSrcweir 							    " container and library info files!\n" );
937*cdf0e10cSrcweir 					    }
938*cdf0e10cSrcweir 						if( GbMigrationSuppressErrors && !bLoaded )
939*cdf0e10cSrcweir 							removeLibrary( aLibName );
940*cdf0e10cSrcweir 				    }
941*cdf0e10cSrcweir 			    }
942*cdf0e10cSrcweir 			    else if( !bStorage )
943*cdf0e10cSrcweir 			    {
944*cdf0e10cSrcweir 				    // Write new index file immediately because otherwise
945*cdf0e10cSrcweir 				    // the library elements will be lost when storing into
946*cdf0e10cSrcweir 				    // the new info format
947*cdf0e10cSrcweir 				    uno::Reference< embed::XStorage > xTmpStorage;
948*cdf0e10cSrcweir 				    implStoreLibraryIndexFile( pImplLib, rLib, xTmpStorage );
949*cdf0e10cSrcweir 			    }
950*cdf0e10cSrcweir 
951*cdf0e10cSrcweir 			    implImportLibDescriptor( pImplLib, rLib );
952*cdf0e10cSrcweir 
953*cdf0e10cSrcweir 			    if( nPass == 1 )
954*cdf0e10cSrcweir 			    {
955*cdf0e10cSrcweir 				    pImplLib->mbSharedIndexFile = sal_True;
956*cdf0e10cSrcweir 				    pImplLib->mbReadOnly = sal_True;
957*cdf0e10cSrcweir 			    }
958*cdf0e10cSrcweir 		    }
959*cdf0e10cSrcweir 
960*cdf0e10cSrcweir 		    // Keep flag for documents to force writing the new index files
961*cdf0e10cSrcweir 		    if( !bStorage )
962*cdf0e10cSrcweir 			    mbOldInfoFormat = sal_False;
963*cdf0e10cSrcweir 
964*cdf0e10cSrcweir 		    delete pLibArray;
965*cdf0e10cSrcweir         }
966*cdf0e10cSrcweir 		// Only in the first pass it's an error when no index file is found
967*cdf0e10cSrcweir 		else if( nPass == 0 )
968*cdf0e10cSrcweir 		{
969*cdf0e10cSrcweir 			return sal_False;
970*cdf0e10cSrcweir 		}
971*cdf0e10cSrcweir 	}
972*cdf0e10cSrcweir 
973*cdf0e10cSrcweir 	// #110009: END Scope to force the StorageRefs to be destructed
974*cdf0e10cSrcweir 	}
975*cdf0e10cSrcweir 	// #110009
976*cdf0e10cSrcweir 
977*cdf0e10cSrcweir 	if( !bStorage && meInitMode == DEFAULT )
978*cdf0e10cSrcweir     {
979*cdf0e10cSrcweir         try
980*cdf0e10cSrcweir         {
981*cdf0e10cSrcweir             implScanExtensions();
982*cdf0e10cSrcweir         }
983*cdf0e10cSrcweir         catch( uno::Exception& )
984*cdf0e10cSrcweir         {
985*cdf0e10cSrcweir             // TODO: error handling?
986*cdf0e10cSrcweir             OSL_ASSERT( "Cannot access extensions!" );
987*cdf0e10cSrcweir         }
988*cdf0e10cSrcweir     }
989*cdf0e10cSrcweir 
990*cdf0e10cSrcweir 	// #110009 Preload?
991*cdf0e10cSrcweir     {
992*cdf0e10cSrcweir 	    Sequence< OUString > aNames = maNameContainer.getElementNames();
993*cdf0e10cSrcweir 	    const OUString* pNames = aNames.getConstArray();
994*cdf0e10cSrcweir 	    sal_Int32 nNameCount = aNames.getLength();
995*cdf0e10cSrcweir 	    for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
996*cdf0e10cSrcweir 	    {
997*cdf0e10cSrcweir 		    OUString aName = pNames[ i ];
998*cdf0e10cSrcweir             SfxLibrary* pImplLib = getImplLib( aName );
999*cdf0e10cSrcweir 		    if( pImplLib->mbPreload )
1000*cdf0e10cSrcweir 			    loadLibrary( aName );
1001*cdf0e10cSrcweir 	    }
1002*cdf0e10cSrcweir     }
1003*cdf0e10cSrcweir 
1004*cdf0e10cSrcweir 	// #118803# upgrade installation 7.0 -> 8.0
1005*cdf0e10cSrcweir 	if( meInitMode == DEFAULT )
1006*cdf0e10cSrcweir 	{
1007*cdf0e10cSrcweir 		INetURLObject aUserBasicInetObj( String(maLibraryPath).GetToken(1) );
1008*cdf0e10cSrcweir 		OUString aStandardStr( RTL_CONSTASCII_USTRINGPARAM("Standard") );
1009*cdf0e10cSrcweir 
1010*cdf0e10cSrcweir 		static char strPrevFolderName_1[] = "__basic_80";
1011*cdf0e10cSrcweir 		static char strPrevFolderName_2[] = "__basic_80_2";
1012*cdf0e10cSrcweir 		INetURLObject aPrevUserBasicInetObj_1( aUserBasicInetObj );
1013*cdf0e10cSrcweir 		aPrevUserBasicInetObj_1.removeSegment();
1014*cdf0e10cSrcweir 		INetURLObject aPrevUserBasicInetObj_2 = aPrevUserBasicInetObj_1;
1015*cdf0e10cSrcweir 		aPrevUserBasicInetObj_1.Append( strPrevFolderName_1 );
1016*cdf0e10cSrcweir 		aPrevUserBasicInetObj_2.Append( strPrevFolderName_2 );
1017*cdf0e10cSrcweir 
1018*cdf0e10cSrcweir 		// #i93163
1019*cdf0e10cSrcweir 		bool bCleanUp = false;
1020*cdf0e10cSrcweir 		try
1021*cdf0e10cSrcweir 		{
1022*cdf0e10cSrcweir 			INetURLObject aPrevUserBasicInetObj = aPrevUserBasicInetObj_1;
1023*cdf0e10cSrcweir 			String aPrevFolder = aPrevUserBasicInetObj.GetMainURL( INetURLObject::NO_DECODE );
1024*cdf0e10cSrcweir 			bool bSecondTime = false;
1025*cdf0e10cSrcweir 			if( mxSFI->isFolder( aPrevFolder ) )
1026*cdf0e10cSrcweir 			{
1027*cdf0e10cSrcweir 				// #110101 Check if Standard folder exists and is complete
1028*cdf0e10cSrcweir 				INetURLObject aUserBasicStandardInetObj( aUserBasicInetObj );
1029*cdf0e10cSrcweir 				aUserBasicStandardInetObj.insertName( aStandardStr, sal_True, INetURLObject::LAST_SEGMENT,
1030*cdf0e10cSrcweir 													  sal_True, INetURLObject::ENCODE_ALL );
1031*cdf0e10cSrcweir 				INetURLObject aPrevUserBasicStandardInetObj( aPrevUserBasicInetObj );
1032*cdf0e10cSrcweir 				aPrevUserBasicStandardInetObj.insertName( aStandardStr, sal_True, INetURLObject::LAST_SEGMENT,
1033*cdf0e10cSrcweir 														sal_True, INetURLObject::ENCODE_ALL );
1034*cdf0e10cSrcweir 				OUString aPrevStandardFolder = aPrevUserBasicStandardInetObj.GetMainURL( INetURLObject::NO_DECODE );
1035*cdf0e10cSrcweir 				if( mxSFI->isFolder( aPrevStandardFolder ) )
1036*cdf0e10cSrcweir 				{
1037*cdf0e10cSrcweir 					OUString aXlbExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xlb") ) );
1038*cdf0e10cSrcweir 					OUString aCheckFileName;
1039*cdf0e10cSrcweir 
1040*cdf0e10cSrcweir 					// Check if script.xlb exists
1041*cdf0e10cSrcweir 					aCheckFileName = OUString( RTL_CONSTASCII_USTRINGPARAM("script") );
1042*cdf0e10cSrcweir 					checkAndCopyFileImpl( aUserBasicStandardInetObj,
1043*cdf0e10cSrcweir 										  aPrevUserBasicStandardInetObj,
1044*cdf0e10cSrcweir 										  aCheckFileName, aXlbExtension, mxSFI );
1045*cdf0e10cSrcweir 
1046*cdf0e10cSrcweir 					// Check if dialog.xlb exists
1047*cdf0e10cSrcweir 					aCheckFileName = OUString( RTL_CONSTASCII_USTRINGPARAM("dialog") );
1048*cdf0e10cSrcweir 					checkAndCopyFileImpl( aUserBasicStandardInetObj,
1049*cdf0e10cSrcweir 										  aPrevUserBasicStandardInetObj,
1050*cdf0e10cSrcweir 										  aCheckFileName, aXlbExtension, mxSFI );
1051*cdf0e10cSrcweir 
1052*cdf0e10cSrcweir 					// Check if module1.xba exists
1053*cdf0e10cSrcweir 					OUString aXbaExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xba") ) );
1054*cdf0e10cSrcweir 					aCheckFileName = OUString( RTL_CONSTASCII_USTRINGPARAM("Module1") );
1055*cdf0e10cSrcweir 					checkAndCopyFileImpl( aUserBasicStandardInetObj,
1056*cdf0e10cSrcweir 										  aPrevUserBasicStandardInetObj,
1057*cdf0e10cSrcweir 										  aCheckFileName, aXbaExtension, mxSFI );
1058*cdf0e10cSrcweir 				}
1059*cdf0e10cSrcweir 				else
1060*cdf0e10cSrcweir 				{
1061*cdf0e10cSrcweir 					String aStandardFolder = aUserBasicStandardInetObj.GetMainURL( INetURLObject::NO_DECODE );
1062*cdf0e10cSrcweir 					mxSFI->copy( aStandardFolder, aPrevStandardFolder );
1063*cdf0e10cSrcweir 				}
1064*cdf0e10cSrcweir 
1065*cdf0e10cSrcweir 				String aPrevCopyToFolder = aPrevUserBasicInetObj_2.GetMainURL( INetURLObject::NO_DECODE );
1066*cdf0e10cSrcweir 				mxSFI->copy( aPrevFolder, aPrevCopyToFolder );
1067*cdf0e10cSrcweir 			}
1068*cdf0e10cSrcweir 			else
1069*cdf0e10cSrcweir 			{
1070*cdf0e10cSrcweir 				bSecondTime = true;
1071*cdf0e10cSrcweir 				aPrevUserBasicInetObj = aPrevUserBasicInetObj_2;
1072*cdf0e10cSrcweir 				aPrevFolder = aPrevUserBasicInetObj.GetMainURL( INetURLObject::NO_DECODE );
1073*cdf0e10cSrcweir 			}
1074*cdf0e10cSrcweir 			if( mxSFI->isFolder( aPrevFolder ) )
1075*cdf0e10cSrcweir 			{
1076*cdf0e10cSrcweir 				SfxLibraryContainer* pPrevCont = createInstanceImpl();
1077*cdf0e10cSrcweir 				Reference< XInterface > xRef = static_cast< XInterface* >( static_cast< OWeakObject* >(pPrevCont) );
1078*cdf0e10cSrcweir 
1079*cdf0e10cSrcweir 				// Rename previous basic folder to make storage URLs correct during initialisation
1080*cdf0e10cSrcweir 				String aFolderUserBasic = aUserBasicInetObj.GetMainURL( INetURLObject::NO_DECODE );
1081*cdf0e10cSrcweir 				INetURLObject aUserBasicTmpInetObj( aUserBasicInetObj );
1082*cdf0e10cSrcweir 				aUserBasicTmpInetObj.removeSegment();
1083*cdf0e10cSrcweir 				aUserBasicTmpInetObj.Append( "__basic_tmp" );
1084*cdf0e10cSrcweir 				String aFolderTmp = aUserBasicTmpInetObj.GetMainURL( INetURLObject::NO_DECODE );
1085*cdf0e10cSrcweir 
1086*cdf0e10cSrcweir 				mxSFI->move( aFolderUserBasic, aFolderTmp );
1087*cdf0e10cSrcweir 				try
1088*cdf0e10cSrcweir 				{
1089*cdf0e10cSrcweir 					mxSFI->move( aPrevFolder, aFolderUserBasic );
1090*cdf0e10cSrcweir 				}
1091*cdf0e10cSrcweir 				catch( Exception& )
1092*cdf0e10cSrcweir 				{
1093*cdf0e10cSrcweir 					// Move back user/basic folder
1094*cdf0e10cSrcweir 					try
1095*cdf0e10cSrcweir 					{
1096*cdf0e10cSrcweir            				mxSFI->kill( aFolderUserBasic );
1097*cdf0e10cSrcweir 					}
1098*cdf0e10cSrcweir 					catch( Exception& )
1099*cdf0e10cSrcweir 					{}
1100*cdf0e10cSrcweir 					mxSFI->move( aFolderTmp, aFolderUserBasic );
1101*cdf0e10cSrcweir 					throw;
1102*cdf0e10cSrcweir 				}
1103*cdf0e10cSrcweir 
1104*cdf0e10cSrcweir 				INetURLObject aPrevUserBasicLibInfoInetObj( aUserBasicInetObj );
1105*cdf0e10cSrcweir 				aPrevUserBasicLibInfoInetObj.insertName( maInfoFileName, sal_True, INetURLObject::LAST_SEGMENT,
1106*cdf0e10cSrcweir 													sal_True, INetURLObject::ENCODE_ALL );
1107*cdf0e10cSrcweir 				aPrevUserBasicLibInfoInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xlc") ) );
1108*cdf0e10cSrcweir 				OUString aLibInfoFileName = aPrevUserBasicLibInfoInetObj.GetMainURL( INetURLObject::NO_DECODE );
1109*cdf0e10cSrcweir 				Sequence<Any> aInitSeq( 1 );
1110*cdf0e10cSrcweir 				aInitSeq.getArray()[0] <<= aLibInfoFileName;
1111*cdf0e10cSrcweir 				GbMigrationSuppressErrors = true;
1112*cdf0e10cSrcweir 				pPrevCont->initialize( aInitSeq );
1113*cdf0e10cSrcweir 				GbMigrationSuppressErrors = false;
1114*cdf0e10cSrcweir 
1115*cdf0e10cSrcweir 				// Rename folders back
1116*cdf0e10cSrcweir 				mxSFI->move( aFolderUserBasic, aPrevFolder );
1117*cdf0e10cSrcweir 				mxSFI->move( aFolderTmp, aFolderUserBasic );
1118*cdf0e10cSrcweir 
1119*cdf0e10cSrcweir 				OUString aUserSearchStr   = OUString::createFromAscii( "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE" );
1120*cdf0e10cSrcweir 				OUString aSharedSearchStr = OUString::createFromAscii( "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE" );
1121*cdf0e10cSrcweir             	OUString aBundledSearchStr = OUString::createFromAscii( "vnd.sun.star.expand:$BUNDLED_EXTENSIONS" );
1122*cdf0e10cSrcweir 				OUString aInstSearchStr   = OUString::createFromAscii( "$(INST)" );
1123*cdf0e10cSrcweir 
1124*cdf0e10cSrcweir 				Sequence< OUString > aNames = pPrevCont->getElementNames();
1125*cdf0e10cSrcweir 				const OUString* pNames = aNames.getConstArray();
1126*cdf0e10cSrcweir 				sal_Int32 nNameCount = aNames.getLength();
1127*cdf0e10cSrcweir 
1128*cdf0e10cSrcweir 				for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
1129*cdf0e10cSrcweir 				{
1130*cdf0e10cSrcweir 					OUString aLibName = pNames[ i ];
1131*cdf0e10cSrcweir 					if( hasByName( aLibName ) )
1132*cdf0e10cSrcweir 					{
1133*cdf0e10cSrcweir 						if( aLibName == aStandardStr )
1134*cdf0e10cSrcweir 						{
1135*cdf0e10cSrcweir 							SfxLibrary* pImplLib = getImplLib( aStandardStr );
1136*cdf0e10cSrcweir 							INetURLObject aStandardFolderInetObj( pImplLib->maStorageURL );
1137*cdf0e10cSrcweir 							String aStandardFolder = pImplLib->maStorageURL;
1138*cdf0e10cSrcweir             				mxSFI->kill( aStandardFolder );
1139*cdf0e10cSrcweir 						}
1140*cdf0e10cSrcweir 						else
1141*cdf0e10cSrcweir 						{
1142*cdf0e10cSrcweir 							continue;
1143*cdf0e10cSrcweir 						}
1144*cdf0e10cSrcweir 					}
1145*cdf0e10cSrcweir 
1146*cdf0e10cSrcweir 					SfxLibrary* pImplLib = pPrevCont->getImplLib( aLibName );
1147*cdf0e10cSrcweir 					if( pImplLib->mbLink )
1148*cdf0e10cSrcweir 					{
1149*cdf0e10cSrcweir 						OUString aStorageURL = pImplLib->maUnexpandedStorageURL;
1150*cdf0e10cSrcweir 						bool bCreateLink = true;
1151*cdf0e10cSrcweir 						if( aStorageURL.indexOf( aUserSearchStr   ) != -1 ||
1152*cdf0e10cSrcweir 							aStorageURL.indexOf( aSharedSearchStr ) != -1 ||
1153*cdf0e10cSrcweir 							aStorageURL.indexOf( aBundledSearchStr ) != -1 ||
1154*cdf0e10cSrcweir 							aStorageURL.indexOf( aInstSearchStr   ) != -1 )
1155*cdf0e10cSrcweir 						{
1156*cdf0e10cSrcweir 							bCreateLink = false;
1157*cdf0e10cSrcweir 						}
1158*cdf0e10cSrcweir 						if( bCreateLink )
1159*cdf0e10cSrcweir 							createLibraryLink( aLibName, pImplLib->maStorageURL, pImplLib->mbReadOnly );
1160*cdf0e10cSrcweir 					}
1161*cdf0e10cSrcweir 					else
1162*cdf0e10cSrcweir 					{
1163*cdf0e10cSrcweir 						// Move folder if not already done
1164*cdf0e10cSrcweir 						INetURLObject aUserBasicLibFolderInetObj( aUserBasicInetObj );
1165*cdf0e10cSrcweir     					aUserBasicLibFolderInetObj.Append( aLibName );
1166*cdf0e10cSrcweir 						String aLibFolder = aUserBasicLibFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
1167*cdf0e10cSrcweir 
1168*cdf0e10cSrcweir 						INetURLObject aPrevUserBasicLibFolderInetObj( aPrevUserBasicInetObj );
1169*cdf0e10cSrcweir     					aPrevUserBasicLibFolderInetObj.Append( aLibName );
1170*cdf0e10cSrcweir 						String aPrevLibFolder = aPrevUserBasicLibFolderInetObj.GetMainURL( INetURLObject::NO_DECODE );
1171*cdf0e10cSrcweir 
1172*cdf0e10cSrcweir 						if( mxSFI->isFolder( aPrevLibFolder ) && !mxSFI->isFolder( aLibFolder ) )
1173*cdf0e10cSrcweir 							mxSFI->move( aPrevLibFolder, aLibFolder );
1174*cdf0e10cSrcweir 
1175*cdf0e10cSrcweir 						if( aLibName == aStandardStr )
1176*cdf0e10cSrcweir                    			maNameContainer.removeByName( aLibName );
1177*cdf0e10cSrcweir 
1178*cdf0e10cSrcweir 						// Create library
1179*cdf0e10cSrcweir 						Reference< XNameContainer > xLib = createLibrary( aLibName );
1180*cdf0e10cSrcweir            				SfxLibrary* pNewLib = static_cast< SfxLibrary* >( xLib.get() );
1181*cdf0e10cSrcweir 						pNewLib->mbLoaded = false;
1182*cdf0e10cSrcweir 						pNewLib->implSetModified( sal_False );
1183*cdf0e10cSrcweir 						checkStorageURL( aLibFolder, pNewLib->maLibInfoFileURL,
1184*cdf0e10cSrcweir 							pNewLib->maStorageURL, pNewLib->maUnexpandedStorageURL );
1185*cdf0e10cSrcweir 
1186*cdf0e10cSrcweir 						uno::Reference< embed::XStorage > xDummyStor;
1187*cdf0e10cSrcweir 						::xmlscript::LibDescriptor aLibDesc;
1188*cdf0e10cSrcweir 						/*sal_Bool bReadIndexFile =*/ implLoadLibraryIndexFile
1189*cdf0e10cSrcweir 							( pNewLib, aLibDesc, xDummyStor, pNewLib->maLibInfoFileURL );
1190*cdf0e10cSrcweir 						implImportLibDescriptor( pNewLib, aLibDesc );
1191*cdf0e10cSrcweir 					}
1192*cdf0e10cSrcweir 				}
1193*cdf0e10cSrcweir 				mxSFI->kill( aPrevFolder );
1194*cdf0e10cSrcweir 			}
1195*cdf0e10cSrcweir 		}
1196*cdf0e10cSrcweir 		catch( Exception& )
1197*cdf0e10cSrcweir 		{
1198*cdf0e10cSrcweir 			bCleanUp = true;
1199*cdf0e10cSrcweir 		}
1200*cdf0e10cSrcweir 
1201*cdf0e10cSrcweir 		// #i93163
1202*cdf0e10cSrcweir 		if( bCleanUp )
1203*cdf0e10cSrcweir 		{
1204*cdf0e10cSrcweir 			DBG_ERROR( "Upgrade of Basic installation failed somehow" );
1205*cdf0e10cSrcweir 
1206*cdf0e10cSrcweir 			static char strErrorSavFolderName[] = "__basic_80_err";
1207*cdf0e10cSrcweir 			INetURLObject aPrevUserBasicInetObj_Err( aUserBasicInetObj );
1208*cdf0e10cSrcweir 			aPrevUserBasicInetObj_Err.removeSegment();
1209*cdf0e10cSrcweir 			aPrevUserBasicInetObj_Err.Append( strErrorSavFolderName );
1210*cdf0e10cSrcweir 			String aPrevFolder_Err = aPrevUserBasicInetObj_Err.GetMainURL( INetURLObject::NO_DECODE );
1211*cdf0e10cSrcweir 
1212*cdf0e10cSrcweir 			bool bSaved = false;
1213*cdf0e10cSrcweir 			try
1214*cdf0e10cSrcweir 			{
1215*cdf0e10cSrcweir 				String aPrevFolder_1 = aPrevUserBasicInetObj_1.GetMainURL( INetURLObject::NO_DECODE );
1216*cdf0e10cSrcweir 				if( mxSFI->isFolder( aPrevFolder_1 ) )
1217*cdf0e10cSrcweir 				{
1218*cdf0e10cSrcweir 					mxSFI->move( aPrevFolder_1, aPrevFolder_Err );
1219*cdf0e10cSrcweir 					bSaved = true;
1220*cdf0e10cSrcweir 				}
1221*cdf0e10cSrcweir 			}
1222*cdf0e10cSrcweir 			catch( Exception& )
1223*cdf0e10cSrcweir 			{}
1224*cdf0e10cSrcweir 			try
1225*cdf0e10cSrcweir 			{
1226*cdf0e10cSrcweir 				String aPrevFolder_2 = aPrevUserBasicInetObj_2.GetMainURL( INetURLObject::NO_DECODE );
1227*cdf0e10cSrcweir 				if( !bSaved && mxSFI->isFolder( aPrevFolder_2 ) )
1228*cdf0e10cSrcweir 					mxSFI->move( aPrevFolder_2, aPrevFolder_Err );
1229*cdf0e10cSrcweir 				else
1230*cdf0e10cSrcweir 					mxSFI->kill( aPrevFolder_2 );
1231*cdf0e10cSrcweir 			}
1232*cdf0e10cSrcweir 			catch( Exception& )
1233*cdf0e10cSrcweir 			{}
1234*cdf0e10cSrcweir 		}
1235*cdf0e10cSrcweir 	}
1236*cdf0e10cSrcweir 
1237*cdf0e10cSrcweir 	return sal_True;
1238*cdf0e10cSrcweir }
1239*cdf0e10cSrcweir 
1240*cdf0e10cSrcweir void SfxLibraryContainer::implScanExtensions( void )
1241*cdf0e10cSrcweir {
1242*cdf0e10cSrcweir 	ScriptExtensionIterator aScriptIt;
1243*cdf0e10cSrcweir 	rtl::OUString aLibURL;
1244*cdf0e10cSrcweir 
1245*cdf0e10cSrcweir 	bool bPureDialogLib = false;
1246*cdf0e10cSrcweir 	while( (aLibURL = aScriptIt.nextBasicOrDialogLibrary( bPureDialogLib )).getLength() > 0 )
1247*cdf0e10cSrcweir 	{
1248*cdf0e10cSrcweir 		if( bPureDialogLib && maInfoFileName.equalsAscii( "script" ) )
1249*cdf0e10cSrcweir 			continue;
1250*cdf0e10cSrcweir 
1251*cdf0e10cSrcweir 		// Extract lib name
1252*cdf0e10cSrcweir 		sal_Int32 nLen = aLibURL.getLength();
1253*cdf0e10cSrcweir 		sal_Int32 indexLastSlash = aLibURL.lastIndexOf( '/' );
1254*cdf0e10cSrcweir 		sal_Int32 nReduceCopy = 0;
1255*cdf0e10cSrcweir 		if( indexLastSlash == nLen - 1 )
1256*cdf0e10cSrcweir 		{
1257*cdf0e10cSrcweir 			nReduceCopy = 1;
1258*cdf0e10cSrcweir 			indexLastSlash = aLibURL.lastIndexOf( '/', nLen - 1 );
1259*cdf0e10cSrcweir 		}
1260*cdf0e10cSrcweir 
1261*cdf0e10cSrcweir 		OUString aLibName = aLibURL.copy( indexLastSlash + 1, nLen - indexLastSlash - nReduceCopy - 1 );
1262*cdf0e10cSrcweir 
1263*cdf0e10cSrcweir 	    // If a library of the same exists the existing library wins
1264*cdf0e10cSrcweir 	    if( hasByName( aLibName ) )
1265*cdf0e10cSrcweir 		    continue;
1266*cdf0e10cSrcweir 
1267*cdf0e10cSrcweir 		// Add index file to URL
1268*cdf0e10cSrcweir 		OUString aIndexFileURL = aLibURL;
1269*cdf0e10cSrcweir 		if( nReduceCopy == 0 )
1270*cdf0e10cSrcweir 			aIndexFileURL += OUString::createFromAscii( "/" );
1271*cdf0e10cSrcweir 		aIndexFileURL += maInfoFileName;
1272*cdf0e10cSrcweir 		aIndexFileURL += OUString::createFromAscii( ".xlb" );
1273*cdf0e10cSrcweir 
1274*cdf0e10cSrcweir 		// Create link
1275*cdf0e10cSrcweir 		const bool bReadOnly = false;
1276*cdf0e10cSrcweir 	    Reference< XNameAccess > xLib =
1277*cdf0e10cSrcweir 		    createLibraryLink( aLibName, aIndexFileURL, bReadOnly );
1278*cdf0e10cSrcweir     }
1279*cdf0e10cSrcweir }
1280*cdf0e10cSrcweir 
1281*cdf0e10cSrcweir // Handle maLibInfoFileURL and maStorageURL correctly
1282*cdf0e10cSrcweir void SfxLibraryContainer::checkStorageURL( const OUString& aSourceURL,
1283*cdf0e10cSrcweir     OUString& aLibInfoFileURL, OUString& aStorageURL, OUString& aUnexpandedStorageURL )
1284*cdf0e10cSrcweir {
1285*cdf0e10cSrcweir     OUString aExpandedSourceURL = expand_url( aSourceURL );
1286*cdf0e10cSrcweir     if( aExpandedSourceURL != aSourceURL )
1287*cdf0e10cSrcweir         aUnexpandedStorageURL = aSourceURL;
1288*cdf0e10cSrcweir 
1289*cdf0e10cSrcweir 	INetURLObject aInetObj( aExpandedSourceURL );
1290*cdf0e10cSrcweir     OUString aExtension = aInetObj.getExtension();
1291*cdf0e10cSrcweir     if( aExtension.compareToAscii( "xlb" ) == COMPARE_EQUAL )
1292*cdf0e10cSrcweir     {
1293*cdf0e10cSrcweir         // URL to xlb file
1294*cdf0e10cSrcweir 		aLibInfoFileURL = aExpandedSourceURL;
1295*cdf0e10cSrcweir         aInetObj.removeSegment();
1296*cdf0e10cSrcweir 		aStorageURL = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
1297*cdf0e10cSrcweir     }
1298*cdf0e10cSrcweir     else
1299*cdf0e10cSrcweir     {
1300*cdf0e10cSrcweir         // URL to library folder
1301*cdf0e10cSrcweir         aStorageURL = aExpandedSourceURL;
1302*cdf0e10cSrcweir 		aInetObj.insertName( maInfoFileName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1303*cdf0e10cSrcweir 		aInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xlb") ) );
1304*cdf0e10cSrcweir 		aLibInfoFileURL = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
1305*cdf0e10cSrcweir     }
1306*cdf0e10cSrcweir }
1307*cdf0e10cSrcweir 
1308*cdf0e10cSrcweir SfxLibrary* SfxLibraryContainer::getImplLib( const String& rLibraryName )
1309*cdf0e10cSrcweir {
1310*cdf0e10cSrcweir 	Any aLibAny = maNameContainer.getByName( rLibraryName ) ;
1311*cdf0e10cSrcweir 	Reference< XNameAccess > xNameAccess;
1312*cdf0e10cSrcweir 	aLibAny >>= xNameAccess;
1313*cdf0e10cSrcweir 	SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
1314*cdf0e10cSrcweir     return pImplLib;
1315*cdf0e10cSrcweir }
1316*cdf0e10cSrcweir 
1317*cdf0e10cSrcweir 
1318*cdf0e10cSrcweir // Storing with password encryption
1319*cdf0e10cSrcweir 
1320*cdf0e10cSrcweir // Empty implementation, avoids unneccesary implementation in dlgcont.cxx
1321*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::implStorePasswordLibrary(
1322*cdf0e10cSrcweir     SfxLibrary*,
1323*cdf0e10cSrcweir     const OUString&,
1324*cdf0e10cSrcweir     const uno::Reference< embed::XStorage >&, const uno::Reference< task::XInteractionHandler >&  )
1325*cdf0e10cSrcweir {
1326*cdf0e10cSrcweir     return sal_False;
1327*cdf0e10cSrcweir }
1328*cdf0e10cSrcweir 
1329*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::implStorePasswordLibrary(
1330*cdf0e10cSrcweir     SfxLibrary* /*pLib*/,
1331*cdf0e10cSrcweir     const ::rtl::OUString& /*aName*/,
1332*cdf0e10cSrcweir     const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& /*xStorage*/,
1333*cdf0e10cSrcweir     const ::rtl::OUString& /*aTargetURL*/,
1334*cdf0e10cSrcweir     const Reference< XSimpleFileAccess > /*xToUseSFI*/,
1335*cdf0e10cSrcweir     const uno::Reference< task::XInteractionHandler >&  )
1336*cdf0e10cSrcweir {
1337*cdf0e10cSrcweir     return sal_False;
1338*cdf0e10cSrcweir }
1339*cdf0e10cSrcweir 
1340*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::implLoadPasswordLibrary(
1341*cdf0e10cSrcweir     SfxLibrary* /*pLib*/,
1342*cdf0e10cSrcweir     const OUString& /*Name*/,
1343*cdf0e10cSrcweir     sal_Bool /*bVerifyPasswordOnly*/ )
1344*cdf0e10cSrcweir throw(WrappedTargetException, RuntimeException)
1345*cdf0e10cSrcweir {
1346*cdf0e10cSrcweir     return sal_True;
1347*cdf0e10cSrcweir }
1348*cdf0e10cSrcweir 
1349*cdf0e10cSrcweir 
1350*cdf0e10cSrcweir 
1351*cdf0e10cSrcweir #define EXPAND_PROTOCOL "vnd.sun.star.expand"
1352*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
1353*cdf0e10cSrcweir 
1354*cdf0e10cSrcweir OUString SfxLibraryContainer::createAppLibraryFolder
1355*cdf0e10cSrcweir     ( SfxLibrary* pLib, const OUString& aName )
1356*cdf0e10cSrcweir {
1357*cdf0e10cSrcweir 	OUString aLibDirPath = pLib->maStorageURL;
1358*cdf0e10cSrcweir 	if( !aLibDirPath.getLength() )
1359*cdf0e10cSrcweir     {
1360*cdf0e10cSrcweir 		INetURLObject aInetObj( String(maLibraryPath).GetToken(1) );
1361*cdf0e10cSrcweir 		aInetObj.insertName( aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1362*cdf0e10cSrcweir         checkStorageURL( aInetObj.GetMainURL( INetURLObject::NO_DECODE ), pLib->maLibInfoFileURL,
1363*cdf0e10cSrcweir             pLib->maStorageURL, pLib->maUnexpandedStorageURL );
1364*cdf0e10cSrcweir 		aLibDirPath = pLib->maStorageURL;
1365*cdf0e10cSrcweir     }
1366*cdf0e10cSrcweir 
1367*cdf0e10cSrcweir 	if( !mxSFI->isFolder( aLibDirPath ) )
1368*cdf0e10cSrcweir     {
1369*cdf0e10cSrcweir 	    try
1370*cdf0e10cSrcweir 	    {
1371*cdf0e10cSrcweir 		    mxSFI->createFolder( aLibDirPath );
1372*cdf0e10cSrcweir         }
1373*cdf0e10cSrcweir         catch( Exception& )
1374*cdf0e10cSrcweir         {}
1375*cdf0e10cSrcweir     }
1376*cdf0e10cSrcweir 
1377*cdf0e10cSrcweir     return aLibDirPath;
1378*cdf0e10cSrcweir }
1379*cdf0e10cSrcweir 
1380*cdf0e10cSrcweir // Storing
1381*cdf0e10cSrcweir void SfxLibraryContainer::implStoreLibrary( SfxLibrary* pLib,
1382*cdf0e10cSrcweir 	const OUString& aName, const uno::Reference< embed::XStorage >& xStorage )
1383*cdf0e10cSrcweir {
1384*cdf0e10cSrcweir 	OUString aDummyLocation;
1385*cdf0e10cSrcweir 	Reference< XSimpleFileAccess > xDummySFA;
1386*cdf0e10cSrcweir 	Reference< XInteractionHandler > xDummyHandler;
1387*cdf0e10cSrcweir 	implStoreLibrary( pLib, aName, xStorage, aDummyLocation, xDummySFA, xDummyHandler );
1388*cdf0e10cSrcweir }
1389*cdf0e10cSrcweir 
1390*cdf0e10cSrcweir // New variant for library export
1391*cdf0e10cSrcweir void SfxLibraryContainer::implStoreLibrary( SfxLibrary* pLib,
1392*cdf0e10cSrcweir 	const OUString& aName, const uno::Reference< embed::XStorage >& xStorage,
1393*cdf0e10cSrcweir 	const ::rtl::OUString& aTargetURL, Reference< XSimpleFileAccess > xToUseSFI,
1394*cdf0e10cSrcweir 	const Reference< XInteractionHandler >& xHandler )
1395*cdf0e10cSrcweir {
1396*cdf0e10cSrcweir 	sal_Bool bLink = pLib->mbLink;
1397*cdf0e10cSrcweir 	sal_Bool bStorage = xStorage.is() && !bLink;
1398*cdf0e10cSrcweir 
1399*cdf0e10cSrcweir 	Sequence< OUString > aElementNames = pLib->getElementNames();
1400*cdf0e10cSrcweir 	sal_Int32 nNameCount = aElementNames.getLength();
1401*cdf0e10cSrcweir 	const OUString* pNames = aElementNames.getConstArray();
1402*cdf0e10cSrcweir 
1403*cdf0e10cSrcweir 	if( bStorage )
1404*cdf0e10cSrcweir 	{
1405*cdf0e10cSrcweir 		for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
1406*cdf0e10cSrcweir 		{
1407*cdf0e10cSrcweir 			OUString aElementName = pNames[ i ];
1408*cdf0e10cSrcweir 
1409*cdf0e10cSrcweir 			OUString aStreamName = aElementName;
1410*cdf0e10cSrcweir 			aStreamName += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
1411*cdf0e10cSrcweir 
1412*cdf0e10cSrcweir 			/*Any aElement = pLib->getByName( aElementName );*/
1413*cdf0e10cSrcweir 			if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
1414*cdf0e10cSrcweir             {
1415*cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 0
1416*cdf0e10cSrcweir                 ::rtl::OStringBuffer aMessage;
1417*cdf0e10cSrcweir                 aMessage.append( "invalid library element '" );
1418*cdf0e10cSrcweir                 aMessage.append( ::rtl::OUStringToOString( aElementName, osl_getThreadTextEncoding() ) );
1419*cdf0e10cSrcweir                 aMessage.append( "'." );
1420*cdf0e10cSrcweir 			    OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
1421*cdf0e10cSrcweir             #endif
1422*cdf0e10cSrcweir                 continue;
1423*cdf0e10cSrcweir             }
1424*cdf0e10cSrcweir 			try {
1425*cdf0e10cSrcweir 				uno::Reference< io::XStream > xElementStream = xStorage->openStreamElement(
1426*cdf0e10cSrcweir 																	aStreamName,
1427*cdf0e10cSrcweir 																	embed::ElementModes::READWRITE );
1428*cdf0e10cSrcweir                 //if ( !xElementStream.is() )
1429*cdf0e10cSrcweir                 //    throw uno::RuntimeException(); // TODO: method must either return the stream or throw an exception
1430*cdf0e10cSrcweir 
1431*cdf0e10cSrcweir 				String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
1432*cdf0e10cSrcweir 				OUString aMime( RTL_CONSTASCII_USTRINGPARAM("text/xml") );
1433*cdf0e10cSrcweir 
1434*cdf0e10cSrcweir 				uno::Reference< beans::XPropertySet > xProps( xElementStream, uno::UNO_QUERY );
1435*cdf0e10cSrcweir 				OSL_ENSURE( xProps.is(), "The StorageStream must implement XPropertySet interface!\n" );
1436*cdf0e10cSrcweir                 //if ( !xProps.is() ) //TODO
1437*cdf0e10cSrcweir                 //    throw uno::RuntimeException();
1438*cdf0e10cSrcweir 
1439*cdf0e10cSrcweir                 if ( xProps.is() )
1440*cdf0e10cSrcweir                 {
1441*cdf0e10cSrcweir                     xProps->setPropertyValue( aPropName, uno::makeAny( aMime ) );
1442*cdf0e10cSrcweir 
1443*cdf0e10cSrcweir                     // #87671 Allow encryption
1444*cdf0e10cSrcweir //REMOVE	                        aPropName = String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("Encrypted") );
1445*cdf0e10cSrcweir                     aPropName = String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "UseCommonStoragePasswordEncryption" ) );
1446*cdf0e10cSrcweir                     xProps->setPropertyValue( aPropName, uno::makeAny( sal_True ) );
1447*cdf0e10cSrcweir 
1448*cdf0e10cSrcweir                     Reference< XOutputStream > xOutput = xElementStream->getOutputStream();
1449*cdf0e10cSrcweir 					Reference< XNameContainer > xLib( pLib );
1450*cdf0e10cSrcweir                     writeLibraryElement( xLib, aElementName, xOutput );
1451*cdf0e10cSrcweir 					// writeLibraryElement closes the stream
1452*cdf0e10cSrcweir                     // xOutput->closeOutput();
1453*cdf0e10cSrcweir                 }
1454*cdf0e10cSrcweir 			}
1455*cdf0e10cSrcweir 			catch( uno::Exception& )
1456*cdf0e10cSrcweir 			{
1457*cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "Problem during storing of library!\n" );
1458*cdf0e10cSrcweir                 // TODO: error handling?
1459*cdf0e10cSrcweir 			}
1460*cdf0e10cSrcweir 		}
1461*cdf0e10cSrcweir 
1462*cdf0e10cSrcweir 		pLib->storeResourcesToStorage( xStorage );
1463*cdf0e10cSrcweir 	}
1464*cdf0e10cSrcweir 	else
1465*cdf0e10cSrcweir 	{
1466*cdf0e10cSrcweir 		// Export?
1467*cdf0e10cSrcweir 		bool bExport = aTargetURL.getLength();
1468*cdf0e10cSrcweir 		try
1469*cdf0e10cSrcweir 		{
1470*cdf0e10cSrcweir 			Reference< XSimpleFileAccess > xSFI = mxSFI;
1471*cdf0e10cSrcweir 			if( xToUseSFI.is() )
1472*cdf0e10cSrcweir 				xSFI = xToUseSFI;
1473*cdf0e10cSrcweir 
1474*cdf0e10cSrcweir             OUString aLibDirPath;
1475*cdf0e10cSrcweir 			if( bExport )
1476*cdf0e10cSrcweir 			{
1477*cdf0e10cSrcweir 				INetURLObject aInetObj( aTargetURL );
1478*cdf0e10cSrcweir 				aInetObj.insertName( aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1479*cdf0e10cSrcweir 				aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
1480*cdf0e10cSrcweir 
1481*cdf0e10cSrcweir 				if( !xSFI->isFolder( aLibDirPath ) )
1482*cdf0e10cSrcweir 					xSFI->createFolder( aLibDirPath );
1483*cdf0e10cSrcweir 
1484*cdf0e10cSrcweir 				pLib->storeResourcesToURL( aLibDirPath, xHandler );
1485*cdf0e10cSrcweir 			}
1486*cdf0e10cSrcweir 			else
1487*cdf0e10cSrcweir 			{
1488*cdf0e10cSrcweir 	            aLibDirPath = createAppLibraryFolder( pLib, aName );
1489*cdf0e10cSrcweir 				pLib->storeResources();
1490*cdf0e10cSrcweir 			}
1491*cdf0e10cSrcweir 
1492*cdf0e10cSrcweir 			for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
1493*cdf0e10cSrcweir 			{
1494*cdf0e10cSrcweir 				OUString aElementName = pNames[ i ];
1495*cdf0e10cSrcweir 
1496*cdf0e10cSrcweir 				INetURLObject aElementInetObj( aLibDirPath );
1497*cdf0e10cSrcweir 				aElementInetObj.insertName( aElementName, sal_False,
1498*cdf0e10cSrcweir 					INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1499*cdf0e10cSrcweir 				aElementInetObj.setExtension( maLibElementFileExtension );
1500*cdf0e10cSrcweir 				String aElementPath( aElementInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
1501*cdf0e10cSrcweir 
1502*cdf0e10cSrcweir 				/*Any aElement = pLib->getByName( aElementName );*/
1503*cdf0e10cSrcweir 				if( !isLibraryElementValid( pLib->getByName( aElementName ) ) )
1504*cdf0e10cSrcweir                 {
1505*cdf0e10cSrcweir                 #if OSL_DEBUG_LEVEL > 0
1506*cdf0e10cSrcweir                     ::rtl::OStringBuffer aMessage;
1507*cdf0e10cSrcweir                     aMessage.append( "invalid library element '" );
1508*cdf0e10cSrcweir                     aMessage.append( ::rtl::OUStringToOString( aElementName, osl_getThreadTextEncoding() ) );
1509*cdf0e10cSrcweir                     aMessage.append( "'." );
1510*cdf0e10cSrcweir 			        OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
1511*cdf0e10cSrcweir                 #endif
1512*cdf0e10cSrcweir                     continue;
1513*cdf0e10cSrcweir                 }
1514*cdf0e10cSrcweir 
1515*cdf0e10cSrcweir 				// TODO: Check modified
1516*cdf0e10cSrcweir 	            try
1517*cdf0e10cSrcweir 	            {
1518*cdf0e10cSrcweir 				    if( xSFI->exists( aElementPath ) )
1519*cdf0e10cSrcweir 					    xSFI->kill( aElementPath );
1520*cdf0e10cSrcweir 					Reference< XOutputStream > xOutput = xSFI->openFileWrite( aElementPath );
1521*cdf0e10cSrcweir 					Reference< XNameContainer > xLib( pLib );
1522*cdf0e10cSrcweir 				    writeLibraryElement( xLib, aElementName, xOutput );
1523*cdf0e10cSrcweir 				    xOutput->closeOutput();
1524*cdf0e10cSrcweir                 }
1525*cdf0e10cSrcweir         		catch( Exception& )
1526*cdf0e10cSrcweir                 {
1527*cdf0e10cSrcweir 					if( bExport )
1528*cdf0e10cSrcweir 						throw;
1529*cdf0e10cSrcweir 
1530*cdf0e10cSrcweir 		            SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aElementPath );
1531*cdf0e10cSrcweir                     sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
1532*cdf0e10cSrcweir             	    ErrorHandler::HandleError( nErrorCode );
1533*cdf0e10cSrcweir                 }
1534*cdf0e10cSrcweir 			}
1535*cdf0e10cSrcweir 		}
1536*cdf0e10cSrcweir 		catch( Exception& )
1537*cdf0e10cSrcweir 		{
1538*cdf0e10cSrcweir 			if( bExport )
1539*cdf0e10cSrcweir 				throw;
1540*cdf0e10cSrcweir 		}
1541*cdf0e10cSrcweir 	}
1542*cdf0e10cSrcweir }
1543*cdf0e10cSrcweir 
1544*cdf0e10cSrcweir void SfxLibraryContainer::implStoreLibraryIndexFile( SfxLibrary* pLib,
1545*cdf0e10cSrcweir 	const ::xmlscript::LibDescriptor& rLib, const uno::Reference< embed::XStorage >& xStorage )
1546*cdf0e10cSrcweir {
1547*cdf0e10cSrcweir 	OUString aDummyLocation;
1548*cdf0e10cSrcweir 	Reference< XSimpleFileAccess > xDummySFA;
1549*cdf0e10cSrcweir 	implStoreLibraryIndexFile( pLib, rLib, xStorage, aDummyLocation, xDummySFA );
1550*cdf0e10cSrcweir }
1551*cdf0e10cSrcweir 
1552*cdf0e10cSrcweir // New variant for library export
1553*cdf0e10cSrcweir void SfxLibraryContainer::implStoreLibraryIndexFile( SfxLibrary* pLib,
1554*cdf0e10cSrcweir 	const ::xmlscript::LibDescriptor& rLib, const uno::Reference< embed::XStorage >& xStorage,
1555*cdf0e10cSrcweir 	const ::rtl::OUString& aTargetURL, Reference< XSimpleFileAccess > xToUseSFI )
1556*cdf0e10cSrcweir {
1557*cdf0e10cSrcweir 	// Create sax writer
1558*cdf0e10cSrcweir 	Reference< XExtendedDocumentHandler > xHandler(
1559*cdf0e10cSrcweir 		mxMSF->createInstance(
1560*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") ) ), UNO_QUERY );
1561*cdf0e10cSrcweir 	if( !xHandler.is() )
1562*cdf0e10cSrcweir 	{
1563*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create sax-writer component\n" );
1564*cdf0e10cSrcweir 		return;
1565*cdf0e10cSrcweir 	}
1566*cdf0e10cSrcweir 
1567*cdf0e10cSrcweir 	sal_Bool bLink = pLib->mbLink;
1568*cdf0e10cSrcweir 	sal_Bool bStorage = xStorage.is() && !bLink;
1569*cdf0e10cSrcweir 
1570*cdf0e10cSrcweir 	// Write info file
1571*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xOut;
1572*cdf0e10cSrcweir 	uno::Reference< io::XStream > xInfoStream;
1573*cdf0e10cSrcweir 	if( bStorage )
1574*cdf0e10cSrcweir 	{
1575*cdf0e10cSrcweir 		OUString aStreamName( maInfoFileName );
1576*cdf0e10cSrcweir 		aStreamName += String( RTL_CONSTASCII_USTRINGPARAM("-lb.xml") );
1577*cdf0e10cSrcweir 
1578*cdf0e10cSrcweir 		try {
1579*cdf0e10cSrcweir 			xInfoStream = xStorage->openStreamElement( aStreamName, embed::ElementModes::READWRITE );
1580*cdf0e10cSrcweir             OSL_ENSURE( xInfoStream.is(), "No stream!\n" );
1581*cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xProps( xInfoStream, uno::UNO_QUERY );
1582*cdf0e10cSrcweir             //if ( !xProps.is() )
1583*cdf0e10cSrcweir             //    throw uno::RuntimeException(); // TODO
1584*cdf0e10cSrcweir 
1585*cdf0e10cSrcweir             if ( xProps.is() )
1586*cdf0e10cSrcweir             {
1587*cdf0e10cSrcweir                 String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
1588*cdf0e10cSrcweir                 OUString aMime( RTL_CONSTASCII_USTRINGPARAM("text/xml") );
1589*cdf0e10cSrcweir                 xProps->setPropertyValue( aPropName, uno::makeAny( aMime ) );
1590*cdf0e10cSrcweir 
1591*cdf0e10cSrcweir                 // #87671 Allow encryption
1592*cdf0e10cSrcweir //REMOVE	                aPropName = String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("Encrypted") );
1593*cdf0e10cSrcweir                 aPropName = String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "UseCommonStoragePasswordEncryption" ) );
1594*cdf0e10cSrcweir                 xProps->setPropertyValue( aPropName, uno::makeAny( sal_True ) );
1595*cdf0e10cSrcweir 
1596*cdf0e10cSrcweir                 xOut = xInfoStream->getOutputStream();
1597*cdf0e10cSrcweir             }
1598*cdf0e10cSrcweir 		}
1599*cdf0e10cSrcweir 		catch( uno::Exception& )
1600*cdf0e10cSrcweir 		{
1601*cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Problem during storing of library index file!\n" );
1602*cdf0e10cSrcweir             // TODO: error handling?
1603*cdf0e10cSrcweir 		}
1604*cdf0e10cSrcweir 	}
1605*cdf0e10cSrcweir 	else
1606*cdf0e10cSrcweir 	{
1607*cdf0e10cSrcweir 		// Export?
1608*cdf0e10cSrcweir 		bool bExport = aTargetURL.getLength();
1609*cdf0e10cSrcweir 		Reference< XSimpleFileAccess > xSFI = mxSFI;
1610*cdf0e10cSrcweir 		if( xToUseSFI.is() )
1611*cdf0e10cSrcweir 			xSFI = xToUseSFI;
1612*cdf0e10cSrcweir 
1613*cdf0e10cSrcweir         OUString aLibInfoPath;
1614*cdf0e10cSrcweir 		if( bExport )
1615*cdf0e10cSrcweir 		{
1616*cdf0e10cSrcweir 			INetURLObject aInetObj( aTargetURL );
1617*cdf0e10cSrcweir 			aInetObj.insertName( rLib.aName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1618*cdf0e10cSrcweir 			OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
1619*cdf0e10cSrcweir 			if( !xSFI->isFolder( aLibDirPath ) )
1620*cdf0e10cSrcweir 				xSFI->createFolder( aLibDirPath );
1621*cdf0e10cSrcweir 
1622*cdf0e10cSrcweir 			aInetObj.insertName( maInfoFileName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1623*cdf0e10cSrcweir 			aInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xlb") ) );
1624*cdf0e10cSrcweir 			aLibInfoPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
1625*cdf0e10cSrcweir 		}
1626*cdf0e10cSrcweir 		else
1627*cdf0e10cSrcweir 		{
1628*cdf0e10cSrcweir 			createAppLibraryFolder( pLib, rLib.aName );
1629*cdf0e10cSrcweir 			aLibInfoPath = pLib->maLibInfoFileURL;
1630*cdf0e10cSrcweir 		}
1631*cdf0e10cSrcweir 
1632*cdf0e10cSrcweir 		try
1633*cdf0e10cSrcweir 		{
1634*cdf0e10cSrcweir 		    if( xSFI->exists( aLibInfoPath ) )
1635*cdf0e10cSrcweir 			    xSFI->kill( aLibInfoPath );
1636*cdf0e10cSrcweir 		    xOut = xSFI->openFileWrite( aLibInfoPath );
1637*cdf0e10cSrcweir         }
1638*cdf0e10cSrcweir         catch( Exception& )
1639*cdf0e10cSrcweir         {
1640*cdf0e10cSrcweir 			if( bExport )
1641*cdf0e10cSrcweir 				throw;
1642*cdf0e10cSrcweir 
1643*cdf0e10cSrcweir 			SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aLibInfoPath );
1644*cdf0e10cSrcweir             sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
1645*cdf0e10cSrcweir             ErrorHandler::HandleError( nErrorCode );
1646*cdf0e10cSrcweir         }
1647*cdf0e10cSrcweir 	}
1648*cdf0e10cSrcweir 	if( !xOut.is() )
1649*cdf0e10cSrcweir 	{
1650*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't open output stream\n" );
1651*cdf0e10cSrcweir 		return;
1652*cdf0e10cSrcweir 	}
1653*cdf0e10cSrcweir 
1654*cdf0e10cSrcweir 	Reference< XActiveDataSource > xSource( xHandler, UNO_QUERY );
1655*cdf0e10cSrcweir 	xSource->setOutputStream( xOut );
1656*cdf0e10cSrcweir 
1657*cdf0e10cSrcweir     xmlscript::exportLibrary( xHandler, rLib );
1658*cdf0e10cSrcweir }
1659*cdf0e10cSrcweir 
1660*cdf0e10cSrcweir 
1661*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::implLoadLibraryIndexFile(  SfxLibrary* pLib,
1662*cdf0e10cSrcweir     ::xmlscript::LibDescriptor& rLib, const uno::Reference< embed::XStorage >& xStorage, const OUString& aIndexFileName )
1663*cdf0e10cSrcweir {
1664*cdf0e10cSrcweir 	Reference< XParser > xParser( mxMSF->createInstance(
1665*cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ) ), UNO_QUERY );
1666*cdf0e10cSrcweir 	if( !xParser.is() )
1667*cdf0e10cSrcweir 	{
1668*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create sax parser component\n" );
1669*cdf0e10cSrcweir 		return sal_False;
1670*cdf0e10cSrcweir 	}
1671*cdf0e10cSrcweir 
1672*cdf0e10cSrcweir 	sal_Bool bLink = sal_False;
1673*cdf0e10cSrcweir 	sal_Bool bStorage = sal_False;
1674*cdf0e10cSrcweir     if( pLib )
1675*cdf0e10cSrcweir     {
1676*cdf0e10cSrcweir 	    bLink = pLib->mbLink;
1677*cdf0e10cSrcweir 	    bStorage = xStorage.is() && !bLink;
1678*cdf0e10cSrcweir     }
1679*cdf0e10cSrcweir 
1680*cdf0e10cSrcweir 	// Read info file
1681*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInput;
1682*cdf0e10cSrcweir     String aLibInfoPath;
1683*cdf0e10cSrcweir 	if( bStorage )
1684*cdf0e10cSrcweir 	{
1685*cdf0e10cSrcweir 		aLibInfoPath = maInfoFileName;
1686*cdf0e10cSrcweir 		aLibInfoPath += String( RTL_CONSTASCII_USTRINGPARAM("-lb.xml") );
1687*cdf0e10cSrcweir 
1688*cdf0e10cSrcweir 		try {
1689*cdf0e10cSrcweir 			uno::Reference< io::XStream > xInfoStream =
1690*cdf0e10cSrcweir 						xStorage->openStreamElement( aLibInfoPath, embed::ElementModes::READ );
1691*cdf0e10cSrcweir 			xInput = xInfoStream->getInputStream();
1692*cdf0e10cSrcweir 		}
1693*cdf0e10cSrcweir 		catch( uno::Exception& )
1694*cdf0e10cSrcweir 		{}
1695*cdf0e10cSrcweir 	}
1696*cdf0e10cSrcweir 	else
1697*cdf0e10cSrcweir 	{
1698*cdf0e10cSrcweir 		// Create Input stream
1699*cdf0e10cSrcweir         //String aLibInfoPath; // attention: THIS PROBLEM MUST BE REVIEWED BY SCRIPTING OWNER!!!
1700*cdf0e10cSrcweir 
1701*cdf0e10cSrcweir         if( pLib )
1702*cdf0e10cSrcweir         {
1703*cdf0e10cSrcweir             createAppLibraryFolder( pLib, rLib.aName );
1704*cdf0e10cSrcweir             aLibInfoPath = pLib->maLibInfoFileURL;
1705*cdf0e10cSrcweir         }
1706*cdf0e10cSrcweir         else
1707*cdf0e10cSrcweir             aLibInfoPath = aIndexFileName;
1708*cdf0e10cSrcweir 
1709*cdf0e10cSrcweir 		try
1710*cdf0e10cSrcweir 		{
1711*cdf0e10cSrcweir 			xInput = mxSFI->openFileRead( aLibInfoPath );
1712*cdf0e10cSrcweir 		}
1713*cdf0e10cSrcweir 		catch( Exception& )
1714*cdf0e10cSrcweir 		{
1715*cdf0e10cSrcweir             xInput.clear();
1716*cdf0e10cSrcweir 			if( !GbMigrationSuppressErrors )
1717*cdf0e10cSrcweir 			{
1718*cdf0e10cSrcweir 				SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aLibInfoPath );
1719*cdf0e10cSrcweir 				sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
1720*cdf0e10cSrcweir 				ErrorHandler::HandleError( nErrorCode );
1721*cdf0e10cSrcweir 			}
1722*cdf0e10cSrcweir 		}
1723*cdf0e10cSrcweir 	}
1724*cdf0e10cSrcweir 	if( !xInput.is() )
1725*cdf0e10cSrcweir 	{
1726*cdf0e10cSrcweir 		// OSL_ENSURE( 0, "### couln't open input stream\n" );
1727*cdf0e10cSrcweir 		return sal_False;
1728*cdf0e10cSrcweir 	}
1729*cdf0e10cSrcweir 
1730*cdf0e10cSrcweir 	InputSource source;
1731*cdf0e10cSrcweir 	source.aInputStream = xInput;
1732*cdf0e10cSrcweir 	source.sSystemId 	= aLibInfoPath;
1733*cdf0e10cSrcweir 
1734*cdf0e10cSrcweir 	// start parsing
1735*cdf0e10cSrcweir 	try {
1736*cdf0e10cSrcweir 		xParser->setDocumentHandler( ::xmlscript::importLibrary( rLib ) );
1737*cdf0e10cSrcweir 		xParser->parseStream( source );
1738*cdf0e10cSrcweir 	}
1739*cdf0e10cSrcweir 	catch( Exception& )
1740*cdf0e10cSrcweir 	{
1741*cdf0e10cSrcweir 		// throw WrappedTargetException( OUString::createFromAscii( "parsing error!\n" ),
1742*cdf0e10cSrcweir 		//								Reference< XInterface >(),
1743*cdf0e10cSrcweir 		//								makeAny( e ) );
1744*cdf0e10cSrcweir 		OSL_ENSURE( 0, "Parsing error\n" );
1745*cdf0e10cSrcweir 		SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aLibInfoPath );
1746*cdf0e10cSrcweir         sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
1747*cdf0e10cSrcweir         ErrorHandler::HandleError( nErrorCode );
1748*cdf0e10cSrcweir 		return sal_False;
1749*cdf0e10cSrcweir 	}
1750*cdf0e10cSrcweir 
1751*cdf0e10cSrcweir     if( !pLib )
1752*cdf0e10cSrcweir     {
1753*cdf0e10cSrcweir 		Reference< XNameContainer > xLib = createLibrary( rLib.aName );
1754*cdf0e10cSrcweir 		pLib = static_cast< SfxLibrary* >( xLib.get() );
1755*cdf0e10cSrcweir 		pLib->mbLoaded = sal_False;
1756*cdf0e10cSrcweir         rLib.aStorageURL = aIndexFileName;
1757*cdf0e10cSrcweir         checkStorageURL( rLib.aStorageURL, pLib->maLibInfoFileURL, pLib->maStorageURL,
1758*cdf0e10cSrcweir             pLib->maUnexpandedStorageURL );
1759*cdf0e10cSrcweir 
1760*cdf0e10cSrcweir         implImportLibDescriptor( pLib, rLib );
1761*cdf0e10cSrcweir     }
1762*cdf0e10cSrcweir 
1763*cdf0e10cSrcweir     return sal_True;
1764*cdf0e10cSrcweir }
1765*cdf0e10cSrcweir 
1766*cdf0e10cSrcweir void SfxLibraryContainer::implImportLibDescriptor
1767*cdf0e10cSrcweir     ( SfxLibrary* pLib, ::xmlscript::LibDescriptor& rLib )
1768*cdf0e10cSrcweir {
1769*cdf0e10cSrcweir     if( !pLib->mbInitialised )
1770*cdf0e10cSrcweir     {
1771*cdf0e10cSrcweir 	    sal_Int32 nElementCount = rLib.aElementNames.getLength();
1772*cdf0e10cSrcweir 	    const OUString* pElementNames = rLib.aElementNames.getConstArray();
1773*cdf0e10cSrcweir 	    Any aDummyElement = createEmptyLibraryElement();
1774*cdf0e10cSrcweir 	    for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
1775*cdf0e10cSrcweir 	    {
1776*cdf0e10cSrcweir 		    pLib->maNameContainer.insertByName( pElementNames[i], aDummyElement );
1777*cdf0e10cSrcweir 	    }
1778*cdf0e10cSrcweir         pLib->mbPasswordProtected = rLib.bPasswordProtected;
1779*cdf0e10cSrcweir         pLib->mbReadOnly = rLib.bReadOnly;
1780*cdf0e10cSrcweir 		pLib->mbPreload  = rLib.bPreload;
1781*cdf0e10cSrcweir         pLib->implSetModified( sal_False );
1782*cdf0e10cSrcweir 
1783*cdf0e10cSrcweir         pLib->mbInitialised = sal_True;
1784*cdf0e10cSrcweir     }
1785*cdf0e10cSrcweir }
1786*cdf0e10cSrcweir 
1787*cdf0e10cSrcweir 
1788*cdf0e10cSrcweir // Methods of new XLibraryStorage interface?
1789*cdf0e10cSrcweir void SfxLibraryContainer::storeLibraries_Impl( const uno::Reference< embed::XStorage >& i_rStorage, sal_Bool bComplete )
1790*cdf0e10cSrcweir {
1791*cdf0e10cSrcweir 	const Sequence< OUString > aNames = maNameContainer.getElementNames();
1792*cdf0e10cSrcweir 	sal_Int32 nNameCount = aNames.getLength();
1793*cdf0e10cSrcweir     const OUString* pName = aNames.getConstArray();
1794*cdf0e10cSrcweir 	const OUString* pNamesEnd = aNames.getConstArray() + nNameCount;
1795*cdf0e10cSrcweir 
1796*cdf0e10cSrcweir 	// Don't count libs from shared index file
1797*cdf0e10cSrcweir 	sal_Int32 nLibsToSave = nNameCount;
1798*cdf0e10cSrcweir 	for( ; pName != pNamesEnd; ++pName )
1799*cdf0e10cSrcweir 	{
1800*cdf0e10cSrcweir         SfxLibrary* pImplLib = getImplLib( *pName );
1801*cdf0e10cSrcweir 		if( pImplLib->mbSharedIndexFile || pImplLib->mbExtension )
1802*cdf0e10cSrcweir 			nLibsToSave--;
1803*cdf0e10cSrcweir 	}
1804*cdf0e10cSrcweir     if( !nLibsToSave )
1805*cdf0e10cSrcweir         return;
1806*cdf0e10cSrcweir 
1807*cdf0e10cSrcweir 	::xmlscript::LibDescriptorArray* pLibArray = new ::xmlscript::LibDescriptorArray( nLibsToSave );
1808*cdf0e10cSrcweir 
1809*cdf0e10cSrcweir 	// Write to storage?
1810*cdf0e10cSrcweir 	sal_Bool bStorage = i_rStorage.is();
1811*cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xSourceLibrariesStor;
1812*cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTargetLibrariesStor;
1813*cdf0e10cSrcweir     ::rtl::OUString sTempTargetStorName;
1814*cdf0e10cSrcweir     const bool bInplaceStorage = bStorage && ( i_rStorage == mxStorage );
1815*cdf0e10cSrcweir 	if ( bStorage )
1816*cdf0e10cSrcweir 	{
1817*cdf0e10cSrcweir         // Don't write if only empty standard lib exists
1818*cdf0e10cSrcweir         if ( ( nNameCount == 1 ) && ( aNames[0].equalsAscii( "Standard" ) ) )
1819*cdf0e10cSrcweir         {
1820*cdf0e10cSrcweir 		    Any aLibAny = maNameContainer.getByName( aNames[0] );
1821*cdf0e10cSrcweir 		    Reference< XNameAccess > xNameAccess;
1822*cdf0e10cSrcweir 		    aLibAny >>= xNameAccess;
1823*cdf0e10cSrcweir             if ( !xNameAccess->hasElements() )
1824*cdf0e10cSrcweir                 return;
1825*cdf0e10cSrcweir         }
1826*cdf0e10cSrcweir 
1827*cdf0e10cSrcweir         // create the empty target storage
1828*cdf0e10cSrcweir         try
1829*cdf0e10cSrcweir         {
1830*cdf0e10cSrcweir             ::rtl::OUString sTargetLibrariesStoreName;
1831*cdf0e10cSrcweir             if ( bInplaceStorage )
1832*cdf0e10cSrcweir             {
1833*cdf0e10cSrcweir                 // create a temporary target storage
1834*cdf0e10cSrcweir                 const ::rtl::OUStringBuffer aTempTargetNameBase = maLibrariesDir + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_temp_" ) );
1835*cdf0e10cSrcweir                 sal_Int32 index = 0;
1836*cdf0e10cSrcweir                 do
1837*cdf0e10cSrcweir                 {
1838*cdf0e10cSrcweir                     ::rtl::OUStringBuffer aTempTargetName( aTempTargetNameBase );
1839*cdf0e10cSrcweir                     aTempTargetName.append( index++ );
1840*cdf0e10cSrcweir 
1841*cdf0e10cSrcweir                     sTargetLibrariesStoreName = aTempTargetName.makeStringAndClear();
1842*cdf0e10cSrcweir                     if ( !i_rStorage->hasByName( sTargetLibrariesStoreName ) )
1843*cdf0e10cSrcweir                         break;
1844*cdf0e10cSrcweir                 }
1845*cdf0e10cSrcweir                 while ( true );
1846*cdf0e10cSrcweir                 sTempTargetStorName = sTargetLibrariesStoreName;
1847*cdf0e10cSrcweir             }
1848*cdf0e10cSrcweir             else
1849*cdf0e10cSrcweir             {
1850*cdf0e10cSrcweir                 sTargetLibrariesStoreName = maLibrariesDir;
1851*cdf0e10cSrcweir 			    if ( i_rStorage->hasByName( sTargetLibrariesStoreName ) )
1852*cdf0e10cSrcweir 				    i_rStorage->removeElement( sTargetLibrariesStoreName );
1853*cdf0e10cSrcweir             }
1854*cdf0e10cSrcweir 
1855*cdf0e10cSrcweir             xTargetLibrariesStor.set( i_rStorage->openStorageElement( sTargetLibrariesStoreName, embed::ElementModes::READWRITE ), UNO_QUERY_THROW );
1856*cdf0e10cSrcweir 	    }
1857*cdf0e10cSrcweir 	    catch( const uno::Exception& )
1858*cdf0e10cSrcweir 	    {
1859*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
1860*cdf0e10cSrcweir 		    return;
1861*cdf0e10cSrcweir 	    }
1862*cdf0e10cSrcweir 
1863*cdf0e10cSrcweir         // open the source storage which might be used to copy yet-unmodified libraries
1864*cdf0e10cSrcweir 		try
1865*cdf0e10cSrcweir         {
1866*cdf0e10cSrcweir             if ( mxStorage->hasByName( maLibrariesDir ) )
1867*cdf0e10cSrcweir                 xSourceLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, bInplaceStorage ? embed::ElementModes::READWRITE : embed::ElementModes::READ );
1868*cdf0e10cSrcweir             else if ( bInplaceStorage )
1869*cdf0e10cSrcweir                 xSourceLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READWRITE );
1870*cdf0e10cSrcweir 		}
1871*cdf0e10cSrcweir 		catch( const uno::Exception& )
1872*cdf0e10cSrcweir 		{
1873*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
1874*cdf0e10cSrcweir 			return;
1875*cdf0e10cSrcweir         }
1876*cdf0e10cSrcweir 	}
1877*cdf0e10cSrcweir 
1878*cdf0e10cSrcweir 	int iArray = 0;
1879*cdf0e10cSrcweir 	pName = aNames.getConstArray();
1880*cdf0e10cSrcweir 	::xmlscript::LibDescriptor aLibDescriptorForExtensionLibs;
1881*cdf0e10cSrcweir     for( ; pName != pNamesEnd; ++pName )
1882*cdf0e10cSrcweir 	{
1883*cdf0e10cSrcweir         SfxLibrary* pImplLib = getImplLib( *pName );
1884*cdf0e10cSrcweir 		if( pImplLib->mbSharedIndexFile )
1885*cdf0e10cSrcweir 			continue;
1886*cdf0e10cSrcweir 		const bool bExtensionLib = pImplLib->mbExtension;
1887*cdf0e10cSrcweir 		::xmlscript::LibDescriptor& rLib = bExtensionLib ?
1888*cdf0e10cSrcweir 			aLibDescriptorForExtensionLibs : pLibArray->mpLibs[iArray];
1889*cdf0e10cSrcweir 		if( !bExtensionLib )
1890*cdf0e10cSrcweir 			iArray++;
1891*cdf0e10cSrcweir 		rLib.aName = *pName;
1892*cdf0e10cSrcweir 
1893*cdf0e10cSrcweir 		rLib.bLink = pImplLib->mbLink;
1894*cdf0e10cSrcweir 		if( !bStorage || pImplLib->mbLink )
1895*cdf0e10cSrcweir 		{
1896*cdf0e10cSrcweir 			rLib.aStorageURL = ( pImplLib->maUnexpandedStorageURL.getLength() ) ?
1897*cdf0e10cSrcweir 				pImplLib->maUnexpandedStorageURL : pImplLib->maLibInfoFileURL;
1898*cdf0e10cSrcweir 		}
1899*cdf0e10cSrcweir 		rLib.bReadOnly = pImplLib->mbReadOnly;
1900*cdf0e10cSrcweir 		rLib.bPreload = pImplLib->mbPreload;
1901*cdf0e10cSrcweir 		rLib.bPasswordProtected = pImplLib->mbPasswordProtected;
1902*cdf0e10cSrcweir 		rLib.aElementNames = pImplLib->getElementNames();
1903*cdf0e10cSrcweir 
1904*cdf0e10cSrcweir 		if( pImplLib->implIsModified() || bComplete )
1905*cdf0e10cSrcweir 		{
1906*cdf0e10cSrcweir             // Can we simply copy the storage?
1907*cdf0e10cSrcweir             if( !mbOldInfoFormat && !pImplLib->implIsModified() && !mbOasis2OOoFormat && xSourceLibrariesStor.is() )
1908*cdf0e10cSrcweir             {
1909*cdf0e10cSrcweir 				try
1910*cdf0e10cSrcweir                 {
1911*cdf0e10cSrcweir                 	xSourceLibrariesStor->copyElementTo( rLib.aName, xTargetLibrariesStor, rLib.aName );
1912*cdf0e10cSrcweir 				}
1913*cdf0e10cSrcweir                 catch( const uno::Exception& )
1914*cdf0e10cSrcweir 				{
1915*cdf0e10cSrcweir                     DBG_UNHANDLED_EXCEPTION();
1916*cdf0e10cSrcweir                     // TODO: error handling?
1917*cdf0e10cSrcweir 				}
1918*cdf0e10cSrcweir             }
1919*cdf0e10cSrcweir             else
1920*cdf0e10cSrcweir             {
1921*cdf0e10cSrcweir 				uno::Reference< embed::XStorage > xLibraryStor;
1922*cdf0e10cSrcweir 				if( bStorage )
1923*cdf0e10cSrcweir 				{
1924*cdf0e10cSrcweir 					try
1925*cdf0e10cSrcweir                     {
1926*cdf0e10cSrcweir 						xLibraryStor = xTargetLibrariesStor->openStorageElement(
1927*cdf0e10cSrcweir 																		rLib.aName,
1928*cdf0e10cSrcweir 																		embed::ElementModes::READWRITE );
1929*cdf0e10cSrcweir 					}
1930*cdf0e10cSrcweir 					catch( uno::Exception& )
1931*cdf0e10cSrcweir 					{
1932*cdf0e10cSrcweir                     #if OSL_DEBUG_LEVEL > 0
1933*cdf0e10cSrcweir                         Any aError( ::cppu::getCaughtException() );
1934*cdf0e10cSrcweir                         ::rtl::OStringBuffer aMessage;
1935*cdf0e10cSrcweir                         aMessage.append( "couln't create sub storage for library '" );
1936*cdf0e10cSrcweir                         aMessage.append( ::rtl::OUStringToOString( rLib.aName, osl_getThreadTextEncoding() ) );
1937*cdf0e10cSrcweir                         aMessage.append( "'.\n\nException:" );
1938*cdf0e10cSrcweir                         aMessage.append( ::rtl::OUStringToOString( ::comphelper::anyToString( aError ), osl_getThreadTextEncoding() ) );
1939*cdf0e10cSrcweir 					    OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
1940*cdf0e10cSrcweir                     #endif
1941*cdf0e10cSrcweir 						return;
1942*cdf0e10cSrcweir 					}
1943*cdf0e10cSrcweir 				}
1944*cdf0e10cSrcweir 
1945*cdf0e10cSrcweir 				// Maybe lib is not loaded?!
1946*cdf0e10cSrcweir 				if( bComplete )
1947*cdf0e10cSrcweir 					loadLibrary( rLib.aName );
1948*cdf0e10cSrcweir 
1949*cdf0e10cSrcweir     			if( pImplLib->mbPasswordProtected )
1950*cdf0e10cSrcweir 				    implStorePasswordLibrary( pImplLib, rLib.aName, xLibraryStor, uno::Reference< task::XInteractionHandler >() );
1951*cdf0e10cSrcweir                     // TODO: Check return value
1952*cdf0e10cSrcweir                 else
1953*cdf0e10cSrcweir 				    implStoreLibrary( pImplLib, rLib.aName, xLibraryStor );
1954*cdf0e10cSrcweir 
1955*cdf0e10cSrcweir                 implStoreLibraryIndexFile( pImplLib, rLib, xLibraryStor );
1956*cdf0e10cSrcweir 				if( bStorage )
1957*cdf0e10cSrcweir 				{
1958*cdf0e10cSrcweir 					try
1959*cdf0e10cSrcweir                     {
1960*cdf0e10cSrcweir 						uno::Reference< embed::XTransactedObject > xTransact( xLibraryStor, uno::UNO_QUERY_THROW );
1961*cdf0e10cSrcweir 						xTransact->commit();
1962*cdf0e10cSrcweir 					}
1963*cdf0e10cSrcweir 					catch( uno::Exception& )
1964*cdf0e10cSrcweir 					{
1965*cdf0e10cSrcweir                         DBG_UNHANDLED_EXCEPTION();
1966*cdf0e10cSrcweir                         // TODO: error handling
1967*cdf0e10cSrcweir 					}
1968*cdf0e10cSrcweir 				}
1969*cdf0e10cSrcweir             }
1970*cdf0e10cSrcweir 
1971*cdf0e10cSrcweir 			maModifiable.setModified( sal_True );
1972*cdf0e10cSrcweir 			pImplLib->implSetModified( sal_False );
1973*cdf0e10cSrcweir 		}
1974*cdf0e10cSrcweir 
1975*cdf0e10cSrcweir         // For container info ReadOnly refers to mbReadOnlyLink
1976*cdf0e10cSrcweir 		rLib.bReadOnly = pImplLib->mbReadOnlyLink;
1977*cdf0e10cSrcweir 	}
1978*cdf0e10cSrcweir 
1979*cdf0e10cSrcweir     // if we did an in-place save into a storage (i.e. a save into the storage we were already based on),
1980*cdf0e10cSrcweir     // then we need to clean up the temporary storage we used for this
1981*cdf0e10cSrcweir     if ( bInplaceStorage && sTempTargetStorName.getLength() )
1982*cdf0e10cSrcweir     {
1983*cdf0e10cSrcweir         OSL_ENSURE( xSourceLibrariesStor.is(), "SfxLibrariesContainer::storeLibraries_impl: unexpected: we should have a source storage here!" );
1984*cdf0e10cSrcweir         try
1985*cdf0e10cSrcweir         {
1986*cdf0e10cSrcweir             // for this, we first remove everything from the source storage, then copy the complete content
1987*cdf0e10cSrcweir             // from the temporary target storage. From then on, what used to be the "source storage" becomes
1988*cdf0e10cSrcweir             // the "targt storage" for all subsequent operations.
1989*cdf0e10cSrcweir 
1990*cdf0e10cSrcweir             // (We cannot simply remove the storage, denoted by maLibrariesDir, from i_rStorage - there might be
1991*cdf0e10cSrcweir             // open references to it.)
1992*cdf0e10cSrcweir 
1993*cdf0e10cSrcweir             if ( xSourceLibrariesStor.is() )
1994*cdf0e10cSrcweir             {
1995*cdf0e10cSrcweir                 // remove
1996*cdf0e10cSrcweir                 const Sequence< ::rtl::OUString > aRemoveNames( xSourceLibrariesStor->getElementNames() );
1997*cdf0e10cSrcweir                 for (   const ::rtl::OUString* pRemoveName = aRemoveNames.getConstArray();
1998*cdf0e10cSrcweir                         pRemoveName != aRemoveNames.getConstArray() + aRemoveNames.getLength();
1999*cdf0e10cSrcweir                             ++pRemoveName
2000*cdf0e10cSrcweir                     )
2001*cdf0e10cSrcweir                 {
2002*cdf0e10cSrcweir                     xSourceLibrariesStor->removeElement( *pRemoveName );
2003*cdf0e10cSrcweir                 }
2004*cdf0e10cSrcweir 
2005*cdf0e10cSrcweir                 // copy
2006*cdf0e10cSrcweir                 const Sequence< ::rtl::OUString > aCopyNames( xTargetLibrariesStor->getElementNames() );
2007*cdf0e10cSrcweir                 for (   const ::rtl::OUString* pCopyName = aCopyNames.getConstArray();
2008*cdf0e10cSrcweir                         pCopyName != aCopyNames.getConstArray() + aCopyNames.getLength();
2009*cdf0e10cSrcweir                         ++pCopyName
2010*cdf0e10cSrcweir                     )
2011*cdf0e10cSrcweir                 {
2012*cdf0e10cSrcweir                 	xTargetLibrariesStor->copyElementTo( *pCopyName, xSourceLibrariesStor, *pCopyName );
2013*cdf0e10cSrcweir                 }
2014*cdf0e10cSrcweir             }
2015*cdf0e10cSrcweir 
2016*cdf0e10cSrcweir             // close and remove temp target
2017*cdf0e10cSrcweir             xTargetLibrariesStor->dispose();
2018*cdf0e10cSrcweir             i_rStorage->removeElement( sTempTargetStorName );
2019*cdf0e10cSrcweir             xTargetLibrariesStor.clear();
2020*cdf0e10cSrcweir             sTempTargetStorName = ::rtl::OUString();
2021*cdf0e10cSrcweir 
2022*cdf0e10cSrcweir             // adjust target
2023*cdf0e10cSrcweir             xTargetLibrariesStor = xSourceLibrariesStor;
2024*cdf0e10cSrcweir             xSourceLibrariesStor.clear();
2025*cdf0e10cSrcweir         }
2026*cdf0e10cSrcweir         catch( const Exception& )
2027*cdf0e10cSrcweir         {
2028*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
2029*cdf0e10cSrcweir         }
2030*cdf0e10cSrcweir     }
2031*cdf0e10cSrcweir 
2032*cdf0e10cSrcweir 	if( !mbOldInfoFormat && !maModifiable.isModified() )
2033*cdf0e10cSrcweir 		return;
2034*cdf0e10cSrcweir 	maModifiable.setModified( sal_False );
2035*cdf0e10cSrcweir     mbOldInfoFormat = sal_False;
2036*cdf0e10cSrcweir 
2037*cdf0e10cSrcweir 	// Write library container info
2038*cdf0e10cSrcweir 	// Create sax writer
2039*cdf0e10cSrcweir 	Reference< XExtendedDocumentHandler > xHandler(
2040*cdf0e10cSrcweir 		mxMSF->createInstance(
2041*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") ) ), UNO_QUERY );
2042*cdf0e10cSrcweir 	if( !xHandler.is() )
2043*cdf0e10cSrcweir 	{
2044*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create sax-writer component\n" );
2045*cdf0e10cSrcweir 		return;
2046*cdf0e10cSrcweir 	}
2047*cdf0e10cSrcweir 
2048*cdf0e10cSrcweir 	// Write info file
2049*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xOut;
2050*cdf0e10cSrcweir 	uno::Reference< io::XStream > xInfoStream;
2051*cdf0e10cSrcweir 	if( bStorage )
2052*cdf0e10cSrcweir 	{
2053*cdf0e10cSrcweir 		OUString aStreamName( maInfoFileName );
2054*cdf0e10cSrcweir 		aStreamName += String( RTL_CONSTASCII_USTRINGPARAM("-lc.xml") );
2055*cdf0e10cSrcweir 
2056*cdf0e10cSrcweir 		try {
2057*cdf0e10cSrcweir 			xInfoStream = xTargetLibrariesStor->openStreamElement( aStreamName, embed::ElementModes::READWRITE );
2058*cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xProps( xInfoStream, uno::UNO_QUERY );
2059*cdf0e10cSrcweir 			OSL_ENSURE ( xProps.is(), "The stream must implement XPropertySet!\n" );
2060*cdf0e10cSrcweir 			if ( !xProps.is() )
2061*cdf0e10cSrcweir 				throw uno::RuntimeException();
2062*cdf0e10cSrcweir 
2063*cdf0e10cSrcweir 			String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
2064*cdf0e10cSrcweir 			OUString aMime( RTL_CONSTASCII_USTRINGPARAM("text/xml") );
2065*cdf0e10cSrcweir 			xProps->setPropertyValue( aPropName, uno::makeAny( aMime ) );
2066*cdf0e10cSrcweir 
2067*cdf0e10cSrcweir             // #87671 Allow encryption
2068*cdf0e10cSrcweir 			aPropName = String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("UseCommonStoragePasswordEncryption") );
2069*cdf0e10cSrcweir 			xProps->setPropertyValue( aPropName, uno::makeAny( sal_True ) );
2070*cdf0e10cSrcweir 
2071*cdf0e10cSrcweir 			xOut = xInfoStream->getOutputStream();
2072*cdf0e10cSrcweir 		}
2073*cdf0e10cSrcweir 		catch( uno::Exception& )
2074*cdf0e10cSrcweir 		{
2075*cdf0e10cSrcweir 			sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
2076*cdf0e10cSrcweir 			ErrorHandler::HandleError( nErrorCode );
2077*cdf0e10cSrcweir 		}
2078*cdf0e10cSrcweir 	}
2079*cdf0e10cSrcweir 	else
2080*cdf0e10cSrcweir 	{
2081*cdf0e10cSrcweir 		// Create Output stream
2082*cdf0e10cSrcweir 		INetURLObject aLibInfoInetObj( String(maLibraryPath).GetToken(1) );
2083*cdf0e10cSrcweir 		aLibInfoInetObj.insertName( maInfoFileName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
2084*cdf0e10cSrcweir 		aLibInfoInetObj.setExtension( OUString( RTL_CONSTASCII_USTRINGPARAM("xlc") ) );
2085*cdf0e10cSrcweir 		String aLibInfoPath( aLibInfoInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
2086*cdf0e10cSrcweir 
2087*cdf0e10cSrcweir 		try
2088*cdf0e10cSrcweir 		{
2089*cdf0e10cSrcweir 		    if( mxSFI->exists( aLibInfoPath ) )
2090*cdf0e10cSrcweir 			    mxSFI->kill( aLibInfoPath );
2091*cdf0e10cSrcweir 		    xOut = mxSFI->openFileWrite( aLibInfoPath );
2092*cdf0e10cSrcweir         }
2093*cdf0e10cSrcweir         catch( Exception& )
2094*cdf0e10cSrcweir         {
2095*cdf0e10cSrcweir             xOut.clear();
2096*cdf0e10cSrcweir 			SfxErrorContext aEc( ERRCTX_SFX_SAVEDOC, aLibInfoPath );
2097*cdf0e10cSrcweir             sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
2098*cdf0e10cSrcweir             ErrorHandler::HandleError( nErrorCode );
2099*cdf0e10cSrcweir         }
2100*cdf0e10cSrcweir 
2101*cdf0e10cSrcweir 	}
2102*cdf0e10cSrcweir 	if( !xOut.is() )
2103*cdf0e10cSrcweir 	{
2104*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't open output stream\n" );
2105*cdf0e10cSrcweir 		return;
2106*cdf0e10cSrcweir 	}
2107*cdf0e10cSrcweir 
2108*cdf0e10cSrcweir 	Reference< XActiveDataSource > xSource( xHandler, UNO_QUERY );
2109*cdf0e10cSrcweir 	xSource->setOutputStream( xOut );
2110*cdf0e10cSrcweir 
2111*cdf0e10cSrcweir     try
2112*cdf0e10cSrcweir 	{
2113*cdf0e10cSrcweir 		xmlscript::exportLibraryContainer( xHandler, pLibArray );
2114*cdf0e10cSrcweir 		if ( bStorage )
2115*cdf0e10cSrcweir 		{
2116*cdf0e10cSrcweir             uno::Reference< embed::XTransactedObject > xTransact( xTargetLibrariesStor, uno::UNO_QUERY );
2117*cdf0e10cSrcweir             OSL_ENSURE( xTransact.is(), "The storage must implement XTransactedObject!\n" );
2118*cdf0e10cSrcweir             if ( !xTransact.is() )
2119*cdf0e10cSrcweir                 throw uno::RuntimeException();
2120*cdf0e10cSrcweir 
2121*cdf0e10cSrcweir             xTransact->commit();
2122*cdf0e10cSrcweir         }
2123*cdf0e10cSrcweir     }
2124*cdf0e10cSrcweir     catch( uno::Exception& )
2125*cdf0e10cSrcweir     {
2126*cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Problem during storing of libraries!\n" );
2127*cdf0e10cSrcweir         sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
2128*cdf0e10cSrcweir         ErrorHandler::HandleError( nErrorCode );
2129*cdf0e10cSrcweir     }
2130*cdf0e10cSrcweir 
2131*cdf0e10cSrcweir 	delete pLibArray;
2132*cdf0e10cSrcweir }
2133*cdf0e10cSrcweir 
2134*cdf0e10cSrcweir 
2135*cdf0e10cSrcweir // Methods XElementAccess
2136*cdf0e10cSrcweir Type SAL_CALL SfxLibraryContainer::getElementType()
2137*cdf0e10cSrcweir 	throw(RuntimeException)
2138*cdf0e10cSrcweir {
2139*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2140*cdf0e10cSrcweir 	return maNameContainer.getElementType();
2141*cdf0e10cSrcweir }
2142*cdf0e10cSrcweir 
2143*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::hasElements()
2144*cdf0e10cSrcweir 	throw(RuntimeException)
2145*cdf0e10cSrcweir {
2146*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2147*cdf0e10cSrcweir 	sal_Bool bRet = maNameContainer.hasElements();
2148*cdf0e10cSrcweir 	return bRet;
2149*cdf0e10cSrcweir }
2150*cdf0e10cSrcweir 
2151*cdf0e10cSrcweir // Methods XNameAccess
2152*cdf0e10cSrcweir Any SfxLibraryContainer::getByName( const OUString& aName )
2153*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2154*cdf0e10cSrcweir {
2155*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2156*cdf0e10cSrcweir 	Any aRetAny = maNameContainer.getByName( aName ) ;
2157*cdf0e10cSrcweir 	return aRetAny;
2158*cdf0e10cSrcweir }
2159*cdf0e10cSrcweir 
2160*cdf0e10cSrcweir Sequence< OUString > SfxLibraryContainer::getElementNames()
2161*cdf0e10cSrcweir 	throw(RuntimeException)
2162*cdf0e10cSrcweir {
2163*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2164*cdf0e10cSrcweir 	return maNameContainer.getElementNames();
2165*cdf0e10cSrcweir }
2166*cdf0e10cSrcweir 
2167*cdf0e10cSrcweir sal_Bool SfxLibraryContainer::hasByName( const OUString& aName )
2168*cdf0e10cSrcweir 	throw(RuntimeException)
2169*cdf0e10cSrcweir {
2170*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2171*cdf0e10cSrcweir 	return maNameContainer.hasByName( aName ) ;
2172*cdf0e10cSrcweir }
2173*cdf0e10cSrcweir 
2174*cdf0e10cSrcweir // Methods XLibraryContainer
2175*cdf0e10cSrcweir Reference< XNameContainer > SAL_CALL SfxLibraryContainer::createLibrary( const OUString& Name )
2176*cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, RuntimeException)
2177*cdf0e10cSrcweir {
2178*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2179*cdf0e10cSrcweir 	SfxLibrary* pNewLib = implCreateLibrary( Name );
2180*cdf0e10cSrcweir     pNewLib->maLibElementFileExtension = maLibElementFileExtension;
2181*cdf0e10cSrcweir 
2182*cdf0e10cSrcweir 	createVariableURL( pNewLib->maUnexpandedStorageURL, Name, maInfoFileName, true );
2183*cdf0e10cSrcweir 
2184*cdf0e10cSrcweir 	Reference< XNameAccess > xNameAccess = static_cast< XNameAccess* >( pNewLib );
2185*cdf0e10cSrcweir 	Any aElement;
2186*cdf0e10cSrcweir 	aElement <<= xNameAccess;
2187*cdf0e10cSrcweir 	maNameContainer.insertByName( Name, aElement );
2188*cdf0e10cSrcweir 	maModifiable.setModified( sal_True );
2189*cdf0e10cSrcweir     Reference< XNameContainer > xRet( xNameAccess, UNO_QUERY );
2190*cdf0e10cSrcweir 	return xRet;
2191*cdf0e10cSrcweir }
2192*cdf0e10cSrcweir 
2193*cdf0e10cSrcweir Reference< XNameAccess > SAL_CALL SfxLibraryContainer::createLibraryLink
2194*cdf0e10cSrcweir 	( const OUString& Name, const OUString& StorageURL, sal_Bool ReadOnly )
2195*cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, RuntimeException)
2196*cdf0e10cSrcweir {
2197*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2198*cdf0e10cSrcweir     // TODO: Check other reasons to force ReadOnly status
2199*cdf0e10cSrcweir 	//if( !ReadOnly )
2200*cdf0e10cSrcweir 	//{
2201*cdf0e10cSrcweir 	//}
2202*cdf0e10cSrcweir 
2203*cdf0e10cSrcweir     OUString aLibInfoFileURL;
2204*cdf0e10cSrcweir     OUString aLibDirURL;
2205*cdf0e10cSrcweir     OUString aUnexpandedStorageURL;
2206*cdf0e10cSrcweir     checkStorageURL( StorageURL, aLibInfoFileURL, aLibDirURL, aUnexpandedStorageURL );
2207*cdf0e10cSrcweir 
2208*cdf0e10cSrcweir 
2209*cdf0e10cSrcweir 	SfxLibrary* pNewLib = implCreateLibraryLink( Name, aLibInfoFileURL, aLibDirURL, ReadOnly );
2210*cdf0e10cSrcweir     pNewLib->maLibElementFileExtension = maLibElementFileExtension;
2211*cdf0e10cSrcweir     pNewLib->maUnexpandedStorageURL = aUnexpandedStorageURL;
2212*cdf0e10cSrcweir     pNewLib->maOrignialStorageURL = StorageURL;
2213*cdf0e10cSrcweir 
2214*cdf0e10cSrcweir     OUString aInitFileName;
2215*cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xDummyStor;
2216*cdf0e10cSrcweir     ::xmlscript::LibDescriptor aLibDesc;
2217*cdf0e10cSrcweir     /*sal_Bool bReadIndexFile = */implLoadLibraryIndexFile( pNewLib, aLibDesc, xDummyStor, aInitFileName );
2218*cdf0e10cSrcweir     implImportLibDescriptor( pNewLib, aLibDesc );
2219*cdf0e10cSrcweir 
2220*cdf0e10cSrcweir 	Reference< XNameAccess > xRet = static_cast< XNameAccess* >( pNewLib );
2221*cdf0e10cSrcweir 	Any aElement;
2222*cdf0e10cSrcweir 	aElement <<= xRet;
2223*cdf0e10cSrcweir 	maNameContainer.insertByName( Name, aElement );
2224*cdf0e10cSrcweir 	maModifiable.setModified( sal_True );
2225*cdf0e10cSrcweir 
2226*cdf0e10cSrcweir 	OUString aUserSearchStr   = OUString::createFromAscii( "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE" );
2227*cdf0e10cSrcweir 	OUString aSharedSearchStr = OUString::createFromAscii( "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE" );
2228*cdf0e10cSrcweir 	OUString aBundledSearchStr = OUString::createFromAscii( "vnd.sun.star.expand:$BUNDLED_EXTENSIONS" );
2229*cdf0e10cSrcweir 	if( StorageURL.indexOf( aUserSearchStr ) != -1 )
2230*cdf0e10cSrcweir 	{
2231*cdf0e10cSrcweir 	    pNewLib->mbExtension = sal_True;
2232*cdf0e10cSrcweir 	}
2233*cdf0e10cSrcweir 	else if( StorageURL.indexOf( aSharedSearchStr ) != -1 || StorageURL.indexOf( aBundledSearchStr ) != -1 )
2234*cdf0e10cSrcweir 	{
2235*cdf0e10cSrcweir 	    pNewLib->mbExtension = sal_True;
2236*cdf0e10cSrcweir 	    pNewLib->mbReadOnly = sal_True;
2237*cdf0e10cSrcweir 	}
2238*cdf0e10cSrcweir 
2239*cdf0e10cSrcweir 	return xRet;
2240*cdf0e10cSrcweir }
2241*cdf0e10cSrcweir 
2242*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::removeLibrary( const OUString& Name )
2243*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2244*cdf0e10cSrcweir {
2245*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2246*cdf0e10cSrcweir     // Get and hold library before removing
2247*cdf0e10cSrcweir 	Any aLibAny = maNameContainer.getByName( Name ) ;
2248*cdf0e10cSrcweir 	Reference< XNameAccess > xNameAccess;
2249*cdf0e10cSrcweir 	aLibAny >>= xNameAccess;
2250*cdf0e10cSrcweir 	SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
2251*cdf0e10cSrcweir 	if( pImplLib->mbReadOnly && !pImplLib->mbLink )
2252*cdf0e10cSrcweir 		throw IllegalArgumentException();
2253*cdf0e10cSrcweir 
2254*cdf0e10cSrcweir     // Remove from container
2255*cdf0e10cSrcweir 	maNameContainer.removeByName( Name );
2256*cdf0e10cSrcweir 	maModifiable.setModified( sal_True );
2257*cdf0e10cSrcweir 
2258*cdf0e10cSrcweir     // Delete library files, but not for linked libraries
2259*cdf0e10cSrcweir     if( !pImplLib->mbLink )
2260*cdf0e10cSrcweir     {
2261*cdf0e10cSrcweir 	    if( mxStorage.is() )
2262*cdf0e10cSrcweir             return;
2263*cdf0e10cSrcweir 	    if( xNameAccess->hasElements() )
2264*cdf0e10cSrcweir 	    {
2265*cdf0e10cSrcweir 		    Sequence< OUString > aNames = pImplLib->getElementNames();
2266*cdf0e10cSrcweir 		    sal_Int32 nNameCount = aNames.getLength();
2267*cdf0e10cSrcweir 		    const OUString* pNames = aNames.getConstArray();
2268*cdf0e10cSrcweir 		    for( sal_Int32 i = 0 ; i < nNameCount ; ++i, ++pNames )
2269*cdf0e10cSrcweir 		    {
2270*cdf0e10cSrcweir                 pImplLib->removeElementWithoutChecks( *pNames, SfxLibrary::LibraryContainerAccess() );
2271*cdf0e10cSrcweir 		    }
2272*cdf0e10cSrcweir 	    }
2273*cdf0e10cSrcweir 
2274*cdf0e10cSrcweir         // Delete index file
2275*cdf0e10cSrcweir         createAppLibraryFolder( pImplLib, Name );
2276*cdf0e10cSrcweir         String aLibInfoPath = pImplLib->maLibInfoFileURL;
2277*cdf0e10cSrcweir 		try
2278*cdf0e10cSrcweir 		{
2279*cdf0e10cSrcweir 		    if( mxSFI->exists( aLibInfoPath ) )
2280*cdf0e10cSrcweir 			    mxSFI->kill( aLibInfoPath );
2281*cdf0e10cSrcweir         }
2282*cdf0e10cSrcweir         catch( Exception& ) {}
2283*cdf0e10cSrcweir 
2284*cdf0e10cSrcweir         // Delete folder if empty
2285*cdf0e10cSrcweir 	    INetURLObject aInetObj( String(maLibraryPath).GetToken(1) );
2286*cdf0e10cSrcweir 	    aInetObj.insertName( Name, sal_True, INetURLObject::LAST_SEGMENT,
2287*cdf0e10cSrcweir 		    sal_True, INetURLObject::ENCODE_ALL );
2288*cdf0e10cSrcweir 	    OUString aLibDirPath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
2289*cdf0e10cSrcweir 
2290*cdf0e10cSrcweir 	    try
2291*cdf0e10cSrcweir 	    {
2292*cdf0e10cSrcweir 	        if( mxSFI->isFolder( aLibDirPath ) )
2293*cdf0e10cSrcweir 	        {
2294*cdf0e10cSrcweir                 Sequence< OUString > aContentSeq = mxSFI->getFolderContents( aLibDirPath, true );
2295*cdf0e10cSrcweir     		    sal_Int32 nCount = aContentSeq.getLength();
2296*cdf0e10cSrcweir 	            if( !nCount )
2297*cdf0e10cSrcweir 		            mxSFI->kill( aLibDirPath );
2298*cdf0e10cSrcweir 	        }
2299*cdf0e10cSrcweir         }
2300*cdf0e10cSrcweir         catch( Exception& )
2301*cdf0e10cSrcweir         {
2302*cdf0e10cSrcweir         }
2303*cdf0e10cSrcweir     }
2304*cdf0e10cSrcweir }
2305*cdf0e10cSrcweir 
2306*cdf0e10cSrcweir sal_Bool SAL_CALL SfxLibraryContainer::isLibraryLoaded( const OUString& Name )
2307*cdf0e10cSrcweir 	throw(NoSuchElementException, RuntimeException)
2308*cdf0e10cSrcweir {
2309*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2310*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2311*cdf0e10cSrcweir 	sal_Bool bRet = pImplLib->mbLoaded;
2312*cdf0e10cSrcweir 	return bRet;
2313*cdf0e10cSrcweir }
2314*cdf0e10cSrcweir 
2315*cdf0e10cSrcweir 
2316*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::loadLibrary( const OUString& Name )
2317*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
2318*cdf0e10cSrcweir {
2319*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2320*cdf0e10cSrcweir 	Any aLibAny = maNameContainer.getByName( Name ) ;
2321*cdf0e10cSrcweir 	Reference< XNameAccess > xNameAccess;
2322*cdf0e10cSrcweir 	aLibAny >>= xNameAccess;
2323*cdf0e10cSrcweir 	SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
2324*cdf0e10cSrcweir 
2325*cdf0e10cSrcweir     sal_Bool bLoaded = pImplLib->mbLoaded;
2326*cdf0e10cSrcweir 	pImplLib->mbLoaded = sal_True;
2327*cdf0e10cSrcweir 	if( !bLoaded && xNameAccess->hasElements() )
2328*cdf0e10cSrcweir 	{
2329*cdf0e10cSrcweir         if( pImplLib->mbPasswordProtected )
2330*cdf0e10cSrcweir         {
2331*cdf0e10cSrcweir             implLoadPasswordLibrary( pImplLib, Name );
2332*cdf0e10cSrcweir             return;
2333*cdf0e10cSrcweir         }
2334*cdf0e10cSrcweir 
2335*cdf0e10cSrcweir 		sal_Bool bLink = pImplLib->mbLink;
2336*cdf0e10cSrcweir 		sal_Bool bStorage = mxStorage.is() && !bLink;
2337*cdf0e10cSrcweir 
2338*cdf0e10cSrcweir 		uno::Reference< embed::XStorage > xLibrariesStor;
2339*cdf0e10cSrcweir 		uno::Reference< embed::XStorage > xLibraryStor;
2340*cdf0e10cSrcweir 		if( bStorage )
2341*cdf0e10cSrcweir 		{
2342*cdf0e10cSrcweir 			try {
2343*cdf0e10cSrcweir 				xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
2344*cdf0e10cSrcweir 				OSL_ENSURE( xLibrariesStor.is(), "The method must either throw exception or return a storage!\n" );
2345*cdf0e10cSrcweir 				if ( !xLibrariesStor.is() )
2346*cdf0e10cSrcweir 					throw uno::RuntimeException();
2347*cdf0e10cSrcweir 
2348*cdf0e10cSrcweir 				xLibraryStor = xLibrariesStor->openStorageElement( Name, embed::ElementModes::READ );
2349*cdf0e10cSrcweir 				OSL_ENSURE( xLibraryStor.is(), "The method must either throw exception or return a storage!\n" );
2350*cdf0e10cSrcweir 				if ( !xLibrariesStor.is() )
2351*cdf0e10cSrcweir 					throw uno::RuntimeException();
2352*cdf0e10cSrcweir 			}
2353*cdf0e10cSrcweir 			catch( uno::Exception& )
2354*cdf0e10cSrcweir 			{
2355*cdf0e10cSrcweir             #if OSL_DEBUG_LEVEL > 0
2356*cdf0e10cSrcweir                 Any aError( ::cppu::getCaughtException() );
2357*cdf0e10cSrcweir                 ::rtl::OStringBuffer aMessage;
2358*cdf0e10cSrcweir                 aMessage.append( "couln't open sub storage for library '" );
2359*cdf0e10cSrcweir                 aMessage.append( ::rtl::OUStringToOString( Name, osl_getThreadTextEncoding() ) );
2360*cdf0e10cSrcweir                 aMessage.append( "'.\n\nException:" );
2361*cdf0e10cSrcweir                 aMessage.append( ::rtl::OUStringToOString( ::comphelper::anyToString( aError ), osl_getThreadTextEncoding() ) );
2362*cdf0e10cSrcweir 			    OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
2363*cdf0e10cSrcweir             #endif
2364*cdf0e10cSrcweir 				return;
2365*cdf0e10cSrcweir 			}
2366*cdf0e10cSrcweir 		}
2367*cdf0e10cSrcweir 
2368*cdf0e10cSrcweir 		Sequence< OUString > aNames = pImplLib->getElementNames();
2369*cdf0e10cSrcweir 		sal_Int32 nNameCount = aNames.getLength();
2370*cdf0e10cSrcweir 		const OUString* pNames = aNames.getConstArray();
2371*cdf0e10cSrcweir 		for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
2372*cdf0e10cSrcweir 		{
2373*cdf0e10cSrcweir 			OUString aElementName = pNames[ i ];
2374*cdf0e10cSrcweir 
2375*cdf0e10cSrcweir 			OUString aFile;
2376*cdf0e10cSrcweir 			uno::Reference< io::XInputStream > xInStream;
2377*cdf0e10cSrcweir 
2378*cdf0e10cSrcweir 			if( bStorage )
2379*cdf0e10cSrcweir 			{
2380*cdf0e10cSrcweir 				uno::Reference< io::XStream > xElementStream;
2381*cdf0e10cSrcweir 
2382*cdf0e10cSrcweir 				aFile = aElementName;
2383*cdf0e10cSrcweir 				aFile += String( RTL_CONSTASCII_USTRINGPARAM(".xml") );
2384*cdf0e10cSrcweir 
2385*cdf0e10cSrcweir 				try {
2386*cdf0e10cSrcweir 					xElementStream = xLibraryStor->openStreamElement( aFile, embed::ElementModes::READ );
2387*cdf0e10cSrcweir 				} catch( uno::Exception& )
2388*cdf0e10cSrcweir 				{}
2389*cdf0e10cSrcweir 
2390*cdf0e10cSrcweir 				if( !xElementStream.is() )
2391*cdf0e10cSrcweir 				{
2392*cdf0e10cSrcweir 					// Check for EA2 document version with wrong extensions
2393*cdf0e10cSrcweir 					aFile = aElementName;
2394*cdf0e10cSrcweir 					aFile += String( RTL_CONSTASCII_USTRINGPARAM(".") );
2395*cdf0e10cSrcweir 					aFile += maLibElementFileExtension;
2396*cdf0e10cSrcweir 					try {
2397*cdf0e10cSrcweir 						xElementStream = xLibraryStor->openStreamElement( aFile, embed::ElementModes::READ );
2398*cdf0e10cSrcweir 					} catch( uno::Exception& )
2399*cdf0e10cSrcweir 					{}
2400*cdf0e10cSrcweir 				}
2401*cdf0e10cSrcweir 
2402*cdf0e10cSrcweir 				if ( xElementStream.is() )
2403*cdf0e10cSrcweir 					xInStream = xElementStream->getInputStream();
2404*cdf0e10cSrcweir 
2405*cdf0e10cSrcweir 				if ( !xInStream.is() )
2406*cdf0e10cSrcweir 				{
2407*cdf0e10cSrcweir                 #if OSL_DEBUG_LEVEL > 0
2408*cdf0e10cSrcweir                     ::rtl::OStringBuffer aMessage;
2409*cdf0e10cSrcweir                     aMessage.append( "couln't open library element stream - attempted to open library '" );
2410*cdf0e10cSrcweir                     aMessage.append( ::rtl::OUStringToOString( Name, osl_getThreadTextEncoding() ) );
2411*cdf0e10cSrcweir                     aMessage.append( "'." );
2412*cdf0e10cSrcweir 					OSL_ENSURE( false, aMessage.makeStringAndClear().getStr() );
2413*cdf0e10cSrcweir                 #endif
2414*cdf0e10cSrcweir 					return;
2415*cdf0e10cSrcweir 				}
2416*cdf0e10cSrcweir 			}
2417*cdf0e10cSrcweir 			else
2418*cdf0e10cSrcweir 			{
2419*cdf0e10cSrcweir 		        String aLibDirPath = pImplLib->maStorageURL;
2420*cdf0e10cSrcweir 				INetURLObject aElementInetObj( aLibDirPath );
2421*cdf0e10cSrcweir 				aElementInetObj.insertName( aElementName, sal_False,
2422*cdf0e10cSrcweir 					INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
2423*cdf0e10cSrcweir 				aElementInetObj.setExtension( maLibElementFileExtension );
2424*cdf0e10cSrcweir 				aFile = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
2425*cdf0e10cSrcweir 			}
2426*cdf0e10cSrcweir 
2427*cdf0e10cSrcweir 			Reference< XNameContainer > xLib( pImplLib );
2428*cdf0e10cSrcweir 			Any aAny = importLibraryElement( xLib, aElementName,
2429*cdf0e10cSrcweir 					   						 aFile, xInStream );
2430*cdf0e10cSrcweir 			if( pImplLib->hasByName( aElementName ) )
2431*cdf0e10cSrcweir             {
2432*cdf0e10cSrcweir                 if( aAny.hasValue() )
2433*cdf0e10cSrcweir 				    pImplLib->maNameContainer.replaceByName( aElementName, aAny );
2434*cdf0e10cSrcweir             }
2435*cdf0e10cSrcweir 			else
2436*cdf0e10cSrcweir             {
2437*cdf0e10cSrcweir 				pImplLib->maNameContainer.insertByName( aElementName, aAny );
2438*cdf0e10cSrcweir             }
2439*cdf0e10cSrcweir 		}
2440*cdf0e10cSrcweir 
2441*cdf0e10cSrcweir         pImplLib->implSetModified( sal_False );
2442*cdf0e10cSrcweir 	}
2443*cdf0e10cSrcweir }
2444*cdf0e10cSrcweir 
2445*cdf0e10cSrcweir // Methods XLibraryContainer2
2446*cdf0e10cSrcweir sal_Bool SAL_CALL SfxLibraryContainer::isLibraryLink( const OUString& Name )
2447*cdf0e10cSrcweir     throw (NoSuchElementException, RuntimeException)
2448*cdf0e10cSrcweir {
2449*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2450*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2451*cdf0e10cSrcweir 	sal_Bool bRet = pImplLib->mbLink;
2452*cdf0e10cSrcweir 	return bRet;
2453*cdf0e10cSrcweir }
2454*cdf0e10cSrcweir 
2455*cdf0e10cSrcweir OUString SAL_CALL SfxLibraryContainer::getLibraryLinkURL( const OUString& Name )
2456*cdf0e10cSrcweir     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
2457*cdf0e10cSrcweir {
2458*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2459*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2460*cdf0e10cSrcweir 	sal_Bool bLink = pImplLib->mbLink;
2461*cdf0e10cSrcweir 	if( !bLink )
2462*cdf0e10cSrcweir 		throw IllegalArgumentException();
2463*cdf0e10cSrcweir     OUString aRetStr = pImplLib->maLibInfoFileURL;
2464*cdf0e10cSrcweir     return aRetStr;
2465*cdf0e10cSrcweir }
2466*cdf0e10cSrcweir 
2467*cdf0e10cSrcweir sal_Bool SAL_CALL SfxLibraryContainer::isLibraryReadOnly( const OUString& Name )
2468*cdf0e10cSrcweir     throw (NoSuchElementException, RuntimeException)
2469*cdf0e10cSrcweir {
2470*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2471*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2472*cdf0e10cSrcweir 	sal_Bool bRet = pImplLib->mbReadOnly || (pImplLib->mbLink && pImplLib->mbReadOnlyLink);
2473*cdf0e10cSrcweir 	return bRet;
2474*cdf0e10cSrcweir }
2475*cdf0e10cSrcweir 
2476*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::setLibraryReadOnly( const OUString& Name, sal_Bool bReadOnly )
2477*cdf0e10cSrcweir     throw (NoSuchElementException, RuntimeException)
2478*cdf0e10cSrcweir {
2479*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2480*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2481*cdf0e10cSrcweir     if( pImplLib->mbLink )
2482*cdf0e10cSrcweir     {
2483*cdf0e10cSrcweir         if( pImplLib->mbReadOnlyLink != bReadOnly )
2484*cdf0e10cSrcweir         {
2485*cdf0e10cSrcweir             pImplLib->mbReadOnlyLink = bReadOnly;
2486*cdf0e10cSrcweir             pImplLib->implSetModified( sal_True );
2487*cdf0e10cSrcweir             maModifiable.setModified( sal_True );
2488*cdf0e10cSrcweir         }
2489*cdf0e10cSrcweir     }
2490*cdf0e10cSrcweir     else
2491*cdf0e10cSrcweir     {
2492*cdf0e10cSrcweir         if( pImplLib->mbReadOnly != bReadOnly )
2493*cdf0e10cSrcweir         {
2494*cdf0e10cSrcweir 	        pImplLib->mbReadOnly = bReadOnly;
2495*cdf0e10cSrcweir             pImplLib->implSetModified( sal_True );
2496*cdf0e10cSrcweir         }
2497*cdf0e10cSrcweir     }
2498*cdf0e10cSrcweir }
2499*cdf0e10cSrcweir 
2500*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::renameLibrary( const OUString& Name, const OUString& NewName )
2501*cdf0e10cSrcweir     throw (NoSuchElementException, ElementExistException, RuntimeException)
2502*cdf0e10cSrcweir {
2503*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2504*cdf0e10cSrcweir 	if( maNameContainer.hasByName( NewName ) )
2505*cdf0e10cSrcweir 		throw ElementExistException();
2506*cdf0e10cSrcweir 
2507*cdf0e10cSrcweir     // Get and hold library before removing
2508*cdf0e10cSrcweir 	Any aLibAny = maNameContainer.getByName( Name ) ;
2509*cdf0e10cSrcweir 
2510*cdf0e10cSrcweir 	// #i24094 Maybe lib is not loaded!
2511*cdf0e10cSrcweir 	Reference< XNameAccess > xNameAccess;
2512*cdf0e10cSrcweir 	aLibAny >>= xNameAccess;
2513*cdf0e10cSrcweir 	SfxLibrary* pImplLib = static_cast< SfxLibrary* >( xNameAccess.get() );
2514*cdf0e10cSrcweir 	if( pImplLib->mbPasswordProtected && !pImplLib->mbPasswordVerified )
2515*cdf0e10cSrcweir 		return;		// Lib with unverified password cannot be renamed
2516*cdf0e10cSrcweir 	loadLibrary( Name );
2517*cdf0e10cSrcweir 
2518*cdf0e10cSrcweir     // Remove from container
2519*cdf0e10cSrcweir 	maNameContainer.removeByName( Name );
2520*cdf0e10cSrcweir 	maModifiable.setModified( sal_True );
2521*cdf0e10cSrcweir 
2522*cdf0e10cSrcweir     // Rename library folder, but not for linked libraries
2523*cdf0e10cSrcweir     bool bMovedSuccessful = true;
2524*cdf0e10cSrcweir 
2525*cdf0e10cSrcweir     // Rename files
2526*cdf0e10cSrcweir     sal_Bool bStorage = mxStorage.is();
2527*cdf0e10cSrcweir     if( !bStorage && !pImplLib->mbLink )
2528*cdf0e10cSrcweir     {
2529*cdf0e10cSrcweir         bMovedSuccessful = false;
2530*cdf0e10cSrcweir 
2531*cdf0e10cSrcweir 	    OUString aLibDirPath = pImplLib->maStorageURL;
2532*cdf0e10cSrcweir 
2533*cdf0e10cSrcweir 	    INetURLObject aDestInetObj( String(maLibraryPath).GetToken(1) );
2534*cdf0e10cSrcweir 	    aDestInetObj.insertName( NewName, sal_True, INetURLObject::LAST_SEGMENT,
2535*cdf0e10cSrcweir 		    sal_True, INetURLObject::ENCODE_ALL );
2536*cdf0e10cSrcweir 	    OUString aDestDirPath = aDestInetObj.GetMainURL( INetURLObject::NO_DECODE );
2537*cdf0e10cSrcweir 
2538*cdf0e10cSrcweir         // Store new URL
2539*cdf0e10cSrcweir         OUString aLibInfoFileURL = pImplLib->maLibInfoFileURL;
2540*cdf0e10cSrcweir         checkStorageURL( aDestDirPath, pImplLib->maLibInfoFileURL, pImplLib->maStorageURL,
2541*cdf0e10cSrcweir             pImplLib->maUnexpandedStorageURL );
2542*cdf0e10cSrcweir 
2543*cdf0e10cSrcweir 	    try
2544*cdf0e10cSrcweir 	    {
2545*cdf0e10cSrcweir 	        if( mxSFI->isFolder( aLibDirPath ) )
2546*cdf0e10cSrcweir 	        {
2547*cdf0e10cSrcweir 			    if( !mxSFI->isFolder( aDestDirPath ) )
2548*cdf0e10cSrcweir 				    mxSFI->createFolder( aDestDirPath );
2549*cdf0e10cSrcweir 
2550*cdf0e10cSrcweir                 // Move index file
2551*cdf0e10cSrcweir 		        try
2552*cdf0e10cSrcweir 		        {
2553*cdf0e10cSrcweir 					if( mxSFI->exists( pImplLib->maLibInfoFileURL ) )
2554*cdf0e10cSrcweir 						mxSFI->kill( pImplLib->maLibInfoFileURL );
2555*cdf0e10cSrcweir             	    mxSFI->move( aLibInfoFileURL, pImplLib->maLibInfoFileURL );
2556*cdf0e10cSrcweir                 }
2557*cdf0e10cSrcweir             	catch( Exception& )
2558*cdf0e10cSrcweir                 {
2559*cdf0e10cSrcweir                 }
2560*cdf0e10cSrcweir 
2561*cdf0e10cSrcweir 			    Sequence< OUString > aElementNames = xNameAccess->getElementNames();
2562*cdf0e10cSrcweir 			    sal_Int32 nNameCount = aElementNames.getLength();
2563*cdf0e10cSrcweir 			    const OUString* pNames = aElementNames.getConstArray();
2564*cdf0e10cSrcweir 			    for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
2565*cdf0e10cSrcweir 			    {
2566*cdf0e10cSrcweir 				    OUString aElementName = pNames[ i ];
2567*cdf0e10cSrcweir 
2568*cdf0e10cSrcweir 				    INetURLObject aElementInetObj( aLibDirPath );
2569*cdf0e10cSrcweir 				    aElementInetObj.insertName( aElementName, sal_False,
2570*cdf0e10cSrcweir 					    INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
2571*cdf0e10cSrcweir 				    aElementInetObj.setExtension( maLibElementFileExtension );
2572*cdf0e10cSrcweir 				    String aElementPath( aElementInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
2573*cdf0e10cSrcweir 
2574*cdf0e10cSrcweir 				    INetURLObject aElementDestInetObj( aDestDirPath );
2575*cdf0e10cSrcweir 				    aElementDestInetObj.insertName( aElementName, sal_False,
2576*cdf0e10cSrcweir 					    INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
2577*cdf0e10cSrcweir 				    aElementDestInetObj.setExtension( maLibElementFileExtension );
2578*cdf0e10cSrcweir 				    String aDestElementPath( aElementDestInetObj.GetMainURL( INetURLObject::NO_DECODE ) );
2579*cdf0e10cSrcweir 
2580*cdf0e10cSrcweir 		            try
2581*cdf0e10cSrcweir 		            {
2582*cdf0e10cSrcweir 					    if( mxSFI->exists( aDestElementPath ) )
2583*cdf0e10cSrcweir 						    mxSFI->kill( aDestElementPath );
2584*cdf0e10cSrcweir             	        mxSFI->move( aElementPath, aDestElementPath );
2585*cdf0e10cSrcweir                     }
2586*cdf0e10cSrcweir             		catch( Exception& )
2587*cdf0e10cSrcweir                     {
2588*cdf0e10cSrcweir                     }
2589*cdf0e10cSrcweir 			    }
2590*cdf0e10cSrcweir 				pImplLib->storeResourcesAsURL( aDestDirPath, NewName );
2591*cdf0e10cSrcweir 
2592*cdf0e10cSrcweir                 // Delete folder if empty
2593*cdf0e10cSrcweir                 Sequence< OUString > aContentSeq = mxSFI->getFolderContents( aLibDirPath, true );
2594*cdf0e10cSrcweir     		    sal_Int32 nCount = aContentSeq.getLength();
2595*cdf0e10cSrcweir 	            if( !nCount )
2596*cdf0e10cSrcweir                 {
2597*cdf0e10cSrcweir        	            mxSFI->kill( aLibDirPath );
2598*cdf0e10cSrcweir                 }
2599*cdf0e10cSrcweir 
2600*cdf0e10cSrcweir                 bMovedSuccessful = true;
2601*cdf0e10cSrcweir 				pImplLib->implSetModified( sal_True );
2602*cdf0e10cSrcweir 	        }
2603*cdf0e10cSrcweir         }
2604*cdf0e10cSrcweir         catch( Exception& )
2605*cdf0e10cSrcweir         {
2606*cdf0e10cSrcweir             // Restore old library
2607*cdf0e10cSrcweir         	maNameContainer.insertByName( Name, aLibAny ) ;
2608*cdf0e10cSrcweir         }
2609*cdf0e10cSrcweir     }
2610*cdf0e10cSrcweir 
2611*cdf0e10cSrcweir     if( bStorage && !pImplLib->mbLink )
2612*cdf0e10cSrcweir 		pImplLib->implSetModified( sal_True );
2613*cdf0e10cSrcweir 
2614*cdf0e10cSrcweir     if( bMovedSuccessful )
2615*cdf0e10cSrcweir        	maNameContainer.insertByName( NewName, aLibAny ) ;
2616*cdf0e10cSrcweir 
2617*cdf0e10cSrcweir }
2618*cdf0e10cSrcweir 
2619*cdf0e10cSrcweir 
2620*cdf0e10cSrcweir // Methods XInitialization
2621*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::initialize( const Sequence< Any >& _rArguments )
2622*cdf0e10cSrcweir     throw (Exception, RuntimeException)
2623*cdf0e10cSrcweir {
2624*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2625*cdf0e10cSrcweir 	sal_Int32 nArgCount = _rArguments.getLength();
2626*cdf0e10cSrcweir     if ( nArgCount == 1 )
2627*cdf0e10cSrcweir     {
2628*cdf0e10cSrcweir         OUString sInitialDocumentURL;
2629*cdf0e10cSrcweir         Reference< XStorageBasedDocument > xDocument;
2630*cdf0e10cSrcweir         if ( _rArguments[0] >>= sInitialDocumentURL )
2631*cdf0e10cSrcweir         {
2632*cdf0e10cSrcweir             initializeFromDocumentURL( sInitialDocumentURL );
2633*cdf0e10cSrcweir             return;
2634*cdf0e10cSrcweir         }
2635*cdf0e10cSrcweir 
2636*cdf0e10cSrcweir         if ( _rArguments[0] >>= xDocument )
2637*cdf0e10cSrcweir         {
2638*cdf0e10cSrcweir             initializeFromDocument( xDocument );
2639*cdf0e10cSrcweir             return;
2640*cdf0e10cSrcweir         }
2641*cdf0e10cSrcweir     }
2642*cdf0e10cSrcweir 
2643*cdf0e10cSrcweir     throw IllegalArgumentException();
2644*cdf0e10cSrcweir }
2645*cdf0e10cSrcweir 
2646*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::initializeFromDocumentURL( const ::rtl::OUString& _rInitialDocumentURL )
2647*cdf0e10cSrcweir {
2648*cdf0e10cSrcweir     init( _rInitialDocumentURL, NULL );
2649*cdf0e10cSrcweir }
2650*cdf0e10cSrcweir 
2651*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::initializeFromDocument( const Reference< XStorageBasedDocument >& _rxDocument )
2652*cdf0e10cSrcweir {
2653*cdf0e10cSrcweir     // check whether this is a valid OfficeDocument, and obtain the document's root storage
2654*cdf0e10cSrcweir     Reference< XStorage > xDocStorage;
2655*cdf0e10cSrcweir     try
2656*cdf0e10cSrcweir     {
2657*cdf0e10cSrcweir         Reference< XServiceInfo > xSI( _rxDocument, UNO_QUERY_THROW );
2658*cdf0e10cSrcweir         if ( xSI->supportsService( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.OfficeDocument" ) ) ) )
2659*cdf0e10cSrcweir             xDocStorage.set( _rxDocument->getDocumentStorage(), UNO_QUERY_THROW );
2660*cdf0e10cSrcweir 
2661*cdf0e10cSrcweir         Reference< XModel > xDocument( _rxDocument, UNO_QUERY_THROW );
2662*cdf0e10cSrcweir         Reference< XComponent > xDocComponent( _rxDocument, UNO_QUERY_THROW );
2663*cdf0e10cSrcweir 
2664*cdf0e10cSrcweir         mxOwnerDocument = xDocument;
2665*cdf0e10cSrcweir         startComponentListening( xDocComponent );
2666*cdf0e10cSrcweir     }
2667*cdf0e10cSrcweir     catch( const Exception& ) { }
2668*cdf0e10cSrcweir 
2669*cdf0e10cSrcweir     if ( !xDocStorage.is() )
2670*cdf0e10cSrcweir         throw IllegalArgumentException();
2671*cdf0e10cSrcweir 
2672*cdf0e10cSrcweir     init( OUString(), xDocStorage );
2673*cdf0e10cSrcweir }
2674*cdf0e10cSrcweir 
2675*cdf0e10cSrcweir // OEventListenerAdapter
2676*cdf0e10cSrcweir void SfxLibraryContainer::_disposing( const EventObject& _rSource )
2677*cdf0e10cSrcweir {
2678*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
2679*cdf0e10cSrcweir     Reference< XModel > xDocument( mxOwnerDocument.get(), UNO_QUERY );
2680*cdf0e10cSrcweir     OSL_ENSURE( ( xDocument == _rSource.Source ) && xDocument.is(), "SfxLibraryContainer::_disposing: where does this come from?" );
2681*cdf0e10cSrcweir #else
2682*cdf0e10cSrcweir     (void)_rSource;
2683*cdf0e10cSrcweir #endif
2684*cdf0e10cSrcweir     dispose();
2685*cdf0e10cSrcweir }
2686*cdf0e10cSrcweir 
2687*cdf0e10cSrcweir // OComponentHelper
2688*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::disposing()
2689*cdf0e10cSrcweir {
2690*cdf0e10cSrcweir     Reference< XModel > xModel = mxOwnerDocument;
2691*cdf0e10cSrcweir     EventObject aEvent( xModel.get() );
2692*cdf0e10cSrcweir     maVBAScriptListeners.disposing( aEvent );
2693*cdf0e10cSrcweir     stopAllComponentListening();
2694*cdf0e10cSrcweir     mxOwnerDocument = WeakReference< XModel >();
2695*cdf0e10cSrcweir }
2696*cdf0e10cSrcweir 
2697*cdf0e10cSrcweir // Methods XLibraryContainerPassword
2698*cdf0e10cSrcweir sal_Bool SAL_CALL SfxLibraryContainer::isLibraryPasswordProtected( const OUString& )
2699*cdf0e10cSrcweir     throw (NoSuchElementException, RuntimeException)
2700*cdf0e10cSrcweir {
2701*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2702*cdf0e10cSrcweir     return sal_False;
2703*cdf0e10cSrcweir }
2704*cdf0e10cSrcweir 
2705*cdf0e10cSrcweir sal_Bool SAL_CALL SfxLibraryContainer::isLibraryPasswordVerified( const OUString& )
2706*cdf0e10cSrcweir     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
2707*cdf0e10cSrcweir {
2708*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2709*cdf0e10cSrcweir 	throw IllegalArgumentException();
2710*cdf0e10cSrcweir }
2711*cdf0e10cSrcweir 
2712*cdf0e10cSrcweir sal_Bool SAL_CALL SfxLibraryContainer::verifyLibraryPassword
2713*cdf0e10cSrcweir     ( const OUString&, const OUString& )
2714*cdf0e10cSrcweir         throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
2715*cdf0e10cSrcweir {
2716*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2717*cdf0e10cSrcweir 	throw IllegalArgumentException();
2718*cdf0e10cSrcweir }
2719*cdf0e10cSrcweir 
2720*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::changeLibraryPassword(
2721*cdf0e10cSrcweir     const OUString&, const OUString&, const OUString& )
2722*cdf0e10cSrcweir         throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
2723*cdf0e10cSrcweir {
2724*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2725*cdf0e10cSrcweir 	throw IllegalArgumentException();
2726*cdf0e10cSrcweir }
2727*cdf0e10cSrcweir 
2728*cdf0e10cSrcweir // Methods XContainer
2729*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::addContainerListener( const Reference< XContainerListener >& xListener )
2730*cdf0e10cSrcweir 	throw (RuntimeException)
2731*cdf0e10cSrcweir {
2732*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2733*cdf0e10cSrcweir 	maNameContainer.setEventSource( static_cast< XInterface* >( (OWeakObject*)this ) );
2734*cdf0e10cSrcweir 	maNameContainer.addContainerListener( xListener );
2735*cdf0e10cSrcweir }
2736*cdf0e10cSrcweir 
2737*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::removeContainerListener( const Reference< XContainerListener >& xListener )
2738*cdf0e10cSrcweir 	throw (RuntimeException)
2739*cdf0e10cSrcweir {
2740*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2741*cdf0e10cSrcweir 	maNameContainer.removeContainerListener( xListener );
2742*cdf0e10cSrcweir }
2743*cdf0e10cSrcweir 
2744*cdf0e10cSrcweir // Methods XLibraryContainerExport
2745*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::exportLibrary( const OUString& Name, const OUString& URL,
2746*cdf0e10cSrcweir 	const Reference< XInteractionHandler >& Handler )
2747*cdf0e10cSrcweir 		throw ( uno::Exception, NoSuchElementException, RuntimeException)
2748*cdf0e10cSrcweir {
2749*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2750*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2751*cdf0e10cSrcweir 
2752*cdf0e10cSrcweir 	Reference< XSimpleFileAccess > xToUseSFI;
2753*cdf0e10cSrcweir 	if( Handler.is() )
2754*cdf0e10cSrcweir 	{
2755*cdf0e10cSrcweir 		xToUseSFI = Reference< XSimpleFileAccess >( mxMSF->createInstance
2756*cdf0e10cSrcweir 			( OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), UNO_QUERY );
2757*cdf0e10cSrcweir 		if( xToUseSFI.is() )
2758*cdf0e10cSrcweir 			xToUseSFI->setInteractionHandler( Handler );
2759*cdf0e10cSrcweir 	}
2760*cdf0e10cSrcweir 
2761*cdf0e10cSrcweir 	// Maybe lib is not loaded?!
2762*cdf0e10cSrcweir 	loadLibrary( Name );
2763*cdf0e10cSrcweir 
2764*cdf0e10cSrcweir 	uno::Reference< ::com::sun::star::embed::XStorage > xDummyStor;
2765*cdf0e10cSrcweir     if( pImplLib->mbPasswordProtected )
2766*cdf0e10cSrcweir 		implStorePasswordLibrary( pImplLib, Name, xDummyStor, URL, xToUseSFI, Handler );
2767*cdf0e10cSrcweir     else
2768*cdf0e10cSrcweir 		implStoreLibrary( pImplLib, Name, xDummyStor, URL, xToUseSFI, Handler );
2769*cdf0e10cSrcweir 
2770*cdf0e10cSrcweir 	::xmlscript::LibDescriptor aLibDesc;
2771*cdf0e10cSrcweir 	aLibDesc.aName = Name;
2772*cdf0e10cSrcweir 	aLibDesc.bLink = false;				// Link status gets lost?
2773*cdf0e10cSrcweir 	aLibDesc.bReadOnly = pImplLib->mbReadOnly;
2774*cdf0e10cSrcweir 	aLibDesc.bPreload = false;			// Preload status gets lost?
2775*cdf0e10cSrcweir 	aLibDesc.bPasswordProtected = pImplLib->mbPasswordProtected;
2776*cdf0e10cSrcweir 	aLibDesc.aElementNames = pImplLib->getElementNames();
2777*cdf0e10cSrcweir 
2778*cdf0e10cSrcweir 	implStoreLibraryIndexFile( pImplLib, aLibDesc, xDummyStor, URL, xToUseSFI );
2779*cdf0e10cSrcweir }
2780*cdf0e10cSrcweir 
2781*cdf0e10cSrcweir OUString SfxLibraryContainer::expand_url( const OUString& url )
2782*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
2783*cdf0e10cSrcweir {
2784*cdf0e10cSrcweir     if (0 == url.compareToAscii( RTL_CONSTASCII_STRINGPARAM(EXPAND_PROTOCOL ":") ))
2785*cdf0e10cSrcweir     {
2786*cdf0e10cSrcweir         if( !mxMacroExpander.is() )
2787*cdf0e10cSrcweir         {
2788*cdf0e10cSrcweir             Reference< XPropertySet > xProps( mxMSF, UNO_QUERY );
2789*cdf0e10cSrcweir             OSL_ASSERT( xProps.is() );
2790*cdf0e10cSrcweir             if( xProps.is() )
2791*cdf0e10cSrcweir             {
2792*cdf0e10cSrcweir                 Reference< XComponentContext > xContext;
2793*cdf0e10cSrcweir                 xProps->getPropertyValue(
2794*cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xContext;
2795*cdf0e10cSrcweir                 OSL_ASSERT( xContext.is() );
2796*cdf0e10cSrcweir                 if( xContext.is() )
2797*cdf0e10cSrcweir                 {
2798*cdf0e10cSrcweir                     Reference< util::XMacroExpander > xExpander;
2799*cdf0e10cSrcweir                     xContext->getValueByName(
2800*cdf0e10cSrcweir                         OUSTR("/singletons/com.sun.star.util.theMacroExpander") ) >>= xExpander;
2801*cdf0e10cSrcweir                     if(! xExpander.is())
2802*cdf0e10cSrcweir                     {
2803*cdf0e10cSrcweir 						throw uno::DeploymentException(
2804*cdf0e10cSrcweir                             OUSTR("no macro expander singleton available!"), Reference< XInterface >() );
2805*cdf0e10cSrcweir                     }
2806*cdf0e10cSrcweir                     MutexGuard guard( Mutex::getGlobalMutex() );
2807*cdf0e10cSrcweir                     if( !mxMacroExpander.is() )
2808*cdf0e10cSrcweir                     {
2809*cdf0e10cSrcweir                         mxMacroExpander = xExpander;
2810*cdf0e10cSrcweir                     }
2811*cdf0e10cSrcweir                 }
2812*cdf0e10cSrcweir             }
2813*cdf0e10cSrcweir         }
2814*cdf0e10cSrcweir 
2815*cdf0e10cSrcweir         if( !mxMacroExpander.is() )
2816*cdf0e10cSrcweir             return url;
2817*cdf0e10cSrcweir 
2818*cdf0e10cSrcweir         // cut protocol
2819*cdf0e10cSrcweir         OUString macro( url.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
2820*cdf0e10cSrcweir         // decode uric class chars
2821*cdf0e10cSrcweir         macro = Uri::decode( macro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
2822*cdf0e10cSrcweir         // expand macro string
2823*cdf0e10cSrcweir         OUString ret( mxMacroExpander->expandMacros( macro ) );
2824*cdf0e10cSrcweir         return ret;
2825*cdf0e10cSrcweir     }
2826*cdf0e10cSrcweir 	else if( mxStringSubstitution.is() )
2827*cdf0e10cSrcweir 	{
2828*cdf0e10cSrcweir 		OUString ret( mxStringSubstitution->substituteVariables( url, false ) );
2829*cdf0e10cSrcweir         return ret;
2830*cdf0e10cSrcweir 	}
2831*cdf0e10cSrcweir     else
2832*cdf0e10cSrcweir     {
2833*cdf0e10cSrcweir         return url;
2834*cdf0e10cSrcweir     }
2835*cdf0e10cSrcweir }
2836*cdf0e10cSrcweir 
2837*cdf0e10cSrcweir //XLibraryContainer3
2838*cdf0e10cSrcweir OUString SAL_CALL SfxLibraryContainer::getOriginalLibraryLinkURL( const OUString& Name )
2839*cdf0e10cSrcweir     throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
2840*cdf0e10cSrcweir {
2841*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2842*cdf0e10cSrcweir     SfxLibrary* pImplLib = getImplLib( Name );
2843*cdf0e10cSrcweir 	sal_Bool bLink = pImplLib->mbLink;
2844*cdf0e10cSrcweir 	if( !bLink )
2845*cdf0e10cSrcweir 		throw IllegalArgumentException();
2846*cdf0e10cSrcweir     OUString aRetStr = pImplLib->maOrignialStorageURL;
2847*cdf0e10cSrcweir     return aRetStr;
2848*cdf0e10cSrcweir }
2849*cdf0e10cSrcweir 
2850*cdf0e10cSrcweir 
2851*cdf0e10cSrcweir // XVBACompatibility
2852*cdf0e10cSrcweir ::sal_Bool SAL_CALL SfxLibraryContainer::getVBACompatibilityMode() throw (RuntimeException)
2853*cdf0e10cSrcweir {
2854*cdf0e10cSrcweir 	return mbVBACompat;
2855*cdf0e10cSrcweir }
2856*cdf0e10cSrcweir 
2857*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::setVBACompatibilityMode( ::sal_Bool _vbacompatmodeon ) throw (RuntimeException)
2858*cdf0e10cSrcweir {
2859*cdf0e10cSrcweir     /*  The member variable mbVBACompat must be set first, the following call
2860*cdf0e10cSrcweir         to getBasicManager() may call getVBACompatibilityMode() which returns
2861*cdf0e10cSrcweir         this value. */
2862*cdf0e10cSrcweir     mbVBACompat = _vbacompatmodeon;
2863*cdf0e10cSrcweir 	if( BasicManager* pBasMgr = getBasicManager() )
2864*cdf0e10cSrcweir 	{
2865*cdf0e10cSrcweir 		// get the standard library
2866*cdf0e10cSrcweir         String aLibName = pBasMgr->GetName();
2867*cdf0e10cSrcweir         if ( aLibName.Len() == 0 )
2868*cdf0e10cSrcweir             aLibName = String( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
2869*cdf0e10cSrcweir 
2870*cdf0e10cSrcweir 		if( StarBASIC* pBasic = pBasMgr->GetLib( aLibName ) )
2871*cdf0e10cSrcweir 			pBasic->SetVBAEnabled( _vbacompatmodeon );
2872*cdf0e10cSrcweir 
2873*cdf0e10cSrcweir         /*  If in VBA compatibility mode, force creation of the VBA Globals
2874*cdf0e10cSrcweir             object. Each application will create an instance of its own
2875*cdf0e10cSrcweir             implementation and store it in its Basic manager. Implementations
2876*cdf0e10cSrcweir             will do all necessary additional initialization, such as
2877*cdf0e10cSrcweir             registering the global "This***Doc" UNO constant, starting the
2878*cdf0e10cSrcweir             document events processor etc.
2879*cdf0e10cSrcweir          */
2880*cdf0e10cSrcweir         if( mbVBACompat ) try
2881*cdf0e10cSrcweir         {
2882*cdf0e10cSrcweir             Reference< XModel > xModel( mxOwnerDocument );   // weak-ref -> ref
2883*cdf0e10cSrcweir             Reference< XMultiServiceFactory > xFactory( xModel, UNO_QUERY_THROW );
2884*cdf0e10cSrcweir             xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.VBAGlobals" ) ) );
2885*cdf0e10cSrcweir         }
2886*cdf0e10cSrcweir         catch( Exception& )
2887*cdf0e10cSrcweir         {
2888*cdf0e10cSrcweir         }
2889*cdf0e10cSrcweir 	}
2890*cdf0e10cSrcweir }
2891*cdf0e10cSrcweir 
2892*cdf0e10cSrcweir sal_Int32 SAL_CALL SfxLibraryContainer::getRunningVBAScripts() throw (RuntimeException)
2893*cdf0e10cSrcweir {
2894*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2895*cdf0e10cSrcweir     return mnRunningVBAScripts;
2896*cdf0e10cSrcweir }
2897*cdf0e10cSrcweir 
2898*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::addVBAScriptListener( const Reference< vba::XVBAScriptListener >& rxListener ) throw (RuntimeException)
2899*cdf0e10cSrcweir {
2900*cdf0e10cSrcweir     maVBAScriptListeners.addTypedListener( rxListener );
2901*cdf0e10cSrcweir }
2902*cdf0e10cSrcweir 
2903*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::removeVBAScriptListener( const Reference< vba::XVBAScriptListener >& rxListener ) throw (RuntimeException)
2904*cdf0e10cSrcweir {
2905*cdf0e10cSrcweir     maVBAScriptListeners.removeTypedListener( rxListener );
2906*cdf0e10cSrcweir }
2907*cdf0e10cSrcweir 
2908*cdf0e10cSrcweir void SAL_CALL SfxLibraryContainer::broadcastVBAScriptEvent( sal_Int32 nIdentifier, const ::rtl::OUString& rModuleName ) throw (RuntimeException)
2909*cdf0e10cSrcweir {
2910*cdf0e10cSrcweir     // own lock for accessing the number of running scripts
2911*cdf0e10cSrcweir     enterMethod();
2912*cdf0e10cSrcweir     switch( nIdentifier )
2913*cdf0e10cSrcweir     {
2914*cdf0e10cSrcweir         case vba::VBAScriptEventId::SCRIPT_STARTED:
2915*cdf0e10cSrcweir             ++mnRunningVBAScripts;
2916*cdf0e10cSrcweir         break;
2917*cdf0e10cSrcweir         case vba::VBAScriptEventId::SCRIPT_STOPPED:
2918*cdf0e10cSrcweir             --mnRunningVBAScripts;
2919*cdf0e10cSrcweir         break;
2920*cdf0e10cSrcweir     }
2921*cdf0e10cSrcweir     leaveMethod();
2922*cdf0e10cSrcweir 
2923*cdf0e10cSrcweir     Reference< XModel > xModel = mxOwnerDocument;  // weak-ref -> ref
2924*cdf0e10cSrcweir     Reference< XInterface > xSender( xModel, UNO_QUERY_THROW );
2925*cdf0e10cSrcweir     vba::VBAScriptEvent aEvent( xSender, nIdentifier, rModuleName );
2926*cdf0e10cSrcweir     maVBAScriptListeners.notify( aEvent );
2927*cdf0e10cSrcweir }
2928*cdf0e10cSrcweir 
2929*cdf0e10cSrcweir // Methods XServiceInfo
2930*cdf0e10cSrcweir ::sal_Bool SAL_CALL SfxLibraryContainer::supportsService( const ::rtl::OUString& _rServiceName )
2931*cdf0e10cSrcweir     throw (RuntimeException)
2932*cdf0e10cSrcweir {
2933*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
2934*cdf0e10cSrcweir     Sequence< OUString > aSupportedServices( getSupportedServiceNames() );
2935*cdf0e10cSrcweir     const OUString* pSupportedServices = aSupportedServices.getConstArray();
2936*cdf0e10cSrcweir     for ( sal_Int32 i=0; i<aSupportedServices.getLength(); ++i, ++pSupportedServices )
2937*cdf0e10cSrcweir         if ( *pSupportedServices == _rServiceName )
2938*cdf0e10cSrcweir             return sal_True;
2939*cdf0e10cSrcweir     return sal_False;
2940*cdf0e10cSrcweir }
2941*cdf0e10cSrcweir 
2942*cdf0e10cSrcweir //============================================================================
2943*cdf0e10cSrcweir 
2944*cdf0e10cSrcweir // Implementation class SfxLibrary
2945*cdf0e10cSrcweir 
2946*cdf0e10cSrcweir // Ctor
2947*cdf0e10cSrcweir SfxLibrary::SfxLibrary( ModifiableHelper& _rModifiable, const Type& aType,
2948*cdf0e10cSrcweir     const Reference< XMultiServiceFactory >& xMSF, const Reference< XSimpleFileAccess >& xSFI )
2949*cdf0e10cSrcweir 		: OComponentHelper( m_aMutex )
2950*cdf0e10cSrcweir 		, mxMSF( xMSF )
2951*cdf0e10cSrcweir 		, mxSFI( xSFI )
2952*cdf0e10cSrcweir         , mrModifiable( _rModifiable )
2953*cdf0e10cSrcweir 		, maNameContainer( aType )
2954*cdf0e10cSrcweir 		, mbLoaded( sal_True )
2955*cdf0e10cSrcweir 		, mbIsModified( sal_True )
2956*cdf0e10cSrcweir 		, mbInitialised( sal_False )
2957*cdf0e10cSrcweir 		, mbLink( sal_False )
2958*cdf0e10cSrcweir 		, mbReadOnly( sal_False )
2959*cdf0e10cSrcweir 		, mbReadOnlyLink( sal_False )
2960*cdf0e10cSrcweir 		, mbPreload( sal_False )
2961*cdf0e10cSrcweir 		, mbPasswordProtected( sal_False )
2962*cdf0e10cSrcweir 		, mbPasswordVerified( sal_False )
2963*cdf0e10cSrcweir 		, mbDoc50Password( sal_False )
2964*cdf0e10cSrcweir 		, mbSharedIndexFile( sal_False )
2965*cdf0e10cSrcweir 		, mbExtension( sal_False )
2966*cdf0e10cSrcweir {
2967*cdf0e10cSrcweir }
2968*cdf0e10cSrcweir 
2969*cdf0e10cSrcweir SfxLibrary::SfxLibrary( ModifiableHelper& _rModifiable, const Type& aType,
2970*cdf0e10cSrcweir     const Reference< XMultiServiceFactory >& xMSF, const Reference< XSimpleFileAccess >& xSFI,
2971*cdf0e10cSrcweir 	const OUString& aLibInfoFileURL, const OUString& aStorageURL, sal_Bool ReadOnly )
2972*cdf0e10cSrcweir 		: OComponentHelper( m_aMutex )
2973*cdf0e10cSrcweir 		, mxMSF( xMSF )
2974*cdf0e10cSrcweir 		, mxSFI( xSFI )
2975*cdf0e10cSrcweir         , mrModifiable( _rModifiable )
2976*cdf0e10cSrcweir 		, maNameContainer( aType )
2977*cdf0e10cSrcweir 		, mbLoaded( sal_False )
2978*cdf0e10cSrcweir 		, mbIsModified( sal_True )
2979*cdf0e10cSrcweir 		, mbInitialised( sal_False )
2980*cdf0e10cSrcweir 		, maLibInfoFileURL( aLibInfoFileURL )
2981*cdf0e10cSrcweir 		, maStorageURL( aStorageURL )
2982*cdf0e10cSrcweir 		, mbLink( sal_True )
2983*cdf0e10cSrcweir 		, mbReadOnly( sal_False )
2984*cdf0e10cSrcweir 		, mbReadOnlyLink( ReadOnly )
2985*cdf0e10cSrcweir 		, mbPreload( sal_False )
2986*cdf0e10cSrcweir 		, mbPasswordProtected( sal_False )
2987*cdf0e10cSrcweir 		, mbPasswordVerified( sal_False )
2988*cdf0e10cSrcweir 		, mbDoc50Password( sal_False )
2989*cdf0e10cSrcweir 		, mbSharedIndexFile( sal_False )
2990*cdf0e10cSrcweir 		, mbExtension( sal_False )
2991*cdf0e10cSrcweir {
2992*cdf0e10cSrcweir }
2993*cdf0e10cSrcweir 
2994*cdf0e10cSrcweir void SfxLibrary::implSetModified( sal_Bool _bIsModified )
2995*cdf0e10cSrcweir {
2996*cdf0e10cSrcweir     if ( mbIsModified == _bIsModified )
2997*cdf0e10cSrcweir         return;
2998*cdf0e10cSrcweir     mbIsModified = _bIsModified;
2999*cdf0e10cSrcweir     if ( mbIsModified )
3000*cdf0e10cSrcweir         mrModifiable.setModified( sal_True );
3001*cdf0e10cSrcweir }
3002*cdf0e10cSrcweir 
3003*cdf0e10cSrcweir // Methods XInterface
3004*cdf0e10cSrcweir Any SAL_CALL SfxLibrary::queryInterface( const Type& rType )
3005*cdf0e10cSrcweir 	throw( RuntimeException )
3006*cdf0e10cSrcweir {
3007*cdf0e10cSrcweir 	Any aRet;
3008*cdf0e10cSrcweir 
3009*cdf0e10cSrcweir     /*
3010*cdf0e10cSrcweir 	if( mbReadOnly )
3011*cdf0e10cSrcweir 	{
3012*cdf0e10cSrcweir 		aRet = Any( ::cppu::queryInterface( rType,
3013*cdf0e10cSrcweir 			static_cast< XContainer * >( this ),
3014*cdf0e10cSrcweir 			static_cast< XNameAccess * >( this ) ) );
3015*cdf0e10cSrcweir 	}
3016*cdf0e10cSrcweir 	else
3017*cdf0e10cSrcweir 	{
3018*cdf0e10cSrcweir     */
3019*cdf0e10cSrcweir 		aRet = Any( ::cppu::queryInterface( rType,
3020*cdf0e10cSrcweir 			static_cast< XContainer * >( this ),
3021*cdf0e10cSrcweir 			static_cast< XNameContainer * >( this ),
3022*cdf0e10cSrcweir 			static_cast< XNameAccess * >( this ),
3023*cdf0e10cSrcweir 			static_cast< XElementAccess * >( this ),
3024*cdf0e10cSrcweir 			static_cast< XChangesNotifier * >( this ) ) );
3025*cdf0e10cSrcweir 	//}
3026*cdf0e10cSrcweir 	if( !aRet.hasValue() )
3027*cdf0e10cSrcweir 		aRet = OComponentHelper::queryInterface( rType );
3028*cdf0e10cSrcweir 	return aRet;
3029*cdf0e10cSrcweir }
3030*cdf0e10cSrcweir 
3031*cdf0e10cSrcweir // Methods XElementAccess
3032*cdf0e10cSrcweir Type SfxLibrary::getElementType()
3033*cdf0e10cSrcweir 	throw(RuntimeException)
3034*cdf0e10cSrcweir {
3035*cdf0e10cSrcweir 	return maNameContainer.getElementType();
3036*cdf0e10cSrcweir }
3037*cdf0e10cSrcweir 
3038*cdf0e10cSrcweir sal_Bool SfxLibrary::hasElements()
3039*cdf0e10cSrcweir 	throw(RuntimeException)
3040*cdf0e10cSrcweir {
3041*cdf0e10cSrcweir 	sal_Bool bRet = maNameContainer.hasElements();
3042*cdf0e10cSrcweir 	return bRet;
3043*cdf0e10cSrcweir }
3044*cdf0e10cSrcweir 
3045*cdf0e10cSrcweir // Methods XNameAccess
3046*cdf0e10cSrcweir Any SfxLibrary::getByName( const OUString& aName )
3047*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
3048*cdf0e10cSrcweir {
3049*cdf0e10cSrcweir     impl_checkLoaded();
3050*cdf0e10cSrcweir 
3051*cdf0e10cSrcweir 	Any aRetAny = maNameContainer.getByName( aName ) ;
3052*cdf0e10cSrcweir 	return aRetAny;
3053*cdf0e10cSrcweir }
3054*cdf0e10cSrcweir 
3055*cdf0e10cSrcweir Sequence< OUString > SfxLibrary::getElementNames()
3056*cdf0e10cSrcweir 	throw(RuntimeException)
3057*cdf0e10cSrcweir {
3058*cdf0e10cSrcweir 	return maNameContainer.getElementNames();
3059*cdf0e10cSrcweir }
3060*cdf0e10cSrcweir 
3061*cdf0e10cSrcweir sal_Bool SfxLibrary::hasByName( const OUString& aName )
3062*cdf0e10cSrcweir 	throw(RuntimeException)
3063*cdf0e10cSrcweir {
3064*cdf0e10cSrcweir 	sal_Bool bRet = maNameContainer.hasByName( aName );
3065*cdf0e10cSrcweir 	return bRet;
3066*cdf0e10cSrcweir }
3067*cdf0e10cSrcweir 
3068*cdf0e10cSrcweir void SfxLibrary::impl_checkReadOnly()
3069*cdf0e10cSrcweir {
3070*cdf0e10cSrcweir 	if( mbReadOnly || (mbLink && mbReadOnlyLink) )
3071*cdf0e10cSrcweir         throw IllegalArgumentException(
3072*cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Library is readonly." ) ),
3073*cdf0e10cSrcweir             // TODO: resource
3074*cdf0e10cSrcweir             *this, 0
3075*cdf0e10cSrcweir         );
3076*cdf0e10cSrcweir }
3077*cdf0e10cSrcweir 
3078*cdf0e10cSrcweir void SfxLibrary::impl_checkLoaded()
3079*cdf0e10cSrcweir {
3080*cdf0e10cSrcweir     if ( !mbLoaded )
3081*cdf0e10cSrcweir         throw WrappedTargetException(
3082*cdf0e10cSrcweir             ::rtl::OUString(),
3083*cdf0e10cSrcweir             *this,
3084*cdf0e10cSrcweir             makeAny( LibraryNotLoadedException(
3085*cdf0e10cSrcweir                 ::rtl::OUString(),
3086*cdf0e10cSrcweir                 *this
3087*cdf0e10cSrcweir             ) )
3088*cdf0e10cSrcweir         );
3089*cdf0e10cSrcweir }
3090*cdf0e10cSrcweir 
3091*cdf0e10cSrcweir // Methods XNameReplace
3092*cdf0e10cSrcweir void SfxLibrary::replaceByName( const OUString& aName, const Any& aElement )
3093*cdf0e10cSrcweir 	throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
3094*cdf0e10cSrcweir {
3095*cdf0e10cSrcweir     impl_checkReadOnly();
3096*cdf0e10cSrcweir     impl_checkLoaded();
3097*cdf0e10cSrcweir 
3098*cdf0e10cSrcweir     OSL_ENSURE( isLibraryElementValid( aElement ), "SfxLibrary::replaceByName: replacing element is invalid!" );
3099*cdf0e10cSrcweir 
3100*cdf0e10cSrcweir 	maNameContainer.replaceByName( aName, aElement );
3101*cdf0e10cSrcweir 	implSetModified( sal_True );
3102*cdf0e10cSrcweir }
3103*cdf0e10cSrcweir 
3104*cdf0e10cSrcweir 
3105*cdf0e10cSrcweir // Methods XNameContainer
3106*cdf0e10cSrcweir void SfxLibrary::insertByName( const OUString& aName, const Any& aElement )
3107*cdf0e10cSrcweir 	throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
3108*cdf0e10cSrcweir {
3109*cdf0e10cSrcweir     impl_checkReadOnly();
3110*cdf0e10cSrcweir     impl_checkLoaded();
3111*cdf0e10cSrcweir 
3112*cdf0e10cSrcweir     OSL_ENSURE( isLibraryElementValid( aElement ), "SfxLibrary::insertByName: to-be-inserted element is invalid!" );
3113*cdf0e10cSrcweir 
3114*cdf0e10cSrcweir 	maNameContainer.insertByName( aName, aElement );
3115*cdf0e10cSrcweir 	implSetModified( sal_True );
3116*cdf0e10cSrcweir }
3117*cdf0e10cSrcweir 
3118*cdf0e10cSrcweir void SfxLibrary::impl_removeWithoutChecks( const ::rtl::OUString& _rElementName )
3119*cdf0e10cSrcweir {
3120*cdf0e10cSrcweir 	maNameContainer.removeByName( _rElementName );
3121*cdf0e10cSrcweir 	implSetModified( sal_True );
3122*cdf0e10cSrcweir 
3123*cdf0e10cSrcweir     // Remove element file
3124*cdf0e10cSrcweir 	if( maStorageURL.getLength() )
3125*cdf0e10cSrcweir 	{
3126*cdf0e10cSrcweir 		INetURLObject aElementInetObj( maStorageURL );
3127*cdf0e10cSrcweir 		aElementInetObj.insertName( _rElementName, sal_False,
3128*cdf0e10cSrcweir 			INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
3129*cdf0e10cSrcweir 		aElementInetObj.setExtension( maLibElementFileExtension );
3130*cdf0e10cSrcweir 		OUString aFile = aElementInetObj.GetMainURL( INetURLObject::NO_DECODE );
3131*cdf0e10cSrcweir 
3132*cdf0e10cSrcweir 		try
3133*cdf0e10cSrcweir 		{
3134*cdf0e10cSrcweir 	        if( mxSFI->exists( aFile ) )
3135*cdf0e10cSrcweir 		        mxSFI->kill( aFile );
3136*cdf0e10cSrcweir         }
3137*cdf0e10cSrcweir         catch( Exception& )
3138*cdf0e10cSrcweir         {
3139*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
3140*cdf0e10cSrcweir         }
3141*cdf0e10cSrcweir 	}
3142*cdf0e10cSrcweir }
3143*cdf0e10cSrcweir 
3144*cdf0e10cSrcweir void SfxLibrary::removeByName( const OUString& Name )
3145*cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, RuntimeException)
3146*cdf0e10cSrcweir {
3147*cdf0e10cSrcweir     impl_checkReadOnly();
3148*cdf0e10cSrcweir     impl_checkLoaded();
3149*cdf0e10cSrcweir     impl_removeWithoutChecks( Name );
3150*cdf0e10cSrcweir }
3151*cdf0e10cSrcweir 
3152*cdf0e10cSrcweir // XTypeProvider
3153*cdf0e10cSrcweir Sequence< Type > SfxLibrary::getTypes()
3154*cdf0e10cSrcweir 	throw( RuntimeException )
3155*cdf0e10cSrcweir {
3156*cdf0e10cSrcweir 	static OTypeCollection * s_pTypes_NameContainer = 0;
3157*cdf0e10cSrcweir 	{
3158*cdf0e10cSrcweir 		if( !s_pTypes_NameContainer )
3159*cdf0e10cSrcweir 		{
3160*cdf0e10cSrcweir 			MutexGuard aGuard( Mutex::getGlobalMutex() );
3161*cdf0e10cSrcweir 			if( !s_pTypes_NameContainer )
3162*cdf0e10cSrcweir 			{
3163*cdf0e10cSrcweir 				static OTypeCollection s_aTypes_NameContainer(
3164*cdf0e10cSrcweir 					::getCppuType( (const Reference< XNameContainer > *)0 ),
3165*cdf0e10cSrcweir 					::getCppuType( (const Reference< XContainer > *)0 ),
3166*cdf0e10cSrcweir 					::getCppuType( (const Reference< XChangesNotifier > *)0 ),
3167*cdf0e10cSrcweir 					OComponentHelper::getTypes() );
3168*cdf0e10cSrcweir 				s_pTypes_NameContainer = &s_aTypes_NameContainer;
3169*cdf0e10cSrcweir 			}
3170*cdf0e10cSrcweir 		}
3171*cdf0e10cSrcweir 		return s_pTypes_NameContainer->getTypes();
3172*cdf0e10cSrcweir 	}
3173*cdf0e10cSrcweir }
3174*cdf0e10cSrcweir 
3175*cdf0e10cSrcweir 
3176*cdf0e10cSrcweir Sequence< sal_Int8 > SfxLibrary::getImplementationId()
3177*cdf0e10cSrcweir 	throw( RuntimeException )
3178*cdf0e10cSrcweir {
3179*cdf0e10cSrcweir 	static OImplementationId * s_pId_NameContainer = 0;
3180*cdf0e10cSrcweir 	{
3181*cdf0e10cSrcweir 		if( !s_pId_NameContainer )
3182*cdf0e10cSrcweir 		{
3183*cdf0e10cSrcweir 			MutexGuard aGuard( Mutex::getGlobalMutex() );
3184*cdf0e10cSrcweir 			if( !s_pId_NameContainer )
3185*cdf0e10cSrcweir 			{
3186*cdf0e10cSrcweir 				static OImplementationId s_aId_NameContainer;
3187*cdf0e10cSrcweir 				s_pId_NameContainer = &s_aId_NameContainer;
3188*cdf0e10cSrcweir 			}
3189*cdf0e10cSrcweir 		}
3190*cdf0e10cSrcweir 		return s_pId_NameContainer->getImplementationId();
3191*cdf0e10cSrcweir 	}
3192*cdf0e10cSrcweir }
3193*cdf0e10cSrcweir 
3194*cdf0e10cSrcweir // Methods XContainer
3195*cdf0e10cSrcweir void SAL_CALL SfxLibrary::addContainerListener( const Reference< XContainerListener >& xListener )
3196*cdf0e10cSrcweir 	throw (RuntimeException)
3197*cdf0e10cSrcweir {
3198*cdf0e10cSrcweir 	maNameContainer.setEventSource( static_cast< XInterface* >( (OWeakObject*)this ) );
3199*cdf0e10cSrcweir 	maNameContainer.addContainerListener( xListener );
3200*cdf0e10cSrcweir }
3201*cdf0e10cSrcweir 
3202*cdf0e10cSrcweir void SAL_CALL SfxLibrary::removeContainerListener( const Reference< XContainerListener >& xListener )
3203*cdf0e10cSrcweir 	throw (RuntimeException)
3204*cdf0e10cSrcweir {
3205*cdf0e10cSrcweir 	maNameContainer.removeContainerListener( xListener );
3206*cdf0e10cSrcweir }
3207*cdf0e10cSrcweir 
3208*cdf0e10cSrcweir // Methods XChangesNotifier
3209*cdf0e10cSrcweir void SAL_CALL SfxLibrary::addChangesListener( const Reference< XChangesListener >& xListener )
3210*cdf0e10cSrcweir     throw (RuntimeException)
3211*cdf0e10cSrcweir {
3212*cdf0e10cSrcweir 	maNameContainer.setEventSource( static_cast< XInterface* >( (OWeakObject*)this ) );
3213*cdf0e10cSrcweir 	maNameContainer.addChangesListener( xListener );
3214*cdf0e10cSrcweir }
3215*cdf0e10cSrcweir 
3216*cdf0e10cSrcweir void SAL_CALL SfxLibrary::removeChangesListener( const Reference< XChangesListener >& xListener )
3217*cdf0e10cSrcweir     throw (RuntimeException)
3218*cdf0e10cSrcweir {
3219*cdf0e10cSrcweir 	maNameContainer.removeChangesListener( xListener );
3220*cdf0e10cSrcweir }
3221*cdf0e10cSrcweir 
3222*cdf0e10cSrcweir //============================================================================
3223*cdf0e10cSrcweir // Implementation class ScriptExtensionIterator
3224*cdf0e10cSrcweir 
3225*cdf0e10cSrcweir static rtl::OUString aBasicLibMediaType( rtl::OUString::createFromAscii( "application/vnd.sun.star.basic-library" ) );
3226*cdf0e10cSrcweir static rtl::OUString aDialogLibMediaType( rtl::OUString::createFromAscii( "application/vnd.sun.star.dialog-library" ) );
3227*cdf0e10cSrcweir 
3228*cdf0e10cSrcweir ScriptExtensionIterator::ScriptExtensionIterator( void )
3229*cdf0e10cSrcweir 	: m_eState( USER_EXTENSIONS )
3230*cdf0e10cSrcweir 	, m_bUserPackagesLoaded( false )
3231*cdf0e10cSrcweir 	, m_bSharedPackagesLoaded( false )
3232*cdf0e10cSrcweir     , m_bBundledPackagesLoaded( false )
3233*cdf0e10cSrcweir 	, m_iUserPackage( 0 )
3234*cdf0e10cSrcweir 	, m_iSharedPackage( 0 )
3235*cdf0e10cSrcweir    	, m_iBundledPackage( 0 )
3236*cdf0e10cSrcweir 	, m_pScriptSubPackageIterator( NULL )
3237*cdf0e10cSrcweir {
3238*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xFactory = comphelper::getProcessServiceFactory();
3239*cdf0e10cSrcweir 	Reference< XPropertySet > xProps( xFactory, UNO_QUERY );
3240*cdf0e10cSrcweir 	OSL_ASSERT( xProps.is() );
3241*cdf0e10cSrcweir 	if (xProps.is())
3242*cdf0e10cSrcweir 	{
3243*cdf0e10cSrcweir 		xProps->getPropertyValue(
3244*cdf0e10cSrcweir 			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= m_xContext;
3245*cdf0e10cSrcweir 		OSL_ASSERT( m_xContext.is() );
3246*cdf0e10cSrcweir 	}
3247*cdf0e10cSrcweir 	if( !m_xContext.is() )
3248*cdf0e10cSrcweir 	{
3249*cdf0e10cSrcweir 		throw RuntimeException(
3250*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "ScriptExtensionIterator::init(), no XComponentContext" ),
3251*cdf0e10cSrcweir 			Reference< XInterface >() );
3252*cdf0e10cSrcweir 	}
3253*cdf0e10cSrcweir }
3254*cdf0e10cSrcweir 
3255*cdf0e10cSrcweir rtl::OUString ScriptExtensionIterator::nextBasicOrDialogLibrary( bool& rbPureDialogLib )
3256*cdf0e10cSrcweir {
3257*cdf0e10cSrcweir 	rtl::OUString aRetLib;
3258*cdf0e10cSrcweir 
3259*cdf0e10cSrcweir 	while( !aRetLib.getLength() && m_eState != END_REACHED )
3260*cdf0e10cSrcweir 	{
3261*cdf0e10cSrcweir 		switch( m_eState )
3262*cdf0e10cSrcweir 		{
3263*cdf0e10cSrcweir 			case USER_EXTENSIONS:
3264*cdf0e10cSrcweir 			{
3265*cdf0e10cSrcweir 				Reference< deployment::XPackage > xScriptPackage =
3266*cdf0e10cSrcweir 					implGetNextUserScriptPackage( rbPureDialogLib );
3267*cdf0e10cSrcweir 				if( !xScriptPackage.is() )
3268*cdf0e10cSrcweir 					break;
3269*cdf0e10cSrcweir 
3270*cdf0e10cSrcweir 				aRetLib = xScriptPackage->getURL();
3271*cdf0e10cSrcweir 				break;
3272*cdf0e10cSrcweir 			}
3273*cdf0e10cSrcweir 
3274*cdf0e10cSrcweir 			case SHARED_EXTENSIONS:
3275*cdf0e10cSrcweir 			{
3276*cdf0e10cSrcweir 				Reference< deployment::XPackage > xScriptPackage =
3277*cdf0e10cSrcweir 					implGetNextSharedScriptPackage( rbPureDialogLib );
3278*cdf0e10cSrcweir 				if( !xScriptPackage.is() )
3279*cdf0e10cSrcweir 					break;
3280*cdf0e10cSrcweir 
3281*cdf0e10cSrcweir 				aRetLib = xScriptPackage->getURL();
3282*cdf0e10cSrcweir 				break;
3283*cdf0e10cSrcweir 			}
3284*cdf0e10cSrcweir 			case BUNDLED_EXTENSIONS:
3285*cdf0e10cSrcweir 			{
3286*cdf0e10cSrcweir 				Reference< deployment::XPackage > xScriptPackage =
3287*cdf0e10cSrcweir 					implGetNextBundledScriptPackage( rbPureDialogLib );
3288*cdf0e10cSrcweir 				if( !xScriptPackage.is() )
3289*cdf0e10cSrcweir 					break;
3290*cdf0e10cSrcweir 
3291*cdf0e10cSrcweir 				aRetLib = xScriptPackage->getURL();
3292*cdf0e10cSrcweir 				break;
3293*cdf0e10cSrcweir 			}
3294*cdf0e10cSrcweir             case END_REACHED:
3295*cdf0e10cSrcweir 				VOS_ENSURE( false, "ScriptExtensionIterator::nextBasicOrDialogLibrary(): Invalid case END_REACHED" );
3296*cdf0e10cSrcweir 				break;
3297*cdf0e10cSrcweir 		}
3298*cdf0e10cSrcweir 	}
3299*cdf0e10cSrcweir 
3300*cdf0e10cSrcweir 	return aRetLib;
3301*cdf0e10cSrcweir }
3302*cdf0e10cSrcweir 
3303*cdf0e10cSrcweir ScriptSubPackageIterator::ScriptSubPackageIterator( Reference< deployment::XPackage > xMainPackage )
3304*cdf0e10cSrcweir 	: m_xMainPackage( xMainPackage )
3305*cdf0e10cSrcweir 	, m_bIsValid( false )
3306*cdf0e10cSrcweir 	, m_bIsBundle( false )
3307*cdf0e10cSrcweir 	, m_nSubPkgCount( 0 )
3308*cdf0e10cSrcweir 	, m_iNextSubPkg( 0 )
3309*cdf0e10cSrcweir {
3310*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3311*cdf0e10cSrcweir 	if( !m_xMainPackage.is() )
3312*cdf0e10cSrcweir 		return;
3313*cdf0e10cSrcweir 
3314*cdf0e10cSrcweir 	// Check if parent package is registered
3315*cdf0e10cSrcweir     beans::Optional< beans::Ambiguous<sal_Bool> > option( m_xMainPackage->isRegistered
3316*cdf0e10cSrcweir 		( Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>() ) );
3317*cdf0e10cSrcweir 	bool bRegistered = false;
3318*cdf0e10cSrcweir     if( option.IsPresent )
3319*cdf0e10cSrcweir     {
3320*cdf0e10cSrcweir         beans::Ambiguous<sal_Bool> const & reg = option.Value;
3321*cdf0e10cSrcweir         if( !reg.IsAmbiguous && reg.Value )
3322*cdf0e10cSrcweir 			bRegistered = true;
3323*cdf0e10cSrcweir     }
3324*cdf0e10cSrcweir 	if( bRegistered )
3325*cdf0e10cSrcweir 	{
3326*cdf0e10cSrcweir 		m_bIsValid = true;
3327*cdf0e10cSrcweir 		if( m_xMainPackage->isBundle() )
3328*cdf0e10cSrcweir 		{
3329*cdf0e10cSrcweir 			m_bIsBundle = true;
3330*cdf0e10cSrcweir 			m_aSubPkgSeq = m_xMainPackage->getBundle
3331*cdf0e10cSrcweir 				( Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>() );
3332*cdf0e10cSrcweir 			m_nSubPkgCount = m_aSubPkgSeq.getLength();
3333*cdf0e10cSrcweir 		}
3334*cdf0e10cSrcweir 	}
3335*cdf0e10cSrcweir }
3336*cdf0e10cSrcweir 
3337*cdf0e10cSrcweir Reference< deployment::XPackage > ScriptSubPackageIterator::getNextScriptSubPackage
3338*cdf0e10cSrcweir 	( bool& rbPureDialogLib )
3339*cdf0e10cSrcweir {
3340*cdf0e10cSrcweir 	rbPureDialogLib = false;
3341*cdf0e10cSrcweir 
3342*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3343*cdf0e10cSrcweir 	if( !m_bIsValid )
3344*cdf0e10cSrcweir 		return xScriptPackage;
3345*cdf0e10cSrcweir 
3346*cdf0e10cSrcweir 	if( m_bIsBundle )
3347*cdf0e10cSrcweir 	{
3348*cdf0e10cSrcweir 		const Reference< deployment::XPackage >* pSeq = m_aSubPkgSeq.getConstArray();
3349*cdf0e10cSrcweir 		sal_Int32 iPkg;
3350*cdf0e10cSrcweir 		for( iPkg = m_iNextSubPkg ; iPkg < m_nSubPkgCount ; ++iPkg )
3351*cdf0e10cSrcweir 		{
3352*cdf0e10cSrcweir 			const Reference< deployment::XPackage > xSubPkg = pSeq[ iPkg ];
3353*cdf0e10cSrcweir 			xScriptPackage = implDetectScriptPackage( xSubPkg, rbPureDialogLib );
3354*cdf0e10cSrcweir 			if( xScriptPackage.is() )
3355*cdf0e10cSrcweir 				break;
3356*cdf0e10cSrcweir 		}
3357*cdf0e10cSrcweir 		m_iNextSubPkg = iPkg + 1;
3358*cdf0e10cSrcweir 	}
3359*cdf0e10cSrcweir 	else
3360*cdf0e10cSrcweir 	{
3361*cdf0e10cSrcweir 		xScriptPackage = implDetectScriptPackage( m_xMainPackage, rbPureDialogLib );
3362*cdf0e10cSrcweir 		m_bIsValid = false;		// No more script packages
3363*cdf0e10cSrcweir 	}
3364*cdf0e10cSrcweir 
3365*cdf0e10cSrcweir 	return xScriptPackage;
3366*cdf0e10cSrcweir }
3367*cdf0e10cSrcweir 
3368*cdf0e10cSrcweir Reference< deployment::XPackage > ScriptSubPackageIterator::implDetectScriptPackage
3369*cdf0e10cSrcweir 	( const Reference< deployment::XPackage > xPackage, bool& rbPureDialogLib )
3370*cdf0e10cSrcweir {
3371*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3372*cdf0e10cSrcweir 
3373*cdf0e10cSrcweir 	if( xPackage.is() )
3374*cdf0e10cSrcweir 	{
3375*cdf0e10cSrcweir 		const Reference< deployment::XPackageTypeInfo > xPackageTypeInfo = xPackage->getPackageType();
3376*cdf0e10cSrcweir 		rtl::OUString aMediaType = xPackageTypeInfo->getMediaType();
3377*cdf0e10cSrcweir 		if( aMediaType.equals( aBasicLibMediaType ) )
3378*cdf0e10cSrcweir 		{
3379*cdf0e10cSrcweir 			xScriptPackage = xPackage;
3380*cdf0e10cSrcweir 		}
3381*cdf0e10cSrcweir 		else if( aMediaType.equals( aDialogLibMediaType ) )
3382*cdf0e10cSrcweir 		{
3383*cdf0e10cSrcweir 			rbPureDialogLib = true;
3384*cdf0e10cSrcweir 			xScriptPackage = xPackage;
3385*cdf0e10cSrcweir 		}
3386*cdf0e10cSrcweir 	}
3387*cdf0e10cSrcweir 
3388*cdf0e10cSrcweir 	return xScriptPackage;
3389*cdf0e10cSrcweir }
3390*cdf0e10cSrcweir 
3391*cdf0e10cSrcweir Reference< deployment::XPackage > ScriptExtensionIterator::implGetScriptPackageFromPackage
3392*cdf0e10cSrcweir 	( const Reference< deployment::XPackage > xPackage, bool& rbPureDialogLib )
3393*cdf0e10cSrcweir {
3394*cdf0e10cSrcweir 	rbPureDialogLib = false;
3395*cdf0e10cSrcweir 
3396*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3397*cdf0e10cSrcweir 	if( !xPackage.is() )
3398*cdf0e10cSrcweir 		return xScriptPackage;
3399*cdf0e10cSrcweir 
3400*cdf0e10cSrcweir 	// Check if parent package is registered
3401*cdf0e10cSrcweir     beans::Optional< beans::Ambiguous<sal_Bool> > option( xPackage->isRegistered
3402*cdf0e10cSrcweir 		( Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>() ) );
3403*cdf0e10cSrcweir 	bool bRegistered = false;
3404*cdf0e10cSrcweir     if( option.IsPresent )
3405*cdf0e10cSrcweir     {
3406*cdf0e10cSrcweir         beans::Ambiguous<sal_Bool> const & reg = option.Value;
3407*cdf0e10cSrcweir         if( !reg.IsAmbiguous && reg.Value )
3408*cdf0e10cSrcweir 			bRegistered = true;
3409*cdf0e10cSrcweir     }
3410*cdf0e10cSrcweir 	if( bRegistered )
3411*cdf0e10cSrcweir 	{
3412*cdf0e10cSrcweir 		if( xPackage->isBundle() )
3413*cdf0e10cSrcweir 		{
3414*cdf0e10cSrcweir 			Sequence< Reference< deployment::XPackage > > aPkgSeq = xPackage->getBundle
3415*cdf0e10cSrcweir 				( Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>() );
3416*cdf0e10cSrcweir 			sal_Int32 nPkgCount = aPkgSeq.getLength();
3417*cdf0e10cSrcweir 			const Reference< deployment::XPackage >* pSeq = aPkgSeq.getConstArray();
3418*cdf0e10cSrcweir 			for( sal_Int32 iPkg = 0 ; iPkg < nPkgCount ; ++iPkg )
3419*cdf0e10cSrcweir 			{
3420*cdf0e10cSrcweir 				const Reference< deployment::XPackage > xSubPkg = pSeq[ iPkg ];
3421*cdf0e10cSrcweir 				const Reference< deployment::XPackageTypeInfo > xPackageTypeInfo = xSubPkg->getPackageType();
3422*cdf0e10cSrcweir 				rtl::OUString aMediaType = xPackageTypeInfo->getMediaType();
3423*cdf0e10cSrcweir 				if( aMediaType.equals( aBasicLibMediaType ) )
3424*cdf0e10cSrcweir 				{
3425*cdf0e10cSrcweir 					xScriptPackage = xSubPkg;
3426*cdf0e10cSrcweir 					break;
3427*cdf0e10cSrcweir 				}
3428*cdf0e10cSrcweir 				else if( aMediaType.equals( aDialogLibMediaType ) )
3429*cdf0e10cSrcweir 				{
3430*cdf0e10cSrcweir 					rbPureDialogLib = true;
3431*cdf0e10cSrcweir 					xScriptPackage = xSubPkg;
3432*cdf0e10cSrcweir 					break;
3433*cdf0e10cSrcweir 				}
3434*cdf0e10cSrcweir 			}
3435*cdf0e10cSrcweir 		}
3436*cdf0e10cSrcweir 		else
3437*cdf0e10cSrcweir 		{
3438*cdf0e10cSrcweir 			const Reference< deployment::XPackageTypeInfo > xPackageTypeInfo = xPackage->getPackageType();
3439*cdf0e10cSrcweir 			rtl::OUString aMediaType = xPackageTypeInfo->getMediaType();
3440*cdf0e10cSrcweir 			if( aMediaType.equals( aBasicLibMediaType ) )
3441*cdf0e10cSrcweir 			{
3442*cdf0e10cSrcweir 				xScriptPackage = xPackage;
3443*cdf0e10cSrcweir 			}
3444*cdf0e10cSrcweir 			else if( aMediaType.equals( aDialogLibMediaType ) )
3445*cdf0e10cSrcweir 			{
3446*cdf0e10cSrcweir 				rbPureDialogLib = true;
3447*cdf0e10cSrcweir 				xScriptPackage = xPackage;
3448*cdf0e10cSrcweir 			}
3449*cdf0e10cSrcweir 		}
3450*cdf0e10cSrcweir 	}
3451*cdf0e10cSrcweir 
3452*cdf0e10cSrcweir 	return xScriptPackage;
3453*cdf0e10cSrcweir }
3454*cdf0e10cSrcweir 
3455*cdf0e10cSrcweir Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextUserScriptPackage
3456*cdf0e10cSrcweir 	( bool& rbPureDialogLib )
3457*cdf0e10cSrcweir {
3458*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3459*cdf0e10cSrcweir 
3460*cdf0e10cSrcweir 	if( !m_bUserPackagesLoaded )
3461*cdf0e10cSrcweir 	{
3462*cdf0e10cSrcweir 		try
3463*cdf0e10cSrcweir 		{
3464*cdf0e10cSrcweir 			Reference< XExtensionManager > xManager =
3465*cdf0e10cSrcweir 				ExtensionManager::get( m_xContext );
3466*cdf0e10cSrcweir 			m_aUserPackagesSeq = xManager->getDeployedExtensions
3467*cdf0e10cSrcweir 				(rtl::OUString::createFromAscii("user"),
3468*cdf0e10cSrcweir                  Reference< task::XAbortChannel >(), Reference< ucb::XCommandEnvironment >() );
3469*cdf0e10cSrcweir 		}
3470*cdf0e10cSrcweir 		catch( com::sun::star::uno::DeploymentException& )
3471*cdf0e10cSrcweir 		{
3472*cdf0e10cSrcweir 			// Special Office installations may not contain deployment code
3473*cdf0e10cSrcweir 			m_eState = END_REACHED;
3474*cdf0e10cSrcweir 			return xScriptPackage;
3475*cdf0e10cSrcweir 		}
3476*cdf0e10cSrcweir 
3477*cdf0e10cSrcweir 		m_bUserPackagesLoaded = true;
3478*cdf0e10cSrcweir 	}
3479*cdf0e10cSrcweir 
3480*cdf0e10cSrcweir 	if( m_iUserPackage == m_aUserPackagesSeq.getLength() )
3481*cdf0e10cSrcweir 	{
3482*cdf0e10cSrcweir 		m_eState = SHARED_EXTENSIONS;		// Later: SHARED_MODULE
3483*cdf0e10cSrcweir 	}
3484*cdf0e10cSrcweir 	else
3485*cdf0e10cSrcweir 	{
3486*cdf0e10cSrcweir 		if( m_pScriptSubPackageIterator == NULL )
3487*cdf0e10cSrcweir 		{
3488*cdf0e10cSrcweir 			const Reference< deployment::XPackage >* pUserPackages = m_aUserPackagesSeq.getConstArray();
3489*cdf0e10cSrcweir 			Reference< deployment::XPackage > xPackage = pUserPackages[ m_iUserPackage ];
3490*cdf0e10cSrcweir 			VOS_ENSURE( xPackage.is(), "ScriptExtensionIterator::implGetNextUserScriptPackage(): Invalid package" );
3491*cdf0e10cSrcweir 			m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
3492*cdf0e10cSrcweir 		}
3493*cdf0e10cSrcweir 
3494*cdf0e10cSrcweir 		if( m_pScriptSubPackageIterator != NULL )
3495*cdf0e10cSrcweir 		{
3496*cdf0e10cSrcweir 			xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
3497*cdf0e10cSrcweir 			if( !xScriptPackage.is() )
3498*cdf0e10cSrcweir 			{
3499*cdf0e10cSrcweir 				delete m_pScriptSubPackageIterator;
3500*cdf0e10cSrcweir 				m_pScriptSubPackageIterator = NULL;
3501*cdf0e10cSrcweir 				m_iUserPackage++;
3502*cdf0e10cSrcweir 			}
3503*cdf0e10cSrcweir 		}
3504*cdf0e10cSrcweir 	}
3505*cdf0e10cSrcweir 
3506*cdf0e10cSrcweir 	return xScriptPackage;
3507*cdf0e10cSrcweir }
3508*cdf0e10cSrcweir 
3509*cdf0e10cSrcweir Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextSharedScriptPackage
3510*cdf0e10cSrcweir 	( bool& rbPureDialogLib )
3511*cdf0e10cSrcweir {
3512*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3513*cdf0e10cSrcweir 
3514*cdf0e10cSrcweir 	if( !m_bSharedPackagesLoaded )
3515*cdf0e10cSrcweir 	{
3516*cdf0e10cSrcweir 		try
3517*cdf0e10cSrcweir 		{
3518*cdf0e10cSrcweir 			Reference< XExtensionManager > xSharedManager =
3519*cdf0e10cSrcweir 				ExtensionManager::get( m_xContext );
3520*cdf0e10cSrcweir 			m_aSharedPackagesSeq = xSharedManager->getDeployedExtensions
3521*cdf0e10cSrcweir 				(rtl::OUString::createFromAscii("shared"),
3522*cdf0e10cSrcweir                  Reference< task::XAbortChannel >(), Reference< ucb::XCommandEnvironment >() );
3523*cdf0e10cSrcweir 		}
3524*cdf0e10cSrcweir 		catch( com::sun::star::uno::DeploymentException& )
3525*cdf0e10cSrcweir 		{
3526*cdf0e10cSrcweir 			// Special Office installations may not contain deployment code
3527*cdf0e10cSrcweir 			return xScriptPackage;
3528*cdf0e10cSrcweir 		}
3529*cdf0e10cSrcweir 
3530*cdf0e10cSrcweir 		m_bSharedPackagesLoaded = true;
3531*cdf0e10cSrcweir 	}
3532*cdf0e10cSrcweir 
3533*cdf0e10cSrcweir 	if( m_iSharedPackage == m_aSharedPackagesSeq.getLength() )
3534*cdf0e10cSrcweir 	{
3535*cdf0e10cSrcweir 		m_eState = BUNDLED_EXTENSIONS;
3536*cdf0e10cSrcweir 	}
3537*cdf0e10cSrcweir 	else
3538*cdf0e10cSrcweir 	{
3539*cdf0e10cSrcweir 		if( m_pScriptSubPackageIterator == NULL )
3540*cdf0e10cSrcweir 		{
3541*cdf0e10cSrcweir 			const Reference< deployment::XPackage >* pSharedPackages = m_aSharedPackagesSeq.getConstArray();
3542*cdf0e10cSrcweir 			Reference< deployment::XPackage > xPackage = pSharedPackages[ m_iSharedPackage ];
3543*cdf0e10cSrcweir 			VOS_ENSURE( xPackage.is(), "ScriptExtensionIterator::implGetNextSharedScriptPackage(): Invalid package" );
3544*cdf0e10cSrcweir 			m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
3545*cdf0e10cSrcweir 		}
3546*cdf0e10cSrcweir 
3547*cdf0e10cSrcweir 		if( m_pScriptSubPackageIterator != NULL )
3548*cdf0e10cSrcweir 		{
3549*cdf0e10cSrcweir 			xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
3550*cdf0e10cSrcweir 			if( !xScriptPackage.is() )
3551*cdf0e10cSrcweir 			{
3552*cdf0e10cSrcweir 				delete m_pScriptSubPackageIterator;
3553*cdf0e10cSrcweir 				m_pScriptSubPackageIterator = NULL;
3554*cdf0e10cSrcweir 				m_iSharedPackage++;
3555*cdf0e10cSrcweir 			}
3556*cdf0e10cSrcweir 		}
3557*cdf0e10cSrcweir 	}
3558*cdf0e10cSrcweir 
3559*cdf0e10cSrcweir 	return xScriptPackage;
3560*cdf0e10cSrcweir }
3561*cdf0e10cSrcweir 
3562*cdf0e10cSrcweir Reference< deployment::XPackage > ScriptExtensionIterator::implGetNextBundledScriptPackage
3563*cdf0e10cSrcweir 	( bool& rbPureDialogLib )
3564*cdf0e10cSrcweir {
3565*cdf0e10cSrcweir 	Reference< deployment::XPackage > xScriptPackage;
3566*cdf0e10cSrcweir 
3567*cdf0e10cSrcweir 	if( !m_bBundledPackagesLoaded )
3568*cdf0e10cSrcweir 	{
3569*cdf0e10cSrcweir 		try
3570*cdf0e10cSrcweir 		{
3571*cdf0e10cSrcweir 			Reference< XExtensionManager > xManager =
3572*cdf0e10cSrcweir 				ExtensionManager::get( m_xContext );
3573*cdf0e10cSrcweir 			m_aBundledPackagesSeq = xManager->getDeployedExtensions
3574*cdf0e10cSrcweir 				(rtl::OUString::createFromAscii("bundled"),
3575*cdf0e10cSrcweir                  Reference< task::XAbortChannel >(), Reference< ucb::XCommandEnvironment >() );
3576*cdf0e10cSrcweir 		}
3577*cdf0e10cSrcweir 		catch( com::sun::star::uno::DeploymentException& )
3578*cdf0e10cSrcweir 		{
3579*cdf0e10cSrcweir 			// Special Office installations may not contain deployment code
3580*cdf0e10cSrcweir 			return xScriptPackage;
3581*cdf0e10cSrcweir 		}
3582*cdf0e10cSrcweir 
3583*cdf0e10cSrcweir 		m_bBundledPackagesLoaded = true;
3584*cdf0e10cSrcweir 	}
3585*cdf0e10cSrcweir 
3586*cdf0e10cSrcweir 	if( m_iBundledPackage == m_aBundledPackagesSeq.getLength() )
3587*cdf0e10cSrcweir 	{
3588*cdf0e10cSrcweir 		m_eState = END_REACHED;
3589*cdf0e10cSrcweir 	}
3590*cdf0e10cSrcweir 	else
3591*cdf0e10cSrcweir 	{
3592*cdf0e10cSrcweir 		if( m_pScriptSubPackageIterator == NULL )
3593*cdf0e10cSrcweir 		{
3594*cdf0e10cSrcweir 			const Reference< deployment::XPackage >* pBundledPackages = m_aBundledPackagesSeq.getConstArray();
3595*cdf0e10cSrcweir 			Reference< deployment::XPackage > xPackage = pBundledPackages[ m_iBundledPackage ];
3596*cdf0e10cSrcweir 			VOS_ENSURE( xPackage.is(), "ScriptExtensionIterator::implGetNextBundledScriptPackage(): Invalid package" );
3597*cdf0e10cSrcweir 			m_pScriptSubPackageIterator = new ScriptSubPackageIterator( xPackage );
3598*cdf0e10cSrcweir 		}
3599*cdf0e10cSrcweir 
3600*cdf0e10cSrcweir 		if( m_pScriptSubPackageIterator != NULL )
3601*cdf0e10cSrcweir 		{
3602*cdf0e10cSrcweir 			xScriptPackage = m_pScriptSubPackageIterator->getNextScriptSubPackage( rbPureDialogLib );
3603*cdf0e10cSrcweir 			if( !xScriptPackage.is() )
3604*cdf0e10cSrcweir 			{
3605*cdf0e10cSrcweir 				delete m_pScriptSubPackageIterator;
3606*cdf0e10cSrcweir 				m_pScriptSubPackageIterator = NULL;
3607*cdf0e10cSrcweir 				m_iBundledPackage++;
3608*cdf0e10cSrcweir 			}
3609*cdf0e10cSrcweir 		}
3610*cdf0e10cSrcweir 	}
3611*cdf0e10cSrcweir 
3612*cdf0e10cSrcweir 	return xScriptPackage;
3613*cdf0e10cSrcweir }
3614*cdf0e10cSrcweir 
3615*cdf0e10cSrcweir }   // namespace basic
3616