bump product version to 5.0.4.1
[LibreOffice.git] / vcl / workben / svpclient.cxx
blobb0577d9e62e21dac00d289ac3af9f3fa8776d045
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>
22 #include <cppuhelper/bootstrap.hxx>
23 #include <comphelper/processfactory.hxx>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/awt/ImageScaleMode.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/imgctrl.hxx>
35 #include <vcl/bitmapex.hxx>
36 #include <vcl/graphicfilter.hxx>
37 #include <vcl/graph.hxx>
38 #include <tools/extendapplicationenvironment.hxx>
39 #include <tools/stream.hxx>
41 #include <rtl/strbuf.hxx>
42 #include <rtl/ustrbuf.hxx>
44 #include <math.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
55 using namespace cppu;
57 // Forward declaration
58 void Main();
60 SAL_IMPLEMENT_MAIN()
62 try
64 tools::extendApplicationEnvironment();
66 // create the global service-manager
67 Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
68 Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
70 if( !xServiceManager.is() )
71 Application::Abort( "Failed to bootstrap" );
73 comphelper::setProcessServiceFactory( xServiceManager );
75 InitVCL();
76 ::Main();
77 DeInitVCL();
79 catch (const Exception& e)
81 SAL_WARN("vcl.app", "Fatal exception: " << e.Message);
82 return 1;
84 catch (const std::exception& e)
86 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
87 return 1;
90 return 0;
93 class MyWin : public WorkWindow
95 VclPtr<PushButton> m_aListButton;
96 VclPtr<ListBox> m_aSvpBitmaps;
97 VclPtr<ImageControl> m_aImage;
98 VclPtr<PushButton> m_aQuitButton;
99 public:
100 MyWin( vcl::Window* pParent, WinBits nWinStyle );
102 virtual void MouseMove( const MouseEvent& rMEvt ) SAL_OVERRIDE;
103 virtual void MouseButtonDown( const MouseEvent& rMEvt ) SAL_OVERRIDE;
104 virtual void MouseButtonUp( const MouseEvent& rMEvt ) SAL_OVERRIDE;
105 virtual void KeyInput( const KeyEvent& rKEvt ) SAL_OVERRIDE;
106 virtual void KeyUp( const KeyEvent& rKEvt ) SAL_OVERRIDE;
107 virtual void Paint( vcl::RenderContext& /*rRenderContext*/, const Rectangle& rRect ) SAL_OVERRIDE;
108 virtual void Resize() SAL_OVERRIDE;
110 virtual bool Close() SAL_OVERRIDE;
111 virtual ~MyWin() { disposeOnce(); }
112 virtual void dispose() SAL_OVERRIDE;
114 void parseList( const OString& rList );
115 static OString processCommand( const OString& rCommand );
117 DECL_LINK( ListHdl, Button* );
118 DECL_LINK( SelectHdl, ListBox* );
119 DECL_STATIC_LINK( MyWin, QuitHdl, Button* );
122 void Main()
124 ScopedVclPtrInstance< MyWin > aMainWin( nullptr, WB_STDWORK );
125 aMainWin->SetText( OUString( "SvpClient" ) );
126 aMainWin->Show();
128 Application::Execute();
131 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
132 WorkWindow( pParent, nWinStyle ),
133 m_aListButton(VclPtr<PushButton>::Create(this, 0)),
134 m_aSvpBitmaps(VclPtr<ListBox>::Create(this, WB_BORDER)),
135 m_aImage(VclPtr<ImageControl>::Create(this, WB_BORDER)),
136 m_aQuitButton(VclPtr<PushButton>::Create(this, 0))
138 m_aListButton->SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
139 m_aListButton->SetText( OUString( "List Elements" ) );
140 m_aListButton->SetClickHdl( LINK( this, MyWin, ListHdl ) );
141 m_aListButton->Show();
143 m_aSvpBitmaps->SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
144 m_aSvpBitmaps->SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
145 m_aSvpBitmaps->Show();
147 m_aImage->SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
148 m_aImage->SetScaleMode( com::sun::star::awt::ImageScaleMode::NONE );
149 m_aImage->Show();
151 m_aQuitButton->SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
152 m_aQuitButton->SetText( OUString( "Quit SVP server" ) );
153 m_aQuitButton->SetClickHdl( LINK( this, MyWin, QuitHdl ) );
154 m_aQuitButton->Show();
157 bool MyWin::Close()
159 bool bRet = WorkWindow::Close();
160 if( bRet )
161 Application::Quit();
162 return bRet;
165 void MyWin::dispose()
167 m_aListButton.disposeAndClear();
168 m_aSvpBitmaps.disposeAndClear();
169 m_aImage.disposeAndClear();
170 m_aQuitButton.disposeAndClear();
171 WorkWindow::dispose();
174 void MyWin::parseList( const OString& rList )
176 sal_Int32 nTokenPos = 0;
177 OUString aElementType;
178 m_aSvpBitmaps->Clear();
179 while( nTokenPos >= 0 )
181 OString aLine = rList.getToken( 0, '\n', nTokenPos );
182 if( ! aLine.getLength() || *aLine.getStr() == '#' )
183 continue;
185 if( aLine.startsWith( "ElementType: " ) )
186 aElementType = OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
187 else
189 OUStringBuffer aNewElement( 64 );
190 aNewElement.append( aElementType );
191 aNewElement.appendAscii( ": " );
192 aNewElement.append( OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
193 m_aSvpBitmaps->InsertEntry( aNewElement.makeStringAndClear() );
198 OString MyWin::processCommand( const OString& rCommand )
200 static const char* pEnv = getenv("SVP_LISTENER_PORT");
201 OStringBuffer aAnswer;
202 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
203 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
204 if( nSocket >= 0)
206 struct sockaddr_in addr;
207 memset(&addr, 0, sizeof(struct sockaddr_in));
208 addr.sin_family = AF_INET;
209 addr.sin_port = htons(nPort);
210 addr.sin_addr.s_addr = INADDR_ANY;
211 if( connect( nSocket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr) ) )
213 perror( "SvpElementContainer: connect() failed" );
215 else
217 ssize_t nBytes = 0;
218 nBytes = write( nSocket, rCommand.getStr(), rCommand.getLength() );
219 nBytes = write( nSocket, "\n", 1 );
220 nBytes = 0;
221 char buf[256];
224 nBytes = read( nSocket, buf, sizeof(buf) );
225 aAnswer.append( buf, nBytes );
226 } while( nBytes == sizeof( buf ) );
228 close(nSocket);
230 else
231 perror( "SvpElementContainer: socket() failed\n" );
232 return aAnswer.makeStringAndClear();
235 IMPL_LINK( MyWin, ListHdl, Button*, )
237 parseList( processCommand( "list" ) );
238 return 0;
241 IMPL_STATIC_LINK( MyWin, QuitHdl, Button*, )
243 processCommand( "quit" );
244 return 0;
247 IMPL_LINK( MyWin, SelectHdl, ListBox*, )
249 OUString aEntry = m_aSvpBitmaps->GetSelectEntry();
250 sal_Int32 nPos = aEntry.indexOf( ": " );
251 if( nPos != -1 )
253 OStringBuffer aCommand( 64 );
254 aCommand.append( "get " );
255 aCommand.append( OUStringToOString( aEntry.copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
256 OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
257 SvMemoryStream aStream( aAnswer.getLength() );
258 aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
259 aStream.Seek( STREAM_SEEK_TO_BEGIN );
261 Graphic aGraphicResult;
262 GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
263 rFilter.ImportGraphic( aGraphicResult, OUString("import"), aStream );
265 Bitmap aBitmap = aGraphicResult.GetBitmap();
267 fprintf( stderr, "got bitmap of size %ldx%ld\n",
268 sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
269 sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
270 Size aFixedSize( aBitmap.GetSizePixel() );
271 aFixedSize.Width() += 10;
272 aFixedSize.Height() += 10;
273 m_aImage->SetSizePixel( aFixedSize );
274 m_aImage->SetImage( Image( BitmapEx( aBitmap ) ) );
276 return 0;
279 void MyWin::MouseMove( const MouseEvent& rMEvt )
281 WorkWindow::MouseMove( rMEvt );
284 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
286 WorkWindow::MouseButtonDown( rMEvt );
289 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
291 WorkWindow::MouseButtonUp( rMEvt );
294 void MyWin::KeyInput( const KeyEvent& rKEvt )
296 WorkWindow::KeyInput( rKEvt );
299 void MyWin::KeyUp( const KeyEvent& rKEvt )
301 WorkWindow::KeyUp( rKEvt );
304 void MyWin::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
306 WorkWindow::Paint(rRenderContext, rRect);
309 void MyWin::Resize()
311 WorkWindow::Resize();
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */