Line data Source code
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 "MasterPageContainerQueue.hxx"
21 :
22 : #include "tools/IdleDetection.hxx"
23 :
24 : #include <set>
25 :
26 : namespace sd { namespace sidebar {
27 :
28 : const sal_Int32 MasterPageContainerQueue::snDelayedCreationTimeout (15);
29 : const sal_Int32 MasterPageContainerQueue::snDelayedCreationTimeoutWhenNotIdle (100);
30 : const sal_Int32 MasterPageContainerQueue::snMasterPagePriorityBoost (5);
31 : const sal_Int32 MasterPageContainerQueue::snWaitForMoreRequestsPriorityThreshold (-10);
32 : sal_uInt32 MasterPageContainerQueue::snWaitForMoreRequestsCount(15);
33 :
34 : //===== MasterPageContainerQueue::PreviewCreationRequest ======================
35 :
36 0 : class MasterPageContainerQueue::PreviewCreationRequest
37 : {
38 : public:
39 0 : PreviewCreationRequest (const SharedMasterPageDescriptor& rpDescriptor, int nPriority)
40 : : mpDescriptor(rpDescriptor),
41 0 : mnPriority(nPriority)
42 0 : {}
43 : SharedMasterPageDescriptor mpDescriptor;
44 : int mnPriority;
45 : class Compare
46 : {
47 : public:
48 0 : bool operator() (const PreviewCreationRequest& r1,const PreviewCreationRequest& r2) const
49 : {
50 0 : if (r1.mnPriority != r2.mnPriority)
51 : {
52 : // Prefer requests with higher priority.
53 0 : return r1.mnPriority > r2.mnPriority;
54 : }
55 : else
56 : {
57 : // Prefer tokens that have been earlier created (those with lower
58 : // value).
59 0 : return r1.mpDescriptor->maToken < r2.mpDescriptor->maToken;
60 : }
61 : }
62 : };
63 : class CompareToken
64 : {
65 : public:
66 : MasterPageContainer::Token maToken;
67 0 : CompareToken(MasterPageContainer::Token aToken) : maToken(aToken) {}
68 0 : bool operator() (const PreviewCreationRequest& rRequest) const
69 0 : { return maToken==rRequest.mpDescriptor->maToken; }
70 : };
71 : };
72 :
73 :
74 :
75 :
76 : //===== MasterPageContainerQueue::RequestQueue ================================
77 :
78 0 : class MasterPageContainerQueue::RequestQueue
79 : : public ::std::set<PreviewCreationRequest,PreviewCreationRequest::Compare>
80 : {
81 : public:
82 0 : RequestQueue (void) {}
83 : };
84 :
85 :
86 :
87 :
88 : //===== MasterPageContainerQueue ==============================================
89 :
90 0 : MasterPageContainerQueue* MasterPageContainerQueue::Create (
91 : const ::boost::weak_ptr<ContainerAdapter>& rpContainer)
92 : {
93 0 : MasterPageContainerQueue* pQueue = new MasterPageContainerQueue(rpContainer);
94 0 : pQueue->LateInit();
95 0 : return pQueue;
96 : }
97 :
98 :
99 :
100 :
101 0 : MasterPageContainerQueue::MasterPageContainerQueue (
102 : const ::boost::weak_ptr<ContainerAdapter>& rpContainer)
103 : : mpWeakContainer(rpContainer),
104 0 : mpRequestQueue(new RequestQueue()),
105 : maDelayedPreviewCreationTimer(),
106 0 : mnRequestsServedCount(0)
107 : {
108 0 : }
109 :
110 :
111 :
112 :
113 0 : MasterPageContainerQueue::~MasterPageContainerQueue (void)
114 : {
115 0 : maDelayedPreviewCreationTimer.Stop();
116 0 : while ( ! mpRequestQueue->empty())
117 0 : mpRequestQueue->erase(mpRequestQueue->begin());
118 0 : }
119 :
120 :
121 :
122 :
123 0 : void MasterPageContainerQueue::LateInit (void)
124 : {
125 : // Set up the timer for the delayed creation of preview bitmaps.
126 0 : maDelayedPreviewCreationTimer.SetTimeout (snDelayedCreationTimeout);
127 0 : Link aLink (LINK(this,MasterPageContainerQueue,DelayedPreviewCreation));
128 0 : maDelayedPreviewCreationTimer.SetTimeoutHdl(aLink);
129 0 : }
130 :
131 :
132 :
133 :
134 0 : bool MasterPageContainerQueue::RequestPreview (const SharedMasterPageDescriptor& rpDescriptor)
135 : {
136 0 : bool bSuccess (false);
137 0 : if (rpDescriptor.get() != NULL
138 0 : && rpDescriptor->maLargePreview.GetSizePixel().Width() == 0)
139 : {
140 0 : sal_Int32 nPriority (CalculatePriority(rpDescriptor));
141 :
142 : // Add a new or replace an existing request.
143 : RequestQueue::iterator iRequest (::std::find_if(
144 0 : mpRequestQueue->begin(),
145 0 : mpRequestQueue->end(),
146 0 : PreviewCreationRequest::CompareToken(rpDescriptor->maToken)));
147 : // When a request for the same token exists then the lowest of the
148 : // two priorities is used.
149 0 : if (iRequest != mpRequestQueue->end())
150 0 : if (iRequest->mnPriority < nPriority)
151 : {
152 0 : mpRequestQueue->erase(iRequest);
153 0 : iRequest = mpRequestQueue->end();
154 : }
155 :
156 : // Add a new request when none exists (or has just been erased).
157 0 : if (iRequest == mpRequestQueue->end())
158 : {
159 0 : mpRequestQueue->insert(PreviewCreationRequest(rpDescriptor,nPriority));
160 0 : maDelayedPreviewCreationTimer.Start();
161 0 : bSuccess = true;
162 : }
163 : }
164 0 : return bSuccess;
165 : }
166 :
167 :
168 :
169 :
170 0 : sal_Int32 MasterPageContainerQueue::CalculatePriority (
171 : const SharedMasterPageDescriptor& rpDescriptor) const
172 : {
173 : sal_Int32 nPriority;
174 :
175 : // The cost is used as a starting value.
176 0 : int nCost (0);
177 0 : if (rpDescriptor->mpPreviewProvider.get() != NULL)
178 : {
179 0 : nCost = rpDescriptor->mpPreviewProvider->GetCostIndex();
180 0 : if (rpDescriptor->mpPreviewProvider->NeedsPageObject())
181 0 : if (rpDescriptor->mpPageObjectProvider.get() != NULL)
182 0 : nCost += rpDescriptor->mpPageObjectProvider->GetCostIndex();
183 : }
184 :
185 : // Its negative value is used so that requests with a low cost are
186 : // preferred over those with high costs.
187 0 : nPriority = -nCost;
188 :
189 : // Add a term that introduces an order based on the appearance in the
190 : // AllMasterPagesSelector.
191 0 : nPriority -= rpDescriptor->maToken / 3;
192 :
193 : // Process requests for the CurrentMasterPagesSelector first.
194 0 : if (rpDescriptor->meOrigin == MasterPageContainer::MASTERPAGE)
195 0 : nPriority += snMasterPagePriorityBoost;
196 :
197 0 : return nPriority;
198 : }
199 :
200 :
201 :
202 :
203 0 : IMPL_LINK(MasterPageContainerQueue, DelayedPreviewCreation, Timer*, pTimer)
204 : {
205 0 : bool bIsShowingFullScreenShow (false);
206 0 : bool bWaitForMoreRequests (false);
207 :
208 : do
209 : {
210 0 : if (mpRequestQueue->empty())
211 0 : break;
212 :
213 : // First check whether the system is idle.
214 0 : sal_Int32 nIdleState (tools::IdleDetection::GetIdleState());
215 0 : if (nIdleState != tools::IdleDetection::IDET_IDLE)
216 : {
217 0 : if ((nIdleState&tools::IdleDetection::IDET_FULL_SCREEN_SHOW_ACTIVE) != 0)
218 0 : bIsShowingFullScreenShow = true;
219 0 : break;
220 : }
221 :
222 0 : PreviewCreationRequest aRequest (*mpRequestQueue->begin());
223 :
224 : // Check if the request should really be processed right now.
225 : // Reasons to not do it are when its cost is high and not many other
226 : // requests have been inserted into the queue that would otherwise
227 : // be processed first.
228 0 : if (aRequest.mnPriority < snWaitForMoreRequestsPriorityThreshold
229 0 : && (mnRequestsServedCount+mpRequestQueue->size() < snWaitForMoreRequestsCount))
230 : {
231 : // Wait for more requests before this one is processed. Note
232 : // that the queue processing is not started anew when this
233 : // method is left. That is done when the next request is
234 : // inserted.
235 0 : bWaitForMoreRequests = true;
236 0 : break;
237 : }
238 :
239 0 : mpRequestQueue->erase(mpRequestQueue->begin());
240 :
241 0 : if (aRequest.mpDescriptor.get() != NULL)
242 : {
243 0 : mnRequestsServedCount += 1;
244 0 : if ( ! mpWeakContainer.expired())
245 : {
246 0 : ::boost::shared_ptr<ContainerAdapter> pContainer (mpWeakContainer);
247 0 : if (pContainer.get() != NULL)
248 0 : pContainer->UpdateDescriptor(aRequest.mpDescriptor,false,true,true);
249 : }
250 0 : }
251 : }
252 : while (false);
253 :
254 0 : if (mpRequestQueue->size() > 0 && ! bWaitForMoreRequests)
255 : {
256 0 : int nTimeout (snDelayedCreationTimeout);
257 0 : if (bIsShowingFullScreenShow)
258 0 : nTimeout = snDelayedCreationTimeoutWhenNotIdle;
259 0 : maDelayedPreviewCreationTimer.SetTimeout(nTimeout);
260 0 : pTimer->Start();
261 : }
262 :
263 0 : return 0;
264 : }
265 :
266 :
267 :
268 :
269 0 : bool MasterPageContainerQueue::HasRequest (MasterPageContainer::Token aToken) const
270 : {
271 : RequestQueue::iterator iRequest (::std::find_if(
272 0 : mpRequestQueue->begin(),
273 0 : mpRequestQueue->end(),
274 0 : PreviewCreationRequest::CompareToken(aToken)));
275 0 : return (iRequest != mpRequestQueue->end());
276 : }
277 :
278 :
279 :
280 :
281 0 : bool MasterPageContainerQueue::IsEmpty (void) const
282 : {
283 0 : return mpRequestQueue->empty();
284 : }
285 :
286 :
287 :
288 :
289 0 : void MasterPageContainerQueue::ProcessAllRequests (void)
290 : {
291 0 : snWaitForMoreRequestsCount = 0;
292 0 : if (mpRequestQueue->size() > 0)
293 0 : maDelayedPreviewCreationTimer.Start();
294 0 : }
295 :
296 :
297 : } } // end of namespace sd::sidebar
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|