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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/types.h>

#include <com/sun/star/task/theJobExecutor.hpp>
#include <com/sun/star/container/XNameReplace.hpp>
#include <com/sun/star/container/XSet.hpp>
#include <com/sun/star/document/XEventListener.hpp>
#include <com/sun/star/document/XEventBroadcaster.hpp>
#include <com/sun/star/document/XDocumentEventListener.hpp>
#include <com/sun/star/frame/XGlobalEventBroadcaster.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/lang/NoSupportException.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/uno/Type.hxx>

#include <cppuhelper/implbase.hxx>
#include <comphelper/interfacecontainer2.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <rtl/ref.hxx>
#include <comphelper/enumhelper.hxx>
#include <sfx2/app.hxx>
#include <tools/diagnose_ex.h>
#include <unotools/eventcfg.hxx>
#include <eventsupplier.hxx>

#include <set>
#include <vector>

using namespace css;

namespace {

struct ModelCollectionMutexBase
{
public:
    osl::Mutex m_aLock;
};

typedef ::std::vector< css::uno::Reference< css::frame::XModel > > TModelList;


//TODO: remove support of obsolete document::XEventBroadcaster/Listener
class SfxGlobalEvents_Impl : public ModelCollectionMutexBase
                           , public ::cppu::WeakImplHelper< css::lang::XServiceInfo
                                                           , css::frame::XGlobalEventBroadcaster
                                                           , css::document::XEventBroadcaster
                                                           , css::document::XEventListener
                                                           , css::lang::XComponent
                                                            >
{
    css::uno::Reference< css::container::XNameReplace > m_xEvents;
    css::uno::Reference< css::document::XEventListener > m_xJobExecutorListener;
    ::comphelper::OInterfaceContainerHelper2 m_aLegacyListeners;
    ::comphelper::OInterfaceContainerHelper2 m_aDocumentListeners;
    std::multiset<css::uno::Reference<css::lang::XEventListener>> m_disposeListeners;
    TModelList m_lModels;
    bool m_disposed;

public:
    explicit SfxGlobalEvents_Impl(const css::uno::Reference < css::uno::XComponentContext >& rxContext);

    virtual OUString SAL_CALL getImplementationName() override
    {
        return "com.sun.star.comp.sfx2.GlobalEventBroadcaster";
    }

    virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
    {
        return cppu::supportsService(this, ServiceName);
    }

    virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
    {
        css::uno::Sequence< OUString > aSeq { "com.sun.star.frame.GlobalEventBroadcaster" };
        return aSeq;
    }

    // css.document.XEventBroadcaster
    virtual css::uno::Reference< css::container::XNameReplace > SAL_CALL getEvents() override;

    virtual void SAL_CALL addEventListener(const css::uno::Reference< css::document::XEventListener >& xListener) override;

    virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::document::XEventListener >& xListener) override;

    // css.document.XDocumentEventBroadcaster
    virtual void SAL_CALL addDocumentEventListener( const css::uno::Reference< css::document::XDocumentEventListener >& Listener ) override;
    virtual void SAL_CALL removeDocumentEventListener( const css::uno::Reference< css::document::XDocumentEventListener >& Listener ) override;
    virtual void SAL_CALL notifyDocumentEvent( const OUString& EventName, const css::uno::Reference< css::frame::XController2 >& ViewController, const css::uno::Any& Supplement ) override;

    // css.document.XEventListener
    virtual void SAL_CALL notifyEvent(const css::document::EventObject& aEvent) override;

    // css.document.XDocumentEventListener
    virtual void SAL_CALL documentEventOccured( const css::document::DocumentEvent& Event ) override;

    // css.container.XSet
    virtual sal_Bool SAL_CALL has(const css::uno::Any& aElement) override;

    virtual void SAL_CALL insert(const css::uno::Any& aElement) override;

    virtual void SAL_CALL remove(const css::uno::Any& aElement) override;

    // css.container.XEnumerationAccess
    virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override;

    // css.container.XElementAccess
    virtual css::uno::Type SAL_CALL getElementType() override;

    virtual sal_Bool SAL_CALL hasElements() override;

    // css.lang.XEventListener
    virtual void SAL_CALL disposing(const css::lang::EventObject& aEvent) override;

    // css.lang.XComponent
    void SAL_CALL dispose() override;

    void SAL_CALL addEventListener(css::uno::Reference<css::lang::XEventListener> const & xListener)
        override;

    void SAL_CALL removeEventListener(
        css::uno::Reference<css::lang::XEventListener> const & aListener) override;

private:

