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 1 : OCommonPicker::OCommonPicker( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory )
50 : :OCommonPicker_Base( m_aMutex )
51 1 : ,OPropertyContainer( GetBroadcastHelper() )
52 : ,m_xORB( _rxFactory )
53 : ,m_pDlg( NULL )
54 : ,m_nCancelEvent( 0 )
55 2 : ,m_bExecuting( false )
56 : {
57 : // the two properties we have
58 : registerProperty(
59 : OUString( "HelpURL" ), PROPERTY_ID_HELPURL,
60 : PropertyAttribute::TRANSIENT,
61 1 : &m_sHelpURL, cppu::UnoType<decltype(m_sHelpURL)>::get()
62 1 : );
63 :
64 : registerProperty(
65 : OUString( "Window" ), PROPERTY_ID_WINDOW,
66 : PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY,
67 1 : &m_xWindow, cppu::UnoType<decltype(m_xWindow)>::get()
68 1 : );
69 1 : }
70 :
71 :
72 2 : OCommonPicker::~OCommonPicker()
73 : {
74 1 : if ( !GetBroadcastHelper().bDisposed )
75 : {
76 0 : acquire();
77 0 : dispose();
78 : }
79 1 : }
80 :
81 :
82 : // disambiguate XInterface
83 :
84 37 : 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 1 : void SAL_CALL OCommonPicker::disposing()
112 : {
113 1 : SolarMutexGuard aGuard;
114 :
115 1 : stopWindowListening();
116 :
117 1 : if ( m_nCancelEvent )
118 0 : Application::RemoveUserEvent( m_nCancelEvent );
119 :
120 : {
121 1 : ::osl::MutexGuard aOwnGuard( m_aMutex );
122 1 : if ( m_bExecuting && m_pDlg )
123 0 : m_pDlg->EndDialog( RET_CANCEL );
124 : }
125 :
126 1 : m_pDlg.disposeAndClear();
127 1 : m_xWindow = NULL;
128 1 : m_xDialogParent = NULL;
129 1 : }
130 :
131 :
132 1 : void OCommonPicker::stopWindowListening()
133 : {
134 1 : disposeComponent( m_xWindowListenerAdapter );
135 1 : disposeComponent( m_xParentListenerAdapter );
136 1 : }
137 :
138 :
139 : // XEventListener
140 :
141 0 : void SAL_CALL OCommonPicker::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
142 : {
143 0 : SolarMutexGuard aGuard;
144 0 : bool bDialogDying = _rSource.Source == m_xWindow;
145 0 : bool bParentDying = _rSource.Source == m_xDialogParent;
146 :
147 0 : if ( bDialogDying || bParentDying )
148 : {
149 0 : stopWindowListening();
150 :
151 0 : if ( !bDialogDying ) // it's the parent which is dying -> delete the dialog
152 0 : m_pDlg.disposeAndClear();
153 : else
154 0 : m_pDlg.clear();
155 :
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 *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.reset( 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 nullptr != 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 : return svt::OControlAccess::isControlSupported( aControlName );
302 : }
303 :
304 0 : return sal_False;
305 : }
306 :
307 :
308 0 : Sequence< OUString > SAL_CALL OCommonPicker::getSupportedControlProperties( const OUString& aControlName ) throw (IllegalArgumentException, RuntimeException, std::exception)
309 : {
310 0 : checkAlive();
311 :
312 0 : SolarMutexGuard aGuard;
313 0 : if ( createPicker() )
314 : {
315 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
316 0 : return aAccess.getSupportedControlProperties( aControlName );
317 : }
318 :
319 0 : return Sequence< OUString >();
320 : }
321 :
322 :
323 0 : sal_Bool SAL_CALL OCommonPicker::isControlPropertySupported( const OUString& aControlName, const OUString& aControlProperty ) throw (IllegalArgumentException, RuntimeException, std::exception)
324 : {
325 0 : checkAlive();
326 :
327 0 : SolarMutexGuard aGuard;
328 0 : if ( createPicker() )
329 : {
330 0 : ::svt::OControlAccess aAccess( m_pDlg, m_pDlg->GetView() );
331 0 : return aAccess.isControlPropertySupported( aControlName, aControlProperty );
332 : }
333 :
334 0 : return sal_False;
335 : }
336 :
337 :
338 : // XExecutableDialog functions
339 :
340 0 : void SAL_CALL OCommonPicker::setTitle( const OUString& _rTitle ) throw( RuntimeException, std::exception )
341 : {
342 0 : SolarMutexGuard aGuard;
343 0 : m_aTitle = _rTitle;
344 0 : }
345 :
346 :
347 0 : sal_Int16 OCommonPicker::execute() throw (RuntimeException, std::exception)
348 : {
349 0 : SolarMutexGuard aGuard;
350 :
351 0 : prepareDialog();
352 :
353 : {
354 0 : ::osl::MutexGuard aOwnGuard( m_aMutex );
355 0 : m_bExecuting = true;
356 : }
357 0 : sal_Int16 nResult = implExecutePicker();
358 : {
359 0 : ::osl::MutexGuard aOwnGuard( m_aMutex );
360 0 : m_bExecuting = false;
361 : }
362 :
363 0 : return nResult;
364 : }
365 :
366 :
367 : // XCancellable functions
368 :
369 0 : void SAL_CALL OCommonPicker::cancel( ) throw (RuntimeException, std::exception)
370 : {
371 : {
372 0 : ::osl::MutexGuard aGuard( m_aMutex );
373 0 : if ( m_nCancelEvent )
374 : // nothing to do - the event for cancelling the dialog is already on the way
375 0 : return;
376 : }
377 :
378 : // The thread which executes our dialog has locked the solar mutex for
379 : // sure. Cancelling the dialog should be done with a locked solar mutex, too.
380 : // Thus we post ourself a message for cancelling the dialog. This way, the message
381 : // is either handled in the thread which opened the dialog (which may even be
382 : // this thread here), or, if no dialog is open, in the thread doing scheduling
383 : // currently. Both is okay for us ....
384 :
385 : // Note that we could do check if we are really executing the dialog currently.
386 : // but the information would be potentially obsolete at the moment our event
387 : // arrives, so we need to check it there, anyway ...
388 0 : m_nCancelEvent = Application::PostUserEvent( LINK( this, OCommonPicker, OnCancelPicker ) );
389 : }
390 :
391 :
392 0 : IMPL_LINK_NOARG(OCommonPicker, OnCancelPicker)
393 : {
394 : // By definition, the solar mutex is locked when we arrive here. Note that this
395 : // is important, as for instance the consistency of m_pDlg depends on this mutex.
396 0 : ::osl::MutexGuard aGuard( m_aMutex );
397 0 : m_nCancelEvent = 0;
398 :
399 0 : if ( !m_bExecuting )
400 : // nothing to do. This may be because the dialog was canceled after our cancel method
401 : // posted this async event, or because somebody called cancel without the dialog
402 : // being executed at this time.
403 0 : return 0;
404 :
405 : OSL_ENSURE( getDialog(), "OCommonPicker::OnCancelPicker: executing, but no dialog!" );
406 0 : if ( getDialog() )
407 0 : getDialog()->EndDialog( RET_CANCEL );
408 :
409 0 : return 0L;
410 : }
411 :
412 :
413 : // XInitialization functions
414 :
415 0 : void SAL_CALL OCommonPicker::initialize( const Sequence< Any >& _rArguments )
416 : throw ( Exception, RuntimeException, std::exception )
417 : {
418 0 : checkAlive();
419 :
420 0 : OUString sSettingName;
421 0 : Any aSettingValue;
422 :
423 0 : PropertyValue aPropArg;
424 0 : NamedValue aPairArg;
425 :
426 :
427 0 : const Any* pArguments = _rArguments.getConstArray();
428 0 : const Any* pArgumentsEnd = _rArguments.getConstArray() + _rArguments.getLength();
429 0 : for ( const Any* pArgument = pArguments;
430 : pArgument != pArgumentsEnd;
431 : ++pArgument
432 : )
433 : {
434 0 : if ( *pArgument >>= aPropArg )
435 : {
436 0 : if ( aPropArg.Name.isEmpty())
437 0 : continue;
438 :
439 0 : sSettingName = aPropArg.Name;
440 0 : aSettingValue = aPropArg.Value;
441 : }
442 0 : else if ( *pArgument >>= aPairArg )
443 : {
444 0 : if ( aPairArg.Name.isEmpty())
445 0 : continue;
446 :
447 0 : sSettingName = aPairArg.Name;
448 0 : aSettingValue = aPairArg.Value;
449 :
450 :
451 : }
452 : else
453 : {
454 : OSL_FAIL(
455 : ( OString( "OCommonPicker::initialize: unknown argument type at position " )
456 : += OString::number( pArguments - _rArguments.getConstArray() )
457 : ).getStr()
458 : );
459 0 : continue;
460 : }
461 :
462 : #ifdef DBG_UTIL
463 : bool bKnownSetting =
464 : #endif
465 0 : implHandleInitializationArgument( sSettingName, aSettingValue );
466 : DBG_ASSERT( bKnownSetting,
467 : ( OString( "OCommonPicker::initialize: unknown argument \"" )
468 : += OString( sSettingName.getStr(), sSettingName.getLength(), osl_getThreadTextEncoding() )
469 : += OString( "\"!" )
470 : ).getStr()
471 : );
472 0 : }
473 0 : }
474 :
475 :
476 0 : bool OCommonPicker::implHandleInitializationArgument( const OUString& _rName, const Any& _rValue )
477 : {
478 0 : bool bKnown = true;
479 0 : if ( _rName == "ParentWindow" )
480 : {
481 0 : m_xDialogParent.clear();
482 0 : OSL_VERIFY( _rValue >>= m_xDialogParent );
483 : OSL_ENSURE( VCLUnoHelper::GetWindow( m_xDialogParent ), "OCommonPicker::implHandleInitializationArgument: invalid parent window given!" );
484 : }
485 : else
486 0 : bKnown = false;
487 0 : return bKnown;
488 : }
489 :
490 :
491 3 : } // namespace svt
492 :
493 :
494 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|