summaryrefslogtreecommitdiff
path: root/sd/source/ui/remotecontrol/Receiver.cxx
blob: 86a1a03decae6f4e98ab43514ff8ccd5d3f404c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include "Receiver.hxx"
#include <string.h>
#include <com/sun/star/presentation/XSlideShowController.hpp>
#include <com/sun/star/presentation/XPresentationSupplier.hpp>
#include <com/sun/star/presentation/XPresentation2.hpp>
#include <com/sun/star/frame/XFramesSupplier.hpp>
#include <com/sun/star/document/XFilter.hpp>
#include <com/sun/star/document/XExporter.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>

#include <comphelper/processfactory.hxx>
#include <osl/file.hxx>
#include <xmlsec/base64.h>
#include <rtl/ustrbuf.hxx>
#include <sax/tools/converter.hxx>
#include <rtl/strbuf.hxx>

using namespace sd;
using namespace ::com::sun::star;
using rtl::OUString;
using rtl::OString;
using namespace ::osl;
using namespace std;

Receiver::Receiver( Transmitter *aTransmitter )
{
    pTransmitter = aTransmitter;
}

Receiver::~Receiver()
{
}

void Receiver::parseCommand( std::vector<OString> aCommand )
{
    uno::Reference<presentation::XSlideShowController> xSlideShowController;
    uno::Reference<presentation::XPresentation2> xPresentation;
    try {
        uno::Reference< lang::XMultiServiceFactory > xServiceManager(
            ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
        uno::Reference< frame::XFramesSupplier > xFramesSupplier( xServiceManager->createInstance(
        "com.sun.star.frame.Desktop" ) , uno::UNO_QUERY_THROW );
        uno::Reference< frame::XFrame > xFrame ( xFramesSupplier->getActiveFrame(), uno::UNO_QUERY_THROW );
        uno::Reference<presentation::XPresentationSupplier> xPS ( xFrame->getController()->getModel(), uno::UNO_QUERY_THROW);
        xPresentation = uno::Reference<presentation::XPresentation2>(
            xPS->getPresentation(), uno::UNO_QUERY_THROW);
        // Throws an exception if now slideshow running
        xSlideShowController =  uno::Reference<presentation::XSlideShowController>(
           xPresentation->getController(), uno::UNO_QUERY_THROW );
    }
    catch ( com::sun::star::uno::RuntimeException &e )
    {
        fprintf( stderr, "Error in retrieving Controller\n" );
        //return;
    }

    if ( aCommand[0].equals( "transition_next" ) )
    {
        if ( xSlideShowController.is() )
            xSlideShowController->gotoNextEffect();
    }
    else if ( aCommand[0].equals( "transition_previous" ) )
    {
        if ( xSlideShowController.is() )
            xSlideShowController->gotoPreviousEffect();
    }
    else if ( aCommand[0].equals( "goto_slide" ) )
    {
        // FIXME: if 0 returned, then not a valid number
        sal_Int32 aSlide = aCommand[1].toInt32();
        if ( xSlideShowController.is() )
            xSlideShowController->gotoSlideIndex( aSlide );
    }
    else if ( aCommand[0].equals( "presentation_start" ) )
    {
        if ( xPresentation.is() )
            xPresentation->start();
    }
    else if ( aCommand[0].equals( "presentation_stop" ) )
    {
        if ( xPresentation.is() )
            xPresentation->end();
    }
    else if ( aCommand[0].equals( "presentation_blank_screen" ) )
    {
        sal_Int32 aColour = 0; // Default is black
        if ( aCommand.size() > 1 )
        {
//             aColour = FIXME: get the colour in some format from this string
//              Determine the formatting first.
        }
        if ( xSlideShowController.is() )
        {
            xSlideShowController->blankScreen( aColour );
        }
    }
    else if ( aCommand[0].equals( "presentation_resume" ) )
    {
        if ( xSlideShowController.is() )
        {
            xSlideShowController->resume();
        }
    }
            // FIXME: remove later, this is just to test functionality
//     sendPreview( 0, xSlideShowController, mTransmitter );

}

void sendPreview( sal_uInt32 aSlideNumber,
                 const uno::Reference<presentation::XSlideShowController>& xSlideShowController, Transmitter *aTransmitter )
{

    sal_uInt64 aSize; // Unused
    uno::Sequence<sal_Int8> aImageData = preparePreview( aSlideNumber, xSlideShowController, 320, 240, aSize );
    rtl::OUStringBuffer aStrBuffer;
    ::sax::Converter::encodeBase64( aStrBuffer, aImageData );

    OString aEncodedShortString = rtl::OUStringToOString(
        aStrBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );

    // Start the writing
    rtl::OStringBuffer aBuffer;

    aBuffer.append( "slide_preview\n" );

    rtl::OString aSlideNumberString( rtl::OString::valueOf( sal_Int32(2) ) ); // FIXME get number
    aBuffer.append( aSlideNumberString.getStr() );
    aBuffer.append( "\n" );

    aBuffer.append( aEncodedShortString.getStr() );
    aBuffer.append( "\n\n" );
    aTransmitter->addMessage( aBuffer.makeStringAndClear(), Transmitter::Priority::LOW );

}

uno::Sequence<sal_Int8>
preparePreview(sal_uInt32 aSlideNumber,
               const uno::Reference<presentation::XSlideShowController>& xSlideShowController,
               sal_uInt32 aWidth, sal_uInt32 aHeight, sal_uInt64 &rSize )
{
    (void)aWidth; (void)aHeight; // FIXME: remove me when I'm used
    // Create temp file
    OUString aFileURL;
    FileBase::createTempFile( 0, 0, &aFileURL );

    uno::Reference< lang::XMultiServiceFactory > xServiceManager(
            ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );

    uno::Reference< document::XFilter > xFilter( xServiceManager->createInstance(
        "com.sun.star.drawing.GraphicExportFilter"  ) , uno::UNO_QUERY_THROW );

    uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY_THROW );

    uno::Reference< lang::XComponent > xSourceDoc(
        xSlideShowController->getSlideByIndex( aSlideNumber ) , uno::UNO_QUERY_THROW );

    xExporter->setSourceDocument( xSourceDoc );

    uno::Sequence< beans::PropertyValue > aFilterData(3);
    aFilterData[0].Name = "PixelWidth";
    aFilterData[0].Value <<= sal_Int32(2000);
    aFilterData[1].Name = "PixelHeight";
    aFilterData[1].Value <<= sal_Int32(2000);

    // Add quality if jpg "Quality" [1-100]
    // FIXME: is setting color mode needed.
    aFilterData[2].Name = "ColorMode";
    aFilterData[2].Value <<= sal_Int32(0); // Color

    uno::Sequence< beans::PropertyValue > aProps(3);
    aProps[0].Name = "MediaType";
    aProps[0].Value <<= OUString( "image/png" );

    aProps[1].Name = "URL";
    aProps[1].Value <<= aFileURL;

    aProps[2].Name = "FilterData";
    aProps[2].Value <<= aFilterData;

    xFilter->filter( aProps );

    // FIXME: error handling.

    File aFile( aFileURL );
    aFile.open(0);
    sal_uInt64 aRead;
    rSize = 0;
    aFile.getSize( rSize );
    uno::Sequence<sal_Int8> aContents( rSize );

    aFile.read( aContents.getArray(), rSize, aRead );
    aFile.close();
    File::remove( aFileURL );
    return aContents;

}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */