Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "svtools/genericunodialog.hxx"
31 : :
32 : : #include <com/sun/star/beans/NamedValue.hpp>
33 : : #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
34 : :
35 : : #include <toolkit/awt/vclxwindow.hxx>
36 : : #include <comphelper/extract.hxx>
37 : : #include <cppuhelper/typeprovider.hxx>
38 : : #include <comphelper/property.hxx>
39 : : #include <osl/diagnose.h>
40 : : #include <tools/diagnose_ex.h>
41 : : #include <vcl/msgbox.hxx>
42 : : #include <osl/mutex.hxx>
43 : : #include <vcl/svapp.hxx>
44 : :
45 : : using namespace ::comphelper;
46 : : using namespace ::com::sun::star::uno;
47 : : using namespace ::com::sun::star::lang;
48 : : using namespace ::com::sun::star::beans;
49 : : using namespace ::com::sun::star::ucb;
50 : :
51 : : //.........................................................................
52 : : namespace svt
53 : : {
54 : : //.........................................................................
55 : :
56 : : //=========================================================================
57 : : //-------------------------------------------------------------------------
58 : 8 : OGenericUnoDialog::OGenericUnoDialog(const Reference< XMultiServiceFactory >& _rxORB)
59 : 8 : :OPropertyContainer(GetBroadcastHelper())
60 : : ,m_pDialog(NULL)
61 : : ,m_bExecuting(sal_False)
62 : : ,m_bCanceled(sal_False)
63 : : ,m_bTitleAmbiguous(sal_True)
64 : : ,m_bInitialized( false )
65 : : ,m_bNeedInitialization( false )
66 [ + - + - ]: 16 : ,m_aContext( _rxORB )
[ + - ][ + - ]
67 : : {
68 : : registerProperty(::rtl::OUString(UNODIALOG_PROPERTY_TITLE), UNODIALOG_PROPERTY_ID_TITLE, PropertyAttribute::TRANSIENT,
69 [ + - ][ + - ]: 8 : &m_sTitle, getCppuType(&m_sTitle));
70 : : registerProperty(::rtl::OUString(UNODIALOG_PROPERTY_PARENT), UNODIALOG_PROPERTY_ID_PARENT, PropertyAttribute::TRANSIENT,
71 [ + - ][ + - ]: 8 : &m_xParent, getCppuType(&m_xParent));
72 : 8 : }
73 : :
74 : : //-------------------------------------------------------------------------
75 : 0 : OGenericUnoDialog::OGenericUnoDialog(const Reference< XComponentContext >& _rxContext)
76 : 0 : :OPropertyContainer(GetBroadcastHelper())
77 : : ,m_pDialog(NULL)
78 : : ,m_bExecuting(sal_False)
79 : : ,m_bCanceled(sal_False)
80 : : ,m_bTitleAmbiguous(sal_True)
81 : : ,m_bInitialized( false )
82 : : ,m_bNeedInitialization( false )
83 [ # # # # ]: 0 : ,m_aContext(_rxContext)
[ # # ][ # # ]
84 : : {
85 : : registerProperty(::rtl::OUString(UNODIALOG_PROPERTY_TITLE), UNODIALOG_PROPERTY_ID_TITLE, PropertyAttribute::TRANSIENT,
86 [ # # ][ # # ]: 0 : &m_sTitle, getCppuType(&m_sTitle));
87 : : registerProperty(::rtl::OUString(UNODIALOG_PROPERTY_PARENT), UNODIALOG_PROPERTY_ID_PARENT, PropertyAttribute::TRANSIENT,
88 [ # # ][ # # ]: 0 : &m_xParent, getCppuType(&m_xParent));
89 : 0 : }
90 : :
91 : : //-------------------------------------------------------------------------
92 [ + - ][ + - ]: 8 : OGenericUnoDialog::~OGenericUnoDialog()
[ + - ][ + - ]
93 : : {
94 [ - + ]: 8 : if ( m_pDialog )
95 : : {
96 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
97 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
98 [ # # ]: 0 : if ( m_pDialog )
99 [ # # ][ # # ]: 0 : destroyDialog();
[ # # ]
100 : : }
101 [ - + ]: 8 : }
102 : :
103 : : //-------------------------------------------------------------------------
104 : 56 : Any SAL_CALL OGenericUnoDialog::queryInterface(const Type& _rType) throw (RuntimeException)
105 : : {
106 : 56 : Any aReturn = OGenericUnoDialogBase::queryInterface(_rType);
107 : :
108 [ + + ]: 56 : if (!aReturn.hasValue())
109 : : aReturn = ::cppu::queryInterface(_rType
110 : : ,static_cast<XPropertySet*>(this)
111 : : ,static_cast<XMultiPropertySet*>(this)
112 : : ,static_cast<XFastPropertySet*>(this)
113 [ + - ]: 6 : );
114 : :
115 : 56 : return aReturn;
116 : : }
117 : :
118 : : //-------------------------------------------------------------------------
119 : 0 : Sequence<Type> SAL_CALL OGenericUnoDialog::getTypes( ) throw(RuntimeException)
120 : : {
121 : : return ::comphelper::concatSequences(
122 : : OGenericUnoDialogBase::getTypes(),
123 : : ::comphelper::OPropertyContainer::getTypes()
124 [ # # ][ # # ]: 0 : );
[ # # ]
125 : : }
126 : :
127 : : //-------------------------------------------------------------------------
128 : 2 : sal_Bool SAL_CALL OGenericUnoDialog::supportsService(const ::rtl::OUString& ServiceName) throw(RuntimeException)
129 : : {
130 [ + - ]: 2 : Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
131 : 2 : const ::rtl::OUString* pArray = aSupported.getConstArray();
132 [ + - ]: 2 : for (sal_Int32 i = 0; i < aSupported.getLength(); ++i, ++pArray)
133 [ + - ]: 2 : if (pArray->equals(ServiceName))
134 : 2 : return sal_True;
135 [ + - ]: 2 : return sal_False;
136 : : }
137 : :
138 : : //-------------------------------------------------------------------------
139 : 16 : void OGenericUnoDialog::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw(Exception)
140 : : {
141 : : // TODO : need some handling if we're currently executing ...
142 : :
143 : 16 : OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle, rValue);
144 : :
145 [ + + ]: 16 : if (UNODIALOG_PROPERTY_ID_TITLE == nHandle)
146 : : {
147 : : // from now on m_sTitle is valid
148 : 14 : m_bTitleAmbiguous = sal_False;
149 : :
150 [ - + ]: 14 : if (m_pDialog)
151 [ # # ]: 0 : m_pDialog->SetText(String(m_sTitle));
152 : : }
153 : 16 : }
154 : :
155 : : //-------------------------------------------------------------------------
156 : 16 : sal_Bool OGenericUnoDialog::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue) throw(IllegalArgumentException)
157 : : {
158 [ + + ]: 16 : switch (nHandle)
159 : : {
160 : : case UNODIALOG_PROPERTY_ID_PARENT:
161 : : {
162 : 2 : Reference<starawt::XWindow> xNew;
163 [ + - ]: 2 : ::cppu::extractInterface(xNew, rValue);
164 [ + - ][ + - ]: 2 : if (xNew != m_xParent)
165 : : {
166 [ + - ]: 2 : rConvertedValue <<= xNew;
167 [ + - ]: 2 : rOldValue <<= m_xParent;
168 : 2 : return sal_True;
169 : : }
170 : 2 : return sal_False;
171 : : }
172 : : }
173 : 16 : return OPropertyContainer::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
174 : : }
175 : :
176 : : //-------------------------------------------------------------------------
177 : 4 : void SAL_CALL OGenericUnoDialog::setTitle( const ::rtl::OUString& _rTitle ) throw(RuntimeException)
178 : : {
179 [ + - ]: 4 : UnoDialogEntryGuard aGuard( *this );
180 : :
181 : : try
182 : : {
183 [ + - ][ + - ]: 4 : setPropertyValue(::rtl::OUString(UNODIALOG_PROPERTY_TITLE), makeAny(_rTitle));
184 : : }
185 [ # # # ]: 0 : catch(RuntimeException&)
186 : : {
187 : : // allowed to pass
188 : 0 : throw;
189 : : }
190 [ # # ]: 0 : catch( const Exception& )
191 : : {
192 : : DBG_UNHANDLED_EXCEPTION();
193 : : // not allowed to pass
194 [ + - ]: 4 : }
195 : 4 : }
196 : :
197 : : //-------------------------------------------------------------------------
198 : 0 : bool OGenericUnoDialog::impl_ensureDialog_lck()
199 : : {
200 [ # # ]: 0 : if ( m_pDialog )
201 : 0 : return true;
202 : :
203 : : // get the parameters for the dialog from the current settings
204 : :
205 : : // the parent window
206 : 0 : Window* pParent = NULL;
207 : 0 : VCLXWindow* pImplementation = VCLXWindow::GetImplementation(m_xParent);
208 [ # # ]: 0 : if (pImplementation)
209 : 0 : pParent = pImplementation->GetWindow();
210 : :
211 : : // the title
212 [ # # ]: 0 : String sTitle = m_sTitle;
213 : :
214 [ # # ]: 0 : Dialog* pDialog = createDialog( pParent );
215 : : OSL_ENSURE( pDialog, "OGenericUnoDialog::impl_ensureDialog_lck: createDialog returned nonsense!" );
216 [ # # ]: 0 : if ( !pDialog )
217 : 0 : return false;
218 : :
219 : : // do some initialisations
220 [ # # ]: 0 : if ( !m_bTitleAmbiguous )
221 [ # # ]: 0 : pDialog->SetText( sTitle );
222 : :
223 : : // be notified when the dialog is killed by somebody else
224 : : // #i65958# / 2006-07-07 / frank.schoenheit@sun.com
225 [ # # ][ # # ]: 0 : pDialog->AddEventListener( LINK( this, OGenericUnoDialog, OnDialogDying ) );
226 : :
227 : 0 : m_pDialog = pDialog;
228 : :
229 [ # # ]: 0 : return true;
230 : : }
231 : :
232 : : //-------------------------------------------------------------------------
233 : 0 : sal_Int16 SAL_CALL OGenericUnoDialog::execute( ) throw(RuntimeException)
234 : : {
235 : : // both creation and execution of the dialog must be guarded with the SolarMutex, so be generous here
236 [ # # ]: 0 : SolarMutexGuard aSolarGuard;
237 : :
238 : 0 : Dialog* pDialogToExecute = NULL;
239 : : // create the dialog, if neccessary
240 : : {
241 [ # # ]: 0 : UnoDialogEntryGuard aGuard( *this );
242 : :
243 [ # # ]: 0 : if (m_bExecuting)
244 : : throw RuntimeException(
245 : : ::rtl::OUString( "already executing the dialog (recursive call)" ),
246 : : *this
247 [ # # ][ # # ]: 0 : );
248 : :
249 : 0 : m_bCanceled = sal_False;
250 : 0 : m_bExecuting = sal_True;
251 : :
252 [ # # ][ # # ]: 0 : if ( !impl_ensureDialog_lck() )
253 : 0 : return 0;
254 : :
255 [ # # ][ # # ]: 0 : pDialogToExecute = m_pDialog;
256 : : }
257 : :
258 : : // start execution
259 : 0 : sal_Int16 nReturn(0);
260 [ # # ]: 0 : if ( pDialogToExecute )
261 [ # # ]: 0 : nReturn = pDialogToExecute->Execute();
262 : :
263 : : {
264 [ # # ]: 0 : ::osl::MutexGuard aExecutionGuard(m_aExecutionMutex);
265 [ # # ]: 0 : if (m_bCanceled)
266 [ # # ]: 0 : nReturn = RET_CANCEL;
267 : : }
268 : :
269 : : {
270 [ # # ]: 0 : ::osl::MutexGuard aGuard(m_aMutex);
271 : :
272 : : // get the settings of the dialog
273 [ # # ]: 0 : executedDialog( nReturn );
274 : :
275 [ # # ]: 0 : m_bExecuting = sal_False;
276 : : }
277 : :
278 : : // outta here
279 [ # # ]: 0 : return nReturn;
280 : : }
281 : :
282 : : #ifdef AWT_DIALOG
283 : : //-------------------------------------------------------------------------
284 : : void SAL_CALL OGenericUnoDialog::endExecute( ) throw(RuntimeException)
285 : : {
286 : : UnoDialogEntryGuard aGuard( *this );
287 : : if (!m_bExecuting)
288 : : throw RuntimeException();
289 : :
290 : : {
291 : : ::osl::MutexGuard aExecutionGuard(m_aExecutionMutex);
292 : : OSL_ENSURE(m_pDialog, "OGenericUnoDialog::endExecute : executing which dialog ?");
293 : : // m_bExecuting is true but we have no dialog ?
294 : : if (!m_pDialog)
295 : : throw RuntimeException();
296 : :
297 : : if (!m_pDialog->IsInExecute())
298 : : // we tighly missed it ... another thread finished the execution of the dialog,
299 : : // but did not manage it to reset m_bExecuting, it currently tries to acquire
300 : : // m_aMutex or m_aExecutionMutex
301 : : // => nothing to do
302 : : return;
303 : :
304 : : m_pDialog->EndDialog(RET_CANCEL);
305 : : m_bCanceled = sal_True;
306 : : }
307 : : }
308 : : #endif
309 : :
310 : : //-------------------------------------------------------------------------
311 : 0 : void OGenericUnoDialog::implInitialize(const Any& _rValue)
312 : : {
313 : : try
314 : : {
315 : 0 : PropertyValue aProperty;
316 : 0 : NamedValue aValue;
317 [ # # ][ # # ]: 0 : if ( _rValue >>= aProperty )
318 : : {
319 [ # # ]: 0 : setPropertyValue( aProperty.Name, aProperty.Value );
320 : : }
321 [ # # ][ # # ]: 0 : else if ( _rValue >>= aValue )
322 : : {
323 [ # # ]: 0 : setPropertyValue( aValue.Name, aValue.Value );
324 [ # # ]: 0 : }
325 : : }
326 : 0 : catch(const Exception&)
327 : : {
328 : : DBG_UNHANDLED_EXCEPTION();
329 : : }
330 : 0 : }
331 : :
332 : : //-------------------------------------------------------------------------
333 : 4 : void SAL_CALL OGenericUnoDialog::initialize( const Sequence< Any >& aArguments ) throw(Exception, RuntimeException)
334 : : {
335 [ + - ]: 4 : ::osl::MutexGuard aGuard( m_aMutex );
336 [ - + ]: 4 : if ( m_bInitialized )
337 [ # # ][ # # ]: 0 : throw AlreadyInitializedException( ::rtl::OUString(), *this );
338 : :
339 : 4 : const Any* pArguments = aArguments.getConstArray();
340 [ - + ]: 4 : for (sal_Int32 i=0; i<aArguments.getLength(); ++i, ++pArguments)
341 [ # # ]: 0 : implInitialize(*pArguments);
342 : :
343 [ + - ]: 4 : m_bInitialized = true;
344 : 4 : }
345 : :
346 : : //-------------------------------------------------------------------------
347 : 0 : void OGenericUnoDialog::destroyDialog()
348 : : {
349 [ # # ]: 0 : delete m_pDialog;
350 : 0 : m_pDialog = NULL;
351 : 0 : }
352 : :
353 : : //-------------------------------------------------------------------------
354 : 0 : IMPL_LINK( OGenericUnoDialog, OnDialogDying, VclWindowEvent*, _pEvent )
355 : : {
356 : : OSL_ENSURE( _pEvent->GetWindow() == m_pDialog, "OGenericUnoDialog::OnDialogDying: where does this come from?" );
357 [ # # ]: 0 : if ( _pEvent->GetId() == VCLEVENT_OBJECT_DYING )
358 : 0 : m_pDialog = NULL;
359 : 0 : return 0L;
360 : : }
361 : :
362 : : //.........................................................................
363 : : } // namespace svt
364 : : //.........................................................................
365 : :
366 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|