Bump version to 24.04.3.4
[LibreOffice.git] / basctl / source / basicide / doceventnotifier.cxx
blob254c719bb5cc3c655ce1af5c9dc76c3757f06cfc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <doceventnotifier.hxx>
21 #include <scriptdocument.hxx>
23 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
25 #include <vcl/svapp.hxx>
27 #include <comphelper/compbase.hxx>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <comphelper/processfactory.hxx>
31 namespace basctl
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;
45 // DocumentEventNotifier::Impl
47 typedef ::comphelper::WeakComponentImplHelper< XDocumentEventListener
48 > DocumentEventNotifier_Impl_Base;
50 namespace {
52 enum ListenerAction
54 RegisterListener,
55 RemoveListener
60 /** impl class for DocumentEventNotifier
62 class DocumentEventNotifier::Impl : public DocumentEventNotifier_Impl_Base
64 public:
65 // noncopyable
66 Impl(const Impl&) = delete;
67 Impl& operator=(const Impl&) = delete;
69 Impl (DocumentEventListener&, Reference<XModel> const& rxDocument);
70 virtual ~Impl () override;
72 // XDocumentEventListener
73 virtual void SAL_CALL documentEventOccured( const DocumentEvent& Event ) override;
75 // XEventListener
76 virtual void SAL_CALL disposing( const css::lang::EventObject& Event ) override;
78 // WeakComponentImplHelper
79 virtual void disposing(std::unique_lock<std::mutex>&) override;
81 private:
82 /// determines whether the instance is already disposed
83 bool impl_isDisposed_nothrow(std::unique_lock<std::mutex>& /*rGuard*/) const { return m_pListener == nullptr; }
85 /// disposes the instance
86 void impl_dispose_nothrow(std::unique_lock<std::mutex>& rGuard);
88 /// registers or revokes the instance as listener at the global event broadcaster
89 void impl_listenerAction_nothrow( std::unique_lock<std::mutex>& rGuard, ListenerAction _eAction );
91 private:
92 DocumentEventListener* m_pListener;
93 Reference< XModel > m_xModel;
96 DocumentEventNotifier::Impl::Impl (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
97 m_pListener(&rListener),
98 m_xModel(rxDocument)
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 );
106 DocumentEventNotifier::Impl::~Impl ()
108 std::unique_lock aGuard(m_aMutex);
109 if ( !impl_isDisposed_nothrow(aGuard) )
111 acquire();
112 dispose();
116 void SAL_CALL DocumentEventNotifier::Impl::documentEventOccured( const DocumentEvent& _rEvent )
118 std::unique_lock aGuard( m_aMutex );
120 OSL_PRECOND( !impl_isDisposed_nothrow(aGuard), "DocumentEventNotifier::Impl::notifyEvent: disposed, but still getting events?" );
121 if ( impl_isDisposed_nothrow(aGuard) )
122 return;
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;
129 struct EventEntry
131 const char* pEventName;
132 void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
134 static EventEntry const aEvents[] = {
135 { "OnNew", &DocumentEventListener::onDocumentCreated },
136 { "OnLoad", &DocumentEventListener::onDocumentOpened },
137 { "OnSave", &DocumentEventListener::onDocumentSave },
138 { "OnSaveDone", &DocumentEventListener::onDocumentSaveDone },
139 { "OnSaveAs", &DocumentEventListener::onDocumentSaveAs },
140 { "OnSaveAsDone", &DocumentEventListener::onDocumentSaveAsDone },
141 { "OnUnload", &DocumentEventListener::onDocumentClosed },
142 { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
143 { "OnModeChanged", &DocumentEventListener::onDocumentModeChanged }
146 for (EventEntry const & aEvent : aEvents)
148 if ( !_rEvent.EventName.equalsAscii( aEvent.pEventName ) )
149 continue;
151 // Listener implementations require that we hold the mutex, but to avoid lock ordering issues,
152 // we need to take the solar mutex before we take our own mutex.
153 aGuard.unlock();
155 // Listener implements require that we hold the solar mutex.
156 SolarMutexGuard aSolarGuard;
158 // Take the lock again, so we can check our local fields.
159 aGuard.lock();
160 if ( impl_isDisposed_nothrow(aGuard) )
161 // somebody took the chance to dispose us -> bail out
162 return;
163 DocumentEventListener* pListener = m_pListener;
164 ScriptDocument aDocument( xDocument );
165 // We cannot call the listener while holding our mutex because the listener
166 // call might trigger an event which call back into us.
167 aGuard.unlock();
169 (pListener->*aEvent.listenerMethod)( aDocument );
171 break;
175 void SAL_CALL DocumentEventNotifier::Impl::disposing( const css::lang::EventObject& /*Event*/ )
177 SolarMutexGuard aSolarGuard;
178 std::unique_lock aGuard( m_aMutex );
180 if ( !impl_isDisposed_nothrow(aGuard) )
181 impl_dispose_nothrow(aGuard);
184 void DocumentEventNotifier::Impl::disposing(std::unique_lock<std::mutex>& rGuard)
186 impl_listenerAction_nothrow( rGuard, RemoveListener );
187 impl_dispose_nothrow(rGuard);
190 void DocumentEventNotifier::Impl::impl_dispose_nothrow(std::unique_lock<std::mutex>& /*rGuard*/)
192 m_pListener = nullptr;
193 m_xModel.clear();
196 void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( std::unique_lock<std::mutex>& rGuard, ListenerAction _eAction )
200 Reference< XDocumentEventBroadcaster > xBroadcaster;
201 if ( m_xModel.is() )
202 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
203 else
205 Reference< css::uno::XComponentContext > aContext(
206 comphelper::getProcessComponentContext() );
207 xBroadcaster = theGlobalEventBroadcaster::get(aContext);
210 void ( SAL_CALL XDocumentEventBroadcaster::*listenerAction )( const Reference< XDocumentEventListener >& ) =
211 ( _eAction == RegisterListener ) ? &XDocumentEventBroadcaster::addDocumentEventListener : &XDocumentEventBroadcaster::removeDocumentEventListener;
213 rGuard.unlock();
214 (xBroadcaster.get()->*listenerAction)( this );
215 rGuard.lock();
217 catch( const Exception& )
219 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
223 // DocumentEventNotifier
225 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
226 m_pImpl(new Impl(rListener, rxDocument))
229 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener) :
230 m_pImpl(new Impl(rListener, Reference<XModel>()))
233 DocumentEventNotifier::~DocumentEventNotifier()
237 void DocumentEventNotifier::dispose()
239 m_pImpl->dispose();
242 // DocumentEventListener
244 DocumentEventListener::~DocumentEventListener()
248 } // namespace basctl
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */