summaryrefslogtreecommitdiff
path: root/vcl/workben/svpclient.cxx
blob: 74ccb9f9205f7b831f39b010967bc56aad53650d (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/main.h>
#include <sal/log.hxx>

#include <cppuhelper/bootstrap.hxx>
#include <comphelper/processfactory.hxx>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/awt/ImageScaleMode.hpp>

#include <vcl/event.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/button.hxx>
#include <vcl/lstbox.hxx>
#include <vcl/imgctrl.hxx>
#include <vcl/bitmapex.hxx>
#include <vcl/graphicfilter.hxx>
#include <vcl/graph.hxx>
#include <tools/diagnose_ex.h>
#include <tools/extendapplicationenvironment.hxx>
#include <tools/stream.hxx>

#include <rtl/strbuf.hxx>
#include <rtl/ustrbuf.hxx>

#include <math.h>

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace cppu;

// Forward declaration
static void Main();

SAL_IMPLEMENT_MAIN()
{
    try
    {
        tools::extendApplicationEnvironment();

        // create the global service-manager
        Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
        Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );

        if( !xServiceManager.is() )
            Application::Abort( "Failed to bootstrap" );

        comphelper::setProcessServiceFactory( xServiceManager );

        InitVCL();
        ::Main();
        DeInitVCL();
    }
    catch (const Exception&)
    {
        TOOLS_WARN_EXCEPTION("vcl", "Fatal");
        return 1;
    }
    catch (const std::exception& e)
    {
        SAL_WARN("vcl", "Fatal: " << e.what());
        return 1;
    }

    return 0;
}

class MyWin : public WorkWindow
{
    VclPtr<PushButton>   m_aListButton;
    VclPtr<ListBox>      m_aSvpBitmaps;
    VclPtr<ImageControl> m_aImage;
    VclPtr<PushButton>   m_aQuitButton;
public:
                 MyWin( vcl::Window* pParent, WinBits nWinStyle );

    virtual bool Close() override;
    virtual ~MyWin() override { disposeOnce(); }
    virtual void dispose() override;

    void parseList( const OString& rList );
    static OString processCommand( const OString& rCommand );

    DECL_LINK( ListHdl, Button*, void );
    DECL_LINK( SelectHdl, ListBox&, void );
    DECL_STATIC_LINK( MyWin, QuitHdl, Button*, void );
};

void Main()
{
    ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_STDWORK );
    aMainWin->SetText( "SvpClient" );
    aMainWin->Show();

    Application::Execute();
}

MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
    WorkWindow( pParent, nWinStyle ),
    m_aListButton(VclPtr<PushButton>::Create(this, 0)),
    m_aSvpBitmaps(VclPtr<ListBox>::Create(this, WB_BORDER)),
    m_aImage(VclPtr<ImageControl>::Create(this, WB_BORDER)),
    m_aQuitButton(VclPtr<PushButton>::Create(this, 0))
{
    m_aListButton->SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
    m_aListButton->SetText( "List Elements" );
    m_aListButton->SetClickHdl( LINK( this, MyWin, ListHdl ) );
    m_aListButton->Show();

    m_aSvpBitmaps->SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
    m_aSvpBitmaps->SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
    m_aSvpBitmaps->Show();

    m_aImage->SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
    m_aImage->SetScaleMode( css::awt::ImageScaleMode::NONE );
    m_aImage->Show();

    m_aQuitButton->SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
    m_aQuitButton->SetText( "Quit SVP server" );
    m_aQuitButton->SetClickHdl( LINK( this, MyWin, QuitHdl ) );
    m_aQuitButton->Show();
}

bool MyWin::Close()
{
    bool bRet = WorkWindow::Close();
    if( bRet )
        Application::Quit();
    return bRet;
}

void MyWin::dispose()
{
    m_aListButton.disposeAndClear();
    m_aSvpBitmaps.disposeAndClear();
    m_aImage.disposeAndClear();
    m_aQuitButton.disposeAndClear();
    WorkWindow::dispose();
}

void MyWin::parseList( const OString& rList )
{
    sal_Int32 nTokenPos = 0;
    OUString aElementType;
    m_aSvpBitmaps->Clear();
    while( nTokenPos >= 0 )
    {
        OString aLine = rList.getToken( 0, '\n', nTokenPos );
        if( ! aLine.getLength() || *aLine.getStr() == '#' )
            continue;

        if( aLine.startsWith( "ElementType: " ) )
            aElementType = OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
        else
        {
            OUString aNewElement =
                aElementType + ": " +
                OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US );
            m_aSvpBitmaps->InsertEntry( aNewElement );
        }
    }
}

OString MyWin::processCommand( const OString& rCommand )
{
    static const char* pEnv = getenv("SVP_LISTENER_PORT");
    OStringBuffer aAnswer;
    int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
    int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
    if( nSocket >= 0)
    {
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(struct sockaddr_in));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(nPort);
        addr.sin_addr.s_addr = INADDR_ANY;
        if( connect( nSocket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr) ) )
        {
            perror( "SvpElementContainer: connect() failed" );
        }
        else
        {
            ssize_t nBytes = 0;
            ssize_t fd = write( nSocket, rCommand.getStr(), rCommand.getLength() );

            if (fd == 0)
                SAL_WARN("vcl", "Connection closed on other end");
            else if (fd < 0)
                SAL_WARN("vcl", "Error writing to socket: " << strerror( errno ));

            fd = write( nSocket, "\n", 1 );

            if (fd == 0)
                SAL_WARN("vcl", "Connection closed on other end");
            else if (fd < 0)
                SAL_WARN("vcl", "Error writing to socket: " << strerror( errno ));


            char buf[256];
            do
            {
                nBytes = read( nSocket, buf, sizeof(buf) );
                aAnswer.append( buf, nBytes );
            } while( nBytes == sizeof( buf ) );
        }
        close(nSocket);
    }
    else
        perror( "SvpElementContainer: socket() failed\n" );
    return aAnswer.makeStringAndClear();
}

IMPL_LINK_NOARG( MyWin, ListHdl, Button*, void)
{
    parseList( processCommand( "list" ) );
}

IMPL_STATIC_LINK_NOARG( MyWin, QuitHdl, Button*, void)
{
    processCommand( "quit" );
}

IMPL_LINK_NOARG( MyWin, SelectHdl, ListBox&, void)
{
    OUString aEntry = m_aSvpBitmaps->GetSelectedEntry();
    sal_Int32 nPos = aEntry.indexOf( ": " );
    if( nPos == -1 )
        return;

    OString aCommand =
        "get " +
        OUStringToOString( aEntry.copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US );
    OString aAnswer( processCommand( aCommand ) );
    SvMemoryStream aStream( aAnswer.getLength() );
    aStream.WriteBytes( aAnswer.getStr(), aAnswer.getLength() );
    aStream.Seek( STREAM_SEEK_TO_BEGIN );

    Graphic aGraphicResult;
    GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
    rFilter.ImportGraphic( aGraphicResult, OUString("import"), aStream );

    BitmapEx aBitmap = aGraphicResult.GetBitmapEx();

    SAL_INFO("vcl", "got bitmap of size " << aBitmap.GetSizePixel().Width() << "x" << aBitmap.GetSizePixel().Height());
    Size aFixedSize( aBitmap.GetSizePixel() );
    aFixedSize.AdjustWidth(10 );
    aFixedSize.AdjustHeight(10 );
    m_aImage->SetSizePixel( aFixedSize );
    m_aImage->SetImage( Image( aBitmap ) );

}


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