1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MasterPageContainerProviders.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "MasterPageContainerProviders.hxx"
36 #include "DrawDocShell.hxx"
37 #include "drawdoc.hxx"
38 #include "PreviewRenderer.hxx"
39 #include <comphelper/processfactory.hxx>
40 #include <sfx2/app.hxx>
41 #include <sfx2/sfxsids.hrc>
42 #include <unotools/ucbstreamhelper.hxx>
43 #include <vcl/image.hxx>
44 #include <vcl/pngread.hxx>
45 #include <com/sun/star/embed/ElementModes.hpp>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::sd::toolpanel::controls
;
52 namespace sd
{ namespace toolpanel
{ namespace controls
{
55 //===== PagePreviewProvider ===================================================
57 PagePreviewProvider::PagePreviewProvider (void)
64 Image
PagePreviewProvider::operator () (
67 ::sd::PreviewRenderer
& rRenderer
)
73 // Use the given renderer to create a preview of the given page
75 aPreview
= rRenderer
.RenderPage(
78 String::CreateFromAscii(""),
88 int PagePreviewProvider::GetCostIndex (void)
96 bool PagePreviewProvider::NeedsPageObject (void)
104 //===== TemplatePreviewProvider ===============================================
106 TemplatePreviewProvider::TemplatePreviewProvider (const ::rtl::OUString
& rsURL
)
114 Image
TemplatePreviewProvider::operator() (
117 ::sd::PreviewRenderer
& rRenderer
)
119 // Unused parameters.
124 // Load the thumbnail from a template document.
125 uno::Reference
<io::XInputStream
> xIStream
;
127 uno::Reference
< lang::XMultiServiceFactory
> xServiceManager (
128 ::comphelper::getProcessServiceFactory());
129 if (xServiceManager
.is())
133 uno::Reference
<lang::XSingleServiceFactory
> xStorageFactory(
134 xServiceManager
->createInstance(
135 ::rtl::OUString::createFromAscii(
136 "com.sun.star.embed.StorageFactory")),
139 if (xStorageFactory
.is())
141 uno::Sequence
<uno::Any
> aArgs (2);
143 aArgs
[1] <<= embed::ElementModes::READ
;
144 uno::Reference
<embed::XStorage
> xDocStorage (
145 xStorageFactory
->createInstanceWithArguments(aArgs
),
150 if (xDocStorage
.is())
152 uno::Reference
<embed::XStorage
> xStorage (
153 xDocStorage
->openStorageElement(
154 ::rtl::OUString::createFromAscii("Thumbnails"),
155 embed::ElementModes::READ
));
158 uno::Reference
<io::XStream
> xThumbnailCopy (
159 xStorage
->cloneStreamElement(
160 ::rtl::OUString::createFromAscii(
162 if (xThumbnailCopy
.is())
163 xIStream
= xThumbnailCopy
->getInputStream();
167 catch (uno::Exception
& rException
)
170 "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
171 ::rtl::OUStringToOString(msURL
,
172 RTL_TEXTENCODING_UTF8
).getStr(),
173 ::rtl::OUStringToOString(rException
.Message
,
174 RTL_TEXTENCODING_UTF8
).getStr());
179 // An (older) implementation had a bug - The storage
180 // name was "Thumbnail" instead of "Thumbnails". The
181 // old name is still used as fallback but this code can
183 if ( ! xIStream
.is())
185 uno::Reference
<embed::XStorage
> xStorage (
186 xDocStorage
->openStorageElement(
187 ::rtl::OUString::createFromAscii("Thumbnail"),
188 embed::ElementModes::READ
));
191 uno::Reference
<io::XStream
> xThumbnailCopy (
192 xStorage
->cloneStreamElement(
193 ::rtl::OUString::createFromAscii(
195 if (xThumbnailCopy
.is())
196 xIStream
= xThumbnailCopy
->getInputStream();
200 catch (uno::Exception
& rException
)
203 "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
204 ::rtl::OUStringToOString(msURL
,
205 RTL_TEXTENCODING_UTF8
).getStr(),
206 ::rtl::OUStringToOString(rException
.Message
,
207 RTL_TEXTENCODING_UTF8
).getStr());
211 catch (uno::Exception
& rException
)
214 "caught exception while trying to access tuhmbnail of %s: %s",
215 ::rtl::OUStringToOString(msURL
,
216 RTL_TEXTENCODING_UTF8
).getStr(),
217 ::rtl::OUStringToOString(rException
.Message
,
218 RTL_TEXTENCODING_UTF8
).getStr());
222 // Extract the image from the stream.
226 ::std::auto_ptr
<SvStream
> pStream (
227 ::utl::UcbStreamHelper::CreateStream (xIStream
));
228 ::vcl::PNGReader
aReader (*pStream
);
229 aThumbnail
= aReader
.Read ();
232 // Note that the preview is returned without scaling it to the desired
233 // width. This gives the caller the chance to take advantage of a
234 // possibly larger resolution then was asked for.
241 int TemplatePreviewProvider::GetCostIndex (void)
249 bool TemplatePreviewProvider::NeedsPageObject (void)
257 //===== TemplatePageObjectProvider =============================================
259 TemplatePageObjectProvider::TemplatePageObjectProvider (const ::rtl::OUString
& rsURL
)
268 SdPage
* TemplatePageObjectProvider::operator() (SdDrawDocument
* pContainerDocument
)
270 // Unused parameters.
271 (void)pContainerDocument
;
273 SdPage
* pPage
= NULL
;
275 mxDocumentShell
= NULL
;
276 ::sd::DrawDocShell
* pDocumentShell
= NULL
;
279 // Load the template document and return its first page.
280 pDocumentShell
= LoadDocument (msURL
);
281 if (pDocumentShell
!= NULL
)
283 SdDrawDocument
* pDocument
= pDocumentShell
->GetDoc();
284 if (pDocument
!= NULL
)
286 pPage
= pDocument
->GetMasterSdPage(0, PK_STANDARD
);
287 // In order to make the newly loaded master page deletable
288 // when copied into documents it is marked as no "precious".
289 // When it is modified then it is marked as "precious".
291 pPage
->SetPrecious(false);
295 catch (uno::RuntimeException
)
297 OSL_TRACE ("caught exception while loading page from template file");
307 ::sd::DrawDocShell
* TemplatePageObjectProvider::LoadDocument (const ::rtl::OUString
& sFileName
)
309 SfxApplication
* pSfxApp
= SFX_APP();
310 SfxItemSet
* pSet
= new SfxAllItemSet (pSfxApp
->GetPool());
311 pSet
->Put (SfxBoolItem (SID_TEMPLATE
, TRUE
));
312 pSet
->Put (SfxBoolItem (SID_PREVIEW
, TRUE
));
313 if (pSfxApp
->LoadTemplate (mxDocumentShell
, sFileName
, TRUE
, pSet
))
315 mxDocumentShell
= NULL
;
317 SfxObjectShell
* pShell
= mxDocumentShell
;
318 return PTR_CAST(::sd::DrawDocShell
,pShell
);
324 int TemplatePageObjectProvider::GetCostIndex (void)
332 bool TemplatePageObjectProvider::operator== (const PageObjectProvider
& rProvider
)
334 const TemplatePageObjectProvider
* pTemplatePageObjectProvider
335 = dynamic_cast<const TemplatePageObjectProvider
*>(&rProvider
);
336 if (pTemplatePageObjectProvider
!= NULL
)
337 return (msURL
== pTemplatePageObjectProvider
->msURL
);
345 //===== DefaultPageObjectProvider ==============================================
347 DefaultPageObjectProvider::DefaultPageObjectProvider (void)
354 SdPage
* DefaultPageObjectProvider::operator () (SdDrawDocument
* pContainerDocument
)
356 SdPage
* pLocalMasterPage
= NULL
;
357 if (pContainerDocument
!= NULL
)
359 sal_Int32
nIndex (0);
360 SdPage
* pLocalSlide
= pContainerDocument
->GetSdPage((USHORT
)nIndex
, PK_STANDARD
);
361 if (pLocalSlide
!=NULL
&& pLocalSlide
->TRG_HasMasterPage())
362 pLocalMasterPage
= dynamic_cast<SdPage
*>(&pLocalSlide
->TRG_GetMasterPage());
365 if (pLocalMasterPage
== NULL
)
367 DBG_ASSERT(false, "can not create master page for slide");
370 return pLocalMasterPage
;
376 int DefaultPageObjectProvider::GetCostIndex (void)
384 bool DefaultPageObjectProvider::operator== (const PageObjectProvider
& rProvider
)
386 return (dynamic_cast<const DefaultPageObjectProvider
*>(&rProvider
) != NULL
);
392 //===== DefaultPagePreviewProvider ============================================
394 DefaultPagePreviewProvider::DefaultPagePreviewProvider (void)
400 Image
DefaultPagePreviewProvider::operator() (
403 ::sd::PreviewRenderer
& rRenderer
)
405 // Unused parameters.
416 int DefaultPagePreviewProvider::GetCostIndex (void)
424 bool DefaultPagePreviewProvider::NeedsPageObject (void)
432 //===== ExistingPageProvider ==================================================
434 ExistingPageProvider::ExistingPageProvider (SdPage
* pPage
)
442 SdPage
* ExistingPageProvider::operator() (SdDrawDocument
* pDocument
)
444 (void)pDocument
; // Unused parameter.
452 int ExistingPageProvider::GetCostIndex (void)
460 bool ExistingPageProvider::operator== (const PageObjectProvider
& rProvider
)
462 const ExistingPageProvider
* pExistingPageProvider
463 = dynamic_cast<const ExistingPageProvider
*>(&rProvider
);
464 if (pExistingPageProvider
!= NULL
)
465 return (mpPage
== pExistingPageProvider
->mpPage
);
471 } } } // end of namespace ::sd::toolpanel::controls