LibreOffice Module basctl (master) 1
doceventnotifier.cxx
Go to the documentation of this file.
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 <doceventnotifier.hxx>
21#include <scriptdocument.hxx>
22
23#include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
24
25#include <vcl/svapp.hxx>
26
30
31namespace basctl
32{
33
34 using ::com::sun::star::document::XDocumentEventBroadcaster;
35 using ::com::sun::star::document::XDocumentEventListener;
36 using ::com::sun::star::document::DocumentEvent;
37 using ::com::sun::star::uno::XComponentContext;
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::UNO_QUERY_THROW;
40 using ::com::sun::star::uno::Exception;
41 using ::com::sun::star::frame::XModel;
42 using ::com::sun::star::frame::theGlobalEventBroadcaster;
43 using ::com::sun::star::uno::UNO_QUERY;
44
45 // DocumentEventNotifier::Impl
46
47 typedef ::comphelper::WeakComponentImplHelper< XDocumentEventListener
49
50 namespace {
51
52 enum ListenerAction
53 {
54 RegisterListener,
55 RemoveListener
56 };
57
58 }
59
63 {
64 public:
65 // noncopyable
66 Impl(const Impl&) = delete;
67 Impl& operator=(const Impl&) = delete;
68
69 Impl (DocumentEventListener&, Reference<XModel> const& rxDocument);
70 virtual ~Impl () override;
71
72 // XDocumentEventListener
73 virtual void SAL_CALL documentEventOccured( const DocumentEvent& Event ) override;
74
75 // XEventListener
76 virtual void SAL_CALL disposing( const css::lang::EventObject& Event ) override;
77
78 // WeakComponentImplHelper
79 virtual void disposing(std::unique_lock<std::mutex>&) override;
80
81 private:
83 bool impl_isDisposed_nothrow(std::unique_lock<std::mutex>& /*rGuard*/) const { return m_pListener == nullptr; }
84
86 void impl_dispose_nothrow(std::unique_lock<std::mutex>& rGuard);
87
89 void impl_listenerAction_nothrow( std::unique_lock<std::mutex>& rGuard, ListenerAction _eAction );
90
91 private:
94 };
95
97 m_pListener(&rListener),
98 m_xModel(rxDocument)
99 {
100 std::unique_lock aGuard(m_aMutex);
101 osl_atomic_increment( &m_refCount );
102 impl_listenerAction_nothrow( aGuard, RegisterListener );
103 osl_atomic_decrement( &m_refCount );
104 }
105
107 {
108 std::unique_lock aGuard(m_aMutex);
109 if ( !impl_isDisposed_nothrow(aGuard) )
110 {
111 acquire();
112 dispose();
113 }
114 }
115
116 void SAL_CALL DocumentEventNotifier::Impl::documentEventOccured( const DocumentEvent& _rEvent )
117 {
118 std::unique_lock aGuard( m_aMutex );
119
120 OSL_PRECOND( !impl_isDisposed_nothrow(aGuard), "DocumentEventNotifier::Impl::notifyEvent: disposed, but still getting events?" );
121 if ( impl_isDisposed_nothrow(aGuard) )
122 return;
123
124 Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
125 OSL_ENSURE( xDocument.is(), "DocumentEventNotifier::Impl::notifyEvent: illegal source document!" );
126 if ( !xDocument.is() )
127 return;
128
129 struct EventEntry
130 {
131 const char* pEventName;
132 void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
133 };
134 static EventEntry const aEvents[] = {
144 };
145
146 for (EventEntry const & aEvent : aEvents)
147 {
148 if ( !_rEvent.EventName.equalsAscii( aEvent.pEventName ) )
149 continue;
150
151 ScriptDocument aDocument( xDocument );
152 {
153 // the listener implementations usually require the SolarMutex, so lock it here.
154 // But ensure the proper order of locking the solar and the own mutex
155 aGuard.unlock();
156 SolarMutexGuard aSolarGuard;
157 std::unique_lock aGuard2( m_aMutex );
158
159 if ( impl_isDisposed_nothrow(aGuard2) )
160 // somebody took the chance to dispose us -> bail out
161 return;
162
163 (m_pListener->*aEvent.listenerMethod)( aDocument );
164 }
165 break;
166 }
167 }
168
169 void SAL_CALL DocumentEventNotifier::Impl::disposing( const css::lang::EventObject& /*Event*/ )
170 {
171 SolarMutexGuard aSolarGuard;
172 std::unique_lock aGuard( m_aMutex );
173
174 if ( !impl_isDisposed_nothrow(aGuard) )
175 impl_dispose_nothrow(aGuard);
176 }
177
178 void DocumentEventNotifier::Impl::disposing(std::unique_lock<std::mutex>& rGuard)
179 {
180 impl_listenerAction_nothrow( rGuard, RemoveListener );
181 impl_dispose_nothrow(rGuard);
182 }
183
184 void DocumentEventNotifier::Impl::impl_dispose_nothrow(std::unique_lock<std::mutex>& /*rGuard*/)
185 {
186 m_pListener = nullptr;
187 m_xModel.clear();
188 }
189
190 void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( std::unique_lock<std::mutex>& rGuard, ListenerAction _eAction )
191 {
192 try
193 {
195 if ( m_xModel.is() )
196 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
197 else
198 {
201 xBroadcaster = theGlobalEventBroadcaster::get(aContext);
202 }
203
204 void ( SAL_CALL XDocumentEventBroadcaster::*listenerAction )( const Reference< XDocumentEventListener >& ) =
205 ( _eAction == RegisterListener ) ? &XDocumentEventBroadcaster::addDocumentEventListener : &XDocumentEventBroadcaster::removeDocumentEventListener;
206
207 rGuard.unlock();
208 (xBroadcaster.get()->*listenerAction)( this );
209 rGuard.lock();
210 }
211 catch( const Exception& )
212 {
213 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
214 }
215 }
216
217 // DocumentEventNotifier
218
220 m_pImpl(new Impl(rListener, rxDocument))
221 { }
222
224 m_pImpl(new Impl(rListener, Reference<XModel>()))
225 { }
226
228 {
229 }
230
232 {
233 m_pImpl->dispose();
234 }
235
236 // DocumentEventListener
237
239 {
240 }
241
242} // namespace basctl
243
244/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
ScriptDocument aDocument
Definition: basobj2.cxx:194
virtual void onDocumentSaveAs(const ScriptDocument &_rDocument)=0
virtual void onDocumentSave(const ScriptDocument &_rDocument)=0
virtual void onDocumentTitleChanged(const ScriptDocument &_rDocument)=0
virtual void onDocumentSaveDone(const ScriptDocument &_rDocument)=0
virtual void onDocumentModeChanged(const ScriptDocument &_rDocument)=0
virtual void onDocumentCreated(const ScriptDocument &_rDocument)=0
virtual void onDocumentClosed(const ScriptDocument &_rDocument)=0
virtual void onDocumentSaveAsDone(const ScriptDocument &_rDocument)=0
virtual void onDocumentOpened(const ScriptDocument &_rDocument)=0
impl class for DocumentEventNotifier
void impl_dispose_nothrow(std::unique_lock< std::mutex > &rGuard)
disposes the instance
void impl_listenerAction_nothrow(std::unique_lock< std::mutex > &rGuard, ListenerAction _eAction)
registers or revokes the instance as listener at the global event broadcaster
Impl & operator=(const Impl &)=delete
virtual void SAL_CALL disposing(const css::lang::EventObject &Event) override
virtual void SAL_CALL documentEventOccured(const DocumentEvent &Event) override
bool impl_isDisposed_nothrow(std::unique_lock< std::mutex > &) const
determines whether the instance is already disposed
rtl::Reference< Impl > m_pImpl
DocumentEventNotifier(DocumentEventListener &)
create a notifier instance which notifies about events of all documents in the whole application
encapsulates a document which contains Basic scripts and dialogs
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< frame::XModel > m_xModel
ULONG m_refCount
std::mutex m_aMutex
::comphelper::WeakComponentImplHelper< XDocumentEventListener > DocumentEventNotifier_Impl_Base
@ Exception
Reference< XComponentContext > getProcessComponentContext()