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 FORMS_FORM_NAVIGATION_HXX
29 #define FORMS_FORM_NAVIGATION_HXX
30 
31 #include <com/sun/star/frame/XDispatchProviderInterception.hpp>
32 #include <com/sun/star/frame/XStatusListener.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/util/XURLTransformer.hpp>
35 #include <cppuhelper/implbase2.hxx>
36 #include "featuredispatcher.hxx"
37 #include <vector>
38 #include <map>
39 #include <memory>
40 
41 
42 //.........................................................................
43 namespace frm
44 {
45 //.........................................................................
46 
47     class UrlTransformer;
48     class ControlFeatureInterception;
49 
50     //==================================================================
51     //= OFormNavigationHelper
52     //==================================================================
53     typedef ::cppu::ImplHelper2 <   ::com::sun::star::frame::XDispatchProviderInterception
54                                 ,   ::com::sun::star::frame::XStatusListener
55                                 >   OFormNavigationHelper_Base;
56 
57     class OFormNavigationHelper
58                         :public OFormNavigationHelper_Base
59                         ,public IFeatureDispatcher
60     {
61     private:
62         struct FeatureInfo
63         {
64             ::com::sun::star::util::URL                                             aURL;
65             ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >  xDispatcher;
66             sal_Bool                                                                bCachedState;
67             ::com::sun::star::uno::Any                                              aCachedAdditionalState;
68 
69             FeatureInfo() : bCachedState( sal_False ) { }
70         };
71         typedef ::std::map< sal_Int16, FeatureInfo >    FeatureMap;
72 
73     private:
74         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
75                             m_xORB;
76         ::std::auto_ptr< ControlFeatureInterception >
77                             m_pFeatureInterception;
78 
79         // all supported features
80         FeatureMap          m_aSupportedFeatures;
81         // all features which we have an external dispatcher for
82         sal_Int32           m_nConnectedFeatures;
83 
84     protected:
85         inline const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&
86             getORB( ) const { return m_xORB; }
87 
88     protected:
89         OFormNavigationHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB );
90         virtual ~OFormNavigationHelper();
91 
92 	    // XComponent
93         void SAL_CALL dispose(  ) throw( ::com::sun::star::uno::RuntimeException );
94 
95         // XDispatchProviderInterception
96         virtual void SAL_CALL registerDispatchProviderInterceptor( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProviderInterceptor >& Interceptor ) throw (::com::sun::star::uno::RuntimeException);
97         virtual void SAL_CALL releaseDispatchProviderInterceptor( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProviderInterceptor >& Interceptor ) throw (::com::sun::star::uno::RuntimeException);
98 
99         // XStatusListener
100         virtual void SAL_CALL statusChanged( const ::com::sun::star::frame::FeatureStateEvent& State ) throw (::com::sun::star::uno::RuntimeException);
101 
102         // XEventListener
103         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
104 
105         // IFeatureDispatcher
106         virtual void            dispatch( sal_Int16 _nFeatureId ) const;
107         virtual void            dispatchWithArgument( sal_Int16 _nFeatureId, const sal_Char* _pParamName, const ::com::sun::star::uno::Any& _rParamValue ) const;
108         virtual bool            isEnabled( sal_Int16 _nFeatureId ) const;
109         virtual bool            getBooleanState( sal_Int16 _nFeatureId ) const;
110         virtual ::rtl::OUString getStringState( sal_Int16 _nFeatureId ) const;
111         virtual sal_Int32       getIntegerState( sal_Int16 _nFeatureId ) const;
112 
113         // own overridables
114         /** is called when the interceptors have.
115             <p>The default implementations simply calls <member>updateDispatches</member>,
116             derived classes can prevent this in certain cases, or do additional handling.</p>
117         */
118         virtual void    interceptorsChanged( );
119 
120         /** called when the status of a feature changed
121 
122             <p>The default implementation does nothing.</p>
123 
124             <p>If the feature in question does support more state information that just the
125             enabled/disabled state, then this additional information is to be retrieved in
126             a separate call.</p>
127 
128             @param _nFeatureId
129                 the id of the feature
130             @param _bEnabled
131                 determines if the features is enabled or disabled
132             @see getBooleanState
133         */
134         virtual void    featureStateChanged( sal_Int16 _nFeatureId, sal_Bool _bEnabled );
135 
136         /** notification for (potential) changes in the state of all features
137             <p>The base class implementation does nothing. Derived classes could force
138             their peer to update it's state, depending on the result of calls to
139             <member>IFeatureDispatcher::isEnabled</member>.</p>
140         */
141         virtual void    allFeatureStatesChanged( );
142 
143         /** retrieves the list of supported features
144             <p>To be overridden by derived classes</p>
145             @param _rFeatureIds
146                 the array of features to support. Out parameter to fill by the derivee's implementation
147             @pure
148         */
149         virtual void    getSupportedFeatures( ::std::vector< sal_Int16 >& /* [out] */ _rFeatureIds ) = 0;
150 
151     protected:
152         /** update all our dispatches which are controlled by our dispatch interceptors
153         */
154         void    updateDispatches();
155 
156         /** connect to the dispatch interceptors
157         */
158         void    connectDispatchers();
159 
160         /** disconnect from the dispatch interceptors
161         */
162         void    disconnectDispatchers();
163 
164         /** queries the interceptor chain for a dispatcher for the given URL
165         */
166         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >
167                 queryDispatch( const ::com::sun::star::util::URL& _rURL );
168 
169         /** invalidates the set of supported features
170 
171             <p>This will invalidate all structures which are tied to the set of supported
172             features. All dispatches will be disconnected.<br/>
173             No automatic re-connection to potential external dispatchers is done, instead,
174             you have to call updateDispatches explicitly, if necessary.</p>
175         */
176         void    invalidateSupportedFeaturesSet();
177 
178     private:
179         /** initialize m_aSupportedFeatures, if necessary
180         */
181         void    initializeSupportedFeatures();
182     };
183 
184     //==================================================================
185     //= OFormNavigationMapper
186     //==================================================================
187     /** helper class mapping between feature ids and feature URLs
188     */
189     class OFormNavigationMapper
190     {
191     private:
192         ::std::auto_ptr< UrlTransformer >   m_pUrlTransformer;
193 
194     public:
195         OFormNavigationMapper(
196             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB
197         );
198         ~OFormNavigationMapper( );
199 
200         /** retrieves the ASCII representation of a feature URL belonging to an id
201 
202             @complexity O(log n)
203             @return NULL if the given id is not a known feature id (which is a valid usage)
204         */
205         const char* getFeatureURLAscii( sal_Int16 _nFeatureId );
206 
207         /** retrieves the feature URL belonging to an feature id
208 
209             @complexity O(log n), with n being the number of all potentially known URLs
210             @return
211                 <TRUE/> if and only if the given id is a known feature id
212                 (which is a valid usage)
213         */
214         bool        getFeatureURL( sal_Int16 _nFeatureId, ::com::sun::star::util::URL& /* [out] */ _rURL );
215 
216         /** retrieves the feature id belonging to an feature URL
217 
218             @complexity O(n), with n being the number of all potentially known URLs
219             @return
220                 the id of the feature URL, or -1 if the URl is not known
221                 (which is a valid usage)
222         */
223         sal_Int16   getFeatureId( const ::rtl::OUString& _rCompleteURL );
224 
225     private:
226         OFormNavigationMapper( );                                           // never implemented
227         OFormNavigationMapper( const OFormNavigationMapper& );              // never implemented
228         OFormNavigationMapper& operator=( const OFormNavigationMapper& );   // never implemented
229     };
230 
231 //.........................................................................
232 }   // namespace frm
233 //.........................................................................
234 
235 #endif // FORMS_FORM_NAVIGATION_HXX
236