102c50d82SAndre Fischer /**************************************************************
202c50d82SAndre Fischer *
302c50d82SAndre Fischer * Licensed to the Apache Software Foundation (ASF) under one
402c50d82SAndre Fischer * or more contributor license agreements. See the NOTICE file
502c50d82SAndre Fischer * distributed with this work for additional information
602c50d82SAndre Fischer * regarding copyright ownership. The ASF licenses this file
702c50d82SAndre Fischer * to you under the Apache License, Version 2.0 (the
802c50d82SAndre Fischer * "License"); you may not use this file except in compliance
902c50d82SAndre Fischer * with the License. You may obtain a copy of the License at
1002c50d82SAndre Fischer *
1102c50d82SAndre Fischer * http://www.apache.org/licenses/LICENSE-2.0
1202c50d82SAndre Fischer *
1302c50d82SAndre Fischer * Unless required by applicable law or agreed to in writing,
1402c50d82SAndre Fischer * software distributed under the License is distributed on an
1502c50d82SAndre Fischer * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1602c50d82SAndre Fischer * KIND, either express or implied. See the License for the
1702c50d82SAndre Fischer * specific language governing permissions and limitations
1802c50d82SAndre Fischer * under the License.
1902c50d82SAndre Fischer *
2002c50d82SAndre Fischer *************************************************************/
2102c50d82SAndre Fischer
2202c50d82SAndre Fischer #include "precompiled_sd.hxx"
2302c50d82SAndre Fischer
2402c50d82SAndre Fischer #include "MasterPageContainerProviders.hxx"
2502c50d82SAndre Fischer
2602c50d82SAndre Fischer #include "DrawDocShell.hxx"
2702c50d82SAndre Fischer #include "drawdoc.hxx"
2802c50d82SAndre Fischer #include "PreviewRenderer.hxx"
2902c50d82SAndre Fischer #include <comphelper/processfactory.hxx>
3002c50d82SAndre Fischer #include <sfx2/app.hxx>
3102c50d82SAndre Fischer #include <sfx2/sfxsids.hrc>
3202c50d82SAndre Fischer #include <unotools/ucbstreamhelper.hxx>
3302c50d82SAndre Fischer #include <vcl/image.hxx>
3402c50d82SAndre Fischer #include <vcl/pngread.hxx>
3502c50d82SAndre Fischer #include <com/sun/star/embed/ElementModes.hpp>
3602c50d82SAndre Fischer #include <tools/diagnose_ex.h>
3702c50d82SAndre Fischer
3802c50d82SAndre Fischer using namespace ::com::sun::star;
3902c50d82SAndre Fischer using namespace ::com::sun::star::uno;
4002c50d82SAndre Fischer
4102c50d82SAndre Fischer namespace sd { namespace sidebar {
4202c50d82SAndre Fischer
4302c50d82SAndre Fischer //===== PagePreviewProvider ===================================================
4402c50d82SAndre Fischer
PagePreviewProvider(void)4502c50d82SAndre Fischer PagePreviewProvider::PagePreviewProvider (void)
4602c50d82SAndre Fischer {
4702c50d82SAndre Fischer }
4802c50d82SAndre Fischer
4902c50d82SAndre Fischer
operator ()(int nWidth,SdPage * pPage,::sd::PreviewRenderer & rRenderer)5002c50d82SAndre Fischer Image PagePreviewProvider::operator () (
5102c50d82SAndre Fischer int nWidth,
5202c50d82SAndre Fischer SdPage* pPage,
5302c50d82SAndre Fischer ::sd::PreviewRenderer& rRenderer)
5402c50d82SAndre Fischer {
5502c50d82SAndre Fischer Image aPreview;
5602c50d82SAndre Fischer
5702c50d82SAndre Fischer if (pPage != NULL)
5802c50d82SAndre Fischer {
5902c50d82SAndre Fischer // Use the given renderer to create a preview of the given page
6002c50d82SAndre Fischer // object.
6102c50d82SAndre Fischer aPreview = rRenderer.RenderPage(
6202c50d82SAndre Fischer pPage,
6302c50d82SAndre Fischer nWidth,
6402c50d82SAndre Fischer String::CreateFromAscii(""),
6502c50d82SAndre Fischer false);
6602c50d82SAndre Fischer }
6702c50d82SAndre Fischer
6802c50d82SAndre Fischer return aPreview;
6902c50d82SAndre Fischer }
7002c50d82SAndre Fischer
7102c50d82SAndre Fischer
GetCostIndex(void)7202c50d82SAndre Fischer int PagePreviewProvider::GetCostIndex (void)
7302c50d82SAndre Fischer {
7402c50d82SAndre Fischer return 5;
7502c50d82SAndre Fischer }
7602c50d82SAndre Fischer
7702c50d82SAndre Fischer
NeedsPageObject(void)7802c50d82SAndre Fischer bool PagePreviewProvider::NeedsPageObject (void)
7902c50d82SAndre Fischer {
8002c50d82SAndre Fischer return true;
8102c50d82SAndre Fischer }
8202c50d82SAndre Fischer
8302c50d82SAndre Fischer
8402c50d82SAndre Fischer //===== TemplatePreviewProvider ===============================================
8502c50d82SAndre Fischer
TemplatePreviewProvider(const::rtl::OUString & rsURL)8602c50d82SAndre Fischer TemplatePreviewProvider::TemplatePreviewProvider (const ::rtl::OUString& rsURL)
8702c50d82SAndre Fischer : msURL(rsURL)
8802c50d82SAndre Fischer {
8902c50d82SAndre Fischer }
9002c50d82SAndre Fischer
9102c50d82SAndre Fischer
operator ()(int nWidth,SdPage * pPage,::sd::PreviewRenderer & rRenderer)9202c50d82SAndre Fischer Image TemplatePreviewProvider::operator() (
9302c50d82SAndre Fischer int nWidth,
9402c50d82SAndre Fischer SdPage* pPage,
9502c50d82SAndre Fischer ::sd::PreviewRenderer& rRenderer)
9602c50d82SAndre Fischer {
9702c50d82SAndre Fischer // Unused parameters.
9802c50d82SAndre Fischer (void)nWidth;
9902c50d82SAndre Fischer (void)pPage;
10002c50d82SAndre Fischer (void)rRenderer;
10102c50d82SAndre Fischer
10202c50d82SAndre Fischer // Load the thumbnail from a template document.
10302c50d82SAndre Fischer uno::Reference<io::XInputStream> xIStream;
10402c50d82SAndre Fischer
10502c50d82SAndre Fischer uno::Reference< lang::XMultiServiceFactory > xServiceManager (
10602c50d82SAndre Fischer ::comphelper::getProcessServiceFactory());
10702c50d82SAndre Fischer if (xServiceManager.is())
10802c50d82SAndre Fischer {
10902c50d82SAndre Fischer try
11002c50d82SAndre Fischer {
11102c50d82SAndre Fischer uno::Reference<lang::XSingleServiceFactory> xStorageFactory(
11202c50d82SAndre Fischer xServiceManager->createInstance(
11302c50d82SAndre Fischer ::rtl::OUString::createFromAscii(
11402c50d82SAndre Fischer "com.sun.star.embed.StorageFactory")),
11502c50d82SAndre Fischer uno::UNO_QUERY);
11602c50d82SAndre Fischer
11702c50d82SAndre Fischer if (xStorageFactory.is())
11802c50d82SAndre Fischer {
11902c50d82SAndre Fischer uno::Sequence<uno::Any> aArgs (2);
12002c50d82SAndre Fischer aArgs[0] <<= msURL;
12102c50d82SAndre Fischer aArgs[1] <<= embed::ElementModes::READ;
12202c50d82SAndre Fischer uno::Reference<embed::XStorage> xDocStorage (
12302c50d82SAndre Fischer xStorageFactory->createInstanceWithArguments(aArgs),
12402c50d82SAndre Fischer uno::UNO_QUERY);
12502c50d82SAndre Fischer
12602c50d82SAndre Fischer try
12702c50d82SAndre Fischer {
12802c50d82SAndre Fischer if (xDocStorage.is())
12902c50d82SAndre Fischer {
13002c50d82SAndre Fischer uno::Reference<embed::XStorage> xStorage (
13102c50d82SAndre Fischer xDocStorage->openStorageElement(
13202c50d82SAndre Fischer ::rtl::OUString::createFromAscii("Thumbnails"),
13302c50d82SAndre Fischer embed::ElementModes::READ));
13402c50d82SAndre Fischer if (xStorage.is())
13502c50d82SAndre Fischer {
13602c50d82SAndre Fischer uno::Reference<io::XStream> xThumbnailCopy (
13702c50d82SAndre Fischer xStorage->cloneStreamElement(
13802c50d82SAndre Fischer ::rtl::OUString::createFromAscii(
13902c50d82SAndre Fischer "thumbnail.png")));
14002c50d82SAndre Fischer if (xThumbnailCopy.is())
14102c50d82SAndre Fischer xIStream = xThumbnailCopy->getInputStream();
14202c50d82SAndre Fischer }
14302c50d82SAndre Fischer }
14402c50d82SAndre Fischer }
14502c50d82SAndre Fischer catch (uno::Exception& rException)
14602c50d82SAndre Fischer {
14702c50d82SAndre Fischer OSL_TRACE (
14802c50d82SAndre Fischer "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
14902c50d82SAndre Fischer ::rtl::OUStringToOString(msURL,
15002c50d82SAndre Fischer RTL_TEXTENCODING_UTF8).getStr(),
15102c50d82SAndre Fischer ::rtl::OUStringToOString(rException.Message,
15202c50d82SAndre Fischer RTL_TEXTENCODING_UTF8).getStr());
15302c50d82SAndre Fischer }
15402c50d82SAndre Fischer
15502c50d82SAndre Fischer try
15602c50d82SAndre Fischer {
15702c50d82SAndre Fischer // An (older) implementation had a bug - The storage
15802c50d82SAndre Fischer // name was "Thumbnail" instead of "Thumbnails". The
15902c50d82SAndre Fischer // old name is still used as fallback but this code can
16002c50d82SAndre Fischer // be removed soon.
16102c50d82SAndre Fischer if ( ! xIStream.is())
16202c50d82SAndre Fischer {
16302c50d82SAndre Fischer uno::Reference<embed::XStorage> xStorage (
16402c50d82SAndre Fischer xDocStorage->openStorageElement(
16502c50d82SAndre Fischer ::rtl::OUString::createFromAscii("Thumbnail"),
16602c50d82SAndre Fischer embed::ElementModes::READ));
16702c50d82SAndre Fischer if (xStorage.is())
16802c50d82SAndre Fischer {
16902c50d82SAndre Fischer uno::Reference<io::XStream> xThumbnailCopy (
17002c50d82SAndre Fischer xStorage->cloneStreamElement(
17102c50d82SAndre Fischer ::rtl::OUString::createFromAscii(
17202c50d82SAndre Fischer "thumbnail.png")));
17302c50d82SAndre Fischer if (xThumbnailCopy.is())
17402c50d82SAndre Fischer xIStream = xThumbnailCopy->getInputStream();
17502c50d82SAndre Fischer }
17602c50d82SAndre Fischer }
17702c50d82SAndre Fischer }
17802c50d82SAndre Fischer catch (uno::Exception& rException)
17902c50d82SAndre Fischer {
18002c50d82SAndre Fischer OSL_TRACE (
18102c50d82SAndre Fischer "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
18202c50d82SAndre Fischer ::rtl::OUStringToOString(msURL,
18302c50d82SAndre Fischer RTL_TEXTENCODING_UTF8).getStr(),
18402c50d82SAndre Fischer ::rtl::OUStringToOString(rException.Message,
18502c50d82SAndre Fischer RTL_TEXTENCODING_UTF8).getStr());
18602c50d82SAndre Fischer }
18702c50d82SAndre Fischer }
18802c50d82SAndre Fischer }
18902c50d82SAndre Fischer catch (uno::Exception& rException)
19002c50d82SAndre Fischer {
19102c50d82SAndre Fischer OSL_TRACE (
192*4b37ad1dSmseidel "caught exception while trying to access thumbnail of %s: %s",
19302c50d82SAndre Fischer ::rtl::OUStringToOString(msURL,
19402c50d82SAndre Fischer RTL_TEXTENCODING_UTF8).getStr(),
19502c50d82SAndre Fischer ::rtl::OUStringToOString(rException.Message,
19602c50d82SAndre Fischer RTL_TEXTENCODING_UTF8).getStr());
19702c50d82SAndre Fischer }
19802c50d82SAndre Fischer }
19902c50d82SAndre Fischer
20002c50d82SAndre Fischer // Extract the image from the stream.
20102c50d82SAndre Fischer BitmapEx aThumbnail;
20202c50d82SAndre Fischer if (xIStream.is())
20302c50d82SAndre Fischer {
20402c50d82SAndre Fischer ::std::auto_ptr<SvStream> pStream (
20502c50d82SAndre Fischer ::utl::UcbStreamHelper::CreateStream (xIStream));
20602c50d82SAndre Fischer ::vcl::PNGReader aReader (*pStream);
20702c50d82SAndre Fischer aThumbnail = aReader.Read ();
20802c50d82SAndre Fischer }
20902c50d82SAndre Fischer
21002c50d82SAndre Fischer // Note that the preview is returned without scaling it to the desired
21102c50d82SAndre Fischer // width. This gives the caller the chance to take advantage of a
21202c50d82SAndre Fischer // possibly larger resolution then was asked for.
21302c50d82SAndre Fischer return aThumbnail;
21402c50d82SAndre Fischer }
21502c50d82SAndre Fischer
21602c50d82SAndre Fischer
GetCostIndex(void)21702c50d82SAndre Fischer int TemplatePreviewProvider::GetCostIndex (void)
21802c50d82SAndre Fischer {
21902c50d82SAndre Fischer return 10;
22002c50d82SAndre Fischer }
22102c50d82SAndre Fischer
22202c50d82SAndre Fischer
NeedsPageObject(void)22302c50d82SAndre Fischer bool TemplatePreviewProvider::NeedsPageObject (void)
22402c50d82SAndre Fischer {
22502c50d82SAndre Fischer return false;
22602c50d82SAndre Fischer }
22702c50d82SAndre Fischer
22802c50d82SAndre Fischer
22902c50d82SAndre Fischer //===== TemplatePageObjectProvider =============================================
23002c50d82SAndre Fischer
TemplatePageObjectProvider(const::rtl::OUString & rsURL)23102c50d82SAndre Fischer TemplatePageObjectProvider::TemplatePageObjectProvider (const ::rtl::OUString& rsURL)
23202c50d82SAndre Fischer : msURL(rsURL),
23302c50d82SAndre Fischer mxDocumentShell()
23402c50d82SAndre Fischer {
23502c50d82SAndre Fischer }
23602c50d82SAndre Fischer
23702c50d82SAndre Fischer
operator ()(SdDrawDocument * pContainerDocument)23802c50d82SAndre Fischer SdPage* TemplatePageObjectProvider::operator() (SdDrawDocument* pContainerDocument)
23902c50d82SAndre Fischer {
24002c50d82SAndre Fischer // Unused parameters.
24102c50d82SAndre Fischer (void)pContainerDocument;
24202c50d82SAndre Fischer
24302c50d82SAndre Fischer SdPage* pPage = NULL;
24402c50d82SAndre Fischer
24502c50d82SAndre Fischer mxDocumentShell = NULL;
24602c50d82SAndre Fischer ::sd::DrawDocShell* pDocumentShell = NULL;
24702c50d82SAndre Fischer try
24802c50d82SAndre Fischer {
24902c50d82SAndre Fischer // Load the template document and return its first page.
25002c50d82SAndre Fischer pDocumentShell = LoadDocument (msURL);
25102c50d82SAndre Fischer if (pDocumentShell != NULL)
25202c50d82SAndre Fischer {
25302c50d82SAndre Fischer SdDrawDocument* pDocument = pDocumentShell->GetDoc();
25402c50d82SAndre Fischer if (pDocument != NULL)
25502c50d82SAndre Fischer {
25602c50d82SAndre Fischer pPage = pDocument->GetMasterSdPage(0, PK_STANDARD);
25702c50d82SAndre Fischer // In order to make the newly loaded master page deletable
25802c50d82SAndre Fischer // when copied into documents it is marked as no "precious".
25902c50d82SAndre Fischer // When it is modified then it is marked as "precious".
26002c50d82SAndre Fischer if (pPage != NULL)
26102c50d82SAndre Fischer pPage->SetPrecious(false);
26202c50d82SAndre Fischer }
26302c50d82SAndre Fischer }
26402c50d82SAndre Fischer }
26502c50d82SAndre Fischer catch (uno::RuntimeException)
26602c50d82SAndre Fischer {
26702c50d82SAndre Fischer DBG_UNHANDLED_EXCEPTION();
26802c50d82SAndre Fischer pPage = NULL;
26902c50d82SAndre Fischer }
27002c50d82SAndre Fischer
27102c50d82SAndre Fischer return pPage;
27202c50d82SAndre Fischer }
27302c50d82SAndre Fischer
27402c50d82SAndre Fischer
LoadDocument(const::rtl::OUString & sFileName)27502c50d82SAndre Fischer ::sd::DrawDocShell* TemplatePageObjectProvider::LoadDocument (const ::rtl::OUString& sFileName)
27602c50d82SAndre Fischer {
27702c50d82SAndre Fischer SfxApplication* pSfxApp = SFX_APP();
27802c50d82SAndre Fischer SfxItemSet* pSet = new SfxAllItemSet (pSfxApp->GetPool());
27902c50d82SAndre Fischer pSet->Put (SfxBoolItem (SID_TEMPLATE, sal_True));
28002c50d82SAndre Fischer pSet->Put (SfxBoolItem (SID_PREVIEW, sal_True));
28102c50d82SAndre Fischer if (pSfxApp->LoadTemplate (mxDocumentShell, sFileName, sal_True, pSet))
28202c50d82SAndre Fischer {
28302c50d82SAndre Fischer mxDocumentShell = NULL;
28402c50d82SAndre Fischer }
28502c50d82SAndre Fischer SfxObjectShell* pShell = mxDocumentShell;
28602c50d82SAndre Fischer return PTR_CAST(::sd::DrawDocShell,pShell);
28702c50d82SAndre Fischer }
28802c50d82SAndre Fischer
28902c50d82SAndre Fischer
GetCostIndex(void)29002c50d82SAndre Fischer int TemplatePageObjectProvider::GetCostIndex (void)
29102c50d82SAndre Fischer {
29202c50d82SAndre Fischer return 20;
29302c50d82SAndre Fischer }
29402c50d82SAndre Fischer
29502c50d82SAndre Fischer
operator ==(const PageObjectProvider & rProvider)29602c50d82SAndre Fischer bool TemplatePageObjectProvider::operator== (const PageObjectProvider& rProvider)
29702c50d82SAndre Fischer {
29802c50d82SAndre Fischer const TemplatePageObjectProvider* pTemplatePageObjectProvider
29902c50d82SAndre Fischer = dynamic_cast<const TemplatePageObjectProvider*>(&rProvider);
30002c50d82SAndre Fischer if (pTemplatePageObjectProvider != NULL)
30102c50d82SAndre Fischer return (msURL == pTemplatePageObjectProvider->msURL);
30202c50d82SAndre Fischer else
30302c50d82SAndre Fischer return false;
30402c50d82SAndre Fischer }
30502c50d82SAndre Fischer
30602c50d82SAndre Fischer
30702c50d82SAndre Fischer //===== DefaultPageObjectProvider ==============================================
30802c50d82SAndre Fischer
DefaultPageObjectProvider(void)30902c50d82SAndre Fischer DefaultPageObjectProvider::DefaultPageObjectProvider (void)
31002c50d82SAndre Fischer {
31102c50d82SAndre Fischer }
31202c50d82SAndre Fischer
31302c50d82SAndre Fischer
operator ()(SdDrawDocument * pContainerDocument)31402c50d82SAndre Fischer SdPage* DefaultPageObjectProvider::operator () (SdDrawDocument* pContainerDocument)
31502c50d82SAndre Fischer {
31602c50d82SAndre Fischer SdPage* pLocalMasterPage = NULL;
31702c50d82SAndre Fischer if (pContainerDocument != NULL)
31802c50d82SAndre Fischer {
31902c50d82SAndre Fischer sal_Int32 nIndex (0);
32002c50d82SAndre Fischer SdPage* pLocalSlide = pContainerDocument->GetSdPage((sal_uInt16)nIndex, PK_STANDARD);
32102c50d82SAndre Fischer if (pLocalSlide!=NULL && pLocalSlide->TRG_HasMasterPage())
32202c50d82SAndre Fischer pLocalMasterPage = dynamic_cast<SdPage*>(&pLocalSlide->TRG_GetMasterPage());
32302c50d82SAndre Fischer }
32402c50d82SAndre Fischer
32502c50d82SAndre Fischer if (pLocalMasterPage == NULL)
32602c50d82SAndre Fischer {
32702c50d82SAndre Fischer DBG_ASSERT(false, "can not create master page for slide");
32802c50d82SAndre Fischer }
32902c50d82SAndre Fischer
33002c50d82SAndre Fischer return pLocalMasterPage;
33102c50d82SAndre Fischer }
33202c50d82SAndre Fischer
33302c50d82SAndre Fischer
GetCostIndex(void)33402c50d82SAndre Fischer int DefaultPageObjectProvider::GetCostIndex (void)
33502c50d82SAndre Fischer {
33602c50d82SAndre Fischer return 15;
33702c50d82SAndre Fischer }
33802c50d82SAndre Fischer
33902c50d82SAndre Fischer
operator ==(const PageObjectProvider & rProvider)34002c50d82SAndre Fischer bool DefaultPageObjectProvider::operator== (const PageObjectProvider& rProvider)
34102c50d82SAndre Fischer {
34202c50d82SAndre Fischer return (dynamic_cast<const DefaultPageObjectProvider*>(&rProvider) != NULL);
34302c50d82SAndre Fischer }
34402c50d82SAndre Fischer
34502c50d82SAndre Fischer
34602c50d82SAndre Fischer //===== ExistingPageProvider ==================================================
34702c50d82SAndre Fischer
ExistingPageProvider(SdPage * pPage)34802c50d82SAndre Fischer ExistingPageProvider::ExistingPageProvider (SdPage* pPage)
34902c50d82SAndre Fischer : mpPage(pPage)
35002c50d82SAndre Fischer {
35102c50d82SAndre Fischer }
35202c50d82SAndre Fischer
35302c50d82SAndre Fischer
operator ()(SdDrawDocument * pDocument)35402c50d82SAndre Fischer SdPage* ExistingPageProvider::operator() (SdDrawDocument* pDocument)
35502c50d82SAndre Fischer {
35602c50d82SAndre Fischer (void)pDocument; // Unused parameter.
35702c50d82SAndre Fischer
35802c50d82SAndre Fischer return mpPage;
35902c50d82SAndre Fischer }
36002c50d82SAndre Fischer
36102c50d82SAndre Fischer
GetCostIndex(void)36202c50d82SAndre Fischer int ExistingPageProvider::GetCostIndex (void)
36302c50d82SAndre Fischer {
36402c50d82SAndre Fischer return 0;
36502c50d82SAndre Fischer }
36602c50d82SAndre Fischer
36702c50d82SAndre Fischer
operator ==(const PageObjectProvider & rProvider)36802c50d82SAndre Fischer bool ExistingPageProvider::operator== (const PageObjectProvider& rProvider)
36902c50d82SAndre Fischer {
37002c50d82SAndre Fischer const ExistingPageProvider* pExistingPageProvider
37102c50d82SAndre Fischer = dynamic_cast<const ExistingPageProvider*>(&rProvider);
37202c50d82SAndre Fischer if (pExistingPageProvider != NULL)
37302c50d82SAndre Fischer return (mpPage == pExistingPageProvider->mpPage);
37402c50d82SAndre Fischer else
37502c50d82SAndre Fischer return false;
37602c50d82SAndre Fischer }
37702c50d82SAndre Fischer
37802c50d82SAndre Fischer } } // end of namespace sd::sidebar
379*4b37ad1dSmseidel
380*4b37ad1dSmseidel /* vim: set noet sw=4 ts=4: */
381