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