1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_forms.hxx"
30 #include "featuredispatcher.hxx"
31 
32 /** === begin UNO includes === **/
33 /** === end UNO includes === **/
34 
35 //........................................................................
36 namespace frm
37 {
38 //........................................................................
39 
40     using namespace ::com::sun::star::uno;
41     using namespace ::com::sun::star::frame;
42     using namespace ::com::sun::star::lang;
43     using namespace ::com::sun::star::util;
44 
45     //====================================================================
46 	//= ORichTextFeatureDispatcher
47 	//====================================================================
48 	//--------------------------------------------------------------------
49     ORichTextFeatureDispatcher::ORichTextFeatureDispatcher( EditView& _rView, const URL&  _rURL )
50         :m_aFeatureURL( _rURL )
51         ,m_aStatusListeners( m_aMutex )
52         ,m_pEditView( &_rView )
53         ,m_bDisposed( false )
54     {
55     }
56 
57 	//--------------------------------------------------------------------
58     ORichTextFeatureDispatcher::~ORichTextFeatureDispatcher( )
59     {
60         if ( !m_bDisposed )
61         {
62             acquire();
63             dispose();
64         }
65     }
66 
67     //--------------------------------------------------------------------
68     void ORichTextFeatureDispatcher::dispose()
69     {
70         EventObject aEvent( *this );
71         m_aStatusListeners.disposeAndClear( aEvent );
72 
73         ::osl::ClearableMutexGuard aGuard( m_aMutex );
74         m_bDisposed = true;
75         disposing( aGuard );
76     }
77 
78     //--------------------------------------------------------------------
79     void ORichTextFeatureDispatcher::disposing( ::osl::ClearableMutexGuard& /*_rClearBeforeNotify*/ )
80     {
81         m_pEditView = NULL;
82     }
83 
84     //--------------------------------------------------------------------
85     void SAL_CALL ORichTextFeatureDispatcher::addStatusListener( const Reference< XStatusListener >& _rxControl, const URL& _rURL ) throw (RuntimeException)
86     {
87         OSL_ENSURE( !m_bDisposed, "ORichTextFeatureDispatcher::addStatusListener: already disposed!" );
88         if ( m_bDisposed )
89             throw DisposedException();
90 
91         OSL_ENSURE( _rURL.Complete == getFeatureURL().Complete, "ORichTextFeatureDispatcher::addStatusListener: invalid URL!" );
92         if ( _rURL.Complete == getFeatureURL().Complete )
93             if ( _rxControl.is() )
94             {
95                 m_aStatusListeners.addInterface( _rxControl );
96                 newStatusListener( _rxControl );
97             }
98     }
99 
100     //--------------------------------------------------------------------
101     void SAL_CALL ORichTextFeatureDispatcher::removeStatusListener( const Reference< XStatusListener >& _rxControl, const URL& /*_rURL*/ ) throw (RuntimeException)
102     {
103         m_aStatusListeners.removeInterface( _rxControl );
104     }
105 
106     //--------------------------------------------------------------------
107     void ORichTextFeatureDispatcher::invalidate()
108     {
109         invalidateFeatureState_Broadcast();
110     }
111 
112 	//--------------------------------------------------------------------
113     FeatureStateEvent ORichTextFeatureDispatcher::buildStatusEvent() const
114     {
115         FeatureStateEvent aEvent;
116         aEvent.IsEnabled = sal_False;
117         aEvent.Source = *const_cast< ORichTextFeatureDispatcher* >( this );
118         aEvent.FeatureURL = getFeatureURL();
119         aEvent.Requery = sal_False;
120         return aEvent;
121     }
122 
123 	//--------------------------------------------------------------------
124     void ORichTextFeatureDispatcher::invalidateFeatureState_Broadcast()
125     {
126         FeatureStateEvent aEvent( buildStatusEvent() );
127         ::cppu::OInterfaceIteratorHelper aIter( getStatusListeners() );
128     	while ( aIter.hasMoreElements() )
129             doNotify( static_cast< XStatusListener* >( aIter.next() ), aEvent );
130     }
131 
132     //--------------------------------------------------------------------
133     void ORichTextFeatureDispatcher::newStatusListener( const Reference< XStatusListener >& _rxListener )
134     {
135         doNotify( _rxListener, buildStatusEvent() );
136     }
137 
138 	//--------------------------------------------------------------------
139     void ORichTextFeatureDispatcher::doNotify( const Reference< XStatusListener >& _rxListener, const FeatureStateEvent& _rEvent ) const SAL_THROW(())
140     {
141         OSL_PRECOND( _rxListener.is(), "ORichTextFeatureDispatcher::doNotify: invalid listener!" );
142         if ( _rxListener.is() )
143         {
144             try
145             {
146                 _rxListener->statusChanged( _rEvent );
147             }
148             catch( const Exception& )
149             {
150             	OSL_ENSURE( sal_False, "ORichTextFeatureDispatcher::doNotify: caught an exception!" );
151             }
152         }
153     }
154 
155 //........................................................................
156 }   // namespace frm
157 //........................................................................
158