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