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 <comphelper/diagnose_ex.hxx>
39 #include <tools/extendapplicationenvironment.hxx>
40 #include <tools/stream.hxx>
42 #include <rtl/strbuf.hxx>
43 #include <rtl/ustrbuf.hxx>
44 #include <o3tl/string_view.hxx>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 using namespace ::com::sun::star::uno
;
56 using namespace ::com::sun::star::lang
;
59 // Forward declaration
66 tools::extendApplicationEnvironment();
68 // create the global service-manager
69 Reference
< XComponentContext
> xContext
= defaultBootstrap_InitialComponentContext();
70 Reference
< XMultiServiceFactory
> xServiceManager( xContext
->getServiceManager(), UNO_QUERY
);
72 if( !xServiceManager
.is() )
73 Application::Abort( "Failed to bootstrap" );
75 comphelper::setProcessServiceFactory( xServiceManager
);
81 catch (const Exception
&)
83 TOOLS_WARN_EXCEPTION("vcl", "Fatal");
86 catch (const std::exception
& e
)
88 SAL_WARN("vcl", "Fatal: " << e
.what());
97 class MyWin
: public WorkWindow
99 VclPtr
<PushButton
> m_aListButton
;
100 VclPtr
<ListBox
> m_aSvpBitmaps
;
101 VclPtr
<FixedImage
> m_aImage
;
102 VclPtr
<PushButton
> m_aQuitButton
;
104 MyWin( vcl::Window
* pParent
, WinBits nWinStyle
);
106 virtual bool Close() override
;
107 virtual ~MyWin() override
{ disposeOnce(); }
108 virtual void dispose() override
;
110 void parseList( std::string_view rList
);
111 static OString
processCommand( const OString
& rCommand
);
113 DECL_LINK( ListHdl
, Button
*, void );
114 DECL_LINK( SelectHdl
, ListBox
&, void );
115 DECL_STATIC_LINK( MyWin
, QuitHdl
, Button
*, void );
122 ScopedVclPtrInstance
< MyWin
> aMainWin( nullptr, WB_STDWORK
);
123 aMainWin
->SetText( "SvpClient" );
126 Application::Execute();
129 MyWin::MyWin( vcl::Window
* pParent
, WinBits nWinStyle
) :
130 WorkWindow( pParent
, nWinStyle
),
131 m_aListButton(VclPtr
<PushButton
>::Create(this, 0)),
132 m_aSvpBitmaps(VclPtr
<ListBox
>::Create(this, WB_BORDER
)),
133 m_aImage(VclPtr
<FixedImage
>::Create(this, WB_BORDER
)),
134 m_aQuitButton(VclPtr
<PushButton
>::Create(this, 0))
136 m_aListButton
->SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
137 m_aListButton
->SetText( "List Elements" );
138 m_aListButton
->SetClickHdl( LINK( this, MyWin
, ListHdl
) );
139 m_aListButton
->Show();
141 m_aSvpBitmaps
->SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
142 m_aSvpBitmaps
->SetSelectHdl( LINK( this, MyWin
, SelectHdl
) );
143 m_aSvpBitmaps
->Show();
145 m_aImage
->SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
148 m_aQuitButton
->SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
149 m_aQuitButton
->SetText( "Quit SVP server" );
150 m_aQuitButton
->SetClickHdl( LINK( this, MyWin
, QuitHdl
) );
151 m_aQuitButton
->Show();
156 bool bRet
= WorkWindow::Close();
162 void MyWin::dispose()
164 m_aListButton
.disposeAndClear();
165 m_aSvpBitmaps
.disposeAndClear();
166 m_aImage
.disposeAndClear();
167 m_aQuitButton
.disposeAndClear();
168 WorkWindow::dispose();
171 void MyWin::parseList( std::string_view rList
)
173 sal_Int32 nTokenPos
= 0;
174 OUString aElementType
;
175 m_aSvpBitmaps
->Clear();
176 while( nTokenPos
>= 0 )
178 std::string_view aLine
= o3tl::getToken(rList
, 0, '\n', nTokenPos
);
179 if( aLine
.empty() || aLine
[0] == '#' )
182 if( o3tl::starts_with(aLine
, "ElementType: " ) )
183 aElementType
= OStringToOUString( aLine
.substr( 13 ), RTL_TEXTENCODING_ASCII_US
);
186 OUString aNewElement
=
187 aElementType
+ ": " +
188 OStringToOUString( aLine
, RTL_TEXTENCODING_ASCII_US
);
189 m_aSvpBitmaps
->InsertEntry( aNewElement
);
194 OString
MyWin::processCommand( const OString
& rCommand
)
196 static const char* pEnv
= getenv("SVP_LISTENER_PORT");
197 OStringBuffer aAnswer
;
198 int nPort
= (pEnv
&& *pEnv
) ? atoi(pEnv
) : 8000;
199 int nSocket
= socket( PF_INET
, SOCK_STREAM
, 0 );
202 struct sockaddr_in addr
;
203 memset(&addr
, 0, sizeof(struct sockaddr_in
));
204 addr
.sin_family
= AF_INET
;
205 addr
.sin_port
= htons(nPort
);
206 addr
.sin_addr
.s_addr
= INADDR_ANY
;
207 if( connect( nSocket
, reinterpret_cast<sockaddr
*>(&addr
), sizeof(addr
) ) )
209 perror( "SvpElementContainer: connect() failed" );
214 ssize_t fd
= write( nSocket
, rCommand
.getStr(), rCommand
.getLength() );
217 SAL_WARN("vcl", "Connection closed on other end");
219 SAL_WARN("vcl", "Error writing to socket: " << strerror( errno
));
221 fd
= write( nSocket
, "\n", 1 );
224 SAL_WARN("vcl", "Connection closed on other end");
226 SAL_WARN("vcl", "Error writing to socket: " << strerror( errno
));
232 nBytes
= read( nSocket
, buf
, sizeof(buf
) );
233 aAnswer
.append( buf
, nBytes
);
234 } while( nBytes
== sizeof( buf
) );
239 perror( "SvpElementContainer: socket() failed\n" );
240 return aAnswer
.makeStringAndClear();
243 IMPL_LINK_NOARG( MyWin
, ListHdl
, Button
*, void)
245 parseList( processCommand( "list" ) );
248 IMPL_STATIC_LINK_NOARG( MyWin
, QuitHdl
, Button
*, void)
250 processCommand( "quit" );
253 IMPL_LINK_NOARG( MyWin
, SelectHdl
, ListBox
&, void)
255 OUString aEntry
= m_aSvpBitmaps
->GetSelectedEntry();
256 sal_Int32 nPos
= aEntry
.indexOf( ": " );
262 OUStringToOString( aEntry
.subView( nPos
+2 ), RTL_TEXTENCODING_ASCII_US
);
263 OString
aAnswer( processCommand( aCommand
) );
264 SvMemoryStream
aStream( aAnswer
.getLength() );
265 aStream
.WriteBytes( aAnswer
.getStr(), aAnswer
.getLength() );
266 aStream
.Seek( STREAM_SEEK_TO_BEGIN
);
268 Graphic aGraphicResult
;
269 GraphicFilter
&rFilter
= GraphicFilter::GetGraphicFilter();
270 rFilter
.ImportGraphic( aGraphicResult
, u
"import", aStream
);
272 BitmapEx aBitmap
= aGraphicResult
.GetBitmapEx();
274 SAL_INFO("vcl", "got bitmap of size " << aBitmap
.GetSizePixel().Width() << "x" << aBitmap
.GetSizePixel().Height());
275 Size
aFixedSize( aBitmap
.GetSizePixel() );
276 aFixedSize
.AdjustWidth(10 );
277 aFixedSize
.AdjustHeight(10 );
278 m_aImage
->SetSizePixel( aFixedSize
);
279 m_aImage
->SetImage( Image( aBitmap
) );
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */