Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "commonpicker.hxx"
22 : #include <com/sun/star/beans/PropertyAttribute.hpp>
23 : #include <com/sun/star/beans/NamedValue.hpp>
24 : #include <vcl/svapp.hxx>
25 : #include <osl/mutex.hxx>
26 : #include <toolkit/helper/vclunohelper.hxx>
27 : #include <comphelper/weakeventlistener.hxx>
28 : #include <comphelper/types.hxx>
29 : #include <vcl/msgbox.hxx>
30 : #include "iodlg.hxx"
31 :
32 :
33 : namespace svt
34 : {
35 :
36 :
37 : #define PROPERTY_ID_HELPURL 1
38 : #define PROPERTY_ID_WINDOW 2
39 :
40 : // using --------------------------------------------------------------
41 :
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::ui::dialogs;
44 : using namespace ::com::sun::star::uno;
45 : using namespace ::com::sun::star::beans;
46 : using namespace ::comphelper;
47 :
48 :
49 2 : OCommonPicker::OCommonPicker( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory )
50 : :OCommonPicker_Base( m_aMutex )
51 2 : ,OPropertyContainer( GetBroadcastHelper() )
52 : ,m_xORB( _rxFactory )
53 : ,m_pDlg( NULL )
54 : ,m_nCancelEvent( 0 )
55 4 : ,m_bExecuting( false )
56 : {
57 : // the two properties we have
58 : registerProperty(
59 : OUString( "HelpURL" ), PROPERTY_ID_HELPURL,
60 : PropertyAttribute::TRANSIENT,
61 2 : &m_sHelpURL, ::getCppuType( &m_sHelpURL )
62 2 : );
63 :
64 : registerProperty(
65 : OUString( "Window" ), PROPERTY_ID_WINDOW,
66 : PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY,
67 2 : &m_xWindow, ::getCppuType( &m_xWindow )
68 2 : );
69 2 : }
70 :
71 :
72 4 : OCommonPicker::~OCommonPicker()
73 : {
74 2 : if ( !GetBroadcastHelper().bDisposed )
75 : {
76 0 : acquire();
77 0 : dispose();
78 : }
79 2 : }
80 :
81 :
82 : // disambiguate XInterface
83 :
84 58 : IMPLEMENT_FORWARD_XINTERFACE2( OCommonPicker, OCommonPicker_Base, OPropertyContainer )
85 :
86 :
87 : // disambiguate XTypeProvider
88 :
89 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OCommonPicker, OCommonPicker_Base, OPropertyContainer )
90 :
91 :
92 : // XComponent related methods
93 :
94 0 : void OCommonPicker::checkAlive() const
95 : {
96 0 : if ( GetBroadcastHelper().bInDispose || GetBroadcastHelper().bDisposed )
97 0 : throw DisposedException();
98 0 : }
99 :
100 0 : void OCommonPicker::prepareDialog()
101 : {
102 0 : if(createPicker())
103 : {
104 : // set the title
105 0 : if ( !m_aTitle.isEmpty() )
106 0 : m_pDlg->SetText( m_aTitle );
107 : }
108 0 : }
109 :
110 :
111 2 : void SAL_CALL OCommonPicker::disposing()
112 : {
113 2 : SolarMutexGuard aGuard;
114 :
115 2 : stopWindowListening();
116 :
117 2 : if ( m_nCancelEvent )
118 0 : Application::RemoveUserEvent( m_nCancelEvent );
119 :
120 : {
121 2 : ::osl::MutexGuard aOwnGuard( m_aMutex );
122 2 : if ( m_bExecuting && m_pDlg )
123 0 : m_pDlg->EndDialog( RET_CANCEL );
124 : }
125 :
126 2 : delete m_pDlg;
127 2 : m_pDlg = NULL;
128 2 : m_xWindow = NULL;
129 2 : m_xDialogParent = NULL;
130 2 : }
131 :
132 :
133 2 : void OCommonPicker::stopWindowListening()
134 : {
135 2 : disposeComponent( m_xWindowListenerAdapter );
136 2 : disposeComponent( m_xParentListenerAdapter );
137 2 : }
138 :
139 :
140 : // XEventListener
141 :
142 0 : void SAL_CALL OCommonPicker::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
143 : {
144 0 : SolarMutexGuard aGuard;
145 0 : bool bDialogDying = _rSource.Source == m_xWindow;
146 0 : bool bParentDying = _rSource.Source == m_xDialogParent;
147 :
148 0 : if ( bDialogDying || bParentDying )
149 : {
150 0 : stopWindowListening();
151 :
152 0 : if ( !bDialogDying ) // it's the parent which is dying -> delete the dialog
153 0 : delete m_pDlg;
154 :
155 0 : m_pDlg = NULL;
156 0 : m_xWindow = NULL;
157 0 : m_xDialogParent = NULL;
158 : }
159 : else
160 : {
161 : OSL_FAIL( "OCommonPicker::disposing: where did this come from?" );
162 0 : }
163 0 : }
164 :
165 :
166 : // property set related methods
167 :
168 0 : ::cppu::IPropertyArrayHelper* OCommonPicker::createArrayHelper( ) const
169 : {
170 0 : Sequence< Property > aProps;
171 0 : describeProperties( aProps );
172 0 : return new cppu::OPropertyArrayHelper( aProps );
173 : }
174 :
175 :
176 0 : ::cppu::IPropertyArrayHelper& SAL_CALL OCommonPicker::getInfoHelper()
177 : {
178 0 : return *const_cast< OCommonPicker* >( this )->getArrayHelper();
179 : }
180 :
181 :
182 0 : Reference< XPropertySetInfo > SAL_CALL OCommonPicker::getPropertySetInfo( ) throw(RuntimeException, std::exception)
183 : {
184 0 : return ::cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
185 : }
186 :
187 :
188 0 : void SAL_CALL OCommonPicker::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception, std::exception)
189 : {
190 0 : OPropertyContainer::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
191 :
192 : // if the HelpURL changed, forward this to the dialog
193 0 : if ( PROPERTY_ID_HELPURL == _nHandle )
194 0 : if ( m_pDlg )
195 0 : OControlAccess::setHelpURL( m_pDlg, m_sHelpURL, false );
196 0 : }
197 :
198 :
199 :
200 0 : bool OCommonPicker::createPicker()
201 : {
202 0 : if ( !m_pDlg )
203 : {
204 0 : m_pDlg = implCreateDialog( VCLUnoHelper::GetWindow( m_xDialogParent ) );
205 : DBG_ASSERT( m_pDlg, "OCommonPicker::createPicker: invalid dialog returned!" );
206 :
207 0 : if ( m_pDlg )
208 : {
209 : // synchronize the help id of the dialog with out help URL property
210 0 : if ( !m_sHelpURL.isEmpty() )
211 : { // somebody already set the help URL while we had no dialog yet
212 0 : OControlAccess::setHelpURL( m_pDlg, m_sHelpURL, false );
213 : }
214 : else
215 : {
216 0 : m_sHelpURL = OControlAccess::getHelpURL( m_pDlg, false );
217 : }
218 :
219 0 : m_xWindow = VCLUnoHelper::GetInterface( m_pDlg );
220 :
221 : // add as event listener to the window
222 0 : Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY );
223 : OSL_ENSURE( xWindowComp.is(), "OCommonPicker::createFileDialog: invalid window component!" );
224 0 : if ( xWindowComp.is() )
225 : {
226 0 : m_xWindowListenerAdapter = new OWeakEventListenerAdapter( this, xWindowComp );
227 : // the adapter will add itself as listener, and forward notifications
228 : }
229 :
230 : // _and_ add as event listener to the parent - in case the parent is destroyed
231 : // before we are disposed, our disposal would access dead VCL windows then ....
232 0 : m_xDialogParent = VCLUnoHelper::GetInterface( m_pDlg->GetParent() );
233 0 : xWindowComp.set(m_xDialogParent, css::uno::UNO_QUERY);
234 : OSL_ENSURE( xWindowComp.is() || !m_pDlg->GetParent(), "OCommonPicker::createFileDialog: invalid window component (the parent this time)!" );
235 0 : if ( xWindowComp.is() )
236 : {
237 0 : m_xParentListenerAdapter = new OWeakEventListenerAdapter( this, xWindowComp );
238 : // the adapter will add itself as listener, and forward notifications
239 0 : }
240 : }
241 : }
242 :
243 0 : return NULL != m_pDlg;
244 : }
245 :
246 :
247 : // XControlAccess functions
248 :
249 0 : void SAL_CALL OCommonPicker::setControlProperty( const OUString& aControlName, const OUString& aControlProperty, const Any& aValue ) throw (IllegalArgumentException, RuntimeException, std::exception)
250 : {
251 0 : checkAlive();
252 :
253 0 : SolarMutexGuard aGuard;
254 0 : if ( createPicker() )
255 : {
256 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
257 0 : aAccess.setControlProperty( aControlName, aControlProperty, aValue );
258 0 : }
259 0 : }
260 :
261 :
262 0 : Any SAL_CALL OCommonPicker::getControlProperty( const OUString& aControlName, const OUString& aControlProperty ) throw (IllegalArgumentException, RuntimeException, std::exception)
263 : {
264 0 : checkAlive();
265 :
266 0 : SolarMutexGuard aGuard;
267 0 : if ( createPicker() )
268 : {
269 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
270 0 : return aAccess.getControlProperty( aControlName, aControlProperty );
271 : }
272 :
273 0 : return Any();
274 : }
275 :
276 :
277 : // XControlInformation functions
278 :
279 0 : Sequence< OUString > SAL_CALL OCommonPicker::getSupportedControls( ) throw (RuntimeException, std::exception)
280 : {
281 0 : checkAlive();
282 :
283 0 : SolarMutexGuard aGuard;
284 0 : if ( createPicker() )
285 : {
286 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
287 0 : return aAccess.getSupportedControls( );
288 : }
289 :
290 0 : return Sequence< OUString >();
291 : }
292 :
293 :
294 0 : sal_Bool SAL_CALL OCommonPicker::isControlSupported( const OUString& aControlName ) throw (RuntimeException, std::exception)
295 : {
296 0 : checkAlive();
297 :
298 0 : SolarMutexGuard aGuard;
299 0 : if ( createPicker() )
300 : {
301 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
302 0 : return aAccess.isControlSupported( aControlName );
303 : }
304 :
305 0 : return sal_False;
306 : }
307 :
308 :
309 0 : Sequence< OUString > SAL_CALL OCommonPicker::getSupportedControlProperties( const OUString& aControlName ) throw (IllegalArgumentException, RuntimeException, std::exception)
310 : {
311 0 : checkAlive();
312 :
313 0 : SolarMutexGuard aGuard;
314 0 : if ( createPicker() )
315 : {
316 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
317 0 : return aAccess.getSupportedControlProperties( aControlName );
318 : }
319 :
320 0 : return Sequence< OUString >();
321 : }
322 :
323 :
324 0 : sal_Bool SAL_CALL OCommonPicker::isControlPropertySupported( const OUString& aControlName, const OUString& aControlProperty ) throw (IllegalArgumentException, RuntimeException, std::exception)
325 : {
326 0 : checkAlive();
327 :
328 0 : SolarMutexGuard aGuard;
329 0 : if ( createPicker() )
330 : {
331 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
332 0 : return aAccess.isControlPropertySupported( aControlName, aControlProperty );
333 : }
334 :
335 0 : return sal_False;
336 : }
337 :
338 :
339 : // XExecutableDialog functions
340 :
341 0 : void SAL_CALL OCommonPicker::setTitle( const OUString& _rTitle ) throw( RuntimeException, std::exception )
342 : {
343 0 : SolarMutexGuard aGuard;
344 0 : m_aTitle = _rTitle;
345 0 : }
346 :
347 :
348 0 : sal_Int16 OCommonPicker::execute() throw (RuntimeException, std::exception)
349 : {
350 0 : SolarMutexGuard aGuard;
351 :
352 0 : prepareDialog();
353 :
354 : {
355 0 : ::osl::MutexGuard aOwnGuard( m_aMutex );
356 0 : m_bExecuting = true;
357 : }
358 0 : sal_Int16 nResult = implExecutePicker();
359 : {
360 0 : ::osl::MutexGuard aOwnGuard( m_aMutex );
361 0 : m_bExecuting = false;
362 : }
363 :
364 0 : return nResult;
365 : }
366 :
367 :
368 : // XCancellable functions
369 :
370 0 : void SAL_CALL OCommonPicker::cancel( ) throw (RuntimeException, std::exception)
371 : {
372 : {
373 0 : ::osl::MutexGuard aGuard( m_aMutex );
374 0 : if ( m_nCancelEvent )
375 : // nothing to do - the event for cancelling the dialog is already on the way
376 0 : return;
377 : }
378 :
379 : // The thread which executes our dialog has locked the solar mutex for
380 : // sure. Cancelling the dialog should be done with a locked solar mutex, too.
381 : // Thus we post ourself a message for cancelling the dialog. This way, the message
382 : // is either handled in the thread which opened the dialog (which may even be
383 : // this thread here), or, if no dialog is open, in the thread doing scheduling
384 : // currently. Both is okay for us ....
385 :
386 : // Note that we could do check if we are really executing the dialog currently.
387 : // but the information would be potentially obsolete at the moment our event
388 : // arrives, so we need to check it there, anyway ...
389 0 : m_nCancelEvent = Application::PostUserEvent( LINK( this, OCommonPicker, OnCancelPicker ) );
390 : }
391 :
392 :
393 0 : IMPL_LINK_NOARG(OCommonPicker, OnCancelPicker)
394 : {
395 : // By definition, the solar mutex is locked when we arrive here. Note that this
396 : // is important, as for instance the consistency of m_pDlg depends on this mutex.
397 0 : ::osl::MutexGuard aGuard( m_aMutex );
398 0 : m_nCancelEvent = 0;
399 :
400 0 : if ( !m_bExecuting )
401 : // nothing to do. This may be because the dialog was canceled after our cancel method
402 : // posted this async event, or because somebody called cancel without the dialog
403 : // being executed at this time.
404 0 : return 0;
405 :
406 : OSL_ENSURE( getDialog(), "OCommonPicker::OnCancelPicker: executing, but no dialog!" );
407 0 : if ( getDialog() )
408 0 : getDialog()->EndDialog( RET_CANCEL );
409 :
410 0 : return 0L;
411 : }
412 :
413 :
414 : // XInitialization functions
415 :
416 0 : void SAL_CALL OCommonPicker::initialize( const Sequence< Any >& _rArguments )
417 : throw ( Exception, RuntimeException, std::exception )
418 : {
419 0 : checkAlive();
420 :
421 0 : OUString sSettingName;
422 0 : Any aSettingValue;
423 :
424 0 : PropertyValue aPropArg;
425 0 : NamedValue aPairArg;
426 :
427 :
428 0 : const Any* pArguments = _rArguments.getConstArray();
429 0 : const Any* pArgumentsEnd = _rArguments.getConstArray() + _rArguments.getLength();
430 0 : for ( const Any* pArgument = pArguments;
431 : pArgument != pArgumentsEnd;
432 : ++pArgument
433 : )
434 : {
435 0 : if ( *pArgument >>= aPropArg )
436 : {
437 0 : if ( aPropArg.Name.isEmpty())
438 0 : continue;
439 :
440 0 : sSettingName = aPropArg.Name;
441 0 : aSettingValue = aPropArg.Value;
442 : }
443 0 : else if ( *pArgument >>= aPairArg )
444 : {
445 0 : if ( aPairArg.Name.isEmpty())
446 0 : continue;
447 :
448 0 : sSettingName = aPairArg.Name;
449 0 : aSettingValue = aPairArg.Value;
450 :
451 :
452 : }
453 : else
454 : {
455 : OSL_FAIL(
456 : ( OString( "OCommonPicker::initialize: unknown argument type at position " )
457 : += OString::number( pArguments - _rArguments.getConstArray() )
458 : ).getStr()
459 : );
460 0 : continue;
461 : }
462 :
463 : #ifdef DBG_UTIL
464 : bool bKnownSetting =
465 : #endif
466 0 : implHandleInitializationArgument( sSettingName, aSettingValue );
467 : DBG_ASSERT( bKnownSetting,
468 : ( OString( "OCommonPicker::initialize: unknown argument \"" )
469 : += OString( sSettingName.getStr(), sSettingName.getLength(), osl_getThreadTextEncoding() )
470 : += OString( "\"!" )
471 : ).getStr()
472 : );
473 0 : }
474 0 : }
475 :
476 :
477 0 : bool OCommonPicker::implHandleInitializationArgument( const OUString& _rName, const Any& _rValue )
478 : {
479 0 : bool bKnown = true;
480 0 : if ( _rName == "ParentWindow" )
481 : {
482 0 : m_xDialogParent.clear();
483 0 : OSL_VERIFY( _rValue >>= m_xDialogParent );
484 : OSL_ENSURE( VCLUnoHelper::GetWindow( m_xDialogParent ), "OCommonPicker::implHandleInitializationArgument: invalid parent window given!" );
485 : }
486 : else
487 0 : bKnown = false;
488 0 : return bKnown;
489 : }
490 :
491 :
492 6 : } // namespace svt
493 :
494 :
495 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|