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 #ifndef SVX_FMDISPATCH_HXX
29 #define SVX_FMDISPATCH_HXX
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/frame/XDispatch.hpp>
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <com/sun/star/form/runtime/XFormOperations.hpp>
35 /** === end UNO includes === **/
36 
37 #include <cppuhelper/implbase1.hxx>
38 #include <cppuhelper/interfacecontainer.hxx>
39 
40 //........................................................................
41 namespace svx
42 {
43 //........................................................................
44 
45     //====================================================================
46     //= OSingleFeatureDispatcher
47     //====================================================================
48     typedef ::cppu::WeakImplHelper1 <   ::com::sun::star::frame::XDispatch
49                                     >   OSingleFeatureDispatcher_Base;
50 
51     class OSingleFeatureDispatcher : public OSingleFeatureDispatcher_Base
52     {
53     private:
54         ::osl::Mutex&                       m_rMutex;
55         ::cppu::OInterfaceContainerHelper   m_aStatusListeners;
56         ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormOperations >
57                                             m_xFormOperations;
58         const ::com::sun::star::util::URL   m_aFeatureURL;
59         ::com::sun::star::uno::Any          m_aLastKnownState;
60         const sal_Int16                     m_nFormFeature;
61         sal_Bool                            m_bLastKnownEnabled;
62         sal_Bool                            m_bDisposed;
63 
64     public:
65         /** constructs the dispatcher
66 
67             @param _rFeatureURL
68                 the URL of the feature which this instance is responsible for
69 
70             @param _nFeatureId
71                 the feature which this instance is responsible for
72 
73             @param _rController
74                 the controller which is responsible for providing the state of feature of this instance,
75                 and for executing it. After disposing the dispatcher instance, the controller will
76                 not be accessed anymore
77 
78             @see dispose
79         */
80         OSingleFeatureDispatcher(
81             const ::com::sun::star::util::URL& _rFeatureURL,
82             const sal_Int16 _nFormFeature,
83             const ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormOperations >& _rxFormOperations,
84             ::osl::Mutex& _rMutex
85         );
86 
87         /** disposes the dispatcher instance
88 
89             All status listeners will, after receiving an <member scope="com::sun::star::lang">XEventListener::disposing</member>
90             call, be released.
91 
92             The controller provided in the in constructor will not be used anymore after returning from this call.
93 
94             No further requests to dispatch slots will be accepted.
95 
96             Multiple calls are allowed: if the object already was disposed, then subsequent calls are
97             silently ignored.
98         */
99         void    dispose();
100 
101         /** notifies all our listeners of the current state
102         */
103         void    updateAllListeners();
104 
105     protected:
106         // XDispatch
107         virtual void SAL_CALL dispatch( const ::com::sun::star::util::URL& _rURL, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments ) throw (::com::sun::star::uno::RuntimeException);
108         virtual void SAL_CALL addStatusListener( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >& _rxControl, const ::com::sun::star::util::URL& _rURL ) throw (::com::sun::star::uno::RuntimeException);
109         virtual void SAL_CALL removeStatusListener( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >& _rxControl, const ::com::sun::star::util::URL& _rURL ) throw (::com::sun::star::uno::RuntimeException);
110 
111     protected:
112         /** notifies our current state to one or all listeners
113 
114             @param _rxListener
115                 the listener to notify. May be NULL, in this case all our listeners will be
116                 notified with the current state
117 
118             @param _rFreeForNotification
119                 a guard which currently locks our mutex, and which is to be cleared
120                 for actually doing the notification(s)
121         */
122         void    notifyStatus(
123                     const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >& _rxListener,
124                     ::osl::ClearableMutexGuard& _rFreeForNotification
125                 );
126 
127     private:
128         /** checks whether our instance is alive
129 
130             If the instance already received a <member>dispose</member> call, then a
131             <type scope="com::sun::star::lang">DisposedException</type> is thrown.
132 
133             @precond
134                 our Mutex is locked - else calling the method would not make sense, since
135                 it's result could be out-of-date as soon as it's returned to the caller.
136         */
137         void    checkAlive() const SAL_THROW((::com::sun::star::lang::DisposedException));
138 
139         /** retrieves the current status of our feature, in a format which can be used
140             for UNO notifications
141 
142             @precond
143                 our mutex is locked
144         */
145         void    getUnoState( ::com::sun::star::frame::FeatureStateEvent& /* [out] */ _rState ) const;
146 
147     private:
148         OSingleFeatureDispatcher();                                             // never implemented
149         OSingleFeatureDispatcher( const OSingleFeatureDispatcher& );            // never implemented
150         OSingleFeatureDispatcher& operator=( const OSingleFeatureDispatcher& ); // never implemented
151     };
152 
153 //........................................................................
154 }   // namespace svx
155 //........................................................................
156 
157 #endif
158