1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: genericunodialog.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "svtools/genericunodialog.hxx"
36 #include <com/sun/star/beans/NamedValue.hpp>
37 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
39 #include <toolkit/awt/vclxwindow.hxx>
40 #include <cppuhelper/extract.hxx>
41 #include <cppuhelper/typeprovider.hxx>
42 #include <comphelper/property.hxx>
43 #include <osl/diagnose.h>
44 #include <tools/diagnose_ex.h>
45 #include <vcl/msgbox.hxx>
46 #include <vos/mutex.hxx>
47 #include <vcl/svapp.hxx>
49 using namespace ::comphelper
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::beans
;
53 using namespace ::com::sun::star::ucb
;
55 //.........................................................................
58 //.........................................................................
60 //=========================================================================
61 //-------------------------------------------------------------------------
62 OGenericUnoDialog::OGenericUnoDialog(const Reference
< XMultiServiceFactory
>& _rxORB
)
63 :OPropertyContainer(GetBroadcastHelper())
65 ,m_bExecuting(sal_False
)
66 ,m_bCanceled(sal_False
)
67 ,m_bTitleAmbiguous(sal_True
)
68 ,m_bInitialized( false )
69 ,m_bNeedInitialization( false )
72 registerProperty(::rtl::OUString::createFromAscii(UNODIALOG_PROPERTY_TITLE
), UNODIALOG_PROPERTY_ID_TITLE
, PropertyAttribute::TRANSIENT
,
73 &m_sTitle
, getCppuType(&m_sTitle
));
74 registerProperty(::rtl::OUString::createFromAscii(UNODIALOG_PROPERTY_PARENT
), UNODIALOG_PROPERTY_ID_PARENT
, PropertyAttribute::TRANSIENT
,
75 &m_xParent
, getCppuType(&m_xParent
));
78 //-------------------------------------------------------------------------
79 OGenericUnoDialog::OGenericUnoDialog(const Reference
< XComponentContext
>& _rxContext
)
80 :OPropertyContainer(GetBroadcastHelper())
82 ,m_bExecuting(sal_False
)
83 ,m_bCanceled(sal_False
)
84 ,m_bTitleAmbiguous(sal_True
)
85 ,m_bInitialized( false )
86 ,m_bNeedInitialization( false )
87 ,m_aContext(_rxContext
)
89 registerProperty(::rtl::OUString::createFromAscii(UNODIALOG_PROPERTY_TITLE
), UNODIALOG_PROPERTY_ID_TITLE
, PropertyAttribute::TRANSIENT
,
90 &m_sTitle
, getCppuType(&m_sTitle
));
91 registerProperty(::rtl::OUString::createFromAscii(UNODIALOG_PROPERTY_PARENT
), UNODIALOG_PROPERTY_ID_PARENT
, PropertyAttribute::TRANSIENT
,
92 &m_xParent
, getCppuType(&m_xParent
));
95 //-------------------------------------------------------------------------
96 OGenericUnoDialog::~OGenericUnoDialog()
100 ::vos::OGuard
aSolarGuard( Application::GetSolarMutex() );
101 ::osl::MutexGuard
aGuard( m_aMutex
);
107 //-------------------------------------------------------------------------
108 Any SAL_CALL
OGenericUnoDialog::queryInterface(const Type
& _rType
) throw (RuntimeException
)
110 Any aReturn
= OGenericUnoDialogBase::queryInterface(_rType
);
112 if (!aReturn
.hasValue())
113 aReturn
= ::cppu::queryInterface(_rType
114 ,static_cast<XPropertySet
*>(this)
115 ,static_cast<XMultiPropertySet
*>(this)
116 ,static_cast<XFastPropertySet
*>(this)
122 //-------------------------------------------------------------------------
123 Sequence
<Type
> SAL_CALL
OGenericUnoDialog::getTypes( ) throw(RuntimeException
)
125 return ::comphelper::concatSequences(
126 OGenericUnoDialogBase::getTypes(),
127 ::comphelper::OPropertyContainer::getTypes()
131 //-------------------------------------------------------------------------
132 Sequence
<sal_Int8
> SAL_CALL
OGenericUnoDialog::getImplementationId( ) throw(RuntimeException
)
134 static ::cppu::OImplementationId aId
;
135 return aId
.getImplementationId();
138 //-------------------------------------------------------------------------
139 sal_Bool SAL_CALL
OGenericUnoDialog::supportsService(const ::rtl::OUString
& ServiceName
) throw(RuntimeException
)
141 Sequence
< ::rtl::OUString
> aSupported(getSupportedServiceNames());
142 const ::rtl::OUString
* pArray
= aSupported
.getConstArray();
143 for (sal_Int32 i
= 0; i
< aSupported
.getLength(); ++i
, ++pArray
)
144 if (pArray
->equals(ServiceName
))
149 //-------------------------------------------------------------------------
150 void OGenericUnoDialog::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
) throw(Exception
)
152 // TODO : need some handling if we're currently executing ...
154 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle
, rValue
);
156 if (UNODIALOG_PROPERTY_ID_TITLE
== nHandle
)
158 // from now on m_sTitle is valid
159 m_bTitleAmbiguous
= sal_False
;
162 m_pDialog
->SetText(String(m_sTitle
));
166 //-------------------------------------------------------------------------
167 sal_Bool
OGenericUnoDialog::convertFastPropertyValue( Any
& rConvertedValue
, Any
& rOldValue
, sal_Int32 nHandle
, const Any
& rValue
) throw(IllegalArgumentException
)
171 case UNODIALOG_PROPERTY_ID_PARENT
:
173 Reference
<starawt::XWindow
> xNew
;
174 ::cppu::extractInterface(xNew
, rValue
);
175 if (xNew
!= m_xParent
)
177 rConvertedValue
<<= xNew
;
178 rOldValue
<<= m_xParent
;
184 return OPropertyContainer::convertFastPropertyValue(rConvertedValue
, rOldValue
, nHandle
, rValue
);
187 //-------------------------------------------------------------------------
188 void SAL_CALL
OGenericUnoDialog::setTitle( const ::rtl::OUString
& _rTitle
) throw(RuntimeException
)
190 UnoDialogEntryGuard
aGuard( *this );
194 setPropertyValue(::rtl::OUString::createFromAscii(UNODIALOG_PROPERTY_TITLE
), makeAny(_rTitle
));
196 catch(RuntimeException
&)
201 catch( const Exception
& )
203 DBG_UNHANDLED_EXCEPTION();
204 // not allowed to pass
208 //-------------------------------------------------------------------------
209 bool OGenericUnoDialog::impl_ensureDialog_lck()
214 // get the parameters for the dialog from the current settings
217 Window
* pParent
= NULL
;
218 VCLXWindow
* pImplementation
= VCLXWindow::GetImplementation(m_xParent
);
220 pParent
= pImplementation
->GetWindow();
223 String sTitle
= m_sTitle
;
225 Dialog
* pDialog
= createDialog( pParent
);
226 OSL_ENSURE( pDialog
, "OGenericUnoDialog::impl_ensureDialog_lck: createDialog returned nonsense!" );
230 // do some initialisations
231 if ( !m_bTitleAmbiguous
)
232 pDialog
->SetText( sTitle
);
234 // be notified when the dialog is killed by somebody else
235 // #i65958# / 2006-07-07 / frank.schoenheit@sun.com
236 pDialog
->AddEventListener( LINK( this, OGenericUnoDialog
, OnDialogDying
) );
243 //-------------------------------------------------------------------------
244 sal_Int16 SAL_CALL
OGenericUnoDialog::execute( ) throw(RuntimeException
)
246 // both creation and execution of the dialog must be guarded with the SolarMutex, so be generous here
247 ::vos::OGuard
aSolarGuard( Application::GetSolarMutex() );
249 Dialog
* pDialogToExecute
= NULL
;
250 // create the dialog, if neccessary
252 UnoDialogEntryGuard
aGuard( *this );
255 throw RuntimeException(
256 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "already executing the dialog (recursive call)" ) ),
260 m_bCanceled
= sal_False
;
261 m_bExecuting
= sal_True
;
263 if ( !impl_ensureDialog_lck() )
266 pDialogToExecute
= m_pDialog
;
270 sal_Int16
nReturn(0);
271 if ( pDialogToExecute
)
272 nReturn
= pDialogToExecute
->Execute();
275 ::osl::MutexGuard
aExecutionGuard(m_aExecutionMutex
);
277 nReturn
= RET_CANCEL
;
281 ::osl::MutexGuard
aGuard(m_aMutex
);
283 // get the settings of the dialog
284 executedDialog( nReturn
);
286 m_bExecuting
= sal_False
;
294 //-------------------------------------------------------------------------
295 void SAL_CALL
OGenericUnoDialog::endExecute( ) throw(RuntimeException
)
297 UnoDialogEntryGuard
aGuard( *this );
299 throw RuntimeException();
302 ::osl::MutexGuard
aExecutionGuard(m_aExecutionMutex
);
303 OSL_ENSURE(m_pDialog
, "OGenericUnoDialog::endExecute : executing which dialog ?");
304 // m_bExecuting is true but we have no dialog ?
306 throw RuntimeException();
308 if (!m_pDialog
->IsInExecute())
309 // we tighly missed it ... another thread finished the execution of the dialog,
310 // but did not manage it to reset m_bExecuting, it currently tries to acquire
311 // m_aMutex or m_aExecutionMutex
315 m_pDialog
->EndDialog(RET_CANCEL
);
316 m_bCanceled
= sal_True
;
321 //-------------------------------------------------------------------------
322 void OGenericUnoDialog::implInitialize(const Any
& _rValue
)
326 PropertyValue aProperty
;
328 if ( _rValue
>>= aProperty
)
330 setPropertyValue( aProperty
.Name
, aProperty
.Value
);
332 else if ( _rValue
>>= aValue
)
334 setPropertyValue( aValue
.Name
, aValue
.Value
);
337 catch(const Exception
&)
339 DBG_UNHANDLED_EXCEPTION();
343 //-------------------------------------------------------------------------
344 void SAL_CALL
OGenericUnoDialog::initialize( const Sequence
< Any
>& aArguments
) throw(Exception
, RuntimeException
)
346 ::osl::MutexGuard
aGuard( m_aMutex
);
347 if ( m_bInitialized
)
348 throw AlreadyInitializedException( ::rtl::OUString(), *this );
350 const Any
* pArguments
= aArguments
.getConstArray();
351 for (sal_Int32 i
=0; i
<aArguments
.getLength(); ++i
, ++pArguments
)
352 implInitialize(*pArguments
);
354 m_bInitialized
= true;
357 //-------------------------------------------------------------------------
358 void OGenericUnoDialog::destroyDialog()
364 //-------------------------------------------------------------------------
365 IMPL_LINK( OGenericUnoDialog
, OnDialogDying
, VclWindowEvent
*, _pEvent
)
367 OSL_ENSURE( _pEvent
->GetWindow() == m_pDialog
, "OGenericUnoDialog::OnDialogDying: where does this come from?" );
368 if ( _pEvent
->GetId() == VCLEVENT_OBJECT_DYING
)
373 //.........................................................................
375 //.........................................................................