    // threadsafe
    void implts_notifyJobExecution(const css::document::EventObject& aEvent);
    void implts_checkAndExecuteEventBindings(const css::document::DocumentEvent& aEvent);
    void implts_notifyListener(const css::document::DocumentEvent& aEvent);

    // not threadsafe
    TModelList::iterator impl_searchDoc(const css::uno::Reference< css::frame::XModel >& xModel);
};

SfxGlobalEvents_Impl::SfxGlobalEvents_Impl( const uno::Reference < uno::XComponentContext >& rxContext)
    : ModelCollectionMutexBase(       )
    , m_xJobExecutorListener( task::theJobExecutor::get( rxContext ), uno::UNO_QUERY_THROW )
    , m_aLegacyListeners      (m_aLock)
    , m_aDocumentListeners    (m_aLock)
    , m_disposed(false)
{
    osl_atomic_increment(&m_refCount);
    SfxApplication::GetOrCreate();
    m_xEvents = new GlobalEventConfig();
    osl_atomic_decrement(&m_refCount);
}

uno::Reference< container::XNameReplace > SAL_CALL SfxGlobalEvents_Impl::getEvents()
{
    // SAFE ->
    osl::MutexGuard aLock(m_aLock);
    if (m_disposed) {
        throw css::lang::DisposedException();
    }
    return m_xEvents;
    // <- SAFE
}


void SAL_CALL SfxGlobalEvents_Impl::addEventListener(const uno::Reference< document::XEventListener >& xListener)
{
    {
        osl::MutexGuard g(m_aLock);
        if (m_disposed) {
            throw css::lang::DisposedException();
        }
    }
    // container is threadsafe
    m_aLegacyListeners.addInterface(xListener);
    // Take care of an XComponent::dispose being processed in parallel with the above addInterface:
    {
        osl::MutexGuard g(m_aLock);
        if (!m_disposed) {
            return;
        }
    }
    m_aLegacyListeners.disposeAndClear({static_cast<OWeakObject *>(this)});
}


void SAL_CALL SfxGlobalEvents_Impl::removeEventListener(const uno::Reference< document::XEventListener >& xListener)
{
    // The below removeInterface will silently do nothing when m_disposed:
    // container is threadsafe
    m_aLegacyListeners.removeInterface(xListener);
}


void SAL_CALL SfxGlobalEvents_Impl::addDocumentEventListener( const uno::Reference< document::XDocumentEventListener >& Listener )
{
    {
        osl::MutexGuard g(m_aLock);
        if (m_disposed) {
            throw css::lang::DisposedException();
        }
    }
    m_aDocumentListeners.addInterface( Listener );
    // Take care of an XComponent::dispose being processed in parallel with the above addInterface:
    {
        osl::MutexGuard g(m_aLock);
        if (!m_disposed) {
            return;
        }
    }
    m_aDocumentListeners.disposeAndClear({static_cast<OWeakObject *>(this)});
}


void SAL_CALL SfxGlobalEvents_Impl::removeDocumentEventListener( const uno::Reference< document::XDocumentEventListener >& Listener )
{
    // The below removeInterface will silently do nothing when m_disposed:
    m_aDocumentListeners.removeInterface( Listener );
}


void SAL_CALL SfxGlobalEvents_Impl::notifyDocumentEvent( const OUString& /*_EventName*/,
        const uno::Reference< frame::XController2 >& /*_ViewController*/, const uno::Any& /*_Supplement*/ )
{
    // we're a multiplexer only, no chance to generate artificial events here
    throw lang::NoSupportException(OUString(), *this);
}


void SAL_CALL SfxGlobalEvents_Impl::notifyEvent(const document::EventObject& aEvent)
{
    // The below implts_* will silently do nothing when m_disposed:
    document::DocumentEvent aDocEvent(aEvent.Source, aEvent.EventName, nullptr, uno::Any());
    implts_notifyJobExecution(aEvent);
    implts_checkAndExecuteEventBindings(aDocEvent);
    implts_notifyListener(aDocEvent);
}


void SAL_CALL SfxGlobalEvents_Impl::documentEventOccured( const document::DocumentEvent& Event )
{
    // The below implts_* will silently do nothing when m_disposed:
    implts_notifyJobExecution(document::EventObject(Event.Source, Event.EventName));
    implts_checkAndExecuteEventBindings(Event);
    implts_notifyListener(Event);
}


