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 "ImagePreparer.hxx"
21 :
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>
28 :
29 : #include <svl/itemset.hxx>
30 : #include <sfx2/docfile.hxx>
31 :
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>
41 :
42 : using namespace ::sd;
43 : using namespace ::rtl;
44 : using namespace ::osl;
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::uno;
47 :
48 0 : ImagePreparer::ImagePreparer(
49 : const uno::Reference<presentation::XSlideShowController>& rxController,
50 : Transmitter *aTransmitter )
51 : : xController( rxController ),
52 0 : pTransmitter( aTransmitter )
53 : {
54 : SAL_INFO( "sdremote", "ImagePreparer - start" );
55 0 : SetTimeout( 50 );
56 0 : mnSendingSlide = 0;
57 0 : Start();
58 0 : }
59 :
60 0 : ImagePreparer::~ImagePreparer()
61 : {
62 : SAL_INFO( "sdremote", "ImagePreparer - stop" );
63 0 : Stop();
64 0 : }
65 :
66 0 : void ImagePreparer::Timeout()
67 : {
68 0 : sal_uInt32 aSlides = xController->getSlideCount();
69 : SAL_INFO( "sdremote", "ImagePreparer " << xController->isRunning() <<
70 : " sending slide " << mnSendingSlide << " of " << aSlides );
71 0 : if ( xController->isRunning() && // not stopped/disposed of.
72 0 : mnSendingSlide < aSlides )
73 : {
74 0 : sendPreview( mnSendingSlide );
75 0 : sendNotes( mnSendingSlide );
76 0 : mnSendingSlide++;
77 0 : Start();
78 : }
79 : else
80 0 : Stop();
81 0 : }
82 :
83 0 : void ImagePreparer::sendPreview( sal_uInt32 aSlideNumber )
84 : {
85 : sal_uInt64 aSize;
86 : uno::Sequence<sal_Int8> aImageData = preparePreview( aSlideNumber, 320, 240,
87 0 : aSize );
88 0 : if ( !xController->isRunning() )
89 0 : return;
90 :
91 0 : OUStringBuffer aStrBuffer;
92 0 : ::sax::Converter::encodeBase64( aStrBuffer, aImageData );
93 :
94 : OString aEncodedShortString = OUStringToOString(
95 0 : aStrBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
96 :
97 : // Start the writing
98 0 : OStringBuffer aBuffer;
99 :
100 0 : aBuffer.append( "slide_preview\n" );
101 :
102 0 : aBuffer.append( OString::number( aSlideNumber ).getStr() );
103 0 : aBuffer.append( "\n" );
104 :
105 0 : aBuffer.append( aEncodedShortString.getStr() );
106 0 : aBuffer.append( "\n\n" );
107 : pTransmitter->addMessage( aBuffer.makeStringAndClear(),
108 0 : Transmitter::PRIORITY_LOW );
109 :
110 : }
111 :
112 0 : uno::Sequence<sal_Int8> ImagePreparer::preparePreview(
113 : sal_uInt32 aSlideNumber, sal_uInt32 aWidth, sal_uInt32 aHeight,
114 : sal_uInt64 &rSize )
115 : {
116 0 : OUString aFileURL;
117 0 : FileBase::createTempFile( 0, 0, &aFileURL );
118 :
119 : uno::Reference< drawing::XGraphicExportFilter > xFilter =
120 0 : drawing::GraphicExportFilter::create( ::comphelper::getProcessComponentContext() );
121 :
122 0 : if ( !xController->isRunning() )
123 0 : return uno::Sequence<sal_Int8>();
124 :
125 : uno::Reference< lang::XComponent > xSourceDoc(
126 0 : xController->getSlideByIndex( aSlideNumber ),
127 0 : uno::UNO_QUERY_THROW );
128 :
129 0 : xFilter->setSourceDocument( xSourceDoc );
130 :
131 0 : uno::Sequence< beans::PropertyValue > aFilterData(3);
132 :
133 0 : aFilterData[0].Name = "PixelWidth";
134 0 : aFilterData[0].Value <<= aWidth;
135 :
136 0 : aFilterData[1].Name = "PixelHeight";
137 0 : aFilterData[1].Value <<= aHeight;
138 :
139 0 : aFilterData[2].Name = "ColorMode";
140 0 : aFilterData[2].Value <<= sal_Int32(0); // 0: Color, 1: B&W
141 :
142 0 : uno::Sequence< beans::PropertyValue > aProps(3);
143 :
144 0 : aProps[0].Name = "MediaType";
145 0 : aProps[0].Value <<= OUString( "image/png" );
146 :
147 0 : aProps[1].Name = "URL";
148 0 : aProps[1].Value <<= aFileURL;
149 :
150 0 : aProps[2].Name = "FilterData";
151 0 : aProps[2].Value <<= aFilterData;
152 :
153 0 : xFilter->filter( aProps );
154 :
155 : // FIXME: error handling.
156 :
157 0 : File aFile( aFileURL );
158 0 : aFile.open(0);
159 : sal_uInt64 aRead;
160 0 : rSize = 0;
161 0 : aFile.getSize( rSize );
162 0 : uno::Sequence<sal_Int8> aContents( rSize );
163 :
164 0 : aFile.read( aContents.getArray(), rSize, aRead );
165 0 : aFile.close();
166 0 : File::remove( aFileURL );
167 0 : return aContents;
168 :
169 : }
170 :
171 0 : void ImagePreparer::sendNotes( sal_uInt32 aSlideNumber )
172 : {
173 :
174 0 : OString aNotes = prepareNotes( aSlideNumber );
175 :
176 0 : if ( aNotes.isEmpty() )
177 0 : return;
178 :
179 : // OUStringBuffer aStrBuffer;
180 : // ::sax::Converter::encodeBase64( aStrBuffer, aTemp );
181 :
182 : // OString aNotes = OUStringToOString(
183 : // aStrBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
184 :
185 0 : if ( !xController->isRunning() )
186 0 : return;
187 :
188 : // Start the writing
189 0 : OStringBuffer aBuffer;
190 :
191 0 : aBuffer.append( "slide_notes\n" );
192 :
193 0 : aBuffer.append( OString::number( aSlideNumber ).getStr() );
194 0 : aBuffer.append( "\n" );
195 :
196 0 : aBuffer.append( "<html><body>" );
197 0 : aBuffer.append( aNotes );
198 0 : aBuffer.append( "</body></html>" );
199 0 : aBuffer.append( "\n\n" );
200 : pTransmitter->addMessage( aBuffer.makeStringAndClear(),
201 0 : Transmitter::PRIORITY_LOW );
202 : }
203 :
204 : sal_Bool ExportTo( uno::Reference< drawing::XDrawPage>& aNotesPage, const OUString& aUrl );
205 :
206 : // Code copied from sdremote/source/presenter/PresenterNotesView.cxx
207 0 : OString ImagePreparer::prepareNotes( sal_uInt32 aSlideNumber )
208 : {
209 0 : OUStringBuffer aRet;
210 :
211 0 : if ( !xController->isRunning() )
212 0 : return "";
213 :
214 0 : uno::Reference<css::drawing::XDrawPage> aNotesPage;
215 : uno::Reference< drawing::XDrawPage > xSourceDoc(
216 0 : xController->getSlideByIndex( aSlideNumber ),
217 0 : uno::UNO_QUERY_THROW );
218 : uno::Reference<presentation::XPresentationPage> xPresentationPage(
219 0 : xSourceDoc, UNO_QUERY);
220 0 : if (xPresentationPage.is())
221 0 : aNotesPage = xPresentationPage->getNotesPage();
222 : else
223 0 : return "";
224 :
225 :
226 : static const OUString sNotesShapeName (
227 0 : "com.sun.star.presentation.NotesShape" );
228 : static const OUString sTextShapeName (
229 0 : "com.sun.star.drawing.TextShape" );
230 :
231 0 : uno::Reference<container::XIndexAccess> xIndexAccess ( aNotesPage, UNO_QUERY);
232 0 : if (xIndexAccess.is())
233 : {
234 :
235 : // Iterate over all shapes and find the one that holds the text.
236 0 : sal_Int32 nCount (xIndexAccess->getCount());
237 0 : for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
238 : {
239 :
240 : uno::Reference<lang::XServiceName> xServiceName (
241 0 : xIndexAccess->getByIndex(nIndex), UNO_QUERY);
242 0 : if (xServiceName.is()
243 0 : && xServiceName->getServiceName().equals(sNotesShapeName))
244 : {
245 0 : uno::Reference<text::XTextRange> xText (xServiceName, UNO_QUERY);
246 0 : if (xText.is())
247 : {
248 0 : aRet.append(xText->getString());
249 0 : aRet.append("<br/>");
250 0 : }
251 : }
252 : else
253 : {
254 : uno::Reference<drawing::XShapeDescriptor> xShapeDescriptor (
255 0 : xIndexAccess->getByIndex(nIndex), UNO_QUERY);
256 0 : if (xShapeDescriptor.is())
257 : {
258 0 : OUString sType (xShapeDescriptor->getShapeType());
259 0 : if (sType.equals(sNotesShapeName) || sType.equals(sTextShapeName))
260 : {
261 : uno::Reference<text::XTextRange> xText (
262 0 : xIndexAccess->getByIndex(nIndex), UNO_QUERY);
263 0 : if (xText.is())
264 : {
265 0 : aRet.append(xText->getString());
266 0 : aRet.append("<br/>");
267 0 : }
268 0 : }
269 0 : }
270 : }
271 0 : }
272 : }
273 : // Replace all newlines with <br\> tags
274 0 : for ( sal_Int32 i = 0; i < aRet.getLength(); i++ )
275 : {
276 0 : if ( aRet[i] == '\n' )
277 : {
278 0 : aRet[i]= '<';
279 0 : aRet.insert( i+1, "br/>" );
280 : }
281 : }
282 : return OUStringToOString(
283 0 : aRet.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
284 : }
285 :
286 0 : sal_Bool ExportTo( uno::Reference< drawing::XDrawPage>& aNotesPage, const OUString& aUrl )
287 : {
288 0 : OUString aFilterName( "XHTML Draw File" );
289 0 : uno::Reference< document::XExporter > xExporter;
290 :
291 : {
292 0 : uno::Reference< lang::XMultiServiceFactory > xMan = ::comphelper::getProcessServiceFactory();
293 : uno::Reference < lang::XMultiServiceFactory > xFilterFact (
294 0 : xMan->createInstance( "com.sun.star.document.FilterFactory" ), uno::UNO_QUERY );
295 :
296 0 : uno::Sequence < beans::PropertyValue > aProps;
297 0 : uno::Reference < container::XNameAccess > xFilters ( xFilterFact, uno::UNO_QUERY );
298 0 : if ( xFilters->hasByName( aFilterName ) )
299 0 : xFilters->getByName( aFilterName ) >>= aProps;
300 : else
301 0 : fprintf( stderr, "Couldn't find by name.\n" );
302 :
303 0 : OUString aFilterImplName;
304 0 : sal_Int32 nFilterProps = aProps.getLength();
305 0 : for ( sal_Int32 nFilterProp = 0; nFilterProp<nFilterProps; nFilterProp++ )
306 : {
307 0 : const beans::PropertyValue& rFilterProp = aProps[nFilterProp];
308 0 : if ( rFilterProp.Name.equalsAscii("FilterService") )
309 : {
310 0 : rFilterProp.Value >>= aFilterImplName;
311 0 : break;
312 : }
313 : }
314 :
315 0 : fprintf( stderr, "aName%s\n", OUStringToOString(aFilterImplName, RTL_TEXTENCODING_UTF8).getStr() );
316 0 : if ( !aFilterImplName.isEmpty() )
317 : {
318 : try{
319 0 : xExporter = uno::Reference< document::XExporter >
320 0 : ( xFilterFact->createInstanceWithArguments( aFilterName, uno::Sequence < uno::Any >() ), uno::UNO_QUERY );
321 0 : }catch(const uno::Exception&)
322 : {
323 0 : xExporter.clear();
324 0 : fprintf( stderr, "Couldn't create instance of filter.\n" );
325 : }
326 0 : }
327 : }
328 :
329 0 : if ( xExporter.is() )
330 : {
331 : try{
332 0 : uno::Reference< lang::XComponent > xComp( aNotesPage, uno::UNO_QUERY_THROW );
333 0 : uno::Reference< document::XFilter > xFilter( xExporter, uno::UNO_QUERY_THROW );
334 0 : xExporter->setSourceDocument( xComp );
335 :
336 0 : com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue > aOldArgs ( 2 );
337 0 : aOldArgs[0].Name = "FileName";
338 0 : aOldArgs[0].Value <<= OUString( aUrl );
339 0 : aOldArgs[1].Name = "FilterName";
340 0 : aOldArgs[1].Value <<= OUString("com.sun.star.documentconversion.XSLTFilter");
341 :
342 0 : SfxMedium rMedium( aUrl , STREAM_STD_WRITE );
343 :
344 0 : const com::sun::star::beans::PropertyValue * pOldValue = aOldArgs.getConstArray();
345 0 : com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue > aArgs ( aOldArgs.getLength() );
346 0 : com::sun::star::beans::PropertyValue * pNewValue = aArgs.getArray();
347 :
348 :
349 : // put in the REAL file name, and copy all PropertyValues
350 0 : const OUString sOutputStream ( "OutputStream" );
351 0 : const OUString sStream ( "StreamForOutput" );
352 0 : sal_Bool bHasOutputStream = sal_False;
353 0 : sal_Bool bHasStream = sal_False;
354 0 : sal_Bool bHasBaseURL = sal_False;
355 : sal_Int32 i;
356 0 : sal_Int32 nEnd = aOldArgs.getLength();
357 :
358 0 : for ( i = 0; i < nEnd; i++ )
359 : {
360 0 : pNewValue[i] = pOldValue[i];
361 0 : if ( pOldValue[i].Name == "FileName" )
362 0 : pNewValue[i].Value <<= OUString ( rMedium.GetName() );
363 0 : else if ( pOldValue[i].Name == sOutputStream )
364 0 : bHasOutputStream = sal_True;
365 0 : else if ( pOldValue[i].Name == sStream )
366 0 : bHasStream = sal_True;
367 0 : else if ( pOldValue[i].Name == "DocumentBaseURL" )
368 0 : bHasBaseURL = sal_True;
369 : }
370 :
371 0 : if ( !bHasOutputStream )
372 : {
373 0 : aArgs.realloc ( ++nEnd );
374 0 : aArgs[nEnd-1].Name = sOutputStream;
375 0 : aArgs[nEnd-1].Value <<= uno::Reference < io::XOutputStream > ( new utl::OOutputStreamWrapper ( *rMedium.GetOutStream() ) );
376 : }
377 :
378 : // add stream as well, for OOX export and maybe others
379 0 : if ( !bHasStream )
380 : {
381 0 : aArgs.realloc ( ++nEnd );
382 0 : aArgs[nEnd-1].Name = sStream;
383 0 : aArgs[nEnd-1].Value <<= com::sun::star::uno::Reference < com::sun::star::io::XStream > ( new utl::OStreamWrapper ( *rMedium.GetOutStream() ) );
384 : }
385 :
386 0 : if ( !bHasBaseURL )
387 : {
388 0 : aArgs.realloc ( ++nEnd );
389 0 : aArgs[nEnd-1].Name = "DocumentBaseURL";
390 0 : aArgs[nEnd-1].Value <<= rMedium.GetBaseURL( true );
391 : }
392 :
393 0 : return xFilter->filter( aArgs );
394 0 : }catch(const uno::Exception&)
395 : {}
396 : }
397 :
398 0 : return sal_False;
399 : }
400 :
401 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|