Branch data Line data Source code
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 : :
21 : : #include "doceventnotifier.hxx"
22 : : #include "scriptdocument.hxx"
23 : :
24 : : #include <com/sun/star/document/XEventBroadcaster.hpp>
25 : :
26 : : #include <vcl/svapp.hxx>
27 : :
28 : : #include <tools/diagnose_ex.h>
29 : :
30 : : #include <comphelper/componentcontext.hxx>
31 : : #include <comphelper/processfactory.hxx>
32 : :
33 : : #include <osl/mutex.hxx>
34 : : #include <sal/macros.h>
35 : :
36 : : #include <cppuhelper/compbase1.hxx>
37 : : #include <cppuhelper/basemutex.hxx>
38 : :
39 : : //........................................................................
40 : : namespace basctl
41 : : {
42 : : //........................................................................
43 : :
44 : : /** === begin UNO using === **/
45 : : using ::com::sun::star::document::XEventBroadcaster;
46 : : using ::com::sun::star::document::XEventListener;
47 : : using ::com::sun::star::document::EventObject;
48 : : using ::com::sun::star::uno::RuntimeException;
49 : : using ::com::sun::star::uno::Reference;
50 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
51 : : using ::com::sun::star::uno::Exception;
52 : : using ::com::sun::star::frame::XModel;
53 : : using ::com::sun::star::uno::UNO_QUERY;
54 : : /** === end UNO using === **/
55 : : namespace csslang = ::com::sun::star::lang;
56 : :
57 : : //====================================================================
58 : : //= DocumentEventNotifier_Impl
59 : : //====================================================================
60 : : typedef ::cppu::WeakComponentImplHelper1 < XEventListener
61 : : > DocumentEventNotifier_Impl_Base;
62 : :
63 : : enum ListenerAction
64 : : {
65 : : RegisterListener,
66 : : RemoveListener
67 : : };
68 : :
69 : : /** impl class for DocumentEventNotifier
70 : : */
71 : : class DocumentEventNotifier_Impl :public ::boost::noncopyable
72 : : ,public ::cppu::BaseMutex
73 : : ,public DocumentEventNotifier_Impl_Base
74 : : {
75 : : public:
76 : : DocumentEventNotifier_Impl( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument );
77 : :
78 : : // document::XEventListener
79 : : virtual void SAL_CALL notifyEvent( const EventObject& Event ) throw (RuntimeException);
80 : :
81 : : // lang::XEventListener
82 : : virtual void SAL_CALL disposing( const csslang::EventObject& Event ) throw (RuntimeException);
83 : :
84 : : // ComponentHelper
85 : : virtual void SAL_CALL disposing();
86 : :
87 : : protected:
88 : : ~DocumentEventNotifier_Impl();
89 : :
90 : : private:
91 : : /// determines whether the instance is already disposed
92 : 0 : bool impl_isDisposed_nothrow() const { return m_pListener == NULL; }
93 : :
94 : : /// disposes the instance
95 : : void impl_dispose_nothrow();
96 : :
97 : : /// registers or revokes the instance as listener at the global event broadcaster
98 : : void impl_listenerAction_nothrow( ListenerAction _eAction );
99 : :
100 : : private:
101 : : DocumentEventListener* m_pListener;
102 : : Reference< XModel > m_xModel;
103 : : };
104 : :
105 : : //--------------------------------------------------------------------
106 : 0 : DocumentEventNotifier_Impl::DocumentEventNotifier_Impl( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument )
107 : : :DocumentEventNotifier_Impl_Base( m_aMutex )
108 : : ,m_pListener( &_rListener )
109 : 0 : ,m_xModel( _rxDocument )
110 : : {
111 : 0 : osl_incrementInterlockedCount( &m_refCount );
112 : 0 : impl_listenerAction_nothrow( RegisterListener );
113 : 0 : osl_decrementInterlockedCount( &m_refCount );
114 : 0 : }
115 : :
116 : : //--------------------------------------------------------------------
117 : 0 : DocumentEventNotifier_Impl::~DocumentEventNotifier_Impl()
118 : : {
119 : 0 : if ( !impl_isDisposed_nothrow() )
120 : : {
121 : 0 : acquire();
122 : 0 : dispose();
123 : : }
124 : 0 : }
125 : :
126 : : //--------------------------------------------------------------------
127 : 0 : void SAL_CALL DocumentEventNotifier_Impl::notifyEvent( const EventObject& _rEvent ) throw (RuntimeException)
128 : : {
129 : 0 : ::osl::ClearableMutexGuard aGuard( m_aMutex );
130 : :
131 : : OSL_PRECOND( !impl_isDisposed_nothrow(), "DocumentEventNotifier_Impl::notifyEvent: disposed, but still getting events?" );
132 : 0 : if ( impl_isDisposed_nothrow() )
133 : : return;
134 : :
135 : 0 : Reference< XModel > xDocument( _rEvent.Source, UNO_QUERY );
136 : : OSL_ENSURE( xDocument.is(), "DocumentEventNotifier_Impl::notifyEvent: illegal source document!" );
137 : 0 : if ( !xDocument.is() )
138 : : return;
139 : :
140 : : struct EventEntry
141 : : {
142 : : const sal_Char* pEventName;
143 : : void (DocumentEventListener::*listenerMethod)( const ScriptDocument& _rDocument );
144 : : };
145 : : EventEntry aEvents[] = {
146 : : { "OnNew", &DocumentEventListener::onDocumentCreated },
147 : : { "OnLoad", &DocumentEventListener::onDocumentOpened },
148 : : { "OnSave", &DocumentEventListener::onDocumentSave },
149 : : { "OnSaveDone", &DocumentEventListener::onDocumentSaveDone },
150 : : { "OnSaveAs", &DocumentEventListener::onDocumentSaveAs },
151 : : { "OnSaveAsDone", &DocumentEventListener::onDocumentSaveAsDone },
152 : : { "OnUnload", &DocumentEventListener::onDocumentClosed },
153 : : { "OnTitleChanged", &DocumentEventListener::onDocumentTitleChanged },
154 : : { "OnModeChanged", &DocumentEventListener::onDocumentModeChanged }
155 : 0 : };
156 : :
157 : 0 : for ( size_t i=0; i < SAL_N_ELEMENTS( aEvents ); ++i )
158 : : {
159 : 0 : if ( !_rEvent.EventName.equalsAscii( aEvents[i].pEventName ) )
160 : 0 : continue;
161 : :
162 : 0 : ScriptDocument aDocument( xDocument );
163 : : {
164 : : // the listener implementations usually require the SolarMutex, so lock it here.
165 : : // But ensure the proper order of locking the solar and the own mutex
166 : 0 : aGuard.clear();
167 : 0 : SolarMutexGuard aSolarGuard;
168 : 0 : ::osl::MutexGuard aGuard2( m_aMutex );
169 : :
170 : 0 : if ( impl_isDisposed_nothrow() )
171 : : // somebody took the chance to dispose us -> bail out
172 : : return;
173 : :
174 : 0 : (m_pListener->*aEvents[i].listenerMethod)( aDocument );
175 : : }
176 : 0 : break;
177 : 0 : }
178 : : }
179 : :
180 : : //--------------------------------------------------------------------
181 : 0 : void SAL_CALL DocumentEventNotifier_Impl::disposing( const csslang::EventObject& /*Event*/ ) throw (RuntimeException)
182 : : {
183 : 0 : SolarMutexGuard aSolarGuard;
184 : 0 : ::osl::MutexGuard aGuard( m_aMutex );
185 : :
186 : 0 : if ( !impl_isDisposed_nothrow() )
187 : 0 : impl_dispose_nothrow();
188 : 0 : }
189 : :
190 : : //--------------------------------------------------------------------
191 : 0 : void SAL_CALL DocumentEventNotifier_Impl::disposing()
192 : : {
193 : 0 : impl_listenerAction_nothrow( RemoveListener );
194 : 0 : impl_dispose_nothrow();
195 : 0 : }
196 : :
197 : : //--------------------------------------------------------------------
198 : 0 : void DocumentEventNotifier_Impl::impl_dispose_nothrow()
199 : : {
200 : 0 : m_pListener = NULL;
201 : 0 : m_xModel.clear();
202 : 0 : }
203 : :
204 : : //--------------------------------------------------------------------
205 : 0 : void DocumentEventNotifier_Impl::impl_listenerAction_nothrow( ListenerAction _eAction )
206 : : {
207 : : try
208 : : {
209 : 0 : Reference< XEventBroadcaster > xBroadcaster;
210 : 0 : if ( m_xModel.is() )
211 : 0 : xBroadcaster.set( m_xModel, UNO_QUERY_THROW );
212 : : else
213 : : {
214 : 0 : ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
215 : : xBroadcaster.set(
216 : : aContext.createComponent( "com.sun.star.frame.GlobalEventBroadcaster" ),
217 : 0 : UNO_QUERY_THROW );
218 : : }
219 : :
220 : : void ( SAL_CALL XEventBroadcaster::*listenerAction )( const Reference< XEventListener >& ) =
221 : 0 : ( _eAction == RegisterListener ) ? &XEventBroadcaster::addEventListener : &XEventBroadcaster::removeEventListener;
222 : 0 : (xBroadcaster.get()->*listenerAction)( this );
223 : : }
224 : 0 : catch( const Exception& )
225 : : {
226 : : DBG_UNHANDLED_EXCEPTION();
227 : : }
228 : 0 : }
229 : :
230 : : //====================================================================
231 : : //= DocumentEventNotifier
232 : : //====================================================================
233 : : //--------------------------------------------------------------------
234 : 0 : DocumentEventNotifier::DocumentEventNotifier( DocumentEventListener& _rListener, const Reference< XModel >& _rxDocument )
235 : 0 : :m_pImpl( new DocumentEventNotifier_Impl( _rListener, _rxDocument ) )
236 : : {
237 : 0 : }
238 : :
239 : : //--------------------------------------------------------------------
240 : 0 : DocumentEventNotifier::DocumentEventNotifier( DocumentEventListener& _rListener )
241 : 0 : :m_pImpl( new DocumentEventNotifier_Impl( _rListener, Reference< XModel >() ) )
242 : : {
243 : 0 : }
244 : :
245 : : //--------------------------------------------------------------------
246 : 0 : DocumentEventNotifier::~DocumentEventNotifier()
247 : : {
248 : 0 : }
249 : :
250 : : //--------------------------------------------------------------------
251 : 0 : void DocumentEventNotifier::dispose()
252 : : {
253 : 0 : m_pImpl->dispose();
254 : 0 : }
255 : :
256 : : //====================================================================
257 : : //= DocumentEventListener
258 : : //====================================================================
259 : 0 : DocumentEventListener::~DocumentEventListener()
260 : : {
261 : 0 : }
262 : :
263 : : //........................................................................
264 : : } // namespace basctl
265 : : //........................................................................
266 : :
267 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|