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