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 #include "featuredispatcher.hxx"
21 #include <osl/diagnose.h>
22 
23 
24 namespace frm
25 {
26 
27 
28     using namespace ::com::sun::star::uno;
29     using namespace ::com::sun::star::frame;
30     using namespace ::com::sun::star::lang;
31     using namespace ::com::sun::star::util;
32 
ORichTextFeatureDispatcher(EditView & _rView,const URL & _rURL)33     ORichTextFeatureDispatcher::ORichTextFeatureDispatcher( EditView& _rView, const URL&  _rURL )
34         :m_aFeatureURL( _rURL )
35         ,m_aStatusListeners( m_aMutex )
36         ,m_pEditView( &_rView )
37         ,m_bDisposed( false )
38     {
39     }
40 
41 
~ORichTextFeatureDispatcher()42     ORichTextFeatureDispatcher::~ORichTextFeatureDispatcher( )
43     {
44         if ( !m_bDisposed )
45         {
46             acquire();
47             dispose();
48         }
49     }
50 
51 
dispose()52     void ORichTextFeatureDispatcher::dispose()
53     {
54         EventObject aEvent( *this );
55         m_aStatusListeners.disposeAndClear( aEvent );
56 
57         ::osl::ClearableMutexGuard aGuard( m_aMutex );
58         m_bDisposed = true;
59         disposing( aGuard );
60     }
61 
62 
disposing(::osl::ClearableMutexGuard &)63     void ORichTextFeatureDispatcher::disposing( ::osl::ClearableMutexGuard& /*_rClearBeforeNotify*/ )
64     {
65         m_pEditView = nullptr;
66     }
67 
68 
addStatusListener(const Reference<XStatusListener> & _rxControl,const URL & _rURL)69     void SAL_CALL ORichTextFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL )
70     {
71         OSL_ENSURE( !m_bDisposed, "ORichTextFeatureDispatcher::addStatusListener: already disposed!" );
72         if ( m_bDisposed )
73             throw DisposedException();
74 
75         OSL_ENSURE( _rURL.Complete == getFeatureURL().Complete, "ORichTextFeatureDispatcher::addStatusListener: invalid URL!" );
76         if ( _rURL.Complete == getFeatureURL().Complete )
77             if ( _rxControl.is() )
78             {
79                 m_aStatusListeners.addInterface( _rxControl );
80                 doNotify( _rxControl, buildStatusEvent() );
81             }
82     }
83 
84 
removeStatusListener(const Reference<XStatusListener> & _rxControl,const URL &)85     void SAL_CALL ORichTextFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& /*_rURL*/ )
86     {
87         m_aStatusListeners.removeInterface( _rxControl );
88     }
89 
90 
invalidate()91     void ORichTextFeatureDispatcher::invalidate()
92     {
93         invalidateFeatureState_Broadcast();
94     }
95 
96 
buildStatusEvent() const97     FeatureStateEvent ORichTextFeatureDispatcher::buildStatusEvent() const
98     {
99         FeatureStateEvent aEvent;
100         aEvent.IsEnabled = false;
101         aEvent.Source = *const_cast< ORichTextFeatureDispatcher* >( this );
102         aEvent.FeatureURL = getFeatureURL();
103         aEvent.Requery = false;
104         return aEvent;
105     }
106 
107 
invalidateFeatureState_Broadcast()108     void ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast()
109     {
110         FeatureStateEvent aEvent( buildStatusEvent() );
111         ::comphelper::OInterfaceIteratorHelper2 aIter( getStatusListeners() );
112         while ( aIter.hasMoreElements() )
113             doNotify( static_cast< XStatusListener* >( aIter.next() ), aEvent );
114     }
115 
116 
doNotify(const Reference<XStatusListener> & _rxListener,const FeatureStateEvent & _rEvent)117     void ORichTextFeatureDispatcher::doNotify( const Reference< XStatusListener >& _rxListener, const FeatureStateEvent& _rEvent )
118     {
119         OSL_PRECOND( _rxListener.is(), "ORichTextFeatureDispatcher::doNotify: invalid listener!" );
120         if ( _rxListener.is() )
121         {
122             try
123             {
124                 _rxListener->statusChanged( _rEvent );
125             }
126             catch( const Exception& )
127             {
128                 OSL_FAIL( "ORichTextFeatureDispatcher::doNotify: caught an exception!" );
129             }
130         }
131     }
132 
133 
134 }   // namespace frm
135 
136 
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
138