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