tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / basctl / source / basicide / doceventnotifier.cxx
blob2365aef2ee163acd8e559d5132e319f27c6b475c
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 <basctl/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 disposeOnDestruct();
111 void SAL_CALL DocumentEventNotifier::Impl::documentEventOccured( const DocumentEvent& _rEvent )
113 std::unique_lock aGuard( m_aMutex );
115 OSL_PRECOND( !impl_isDisposed_nothrow(aGuard), "DocumentEventNotifier::Impl::notifyEvent: disposed, but still getting events?" );
116 if ( impl_isDisposed_nothrow(aGuard) )
117 return;
119 Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
120 OSL_ENSURE( xDocument.is(), "DocumentEventNotifier::Impl::notifyEvent: illegal source document!" );
121 if ( !xDocument.is() )
122 return;
124 struct EventEntry
126 const char* pEventName;
127 void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
129 static EventEntry const aEvents[] = {
130 { "OnNew", &DocumentEventListener::onDocumentCreated },
131 { "OnLoad", &DocumentEventListener::onDocumentOpened },
132 { "OnSave", &DocumentEventListener::onDocumentSave },
133 { "OnSaveDone", &DocumentEventListener::onDocumentSaveDone },
134 { "OnSaveAs", &DocumentEventListener::onDocumentSaveAs },
135 { "OnSaveAsDone", &DocumentEventListener::onDocumentSaveAsDone },
136 { "OnUnload", &DocumentEventListener::onDocumentClosed },
137 { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
138 { "OnModeChanged", &DocumentEventListener::onDocumentModeChanged }
141 for (EventEntry const & aEvent : aEvents)
143 if ( !_rEvent.EventName.equalsAscii( aEvent.pEventName ) )
144 continue;
146 // Listener implementations require that we hold the mutex, but to avoid lock ordering issues,
147 // we need to take the solar mutex before we take our own mutex.
148 aGuard.unlock();
150 // Listener implements require that we hold the solar mutex.
151 SolarMutexGuard aSolarGuard;
153 // Take the lock again, so we can check our local fields.
154 aGuard.lock();
155 if ( impl_isDisposed_nothrow(aGuard) )
156 // somebody took the chance to dispose us -> bail out
157 return;
158 DocumentEventListener* pListener = m_pListener;
159 ScriptDocument aDocument( xDocument );
160 // We cannot call the listener while holding our mutex because the listener
161 // call might trigger an event which call back into us.
162 aGuard.unlock();
164 (pListener->*aEvent.listenerMethod)( aDocument );
166 break;
170 void SAL_CALL DocumentEventNotifier::Impl::disposing( const css::lang::EventObject& /*Event*/ )
172 SolarMutexGuard aSolarGuard;
173 std::unique_lock aGuard( m_aMutex );
175 if ( !impl_isDisposed_nothrow(aGuard) )
176 impl_dispose_nothrow(aGuard);
179 void DocumentEventNotifier::Impl::disposing(std::unique_lock<std::mutex>& rGuard)
181 impl_listenerAction_nothrow( rGuard, RemoveListener );
182 impl_dispose_nothrow(rGuard);
185 void DocumentEventNotifier::Impl::impl_dispose_nothrow(std::unique_lock<std::mutex>& /*rGuard*/)
187 m_pListener = nullptr;
188 m_xModel.clear();
191 void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( std::unique_lock<std::mutex>& rGuard, ListenerAction _eAction )
195 Reference< XDocumentEventBroadcaster > xBroadcaster;
196 if ( m_xModel.is() )
197 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
198 else
200 const Reference< css::uno::XComponentContext >& aContext(
201 comphelper::getProcessComponentContext() );
202 xBroadcaster = theGlobalEventBroadcaster::get(aContext);
205 void ( SAL_CALL XDocumentEventBroadcaster::*listenerAction )( const Reference< XDocumentEventListener >& ) =
206 ( _eAction == RegisterListener ) ? &XDocumentEventBroadcaster::addDocumentEventListener : &XDocumentEventBroadcaster::removeDocumentEventListener;
208 rGuard.unlock();
209 (xBroadcaster.get()->*listenerAction)( this );
210 rGuard.lock();
212 catch( const Exception& )
214 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
218 // DocumentEventNotifier
220 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
221 m_pImpl(new Impl(rListener, rxDocument))
224 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener) :
225 m_pImpl(new Impl(rListener, Reference<XModel>()))
228 DocumentEventNotifier::~DocumentEventNotifier()
232 void DocumentEventNotifier::dispose()
234 m_pImpl->dispose();
237 // DocumentEventListener
239 DocumentEventListener::~DocumentEventListener()
243 } // namespace basctl
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */