1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
60 /** impl class for DocumentEventNotifier
62 class DocumentEventNotifier::Impl
: public DocumentEventNotifier_Impl_Base
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
;
76 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Event
) override
;
78 // WeakComponentImplHelper
79 virtual void disposing(std::unique_lock
<std::mutex
>&) override
;
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
);
92 DocumentEventListener
* m_pListener
;
93 Reference
< XModel
> m_xModel
;
96 DocumentEventNotifier::Impl::Impl (DocumentEventListener
& rListener
, Reference
<XModel
> const& rxDocument
) :
97 m_pListener(&rListener
),
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
) )
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
) )
124 Reference
< XModel
> xDocument( _rEvent
.Source
, UNO_QUERY
);
125 OSL_ENSURE( xDocument
.is(), "DocumentEventNotifier::Impl::notifyEvent: illegal source document!" );
126 if ( !xDocument
.is() )
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
) )
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.
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.
160 if ( impl_isDisposed_nothrow(aGuard
) )
161 // somebody took the chance to dispose us -> bail out
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.
169 (pListener
->*aEvent
.listenerMethod
)( aDocument
);
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;
196 void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( std::unique_lock
<std::mutex
>& rGuard
, ListenerAction _eAction
)
200 Reference
< XDocumentEventBroadcaster
> xBroadcaster
;
202 xBroadcaster
.set( m_xModel
, UNO_QUERY_THROW
);
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
;
214 (xBroadcaster
.get()->*listenerAction
)( this );
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()
242 // DocumentEventListener
244 DocumentEventListener::~DocumentEventListener()
248 } // namespace basctl
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */