1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SVX_FMDISPATCH_HXX
25 #define SVX_FMDISPATCH_HXX
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/frame/XDispatch.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <com/sun/star/form/runtime/XFormOperations.hpp>
31 /** === end UNO includes === **/
32 
33 #include <cppuhelper/implbase1.hxx>
34 #include <cppuhelper/interfacecontainer.hxx>
35 
36 //........................................................................
37 namespace svx
38 {
39 //........................................................................
40 
41     //====================================================================
42     //= OSingleFeatureDispatcher
43     //====================================================================
44     typedef ::cppu::WeakImplHelper1 <   ::com::sun::star::frame::XDispatch
45                                     >   OSingleFeatureDispatcher_Base;
46 
47     class OSingleFeatureDispatcher : public OSingleFeatureDispatcher_Base
48     {
49     private:
50         ::osl::Mutex&                       m_rMutex;
51         ::cppu::OInterfaceContainerHelper   m_aStatusListeners;
52         ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormOperations >
53                                             m_xFormOperations;
54         const ::com::sun::star::util::URL   m_aFeatureURL;
55         ::com::sun::star::uno::Any          m_aLastKnownState;
56         const sal_Int16                     m_nFormFeature;
57         sal_Bool                            m_bLastKnownEnabled;
58         sal_Bool                            m_bDisposed;
59 
60     public:
61         /** constructs the dispatcher
62 
63             @param _rFeatureURL
64                 the URL of the feature which this instance is responsible for
65 
66             @param _nFeatureId
67                 the feature which this instance is responsible for
68 
69             @param _rController
70                 the controller which is responsible for providing the state of feature of this instance,
71                 and for executing it. After disposing the dispatcher instance, the controller will
72                 not be accessed anymore
73 
74             @see dispose
75         */
76         OSingleFeatureDispatcher(
77             const ::com::sun::star::util::URL& _rFeatureURL,
78             const sal_Int16 _nFormFeature,
79             const ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormOperations >& _rxFormOperations,
80             ::osl::Mutex& _rMutex
81         );
82 
83         /** disposes the dispatcher instance
84 
85             All status listeners will, after receiving an <member scope="com::sun::star::lang">XEventListener::disposing</member>
86             call, be released.
87 
88             The controller provided in the in constructor will not be used anymore after returning from this call.
89 
90             No further requests to dispatch slots will be accepted.
91 
92             Multiple calls are allowed: if the object already was disposed, then subsequent calls are
93             silently ignored.
94         */
95         void    dispose();
96 
97         /** notifies all our listeners of the current state
98         */
99         void    updateAllListeners();
100 
101     protected:
102         // XDispatch
103         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);
104         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);
105         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);
106 
107     protected:
108         /** notifies our current state to one or all listeners
109 
110             @param _rxListener
111                 the listener to notify. May be NULL, in this case all our listeners will be
112                 notified with the current state
113 
114             @param _rFreeForNotification
115                 a guard which currently locks our mutex, and which is to be cleared
116                 for actually doing the notification(s)
117         */
118         void    notifyStatus(
119                     const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >& _rxListener,
120                     ::osl::ClearableMutexGuard& _rFreeForNotification
121                 );
122 
123     private:
124         /** checks whether our instance is alive
125 
126             If the instance already received a <member>dispose</member> call, then a
127             <type scope="com::sun::star::lang">DisposedException</type> is thrown.
128 
129             @precond
130                 our Mutex is locked - else calling the method would not make sense, since
131                 it's result could be out-of-date as soon as it's returned to the caller.
132         */
133         void    checkAlive() const SAL_THROW((::com::sun::star::lang::DisposedException));
134 
135         /** retrieves the current status of our feature, in a format which can be used
136             for UNO notifications
137 
138             @precond
139                 our mutex is locked
140         */
141         void    getUnoState( ::com::sun::star::frame::FeatureStateEvent& /* [out] */ _rState ) const;
142 
143     private:
144         OSingleFeatureDispatcher();                                             // never implemented
145         OSingleFeatureDispatcher( const OSingleFeatureDispatcher& );            // never implemented
146         OSingleFeatureDispatcher& operator=( const OSingleFeatureDispatcher& ); // never implemented
147     };
148 
149 //........................................................................
150 }   // namespace svx
151 //........................................................................
152 
153 #endif
154