1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "debugtrace.hxx"
21 #include "ChangeRequestQueueProcessor.hxx"
22 #include "ConfigurationTracer.hxx"
23 
24 #include "ConfigurationUpdater.hxx"
25 
26 #include <vcl/svapp.hxx>
27 #include <sal/log.hxx>
28 #include <com/sun/star/container/XNamed.hpp>
29 #include <com/sun/star/drawing/framework/XConfigurationChangeRequest.hpp>
30 
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::drawing::framework;
34 
35 namespace {
36 
37 #if DEBUG_SD_CONFIGURATION_TRACE
38 
TraceRequest(const Reference<XConfigurationChangeRequest> & rxRequest)39 void TraceRequest (const Reference<XConfigurationChangeRequest>& rxRequest)
40 {
41     Reference<container::XNamed> xNamed (rxRequest, UNO_QUERY);
42     if (xNamed.is())
43         SAL_INFO("sd.fwk", __func__ << ":    " << xNamed->getName());
44 }
45 
46 #endif
47 
48 } // end of anonymous namespace
49 
50 namespace sd::framework {
51 
ChangeRequestQueueProcessor(const std::shared_ptr<ConfigurationUpdater> & rpConfigurationUpdater)52 ChangeRequestQueueProcessor::ChangeRequestQueueProcessor (
53     const std::shared_ptr<ConfigurationUpdater>& rpConfigurationUpdater)
54     : maMutex(),
55       maQueue(),
56       mnUserEventId(nullptr),
57       mxConfiguration(),
58       mpConfigurationUpdater(rpConfigurationUpdater)
59 {
60 }
61 
~ChangeRequestQueueProcessor()62 ChangeRequestQueueProcessor::~ChangeRequestQueueProcessor()
63 {
64     if (mnUserEventId != nullptr)
65         Application::RemoveUserEvent(mnUserEventId);
66 }
67 
SetConfiguration(const Reference<XConfiguration> & rxConfiguration)68 void ChangeRequestQueueProcessor::SetConfiguration (
69     const Reference<XConfiguration>& rxConfiguration)
70 {
71     ::osl::MutexGuard aGuard (maMutex);
72 
73     mxConfiguration = rxConfiguration;
74     StartProcessing();
75 }
76 
AddRequest(const Reference<XConfigurationChangeRequest> & rxRequest)77 void ChangeRequestQueueProcessor::AddRequest (
78     const Reference<XConfigurationChangeRequest>& rxRequest)
79 {
80     ::osl::MutexGuard aGuard (maMutex);
81 
82 #if DEBUG_SD_CONFIGURATION_TRACE
83     if (maQueue.empty())
84     {
85         SAL_INFO("sd.fwk", __func__ << ": Adding requests to empty queue");
86         ConfigurationTracer::TraceConfiguration(
87             mxConfiguration, "current configuration of queue processor");
88     }
89     SAL_INFO("sd.fwk", __func__ << ": Adding request");
90     TraceRequest(rxRequest);
91 #endif
92 
93     maQueue.push(rxRequest);
94     StartProcessing();
95 }
96 
StartProcessing()97 void ChangeRequestQueueProcessor::StartProcessing()
98 {
99     ::osl::MutexGuard aGuard (maMutex);
100 
101     if (mnUserEventId == nullptr
102         && mxConfiguration.is()
103         && ! maQueue.empty())
104     {
105         SAL_INFO("sd.fwk", __func__ << ": ChangeRequestQueueProcessor scheduling processing");
106         mnUserEventId = Application::PostUserEvent(
107             LINK(this,ChangeRequestQueueProcessor,ProcessEvent));
108     }
109 }
110 
IMPL_LINK_NOARG(ChangeRequestQueueProcessor,ProcessEvent,void *,void)111 IMPL_LINK_NOARG(ChangeRequestQueueProcessor, ProcessEvent, void*, void)
112 {
113     ::osl::MutexGuard aGuard (maMutex);
114 
115     mnUserEventId = nullptr;
116 
117     ProcessOneEvent();
118 
119     if ( ! maQueue.empty())
120     {
121         // Schedule the processing of the next event.
122         StartProcessing();
123     }
124 }
125 
ProcessOneEvent()126 void ChangeRequestQueueProcessor::ProcessOneEvent()
127 {
128     ::osl::MutexGuard aGuard (maMutex);
129 
130     SAL_INFO("sd.fwk", __func__ << ": ProcessOneEvent");
131 
132     if (!mxConfiguration.is() || maQueue.empty())
133         return;
134 
135     // Get and remove the first entry from the queue.
136     Reference<XConfigurationChangeRequest> xRequest (maQueue.front());
137     maQueue.pop();
138 
139     // Execute the change request.
140     if (xRequest.is())
141     {
142 #if DEBUG_SD_CONFIGURATION_TRACE
143         TraceRequest(xRequest);
144 #endif
145         xRequest->execute(mxConfiguration);
146     }
147 
148     if (!maQueue.empty())
149         return;
150 
151     SAL_INFO("sd.fwk", __func__ << ": All requests are processed");
152     // The queue is empty so tell the ConfigurationManager to update
153     // its state.
154     if (mpConfigurationUpdater != nullptr)
155     {
156 #if DEBUG_SD_CONFIGURATION_TRACE
157         ConfigurationTracer::TraceConfiguration (
158             mxConfiguration, "updating to configuration");
159 #endif
160         mpConfigurationUpdater->RequestUpdate(mxConfiguration);
161     }
162 }
163 
IsEmpty() const164 bool ChangeRequestQueueProcessor::IsEmpty() const
165 {
166     return maQueue.empty();
167 }
168 
ProcessUntilEmpty()169 void ChangeRequestQueueProcessor::ProcessUntilEmpty()
170 {
171     while ( ! IsEmpty())
172         ProcessOneEvent();
173 }
174 
Clear()175 void ChangeRequestQueueProcessor::Clear()
176 {
177     ::osl::MutexGuard aGuard (maMutex);
178     ChangeRequestQueue().swap(maQueue);
179 }
180 
181 } // end of namespace sd::framework
182 
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
184