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