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_HXX
23 #define SD_SIDEBAR_PANELS_MASTER_PAGE_CONTAINER_HXX
24 
25 #include "MasterPageContainerProviders.hxx"
26 
27 #include <osl/mutex.hxx>
28 #include <tools/string.hxx>
29 #include <vcl/image.hxx>
30 #include <memory>
31 #include "PreviewRenderer.hxx"
32 #include <com/sun/star/frame/XModel.hpp>
33 #include <vcl/timer.hxx>
34 #include "tools/SdGlobalResourceContainer.hxx"
35 
36 #include <boost/shared_ptr.hpp>
37 
38 class SdPage;
39 class SdDrawDocument;
40 class SfxObjectShellLock;
41 
42 namespace sd {
43 class DrawDocShell;
44 }
45 
46 namespace sd { namespace sidebar {
47 
48 class MasterPageDescriptor;
49 
50 /** This container manages the master pages used by the MasterPagesSelector
51     controls.  It uses internally a singleton implementation object.
52     Therefore, all MasterPageContainer object operator on the same set of
53     master pages.  Each MasterPageContainer, however, has its own
54     PreviewSize value and thus can independantly switch between large and
55     small previews.
56 
57     The container maintains its own document to store master page objects.
58 
59     For each master page container stores its URL, preview bitmap, page
60     name, and, if available, the page object.
61 
62     Entries are accessed via a Token, which is mostly a numerical index but
63     whose values do not neccessarily have to be consecutive.
64 */
65 class MasterPageContainer
66 {
67 public:
68     typedef int Token;
69     static const Token NIL_TOKEN = -1;
70 
71     MasterPageContainer (void);
72     virtual ~MasterPageContainer (void);
73 
74     void AddChangeListener (const Link& rLink);
75     void RemoveChangeListener (const Link& rLink);
76 
77     enum PreviewSize { SMALL, LARGE };
78     /** There are two different preview sizes, a small one and a large one.
79         Which one is used by the called container can be changed with this
80         method.
81         When the preview size is changed then all change listeners are
82         notified of this.
83     */
84     void SetPreviewSize (PreviewSize eSize);
85 
86     /** Returns the preview size.
87     */
88     PreviewSize GetPreviewSize (void) const;
89 
90     /** Return the preview size in pixels.
91     */
92     Size GetPreviewSizePixel (void) const;
93 
94     enum PreviewState { PS_AVAILABLE, PS_CREATABLE, PS_PREPARING, PS_NOT_AVAILABLE };
95     PreviewState GetPreviewState (Token aToken);
96 
97     /** This method is typically called for entries in the container for
98         which GetPreviewState() returns OS_CREATABLE.  The creation of the
99         preview is then scheduled to be executed asynchronously at a later
100         point in time.  When the preview is available the change listeners
101         will be notified.
102     */
103     bool RequestPreview (Token aToken);
104 
105     /** Each entry of the container is either the first page of a template
106         document or is a master page of an Impress document.
107     */
108     enum Origin {
109         MASTERPAGE,  // Master page of a document.
110         TEMPLATE,    // First page of a template file.
111         DEFAULT,     // Empty master page with default style.
112         UNKNOWN
113     };
114 
115     /** Put the master page identified and described by the given parameters
116         into the container.  When there already is a master page with the
117         given URL, page name, or object pointer (when that is not NULL) then
118         the existing entry is replaced/updated by the given one.  Otherwise
119         a new entry is inserted.
120     */
121     Token PutMasterPage (const ::boost::shared_ptr<MasterPageDescriptor>& rDescriptor);
122     void AcquireToken (Token aToken);
123     void ReleaseToken (Token aToken);
124 
125     /** This and the GetTokenForIndex() methods can be used to iterate over
126         all members of the container.
127     */
128     int GetTokenCount (void) const;
129 
130     /** Determine whether the container has a member for the given token.
131     */
132     bool HasToken (Token aToken) const;
133 
134     /** Return a token for an index in the range
135         0 <= index < GetTokenCount().
136     */
137     Token GetTokenForIndex (int nIndex);
138 
139     Token GetTokenForURL (const String& sURL);
140     Token GetTokenForStyleName (const String& sStyleName);
141     Token GetTokenForPageObject (const SdPage* pPage);
142 
143     String GetURLForToken (Token aToken);
144     String GetPageNameForToken (Token aToken);
145     String GetStyleNameForToken (Token aToken);
146     SdPage* GetPageObjectForToken (Token aToken, bool bLoad=true);
147     Origin GetOriginForToken (Token aToken);
148     sal_Int32 GetTemplateIndexForToken (Token aToken);
149     ::boost::shared_ptr<MasterPageDescriptor> GetDescriptorForToken (Token aToken);
150 
151     void InvalidatePreview (Token aToken);
152 
153     /** Return a preview for the specified token.  When the preview is not
154         present then the PreviewProvider associated with the token is
155         executed only when that is not expensive.  It is the responsibility
156         of the caller to call RequestPreview() to do the same
157         (asynchronously) for expensive PreviewProviders.
158         Call GetPreviewState() to find out if that is necessary.
159         @param aToken
160             This token specifies for which master page to return the prview.
161             Tokens are returned for example by the GetTokenFor...() methods.
162         @return
163             The returned image is the requested preview or a substitution.
164     */
165     Image GetPreviewForToken (Token aToken);
166 
167 private:
168     class Implementation;
169     ::boost::shared_ptr<Implementation> mpImpl;
170     PreviewSize mePreviewSize;
171 
172     /** Retrieve the preview of the document specified by the given URL.
173     */
174     static BitmapEx LoadPreviewFromURL (const ::rtl::OUString& aURL);
175 };
176 
177 
178 
179 
180 /** For some changes to the set of master pages in a MasterPageContainer or
181     to the data stored for each master page one or more events are sent to
182     registered listeners.
183     Each event has an event type and a token that tells the listener where
184     the change took place.
185 */
186 class MasterPageContainerChangeEvent
187 {
188 public:
189     enum EventType {
190         // A master page was added to the container.
191         CHILD_ADDED,
192         // A master page was removed from the container.
193         CHILD_REMOVED,
194         // The preview of a master page has changed.
195         PREVIEW_CHANGED,
196         // The size of a preview has changed.
197         SIZE_CHANGED,
198         // Some of the data stored for a master page has changed.
199         DATA_CHANGED,
200         // The TemplateIndex of a master page has changed.
201         INDEX_CHANGED,
202         // More than one entries changed their TemplateIndex
203         INDEXES_CHANGED
204     } meEventType;
205 
206     // Token of the container entry whose data changed or which was added or
207     // removed.
208     MasterPageContainer::Token maChildToken;
209 };
210 
211 
212 } } // end of namespace sd::sidebar
213 
214 #endif
215