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