1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
31 #include "MasterPageContainerProviders.hxx"
33 #include "DrawDocShell.hxx"
34 #include "drawdoc.hxx"
35 #include "PreviewRenderer.hxx"
36 #include <comphelper/processfactory.hxx>
37 #include <sfx2/app.hxx>
38 #include <sfx2/sfxsids.hrc>
39 #include <unotools/ucbstreamhelper.hxx>
40 #include <vcl/image.hxx>
41 #include <vcl/pngread.hxx>
42 #include <com/sun/star/embed/ElementModes.hpp>
43 #include <tools/diagnose_ex.h>
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
47 using namespace ::sd::toolpanel::controls
;
50 namespace sd
{ namespace toolpanel
{ namespace controls
{
53 //===== PagePreviewProvider ===================================================
55 PagePreviewProvider::PagePreviewProvider (void)
62 Image
PagePreviewProvider::operator () (
65 ::sd::PreviewRenderer
& rRenderer
)
71 // Use the given renderer to create a preview of the given page
73 aPreview
= rRenderer
.RenderPage(
76 String::CreateFromAscii(""),
86 int PagePreviewProvider::GetCostIndex (void)
94 bool PagePreviewProvider::NeedsPageObject (void)
102 //===== TemplatePreviewProvider ===============================================
104 TemplatePreviewProvider::TemplatePreviewProvider (const ::rtl::OUString
& rsURL
)
112 Image
TemplatePreviewProvider::operator() (
115 ::sd::PreviewRenderer
& rRenderer
)
117 // Unused parameters.
122 // Load the thumbnail from a template document.
123 uno::Reference
<io::XInputStream
> xIStream
;
125 uno::Reference
< lang::XMultiServiceFactory
> xServiceManager (
126 ::comphelper::getProcessServiceFactory());
127 if (xServiceManager
.is())
131 uno::Reference
<lang::XSingleServiceFactory
> xStorageFactory(
132 xServiceManager
->createInstance(
133 ::rtl::OUString::createFromAscii(
134 "com.sun.star.embed.StorageFactory")),
137 if (xStorageFactory
.is())
139 uno::Sequence
<uno::Any
> aArgs (2);
141 aArgs
[1] <<= embed::ElementModes::READ
;
142 uno::Reference
<embed::XStorage
> xDocStorage (
143 xStorageFactory
->createInstanceWithArguments(aArgs
),
148 if (xDocStorage
.is())
150 uno::Reference
<embed::XStorage
> xStorage (
151 xDocStorage
->openStorageElement(
152 ::rtl::OUString::createFromAscii("Thumbnails"),
153 embed::ElementModes::READ
));
156 uno::Reference
<io::XStream
> xThumbnailCopy (
157 xStorage
->cloneStreamElement(
158 ::rtl::OUString::createFromAscii(
160 if (xThumbnailCopy
.is())
161 xIStream
= xThumbnailCopy
->getInputStream();
165 catch (uno::Exception
& rException
)
168 "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
169 ::rtl::OUStringToOString(msURL
,
170 RTL_TEXTENCODING_UTF8
).getStr(),
171 ::rtl::OUStringToOString(rException
.Message
,
172 RTL_TEXTENCODING_UTF8
).getStr());
177 // An (older) implementation had a bug - The storage
178 // name was "Thumbnail" instead of "Thumbnails". The
179 // old name is still used as fallback but this code can
181 if ( ! xIStream
.is())
183 uno::Reference
<embed::XStorage
> xStorage (
184 xDocStorage
->openStorageElement(
185 ::rtl::OUString::createFromAscii("Thumbnail"),
186 embed::ElementModes::READ
));
189 uno::Reference
<io::XStream
> xThumbnailCopy (
190 xStorage
->cloneStreamElement(
191 ::rtl::OUString::createFromAscii(
193 if (xThumbnailCopy
.is())
194 xIStream
= xThumbnailCopy
->getInputStream();
198 catch (uno::Exception
& rException
)
201 "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
202 ::rtl::OUStringToOString(msURL
,
203 RTL_TEXTENCODING_UTF8
).getStr(),
204 ::rtl::OUStringToOString(rException
.Message
,
205 RTL_TEXTENCODING_UTF8
).getStr());
209 catch (uno::Exception
& rException
)
212 "caught exception while trying to access tuhmbnail of %s: %s",
213 ::rtl::OUStringToOString(msURL
,
214 RTL_TEXTENCODING_UTF8
).getStr(),
215 ::rtl::OUStringToOString(rException
.Message
,
216 RTL_TEXTENCODING_UTF8
).getStr());
220 // Extract the image from the stream.
224 ::std::auto_ptr
<SvStream
> pStream (
225 ::utl::UcbStreamHelper::CreateStream (xIStream
));
226 ::vcl::PNGReader
aReader (*pStream
);
227 aThumbnail
= aReader
.Read ();
230 // Note that the preview is returned without scaling it to the desired
231 // width. This gives the caller the chance to take advantage of a
232 // possibly larger resolution then was asked for.
239 int TemplatePreviewProvider::GetCostIndex (void)
247 bool TemplatePreviewProvider::NeedsPageObject (void)
255 //===== TemplatePageObjectProvider =============================================
257 TemplatePageObjectProvider::TemplatePageObjectProvider (const ::rtl::OUString
& rsURL
)
266 SdPage
* TemplatePageObjectProvider::operator() (SdDrawDocument
* pContainerDocument
)
268 // Unused parameters.
269 (void)pContainerDocument
;
271 SdPage
* pPage
= NULL
;
273 mxDocumentShell
= NULL
;
274 ::sd::DrawDocShell
* pDocumentShell
= NULL
;
277 // Load the template document and return its first page.
278 pDocumentShell
= LoadDocument (msURL
);
279 if (pDocumentShell
!= NULL
)
281 SdDrawDocument
* pDocument
= pDocumentShell
->GetDoc();
282 if (pDocument
!= NULL
)
284 pPage
= pDocument
->GetMasterSdPage(0, PK_STANDARD
);
285 // In order to make the newly loaded master page deletable
286 // when copied into documents it is marked as no "precious".
287 // When it is modified then it is marked as "precious".
289 pPage
->SetPrecious(false);
293 catch (uno::RuntimeException
)
295 DBG_UNHANDLED_EXCEPTION();
305 ::sd::DrawDocShell
* TemplatePageObjectProvider::LoadDocument (const ::rtl::OUString
& sFileName
)
307 SfxApplication
* pSfxApp
= SFX_APP();
308 SfxItemSet
* pSet
= new SfxAllItemSet (pSfxApp
->GetPool());
309 pSet
->Put (SfxBoolItem (SID_TEMPLATE
, sal_True
));
310 pSet
->Put (SfxBoolItem (SID_PREVIEW
, sal_True
));
311 if (pSfxApp
->LoadTemplate (mxDocumentShell
, sFileName
, sal_True
, pSet
))
313 mxDocumentShell
= NULL
;
315 SfxObjectShell
* pShell
= mxDocumentShell
;
316 return PTR_CAST(::sd::DrawDocShell
,pShell
);
322 int TemplatePageObjectProvider::GetCostIndex (void)
330 bool TemplatePageObjectProvider::operator== (const PageObjectProvider
& rProvider
)
332 const TemplatePageObjectProvider
* pTemplatePageObjectProvider
333 = dynamic_cast<const TemplatePageObjectProvider
*>(&rProvider
);
334 if (pTemplatePageObjectProvider
!= NULL
)
335 return (msURL
== pTemplatePageObjectProvider
->msURL
);
343 //===== DefaultPageObjectProvider ==============================================
345 DefaultPageObjectProvider::DefaultPageObjectProvider (void)
352 SdPage
* DefaultPageObjectProvider::operator () (SdDrawDocument
* pContainerDocument
)
354 SdPage
* pLocalMasterPage
= NULL
;
355 if (pContainerDocument
!= NULL
)
357 sal_Int32
nIndex (0);
358 SdPage
* pLocalSlide
= pContainerDocument
->GetSdPage((sal_uInt16
)nIndex
, PK_STANDARD
);
359 if (pLocalSlide
!=NULL
&& pLocalSlide
->TRG_HasMasterPage())
360 pLocalMasterPage
= dynamic_cast<SdPage
*>(&pLocalSlide
->TRG_GetMasterPage());
363 if (pLocalMasterPage
== NULL
)
365 DBG_ASSERT(false, "can not create master page for slide");
368 return pLocalMasterPage
;
374 int DefaultPageObjectProvider::GetCostIndex (void)
382 bool DefaultPageObjectProvider::operator== (const PageObjectProvider
& rProvider
)
384 return (dynamic_cast<const DefaultPageObjectProvider
*>(&rProvider
) != NULL
);
390 //===== ExistingPageProvider ==================================================
392 ExistingPageProvider::ExistingPageProvider (SdPage
* pPage
)
400 SdPage
* ExistingPageProvider::operator() (SdDrawDocument
* pDocument
)
402 (void)pDocument
; // Unused parameter.
410 int ExistingPageProvider::GetCostIndex (void)
418 bool ExistingPageProvider::operator== (const PageObjectProvider
& rProvider
)
420 const ExistingPageProvider
* pExistingPageProvider
421 = dynamic_cast<const ExistingPageProvider
*>(&rProvider
);
422 if (pExistingPageProvider
!= NULL
)
423 return (mpPage
== pExistingPageProvider
->mpPage
);
429 } } } // end of namespace ::sd::toolpanel::controls