1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/log.hxx>
23 #include <cppuhelper/bootstrap.hxx>
24 #include <comphelper/processfactory.hxx>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <vcl/event.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/wrkwin.hxx>
32 #include <vcl/toolkit/button.hxx>
33 #include <vcl/toolkit/fixed.hxx>
34 #include <vcl/toolkit/lstbox.hxx>
35 #include <vcl/bitmapex.hxx>
36 #include <vcl/graphicfilter.hxx>
37 #include <vcl/graph.hxx>
38 #include <tools/diagnose_ex.h>
39 #include <tools/extendapplicationenvironment.hxx>
40 #include <tools/stream.hxx>
42 #include <rtl/strbuf.hxx>
43 #include <rtl/ustrbuf.hxx>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
54 using namespace ::com::sun::star::uno
;
55 using namespace ::com::sun::star::lang
;
58 // Forward declaration
65 tools::extendApplicationEnvironment();
67 // create the global service-manager
68 Reference
< XComponentContext
> xContext
= defaultBootstrap_InitialComponentContext();
69 Reference
< XMultiServiceFactory
> xServiceManager( xContext
->getServiceManager(), UNO_QUERY
);
71 if( !xServiceManager
.is() )
72 Application::Abort( "Failed to bootstrap" );
74 comphelper::setProcessServiceFactory( xServiceManager
);
80 catch (const Exception
&)
82 TOOLS_WARN_EXCEPTION("vcl", "Fatal");
85 catch (const std::exception
& e
)
87 SAL_WARN("vcl", "Fatal: " << e
.what());
96 class MyWin
: public WorkWindow
98 VclPtr
<PushButton
> m_aListButton
;
99 VclPtr
<ListBox
> m_aSvpBitmaps
;
100 VclPtr
<FixedImage
> m_aImage
;
101 VclPtr
<PushButton
> m_aQuitButton
;
103 MyWin( vcl::Window
* pParent
, WinBits nWinStyle
);
105 virtual bool Close() override
;
106 virtual ~MyWin() override
{ disposeOnce(); }
107 virtual void dispose() override
;
109 void parseList( const OString
& rList
);
110 static OString
processCommand( const OString
& rCommand
);
112 DECL_LINK( ListHdl
, Button
*, void );
113 DECL_LINK( SelectHdl
, ListBox
&, void );
114 DECL_STATIC_LINK( MyWin
, QuitHdl
, Button
*, void );
121 ScopedVclPtrInstance
< MyWin
> aMainWin( nullptr, WB_STDWORK
);
122 aMainWin
->SetText( "SvpClient" );
125 Application::Execute();
128 MyWin::MyWin( vcl::Window
* pParent
, WinBits nWinStyle
) :
129 WorkWindow( pParent
, nWinStyle
),
130 m_aListButton(VclPtr
<PushButton
>::Create(this, 0)),
131 m_aSvpBitmaps(VclPtr
<ListBox
>::Create(this, WB_BORDER
)),
132 m_aImage(VclPtr
<FixedImage
>::Create(this, WB_BORDER
)),
133 m_aQuitButton(VclPtr
<PushButton
>::Create(this, 0))
135 m_aListButton
->SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
136 m_aListButton
->SetText( "List Elements" );
137 m_aListButton
->SetClickHdl( LINK( this, MyWin
, ListHdl
) );
138 m_aListButton
->Show();
140 m_aSvpBitmaps
->SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
141 m_aSvpBitmaps
->SetSelectHdl( LINK( this, MyWin
, SelectHdl
) );
142 m_aSvpBitmaps
->Show();
144 m_aImage
->SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
147 m_aQuitButton
->SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
148 m_aQuitButton
->SetText( "Quit SVP server" );
149 m_aQuitButton
->SetClickHdl( LINK( this, MyWin
, QuitHdl
) );
150 m_aQuitButton
->Show();
155 bool bRet
= WorkWindow::Close();
161 void MyWin::dispose()
163 m_aListButton
.disposeAndClear();
164 m_aSvpBitmaps
.disposeAndClear();
165 m_aImage
.disposeAndClear();
166 m_aQuitButton
.disposeAndClear();
167 WorkWindow::dispose();
170 void MyWin::parseList( const OString
& rList
)
172 sal_Int32 nTokenPos
= 0;
173 OUString aElementType
;
174 m_aSvpBitmaps
->Clear();
175 while( nTokenPos
>= 0 )
177 OString aLine
= rList
.getToken( 0, '\n', nTokenPos
);
178 if( ! aLine
.getLength() || *aLine
.getStr() == '#' )
181 if( aLine
.startsWith( "ElementType: " ) )
182 aElementType
= OStringToOUString( aLine
.subView( 13 ), RTL_TEXTENCODING_ASCII_US
);
185 OUString aNewElement
=
186 aElementType
+ ": " +
187 OStringToOUString( aLine
, RTL_TEXTENCODING_ASCII_US
);
188 m_aSvpBitmaps
->InsertEntry( aNewElement
);
193 OString
MyWin::processCommand( const OString
& rCommand
)
195 static const char* pEnv
= getenv("SVP_LISTENER_PORT");
196 OStringBuffer aAnswer
;
197 int nPort
= (pEnv
&& *pEnv
) ? atoi(pEnv
) : 8000;
198 int nSocket
= socket( PF_INET
, SOCK_STREAM
, 0 );
201 struct sockaddr_in addr
;
202 memset(&addr
, 0, sizeof(struct sockaddr_in
));
203 addr
.sin_family
= AF_INET
;
204 addr
.sin_port
= htons(nPort
);
205 addr
.sin_addr
.s_addr
= INADDR_ANY
;
206 if( connect( nSocket
, reinterpret_cast<sockaddr
*>(&addr
), sizeof(addr
) ) )
208 perror( "SvpElementContainer: connect() failed" );
213 ssize_t fd
= write( nSocket
, rCommand
.getStr(), rCommand
.getLength() );
216 SAL_WARN("vcl", "Connection closed on other end");
218 SAL_WARN("vcl", "Error writing to socket: " << strerror( errno
));
220 fd
= write( nSocket
, "\n", 1 );
223 SAL_WARN("vcl", "Connection closed on other end");
225 SAL_WARN("vcl", "Error writing to socket: " << strerror( errno
));
231 nBytes
= read( nSocket
, buf
, sizeof(buf
) );
232 aAnswer
.append( buf
, nBytes
);
233 } while( nBytes
== sizeof( buf
) );
238 perror( "SvpElementContainer: socket() failed\n" );
239 return aAnswer
.makeStringAndClear();
242 IMPL_LINK_NOARG( MyWin
, ListHdl
, Button
*, void)
244 parseList( processCommand( "list" ) );
247 IMPL_STATIC_LINK_NOARG( MyWin
, QuitHdl
, Button
*, void)
249 processCommand( "quit" );
252 IMPL_LINK_NOARG( MyWin
, SelectHdl
, ListBox
&, void)
254 OUString aEntry
= m_aSvpBitmaps
->GetSelectedEntry();
255 sal_Int32 nPos
= aEntry
.indexOf( ": " );
261 OUStringToOString( aEntry
.subView( nPos
+2 ), RTL_TEXTENCODING_ASCII_US
);
262 OString
aAnswer( processCommand( aCommand
) );
263 SvMemoryStream
aStream( aAnswer
.getLength() );
264 aStream
.WriteBytes( aAnswer
.getStr(), aAnswer
.getLength() );
265 aStream
.Seek( STREAM_SEEK_TO_BEGIN
);
267 Graphic aGraphicResult
;
268 GraphicFilter
&rFilter
= GraphicFilter::GetGraphicFilter();
269 rFilter
.ImportGraphic( aGraphicResult
, OUString("import"), aStream
);
271 BitmapEx aBitmap
= aGraphicResult
.GetBitmapEx();
273 SAL_INFO("vcl", "got bitmap of size " << aBitmap
.GetSizePixel().Width() << "x" << aBitmap
.GetSizePixel().Height());
274 Size
aFixedSize( aBitmap
.GetSizePixel() );
275 aFixedSize
.AdjustWidth(10 );
276 aFixedSize
.AdjustHeight(10 );
277 m_aImage
->SetSizePixel( aFixedSize
);
278 m_aImage
->SetImage( Image( aBitmap
) );
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */