1*c45d927aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c45d927aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c45d927aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c45d927aSAndrew Rist  * distributed with this work for additional information
6*c45d927aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c45d927aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c45d927aSAndrew Rist  * "License"); you may not use this file except in compliance
9*c45d927aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c45d927aSAndrew Rist  *
11*c45d927aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c45d927aSAndrew Rist  *
13*c45d927aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c45d927aSAndrew Rist  * software distributed under the License is distributed on an
15*c45d927aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c45d927aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c45d927aSAndrew Rist  * specific language governing permissions and limitations
18*c45d927aSAndrew Rist  * under the License.
19*c45d927aSAndrew Rist  *
20*c45d927aSAndrew Rist  *************************************************************/
21*c45d927aSAndrew Rist 
22*c45d927aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SD_FRAMEWORK_CONFIGURATION_UPDATER_HXX
25cdf0e10cSrcweir #define SD_FRAMEWORK_CONFIGURATION_UPDATER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "ConfigurationControllerResourceManager.hxx"
28cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XResourceId.hpp>
29cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XConfiguration.hpp>
30cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XControllerManager.hpp>
31cdf0e10cSrcweir #include <vcl/timer.hxx>
32cdf0e10cSrcweir #include <vector>
33cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace css = ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace sd { namespace framework {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir class ConfigurationClassifier;
40cdf0e10cSrcweir class ConfigurationUpdaterLock;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /** This is a helper class for the ConfigurationController.  It handles the
43cdf0e10cSrcweir     update of the current configuration so that it looks like a requested
44cdf0e10cSrcweir     configuration.  An update is made by activating or deactivating drawing
45cdf0e10cSrcweir     framework resources.
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     When an update is not successfull, i.e. after the update the current
48cdf0e10cSrcweir     configuration is not equivalent to the requested configuration, then a
49cdf0e10cSrcweir     timer is started to repeat the update after a short time.
50cdf0e10cSrcweir */
51cdf0e10cSrcweir class ConfigurationUpdater
52cdf0e10cSrcweir {
53cdf0e10cSrcweir public:
54cdf0e10cSrcweir     /** Create a new ConfigurationUpdater object that notifies configuration
55cdf0e10cSrcweir         changes and the start and end of updates via the given broadcaster.
56cdf0e10cSrcweir     */
57cdf0e10cSrcweir     ConfigurationUpdater (
58cdf0e10cSrcweir         const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster,
59cdf0e10cSrcweir         const ::boost::shared_ptr<ConfigurationControllerResourceManager>& rpResourceManager,
60cdf0e10cSrcweir         const css::uno::Reference<
61cdf0e10cSrcweir             css::drawing::framework::XControllerManager>& rxControllerManager);
62cdf0e10cSrcweir     ~ConfigurationUpdater (void);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     /** This method is typically called once, when the controller manager is
65cdf0e10cSrcweir         accessible to the caller.
66cdf0e10cSrcweir     */
67cdf0e10cSrcweir     void SetControllerManager(
68cdf0e10cSrcweir         const css::uno::Reference<
69cdf0e10cSrcweir             css::drawing::framework::XControllerManager>& rxControllerManager);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /** Request an update of the current configuration so that it looks like
72cdf0e10cSrcweir         the given requested configuration.  It checks whether an update of
73cdf0e10cSrcweir         the current configuration can be done.  Calls UpdateConfiguration()
74cdf0e10cSrcweir         if that is the case.  Otherwise it schedules a later call to
75cdf0e10cSrcweir         UpdateConfiguration().
76cdf0e10cSrcweir     */
77cdf0e10cSrcweir     void RequestUpdate (const css::uno::Reference<
78cdf0e10cSrcweir         css::drawing::framework::XConfiguration>& rxRequestedConfiguration);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     css::uno::Reference<
81cdf0e10cSrcweir         css::drawing::framework::XConfiguration> GetCurrentConfiguration (void) const;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     friend class ConfigurationUpdaterLock;
84cdf0e10cSrcweir     /** Return a lock of the called ConfigurationUpdater.  While the
85cdf0e10cSrcweir         returned object exists no update of the current configuration is
86cdf0e10cSrcweir         made.
87cdf0e10cSrcweir     */
88cdf0e10cSrcweir     ::boost::shared_ptr<ConfigurationUpdaterLock> GetLock (void);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir private:
91cdf0e10cSrcweir     /** A reference to the XControllerManager is kept so that
92cdf0e10cSrcweir         UpdateConfiguration() has access to the other sub controllers.
93cdf0e10cSrcweir     */
94cdf0e10cSrcweir     css::uno::Reference<
95cdf0e10cSrcweir         css::drawing::framework::XControllerManager> mxControllerManager;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     ::boost::shared_ptr<ConfigurationControllerBroadcaster> mpBroadcaster;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /** The current configuration holds the resources that are currently
100cdf0e10cSrcweir         active.  It is modified during an update.
101cdf0e10cSrcweir     */
102cdf0e10cSrcweir     css::uno::Reference<
103cdf0e10cSrcweir         css::drawing::framework::XConfiguration> mxCurrentConfiguration;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     /** The requested configuration holds the resources that have been
106cdf0e10cSrcweir         requested to activate or to deactivate since the last update.  It is
107cdf0e10cSrcweir         (usually) not modified during an update.  This configuration is
108cdf0e10cSrcweir         maintained by the ConfigurationController and given to the
109cdf0e10cSrcweir         ConfigurationUpdater in the RequestUpdate() method.
110cdf0e10cSrcweir     */
111cdf0e10cSrcweir     css::uno::Reference<
112cdf0e10cSrcweir         css::drawing::framework::XConfiguration> mxRequestedConfiguration;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     /** This flag is set to </sal_True> when an update of the current
115cdf0e10cSrcweir         configurtion was requested (because the last request in the queue
116cdf0e10cSrcweir         was processed) but could not be exected because the
117cdf0e10cSrcweir         ConfigurationController was locked.  A call to UpdateConfiguration()
118cdf0e10cSrcweir         resets the flag to </sal_False>.
119cdf0e10cSrcweir     */
120cdf0e10cSrcweir     bool mbUpdatePending;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     /** This flag is set to </sal_True> while the UpdateConfiguration() method
123cdf0e10cSrcweir         is running.  It is used to prevent reentrance problems with this
124cdf0e10cSrcweir         method.
125cdf0e10cSrcweir     */
126cdf0e10cSrcweir     bool mbUpdateBeingProcessed;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     /** The ConfigurationController is locked when this count has a value
129cdf0e10cSrcweir         larger then zero.  If the controller is locked then updates of the
130cdf0e10cSrcweir         current configuration are not made.
131cdf0e10cSrcweir     */
132cdf0e10cSrcweir     sal_Int32 mnLockCount;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     /** This timer is used to check from time to time whether the requested
135cdf0e10cSrcweir         configuration and the current configuration are identcal and request
136cdf0e10cSrcweir         an update when they are not.
137cdf0e10cSrcweir         This is used to overcome problems with resources that become
138cdf0e10cSrcweir         available asynchronously.
139cdf0e10cSrcweir     */
140cdf0e10cSrcweir     Timer maUpdateTimer;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     /** The number of failed updates (those after which the current
143cdf0e10cSrcweir         configuration is not equivalent to the requested configuration) is
144cdf0e10cSrcweir         used to determine how long to wait before another update is made.
145cdf0e10cSrcweir     */
146cdf0e10cSrcweir     sal_Int32 mnFailedUpdateCount;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     ::boost::shared_ptr<ConfigurationControllerResourceManager> mpResourceManager;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     /** This method does the main work of an update.  It calls the sub
151cdf0e10cSrcweir         controllers that are responsible for the various types of resources
152cdf0e10cSrcweir         and tells them to update their active resources.  It notifies
153cdf0e10cSrcweir         listeners about the start and end of the configuration update.
154cdf0e10cSrcweir     */
155cdf0e10cSrcweir     void UpdateConfiguration (void);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     /** Basically calls UpdaterStart() andUpdateEnd() and makes some debug
158cdf0e10cSrcweir         output.
159cdf0e10cSrcweir     */
160cdf0e10cSrcweir     void UpdateCore (const ConfigurationClassifier& rClassifier);
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     /** Check for all pure anchors if they have at least one child.
163cdf0e10cSrcweir         Childless pure anchors are deactivated.
164cdf0e10cSrcweir         This affects only the current configuration.
165cdf0e10cSrcweir     */
166cdf0e10cSrcweir     void CheckPureAnchors (
167cdf0e10cSrcweir         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration,
168cdf0e10cSrcweir         ::std::vector<css::uno::Reference<css::drawing::framework::XResourceId> >&
169cdf0e10cSrcweir             rResourcesToDeactivate);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     /** Remove from the requested configration all pure anchors that have no
172cdf0e10cSrcweir         child.  Requested but not yet activated anchors can not be removed
173cdf0e10cSrcweir         because without the actual resource the 'pureness' of an anchor can
174cdf0e10cSrcweir         not be determined.
175cdf0e10cSrcweir     */
176cdf0e10cSrcweir     void CleanRequestedConfiguration (void);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     /** Check the success of a recently executed configuration update.
179cdf0e10cSrcweir         When the update failed then start the timer.
180cdf0e10cSrcweir     */
181cdf0e10cSrcweir     void CheckUpdateSuccess (void);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     /** This method sets the mbUpdateBeingProcessed member that is used to
184cdf0e10cSrcweir         prevent reentrance problems.  This method allows function objects
185cdf0e10cSrcweir         easyly and safely to modify the variable.
186cdf0e10cSrcweir     */
187cdf0e10cSrcweir     void SetUpdateBeingProcessed (bool bValue);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     /** Return whether it is possible to do an update of the configuration.
190cdf0e10cSrcweir         This takes into account whether another update is currently being
191cdf0e10cSrcweir         executed, the lock count, and whether the configuration controller
192cdf0e10cSrcweir         is still valid.
193cdf0e10cSrcweir     */
194cdf0e10cSrcweir     bool IsUpdatePossible (void);
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     /** Lock updates of the current configuration.  For intermediate requests
197cdf0e10cSrcweir         for updates mbUpdatePending is set to <TRUE/>.
198cdf0e10cSrcweir     */
199cdf0e10cSrcweir     void LockUpdates (void);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     /** When an update was requested since the last LockUpdates() call then
202cdf0e10cSrcweir         RequestUpdate() is called.
203cdf0e10cSrcweir     */
204cdf0e10cSrcweir     void UnlockUpdates (void);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     DECL_LINK(TimeoutHandler, Timer*);
207cdf0e10cSrcweir };
208cdf0e10cSrcweir 
209cdf0e10cSrcweir } } // end of namespace sd::framework
210cdf0e10cSrcweir 
211cdf0e10cSrcweir #endif
212