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