1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SD_TOOLPANEL_CONTROLS_MASTER_PAGE_CONTAINER_QUEUE_HXX
29 #define SD_TOOLPANEL_CONTROLS_MASTER_PAGE_CONTAINER_QUEUE_HXX
30 
31 #include "MasterPageContainer.hxx"
32 #include "MasterPageDescriptor.hxx"
33 
34 #include <boost/scoped_ptr.hpp>
35 #include <boost/weak_ptr.hpp>
36 
37 namespace sd { namespace toolpanel { namespace controls {
38 
39 
40 /** The queue stores and processes all requests from a MasterPageContainer
41     for the creation of previews.
42     The order of request processing and its timing is controlled by a
43     heuristic that uses values given with each request and which is
44     controlled by various parameters that are described below.
45 */
46 class MasterPageContainerQueue
47 {
48 public:
49     class ContainerAdapter { public:
50         virtual bool UpdateDescriptor (
51             const SharedMasterPageDescriptor& rpDescriptor,
52             bool bForcePageObject,
53             bool bForcePreview,
54             bool bSendEvents) = 0;
55     };
56 
57     static MasterPageContainerQueue* Create (
58         const ::boost::weak_ptr<ContainerAdapter>& rpContainer);
59     virtual ~MasterPageContainerQueue (void);
60 
61     /** This method is typically called for entries in the container for
62         which GetPreviewState() returns OS_CREATABLE.  The creation of the
63         preview is then scheduled to be executed asynchronously at a later
64         point in time.  When the preview is available the change listeners
65         will be notified.
66     */
67     bool RequestPreview (const SharedMasterPageDescriptor& rDescriptor);
68 
69     /** Return <TRUE/> when there is a request currently in the queue for
70         the given token.
71     */
72     bool HasRequest (MasterPageContainer::Token aToken) const;
73 
74     /** Return <TRUE/> when there is at least one request in the queue.
75     */
76     bool IsEmpty (void) const;
77 
78     /** After this call the queue does not wait anymore for requests with
79         higher priority when only a small number of requests with lower
80         priority are present.  This method should be called when all
81         templates are inserted into the MasterPageContainer.
82     */
83     void ProcessAllRequests (void);
84 
85 private:
86     ::boost::weak_ptr<ContainerAdapter> mpWeakContainer;
87     class PreviewCreationRequest;
88     class RequestQueue;
89     ::boost::scoped_ptr<RequestQueue> mpRequestQueue;
90     Timer maDelayedPreviewCreationTimer;
91     sal_uInt32 mnRequestsServedCount;
92 
93     // There are a couple of values that define various aspects of the
94     // heuristic that defines the order and timing in which requests for
95     // preview creation are processed.
96 
97     /** The time to wait (in milliseconds) between the creation of previews.
98     */
99     static const sal_Int32 snDelayedCreationTimeout;
100 
101     /** The time to wait when the system is not idle.
102     */
103     static const sal_Int32 snDelayedCreationTimeoutWhenNotIdle;
104 
105     /** Requests for previews of master pages in a document have their
106         priority increased by this value.
107     */
108     static const sal_Int32 snMasterPagePriorityBoost;
109 
110     /** When only requests which a priority lower than this threshold exist
111         and not many requests have been made yet then wait with processing
112         them until more requests are present.
113     */
114     static const sal_Int32 snWaitForMoreRequestsPriorityThreshold;
115 
116     /** When only requests which a priority lower than a threshold exist
117         and not more requests than this number have been made or already
118         processed then wait with processing them until more requests are
119         present.
120     */
121     static sal_uInt32 snWaitForMoreRequestsCount;
122 
123     MasterPageContainerQueue (const ::boost::weak_ptr<ContainerAdapter>& rpContainer);
124     void LateInit (void);
125 
126     /** Calculate the priority that defines the order in which requests
127         are processed.
128     */
129     sal_Int32 CalculatePriority (const SharedMasterPageDescriptor& rDescriptor) const;
130 
131     DECL_LINK(DelayedPreviewCreation, Timer *);
132 };
133 
134 } } } // end of namespace ::sd::toolpanel::controls
135 
136 #endif
137