update credits
[LibreOffice.git] / svtools / source / uno / genericunodialog.cxx
blobe3e0af5ef95379fa98faeffe95fa2617f9836c98
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "svtools/genericunodialog.hxx"
23 #include <com/sun/star/beans/NamedValue.hpp>
24 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
26 #include <toolkit/awt/vclxwindow.hxx>
27 #include <comphelper/extract.hxx>
28 #include <cppuhelper/typeprovider.hxx>
29 #include <comphelper/property.hxx>
30 #include <osl/diagnose.h>
31 #include <tools/diagnose_ex.h>
32 #include <vcl/msgbox.hxx>
33 #include <osl/mutex.hxx>
34 #include <vcl/svapp.hxx>
36 using namespace ::comphelper;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::ucb;
42 //.........................................................................
43 namespace svt
45 //.........................................................................
47 //-------------------------------------------------------------------------
48 OGenericUnoDialog::OGenericUnoDialog(const Reference< XComponentContext >& _rxContext)
49 :OPropertyContainer(GetBroadcastHelper())
50 ,m_pDialog(NULL)
51 ,m_bExecuting(sal_False)
52 ,m_bCanceled(sal_False)
53 ,m_bTitleAmbiguous(sal_True)
54 ,m_bInitialized( false )
55 ,m_bNeedInitialization( false )
56 ,m_aContext(_rxContext)
58 registerProperty(OUString(UNODIALOG_PROPERTY_TITLE), UNODIALOG_PROPERTY_ID_TITLE, PropertyAttribute::TRANSIENT,
59 &m_sTitle, getCppuType(&m_sTitle));
60 registerProperty(OUString(UNODIALOG_PROPERTY_PARENT), UNODIALOG_PROPERTY_ID_PARENT, PropertyAttribute::TRANSIENT,
61 &m_xParent, getCppuType(&m_xParent));
64 //-------------------------------------------------------------------------
65 OGenericUnoDialog::~OGenericUnoDialog()
67 if ( m_pDialog )
69 SolarMutexGuard aSolarGuard;
70 ::osl::MutexGuard aGuard( m_aMutex );
71 if ( m_pDialog )
72 destroyDialog();
76 //-------------------------------------------------------------------------
77 Any SAL_CALL OGenericUnoDialog::queryInterface(const Type& _rType) throw (RuntimeException)
79 Any aReturn = OGenericUnoDialogBase::queryInterface(_rType);
81 if (!aReturn.hasValue())
82 aReturn = ::cppu::queryInterface(_rType
83 ,static_cast<XPropertySet*>(this)
84 ,static_cast<XMultiPropertySet*>(this)
85 ,static_cast<XFastPropertySet*>(this)
88 return aReturn;
91 //-------------------------------------------------------------------------
92 Sequence<Type> SAL_CALL OGenericUnoDialog::getTypes( ) throw(RuntimeException)
94 return ::comphelper::concatSequences(
95 OGenericUnoDialogBase::getTypes(),
96 ::comphelper::OPropertyContainer::getTypes()
100 //-------------------------------------------------------------------------
101 sal_Bool SAL_CALL OGenericUnoDialog::supportsService(const OUString& ServiceName) throw(RuntimeException)
103 Sequence< OUString > aSupported(getSupportedServiceNames());
104 const OUString* pArray = aSupported.getConstArray();
105 for (sal_Int32 i = 0; i < aSupported.getLength(); ++i, ++pArray)
106 if (pArray->equals(ServiceName))
107 return sal_True;
108 return sal_False;
111 //-------------------------------------------------------------------------
112 void OGenericUnoDialog::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw(Exception)
114 // TODO : need some handling if we're currently executing ...
116 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle, rValue);
118 if (UNODIALOG_PROPERTY_ID_TITLE == nHandle)
120 // from now on m_sTitle is valid
121 m_bTitleAmbiguous = sal_False;
123 if (m_pDialog)
124 m_pDialog->SetText(String(m_sTitle));
128 //-------------------------------------------------------------------------
129 sal_Bool OGenericUnoDialog::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue) throw(IllegalArgumentException)
131 switch (nHandle)
133 case UNODIALOG_PROPERTY_ID_PARENT:
135 Reference<starawt::XWindow> xNew;
136 ::cppu::extractInterface(xNew, rValue);
137 if (xNew != m_xParent)
139 rConvertedValue <<= xNew;
140 rOldValue <<= m_xParent;
141 return sal_True;
143 return sal_False;
146 return OPropertyContainer::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
149 //-------------------------------------------------------------------------
150 void SAL_CALL OGenericUnoDialog::setTitle( const OUString& _rTitle ) throw(RuntimeException)
152 UnoDialogEntryGuard aGuard( *this );
156 setPropertyValue(OUString(UNODIALOG_PROPERTY_TITLE), makeAny(_rTitle));
158 catch(RuntimeException&)
160 // allowed to pass
161 throw;
163 catch( const Exception& )
165 DBG_UNHANDLED_EXCEPTION();
166 // not allowed to pass
170 //-------------------------------------------------------------------------
171 bool OGenericUnoDialog::impl_ensureDialog_lck()
173 if ( m_pDialog )
174 return true;
176 // get the parameters for the dialog from the current settings
178 // the parent window
179 Window* pParent = NULL;
180 VCLXWindow* pImplementation = VCLXWindow::GetImplementation(m_xParent);
181 if (pImplementation)
182 pParent = pImplementation->GetWindow();
184 // the title
185 String sTitle = m_sTitle;
187 Dialog* pDialog = createDialog( pParent );
188 OSL_ENSURE( pDialog, "OGenericUnoDialog::impl_ensureDialog_lck: createDialog returned nonsense!" );
189 if ( !pDialog )
190 return false;
192 // do some initialisations
193 if ( !m_bTitleAmbiguous )
194 pDialog->SetText( sTitle );
196 // be notified when the dialog is killed by somebody else
197 // #i65958# / 2006-07-07 / frank.schoenheit@sun.com
198 pDialog->AddEventListener( LINK( this, OGenericUnoDialog, OnDialogDying ) );
200 m_pDialog = pDialog;
202 return true;
205 //-------------------------------------------------------------------------
206 sal_Int16 SAL_CALL OGenericUnoDialog::execute( ) throw(RuntimeException)
208 // both creation and execution of the dialog must be guarded with the SolarMutex, so be generous here
209 SolarMutexGuard aSolarGuard;
211 Dialog* pDialogToExecute = NULL;
212 // create the dialog, if necessary
214 UnoDialogEntryGuard aGuard( *this );
216 if (m_bExecuting)
217 throw RuntimeException(
218 OUString( "already executing the dialog (recursive call)" ),
219 *this
222 m_bCanceled = sal_False;
223 m_bExecuting = sal_True;
225 if ( !impl_ensureDialog_lck() )
226 return 0;
228 pDialogToExecute = m_pDialog;
231 // start execution
232 sal_Int16 nReturn(0);
233 if ( pDialogToExecute )
234 nReturn = pDialogToExecute->Execute();
237 ::osl::MutexGuard aExecutionGuard(m_aExecutionMutex);
238 if (m_bCanceled)
239 nReturn = RET_CANCEL;
243 ::osl::MutexGuard aGuard(m_aMutex);
245 // get the settings of the dialog
246 executedDialog( nReturn );
248 m_bExecuting = sal_False;
251 // outta here
252 return nReturn;
255 #ifdef AWT_DIALOG
256 //-------------------------------------------------------------------------
257 void SAL_CALL OGenericUnoDialog::endExecute( ) throw(RuntimeException)
259 UnoDialogEntryGuard aGuard( *this );
260 if (!m_bExecuting)
261 throw RuntimeException();
264 ::osl::MutexGuard aExecutionGuard(m_aExecutionMutex);
265 OSL_ENSURE(m_pDialog, "OGenericUnoDialog::endExecute : executing which dialog ?");
266 // m_bExecuting is true but we have no dialog ?
267 if (!m_pDialog)
268 throw RuntimeException();
270 if (!m_pDialog->IsInExecute())
271 // we tighly missed it ... another thread finished the execution of the dialog,
272 // but did not manage it to reset m_bExecuting, it currently tries to acquire
273 // m_aMutex or m_aExecutionMutex
274 // => nothing to do
275 return;
277 m_pDialog->EndDialog(RET_CANCEL);
278 m_bCanceled = sal_True;
281 #endif
283 //-------------------------------------------------------------------------
284 void OGenericUnoDialog::implInitialize(const Any& _rValue)
288 PropertyValue aProperty;
289 NamedValue aValue;
290 if ( _rValue >>= aProperty )
292 setPropertyValue( aProperty.Name, aProperty.Value );
294 else if ( _rValue >>= aValue )
296 setPropertyValue( aValue.Name, aValue.Value );
299 catch(const Exception&)
301 DBG_UNHANDLED_EXCEPTION();
305 //-------------------------------------------------------------------------
306 void SAL_CALL OGenericUnoDialog::initialize( const Sequence< Any >& aArguments ) throw(Exception, RuntimeException)
308 ::osl::MutexGuard aGuard( m_aMutex );
309 if ( m_bInitialized )
310 throw AlreadyInitializedException( OUString(), *this );
312 const Any* pArguments = aArguments.getConstArray();
313 for (sal_Int32 i=0; i<aArguments.getLength(); ++i, ++pArguments)
314 implInitialize(*pArguments);
316 m_bInitialized = true;
319 //-------------------------------------------------------------------------
320 void OGenericUnoDialog::destroyDialog()
322 delete m_pDialog;
323 m_pDialog = NULL;
326 //-------------------------------------------------------------------------
327 IMPL_LINK( OGenericUnoDialog, OnDialogDying, VclWindowEvent*, _pEvent )
329 OSL_ENSURE( _pEvent->GetWindow() == m_pDialog, "OGenericUnoDialog::OnDialogDying: where does this come from?" );
330 if ( _pEvent->GetId() == VCLEVENT_OBJECT_DYING )
331 m_pDialog = NULL;
332 return 0L;
335 //.........................................................................
336 } // namespace svt
337 //.........................................................................
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */