xref: /trunk/main/basic/source/uno/dlgcont.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
41*cdf0e10cSrcweir #include "com/sun/star/resource/XStringResourceWithStorage.hpp"
42*cdf0e10cSrcweir #include "com/sun/star/resource/XStringResourceWithLocation.hpp"
43*cdf0e10cSrcweir #include "dlgcont.hxx"
44*cdf0e10cSrcweir #include "sbmodule.hxx"
45*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
46*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
47*cdf0e10cSrcweir #include <osl/mutex.hxx>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <vcl/svapp.hxx>
50*cdf0e10cSrcweir #include <vcl/settings.hxx>
51*cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
52*cdf0e10cSrcweir #include <xmlscript/xmldlg_imexp.hxx>
53*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
54*cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
55*cdf0e10cSrcweir #include <svtools/ehdl.hxx>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace basic
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir using namespace com::sun::star::document;
62*cdf0e10cSrcweir using namespace com::sun::star::container;
63*cdf0e10cSrcweir using namespace com::sun::star::io;
64*cdf0e10cSrcweir using namespace com::sun::star::uno;
65*cdf0e10cSrcweir using namespace com::sun::star::ucb;
66*cdf0e10cSrcweir using namespace com::sun::star::lang;
67*cdf0e10cSrcweir using namespace com::sun::star::script;
68*cdf0e10cSrcweir using namespace com::sun::star::xml::sax;
69*cdf0e10cSrcweir using namespace com::sun::star;
70*cdf0e10cSrcweir using namespace cppu;
71*cdf0e10cSrcweir using namespace rtl;
72*cdf0e10cSrcweir using namespace osl;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir using com::sun::star::uno::Reference;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir //============================================================================
77*cdf0e10cSrcweir // Implementation class SfxDialogLibraryContainer
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir const sal_Char* SAL_CALL SfxDialogLibraryContainer::getInfoFileName() const { return "dialog"; }
80*cdf0e10cSrcweir const sal_Char* SAL_CALL SfxDialogLibraryContainer::getOldInfoFileName() const { return "dialogs"; }
81*cdf0e10cSrcweir const sal_Char* SAL_CALL SfxDialogLibraryContainer::getLibElementFileExtension() const { return "xdl"; }
82*cdf0e10cSrcweir const sal_Char* SAL_CALL SfxDialogLibraryContainer::getLibrariesDir() const { return "Dialogs"; }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir // Ctor for service
85*cdf0e10cSrcweir SfxDialogLibraryContainer::SfxDialogLibraryContainer( void )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     // all initialisation has to be done
88*cdf0e10cSrcweir     // by calling XInitialization::initialize
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir SfxDialogLibraryContainer::SfxDialogLibraryContainer( const uno::Reference< embed::XStorage >& xStorage )
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir     init( OUString(), xStorage );
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir // Methods to get library instances of the correct type
97*cdf0e10cSrcweir SfxLibrary* SfxDialogLibraryContainer::implCreateLibrary( const ::rtl::OUString& aName )
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	SfxLibrary* pRet = new SfxDialogLibrary( maModifiable, aName, mxMSF, mxSFI, this );
100*cdf0e10cSrcweir 	return pRet;
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir SfxLibrary* SfxDialogLibraryContainer::implCreateLibraryLink
104*cdf0e10cSrcweir 	( const ::rtl::OUString& aName, const OUString& aLibInfoFileURL,
105*cdf0e10cSrcweir 	  const OUString& StorageURL, sal_Bool ReadOnly )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	SfxLibrary* pRet = new SfxDialogLibrary
108*cdf0e10cSrcweir             ( maModifiable, aName, mxMSF, mxSFI, aLibInfoFileURL, StorageURL, ReadOnly, this );
109*cdf0e10cSrcweir 	return pRet;
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir Any SAL_CALL SfxDialogLibraryContainer::createEmptyLibraryElement( void )
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir 	Reference< XInputStreamProvider > xISP;
115*cdf0e10cSrcweir 	Any aRetAny;
116*cdf0e10cSrcweir 	aRetAny <<= xISP;
117*cdf0e10cSrcweir 	return aRetAny;
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir bool SAL_CALL SfxDialogLibraryContainer::isLibraryElementValid( Any aElement ) const
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     return SfxDialogLibrary::containsValidDialog( aElement );
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir bool writeOasis2OOoLibraryElement(
126*cdf0e10cSrcweir 	Reference< XInputStream > xInput, Reference< XOutputStream > xOutput )
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xMSF(
129*cdf0e10cSrcweir 		comphelper::getProcessServiceFactory() );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     Reference< XComponentContext > xContext;
132*cdf0e10cSrcweir     Reference< beans::XPropertySet > xProps( xMSF, UNO_QUERY );
133*cdf0e10cSrcweir     OSL_ASSERT( xProps.is() );
134*cdf0e10cSrcweir     OSL_VERIFY( xProps->getPropertyValue(
135*cdf0e10cSrcweir 		OUString::createFromAscii(("DefaultContext")) ) >>= xContext );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	Reference< lang::XMultiComponentFactory > xSMgr(
138*cdf0e10cSrcweir 		xContext->getServiceManager() );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	if (! xSMgr.is())
141*cdf0e10cSrcweir 	{
142*cdf0e10cSrcweir 		return sal_False;
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	Reference< xml::sax::XParser > xParser(
146*cdf0e10cSrcweir 		xSMgr->createInstanceWithContext(
147*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
148*cdf0e10cSrcweir 				"com.sun.star.xml.sax.Parser" ) ),
149*cdf0e10cSrcweir 			xContext ),
150*cdf0e10cSrcweir 		UNO_QUERY );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	Reference< xml::sax::XExtendedDocumentHandler > xWriter(
153*cdf0e10cSrcweir 		xSMgr->createInstanceWithContext(
154*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
155*cdf0e10cSrcweir 				"com.sun.star.xml.sax.Writer" ) ),
156*cdf0e10cSrcweir 			xContext ),
157*cdf0e10cSrcweir 		UNO_QUERY );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	Reference< io::XActiveDataSource > xSource( xWriter, UNO_QUERY );
160*cdf0e10cSrcweir 	xSource->setOutputStream( xOutput );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	if ( !xParser.is() || !xWriter.is() )
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir 		return sal_False;
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     Sequence<Any> aArgs( 1 );
168*cdf0e10cSrcweir     aArgs[0] <<= xWriter;
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     Reference< xml::sax::XDocumentHandler > xHandler(
171*cdf0e10cSrcweir         xSMgr->createInstanceWithArgumentsAndContext(
172*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
173*cdf0e10cSrcweir                 "com.sun.star.comp.Oasis2OOoTransformer" ) ),
174*cdf0e10cSrcweir             aArgs, xContext ),
175*cdf0e10cSrcweir         UNO_QUERY );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	xParser->setDocumentHandler( xHandler );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	xml::sax::InputSource source;
180*cdf0e10cSrcweir 	source.aInputStream = xInput;
181*cdf0e10cSrcweir 	source.sSystemId = OUString::createFromAscii( "virtual file" );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	xParser->parseStream( source );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	return sal_True;
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir void SAL_CALL SfxDialogLibraryContainer::writeLibraryElement
189*cdf0e10cSrcweir (
190*cdf0e10cSrcweir 	const Reference < XNameContainer >& xLib,
191*cdf0e10cSrcweir 	const OUString& aElementName,
192*cdf0e10cSrcweir 	const Reference< XOutputStream >& xOutput
193*cdf0e10cSrcweir )
194*cdf0e10cSrcweir 	throw(Exception)
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir 	Any aElement = xLib->getByName( aElementName );
197*cdf0e10cSrcweir 	Reference< XInputStreamProvider > xISP;
198*cdf0e10cSrcweir 	aElement >>= xISP;
199*cdf0e10cSrcweir 	if( !xISP.is() )
200*cdf0e10cSrcweir 		return;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	Reference< XInputStream > xInput( xISP->createInputStream() );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	bool bComplete = sal_False;
205*cdf0e10cSrcweir 	if ( mbOasis2OOoFormat )
206*cdf0e10cSrcweir 	{
207*cdf0e10cSrcweir 		bComplete = writeOasis2OOoLibraryElement( xInput, xOutput );
208*cdf0e10cSrcweir 	}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	if ( bComplete == sal_False )
211*cdf0e10cSrcweir 	{
212*cdf0e10cSrcweir 		Sequence< sal_Int8 > bytes;
213*cdf0e10cSrcweir 		sal_Int32 nRead = xInput->readBytes( bytes, xInput->available() );
214*cdf0e10cSrcweir 		for (;;)
215*cdf0e10cSrcweir 		{
216*cdf0e10cSrcweir 			if( nRead )
217*cdf0e10cSrcweir 				xOutput->writeBytes( bytes );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 			nRead = xInput->readBytes( bytes, 1024 );
220*cdf0e10cSrcweir 			if (! nRead)
221*cdf0e10cSrcweir 				break;
222*cdf0e10cSrcweir 		}
223*cdf0e10cSrcweir 	}
224*cdf0e10cSrcweir 	xInput->closeInput();
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir void SfxDialogLibraryContainer::storeLibrariesToStorage( const uno::Reference< embed::XStorage >& xStorage ) throw ( RuntimeException )
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     LibraryContainerMethodGuard aGuard( *this );
230*cdf0e10cSrcweir 	mbOasis2OOoFormat = sal_False;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	if ( mxStorage.is() && xStorage.is() )
233*cdf0e10cSrcweir 	{
234*cdf0e10cSrcweir 		try
235*cdf0e10cSrcweir 		{
236*cdf0e10cSrcweir 			long nSource = SotStorage::GetVersion( mxStorage );
237*cdf0e10cSrcweir 			long nTarget = SotStorage::GetVersion( xStorage );
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 			if ( nSource == SOFFICE_FILEFORMAT_CURRENT &&
240*cdf0e10cSrcweir 				nTarget != SOFFICE_FILEFORMAT_CURRENT )
241*cdf0e10cSrcweir 			{
242*cdf0e10cSrcweir 				mbOasis2OOoFormat = sal_True;
243*cdf0e10cSrcweir 			}
244*cdf0e10cSrcweir 		}
245*cdf0e10cSrcweir 		catch ( Exception& )
246*cdf0e10cSrcweir 		{
247*cdf0e10cSrcweir 			// if we cannot get the version then the
248*cdf0e10cSrcweir 			// Oasis2OOoTransformer will not be used
249*cdf0e10cSrcweir 			OSL_ASSERT(sal_False);
250*cdf0e10cSrcweir 		}
251*cdf0e10cSrcweir 	}
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 	SfxLibraryContainer::storeLibrariesToStorage( xStorage );
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 	mbOasis2OOoFormat = sal_False;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir Any SAL_CALL SfxDialogLibraryContainer::importLibraryElement
260*cdf0e10cSrcweir 	( const Reference < XNameContainer >& /*xLib*/,
261*cdf0e10cSrcweir 	  const OUString& /*aElementName */, const OUString& aFile,
262*cdf0e10cSrcweir 	  const uno::Reference< io::XInputStream >& xElementStream )
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir 	Any aRetAny;
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 	// TODO: Member because later it will be a component
267*cdf0e10cSrcweir 	//Reference< XMultiServiceFactory > xMSF( comphelper::getProcessServiceFactory() );
268*cdf0e10cSrcweir 	//if( !xMSF.is() )
269*cdf0e10cSrcweir 	//{
270*cdf0e10cSrcweir 	//	OSL_ENSURE( 0, "### couln't get ProcessServiceFactory\n" );
271*cdf0e10cSrcweir 	//	return aRetAny;
272*cdf0e10cSrcweir 	//}
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 	Reference< XParser > xParser( mxMSF->createInstance(
275*cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser") ) ), UNO_QUERY );
276*cdf0e10cSrcweir 	if( !xParser.is() )
277*cdf0e10cSrcweir 	{
278*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create sax parser component\n" );
279*cdf0e10cSrcweir 		return aRetAny;
280*cdf0e10cSrcweir 	}
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 	Reference< XNameContainer > xDialogModel( mxMSF->createInstance
283*cdf0e10cSrcweir 		( OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ) ), UNO_QUERY );
284*cdf0e10cSrcweir 	if( !xDialogModel.is() )
285*cdf0e10cSrcweir 	{
286*cdf0e10cSrcweir 		OSL_ENSURE( 0, "### couln't create com.sun.star.awt.UnoControlDialogModel component\n" );
287*cdf0e10cSrcweir 		return aRetAny;
288*cdf0e10cSrcweir 	}
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	// Read from storage?
291*cdf0e10cSrcweir 	sal_Bool bStorage = xElementStream.is();
292*cdf0e10cSrcweir 	Reference< XInputStream > xInput;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	if( bStorage )
295*cdf0e10cSrcweir 	{
296*cdf0e10cSrcweir 		xInput = xElementStream;
297*cdf0e10cSrcweir 	}
298*cdf0e10cSrcweir 	else
299*cdf0e10cSrcweir 	{
300*cdf0e10cSrcweir 		try
301*cdf0e10cSrcweir 		{
302*cdf0e10cSrcweir 			xInput = mxSFI->openFileRead( aFile );
303*cdf0e10cSrcweir 		}
304*cdf0e10cSrcweir 		catch( Exception& )
305*cdf0e10cSrcweir 		//catch( Exception& e )
306*cdf0e10cSrcweir 		{
307*cdf0e10cSrcweir 			// TODO:
308*cdf0e10cSrcweir 			//throw WrappedTargetException( e );
309*cdf0e10cSrcweir 		}
310*cdf0e10cSrcweir 	}
311*cdf0e10cSrcweir 	if( !xInput.is() )
312*cdf0e10cSrcweir 		return aRetAny;
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir     Reference< XComponentContext > xContext;
315*cdf0e10cSrcweir     Reference< beans::XPropertySet > xProps( mxMSF, UNO_QUERY );
316*cdf0e10cSrcweir     OSL_ASSERT( xProps.is() );
317*cdf0e10cSrcweir     OSL_VERIFY( xProps->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")) ) >>= xContext );
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir 	InputSource source;
320*cdf0e10cSrcweir 	source.aInputStream = xInput;
321*cdf0e10cSrcweir 	source.sSystemId 	= aFile;
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 	try {
324*cdf0e10cSrcweir 		// start parsing
325*cdf0e10cSrcweir 		xParser->setDocumentHandler( ::xmlscript::importDialogModel( xDialogModel, xContext ) );
326*cdf0e10cSrcweir 		xParser->parseStream( source );
327*cdf0e10cSrcweir 	}
328*cdf0e10cSrcweir 	catch( Exception& )
329*cdf0e10cSrcweir 	{
330*cdf0e10cSrcweir 		OSL_ENSURE( 0, "Parsing error\n" );
331*cdf0e10cSrcweir 		SfxErrorContext aEc( ERRCTX_SFX_LOADBASIC, aFile );
332*cdf0e10cSrcweir         sal_uIntPtr nErrorCode = ERRCODE_IO_GENERAL;
333*cdf0e10cSrcweir         ErrorHandler::HandleError( nErrorCode );
334*cdf0e10cSrcweir 		return aRetAny;
335*cdf0e10cSrcweir 	}
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir 	// Create InputStream, TODO: Implement own InputStreamProvider
338*cdf0e10cSrcweir 	// to avoid creating the DialogModel here!
339*cdf0e10cSrcweir 	Reference< XInputStreamProvider > xISP = ::xmlscript::exportDialogModel( xDialogModel, xContext );
340*cdf0e10cSrcweir 	aRetAny <<= xISP;
341*cdf0e10cSrcweir 	return aRetAny;
342*cdf0e10cSrcweir }
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir void SAL_CALL SfxDialogLibraryContainer::importFromOldStorage( const OUString& )
345*cdf0e10cSrcweir {
346*cdf0e10cSrcweir     // Nothing to do here, old dialogs cannot be imported
347*cdf0e10cSrcweir }
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir SfxLibraryContainer* SfxDialogLibraryContainer::createInstanceImpl( void )
350*cdf0e10cSrcweir {
351*cdf0e10cSrcweir     return new SfxDialogLibraryContainer();
352*cdf0e10cSrcweir }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir static OUString aResourceFileNameBase = OUString::createFromAscii( "DialogStrings" );
356*cdf0e10cSrcweir static OUString aResourceFileCommentBase = OUString::createFromAscii( "# Strings for Dialog Library " );
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir // Resource handling
359*cdf0e10cSrcweir Reference< ::com::sun::star::resource::XStringResourcePersistence >
360*cdf0e10cSrcweir 	SfxDialogLibraryContainer::implCreateStringResource( SfxDialogLibrary* pDialogLibrary )
361*cdf0e10cSrcweir {
362*cdf0e10cSrcweir 	Reference< resource::XStringResourcePersistence > xRet;
363*cdf0e10cSrcweir 	OUString aLibName = pDialogLibrary->getName();
364*cdf0e10cSrcweir 	bool bReadOnly = pDialogLibrary->mbReadOnly;
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir 	// get ui locale
367*cdf0e10cSrcweir 	::com::sun	::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir 	OUString aComment = aResourceFileCommentBase;
370*cdf0e10cSrcweir 	aComment += aLibName;
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir 	sal_Bool bStorage = mxStorage.is();
373*cdf0e10cSrcweir 	if( bStorage )
374*cdf0e10cSrcweir 	{
375*cdf0e10cSrcweir 		Sequence<Any> aArgs( 5 );
376*cdf0e10cSrcweir 		aArgs[1] <<= bReadOnly;
377*cdf0e10cSrcweir 		aArgs[2] <<= aLocale;
378*cdf0e10cSrcweir 		aArgs[3] <<= aResourceFileNameBase;
379*cdf0e10cSrcweir 		aArgs[4] <<= aComment;
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir 		// TODO: Ctor
382*cdf0e10cSrcweir 		xRet = Reference< resource::XStringResourcePersistence >( mxMSF->createInstance
383*cdf0e10cSrcweir 			( OUString::createFromAscii( "com.sun.star.resource.StringResourceWithStorage" ) ), UNO_QUERY );
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 		uno::Reference< embed::XStorage > xLibrariesStor;
386*cdf0e10cSrcweir 		uno::Reference< embed::XStorage > xLibraryStor;
387*cdf0e10cSrcweir 		try {
388*cdf0e10cSrcweir 			xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READ );
389*cdf0e10cSrcweir 				// TODO: Should be READWRITE with new storage concept using store() instead of storeTo()
390*cdf0e10cSrcweir 			if ( !xLibrariesStor.is() )
391*cdf0e10cSrcweir 				throw uno::RuntimeException();
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 			xLibraryStor = xLibrariesStor->openStorageElement( aLibName, embed::ElementModes::READ );
394*cdf0e10cSrcweir 				// TODO: Should be READWRITE with new storage concept using store() instead of storeTo()
395*cdf0e10cSrcweir 			if ( !xLibraryStor.is() )
396*cdf0e10cSrcweir 				throw uno::RuntimeException();
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir 			aArgs[0] <<= xLibraryStor;
399*cdf0e10cSrcweir 		}
400*cdf0e10cSrcweir 		catch( uno::Exception& )
401*cdf0e10cSrcweir 		{
402*cdf0e10cSrcweir 			// TODO: Error handling?
403*cdf0e10cSrcweir 			return xRet;
404*cdf0e10cSrcweir 		}
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 		// TODO: Ctor
407*cdf0e10cSrcweir 		if( xRet.is() )
408*cdf0e10cSrcweir 		{
409*cdf0e10cSrcweir 			Reference< XInitialization > xInit( xRet, UNO_QUERY );
410*cdf0e10cSrcweir 			if( xInit.is() )
411*cdf0e10cSrcweir 				xInit->initialize( aArgs );
412*cdf0e10cSrcweir 		}
413*cdf0e10cSrcweir 	}
414*cdf0e10cSrcweir 	else
415*cdf0e10cSrcweir 	{
416*cdf0e10cSrcweir 		Sequence<Any> aArgs( 6 );
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 		OUString aLocation = createAppLibraryFolder( pDialogLibrary, aLibName );
419*cdf0e10cSrcweir 		aArgs[0] <<= aLocation;
420*cdf0e10cSrcweir 		aArgs[1] <<= bReadOnly;
421*cdf0e10cSrcweir 		aArgs[2] <<= aLocale;
422*cdf0e10cSrcweir 		aArgs[3] <<= aResourceFileNameBase;
423*cdf0e10cSrcweir 		aArgs[4] <<= aComment;
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir 		// TODO: Real handler?
426*cdf0e10cSrcweir 		Reference< task::XInteractionHandler > xDummyHandler;
427*cdf0e10cSrcweir 		aArgs[5] <<= xDummyHandler;
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir 		// TODO: Ctor
430*cdf0e10cSrcweir 		xRet = Reference< resource::XStringResourcePersistence >( mxMSF->createInstance
431*cdf0e10cSrcweir 			( OUString::createFromAscii( "com.sun.star.resource.StringResourceWithLocation" ) ), UNO_QUERY );
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 		// TODO: Ctor
434*cdf0e10cSrcweir 		if( xRet.is() )
435*cdf0e10cSrcweir 		{
436*cdf0e10cSrcweir 			Reference< XInitialization > xInit( xRet, UNO_QUERY );
437*cdf0e10cSrcweir 			if( xInit.is() )
438*cdf0e10cSrcweir 				xInit->initialize( aArgs );
439*cdf0e10cSrcweir 		}
440*cdf0e10cSrcweir 	}
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir 	return xRet;
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir void SfxDialogLibraryContainer::onNewRootStorage()
446*cdf0e10cSrcweir {
447*cdf0e10cSrcweir 	// the library container is not modified, go through the libraries and check whether they are modified
448*cdf0e10cSrcweir 	Sequence< OUString > aNames = maNameContainer.getElementNames();
449*cdf0e10cSrcweir 	const OUString* pNames = aNames.getConstArray();
450*cdf0e10cSrcweir 	sal_Int32 nNameCount = aNames.getLength();
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir 	for( sal_Int32 i = 0 ; i < nNameCount ; i++ )
453*cdf0e10cSrcweir 	{
454*cdf0e10cSrcweir 		OUString aName = pNames[ i ];
455*cdf0e10cSrcweir         SfxDialogLibrary* pDialogLibrary = static_cast<SfxDialogLibrary*>( getImplLib( aName ) );
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 		Reference< resource::XStringResourcePersistence > xStringResourcePersistence =
458*cdf0e10cSrcweir 			pDialogLibrary->getStringResourcePersistence();
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir 		if( xStringResourcePersistence.is() )
461*cdf0e10cSrcweir 		{
462*cdf0e10cSrcweir 			Reference< embed::XStorage > xLibrariesStor;
463*cdf0e10cSrcweir 			Reference< embed::XStorage > xLibraryStor;
464*cdf0e10cSrcweir 			try {
465*cdf0e10cSrcweir 				xLibrariesStor = mxStorage->openStorageElement( maLibrariesDir, embed::ElementModes::READWRITE );
466*cdf0e10cSrcweir 				if ( !xLibrariesStor.is() )
467*cdf0e10cSrcweir 					throw uno::RuntimeException();
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir 				OUString aLibName = pDialogLibrary->getName();
470*cdf0e10cSrcweir 				xLibraryStor = xLibrariesStor->openStorageElement( aLibName, embed::ElementModes::READWRITE );
471*cdf0e10cSrcweir 				if ( !xLibraryStor.is() )
472*cdf0e10cSrcweir 					throw uno::RuntimeException();
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir 				Reference< resource::XStringResourceWithStorage >
475*cdf0e10cSrcweir 					xStringResourceWithStorage( xStringResourcePersistence, UNO_QUERY );
476*cdf0e10cSrcweir 				if( xStringResourceWithStorage.is() )
477*cdf0e10cSrcweir 					xStringResourceWithStorage->setStorage( xLibraryStor );
478*cdf0e10cSrcweir 			}
479*cdf0e10cSrcweir 			catch( uno::Exception& )
480*cdf0e10cSrcweir 			{
481*cdf0e10cSrcweir 				// TODO: Error handling?
482*cdf0e10cSrcweir 			}
483*cdf0e10cSrcweir 		}
484*cdf0e10cSrcweir 	}
485*cdf0e10cSrcweir }
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir //============================================================================
489*cdf0e10cSrcweir // Service
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir void createRegistryInfo_SfxDialogLibraryContainer()
492*cdf0e10cSrcweir {
493*cdf0e10cSrcweir     static OAutoRegistration< SfxDialogLibraryContainer > aAutoRegistration;
494*cdf0e10cSrcweir }
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SfxDialogLibraryContainer::getImplementationName( ) throw (RuntimeException)
497*cdf0e10cSrcweir {
498*cdf0e10cSrcweir     return getImplementationName_static();
499*cdf0e10cSrcweir }
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SfxDialogLibraryContainer::getSupportedServiceNames( ) throw (RuntimeException)
502*cdf0e10cSrcweir {
503*cdf0e10cSrcweir     return getSupportedServiceNames_static();
504*cdf0e10cSrcweir }
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir Sequence< OUString > SfxDialogLibraryContainer::getSupportedServiceNames_static()
507*cdf0e10cSrcweir {
508*cdf0e10cSrcweir     Sequence< OUString > aServiceNames( 2 );
509*cdf0e10cSrcweir     aServiceNames[0] = OUString::createFromAscii( "com.sun.star.script.DocumentDialogLibraryContainer" );
510*cdf0e10cSrcweir     // plus, for compatibility:
511*cdf0e10cSrcweir     aServiceNames[1] = OUString::createFromAscii( "com.sun.star.script.DialogLibraryContainer" );
512*cdf0e10cSrcweir     return aServiceNames;
513*cdf0e10cSrcweir }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir OUString SfxDialogLibraryContainer::getImplementationName_static()
516*cdf0e10cSrcweir {
517*cdf0e10cSrcweir     static OUString aImplName;
518*cdf0e10cSrcweir     static sal_Bool bNeedsInit = sal_True;
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir 	MutexGuard aGuard( Mutex::getGlobalMutex() );
521*cdf0e10cSrcweir     if( bNeedsInit )
522*cdf0e10cSrcweir     {
523*cdf0e10cSrcweir         aImplName = OUString::createFromAscii( "com.sun.star.comp.sfx2.DialogLibraryContainer" );
524*cdf0e10cSrcweir         bNeedsInit = sal_False;
525*cdf0e10cSrcweir     }
526*cdf0e10cSrcweir     return aImplName;
527*cdf0e10cSrcweir }
528*cdf0e10cSrcweir 
529*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SfxDialogLibraryContainer::Create( const Reference< XComponentContext >& ) throw( Exception )
530*cdf0e10cSrcweir {
531*cdf0e10cSrcweir     Reference< XInterface > xRet =
532*cdf0e10cSrcweir         static_cast< XInterface* >( static_cast< OWeakObject* >(new SfxDialogLibraryContainer()) );
533*cdf0e10cSrcweir     return xRet;
534*cdf0e10cSrcweir }
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir //============================================================================
538*cdf0e10cSrcweir // Implementation class SfxDialogLibrary
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir // Ctor
541*cdf0e10cSrcweir SfxDialogLibrary::SfxDialogLibrary( ModifiableHelper& _rModifiable,
542*cdf0e10cSrcweir                                     const ::rtl::OUString& aName,
543*cdf0e10cSrcweir 								    const Reference< XMultiServiceFactory >& xMSF,
544*cdf0e10cSrcweir                                     const Reference< XSimpleFileAccess >& xSFI,
545*cdf0e10cSrcweir 									SfxDialogLibraryContainer* pParent )
546*cdf0e10cSrcweir 	: SfxLibrary( _rModifiable, getCppuType( (const Reference< XInputStreamProvider > *)0 ), xMSF, xSFI )
547*cdf0e10cSrcweir 	, m_pParent( pParent )
548*cdf0e10cSrcweir 	, m_aName( aName )
549*cdf0e10cSrcweir {
550*cdf0e10cSrcweir }
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir SfxDialogLibrary::SfxDialogLibrary( ModifiableHelper& _rModifiable,
553*cdf0e10cSrcweir                                     const ::rtl::OUString& aName,
554*cdf0e10cSrcweir 								    const Reference< XMultiServiceFactory >& xMSF,
555*cdf0e10cSrcweir                                     const Reference< XSimpleFileAccess >& xSFI,
556*cdf0e10cSrcweir                                     const OUString& aLibInfoFileURL,
557*cdf0e10cSrcweir                                     const OUString& aStorageURL,
558*cdf0e10cSrcweir                                     sal_Bool ReadOnly,
559*cdf0e10cSrcweir 									SfxDialogLibraryContainer* pParent )
560*cdf0e10cSrcweir 	: SfxLibrary( _rModifiable, getCppuType( (const Reference< XInputStreamProvider > *)0 ),
561*cdf0e10cSrcweir                        xMSF, xSFI, aLibInfoFileURL, aStorageURL, ReadOnly)
562*cdf0e10cSrcweir 	, m_pParent( pParent )
563*cdf0e10cSrcweir 	, m_aName( aName )
564*cdf0e10cSrcweir {
565*cdf0e10cSrcweir }
566*cdf0e10cSrcweir 
567*cdf0e10cSrcweir IMPLEMENT_FORWARD_XINTERFACE2( SfxDialogLibrary, SfxLibrary, SfxDialogLibrary_BASE );
568*cdf0e10cSrcweir IMPLEMENT_FORWARD_XTYPEPROVIDER2( SfxDialogLibrary, SfxLibrary, SfxDialogLibrary_BASE );
569*cdf0e10cSrcweir 
570*cdf0e10cSrcweir // Provide modify state including resources
571*cdf0e10cSrcweir sal_Bool SfxDialogLibrary::isModified( void )
572*cdf0e10cSrcweir {
573*cdf0e10cSrcweir 	sal_Bool bRet = implIsModified();
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir 	if( !bRet && m_xStringResourcePersistence.is() )
576*cdf0e10cSrcweir 		bRet = m_xStringResourcePersistence->isModified();
577*cdf0e10cSrcweir 	// else: Resources not accessed so far -> not modified
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir 	return bRet;
580*cdf0e10cSrcweir }
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir void SfxDialogLibrary::storeResources( void )
583*cdf0e10cSrcweir {
584*cdf0e10cSrcweir 	if( m_xStringResourcePersistence.is() )
585*cdf0e10cSrcweir 		m_xStringResourcePersistence->store();
586*cdf0e10cSrcweir }
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir void SfxDialogLibrary::storeResourcesAsURL
589*cdf0e10cSrcweir 	( const ::rtl::OUString& URL, const ::rtl::OUString& NewName )
590*cdf0e10cSrcweir {
591*cdf0e10cSrcweir 	OUString aComment = aResourceFileCommentBase;
592*cdf0e10cSrcweir 	m_aName = NewName;
593*cdf0e10cSrcweir 	aComment += m_aName;
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir 	if( m_xStringResourcePersistence.is() )
596*cdf0e10cSrcweir 	{
597*cdf0e10cSrcweir 		m_xStringResourcePersistence->setComment( aComment );
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir 		Reference< resource::XStringResourceWithLocation >
600*cdf0e10cSrcweir 			xStringResourceWithLocation( m_xStringResourcePersistence, UNO_QUERY );
601*cdf0e10cSrcweir 		if( xStringResourceWithLocation.is() )
602*cdf0e10cSrcweir 			xStringResourceWithLocation->storeAsURL( URL );
603*cdf0e10cSrcweir 	}
604*cdf0e10cSrcweir }
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir void SfxDialogLibrary::storeResourcesToURL( const OUString& URL,
607*cdf0e10cSrcweir 	const Reference< task::XInteractionHandler >& xHandler )
608*cdf0e10cSrcweir {
609*cdf0e10cSrcweir 	OUString aComment = aResourceFileCommentBase;
610*cdf0e10cSrcweir 	aComment += m_aName;
611*cdf0e10cSrcweir 
612*cdf0e10cSrcweir 	if( m_xStringResourcePersistence.is() )
613*cdf0e10cSrcweir 	{
614*cdf0e10cSrcweir 		m_xStringResourcePersistence->storeToURL
615*cdf0e10cSrcweir 			( URL, aResourceFileNameBase, aComment, xHandler );
616*cdf0e10cSrcweir 	}
617*cdf0e10cSrcweir }
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir void SfxDialogLibrary::storeResourcesToStorage( const ::com::sun::star::uno::Reference
620*cdf0e10cSrcweir 	< ::com::sun::star::embed::XStorage >& xStorage )
621*cdf0e10cSrcweir {
622*cdf0e10cSrcweir 	OUString aComment = aResourceFileCommentBase;
623*cdf0e10cSrcweir 	aComment += m_aName;
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir 	if( m_xStringResourcePersistence.is() )
626*cdf0e10cSrcweir 	{
627*cdf0e10cSrcweir 		m_xStringResourcePersistence->storeToStorage
628*cdf0e10cSrcweir 			( xStorage, aResourceFileNameBase, aComment );
629*cdf0e10cSrcweir 	}
630*cdf0e10cSrcweir }
631*cdf0e10cSrcweir 
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir // XStringResourceSupplier
634*cdf0e10cSrcweir Reference< resource::XStringResourceResolver >
635*cdf0e10cSrcweir 	SAL_CALL SfxDialogLibrary::getStringResource(  ) throw (RuntimeException)
636*cdf0e10cSrcweir {
637*cdf0e10cSrcweir 	if( !m_xStringResourcePersistence.is() )
638*cdf0e10cSrcweir 		m_xStringResourcePersistence = m_pParent->implCreateStringResource( this );
639*cdf0e10cSrcweir 
640*cdf0e10cSrcweir 	Reference< resource::XStringResourceResolver > xRet( m_xStringResourcePersistence, UNO_QUERY );
641*cdf0e10cSrcweir 	return xRet;
642*cdf0e10cSrcweir }
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir bool SfxDialogLibrary::containsValidDialog( const ::com::sun::star::uno::Any& aElement )
645*cdf0e10cSrcweir {
646*cdf0e10cSrcweir 	Reference< XInputStreamProvider > xISP;
647*cdf0e10cSrcweir 	aElement >>= xISP;
648*cdf0e10cSrcweir 	return xISP.is();
649*cdf0e10cSrcweir }
650*cdf0e10cSrcweir 
651*cdf0e10cSrcweir bool SAL_CALL SfxDialogLibrary::isLibraryElementValid( ::com::sun::star::uno::Any aElement ) const
652*cdf0e10cSrcweir {
653*cdf0e10cSrcweir     return SfxDialogLibrary::containsValidDialog( aElement );
654*cdf0e10cSrcweir }
655*cdf0e10cSrcweir 
656*cdf0e10cSrcweir }
657*cdf0e10cSrcweir //============================================================================
658*cdf0e10cSrcweir 
659