1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26 #include <osl/mutex.hxx>
27 #include <vos/mutex.hxx>
28 #include <toolkit/awt/vclxwindow.hxx>
29 
30 #include <osl/thread.h>
31 #include <cppuhelper/factory.hxx>
32 #include <cppuhelper/typeprovider.hxx>
33 #include <cppuhelper/component.hxx>
34 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35 #include <com/sun/star/frame/XDesktop.hpp>
36 #include <com/sun/star/frame/XTerminateListener.hpp>
37 #include <cppuhelper/implbase4.hxx>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/lang/XInitialization.hpp>
40 #include <com/sun/star/awt/XWindow.hpp>
41 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <toolkit/awt/vclxwindow.hxx>
44 #include <tools/resmgr.hxx>
45 #include <vcl/svapp.hxx>
46 
47 #include <svl/solar.hrc>
48 
49 #include "xmlfiltersettingsdialog.hxx"
50 
51 //using namespace ::comphelper;
52 using namespace ::rtl;
53 using namespace ::cppu;
54 using namespace ::osl;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::registry;
59 using namespace ::com::sun::star::frame;
60 
61 
62 class XMLFilterDialogComponentBase
63 {
64 protected:
65 	::osl::Mutex maMutex;
66 };
67 
68 
69 class XMLFilterDialogComponent :	public XMLFilterDialogComponentBase,
70 									public OComponentHelper,
71 									public ::com::sun::star::ui::dialogs::XExecutableDialog,
72 									public XServiceInfo,
73 									public XInitialization,
74 									public XTerminateListener
75 {
76 public:
77 	XMLFilterDialogComponent( const Reference< XMultiServiceFactory >& rxMSF );
78 	virtual ~XMLFilterDialogComponent();
79 
80 protected:
81 	// XInterface
82     virtual Any SAL_CALL queryInterface( const Type& aType ) throw (RuntimeException);
83 	virtual Any SAL_CALL queryAggregation( Type const & rType ) throw (RuntimeException);
84     virtual void SAL_CALL acquire() throw ();
85     virtual void SAL_CALL release() throw ();
86 
87 	// XTypeProvider
88 	virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() throw(RuntimeException);
89 	virtual Sequence< Type > SAL_CALL getTypes() throw (RuntimeException);
90 
91 	// XServiceInfo
92 	virtual ::rtl::OUString SAL_CALL getImplementationName() throw(com::sun::star::uno::RuntimeException);
93 	virtual sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw(RuntimeException);
94     virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
95 
96 	// XExecutableDialog
97 	virtual void SAL_CALL setTitle( const ::rtl::OUString& aTitle ) throw(RuntimeException);
98 	virtual sal_Int16 SAL_CALL execute(  ) throw(RuntimeException);
99 
100 	// XInitialization
101 	virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) throw(Exception, RuntimeException);
102 
103 	// XTerminateListener
104 	virtual void SAL_CALL queryTermination( const EventObject& Event ) throw (TerminationVetoException, RuntimeException);
105 	virtual void SAL_CALL notifyTermination( const EventObject& Event ) throw (RuntimeException);
106     virtual void SAL_CALL disposing( const EventObject& Source ) throw (RuntimeException);
107 
108 	/** Called in dispose method after the listeners were notified.
109     */
110 	virtual void SAL_CALL disposing();
111 
112 private:
113 	com::sun::star::uno::Reference<com::sun::star::awt::XWindow> mxParent;	/// parent window
114 	com::sun::star::uno::Reference< XMultiServiceFactory > mxMSF;
115 
116 	static ResMgr* mpResMgr;
117 	XMLFilterSettingsDialog* mpDialog;
118 };
119 
120 //-------------------------------------------------------------------------
121 
122 ResMgr* XMLFilterDialogComponent::mpResMgr = NULL;
123 
XMLFilterDialogComponent(const com::sun::star::uno::Reference<XMultiServiceFactory> & rxMSF)124 XMLFilterDialogComponent::XMLFilterDialogComponent( const com::sun::star::uno::Reference< XMultiServiceFactory >& rxMSF ) :
125 	OComponentHelper( maMutex ),
126 	mxMSF( rxMSF ),
127 	mpDialog( NULL )
128 {
129 	Reference< XDesktop > xDesktop( mxMSF->createInstance( OUString::createFromAscii( "com.sun.star.frame.Desktop" ) ), UNO_QUERY );
130 	if( xDesktop.is() )
131 	{
132 		Reference< XTerminateListener > xListener( this );
133 		xDesktop->addTerminateListener( xListener );
134 	}
135 }
136 
137 //-------------------------------------------------------------------------
138 
~XMLFilterDialogComponent()139 XMLFilterDialogComponent::~XMLFilterDialogComponent()
140 {
141 }
142 
143 //-------------------------------------------------------------------------
144 
145 // XInterface
queryInterface(const Type & aType)146 Any SAL_CALL XMLFilterDialogComponent::queryInterface( const Type& aType ) throw (RuntimeException)
147 {
148 	return OComponentHelper::queryInterface( aType );
149 }
150 
151 //-------------------------------------------------------------------------
152 
queryAggregation(Type const & rType)153 Any SAL_CALL XMLFilterDialogComponent::queryAggregation( Type const & rType ) throw (RuntimeException)
154 {
155     if (rType == ::getCppuType( (Reference< ::com::sun::star::ui::dialogs::XExecutableDialog > const *)0 ))
156     {
157         void * p = static_cast< ::com::sun::star::ui::dialogs::XExecutableDialog * >( this );
158         return Any( &p, rType );
159     }
160     else if (rType == ::getCppuType( (Reference< XServiceInfo > const *)0 ))
161     {
162         void * p = static_cast< XServiceInfo * >( this );
163         return Any( &p, rType );
164     }
165     else if (rType == ::getCppuType( (Reference< XInitialization > const *)0 ))
166     {
167         void * p = static_cast< XInitialization * >( this );
168         return Any( &p, rType );
169     }
170 	else if (rType == ::getCppuType( (Reference< XTerminateListener > const *)0 ))
171 	{
172         void * p = static_cast< XTerminateListener * >( this );
173         return Any( &p, rType );
174 	}
175     return OComponentHelper::queryAggregation( rType );
176 }
177 
178 //-------------------------------------------------------------------------
179 
acquire()180 void SAL_CALL XMLFilterDialogComponent::acquire() throw ()
181 {
182     OComponentHelper::acquire();
183 }
184 
185 //-------------------------------------------------------------------------
186 
release()187 void SAL_CALL XMLFilterDialogComponent::release() throw ()
188 {
189     OComponentHelper::release();
190 }
191 
192 //-------------------------------------------------------------------------
193 
XMLFilterDialogComponent_getImplementationName()194 OUString XMLFilterDialogComponent_getImplementationName() throw ( RuntimeException )
195 {
196 	return OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLFilterDialogComponent" ) );
197 }
198 
199 //-------------------------------------------------------------------------
200 
XMLFilterDialogComponent_getSupportedServiceNames()201 Sequence< OUString > SAL_CALL XMLFilterDialogComponent_getSupportedServiceNames()  throw ( RuntimeException )
202 {
203 	OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.ui.XSLTFilterDialog" ) );
204 	Sequence< ::rtl::OUString > aSupported( &aServiceName, 1 );
205 	return aSupported;
206 }
207 
208 //-------------------------------------------------------------------------
209 
XMLFilterDialogComponent_supportsService(const OUString & ServiceName)210 sal_Bool SAL_CALL XMLFilterDialogComponent_supportsService( const OUString& ServiceName ) throw ( RuntimeException )
211 {
212 	Sequence< ::rtl::OUString > aSupported(XMLFilterDialogComponent_getSupportedServiceNames());
213 	const ::rtl::OUString* pArray = aSupported.getConstArray();
214 	for (sal_Int32 i = 0; i < aSupported.getLength(); ++i, ++pArray)
215 		if (pArray->equals(ServiceName))
216 			return sal_True;
217 	return sal_False;
218 }
219 
220 //-------------------------------------------------------------------------
221 
XMLFilterDialogComponent_createInstance(const Reference<XMultiServiceFactory> & rSMgr)222 Reference< XInterface > SAL_CALL XMLFilterDialogComponent_createInstance( const Reference< XMultiServiceFactory > & rSMgr) throw ( Exception )
223 {
224 	return (OWeakObject*)new XMLFilterDialogComponent( rSMgr );
225 }
226 
227 //-------------------------------------------------------------------------
getImplementationName()228 ::rtl::OUString SAL_CALL XMLFilterDialogComponent::getImplementationName() throw(com::sun::star::uno::RuntimeException)
229 {
230 	return XMLFilterDialogComponent_getImplementationName();
231 }
232 
233 //-------------------------------------------------------------------------
234 
getImplementationId(void)235 Sequence< sal_Int8 > SAL_CALL XMLFilterDialogComponent::getImplementationId( void ) throw( RuntimeException )
236 {
237 	static OImplementationId* pId = 0;
238 	if( !pId )
239 	{
240 		MutexGuard aGuard( Mutex::getGlobalMutex() );
241 		if( !pId)
242 		{
243 			static OImplementationId aId;
244 			pId = &aId;
245 		}
246 	}
247 	return pId->getImplementationId();
248 }
249 
250 //-------------------------------------------------------------------------
251 
getTypes()252 Sequence< Type > XMLFilterDialogComponent::getTypes() throw (RuntimeException)
253 {
254 	static OTypeCollection * s_pTypes = 0;
255 	if (! s_pTypes)
256 	{
257 		MutexGuard aGuard( Mutex::getGlobalMutex() );
258 		if (! s_pTypes)
259 		{
260 			static OTypeCollection s_aTypes(
261 				::getCppuType( (const Reference< XComponent > *)0 ),
262 				::getCppuType( (const Reference< XTypeProvider > *)0 ),
263 				::getCppuType( (const Reference< XAggregation > *)0 ),
264 				::getCppuType( (const Reference< XWeak > *)0 ),
265 				::getCppuType( (const Reference< XServiceInfo > *)0 ),
266 				::getCppuType( (const Reference< XInitialization > *)0 ),
267 				::getCppuType( (const Reference< XTerminateListener > *)0 ),
268 				::getCppuType( (const Reference< ::com::sun::star::ui::dialogs::XExecutableDialog > *)0 ));
269 			s_pTypes = &s_aTypes;
270 		}
271 	}
272 	return s_pTypes->getTypes();
273 }
274 
275 //-------------------------------------------------------------------------
276 
getSupportedServiceNames()277 Sequence< ::rtl::OUString > SAL_CALL XMLFilterDialogComponent::getSupportedServiceNames() throw(com::sun::star::uno::RuntimeException)
278 {
279 	return XMLFilterDialogComponent_getSupportedServiceNames();
280 }
281 
282 //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)283 sal_Bool SAL_CALL XMLFilterDialogComponent::supportsService(const ::rtl::OUString& ServiceName) throw(RuntimeException)
284 {
285 	return XMLFilterDialogComponent_supportsService( ServiceName );
286 }
287 
288 //-------------------------------------------------------------------------
289 
290 /** Called in dispose method after the listeners were notified.
291 */
disposing()292 void SAL_CALL XMLFilterDialogComponent::disposing()
293 {
294 	vos::OGuard aGuard( Application::GetSolarMutex() );
295 
296 	if( mpDialog )
297 	{
298 		delete mpDialog;
299 		mpDialog = NULL;
300 	}
301 
302 	if( mpResMgr )
303 	{
304 		delete mpResMgr;
305 		mpResMgr = NULL;
306 	}
307 }
308 
309 //-------------------------------------------------------------------------
310 
311 // XTerminateListener
queryTermination(const EventObject &)312 void SAL_CALL XMLFilterDialogComponent::queryTermination( const EventObject& /* Event */ ) throw (TerminationVetoException, RuntimeException)
313 {
314 	vos::OGuard aGuard( Application::GetSolarMutex() );
315 
316 	// we will never give a veto here
317 	if( mpDialog && !mpDialog->isClosable() )
318 	{
319 		mpDialog->ToTop();
320 		throw TerminationVetoException();
321 	}
322 }
323 
324 //-------------------------------------------------------------------------
325 
notifyTermination(const EventObject &)326 void SAL_CALL XMLFilterDialogComponent::notifyTermination( const EventObject& /* Event */ ) throw (RuntimeException)
327 {
328 	// we are going down, so dispose us!
329 	dispose();
330 }
331 
disposing(const EventObject &)332 void SAL_CALL XMLFilterDialogComponent::disposing( const EventObject& /* Source */ ) throw (RuntimeException)
333 {
334 }
335 
336 //-------------------------------------------------------------------------
setTitle(const::rtl::OUString &)337 void SAL_CALL XMLFilterDialogComponent::setTitle( const ::rtl::OUString& /* _rTitle */ ) throw(RuntimeException)
338 {
339 }
340 
341 //-------------------------------------------------------------------------
execute()342 sal_Int16 SAL_CALL XMLFilterDialogComponent::execute(  ) throw(RuntimeException)
343 {
344 	vos::OGuard aGuard( Application::GetSolarMutex() );
345 
346 	if( NULL == mpResMgr )
347 	{
348 		ByteString aResMgrName( "xsltdlg" );
349 
350 		mpResMgr = ResMgr::CreateResMgr( aResMgrName.GetBuffer(), Application::GetSettings().GetUILocale() );
351 	}
352 
353 	if( NULL == mpDialog )
354 	{
355 		Window* pParent = NULL;
356 		if( mxParent.is() )
357 		{
358 			VCLXWindow* pImplementation = VCLXWindow::GetImplementation(mxParent);
359 			if (pImplementation)
360 				pParent = pImplementation->GetWindow();
361 		}
362 
363 		Reference< XComponent > xComp( this );
364 		mpDialog = new XMLFilterSettingsDialog( pParent, *mpResMgr, mxMSF );
365 		mpDialog->ShowWindow();
366 	}
367 	else if( !mpDialog->IsVisible() )
368 	{
369 		mpDialog->ShowWindow();
370 	}
371 	mpDialog->ToTop();
372 
373 	return 0;
374 }
375 
376 //-------------------------------------------------------------------------
initialize(const Sequence<Any> & aArguments)377 void SAL_CALL XMLFilterDialogComponent::initialize( const Sequence< Any >& aArguments ) throw(Exception, RuntimeException)
378 {
379 	const Any* pArguments = aArguments.getConstArray();
380 	for(sal_Int32 i=0; i<aArguments.getLength(); ++i, ++pArguments)
381 	{
382 		PropertyValue aProperty;
383 		if(*pArguments >>= aProperty)
384 		{
385 			if( aProperty.Name.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "ParentWindow" ) ) == 0 )
386 			{
387 				aProperty.Value >>= mxParent;
388 			}
389 		}
390 	}
391 }
392 
393 
394 extern "C"
395 {
396 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)397 void SAL_CALL component_getImplementationEnvironment(
398 	const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
399 {
400 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
401 }
402 
403 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)404 void * SAL_CALL component_getFactory(
405 	const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
406 {
407 	void * pRet = 0;
408 
409 	if( pServiceManager )
410 	{
411 		Reference< XSingleServiceFactory > xFactory;
412 
413 		OUString implName = OUString::createFromAscii( pImplName );
414 		if ( implName.equals(XMLFilterDialogComponent_getImplementationName()) )
415 		{
416 			xFactory = createOneInstanceFactory(
417 				reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
418 				OUString::createFromAscii( pImplName ),
419 				XMLFilterDialogComponent_createInstance, XMLFilterDialogComponent_getSupportedServiceNames() );
420 
421 		}
422 
423 		if (xFactory.is())
424 		{
425 			xFactory->acquire();
426 			pRet = xFactory.get();
427 		}
428 	}
429 
430 	return pRet;
431 }
432 }
433