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 : //===== MasterPageContainerQueue::RequestQueue ================================
74 :
75 0 : class MasterPageContainerQueue::RequestQueue
76 : : public ::std::set<PreviewCreationRequest,PreviewCreationRequest::Compare>
77 : {
78 : public:
79 0 : RequestQueue() {}
80 : };
81 :
82 : //===== MasterPageContainerQueue ==============================================
83 :
84 0 : MasterPageContainerQueue* MasterPageContainerQueue::Create (
85 : const ::boost::weak_ptr<ContainerAdapter>& rpContainer)
86 : {
87 0 : MasterPageContainerQueue* pQueue = new MasterPageContainerQueue(rpContainer);
88 0 : pQueue->LateInit();
89 0 : return pQueue;
90 : }
91 :
92 0 : MasterPageContainerQueue::MasterPageContainerQueue (
93 : const ::boost::weak_ptr<ContainerAdapter>& rpContainer)
94 : : mpWeakContainer(rpContainer),
95 0 : mpRequestQueue(new RequestQueue()),
96 : maDelayedPreviewCreationTimer(),
97 0 : mnRequestsServedCount(0)
98 : {
99 0 : }
100 :
101 0 : MasterPageContainerQueue::~MasterPageContainerQueue()
102 : {
103 0 : maDelayedPreviewCreationTimer.Stop();
104 0 : while ( ! mpRequestQueue->empty())
105 0 : mpRequestQueue->erase(mpRequestQueue->begin());
106 0 : }
107 :
108 0 : void MasterPageContainerQueue::LateInit()
109 : {
110 : // Set up the timer for the delayed creation of preview bitmaps.
111 0 : maDelayedPreviewCreationTimer.SetTimeout (snDelayedCreationTimeout);
112 0 : Link<Timer *, void> aLink (LINK(this,MasterPageContainerQueue,DelayedPreviewCreation));
113 0 : maDelayedPreviewCreationTimer.SetTimeoutHdl(aLink);
114 0 : }
115 :
116 0 : bool MasterPageContainerQueue::RequestPreview (const SharedMasterPageDescriptor& rpDescriptor)
117 : {
118 0 : bool bSuccess (false);
119 0 : if (rpDescriptor.get() != NULL
120 0 : && rpDescriptor->maLargePreview.GetSizePixel().Width() == 0)
121 : {
122 0 : sal_Int32 nPriority (CalculatePriority(rpDescriptor));
123 :
124 : // Add a new or replace an existing request.
125 : RequestQueue::iterator iRequest (::std::find_if(
126 0 : mpRequestQueue->begin(),
127 0 : mpRequestQueue->end(),
128 0 : PreviewCreationRequest::CompareToken(rpDescriptor->maToken)));
129 : // When a request for the same token exists then the lowest of the
130 : // two priorities is used.
131 0 : if (iRequest != mpRequestQueue->end())
132 0 : if (iRequest->mnPriority < nPriority)
133 : {
134 0 : mpRequestQueue->erase(iRequest);
135 0 : iRequest = mpRequestQueue->end();
136 : }
137 :
138 : // Add a new request when none exists (or has just been erased).
139 0 : if (iRequest == mpRequestQueue->end())
140 : {
141 0 : mpRequestQueue->insert(PreviewCreationRequest(rpDescriptor,nPriority));
142 0 : maDelayedPreviewCreationTimer.Start();
143 0 : bSuccess = true;
144 : }
145 : }
146 0 : return bSuccess;
147 : }
148 :
149 0 : sal_Int32 MasterPageContainerQueue::CalculatePriority (
150 : const SharedMasterPageDescriptor& rpDescriptor)
151 : {
152 : sal_Int32 nPriority;
153 :
154 : // The cost is used as a starting value.
155 0 : int nCost (0);
156 0 : if (rpDescriptor->mpPreviewProvider.get() != NULL)
157 : {
158 0 : nCost = rpDescriptor->mpPreviewProvider->GetCostIndex();
159 0 : if (rpDescriptor->mpPreviewProvider->NeedsPageObject())
160 0 : if (rpDescriptor->mpPageObjectProvider.get() != NULL)
161 0 : nCost += rpDescriptor->mpPageObjectProvider->GetCostIndex();
162 : }
163 :
164 : // Its negative value is used so that requests with a low cost are
165 : // preferred over those with high costs.
166 0 : nPriority = -nCost;
167 :
168 : // Add a term that introduces an order based on the appearance in the
169 : // AllMasterPagesSelector.
170 0 : nPriority -= rpDescriptor->maToken / 3;
171 :
172 : // Process requests for the CurrentMasterPagesSelector first.
173 0 : if (rpDescriptor->meOrigin == MasterPageContainer::MASTERPAGE)
174 0 : nPriority += snMasterPagePriorityBoost;
175 :
176 0 : return nPriority;
177 : }
178 :
179 0 : IMPL_LINK_TYPED(MasterPageContainerQueue, DelayedPreviewCreation, Timer*, pTimer, void)
180 : {
181 0 : bool bIsShowingFullScreenShow (false);
182 0 : bool bWaitForMoreRequests (false);
183 :
184 : do
185 : {
186 0 : if (mpRequestQueue->empty())
187 0 : break;
188 :
189 : // First check whether the system is idle.
190 0 : sal_Int32 nIdleState (tools::IdleDetection::GetIdleState());
191 0 : if (nIdleState != tools::IdleDetection::IDET_IDLE)
192 : {
193 0 : if ((nIdleState&tools::IdleDetection::IDET_FULL_SCREEN_SHOW_ACTIVE) != 0)
194 0 : bIsShowingFullScreenShow = true;
195 0 : break;
196 : }
197 :
198 0 : PreviewCreationRequest aRequest (*mpRequestQueue->begin());
199 :
200 : // Check if the request should really be processed right now.
201 : // Reasons to not do it are when its cost is high and not many other
202 : // requests have been inserted into the queue that would otherwise
203 : // be processed first.
204 0 : if (aRequest.mnPriority < snWaitForMoreRequestsPriorityThreshold
205 0 : && (mnRequestsServedCount+mpRequestQueue->size() < snWaitForMoreRequestsCount))
206 : {
207 : // Wait for more requests before this one is processed. Note
208 : // that the queue processing is not started anew when this
209 : // method is left. That is done when the next request is
210 : // inserted.
211 0 : bWaitForMoreRequests = true;
212 0 : break;
213 : }
214 :
215 0 : mpRequestQueue->erase(mpRequestQueue->begin());
216 :
217 0 : if (aRequest.mpDescriptor.get() != NULL)
218 : {
219 0 : mnRequestsServedCount += 1;
220 0 : if ( ! mpWeakContainer.expired())
221 : {
222 0 : ::boost::shared_ptr<ContainerAdapter> pContainer (mpWeakContainer);
223 0 : if (pContainer.get() != NULL)
224 0 : pContainer->UpdateDescriptor(aRequest.mpDescriptor,false,true,true);
225 : }
226 0 : }
227 : }
228 : while (false);
229 :
230 0 : if (mpRequestQueue->size() > 0 && ! bWaitForMoreRequests)
231 : {
232 0 : int nTimeout (snDelayedCreationTimeout);
233 0 : if (bIsShowingFullScreenShow)
234 0 : nTimeout = snDelayedCreationTimeoutWhenNotIdle;
235 0 : maDelayedPreviewCreationTimer.SetTimeout(nTimeout);
236 0 : pTimer->Start();
237 : }
238 0 : }
239 :
240 0 : bool MasterPageContainerQueue::HasRequest (MasterPageContainer::Token aToken) const
241 : {
242 : RequestQueue::iterator iRequest (::std::find_if(
243 0 : mpRequestQueue->begin(),
244 0 : mpRequestQueue->end(),
245 0 : PreviewCreationRequest::CompareToken(aToken)));
246 0 : return (iRequest != mpRequestQueue->end());
247 : }
248 :
249 0 : bool MasterPageContainerQueue::IsEmpty() const
250 : {
251 0 : return mpRequestQueue->empty();
252 : }
253 :
254 0 : void MasterPageContainerQueue::ProcessAllRequests()
255 : {
256 0 : snWaitForMoreRequestsCount = 0;
257 0 : if (mpRequestQueue->size() > 0)
258 0 : maDelayedPreviewCreationTimer.Start();
259 0 : }
260 :
261 66 : } } // end of namespace sd::sidebar
262 :
263 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|