Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "MasterPageContainerProviders.hxx"
21 :
22 : #include "DrawDocShell.hxx"
23 : #include "drawdoc.hxx"
24 : #include "PreviewRenderer.hxx"
25 : #include <comphelper/processfactory.hxx>
26 : #include <sfx2/app.hxx>
27 : #include <sfx2/sfxsids.hrc>
28 : #include <unotools/ucbstreamhelper.hxx>
29 : #include <vcl/image.hxx>
30 : #include <vcl/pngread.hxx>
31 : #include <com/sun/star/embed/ElementModes.hpp>
32 : #include <com/sun/star/embed/StorageFactory.hpp>
33 : #include <tools/diagnose_ex.h>
34 :
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::uno;
37 :
38 : namespace sd { namespace sidebar {
39 :
40 :
41 : //===== PagePreviewProvider ===================================================
42 :
43 0 : PagePreviewProvider::PagePreviewProvider (void)
44 : {
45 0 : }
46 :
47 :
48 :
49 :
50 0 : Image PagePreviewProvider::operator () (
51 : int nWidth,
52 : SdPage* pPage,
53 : ::sd::PreviewRenderer& rRenderer)
54 : {
55 0 : Image aPreview;
56 :
57 0 : if (pPage != NULL)
58 : {
59 : // Use the given renderer to create a preview of the given page
60 : // object.
61 0 : aPreview = rRenderer.RenderPage(
62 : pPage,
63 : nWidth,
64 : OUString(),
65 0 : false);
66 : }
67 :
68 0 : return aPreview;
69 : }
70 :
71 :
72 :
73 :
74 0 : int PagePreviewProvider::GetCostIndex (void)
75 : {
76 0 : return 5;
77 : }
78 :
79 :
80 :
81 :
82 0 : bool PagePreviewProvider::NeedsPageObject (void)
83 : {
84 0 : return true;
85 : }
86 :
87 :
88 :
89 :
90 : //===== TemplatePreviewProvider ===============================================
91 :
92 0 : TemplatePreviewProvider::TemplatePreviewProvider (const OUString& rsURL)
93 0 : : msURL(rsURL)
94 : {
95 0 : }
96 :
97 :
98 :
99 :
100 0 : Image TemplatePreviewProvider::operator() (
101 : int nWidth,
102 : SdPage* pPage,
103 : ::sd::PreviewRenderer& rRenderer)
104 : {
105 : // Unused parameters.
106 : (void)nWidth;
107 : (void)pPage;
108 : (void)rRenderer;
109 :
110 : // Load the thumbnail from a template document.
111 0 : uno::Reference<io::XInputStream> xIStream;
112 :
113 0 : uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext());
114 : try
115 : {
116 0 : uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create(xContext);
117 :
118 0 : uno::Sequence<uno::Any> aArgs (2);
119 0 : aArgs[0] <<= msURL;
120 0 : aArgs[1] <<= embed::ElementModes::READ;
121 : uno::Reference<embed::XStorage> xDocStorage (
122 0 : xStorageFactory->createInstanceWithArguments(aArgs),
123 0 : uno::UNO_QUERY);
124 :
125 : try
126 : {
127 0 : if (xDocStorage.is())
128 : {
129 : uno::Reference<embed::XStorage> xStorage (
130 0 : xDocStorage->openStorageElement(
131 : "Thumbnails",
132 0 : embed::ElementModes::READ));
133 0 : if (xStorage.is())
134 : {
135 : uno::Reference<io::XStream> xThumbnailCopy (
136 0 : xStorage->cloneStreamElement("thumbnail.png"));
137 0 : if (xThumbnailCopy.is())
138 0 : xIStream = xThumbnailCopy->getInputStream();
139 0 : }
140 : }
141 : }
142 0 : catch (const uno::Exception& rException)
143 : {
144 : OSL_TRACE (
145 : "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
146 : OUStringToOString(msURL,
147 : RTL_TEXTENCODING_UTF8).getStr(),
148 : OUStringToOString(rException.Message,
149 : RTL_TEXTENCODING_UTF8).getStr());
150 : }
151 :
152 : try
153 : {
154 : // An (older) implementation had a bug - The storage
155 : // name was "Thumbnail" instead of "Thumbnails". The
156 : // old name is still used as fallback but this code can
157 : // be removed soon.
158 0 : if ( ! xIStream.is())
159 : {
160 : uno::Reference<embed::XStorage> xStorage (
161 0 : xDocStorage->openStorageElement( "Thumbnail",
162 0 : embed::ElementModes::READ));
163 0 : if (xStorage.is())
164 : {
165 : uno::Reference<io::XStream> xThumbnailCopy (
166 0 : xStorage->cloneStreamElement("thumbnail.png"));
167 0 : if (xThumbnailCopy.is())
168 0 : xIStream = xThumbnailCopy->getInputStream();
169 0 : }
170 : }
171 : }
172 0 : catch (const uno::Exception& rException)
173 : {
174 : OSL_TRACE (
175 : "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
176 : OUStringToOString(msURL,
177 : RTL_TEXTENCODING_UTF8).getStr(),
178 : OUStringToOString(rException.Message,
179 : RTL_TEXTENCODING_UTF8).getStr());
180 0 : }
181 : }
182 0 : catch (const uno::Exception& rException)
183 : {
184 : OSL_TRACE (
185 : "caught exception while trying to access tuhmbnail of %s: %s",
186 : OUStringToOString(msURL,
187 : RTL_TEXTENCODING_UTF8).getStr(),
188 : OUStringToOString(rException.Message,
189 : RTL_TEXTENCODING_UTF8).getStr());
190 : }
191 :
192 : // Extract the image from the stream.
193 0 : BitmapEx aThumbnail;
194 0 : if (xIStream.is())
195 : {
196 : ::std::auto_ptr<SvStream> pStream (
197 0 : ::utl::UcbStreamHelper::CreateStream (xIStream));
198 0 : ::vcl::PNGReader aReader (*pStream);
199 0 : aThumbnail = aReader.Read ();
200 : }
201 :
202 : // Note that the preview is returned without scaling it to the desired
203 : // width. This gives the caller the chance to take advantage of a
204 : // possibly larger resolution then was asked for.
205 0 : return aThumbnail;
206 : }
207 :
208 :
209 :
210 :
211 0 : int TemplatePreviewProvider::GetCostIndex (void)
212 : {
213 0 : return 10;
214 : }
215 :
216 :
217 :
218 :
219 0 : bool TemplatePreviewProvider::NeedsPageObject (void)
220 : {
221 0 : return false;
222 : }
223 :
224 :
225 :
226 :
227 : //===== TemplatePageObjectProvider =============================================
228 :
229 0 : TemplatePageObjectProvider::TemplatePageObjectProvider (const OUString& rsURL)
230 : : msURL(rsURL),
231 0 : mxDocumentShell()
232 : {
233 0 : }
234 :
235 :
236 :
237 :
238 0 : SdPage* TemplatePageObjectProvider::operator() (SdDrawDocument* pContainerDocument)
239 : {
240 : // Unused parameters.
241 : (void)pContainerDocument;
242 :
243 0 : SdPage* pPage = NULL;
244 :
245 0 : mxDocumentShell = NULL;
246 0 : ::sd::DrawDocShell* pDocumentShell = NULL;
247 : try
248 : {
249 : // Load the template document and return its first page.
250 0 : pDocumentShell = LoadDocument (msURL);
251 0 : if (pDocumentShell != NULL)
252 : {
253 0 : SdDrawDocument* pDocument = pDocumentShell->GetDoc();
254 0 : if (pDocument != NULL)
255 : {
256 0 : pPage = pDocument->GetMasterSdPage(0, PK_STANDARD);
257 : // In order to make the newly loaded master page deletable
258 : // when copied into documents it is marked as no "precious".
259 : // When it is modified then it is marked as "precious".
260 0 : if (pPage != NULL)
261 0 : pPage->SetPrecious(false);
262 : }
263 : }
264 : }
265 0 : catch (const uno::RuntimeException&)
266 : {
267 : DBG_UNHANDLED_EXCEPTION();
268 0 : pPage = NULL;
269 : }
270 :
271 0 : return pPage;
272 : }
273 :
274 :
275 :
276 :
277 0 : ::sd::DrawDocShell* TemplatePageObjectProvider::LoadDocument (const OUString& sFileName)
278 : {
279 0 : SfxApplication* pSfxApp = SFX_APP();
280 0 : SfxItemSet* pSet = new SfxAllItemSet (pSfxApp->GetPool());
281 0 : pSet->Put (SfxBoolItem (SID_TEMPLATE, sal_True));
282 0 : pSet->Put (SfxBoolItem (SID_PREVIEW, sal_True));
283 0 : if (pSfxApp->LoadTemplate (mxDocumentShell, sFileName, sal_True, pSet))
284 : {
285 0 : mxDocumentShell = NULL;
286 : }
287 0 : SfxObjectShell* pShell = mxDocumentShell;
288 0 : return PTR_CAST(::sd::DrawDocShell,pShell);
289 : }
290 :
291 :
292 :
293 :
294 0 : int TemplatePageObjectProvider::GetCostIndex (void)
295 : {
296 0 : return 20;
297 : }
298 :
299 :
300 :
301 :
302 0 : bool TemplatePageObjectProvider::operator== (const PageObjectProvider& rProvider)
303 : {
304 : const TemplatePageObjectProvider* pTemplatePageObjectProvider
305 0 : = dynamic_cast<const TemplatePageObjectProvider*>(&rProvider);
306 0 : if (pTemplatePageObjectProvider != NULL)
307 0 : return (msURL == pTemplatePageObjectProvider->msURL);
308 : else
309 0 : return false;
310 : }
311 :
312 :
313 :
314 :
315 : //===== DefaultPageObjectProvider ==============================================
316 :
317 0 : DefaultPageObjectProvider::DefaultPageObjectProvider (void)
318 : {
319 0 : }
320 :
321 :
322 :
323 :
324 0 : SdPage* DefaultPageObjectProvider::operator () (SdDrawDocument* pContainerDocument)
325 : {
326 0 : SdPage* pLocalMasterPage = NULL;
327 0 : if (pContainerDocument != NULL)
328 : {
329 0 : sal_Int32 nIndex (0);
330 0 : SdPage* pLocalSlide = pContainerDocument->GetSdPage((sal_uInt16)nIndex, PK_STANDARD);
331 0 : if (pLocalSlide!=NULL && pLocalSlide->TRG_HasMasterPage())
332 0 : pLocalMasterPage = dynamic_cast<SdPage*>(&pLocalSlide->TRG_GetMasterPage());
333 : }
334 :
335 : if (pLocalMasterPage == NULL)
336 : {
337 : DBG_ASSERT(false, "can not create master page for slide");
338 : }
339 :
340 0 : return pLocalMasterPage;
341 : }
342 :
343 :
344 :
345 :
346 0 : int DefaultPageObjectProvider::GetCostIndex (void)
347 : {
348 0 : return 15;
349 : }
350 :
351 :
352 :
353 :
354 0 : bool DefaultPageObjectProvider::operator== (const PageObjectProvider& rProvider)
355 : {
356 0 : return (dynamic_cast<const DefaultPageObjectProvider*>(&rProvider) != NULL);
357 : }
358 :
359 :
360 :
361 :
362 : //===== ExistingPageProvider ==================================================
363 :
364 0 : ExistingPageProvider::ExistingPageProvider (SdPage* pPage)
365 0 : : mpPage(pPage)
366 : {
367 0 : }
368 :
369 :
370 :
371 :
372 0 : SdPage* ExistingPageProvider::operator() (SdDrawDocument* pDocument)
373 : {
374 : (void)pDocument; // Unused parameter.
375 :
376 0 : return mpPage;
377 : }
378 :
379 :
380 :
381 :
382 0 : int ExistingPageProvider::GetCostIndex (void)
383 : {
384 0 : return 0;
385 : }
386 :
387 :
388 :
389 :
390 0 : bool ExistingPageProvider::operator== (const PageObjectProvider& rProvider)
391 : {
392 : const ExistingPageProvider* pExistingPageProvider
393 0 : = dynamic_cast<const ExistingPageProvider*>(&rProvider);
394 0 : if (pExistingPageProvider != NULL)
395 0 : return (mpPage == pExistingPageProvider->mpPage);
396 : else
397 0 : return false;
398 : }
399 :
400 :
401 33 : } } // end of namespace sd::sidebar
402 :
403 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|