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