Bump version to 5.0-14
[LibreOffice.git] / svtools / source / uno / genericunodialog.cxx
blob6cabb2b1ec96dfbccc794e149462e84e8119c60e
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 <cppuhelper/supportsservice.hxx>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <cppuhelper/typeprovider.hxx>
30 #include <comphelper/property.hxx>
31 #include <osl/diagnose.h>
32 #include <tools/diagnose_ex.h>
33 #include <vcl/msgbox.hxx>
34 #include <osl/mutex.hxx>
35 #include <vcl/svapp.hxx>
37 using namespace ::comphelper;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::ucb;
44 namespace svt
49 OGenericUnoDialog::OGenericUnoDialog(const Reference< XComponentContext >& _rxContext)
50 :OPropertyContainer(GetBroadcastHelper())
51 ,m_pDialog(NULL)
52 ,m_bExecuting(false)
53 ,m_bCanceled(false)
54 ,m_bTitleAmbiguous(true)
55 ,m_bInitialized( false )
56 ,m_bNeedInitialization( false )
57 ,m_aContext(_rxContext)
59 registerProperty(OUString(UNODIALOG_PROPERTY_TITLE), UNODIALOG_PROPERTY_ID_TITLE, PropertyAttribute::TRANSIENT,
60 &m_sTitle, cppu::UnoType<decltype(m_sTitle)>::get());
61 registerProperty(OUString(UNODIALOG_PROPERTY_PARENT), UNODIALOG_PROPERTY_ID_PARENT, PropertyAttribute::TRANSIENT,
62 &m_xParent, cppu::UnoType<decltype(m_xParent)>::get());
66 OGenericUnoDialog::~OGenericUnoDialog()
68 if ( m_pDialog )
70 SolarMutexGuard aSolarGuard;
71 ::osl::MutexGuard aGuard( m_aMutex );
72 if ( m_pDialog )
73 destroyDialog();
78 Any SAL_CALL OGenericUnoDialog::queryInterface(const Type& _rType) throw (RuntimeException, std::exception)
80 Any aReturn = OGenericUnoDialogBase::queryInterface(_rType);
82 if (!aReturn.hasValue())
83 aReturn = ::cppu::queryInterface(_rType
84 ,static_cast<XPropertySet*>(this)
85 ,static_cast<XMultiPropertySet*>(this)
86 ,static_cast<XFastPropertySet*>(this)
89 return aReturn;
93 Sequence<Type> SAL_CALL OGenericUnoDialog::getTypes( ) throw(RuntimeException, std::exception)
95 return ::comphelper::concatSequences(
96 OGenericUnoDialogBase::getTypes(),
97 getBaseTypes()
101 sal_Bool SAL_CALL OGenericUnoDialog::supportsService(const OUString& ServiceName) throw(RuntimeException, std::exception)
103 return cppu::supportsService(this, ServiceName);
107 void OGenericUnoDialog::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw(Exception, std::exception)
109 // TODO : need some handling if we're currently executing ...
111 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle, rValue);
113 if (UNODIALOG_PROPERTY_ID_TITLE == nHandle)
115 // from now on m_sTitle is valid
116 m_bTitleAmbiguous = false;
118 if (m_pDialog)
119 m_pDialog->SetText(m_sTitle);
124 sal_Bool OGenericUnoDialog::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue) throw(IllegalArgumentException)
126 switch (nHandle)
128 case UNODIALOG_PROPERTY_ID_PARENT:
130 Reference<css::awt::XWindow> xNew(rValue, css::uno::UNO_QUERY);
131 if (xNew != m_xParent)
133 rConvertedValue <<= xNew;
134 rOldValue <<= m_xParent;
135 return sal_True;
137 return sal_False;
140 return OPropertyContainer::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
144 void SAL_CALL OGenericUnoDialog::setTitle( const OUString& _rTitle ) throw(RuntimeException, std::exception)
146 UnoDialogEntryGuard aGuard( *this );
150 setPropertyValue(OUString(UNODIALOG_PROPERTY_TITLE), makeAny(_rTitle));
152 catch(RuntimeException&)
154 // allowed to pass
155 throw;
157 catch( const Exception& )
159 DBG_UNHANDLED_EXCEPTION();
160 // not allowed to pass
165 bool OGenericUnoDialog::impl_ensureDialog_lck()
167 if ( m_pDialog )
168 return true;
170 // get the parameters for the dialog from the current settings
172 // the parent window
173 vcl::Window* pParent = NULL;
174 VCLXWindow* pImplementation = VCLXWindow::GetImplementation(m_xParent);
175 if (pImplementation)
176 pParent = pImplementation->GetWindow();
178 // the title
179 OUString sTitle = m_sTitle;
181 VclPtr<Dialog> pDialog = createDialog( pParent );
182 OSL_ENSURE( pDialog, "OGenericUnoDialog::impl_ensureDialog_lck: createDialog returned nonsense!" );
183 if ( !pDialog )
184 return false;
186 // do some initialisations
187 if ( !m_bTitleAmbiguous )
188 pDialog->SetText( sTitle );
190 // be notified when the dialog is killed by somebody else
191 // #i65958# / 2006-07-07 / frank.schoenheit@sun.com
192 pDialog->AddEventListener( LINK( this, OGenericUnoDialog, OnDialogDying ) );
194 m_pDialog = pDialog;
196 return true;
200 sal_Int16 SAL_CALL OGenericUnoDialog::execute( ) throw(RuntimeException, std::exception)
202 // both creation and execution of the dialog must be guarded with the SolarMutex, so be generous here
203 SolarMutexGuard aSolarGuard;
205 Dialog* pDialogToExecute = NULL;
206 // create the dialog, if necessary
208 UnoDialogEntryGuard aGuard( *this );
210 if (m_bExecuting)
211 throw RuntimeException(
212 "already executing the dialog (recursive call)",
213 *this
216 m_bCanceled = false;
217 m_bExecuting = true;
219 if ( !impl_ensureDialog_lck() )
220 return 0;
222 pDialogToExecute = m_pDialog;
225 // start execution
226 sal_Int16 nReturn(0);
227 if ( pDialogToExecute )
228 nReturn = pDialogToExecute->Execute();
231 ::osl::MutexGuard aExecutionGuard(m_aExecutionMutex);
232 if (m_bCanceled)
233 nReturn = RET_CANCEL;
237 ::osl::MutexGuard aGuard(m_aMutex);
239 // get the settings of the dialog
240 executedDialog( nReturn );
242 m_bExecuting = false;
245 // outta here
246 return nReturn;
249 #ifdef AWT_DIALOG
251 void SAL_CALL OGenericUnoDialog::endExecute( ) throw(RuntimeException)
253 UnoDialogEntryGuard aGuard( *this );
254 if (!m_bExecuting)
255 throw RuntimeException();
258 ::osl::MutexGuard aExecutionGuard(m_aExecutionMutex);
259 OSL_ENSURE(m_pDialog, "OGenericUnoDialog::endExecute : executing which dialog ?");
260 // m_bExecuting is true but we have no dialog ?
261 if (!m_pDialog)
262 throw RuntimeException();
264 if (!m_pDialog->IsInExecute())
265 // we tighly missed it ... another thread finished the execution of the dialog,
266 // but did not manage it to reset m_bExecuting, it currently tries to acquire
267 // m_aMutex or m_aExecutionMutex
268 // => nothing to do
269 return;
271 m_pDialog->EndDialog(RET_CANCEL);
272 m_bCanceled = sal_True;
275 #endif
278 void OGenericUnoDialog::implInitialize(const Any& _rValue)
282 PropertyValue aProperty;
283 NamedValue aValue;
284 if ( _rValue >>= aProperty )
286 setPropertyValue( aProperty.Name, aProperty.Value );
288 else if ( _rValue >>= aValue )
290 setPropertyValue( aValue.Name, aValue.Value );
293 catch(const Exception&)
295 DBG_UNHANDLED_EXCEPTION();
300 void SAL_CALL OGenericUnoDialog::initialize( const Sequence< Any >& aArguments ) throw(Exception, RuntimeException, std::exception)
302 ::osl::MutexGuard aGuard( m_aMutex );
303 if ( m_bInitialized )
304 throw AlreadyInitializedException( OUString(), *this );
306 const Any* pArguments = aArguments.getConstArray();
307 for (sal_Int32 i=0; i<aArguments.getLength(); ++i, ++pArguments)
308 implInitialize(*pArguments);
310 m_bInitialized = true;
314 void OGenericUnoDialog::destroyDialog()
316 SolarMutexGuard aSolarGuard;
317 m_pDialog.disposeAndClear();
321 IMPL_LINK( OGenericUnoDialog, OnDialogDying, VclWindowEvent*, _pEvent )
323 OSL_ENSURE( _pEvent->GetWindow() == m_pDialog, "OGenericUnoDialog::OnDialogDying: where does this come from?" );
324 if ( _pEvent->GetId() == VCLEVENT_OBJECT_DYING )
325 m_pDialog = NULL;
326 return 0L;
330 } // namespace svt
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */