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 
23 
24 #ifndef SD_PAGE_CACHE_MANAGER_HXX
25 #define SD_PAGE_CACHE_MANAGER_HXX
26 
27 #include <sal/types.h>
28 #include <com/sun/star/uno/XInterface.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <memory>
31 #include <vector>
32 
33 class Size;
34 class SdDrawDocument;
35 class SdrPage;
36 
37 namespace sd { namespace slidesorter { namespace view {
38 class SlideSorterView;
39 class PageObjectViewObjectContact;
40 } } }
41 
42 namespace sd { namespace slidesorter { namespace model {
43 class SlideSorterModel;
44 } } }
45 
46 namespace sd { namespace slidesorter { namespace cache {
47 
48 namespace css = ::com::sun::star;
49 
50 class BitmapCache;
51 
52 
53 /** Provide and manage the preview bitmap caches for all slide sorter
54     instances.  There is one cache per active slide sorter plus a small
55     number of caches that are no longer in use.  The later are kept to speed
56     up the switching between views.
57 */
58 class PageCacheManager
59 {
60 public:
61     typedef BitmapCache Cache;
62     typedef ::std::vector< ::std::pair<Size, ::boost::shared_ptr<BitmapCache> > > BestFittingPageCaches;
63     typedef css::uno::Reference<css::uno::XInterface> DocumentKey;
64 
65     /** Return the one instance of the PageCacheManager class.
66     */
67     static ::boost::shared_ptr<PageCacheManager> Instance (void);
68 
69     /** Look up the cache for the given model in which the previews have the
70         specified size.  If no such cache exists, then one is created.  When
71         a new BitmapCache is created its Recycle() method is called with a
72         sorted list of existing caches from which the new one initialize its
73         previews.
74         @return
75             The returned cache lives as long as somebody keeps a shared
76             pointer and the ReleaseCache() method has not been called.
77     */
78     ::boost::shared_ptr<Cache> GetCache (
79         DocumentKey pDocument,
80         const Size& rPreviewSize);
81 
82     /** Tell the cache manager to release its own reference to the specified
83         cache.  After that the cache will live as long as the caller (and
84         maybe others) holds its reference.
85     */
86     void ReleaseCache (const ::boost::shared_ptr<Cache>& rpCache);
87 
88     /** This is an information to the cache manager that the size of preview
89         bitmaps in the specified cache has changed.
90 
91     */
92     ::boost::shared_ptr<Cache> ChangeSize (
93         const ::boost::shared_ptr<Cache>& rpCache,
94         const Size& rOldPreviewSize,
95         const Size& rNewPreviewSize);
96 
97     /** Invalidate the preview bitmap for one slide that belongs to the
98         specified document.  The bitmaps for this slide in all caches are
99         marked as out-of-date and will be re-created when they are requested
100         the next time.
101     */
102     bool InvalidatePreviewBitmap (
103         DocumentKey pDocument,
104         const SdrPage* pPage);
105 
106     /** Invalidate the preview bitmaps for all slides that belong to the
107         specified document.  This is necessary after model changes that
108         affect e.g. page number fiels.
109     */
110     void InvalidateAllPreviewBitmaps (DocumentKey pDocument);
111 
112     /** Invalidate all the caches that are currently in use and destroy
113         those that are not.  This is used for example when the high contrast
114         mode is turned on or off.
115     */
116     void InvalidateAllCaches (void);
117 
118     /** Call this method when a page has been deleted and its preview
119         is not needed anymore.
120     */
121     void ReleasePreviewBitmap (const SdrPage* pPage);
122 
123 private:
124     /** Singleton instance of the cache manager.  Note that this is a weak
125         pointer.  The (implementation class of) ViewShellBase holds a
126         shared_ptr so that the cache manager has the same life time as the
127         ViewShellBase.
128     */
129     static ::boost::weak_ptr<PageCacheManager> mpInstance;
130 
131     /// List of active caches.
132     class PageCacheContainer;
133     ::std::auto_ptr<PageCacheContainer> mpPageCaches;
134 
135     /// List of inactive, recently used caches.
136     class RecentlyUsedPageCaches;
137     ::std::auto_ptr<RecentlyUsedPageCaches> mpRecentlyUsedPageCaches;
138 
139     /** The maximal number of recently used caches that are kept alive after
140         they have become inactive, i.e. after they are not used anymore by a
141         slide sorter.
142     */
143     const sal_uInt32 mnMaximalRecentlyCacheCount;
144 
145     PageCacheManager (void);
146     ~PageCacheManager (void);
147 
148 	class Deleter;
149 	friend class Deleter;
150 
151     ::boost::shared_ptr<Cache> GetRecentlyUsedCache(
152         DocumentKey pDocument,
153         const Size& rSize);
154 
155     /** Add the given cache to the list of recently used caches for the
156         document.  There is one such list per document.  Each least has at
157         most mnMaximalRecentlyCacheCount members.
158     */
159     void PutRecentlyUsedCache(
160         DocumentKey pDocument,
161         const Size& rPreviewSize,
162         const ::boost::shared_ptr<Cache>& rpCache);
163 
164     /** Return a sorted list of the available caches, both active caches and
165         those recently used, for the given document.  The sort order is so
166         that an exact match of the preview size is at the front.  Other
167         caches follow with the largest size first.
168     */
169     BestFittingPageCaches GetBestFittingCaches (
170         DocumentKey pDocument,
171         const Size& rPreviewSize);
172 
173     /** This method is used internally to initialize a newly created
174         BitmapCache with already exisiting previews.
175     */
176     void Recycle (
177         const ::boost::shared_ptr<Cache>& rpCache,
178         DocumentKey pDocument,
179         const Size& rPreviewSize);
180 };
181 
182 } } } // end of namespace ::sd::slidesorter::cache
183 
184 #endif
185