1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef SD_SIDEBAR_PANELS_MASTER_PAGE_CONTAINER_QUEUE_HXX
23 #define SD_SIDEBAR_PANELS_MASTER_PAGE_CONTAINER_QUEUE_HXX
24 
25 #include "MasterPageContainer.hxx"
26 #include "MasterPageDescriptor.hxx"
27 
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/weak_ptr.hpp>
30 
31 namespace sd { namespace sidebar {
32 
33 
34 /** The queue stores and processes all requests from a MasterPageContainer
35     for the creation of previews.
36     The order of request processing and its timing is controlled by a
37     heuristic that uses values given with each request and which is
38     controlled by various parameters that are described below.
39 */
40 class MasterPageContainerQueue
41 {
42 public:
43     class ContainerAdapter { public:
44         virtual bool UpdateDescriptor (
45             const SharedMasterPageDescriptor& rpDescriptor,
46             bool bForcePageObject,
47             bool bForcePreview,
48             bool bSendEvents) = 0;
49     };
50 
51     static MasterPageContainerQueue* Create (
52         const ::boost::weak_ptr<ContainerAdapter>& rpContainer);
53     virtual ~MasterPageContainerQueue (void);
54 
55     /** This method is typically called for entries in the container for
56         which GetPreviewState() returns OS_CREATABLE.  The creation of the
57         preview is then scheduled to be executed asynchronously at a later
58         point in time.  When the preview is available the change listeners
59         will be notified.
60     */
61     bool RequestPreview (const SharedMasterPageDescriptor& rDescriptor);
62 
63     /** Return <TRUE/> when there is a request currently in the queue for
64         the given token.
65     */
66     bool HasRequest (MasterPageContainer::Token aToken) const;
67 
68     /** Return <TRUE/> when there is at least one request in the queue.
69     */
70     bool IsEmpty (void) const;
71 
72     /** After this call the queue does not wait anymore for requests with
73         higher priority when only a small number of requests with lower
74         priority are present.  This method should be called when all
75         templates are inserted into the MasterPageContainer.
76     */
77     void ProcessAllRequests (void);
78 
79 private:
80     ::boost::weak_ptr<ContainerAdapter> mpWeakContainer;
81     class PreviewCreationRequest;
82     class RequestQueue;
83     ::boost::scoped_ptr<RequestQueue> mpRequestQueue;
84     Timer maDelayedPreviewCreationTimer;
85     sal_uInt32 mnRequestsServedCount;
86 
87     // There are a couple of values that define various aspects of the
88     // heuristic that defines the order and timing in which requests for
89     // preview creation are processed.
90 
91     /** The time to wait (in milliseconds) between the creation of previews.
92     */
93     static const sal_Int32 snDelayedCreationTimeout;
94 
95     /** The time to wait when the system is not idle.
96     */
97     static const sal_Int32 snDelayedCreationTimeoutWhenNotIdle;
98 
99     /** Requests for previews of master pages in a document have their
100         priority increased by this value.
101     */
102     static const sal_Int32 snMasterPagePriorityBoost;
103 
104     /** When only requests which a priority lower than this threshold exist
105         and not many requests have been made yet then wait with processing
106         them until more requests are present.
107     */
108     static const sal_Int32 snWaitForMoreRequestsPriorityThreshold;
109 
110     /** When only requests which a priority lower than a threshold exist
111         and not more requests than this number have been made or already
112         processed then wait with processing them until more requests are
113         present.
114     */
115     static sal_uInt32 snWaitForMoreRequestsCount;
116 
117     MasterPageContainerQueue (const ::boost::weak_ptr<ContainerAdapter>& rpContainer);
118     void LateInit (void);
119 
120     /** Calculate the priority that defines the order in which requests
121         are processed.
122     */
123     sal_Int32 CalculatePriority (const SharedMasterPageDescriptor& rDescriptor) const;
124 
125     DECL_LINK(DelayedPreviewCreation, Timer *);
126 };
127 
128 } } // end of namespace sd::sidebar
129 
130 #endif
131