bump product version to 6.4.0.3
[LibreOffice.git] / vcl / workben / svpclient.cxx
blob2396c6fa69f29e05c443c9622218eb9209ef57fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sal/main.h>
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/button.hxx>
33 #include <vcl/lstbox.hxx>
34 #include <vcl/fixed.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>
45 #include <math.h>
47 #include <errno.h>
48 #include <string.h>
49 #include <unistd.h>
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;
56 using namespace cppu;
58 // Forward declaration
59 static void Main();
61 SAL_IMPLEMENT_MAIN()
63 try
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 );
76 InitVCL();
77 ::Main();
78 DeInitVCL();
80 catch (const Exception&)
82 TOOLS_WARN_EXCEPTION("vcl", "Fatal");
83 return 1;
85 catch (const std::exception& e)
87 SAL_WARN("vcl", "Fatal: " << e.what());
88 return 1;
91 return 0;
94 class MyWin : public WorkWindow
96 VclPtr<PushButton> m_aListButton;
97 VclPtr<ListBox> m_aSvpBitmaps;
98 VclPtr<FixedImage> m_aImage;
99 VclPtr<PushButton> m_aQuitButton;
100 public:
101 MyWin( vcl::Window* pParent, WinBits nWinStyle );
103 virtual bool Close() override;
104 virtual ~MyWin() override { disposeOnce(); }
105 virtual void dispose() override;
107 void parseList( const OString& rList );
108 static OString processCommand( const OString& rCommand );
110 DECL_LINK( ListHdl, Button*, void );
111 DECL_LINK( SelectHdl, ListBox&, void );
112 DECL_STATIC_LINK( MyWin, QuitHdl, Button*, void );
115 void Main()
117 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_STDWORK );
118 aMainWin->SetText( "SvpClient" );
119 aMainWin->Show();
121 Application::Execute();
124 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
125 WorkWindow( pParent, nWinStyle ),
126 m_aListButton(VclPtr<PushButton>::Create(this, 0)),
127 m_aSvpBitmaps(VclPtr<ListBox>::Create(this, WB_BORDER)),
128 m_aImage(VclPtr<FixedImage>::Create(this, WB_BORDER)),
129 m_aQuitButton(VclPtr<PushButton>::Create(this, 0))
131 m_aListButton->SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
132 m_aListButton->SetText( "List Elements" );
133 m_aListButton->SetClickHdl( LINK( this, MyWin, ListHdl ) );
134 m_aListButton->Show();
136 m_aSvpBitmaps->SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
137 m_aSvpBitmaps->SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
138 m_aSvpBitmaps->Show();
140 m_aImage->SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
141 m_aImage->Show();
143 m_aQuitButton->SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
144 m_aQuitButton->SetText( "Quit SVP server" );
145 m_aQuitButton->SetClickHdl( LINK( this, MyWin, QuitHdl ) );
146 m_aQuitButton->Show();
149 bool MyWin::Close()
151 bool bRet = WorkWindow::Close();
152 if( bRet )
153 Application::Quit();
154 return bRet;
157 void MyWin::dispose()
159 m_aListButton.disposeAndClear();
160 m_aSvpBitmaps.disposeAndClear();
161 m_aImage.disposeAndClear();
162 m_aQuitButton.disposeAndClear();
163 WorkWindow::dispose();
166 void MyWin::parseList( const OString& rList )
168 sal_Int32 nTokenPos = 0;
169 OUString aElementType;
170 m_aSvpBitmaps->Clear();
171 while( nTokenPos >= 0 )
173 OString aLine = rList.getToken( 0, '\n', nTokenPos );
174 if( ! aLine.getLength() || *aLine.getStr() == '#' )
175 continue;
177 if( aLine.startsWith( "ElementType: " ) )
178 aElementType = OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
179 else
181 OUString aNewElement =
182 aElementType + ": " +
183 OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US );
184 m_aSvpBitmaps->InsertEntry( aNewElement );
189 OString MyWin::processCommand( const OString& rCommand )
191 static const char* pEnv = getenv("SVP_LISTENER_PORT");
192 OStringBuffer aAnswer;
193 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
194 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
195 if( nSocket >= 0)
197 struct sockaddr_in addr;
198 memset(&addr, 0, sizeof(struct sockaddr_in));
199 addr.sin_family = AF_INET;
200 addr.sin_port = htons(nPort);
201 addr.sin_addr.s_addr = INADDR_ANY;
202 if( connect( nSocket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr) ) )
204 perror( "SvpElementContainer: connect() failed" );
206 else
208 ssize_t nBytes = 0;
209 ssize_t fd = write( nSocket, rCommand.getStr(), rCommand.getLength() );
211 if (fd == 0)
212 SAL_WARN("vcl", "Connection closed on other end");
213 else if (fd < 0)
214 SAL_WARN("vcl", "Error writing to socket: " << strerror( errno ));
216 fd = write( nSocket, "\n", 1 );
218 if (fd == 0)
219 SAL_WARN("vcl", "Connection closed on other end");
220 else if (fd < 0)
221 SAL_WARN("vcl", "Error writing to socket: " << strerror( errno ));
224 char buf[256];
227 nBytes = read( nSocket, buf, sizeof(buf) );
228 aAnswer.append( buf, nBytes );
229 } while( nBytes == sizeof( buf ) );
231 close(nSocket);
233 else
234 perror( "SvpElementContainer: socket() failed\n" );
235 return aAnswer.makeStringAndClear();
238 IMPL_LINK_NOARG( MyWin, ListHdl, Button*, void)
240 parseList( processCommand( "list" ) );
243 IMPL_STATIC_LINK_NOARG( MyWin, QuitHdl, Button*, void)
245 processCommand( "quit" );
248 IMPL_LINK_NOARG( MyWin, SelectHdl, ListBox&, void)
250 OUString aEntry = m_aSvpBitmaps->GetSelectedEntry();
251 sal_Int32 nPos = aEntry.indexOf( ": " );
252 if( nPos == -1 )
253 return;
255 OString aCommand =
256 "get " +
257 OUStringToOString( aEntry.copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US );
258 OString aAnswer( processCommand( aCommand ) );
259 SvMemoryStream aStream( aAnswer.getLength() );
260 aStream.WriteBytes( aAnswer.getStr(), aAnswer.getLength() );
261 aStream.Seek( STREAM_SEEK_TO_BEGIN );
263 Graphic aGraphicResult;
264 GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
265 rFilter.ImportGraphic( aGraphicResult, OUString("import"), aStream );
267 BitmapEx aBitmap = aGraphicResult.GetBitmapEx();
269 SAL_INFO("vcl", "got bitmap of size " << aBitmap.GetSizePixel().Width() << "x" << aBitmap.GetSizePixel().Height());
270 Size aFixedSize( aBitmap.GetSizePixel() );
271 aFixedSize.AdjustWidth(10 );
272 aFixedSize.AdjustHeight(10 );
273 m_aImage->SetSizePixel( aFixedSize );
274 m_aImage->SetImage( Image( aBitmap ) );
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */