1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SVT_GENERICUNODIALOG_HXX_
29 #define _SVT_GENERICUNODIALOG_HXX_
30 
31 #include "svtools/svtdllapi.h"
32 
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/lang/XInitialization.hpp>
35 #include <com/sun/star/awt/XWindow.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #include <com/sun/star/lang/NotInitializedException.hpp>
41 
42 #include <cppuhelper/implbase3.hxx>
43 #include <cppuhelper/propshlp.hxx>
44 #include <comphelper/proparrhlp.hxx>
45 #include <comphelper/uno3.hxx>
46 #include <comphelper/propertycontainer.hxx>
47 #include <comphelper/broadcasthelper.hxx>
48 #include <comphelper/componentcontext.hxx>
49 #include <tools/link.hxx>
50 
51 class Dialog;
52 class Window;
53 class VclWindowEvent;
54 
55 //.........................................................................
56 namespace svt
57 {
58 //.........................................................................
59 
60 	//=========================================================================
61 #define		UNODIALOG_PROPERTY_ID_TITLE		1
62 #define		UNODIALOG_PROPERTY_ID_PARENT	2
63 
64 #define		UNODIALOG_PROPERTY_TITLE		"Title"
65 #define		UNODIALOG_PROPERTY_PARENT		"ParentWindow"
66 
67 
68 	//=========================================================================
69 	typedef	::cppu::WeakImplHelper3	<	com::sun::star::ui::dialogs::XExecutableDialog
70 									,	com::sun::star::lang::XServiceInfo
71 									,	com::sun::star::lang::XInitialization
72 									>	OGenericUnoDialogBase;
73 
74 	/**	abstract base class for implementing UNO objects representing dialogs (<type scope="com.sun.star.awt">XDialog</type>)
75 	*/
76 	class SVT_DLLPUBLIC OGenericUnoDialog
77 			:public OGenericUnoDialogBase
78 			,public ::comphelper::OMutexAndBroadcastHelper
79 			,public ::comphelper::OPropertyContainer
80 	{
81 	private:
82 		::osl::Mutex					m_aExecutionMutex;	/// acess safety for execute/cancel
83 
84 	protected:
85 		Dialog*						m_pDialog;			        /// the dialog to execute
86 		sal_Bool					m_bExecuting : 1;	        /// we're currently executing the dialog
87 		sal_Bool					m_bCanceled : 1;	        /// endDialog was called while we were executing
88 		sal_Bool					m_bTitleAmbiguous : 1;	    /// m_sTitle has not been set yet
89         bool                        m_bInitialized : 1;         /// has "initialize" been called?
90         bool                        m_bNeedInitialization : 1;  /// do we need to be initialized before any other API call is allowed?
91 
92 		// <properties>
93 		::rtl::OUString							                        m_sTitle;	/// title of the dialog
94 		com::sun::star::uno::Reference<com::sun::star::awt::XWindow>	m_xParent;	/// parent window
95 		// </properties>
96 
97         ::comphelper::ComponentContext m_aContext;
98 
99     public:
100         inline bool needInitialization() const { return m_bNeedInitialization && !m_bInitialized; }
101 
102 	protected:
103 		OGenericUnoDialog(const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >& _rxORB);
104 		OGenericUnoDialog(const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& _rxContext);
105 		virtual ~OGenericUnoDialog();
106 
107 	public:
108 		// UNO
109 		DECLARE_UNO3_DEFAULTS(OGenericUnoDialog, OGenericUnoDialogBase);
110 		virtual com::sun::star::uno::Any SAL_CALL queryInterface(const com::sun::star::uno::Type& _rType) throw (com::sun::star::uno::RuntimeException);
111 
112 		// XTypeProvider
113 		virtual com::sun::star::uno::Sequence<com::sun::star::uno::Type> SAL_CALL getTypes(  ) throw(com::sun::star::uno::RuntimeException);
114 		virtual com::sun::star::uno::Sequence<sal_Int8> SAL_CALL getImplementationId(  ) throw(com::sun::star::uno::RuntimeException) = 0;
115 
116 		// XServiceInfo
117 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(com::sun::star::uno::RuntimeException) = 0;
118 		virtual sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw(com::sun::star::uno::RuntimeException);
119 		virtual ::comphelper::StringSequence SAL_CALL getSupportedServiceNames() throw(com::sun::star::uno::RuntimeException) = 0;
120 
121 		// OPropertySetHelper
122 		virtual void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const com::sun::star::uno::Any& rValue ) throw(com::sun::star::uno::Exception);
123 		virtual sal_Bool SAL_CALL convertFastPropertyValue( com::sun::star::uno::Any& rConvertedValue, com::sun::star::uno::Any& rOldValue, sal_Int32 nHandle, const com::sun::star::uno::Any& rValue) throw(com::sun::star::lang::IllegalArgumentException);
124 
125 		// XExecutableDialog
126 		virtual void SAL_CALL setTitle( const ::rtl::OUString& aTitle ) throw(::com::sun::star::uno::RuntimeException);
127 		virtual sal_Int16 SAL_CALL execute(  ) throw(::com::sun::star::uno::RuntimeException);
128 
129 		// XInitialization
130 		virtual void SAL_CALL initialize( const com::sun::star::uno::Sequence< com::sun::star::uno::Any >& aArguments ) throw(com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException);
131 
132 	protected:
133 		/** create the concret dialog instance. note that m_aMutex is not locked when this method get's called,
134 			but the application-wide solar mutex is (to guard the not thread-safe ctor of the dialog).
135 			@param		pParent		the parent window for the new dialog
136 		*/
137 		virtual Dialog*	createDialog(Window* _pParent) = 0;
138 
139 		/// called to destroy the dialog used. the default implementation just deletes m_pDialog and resets it to NULL
140 		virtual void destroyDialog();
141 
142 		/**	called after the dialog has been executed
143 			@param		_nExecutionResult		the execution result as returned by Dialog::Execute
144 		*/
145 		virtual void executedDialog(sal_Int16 /*_nExecutionResult*/) { }
146 
147 		/** smaller form of <method>initialize</method>.<p/>
148 			The <method>initialize</method> method is called with a sequence of <type scope="com.sun.star.uno">Any</type>'s,
149 			which is split up into the single elements, which are passed to implInitialize. The default implementation
150 			tries to exract an <type scope="com.sun.star.beans">PropertyValue</type> from the value an pass it to the
151 			<type scope="com.sun.star.beans">XPropertySet</type> interface of the object.
152 		*/
153 		virtual void implInitialize(const com::sun::star::uno::Any& _rValue);
154 
155     private:
156         DECL_LINK( OnDialogDying, VclWindowEvent* );
157 
158         /** ensures that m_pDialog is not <NULL/>
159 
160             This method does nothing if m_pDialog is already non-<NULL/>. Else, it calls createDialog and does
161             all necessary initializations of the new dialog instance.
162 
163             @precond
164                 m_aMutex is locked
165 
166             @return
167                 <TRUE/> if and only if m_pDialog is non-<NULL/> upon returning from the method. Note that the only
168                 case where m_pDialog is <NULL/> is when createDialog returned <NULL/>, which is will fire an assertion
169                 in non-product builds.
170         */
171         bool    impl_ensureDialog_lck();
172 	};
173 
174     /// helper class for guarding access to methods of a OGenericUnoDialog
175     class UnoDialogEntryGuard
176     {
177     public:
178         UnoDialogEntryGuard( OGenericUnoDialog& _rDialog )
179             :m_aGuard( _rDialog.GetMutex() )
180         {
181             if ( _rDialog.needInitialization() )
182                 throw ::com::sun::star::lang::NotInitializedException();
183         }
184 
185     private:
186         ::osl::MutexGuard   m_aGuard;
187     };
188 
189 //.........................................................................
190 }	// namespace svt
191 //.........................................................................
192 
193 #endif // _SVT_GENERICUNODIALOG_HXX_
194 
195