1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "ChangeRequestQueueProcessor.hxx"
27cdf0e10cSrcweir #include "ConfigurationTracer.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "framework/ConfigurationController.hxx"
30cdf0e10cSrcweir #include "ConfigurationUpdater.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <vcl/svapp.hxx>
33cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
34cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XConfiguration.hpp>
35cdf0e10cSrcweir #include <com/sun/star/drawing/framework/ConfigurationChangeEvent.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::com::sun::star;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #undef VERBOSE
42cdf0e10cSrcweir //#define VERBOSE 1
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #ifdef VERBOSE
47cdf0e10cSrcweir 
TraceRequest(const Reference<XConfigurationChangeRequest> & rxRequest)48cdf0e10cSrcweir void TraceRequest (const Reference<XConfigurationChangeRequest>& rxRequest)
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     Reference<container::XNamed> xNamed (rxRequest, UNO_QUERY);
51cdf0e10cSrcweir     if (xNamed.is())
52cdf0e10cSrcweir         OSL_TRACE("    %s\n",
53cdf0e10cSrcweir             ::rtl::OUStringToOString(xNamed->getName(), RTL_TEXTENCODING_UTF8).getStr());
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #endif
57cdf0e10cSrcweir 
58cdf0e10cSrcweir } // end of anonymous namespace
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir namespace sd { namespace framework {
62cdf0e10cSrcweir 
ChangeRequestQueueProcessor(const::rtl::Reference<ConfigurationController> & rpConfigurationController,const::boost::shared_ptr<ConfigurationUpdater> & rpConfigurationUpdater)63cdf0e10cSrcweir ChangeRequestQueueProcessor::ChangeRequestQueueProcessor (
64cdf0e10cSrcweir     const ::rtl::Reference<ConfigurationController>& rpConfigurationController,
65cdf0e10cSrcweir     const ::boost::shared_ptr<ConfigurationUpdater>& rpConfigurationUpdater)
66cdf0e10cSrcweir     : maMutex(),
67cdf0e10cSrcweir       maQueue(),
68cdf0e10cSrcweir       mnUserEventId(0),
69cdf0e10cSrcweir       mxConfiguration(),
70cdf0e10cSrcweir       mpConfigurationController(rpConfigurationController),
71cdf0e10cSrcweir       mpConfigurationUpdater(rpConfigurationUpdater)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
~ChangeRequestQueueProcessor(void)78cdf0e10cSrcweir ChangeRequestQueueProcessor::~ChangeRequestQueueProcessor (void)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     if (mnUserEventId != 0)
81cdf0e10cSrcweir         Application::RemoveUserEvent(mnUserEventId);
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
SetConfiguration(const Reference<XConfiguration> & rxConfiguration)87cdf0e10cSrcweir void ChangeRequestQueueProcessor::SetConfiguration (
88cdf0e10cSrcweir     const Reference<XConfiguration>& rxConfiguration)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     mxConfiguration = rxConfiguration;
93cdf0e10cSrcweir     StartProcessing();
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
AddRequest(const Reference<XConfigurationChangeRequest> & rxRequest)99cdf0e10cSrcweir void ChangeRequestQueueProcessor::AddRequest (
100cdf0e10cSrcweir     const Reference<XConfigurationChangeRequest>& rxRequest)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir #ifdef VERBOSE
105cdf0e10cSrcweir     if (maQueue.empty())
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         OSL_TRACE("Adding requests to empty queue\n");
108cdf0e10cSrcweir         ConfigurationTracer::TraceConfiguration(
109cdf0e10cSrcweir             mxConfiguration, "current configuration of queue processor");
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     OSL_TRACE("Adding request\n");
112cdf0e10cSrcweir     TraceRequest(rxRequest);
113cdf0e10cSrcweir #endif
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     maQueue.push_back(rxRequest);
116cdf0e10cSrcweir     StartProcessing();
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
StartProcessing(void)122cdf0e10cSrcweir void ChangeRequestQueueProcessor::StartProcessing (void)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     if (mnUserEventId == 0
127cdf0e10cSrcweir         && mxConfiguration.is()
128cdf0e10cSrcweir         && ! maQueue.empty())
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir #ifdef VERBOSE
131cdf0e10cSrcweir         OSL_TRACE("ChangeRequestQueueProcessor scheduling processing\n");
132cdf0e10cSrcweir #endif
133cdf0e10cSrcweir         mnUserEventId = Application::PostUserEvent(
134cdf0e10cSrcweir             LINK(this,ChangeRequestQueueProcessor,ProcessEvent));
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
IMPL_LINK(ChangeRequestQueueProcessor,ProcessEvent,void *,pUnused)141cdf0e10cSrcweir IMPL_LINK(ChangeRequestQueueProcessor, ProcessEvent, void*, pUnused)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     (void)pUnused;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     mnUserEventId = 0;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     ProcessOneEvent();
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     if ( ! maQueue.empty())
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         // Schedule the processing of the next event.
154cdf0e10cSrcweir         StartProcessing();
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     return 0;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
ProcessOneEvent(void)163cdf0e10cSrcweir void ChangeRequestQueueProcessor::ProcessOneEvent (void)
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
166cdf0e10cSrcweir 
167cdf0e10cSrcweir #ifdef VERBOSE
168cdf0e10cSrcweir     OSL_TRACE("ProcessOneEvent\n");
169cdf0e10cSrcweir #endif
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     if (mxConfiguration.is()
172cdf0e10cSrcweir         && ! maQueue.empty())
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         // Get and remove the first entry from the queue.
175cdf0e10cSrcweir         Reference<XConfigurationChangeRequest> xRequest (maQueue.front());
176cdf0e10cSrcweir         maQueue.pop_front();
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         // Execute the change request.
179cdf0e10cSrcweir         if (xRequest.is())
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir #ifdef VERBOSE
182cdf0e10cSrcweir             TraceRequest(xRequest);
183cdf0e10cSrcweir #endif
184cdf0e10cSrcweir             xRequest->execute(mxConfiguration);
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         if (maQueue.empty())
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir #ifdef VERBOSE
190cdf0e10cSrcweir             OSL_TRACE("All requests are processed\n");
191cdf0e10cSrcweir #endif
192cdf0e10cSrcweir             // The queue is empty so tell the ConfigurationManager to update
193cdf0e10cSrcweir             // its state.
194cdf0e10cSrcweir             if (mpConfigurationUpdater.get() != NULL)
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir #ifdef VERBOSE
197cdf0e10cSrcweir                 ConfigurationTracer::TraceConfiguration (
198cdf0e10cSrcweir                     mxConfiguration, "updating to configuration");
199cdf0e10cSrcweir #endif
200cdf0e10cSrcweir                 mpConfigurationUpdater->RequestUpdate(mxConfiguration);
201cdf0e10cSrcweir             }
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 
IsEmpty(void) const209cdf0e10cSrcweir bool ChangeRequestQueueProcessor::IsEmpty (void) const
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     return maQueue.empty();
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 
ProcessUntilEmpty(void)217cdf0e10cSrcweir void ChangeRequestQueueProcessor::ProcessUntilEmpty (void)
218cdf0e10cSrcweir {
219cdf0e10cSrcweir     while ( ! IsEmpty())
220cdf0e10cSrcweir         ProcessOneEvent();
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
Clear(void)226cdf0e10cSrcweir void ChangeRequestQueueProcessor::Clear (void)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
229cdf0e10cSrcweir     maQueue.clear();
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 
233cdf0e10cSrcweir } } // end of namespace sd::framework::configuration
234