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 #include "precompiled_sd.hxx"
23 
24 #include "MasterPageDescriptor.hxx"
25 
26 #include "DocumentHelper.hxx"
27 #include "sdpage.hxx"
28 #include <tools/urlobj.hxx>
29 
30 namespace sd { namespace sidebar {
31 
32 
33 //===== MasterPageDescriptor ==================================================
34 
MasterPageDescriptor(MasterPageContainer::Origin eOrigin,const sal_Int32 nTemplateIndex,const String & rsURL,const String & rsPageName,const String & rsStyleName,const bool bIsPrecious,const::boost::shared_ptr<PageObjectProvider> & rpPageObjectProvider,const::boost::shared_ptr<PreviewProvider> & rpPreviewProvider)35 MasterPageDescriptor::MasterPageDescriptor (
36     MasterPageContainer::Origin eOrigin,
37     const sal_Int32 nTemplateIndex,
38     const String& rsURL,
39     const String& rsPageName,
40     const String& rsStyleName,
41     const bool bIsPrecious,
42     const ::boost::shared_ptr<PageObjectProvider>& rpPageObjectProvider,
43     const ::boost::shared_ptr<PreviewProvider>& rpPreviewProvider)
44     : maToken(MasterPageContainer::NIL_TOKEN),
45       meOrigin(eOrigin),
46       msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS)),
47       msPageName(rsPageName),
48       msStyleName(rsStyleName),
49       mbIsPrecious(bIsPrecious),
50       mpMasterPage(NULL),
51       mpSlide(NULL),
52       maSmallPreview(),
53       maLargePreview(),
54       mpPreviewProvider(rpPreviewProvider),
55       mpPageObjectProvider(rpPageObjectProvider),
56       mnTemplateIndex(nTemplateIndex),
57       meURLClassification(URLCLASS_UNDETERMINED),
58       mnUseCount(0)
59 {
60 }
61 
62 
63 
64 
MasterPageDescriptor(const MasterPageDescriptor & rDescriptor)65 MasterPageDescriptor::MasterPageDescriptor (const MasterPageDescriptor& rDescriptor)
66     : maToken(rDescriptor.maToken),
67       meOrigin(rDescriptor.meOrigin),
68       msURL(rDescriptor.msURL),
69       msPageName(rDescriptor.msPageName),
70       msStyleName(rDescriptor.msStyleName),
71       mbIsPrecious(rDescriptor.mbIsPrecious),
72       mpMasterPage(rDescriptor.mpMasterPage),
73       mpSlide(rDescriptor.mpSlide),
74       maSmallPreview(rDescriptor.maSmallPreview),
75       maLargePreview(rDescriptor.maLargePreview),
76       mpPreviewProvider(rDescriptor.mpPreviewProvider),
77       mpPageObjectProvider(rDescriptor.mpPageObjectProvider),
78       mnTemplateIndex(rDescriptor.mnTemplateIndex),
79       meURLClassification(rDescriptor.meURLClassification),
80       mnUseCount(rDescriptor.mnUseCount)
81 {
82 }
83 
84 
85 
86 
~MasterPageDescriptor(void)87 MasterPageDescriptor::~MasterPageDescriptor (void)
88 {
89 }
90 
91 
92 
93 
SetToken(MasterPageContainer::Token aToken)94 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
95 {
96     maToken = aToken;
97 }
98 
99 
100 
101 
GetPreview(MasterPageContainer::PreviewSize eSize)102 Image MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize)
103 {
104     if (eSize == MasterPageContainer::SMALL)
105         return maSmallPreview;
106     else
107         return maLargePreview;
108 }
109 
110 
111 
112 
113 ::std::auto_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
Update(const MasterPageDescriptor & rDescriptor)114     MasterPageDescriptor::Update (
115         const MasterPageDescriptor& rDescriptor)
116 {
117     bool bDataChanged (false);
118     bool bIndexChanged (false);
119     bool bPreviewChanged (false);
120 
121     if (meOrigin==MasterPageContainer::UNKNOWN
122         && rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
123     {
124         meOrigin = rDescriptor.meOrigin;
125         bIndexChanged = true;
126     }
127 
128     if (msURL.getLength()==0 && rDescriptor.msURL.getLength()!=0)
129     {
130         msURL = rDescriptor.msURL;
131         bDataChanged = true;
132     }
133 
134     if (msPageName.getLength()==0 && rDescriptor.msPageName.getLength()!=0)
135     {
136         msPageName = rDescriptor.msPageName;
137         bDataChanged = true;
138     }
139 
140     if (msStyleName.getLength()==0 && rDescriptor.msStyleName.getLength()!=0)
141     {
142         msStyleName = rDescriptor.msStyleName;
143         bDataChanged = true;
144     }
145 
146     if (mpPageObjectProvider.get()==NULL && rDescriptor.mpPageObjectProvider.get()!=NULL)
147     {
148         mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
149         bDataChanged = true;
150     }
151 
152      if (mpPreviewProvider.get()==NULL && rDescriptor.mpPreviewProvider.get()!=NULL)
153      {
154          mpPreviewProvider = rDescriptor.mpPreviewProvider;
155          bPreviewChanged = true;
156      }
157 
158      if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
159      {
160          mnTemplateIndex = rDescriptor.mnTemplateIndex;
161          bIndexChanged = true;
162      }
163 
164      // Prepare the list of event types that will be returned.
165      ::std::auto_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
166      if (bDataChanged || bIndexChanged || bPreviewChanged)
167      {
168          pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>());
169          if (bDataChanged)
170              pResult->push_back(MasterPageContainerChangeEvent::DATA_CHANGED);
171          if (bIndexChanged)
172              pResult->push_back(MasterPageContainerChangeEvent::INDEX_CHANGED);
173          if (bPreviewChanged)
174              pResult->push_back(MasterPageContainerChangeEvent::PREVIEW_CHANGED);
175      }
176 
177      return pResult;
178 }
179 
180 
181 
182 
UpdatePageObject(sal_Int32 nCostThreshold,SdDrawDocument * pDocument)183 bool MasterPageDescriptor::UpdatePageObject (
184     sal_Int32 nCostThreshold,
185     SdDrawDocument* pDocument)
186 {
187     bool bModified (false);
188 
189     // Update the page object when that is not yet known.
190     if (mpMasterPage == NULL
191         && mpPageObjectProvider.get()!=NULL
192         && (nCostThreshold<0 || mpPageObjectProvider->GetCostIndex()<=nCostThreshold))
193     {
194         // Note that pDocument may be NULL.
195 
196         SdPage* pPage = (*mpPageObjectProvider)(pDocument);
197         if (meOrigin == MasterPageContainer::MASTERPAGE)
198         {
199             mpMasterPage = pPage;
200             if (mpMasterPage != NULL)
201                 mpMasterPage->SetPrecious(mbIsPrecious);
202         }
203         else
204         {
205             // Master pages from templates are copied into the local document.
206             if (pDocument != NULL)
207                 mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
208             mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
209         }
210 
211         if (mpMasterPage != NULL)
212         {
213             // Update page name and style name.
214             if (msPageName.getLength() == 0)
215                 msPageName = mpMasterPage->GetName();
216             msStyleName = mpMasterPage->GetName();
217 
218             // Delete an existing substitution. The next request for a preview
219             // will create the real one.
220             maSmallPreview = Image();
221             maLargePreview = Image();
222             mpPreviewProvider = ::boost::shared_ptr<PreviewProvider>(new PagePreviewProvider());
223         }
224         else
225         {
226             DBG_ASSERT(false, "UpdatePageObject: master page is NULL");
227         }
228 
229         bModified = true;
230     }
231 
232     return bModified;
233 }
234 
235 
236 
237 
UpdatePreview(sal_Int32 nCostThreshold,const Size & rSmallSize,const Size & rLargeSize,::sd::PreviewRenderer & rRenderer)238 bool MasterPageDescriptor::UpdatePreview (
239     sal_Int32 nCostThreshold,
240     const Size& rSmallSize,
241     const Size& rLargeSize,
242     ::sd::PreviewRenderer& rRenderer)
243 {
244     bool bModified (false);
245 
246     // Update the preview when that is not yet known.
247     if (maLargePreview.GetSizePixel().Width()==0
248         && mpPreviewProvider.get()!=NULL
249         && (nCostThreshold<0 || mpPreviewProvider->GetCostIndex()<=nCostThreshold))
250     {
251         SdPage* pPage = mpSlide;
252         if (pPage == NULL)
253         {
254             pPage = mpMasterPage;
255         }
256         maLargePreview = (*mpPreviewProvider)(
257             rLargeSize.Width(),
258             pPage,
259             rRenderer);
260         if (maLargePreview.GetSizePixel().Width() > 0)
261         {
262             // Create the small preview by scaling the large one down.
263             maSmallPreview = rRenderer.ScaleBitmap(
264                 maLargePreview.GetBitmapEx(),
265                 rSmallSize.Width());
266             // The large preview may not have the desired width.  Scale it
267             // accrodingly.
268             if (maLargePreview.GetSizePixel().Width() != rLargeSize.Width())
269                 maLargePreview = rRenderer.ScaleBitmap(
270                     maLargePreview.GetBitmapEx(),
271                     rLargeSize.Width());
272             bModified = true;
273         }
274     }
275 
276     return bModified;
277 }
278 
279 
280 
281 
GetURLClassification(void)282 MasterPageDescriptor::URLClassification MasterPageDescriptor::GetURLClassification (void)
283 {
284     if (meURLClassification == URLCLASS_UNDETERMINED)
285     {
286         if (msURL.getLength() == 0)
287             meURLClassification = URLCLASS_UNKNOWN;
288         else if (msURL.indexOf(::rtl::OUString::createFromAscii("presnt"))>=0)
289         {
290             meURLClassification = URLCLASS_PRESENTATION;
291         }
292         else if (msURL.indexOf(::rtl::OUString::createFromAscii("layout"))>=0)
293         {
294             meURLClassification = URLCLASS_LAYOUT;
295         }
296         else if (msURL.indexOf(::rtl::OUString::createFromAscii("educate"))>=0)
297         {
298             meURLClassification = URLCLASS_OTHER;
299         }
300         else
301         {
302             meURLClassification = URLCLASS_USER;
303         }
304     }
305 
306     return meURLClassification;
307 }
308 
309 
310 
311 //===== URLComparator =========================================================
312 
URLComparator(const::rtl::OUString & sURL)313 MasterPageDescriptor::URLComparator::URLComparator (const ::rtl::OUString& sURL)
314     : msURL(sURL)
315 {
316 }
317 
318 
319 
320 
operator ()(const SharedMasterPageDescriptor & rDescriptor)321 bool MasterPageDescriptor::URLComparator::operator() (
322     const SharedMasterPageDescriptor& rDescriptor)
323 {
324     if (rDescriptor.get() == NULL)
325         return false;
326     else
327         return rDescriptor->msURL.equals(msURL);
328 }
329 
330 
331 
332 
333 // ===== StyleNameComparator ==================================================
334 
StyleNameComparator(const::rtl::OUString & sStyleName)335 MasterPageDescriptor::StyleNameComparator::StyleNameComparator (const ::rtl::OUString& sStyleName)
336     : msStyleName(sStyleName)
337 {
338 }
339 
340 
341 
342 
operator ()(const SharedMasterPageDescriptor & rDescriptor)343 bool MasterPageDescriptor::StyleNameComparator::operator() (
344     const SharedMasterPageDescriptor& rDescriptor)
345 {
346     if (rDescriptor.get() == NULL)
347         return false;
348     else
349         return rDescriptor->msStyleName.equals(msStyleName);
350 }
351 
352 
353 
354 
355 //===== PageObjectComparator ==================================================
356 
PageObjectComparator(const SdPage * pPageObject)357 MasterPageDescriptor::PageObjectComparator::PageObjectComparator (const SdPage* pPageObject)
358     : mpMasterPage(pPageObject)
359 {
360 }
361 
362 
363 
364 
operator ()(const SharedMasterPageDescriptor & rDescriptor)365 bool MasterPageDescriptor::PageObjectComparator::operator() (
366     const SharedMasterPageDescriptor& rDescriptor)
367 {
368     if (rDescriptor.get() == NULL)
369         return false;
370     else
371         return rDescriptor->mpMasterPage==mpMasterPage;
372 }
373 
374 
375 
376 
377 //===== AllComparator =========================================================
378 
AllComparator(const SharedMasterPageDescriptor & rDescriptor)379 MasterPageDescriptor::AllComparator::AllComparator(const SharedMasterPageDescriptor& rDescriptor)
380     : mpDescriptor(rDescriptor)
381 {
382 }
383 
384 
385 
386 
operator ()(const SharedMasterPageDescriptor & rDescriptor)387 bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
388 {
389     if (rDescriptor.get() == NULL)
390         return false;
391     else
392     {
393         // Take URL, page name, style name, and page object into account
394         // when comparing two descriptors.  When two descriptors are
395         // identical in any of these values then their are thought of as
396         // equivalent.  Only the Origin has to be the same in both
397         // descriptors.
398         return
399             mpDescriptor->meOrigin == rDescriptor->meOrigin
400             && (
401                 (mpDescriptor->msURL.getLength()>0
402                     && mpDescriptor->msURL.equals(rDescriptor->msURL))
403                 || (mpDescriptor->msPageName.getLength()>0
404                     && mpDescriptor->msPageName.equals(rDescriptor->msPageName))
405                 || (mpDescriptor->msStyleName.getLength()>0
406                     && mpDescriptor->msStyleName.equals(rDescriptor->msStyleName))
407                 || (mpDescriptor->mpMasterPage!=NULL
408                     && mpDescriptor->mpMasterPage==rDescriptor->mpMasterPage)
409                 || (mpDescriptor->mpPageObjectProvider.get()!=NULL
410                     && rDescriptor->mpPageObjectProvider.get()!=NULL
411                     && mpDescriptor->mpPageObjectProvider==rDescriptor->mpPageObjectProvider));
412     }
413 }
414 
415 
416 } } // end of namespace sd::sidebar
417