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 : #include "subcomponentmanager.hxx"
21 : #include "AppController.hxx"
22 : #include "dbustrings.hrc"
23 :
24 : #include <com/sun/star/frame/XFrame.hpp>
25 : #include <com/sun/star/frame/XModel.hpp>
26 : #include <com/sun/star/frame/XModel2.hpp>
27 : #include <com/sun/star/util/XCloseable.hpp>
28 : #include <com/sun/star/awt/XTopWindow.hpp>
29 : #include <com/sun/star/embed/XComponentSupplier.hpp>
30 : #include <com/sun/star/ucb/XCommandProcessor.hpp>
31 : #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
32 : #include <com/sun/star/beans/XPropertySet.hpp>
33 :
34 : #include <tools/diagnose_ex.h>
35 : #include <vcl/svapp.hxx>
36 : #include <osl/mutex.hxx>
37 :
38 : #include <algorithm>
39 : #include <functional>
40 :
41 : //......................................................................................................................
42 : namespace dbaui
43 : {
44 : //......................................................................................................................
45 :
46 : using ::com::sun::star::uno::Reference;
47 : using ::com::sun::star::uno::XInterface;
48 : using ::com::sun::star::uno::UNO_QUERY;
49 : using ::com::sun::star::uno::UNO_QUERY_THROW;
50 : using ::com::sun::star::uno::UNO_SET_THROW;
51 : using ::com::sun::star::uno::Exception;
52 : using ::com::sun::star::uno::RuntimeException;
53 : using ::com::sun::star::uno::Any;
54 : using ::com::sun::star::uno::makeAny;
55 : using ::com::sun::star::uno::Sequence;
56 : using ::com::sun::star::uno::Type;
57 : using ::com::sun::star::frame::XFrame;
58 : using ::com::sun::star::frame::XController;
59 : using ::com::sun::star::frame::XModel;
60 : using ::com::sun::star::lang::EventObject;
61 : using ::com::sun::star::lang::XComponent;
62 : using ::com::sun::star::frame::XModel2;
63 : using ::com::sun::star::container::XEnumeration;
64 : using ::com::sun::star::util::XCloseable;
65 : using ::com::sun::star::awt::XTopWindow;
66 : using ::com::sun::star::embed::XComponentSupplier;
67 : using ::com::sun::star::ucb::XCommandProcessor;
68 : using ::com::sun::star::ucb::Command;
69 : using ::com::sun::star::document::XDocumentEventBroadcaster;
70 : using ::com::sun::star::beans::XPropertySet;
71 : using ::com::sun::star::beans::PropertyChangeEvent;
72 :
73 : //==================================================================================================================
74 : //= helper structs
75 : //==================================================================================================================
76 : namespace
77 : {
78 : //..............................................................................................................
79 0 : struct SubComponentDescriptor
80 : {
81 : /// the name of the sub component, empty if it is yet unsaved
82 : OUString sName;
83 : /// type of the component - an ElementType value, except for relation design
84 : sal_Int32 nComponentType;
85 : /// the mode in which the sub component has been opened
86 : ElementOpenMode eOpenMode;
87 : /// the frame which the component resides in. Must not be <NULL/>
88 : Reference< XFrame > xFrame;
89 : /// the controller of the sub component. Must not be <NULL/>
90 : Reference< XController > xController;
91 : /// the model of the sub component. Might be <NULL/>
92 : Reference< XModel > xModel;
93 : /// the document definition which holds the component, if any; as CommandProcessor
94 : Reference< XCommandProcessor > xComponentCommandProcessor;
95 : /// the document definition which holds the component, if any; as PropertySet
96 : Reference< XPropertySet > xDocumentDefinitionProperties;
97 :
98 0 : SubComponentDescriptor()
99 : :sName()
100 : ,nComponentType( -1 )
101 : ,eOpenMode( E_OPEN_NORMAL )
102 : ,xFrame()
103 : ,xController()
104 0 : ,xModel()
105 : {
106 0 : }
107 :
108 0 : SubComponentDescriptor( const OUString& i_rName, const sal_Int32 i_nComponentType,
109 : const ElementOpenMode i_eOpenMode, const Reference< XComponent >& i_rComponent )
110 : :sName( i_rName )
111 : ,nComponentType( i_nComponentType )
112 0 : ,eOpenMode( i_eOpenMode )
113 : {
114 0 : if ( !impl_constructFrom( i_rComponent ) )
115 : {
116 : // i_rComponent is neither a model, nor a controller, nor a frame
117 : // => it must be a css.sdb.DocumentDefinition
118 0 : Reference< XComponentSupplier > xCompSupp( i_rComponent, UNO_QUERY_THROW );
119 0 : Reference< XComponent > xComponent( xCompSupp->getComponent(), UNO_QUERY_THROW );
120 0 : if ( !impl_constructFrom( xComponent ) )
121 0 : throw RuntimeException("Illegal component type.", NULL );
122 0 : xComponentCommandProcessor.set( i_rComponent, UNO_QUERY_THROW );
123 0 : xDocumentDefinitionProperties.set( i_rComponent, UNO_QUERY_THROW );
124 : }
125 0 : }
126 :
127 0 : inline bool is() const { return xFrame.is(); }
128 :
129 : private:
130 0 : bool impl_constructFrom( const Reference< XComponent >& _rxComponent )
131 : {
132 : // is it a model?
133 0 : xModel.set( _rxComponent, UNO_QUERY );
134 0 : if ( xModel.is() )
135 : {
136 0 : xController.set( xModel->getCurrentController() );
137 0 : if ( xController.is() )
138 0 : xFrame.set( xController->getFrame(), UNO_SET_THROW );
139 : }
140 : else
141 : {
142 : // is it a controller?
143 0 : xController.set( _rxComponent, UNO_QUERY );
144 0 : if ( xController.is() )
145 : {
146 0 : xFrame.set( xController->getFrame(), UNO_SET_THROW );
147 : }
148 : else
149 : {
150 : // is it a frame?
151 0 : xFrame.set( _rxComponent, UNO_QUERY );
152 0 : if ( !xFrame.is() )
153 0 : return false;
154 :
155 : // ensure we have a controller
156 0 : xController.set( xFrame->getController(), UNO_SET_THROW );
157 : }
158 :
159 : // check whether there is a model (not required)
160 0 : xModel.set( xController->getModel() );
161 : }
162 :
163 0 : return true;
164 : }
165 : };
166 :
167 : //..............................................................................................................
168 : struct SelectSubComponent : public ::std::unary_function< SubComponentDescriptor, Reference< XComponent > >
169 : {
170 0 : Reference< XComponent > operator()( const SubComponentDescriptor &_desc ) const
171 : {
172 0 : if ( _desc.xModel.is() )
173 0 : return _desc.xModel.get();
174 : OSL_ENSURE( _desc.xController.is(), "SelectSubComponent::operator(): illegal component!" );
175 0 : return _desc.xController.get();
176 : }
177 : };
178 :
179 : //..............................................................................................................
180 : typedef ::std::vector< SubComponentDescriptor > SubComponents;
181 :
182 : //..............................................................................................................
183 0 : struct SubComponentMatch : public ::std::unary_function< SubComponentDescriptor, bool >
184 : {
185 : public:
186 0 : SubComponentMatch( const OUString& i_rName, const sal_Int32 i_nComponentType,
187 : const ElementOpenMode i_eOpenMode )
188 : :m_sName( i_rName )
189 : ,m_nComponentType( i_nComponentType )
190 0 : ,m_eOpenMode( i_eOpenMode )
191 : {
192 0 : }
193 :
194 0 : bool operator()( const SubComponentDescriptor& i_rCompareWith ) const
195 : {
196 0 : return ( m_sName == i_rCompareWith.sName )
197 0 : && ( m_nComponentType == i_rCompareWith.nComponentType )
198 0 : && ( m_eOpenMode == i_rCompareWith.eOpenMode );
199 : }
200 : private:
201 : const OUString m_sName;
202 : const sal_Int32 m_nComponentType;
203 : const ElementOpenMode m_eOpenMode;
204 : };
205 : }
206 :
207 : //==================================================================================================================
208 : //= SubComponentManager_Data
209 : //==================================================================================================================
210 0 : struct SubComponentManager_Data
211 : {
212 0 : SubComponentManager_Data( OApplicationController& _rController, const ::comphelper::SharedMutex& _rMutex )
213 : :m_rController( _rController )
214 0 : ,m_aMutex( _rMutex )
215 : {
216 0 : }
217 :
218 : OApplicationController& m_rController;
219 : mutable ::comphelper::SharedMutex m_aMutex;
220 : SubComponents m_aComponents;
221 :
222 0 : ::osl::Mutex& getMutex() const { return m_aMutex; }
223 : };
224 :
225 : //==================================================================================================================
226 : //= SubComponentManager
227 : //==================================================================================================================
228 : //------------------------------------------------------------------------------------------------------------------
229 0 : SubComponentManager::SubComponentManager( OApplicationController& _rController, const ::comphelper::SharedMutex& _rMutex )
230 0 : :m_pData( new SubComponentManager_Data( _rController, _rMutex ) )
231 : {
232 0 : }
233 :
234 : //------------------------------------------------------------------------------------------------------------------
235 0 : SubComponentManager::~SubComponentManager()
236 : {
237 0 : }
238 :
239 : //------------------------------------------------------------------------------------------------------------------
240 0 : void SubComponentManager::disposing()
241 : {
242 0 : ::osl::MutexGuard aGuard( m_pData->getMutex() );
243 0 : m_pData->m_aComponents.clear();
244 0 : }
245 :
246 : //------------------------------------------------------------------------------------------------------------------
247 : namespace
248 : {
249 : //..............................................................................................................
250 0 : bool lcl_fallbackToAnotherController( SubComponentDescriptor& _rCompDesc )
251 : {
252 0 : Reference< XController > xFallback;
253 : OSL_PRECOND( _rCompDesc.xModel.is(), "lcl_fallbackToAnotherController: illegal call!" );
254 0 : if ( !_rCompDesc.xModel.is() )
255 0 : return false;
256 :
257 0 : xFallback.set( _rCompDesc.xModel->getCurrentController() );
258 0 : if ( xFallback == _rCompDesc.xController )
259 : // don't accept the very same controller as fallback
260 0 : xFallback.clear();
261 :
262 0 : if ( !xFallback.is() )
263 : {
264 : // perhaps XModel2 can be of help here
265 0 : Reference< XModel2 > xModel2( _rCompDesc.xModel, UNO_QUERY );
266 0 : Reference< XEnumeration > xControllerEnum;
267 0 : if ( xModel2.is() )
268 0 : xControllerEnum = xModel2->getControllers();
269 0 : while ( xControllerEnum.is() && xControllerEnum->hasMoreElements() )
270 : {
271 0 : xFallback.set( xControllerEnum->nextElement(), UNO_QUERY );
272 0 : if ( xFallback == _rCompDesc.xController )
273 0 : xFallback.clear();
274 0 : }
275 : }
276 :
277 0 : if ( xFallback.is() )
278 : {
279 0 : _rCompDesc.xController = xFallback;
280 0 : _rCompDesc.xFrame.set( xFallback->getFrame(), UNO_SET_THROW );
281 0 : return true;
282 : }
283 :
284 0 : return false;
285 : }
286 :
287 : //..............................................................................................................
288 0 : bool lcl_closeComponent( const Reference< XCommandProcessor >& _rxCommandProcessor )
289 : {
290 0 : bool bSuccess = false;
291 : try
292 : {
293 0 : sal_Int32 nCommandIdentifier = _rxCommandProcessor->createCommandIdentifier();
294 :
295 0 : Command aCommand;
296 0 : aCommand.Name = OUString( "close" );
297 0 : _rxCommandProcessor->execute( aCommand, nCommandIdentifier, NULL );
298 0 : bSuccess = true;
299 : }
300 0 : catch( const Exception& )
301 : {
302 : DBG_UNHANDLED_EXCEPTION();
303 : }
304 0 : return bSuccess;
305 : }
306 :
307 : //..............................................................................................................
308 0 : bool lcl_closeComponent( const SubComponentDescriptor& _rComponent )
309 : {
310 0 : if ( _rComponent.xComponentCommandProcessor.is() )
311 0 : return lcl_closeComponent( _rComponent.xComponentCommandProcessor );
312 :
313 0 : Reference< XController > xController( _rComponent.xController );
314 : OSL_ENSURE( xController.is(), "lcl_closeComponent: invalid controller!" );
315 :
316 : // suspend the controller in the document
317 0 : if ( xController.is() )
318 0 : if ( !xController->suspend( sal_True ) )
319 0 : return false;
320 :
321 0 : bool bSuccess = false;
322 : try
323 : {
324 0 : Reference< XCloseable > xCloseable( _rComponent.xFrame, UNO_QUERY_THROW );
325 0 : xCloseable->close( sal_True );
326 0 : bSuccess = true;
327 : }
328 0 : catch( const Exception& )
329 : {
330 : DBG_UNHANDLED_EXCEPTION();
331 : }
332 0 : return bSuccess;
333 : }
334 :
335 : //..............................................................................................................
336 0 : void lcl_notifySubComponentEvent( const SubComponentManager_Data& _rData, const sal_Char* _pAsciiEventName,
337 : const SubComponentDescriptor& _rComponent )
338 : {
339 : try
340 : {
341 0 : Reference< XDocumentEventBroadcaster > xBroadcaster( _rData.m_rController.getModel(), UNO_QUERY_THROW );
342 0 : xBroadcaster->notifyDocumentEvent(
343 : OUString::createFromAscii( _pAsciiEventName ),
344 : &_rData.m_rController,
345 : makeAny( _rComponent.xFrame )
346 0 : );
347 : }
348 0 : catch( const Exception& )
349 : {
350 : DBG_UNHANDLED_EXCEPTION();
351 : }
352 0 : }
353 : }
354 :
355 : //------------------------------------------------------------------------------------------------------------------
356 0 : void SAL_CALL SubComponentManager::propertyChange( const PropertyChangeEvent& i_rEvent ) throw (RuntimeException)
357 : {
358 0 : if ( i_rEvent.PropertyName != PROPERTY_NAME )
359 : // by definition, it's allowed to broadcast more than what we've registered for
360 0 : return;
361 :
362 : // find the sub component whose name changed
363 0 : for ( SubComponents::iterator comp = m_pData->m_aComponents.begin();
364 0 : comp != m_pData->m_aComponents.end();
365 : ++comp
366 : )
367 : {
368 0 : if ( comp->xDocumentDefinitionProperties != i_rEvent.Source )
369 0 : continue;
370 :
371 0 : OUString sNewName;
372 0 : OSL_VERIFY( i_rEvent.NewValue >>= sNewName );
373 :
374 : #if OSL_DEBUG_LEVEL > 0
375 : OUString sOldKnownName( comp->sName );
376 : OUString sOldName;
377 : OSL_VERIFY( i_rEvent.OldValue >>= sOldName );
378 : OSL_ENSURE( sOldName == sOldKnownName, "SubComponentManager::propertyChange: inconsistency in the old names!" );
379 : #endif
380 :
381 0 : comp->sName = sNewName;
382 0 : break;
383 0 : }
384 : }
385 :
386 : //------------------------------------------------------------------------------------------------------------------
387 0 : void SAL_CALL SubComponentManager::disposing( const EventObject& _rSource ) throw (RuntimeException)
388 : {
389 0 : ::osl::ClearableMutexGuard aGuard( m_pData->getMutex() );
390 :
391 0 : SubComponentDescriptor aClosedComponent;
392 :
393 0 : for ( SubComponents::iterator comp = m_pData->m_aComponents.begin();
394 0 : comp != m_pData->m_aComponents.end();
395 : ++comp
396 : )
397 : {
398 0 : bool bRemove = false;
399 :
400 0 : if ( comp->xController == _rSource.Source )
401 : {
402 0 : if ( !comp->xModel.is() )
403 : {
404 0 : bRemove = true;
405 : }
406 : else
407 : {
408 : // maybe this is just one view to the sub document, and only this view is closed
409 0 : if ( !lcl_fallbackToAnotherController( *comp ) )
410 : {
411 0 : bRemove = true;
412 : }
413 : }
414 : }
415 0 : else if ( comp->xModel == _rSource.Source )
416 : {
417 0 : bRemove = true;
418 : }
419 :
420 0 : if ( bRemove )
421 : {
422 0 : aClosedComponent = *comp;
423 0 : m_pData->m_aComponents.erase( comp );
424 0 : break;
425 : }
426 : }
427 :
428 0 : if ( aClosedComponent.is() )
429 : {
430 0 : aGuard.clear();
431 0 : lcl_notifySubComponentEvent( *m_pData, "OnSubComponentClosed", aClosedComponent );
432 0 : }
433 0 : }
434 :
435 : //------------------------------------------------------------------------------------------------------------------
436 0 : Sequence< Reference< XComponent> > SubComponentManager::getSubComponents() const
437 : {
438 0 : ::osl::MutexGuard aGuard( m_pData->getMutex() );
439 :
440 0 : Sequence< Reference< XComponent > > aComponents( m_pData->m_aComponents.size() );
441 : ::std::transform(
442 0 : m_pData->m_aComponents.begin(),
443 0 : m_pData->m_aComponents.end(),
444 : aComponents.getArray(),
445 : SelectSubComponent()
446 0 : );
447 0 : return aComponents;
448 : }
449 :
450 : //------------------------------------------------------------------------------------------------------------------
451 0 : sal_Bool SubComponentManager::closeSubComponents()
452 : {
453 0 : SolarMutexGuard aSolarGuard;
454 0 : ::osl::MutexGuard aGuard( m_pData->getMutex() );
455 :
456 : try
457 : {
458 0 : SubComponents aWorkingCopy( m_pData->m_aComponents );
459 0 : for ( SubComponents::const_iterator comp = aWorkingCopy.begin();
460 0 : comp != aWorkingCopy.end();
461 : ++comp
462 : )
463 : {
464 0 : lcl_closeComponent( *comp );
465 0 : }
466 : }
467 0 : catch ( const Exception& )
468 : {
469 : DBG_UNHANDLED_EXCEPTION();
470 : }
471 :
472 0 : return empty();
473 : }
474 :
475 : //------------------------------------------------------------------------------------------------------------------
476 0 : bool SubComponentManager::empty() const
477 : {
478 0 : ::osl::MutexGuard aGuard( m_pData->getMutex() );
479 0 : return m_pData->m_aComponents.empty();
480 : }
481 :
482 : //------------------------------------------------------------------------------------------------------------------
483 0 : void SubComponentManager::onSubComponentOpened( const OUString& _rName, const sal_Int32 _nComponentType,
484 : const ElementOpenMode _eOpenMode, const Reference< XComponent >& _rxComponent )
485 : {
486 0 : ::osl::ClearableMutexGuard aGuard( m_pData->getMutex() );
487 :
488 : #if OSL_DEBUG_LEVEL > 0
489 : if ( !_rName.isEmpty() )
490 : {
491 : // check there does not already exist such a component
492 : SubComponents::const_iterator existentPos = ::std::find_if(
493 : m_pData->m_aComponents.begin(),
494 : m_pData->m_aComponents.end(),
495 : SubComponentMatch( _rName, _nComponentType, _eOpenMode )
496 : );
497 : OSL_ENSURE( existentPos == m_pData->m_aComponents.end(), "already existent!" );
498 : }
499 : #endif
500 0 : SubComponentDescriptor aElement( _rName, _nComponentType, _eOpenMode, _rxComponent );
501 0 : ENSURE_OR_THROW( aElement.xModel.is() || aElement.xController.is(), "illegal component" );
502 :
503 0 : m_pData->m_aComponents.push_back( aElement );
504 :
505 : // add as listener
506 0 : if ( aElement.xController.is() )
507 0 : aElement.xController->addEventListener( this );
508 0 : if ( aElement.xModel.is() )
509 0 : aElement.xModel->addEventListener( this );
510 0 : if ( aElement.xDocumentDefinitionProperties.is() )
511 0 : aElement.xDocumentDefinitionProperties->addPropertyChangeListener( PROPERTY_NAME, this );
512 :
513 : // notify this to interested parties
514 0 : aGuard.clear();
515 0 : lcl_notifySubComponentEvent( *m_pData, "OnSubComponentOpened", aElement );
516 0 : }
517 :
518 : //------------------------------------------------------------------------------------------------------------------
519 0 : bool SubComponentManager::activateSubFrame( const OUString& _rName, const sal_Int32 _nComponentType,
520 : const ElementOpenMode _eOpenMode, Reference< XComponent >& o_rComponent ) const
521 : {
522 0 : ::osl::MutexGuard aGuard( m_pData->getMutex() );
523 :
524 : SubComponents::const_iterator pos = ::std::find_if(
525 0 : m_pData->m_aComponents.begin(),
526 0 : m_pData->m_aComponents.end(),
527 : SubComponentMatch( _rName, _nComponentType, _eOpenMode )
528 0 : );
529 0 : if ( pos == m_pData->m_aComponents.end() )
530 : // no component with this name/type/open mode
531 0 : return false;
532 :
533 0 : const Reference< XFrame > xFrame( pos->xFrame, UNO_SET_THROW );
534 0 : const Reference< XTopWindow > xTopWindow( xFrame->getContainerWindow(), UNO_QUERY_THROW );
535 0 : xTopWindow->toFront();
536 :
537 0 : if ( pos->xModel.is() )
538 0 : o_rComponent = pos->xModel.get();
539 0 : else if ( pos->xController.is() )
540 0 : o_rComponent = pos->xController.get();
541 : else
542 0 : o_rComponent = pos->xFrame.get();
543 :
544 0 : return true;
545 : }
546 :
547 : //------------------------------------------------------------------------------------------------------------------
548 0 : bool SubComponentManager::closeSubFrames( const OUString& i_rName, const sal_Int32 _nComponentType )
549 : {
550 0 : ::osl::MutexGuard aGuard( m_pData->getMutex() );
551 0 : ENSURE_OR_RETURN_FALSE( !i_rName.isEmpty(), "SubComponentManager::closeSubFrames: illegal name!" );
552 :
553 0 : SubComponents aWorkingCopy( m_pData->m_aComponents );
554 0 : for ( SubComponents::const_iterator comp = aWorkingCopy.begin();
555 0 : comp != aWorkingCopy.end();
556 : ++comp
557 : )
558 : {
559 0 : if ( ( comp->sName != i_rName ) || ( comp->nComponentType != _nComponentType ) )
560 0 : continue;
561 :
562 0 : if ( !lcl_closeComponent( *comp ) )
563 0 : return false;
564 : }
565 :
566 0 : return true;
567 : }
568 :
569 : //------------------------------------------------------------------------------------------------------------------
570 0 : bool SubComponentManager::lookupSubComponent( const Reference< XComponent >& i_rComponent,
571 : OUString& o_rName, sal_Int32& o_rComponentType )
572 : {
573 0 : for ( SubComponents::const_iterator comp = m_pData->m_aComponents.begin();
574 0 : comp != m_pData->m_aComponents.end();
575 : ++comp
576 : )
577 : {
578 0 : if ( ( comp->xModel.is()
579 0 : && ( comp->xModel == i_rComponent )
580 : )
581 0 : || ( comp->xController.is()
582 0 : && ( comp->xController == i_rComponent )
583 : )
584 0 : || ( comp->xFrame.is()
585 0 : && ( comp->xFrame == i_rComponent )
586 : )
587 : )
588 : {
589 0 : o_rName = comp->sName;
590 0 : o_rComponentType = comp->nComponentType;
591 0 : return true;
592 : }
593 : }
594 0 : return false;
595 : }
596 :
597 : //......................................................................................................................
598 12 : } // namespace dbaui
599 : //......................................................................................................................
600 :
601 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|