Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basctl / source / basicide / doceventnotifier.cxx
blobb309fbd00a1b8737ba05caf01fae080840dbf414
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 <tools/diagnose_ex.h>
29 #include <comphelper/processfactory.hxx>
31 #include <cppuhelper/compbase.hxx>
32 #include <cppuhelper/basemutex.hxx>
34 namespace basctl
37 using ::com::sun::star::document::XDocumentEventBroadcaster;
38 using ::com::sun::star::document::XDocumentEventListener;
39 using ::com::sun::star::document::DocumentEvent;
40 using ::com::sun::star::uno::XComponentContext;
41 using ::com::sun::star::uno::RuntimeException;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::UNO_QUERY_THROW;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::frame::XModel;
46 using ::com::sun::star::frame::theGlobalEventBroadcaster;
47 using ::com::sun::star::uno::UNO_QUERY;
49 namespace csslang = ::com::sun::star::lang;
51 // DocumentEventNotifier::Impl
53 typedef ::cppu::WeakComponentImplHelper< XDocumentEventListener
54 > DocumentEventNotifier_Impl_Base;
56 enum ListenerAction
58 RegisterListener,
59 RemoveListener
62 /** impl class for DocumentEventNotifier
64 class DocumentEventNotifier::Impl :public ::cppu::BaseMutex
65 ,public DocumentEventNotifier_Impl_Base
67 public:
68 // noncopyable
69 Impl(const Impl&) = delete;
70 Impl& operator=(const Impl&) = delete;
72 Impl (DocumentEventListener&, Reference<XModel> const& rxDocument);
73 virtual ~Impl () override;
75 // XDocumentEventListener
76 virtual void SAL_CALL documentEventOccured( const DocumentEvent& Event ) override;
78 // XEventListener
79 virtual void SAL_CALL disposing( const csslang::EventObject& Event ) override;
81 // ComponentHelper
82 virtual void SAL_CALL disposing() override;
84 private:
85 /// determines whether the instance is already disposed
86 bool impl_isDisposed_nothrow() const { return m_pListener == nullptr; }
88 /// disposes the instance
89 void impl_dispose_nothrow();
91 /// registers or revokes the instance as listener at the global event broadcaster
92 void impl_listenerAction_nothrow( ListenerAction _eAction );
94 private:
95 DocumentEventListener* m_pListener;
96 Reference< XModel > m_xModel;
99 DocumentEventNotifier::Impl::Impl (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
100 DocumentEventNotifier_Impl_Base(m_aMutex),
101 m_pListener(&rListener),
102 m_xModel(rxDocument)
104 osl_atomic_increment( &m_refCount );
105 impl_listenerAction_nothrow( RegisterListener );
106 osl_atomic_decrement( &m_refCount );
109 DocumentEventNotifier::Impl::~Impl ()
111 if ( !impl_isDisposed_nothrow() )
113 acquire();
114 dispose();
118 void SAL_CALL DocumentEventNotifier::Impl::documentEventOccured( const DocumentEvent& _rEvent )
120 ::osl::ClearableMutexGuard aGuard( m_aMutex );
122 OSL_PRECOND( !impl_isDisposed_nothrow(), "DocumentEventNotifier::Impl::notifyEvent: disposed, but still getting events?" );
123 if ( impl_isDisposed_nothrow() )
124 return;
126 Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
127 OSL_ENSURE( xDocument.is(), "DocumentEventNotifier::Impl::notifyEvent: illegal source document!" );
128 if ( !xDocument.is() )
129 return;
131 struct EventEntry
133 const sal_Char* pEventName;
134 void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
136 EventEntry const aEvents[] = {
137 { "OnNew", &DocumentEventListener::onDocumentCreated },
138 { "OnLoad", &DocumentEventListener::onDocumentOpened },
139 { "OnSave", &DocumentEventListener::onDocumentSave },
140 { "OnSaveDone", &DocumentEventListener::onDocumentSaveDone },
141 { "OnSaveAs", &DocumentEventListener::onDocumentSaveAs },
142 { "OnSaveAsDone", &DocumentEventListener::onDocumentSaveAsDone },
143 { "OnUnload", &DocumentEventListener::onDocumentClosed },
144 { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
145 { "OnModeChanged", &DocumentEventListener::onDocumentModeChanged }
148 for (EventEntry const & aEvent : aEvents)
150 if ( !_rEvent.EventName.equalsAscii( aEvent.pEventName ) )
151 continue;
153 ScriptDocument aDocument( xDocument );
155 // the listener implementations usually require the SolarMutex, so lock it here.
156 // But ensure the proper order of locking the solar and the own mutex
157 aGuard.clear();
158 SolarMutexGuard aSolarGuard;
159 ::osl::MutexGuard aGuard2( m_aMutex );
161 if ( impl_isDisposed_nothrow() )
162 // somebody took the chance to dispose us -> bail out
163 return;
165 (m_pListener->*aEvent.listenerMethod)( aDocument );
167 break;
171 void SAL_CALL DocumentEventNotifier::Impl::disposing( const csslang::EventObject& /*Event*/ )
173 SolarMutexGuard aSolarGuard;
174 ::osl::MutexGuard aGuard( m_aMutex );
176 if ( !impl_isDisposed_nothrow() )
177 impl_dispose_nothrow();
180 void SAL_CALL DocumentEventNotifier::Impl::disposing()
182 impl_listenerAction_nothrow( RemoveListener );
183 impl_dispose_nothrow();
186 void DocumentEventNotifier::Impl::impl_dispose_nothrow()
188 m_pListener = nullptr;
189 m_xModel.clear();
192 void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( ListenerAction _eAction )
196 Reference< XDocumentEventBroadcaster > xBroadcaster;
197 if ( m_xModel.is() )
198 xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
199 else
201 Reference< css::uno::XComponentContext > aContext(
202 comphelper::getProcessComponentContext() );
203 xBroadcaster = theGlobalEventBroadcaster::get(aContext);
206 void ( SAL_CALL XDocumentEventBroadcaster::*listenerAction )( const Reference< XDocumentEventListener >& ) =
207 ( _eAction == RegisterListener ) ? &XDocumentEventBroadcaster::addDocumentEventListener : &XDocumentEventBroadcaster::removeDocumentEventListener;
208 (xBroadcaster.get()->*listenerAction)( this );
210 catch( const Exception& )
212 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
216 // DocumentEventNotifier
218 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener, Reference<XModel> const& rxDocument) :
219 m_pImpl(new Impl(rListener, rxDocument))
222 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener& rListener) :
223 m_pImpl(new Impl(rListener, Reference<XModel>()))
226 DocumentEventNotifier::~DocumentEventNotifier()
230 void DocumentEventNotifier::dispose()
232 m_pImpl->dispose();
235 // DocumentEventListener
237 DocumentEventListener::~DocumentEventListener()
241 } // namespace basctl
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */