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