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 <tools/diagnose_ex.h>
29 #include <comphelper/processfactory.hxx>
32 #include <cppuhelper/compbase1.hxx>
33 #include <cppuhelper/basemutex.hxx>
38 using ::com::sun::star::document::XDocumentEventBroadcaster
;
39 using ::com::sun::star::document::XDocumentEventListener
;
40 using ::com::sun::star::document::DocumentEvent
;
41 using ::com::sun::star::uno::XComponentContext
;
42 using ::com::sun::star::uno::RuntimeException
;
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::UNO_QUERY_THROW
;
45 using ::com::sun::star::uno::Exception
;
46 using ::com::sun::star::frame::XModel
;
47 using ::com::sun::star::frame::theGlobalEventBroadcaster
;
48 using ::com::sun::star::uno::UNO_QUERY
;
50 namespace csslang
= ::com::sun::star::lang
;
52 // DocumentEventNotifier::Impl
54 typedef ::cppu::WeakComponentImplHelper1
< XDocumentEventListener
55 > DocumentEventNotifier_Impl_Base
;
63 /** impl class for DocumentEventNotifier
65 class DocumentEventNotifier::Impl
:public ::boost::noncopyable
66 ,public ::cppu::BaseMutex
67 ,public DocumentEventNotifier_Impl_Base
70 Impl (DocumentEventListener
&, Reference
<XModel
> const& rxDocument
);
73 // XDocumentEventListener
74 virtual void SAL_CALL
documentEventOccured( const DocumentEvent
& Event
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
77 virtual void SAL_CALL
disposing( const csslang::EventObject
& Event
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
80 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
83 /// determines whether the instance is already disposed
84 bool impl_isDisposed_nothrow() const { return m_pListener
== NULL
; }
86 /// disposes the instance
87 void impl_dispose_nothrow();
89 /// registers or revokes the instance as listener at the global event broadcaster
90 void impl_listenerAction_nothrow( ListenerAction _eAction
);
93 DocumentEventListener
* m_pListener
;
94 Reference
< XModel
> m_xModel
;
97 DocumentEventNotifier::Impl::Impl (DocumentEventListener
& rListener
, Reference
<XModel
> const& rxDocument
) :
98 DocumentEventNotifier_Impl_Base(m_aMutex
),
99 m_pListener(&rListener
),
102 osl_atomic_increment( &m_refCount
);
103 impl_listenerAction_nothrow( RegisterListener
);
104 osl_atomic_decrement( &m_refCount
);
107 DocumentEventNotifier::Impl::~Impl ()
109 if ( !impl_isDisposed_nothrow() )
116 void SAL_CALL
DocumentEventNotifier::Impl::documentEventOccured( const DocumentEvent
& _rEvent
) throw (RuntimeException
, std::exception
)
118 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
120 OSL_PRECOND( !impl_isDisposed_nothrow(), "DocumentEventNotifier::Impl::notifyEvent: disposed, but still getting events?" );
121 if ( impl_isDisposed_nothrow() )
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 sal_Char
* pEventName
;
132 void (DocumentEventListener::*listenerMethod
)( const ScriptDocument
& _rDocument
);
134 EventEntry 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 ( size_t i
=0; i
< SAL_N_ELEMENTS( aEvents
); ++i
)
148 if ( !_rEvent
.EventName
.equalsAscii( aEvents
[i
].pEventName
) )
151 ScriptDocument
aDocument( xDocument
);
153 // the listener implementations usually require the SolarMutex, so lock it here.
154 // But ensure the proper order of locking the solar and the own mutex
156 SolarMutexGuard aSolarGuard
;
157 ::osl::MutexGuard
aGuard2( m_aMutex
);
159 if ( impl_isDisposed_nothrow() )
160 // somebody took the chance to dispose us -> bail out
163 (m_pListener
->*aEvents
[i
].listenerMethod
)( aDocument
);
169 void SAL_CALL
DocumentEventNotifier::Impl::disposing( const csslang::EventObject
& /*Event*/ ) throw (RuntimeException
, std::exception
)
171 SolarMutexGuard aSolarGuard
;
172 ::osl::MutexGuard
aGuard( m_aMutex
);
174 if ( !impl_isDisposed_nothrow() )
175 impl_dispose_nothrow();
178 void SAL_CALL
DocumentEventNotifier::Impl::disposing()
180 impl_listenerAction_nothrow( RemoveListener
);
181 impl_dispose_nothrow();
184 void DocumentEventNotifier::Impl::impl_dispose_nothrow()
190 void DocumentEventNotifier::Impl::impl_listenerAction_nothrow( ListenerAction _eAction
)
194 Reference
< XDocumentEventBroadcaster
> xBroadcaster
;
196 xBroadcaster
.set( m_xModel
, UNO_QUERY_THROW
);
199 Reference
< com::sun::star::uno::XComponentContext
> aContext(
200 comphelper::getProcessComponentContext() );
201 xBroadcaster
= theGlobalEventBroadcaster::get(aContext
);
204 void ( SAL_CALL
XDocumentEventBroadcaster::*listenerAction
)( const Reference
< XDocumentEventListener
>& ) =
205 ( _eAction
== RegisterListener
) ? &XDocumentEventBroadcaster::addDocumentEventListener
: &XDocumentEventBroadcaster::removeDocumentEventListener
;
206 (xBroadcaster
.get()->*listenerAction
)( this );
208 catch( const Exception
& )
210 DBG_UNHANDLED_EXCEPTION();
214 // DocumentEventNotifier
216 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener
& rListener
, Reference
<XModel
> const& rxDocument
) :
217 m_pImpl(new Impl(rListener
, rxDocument
))
220 DocumentEventNotifier::DocumentEventNotifier (DocumentEventListener
& rListener
) :
221 m_pImpl(new Impl(rListener
, Reference
<XModel
>()))
224 DocumentEventNotifier::~DocumentEventNotifier()
228 void DocumentEventNotifier::dispose()
233 // DocumentEventListener
235 DocumentEventListener::~DocumentEventListener()
239 } // namespace basctl
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */