bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / remotecontrol / ImagePreparer.cxx
blob7de0bf84fccd1d77137cc6ffa91414531fc79be6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ImagePreparer.hxx"
22 #include <comphelper/processfactory.hxx>
23 #include <osl/file.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <sax/tools/converter.hxx>
26 #include <rtl/strbuf.hxx>
27 #include <unotools/streamwrap.hxx>
29 #include <svl/itemset.hxx>
30 #include <sfx2/docfile.hxx>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/document/XFilter.hpp>
35 #include <com/sun/star/document/XImporter.hpp>
36 #include <com/sun/star/document/XExporter.hpp>
37 #include <com/sun/star/drawing/GraphicExportFilter.hpp>
38 #include <com/sun/star/lang/XServiceName.hpp>
39 #include <com/sun/star/presentation/XPresentationPage.hpp>
40 #include <com/sun/star/text/XTextRange.hpp>
42 using namespace ::sd;
43 using namespace ::osl;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
47 ImagePreparer::ImagePreparer(
48 const uno::Reference<presentation::XSlideShowController>& rxController,
49 Transmitter *aTransmitter )
50 : xController( rxController ),
51 pTransmitter( aTransmitter )
53 SAL_INFO( "sdremote", "ImagePreparer - start" );
54 SetTimeout( 50 );
55 mnSendingSlide = 0;
56 Start();
59 ImagePreparer::~ImagePreparer()
61 SAL_INFO( "sdremote", "ImagePreparer - stop" );
62 Stop();
65 void ImagePreparer::Invoke()
67 sal_uInt32 aSlides = xController->getSlideCount();
68 SAL_INFO( "sdremote", "ImagePreparer " << xController->isRunning() <<
69 " sending slide " << mnSendingSlide << " of " << aSlides );
70 if ( xController->isRunning() && // not stopped/disposed of.
71 mnSendingSlide < aSlides )
73 sendPreview( mnSendingSlide );
74 sendNotes( mnSendingSlide );
75 mnSendingSlide++;
76 Start();
78 else
79 Stop();
82 void ImagePreparer::sendPreview( sal_uInt32 aSlideNumber )
84 sal_uInt64 aSize;
85 uno::Sequence<sal_Int8> aImageData = preparePreview( aSlideNumber, 320, 240,
86 aSize );
87 if ( !xController->isRunning() )
88 return;
90 OUStringBuffer aStrBuffer;
91 ::sax::Converter::encodeBase64( aStrBuffer, aImageData );
93 OString aEncodedShortString = OUStringToOString(
94 aStrBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
96 // Start the writing
97 OStringBuffer aBuffer;
99 aBuffer.append( "slide_preview\n" );
101 aBuffer.append( OString::number( aSlideNumber ).getStr() );
102 aBuffer.append( "\n" );
104 aBuffer.append( aEncodedShortString.getStr() );
105 aBuffer.append( "\n\n" );
106 pTransmitter->addMessage( aBuffer.makeStringAndClear(),
107 Transmitter::PRIORITY_LOW );
111 uno::Sequence<sal_Int8> ImagePreparer::preparePreview(
112 sal_uInt32 aSlideNumber, sal_uInt32 aWidth, sal_uInt32 aHeight,
113 sal_uInt64 &rSize )
115 OUString aFileURL;
116 FileBase::createTempFile( 0, 0, &aFileURL );
118 uno::Reference< drawing::XGraphicExportFilter > xFilter =
119 drawing::GraphicExportFilter::create( ::comphelper::getProcessComponentContext() );
121 if ( !xController->isRunning() )
122 return uno::Sequence<sal_Int8>();
124 uno::Reference< lang::XComponent > xSourceDoc(
125 xController->getSlideByIndex( aSlideNumber ),
126 uno::UNO_QUERY_THROW );
128 xFilter->setSourceDocument( xSourceDoc );
130 uno::Sequence< beans::PropertyValue > aFilterData(3);
132 aFilterData[0].Name = "PixelWidth";
133 aFilterData[0].Value <<= aWidth;
135 aFilterData[1].Name = "PixelHeight";
136 aFilterData[1].Value <<= aHeight;
138 aFilterData[2].Name = "ColorMode";
139 aFilterData[2].Value <<= sal_Int32(0); // 0: Color, 1: B&W
141 uno::Sequence< beans::PropertyValue > aProps(3);
143 aProps[0].Name = "MediaType";
144 aProps[0].Value <<= OUString( "image/png" );
146 aProps[1].Name = "URL";
147 aProps[1].Value <<= aFileURL;
149 aProps[2].Name = "FilterData";
150 aProps[2].Value <<= aFilterData;
152 xFilter->filter( aProps );
154 // FIXME: error handling.
156 File aFile( aFileURL );
157 aFile.open(0);
158 sal_uInt64 aRead;
159 rSize = 0;
160 aFile.getSize( rSize );
161 uno::Sequence<sal_Int8> aContents( rSize );
163 aFile.read( aContents.getArray(), rSize, aRead );
164 aFile.close();
165 File::remove( aFileURL );
166 return aContents;
170 void ImagePreparer::sendNotes( sal_uInt32 aSlideNumber )
173 OString aNotes = prepareNotes( aSlideNumber );
175 if ( aNotes.isEmpty() )
176 return;
178 // OUStringBuffer aStrBuffer;
179 // ::sax::Converter::encodeBase64( aStrBuffer, aTemp );
181 // OString aNotes = OUStringToOString(
182 // aStrBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
184 if ( !xController->isRunning() )
185 return;
187 // Start the writing
188 OStringBuffer aBuffer;
190 aBuffer.append( "slide_notes\n" );
192 aBuffer.append( OString::number( aSlideNumber ).getStr() );
193 aBuffer.append( "\n" );
195 aBuffer.append( "<html><body>" );
196 aBuffer.append( aNotes );
197 aBuffer.append( "</body></html>" );
198 aBuffer.append( "\n\n" );
199 pTransmitter->addMessage( aBuffer.makeStringAndClear(),
200 Transmitter::PRIORITY_LOW );
203 // Code copied from sdremote/source/presenter/PresenterNotesView.cxx
204 OString ImagePreparer::prepareNotes( sal_uInt32 aSlideNumber )
206 OUStringBuffer aRet;
208 if ( !xController->isRunning() )
209 return "";
211 uno::Reference<css::drawing::XDrawPage> aNotesPage;
212 uno::Reference< drawing::XDrawPage > xSourceDoc(
213 xController->getSlideByIndex( aSlideNumber ),
214 uno::UNO_QUERY_THROW );
215 uno::Reference<presentation::XPresentationPage> xPresentationPage(
216 xSourceDoc, UNO_QUERY);
217 if (xPresentationPage.is())
218 aNotesPage = xPresentationPage->getNotesPage();
219 else
220 return "";
222 static const OUString sNotesShapeName (
223 "com.sun.star.presentation.NotesShape" );
224 static const OUString sTextShapeName (
225 "com.sun.star.drawing.TextShape" );
227 uno::Reference<container::XIndexAccess> xIndexAccess ( aNotesPage, UNO_QUERY);
228 if (xIndexAccess.is())
231 // Iterate over all shapes and find the one that holds the text.
232 sal_Int32 nCount (xIndexAccess->getCount());
233 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
236 uno::Reference<lang::XServiceName> xServiceName (
237 xIndexAccess->getByIndex(nIndex), UNO_QUERY);
238 if (xServiceName.is()
239 && xServiceName->getServiceName().equals(sNotesShapeName))
241 uno::Reference<text::XTextRange> xText (xServiceName, UNO_QUERY);
242 if (xText.is())
244 aRet.append(xText->getString());
245 aRet.append("<br/>");
248 else
250 uno::Reference<drawing::XShapeDescriptor> xShapeDescriptor (
251 xIndexAccess->getByIndex(nIndex), UNO_QUERY);
252 if (xShapeDescriptor.is())
254 OUString sType (xShapeDescriptor->getShapeType());
255 if (sType.equals(sNotesShapeName) || sType.equals(sTextShapeName))
257 uno::Reference<text::XTextRange> xText (
258 xIndexAccess->getByIndex(nIndex), UNO_QUERY);
259 if (xText.is())
261 aRet.append(xText->getString());
262 aRet.append("<br/>");
269 // Replace all newlines with <br\> tags
270 for ( sal_Int32 i = 0; i < aRet.getLength(); i++ )
272 if ( aRet[i] == '\n' )
274 aRet[i]= '<';
275 aRet.insert( i+1, "br/>" );
278 return OUStringToOString(
279 aRet.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */