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 #ifndef _SVT_GENERICUNODIALOG_HXX_
25 #define _SVT_GENERICUNODIALOG_HXX_
26 
27 #include "svtools/svtdllapi.h"
28 
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XInitialization.hpp>
31 #include <com/sun/star/awt/XWindow.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 #include <com/sun/star/lang/NotInitializedException.hpp>
37 
38 #include <cppuhelper/implbase3.hxx>
39 #include <cppuhelper/propshlp.hxx>
40 #include <comphelper/proparrhlp.hxx>
41 #include <comphelper/uno3.hxx>
42 #include <comphelper/propertycontainer.hxx>
43 #include <comphelper/broadcasthelper.hxx>
44 #include <comphelper/componentcontext.hxx>
45 #include <tools/link.hxx>
46 
47 class Dialog;
48 class Window;
49 class VclWindowEvent;
50 
51 //.........................................................................
52 namespace svt
53 {
54 //.........................................................................
55 
56 	//=========================================================================
57 #define		UNODIALOG_PROPERTY_ID_TITLE		1
58 #define		UNODIALOG_PROPERTY_ID_PARENT	2
59 
60 #define		UNODIALOG_PROPERTY_TITLE		"Title"
61 #define		UNODIALOG_PROPERTY_PARENT		"ParentWindow"
62 
63 
64 	//=========================================================================
65 	typedef	::cppu::WeakImplHelper3	<	com::sun::star::ui::dialogs::XExecutableDialog
66 									,	com::sun::star::lang::XServiceInfo
67 									,	com::sun::star::lang::XInitialization
68 									>	OGenericUnoDialogBase;
69 
70 	/**	abstract base class for implementing UNO objects representing dialogs (<type scope="com.sun.star.awt">XDialog</type>)
71 	*/
72 	class SVT_DLLPUBLIC OGenericUnoDialog
73 			:public OGenericUnoDialogBase
74 			,public ::comphelper::OMutexAndBroadcastHelper
75 			,public ::comphelper::OPropertyContainer
76 	{
77 	private:
78 		::osl::Mutex					m_aExecutionMutex;	/// access safety for execute/cancel
79 
80 	protected:
81 		Dialog*						m_pDialog;			        /// the dialog to execute
82 		sal_Bool					m_bExecuting : 1;	        /// we're currently executing the dialog
83 		sal_Bool					m_bCanceled : 1;	        /// endDialog was called while we were executing
84 		sal_Bool					m_bTitleAmbiguous : 1;	    /// m_sTitle has not been set yet
85         bool                        m_bInitialized : 1;         /// has "initialize" been called?
86         bool                        m_bNeedInitialization : 1;  /// do we need to be initialized before any other API call is allowed?
87 
88 		// <properties>
89 		::rtl::OUString							                        m_sTitle;	/// title of the dialog
90 		com::sun::star::uno::Reference<com::sun::star::awt::XWindow>	m_xParent;	/// parent window
91 		// </properties>
92 
93         ::comphelper::ComponentContext m_aContext;
94 
95     public:
needInitialization() const96         inline bool needInitialization() const { return m_bNeedInitialization && !m_bInitialized; }
97 
98 	protected:
99 		OGenericUnoDialog(const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >& _rxORB);
100 		OGenericUnoDialog(const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& _rxContext);
101 		virtual ~OGenericUnoDialog();
102 
103 	public:
104 		// UNO
105 		DECLARE_UNO3_DEFAULTS(OGenericUnoDialog, OGenericUnoDialogBase);
106 		virtual com::sun::star::uno::Any SAL_CALL queryInterface(const com::sun::star::uno::Type& _rType) throw (com::sun::star::uno::RuntimeException);
107 
108 		// XTypeProvider
109 		virtual com::sun::star::uno::Sequence<com::sun::star::uno::Type> SAL_CALL getTypes(  ) throw(com::sun::star::uno::RuntimeException);
110 		virtual com::sun::star::uno::Sequence<sal_Int8> SAL_CALL getImplementationId(  ) throw(com::sun::star::uno::RuntimeException) = 0;
111 
112 		// XServiceInfo
113 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(com::sun::star::uno::RuntimeException) = 0;
114 		virtual sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw(com::sun::star::uno::RuntimeException);
115 		virtual ::comphelper::StringSequence SAL_CALL getSupportedServiceNames() throw(com::sun::star::uno::RuntimeException) = 0;
116 
117 		// OPropertySetHelper
118 		virtual void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const com::sun::star::uno::Any& rValue ) throw(com::sun::star::uno::Exception);
119 		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);
120 
121 		// XExecutableDialog
122 		virtual void SAL_CALL setTitle( const ::rtl::OUString& aTitle ) throw(::com::sun::star::uno::RuntimeException);
123 		virtual sal_Int16 SAL_CALL execute(  ) throw(::com::sun::star::uno::RuntimeException);
124 
125 		// XInitialization
126 		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);
127 
128 	protected:
129 		/** create the concret dialog instance. note that m_aMutex is not locked when this method get's called,
130 			but the application-wide solar mutex is (to guard the not thread-safe ctor of the dialog).
131 			@param		pParent		the parent window for the new dialog
132 		*/
133 		virtual Dialog*	createDialog(Window* _pParent) = 0;
134 
135 		/// called to destroy the dialog used. the default implementation just deletes m_pDialog and resets it to NULL
136 		virtual void destroyDialog();
137 
138 		/**	called after the dialog has been executed
139 			@param		_nExecutionResult		the execution result as returned by Dialog::Execute
140 		*/
executedDialog(sal_Int16)141 		virtual void executedDialog(sal_Int16 /*_nExecutionResult*/) { }
142 
143 		/** smaller form of <method>initialize</method>.<p/>
144 			The <method>initialize</method> method is called with a sequence of <type scope="com.sun.star.uno">Any</type>'s,
145 			which is split up into the single elements, which are passed to implInitialize. The default implementation
146 			tries to exract an <type scope="com.sun.star.beans">PropertyValue</type> from the value an pass it to the
147 			<type scope="com.sun.star.beans">XPropertySet</type> interface of the object.
148 		*/
149 		virtual void implInitialize(const com::sun::star::uno::Any& _rValue);
150 
151     private:
152         DECL_LINK( OnDialogDying, VclWindowEvent* );
153 
154         /** ensures that m_pDialog is not <NULL/>
155 
156             This method does nothing if m_pDialog is already non-<NULL/>. Else, it calls createDialog and does
157             all necessary initializations of the new dialog instance.
158 
159             @precond
160                 m_aMutex is locked
161 
162             @return
163                 <TRUE/> if and only if m_pDialog is non-<NULL/> upon returning from the method. Note that the only
164                 case where m_pDialog is <NULL/> is when createDialog returned <NULL/>, which is will fire an assertion
165                 in non-product builds.
166         */
167         bool    impl_ensureDialog_lck();
168 	};
169 
170     /// helper class for guarding access to methods of a OGenericUnoDialog
171     class UnoDialogEntryGuard
172     {
173     public:
UnoDialogEntryGuard(OGenericUnoDialog & _rDialog)174         UnoDialogEntryGuard( OGenericUnoDialog& _rDialog )
175             :m_aGuard( _rDialog.GetMutex() )
176         {
177             if ( _rDialog.needInitialization() )
178                 throw ::com::sun::star::lang::NotInitializedException();
179         }
180 
181     private:
182         ::osl::MutexGuard   m_aGuard;
183     };
184 
185 //.........................................................................
186 }	// namespace svt
187 //.........................................................................
188 
189 #endif // _SVT_GENERICUNODIALOG_HXX_
190 
191