void SAL_CALL SfxGlobalEvents_Impl::disposing(const lang::EventObject& aEvent)
{
    uno::Reference< frame::XModel > xDoc(aEvent.Source, uno::UNO_QUERY);

    // SAFE ->
    osl::MutexGuard aLock(m_aLock);
    TModelList::iterator pIt = impl_searchDoc(xDoc);
    if (pIt != m_lModels.end())
        m_lModels.erase(pIt);
    // <- SAFE
}

void SfxGlobalEvents_Impl::dispose() {
    std::multiset<css::uno::Reference<css::lang::XEventListener>> listeners;
    {
        osl::MutexGuard g(m_aLock);
        m_xEvents.clear();
        m_xJobExecutorListener.clear();
        m_disposeListeners.swap(listeners);
        m_lModels.clear();
        m_disposed = true;
    }
    m_aLegacyListeners.disposeAndClear({static_cast<OWeakObject *>(this)});
    m_aDocumentListeners.disposeAndClear({static_cast<OWeakObject *>(this)});
    for (auto const & i: listeners) {
        try {
            i->disposing({static_cast< cppu::OWeakObject * >(this)});
        } catch (css::lang::DisposedException &) {}
    }
}

void SfxGlobalEvents_Impl::addEventListener(
    css::uno::Reference<css::lang::XEventListener> const & xListener)
{
    if (!xListener.is()) {
        throw css::uno::RuntimeException("null listener");
    }
    {
        osl::MutexGuard g(m_aLock);
        if (!m_disposed) {
            m_disposeListeners.insert(xListener);
            return;
        }
    }
    try {
        xListener->disposing({static_cast< cppu::OWeakObject * >(this)});
    } catch (css::lang::DisposedException &) {}
}

void SfxGlobalEvents_Impl::removeEventListener(
    css::uno::Reference<css::lang::XEventListener> const & aListener)
{
    osl::MutexGuard g(m_aLock);
    auto const i = m_disposeListeners.find(aListener);
    if (i != m_disposeListeners.end()) {
        m_disposeListeners.erase(i);
    }
}

sal_Bool SAL_CALL SfxGlobalEvents_Impl::has(const uno::Any& aElement)
{
    uno::Reference< frame::XModel > xDoc;
    aElement >>= xDoc;

    bool bHas = false;

    // SAFE ->
    osl::MutexGuard aLock(m_aLock);
    if (m_disposed) {
        throw css::lang::DisposedException();
    }
    TModelList::iterator pIt = impl_searchDoc(xDoc);
    if (pIt != m_lModels.end())
        bHas = true;
    // <- SAFE

    return bHas;
}


void SAL_CALL SfxGlobalEvents_Impl::insert( const uno::Any& aElement )
{
    uno::Reference< frame::XModel > xDoc;
    aElement >>= xDoc;
    if (!xDoc.is())
        throw lang::IllegalArgumentException(
                "Can not locate at least the model parameter.",
                static_cast< container::XSet* >(this),
                0);

    // SAFE ->
    {
        osl::MutexGuard aLock(m_aLock);
        if (m_disposed) {
            throw css::lang::DisposedException();
        }
        TModelList::iterator pIt = impl_searchDoc(xDoc);
        if (pIt != m_lModels.end())
            throw container::ElementExistException(
                OUString(),
                static_cast<container::XSet*>(this));
        m_lModels.push_back(xDoc);
    }
    // <- SAFE

    uno::Reference< document::XDocumentEventBroadcaster > xDocBroadcaster(xDoc, uno::UNO_QUERY );
    if (xDocBroadcaster.is())
        xDocBroadcaster->addDocumentEventListener(this);
    else
    {
        // try the "legacy version" of XDocumentEventBroadcaster, which is XEventBroadcaster
        uno::Reference< document::XEventBroadcaster > xBroadcaster(xDoc, uno::UNO_QUERY);
        if (xBroadcaster.is())
            xBroadcaster->addEventListener(static_cast< document::XEventListener* >(this));
    }
}


void SAL_CALL SfxGlobalEvents_Impl::remove( const uno::Any& aElement )
{
    uno::Reference< frame::XModel > xDoc;
    aElement >>= xDoc;
    if (!xDoc.is())
        throw lang::IllegalArgumentException(
                "Can not locate at least the model parameter.",
                static_cast< container::XSet* >(this),
                0);

    // SAFE ->
    {
        osl::MutexGuard aLock(m_aLock);
        TModelList::iterator pIt = impl_searchDoc(xDoc);
        if (pIt == m_lModels.end())
            throw container::NoSuchElementException(
                OUString(),
                static_cast<container::XSet*>(this));
        m_lModels.erase(pIt);
    }
    // <- SAFE

    uno::Reference< document::XDocumentEventBroadcaster > xDocBroadcaster(xDoc, uno::UNO_QUERY );
    if (xDocBroadcaster.is())
        xDocBroadcaster->removeDocumentEventListener(this);
    else
    {
        // try the "legacy version" of XDocumentEventBroadcaster, which is XEventBroadcaster
        uno::Reference< document::XEventBroadcaster > xBroadcaster(xDoc, uno::UNO_QUERY);
        if (xBroadcaster.is())
            xBroadcaster->removeEventListener(static_cast< document::XEventListener* >(this));
    }
}


uno::Reference< container::XEnumeration > SAL_CALL SfxGlobalEvents_Impl::createEnumeration()
{
    // SAFE ->
    osl::MutexGuard aLock(m_aLock);
    if (m_disposed) {
        throw css::lang::DisposedException();
    }
    uno::Sequence<uno::Any> models(m_lModels.size());
    for (size_t i = 0; i < m_lModels.size(); ++i)
    {
        models[i] <<= m_lModels[i];
    }
    uno::Reference<container::XEnumeration> xEnum(new ::comphelper::OAnyEnumeration(models));
    // <- SAFE

    return xEnum;
}


uno::Type SAL_CALL SfxGlobalEvents_Impl::getElementType()
{
    return cppu::UnoType<frame::XModel>::get();
}


sal_Bool SAL_CALL SfxGlobalEvents_Impl::hasElements()
{
    // SAFE ->
    osl::MutexGuard aLock(m_aLock);
    if (m_disposed) {
        throw css::lang::DisposedException();
    }
    return (!m_lModels.empty());
    // <- SAFE
}


void SfxGlobalEvents_Impl::implts_notifyJobExecution(const document::EventObject& aEvent)
{
    css::uno::Reference<css::document::XEventListener> listener;
    {
        osl::MutexGuard g(m_aLock);
        listener = m_xJobExecutorListener;
    }
    if (!listener.is()) {
        return;
    }
    try
    {
        listener->notifyEvent(aEvent);
    }
    catch(const uno::RuntimeException&)
        { throw; }
    catch(const uno::Exception&)
        {}
}


void SfxGlobalEvents_Impl::implts_checkAndExecuteEventBindings(const document::DocumentEvent& aEvent)
{
    css::uno::Reference<css::container::XNameReplace> events;
    {
        osl::MutexGuard g(m_aLock);
        events = m_xEvents;
    }
    if (!events.is()) {
        return;
    }
    try
    {
        uno::Any aAny;
        if ( events->hasByName( aEvent.EventName ) )
            aAny = events->getByName(aEvent.EventName);
        SfxEvents_Impl::Execute(aAny, aEvent, nullptr);
    }
    catch ( uno::RuntimeException const & )
    {
        throw;
    }
    catch ( uno::Exception const & )
    {
       DBG_UNHANDLED_EXCEPTION("sfx.notify");
    }
}


void SfxGlobalEvents_Impl::implts_notifyListener(const document::DocumentEvent& aEvent)
{
    // containers are threadsafe
    document::EventObject aLegacyEvent(aEvent.Source, aEvent.EventName);
    m_aLegacyListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );

    m_aDocumentListeners.notifyEach( &document::XDocumentEventListener::documentEventOccured, aEvent );
}


// not threadsafe ... must be locked from outside!
TModelList::iterator SfxGlobalEvents_Impl::impl_searchDoc(const uno::Reference< frame::XModel >& xModel)
{
    if (!xModel.is())
        return m_lModels.end();

    return std::find_if(m_lModels.begin(), m_lModels.end(),
        [&xModel](const TModelList::value_type& rxModel) {
            uno::Reference< frame::XModel > xContainerDoc(rxModel, uno::UNO_QUERY);
            return xContainerDoc == xModel;
        });
}

struct Instance {
    explicit Instance(
        css::uno::Reference<css::uno::XComponentContext> const & context):
        instance(
            static_cast<cppu::OWeakObject *>(new SfxGlobalEvents_Impl(context)))
    {}

    rtl::Reference<css::uno::XInterface> instance;
};

struct Singleton:
    public rtl::StaticWithArg<
        Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
{};

}

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_sfx2_GlobalEventBroadcaster_get_implementation(<--- The function 'com_sun_star_comp_sfx2_GlobalEventBroadcaster_get_implementation' is never used.
    css::uno::XComponentContext *context,
    css::uno::Sequence<css::uno::Any> const &)
{
    return cppu::acquire(static_cast<cppu::OWeakObject *>(
                Singleton::get(context).instance.get()));
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */