Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / vcl / workben / svpclient.cxx
blobfa28ecbae8d9b59118eca2ca680d070c9bc7fa02
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 <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/awt/ImageScaleMode.hpp>
24 #include <vcl/event.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/wrkwin.hxx>
27 #include <vcl/button.hxx>
28 #include <vcl/lstbox.hxx>
29 #include <vcl/imgctrl.hxx>
30 #include <vcl/bitmapex.hxx>
31 #include <tools/extendapplicationenvironment.hxx>
32 #include <tools/stream.hxx>
34 #include <rtl/strbuf.hxx>
35 #include <rtl/ustrbuf.hxx>
37 #include <math.h>
39 #include <comphelper/processfactory.hxx>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <cppuhelper/bootstrap.hxx>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 using namespace cppu;
52 using namespace comphelper;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
56 using ::rtl::OUString;
57 using ::rtl::OString;
58 using ::rtl::OUStringToOString;
59 using ::rtl::OStringToOUString;
60 using ::rtl::OUStringBuffer;
61 using ::rtl::OStringBuffer;
62 // -----------------------------------------------------------------------
64 // Forward declaration
65 void Main();
67 // -----------------------------------------------------------------------
69 SAL_IMPLEMENT_MAIN()
71 tools::extendApplicationEnvironment();
73 //-------------------------------------------------
74 // create the global service-manager
75 //-------------------------------------------------
76 Reference< XMultiServiceFactory > xFactory;
77 try
79 Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext();
80 xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY );
81 if( xFactory.is() )
82 setProcessServiceFactory( xFactory );
84 catch(const com::sun::star::uno::Exception&)
88 if( ! xFactory.is() )
90 fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
91 exit( 1 );
94 InitVCL();
95 ::Main();
96 DeInitVCL();
98 return 0;
101 // -----------------------------------------------------------------------
103 class MyWin : public WorkWindow
105 PushButton m_aListButton;
106 ListBox m_aSvpBitmaps;
107 ImageControl m_aImage;
108 PushButton m_aQuitButton;
109 public:
110 MyWin( Window* pParent, WinBits nWinStyle );
112 void MouseMove( const MouseEvent& rMEvt );
113 void MouseButtonDown( const MouseEvent& rMEvt );
114 void MouseButtonUp( const MouseEvent& rMEvt );
115 void KeyInput( const KeyEvent& rKEvt );
116 void KeyUp( const KeyEvent& rKEvt );
117 void Paint( const Rectangle& rRect );
118 void Resize();
120 sal_Bool Close();
122 void parseList( const rtl::OString& rList );
123 rtl::OString processCommand( const rtl::OString& rCommand );
125 DECL_LINK( ListHdl, Button* );
126 DECL_LINK( SelectHdl, ListBox* );
127 DECL_LINK( QuitHdl, Button* );
130 // -----------------------------------------------------------------------
132 void Main()
134 MyWin aMainWin( NULL, WB_STDWORK );
135 aMainWin.SetText( rtl::OUString( "SvpClient" ) );
136 aMainWin.Show();
138 Application::Execute();
141 // -----------------------------------------------------------------------
143 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
144 WorkWindow( pParent, nWinStyle ),
145 m_aListButton( this, 0 ),
146 m_aSvpBitmaps( this, WB_BORDER ),
147 m_aImage( this, WB_BORDER ),
148 m_aQuitButton( this, 0 )
150 m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
151 m_aListButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) );
152 m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) );
153 m_aListButton.Show();
155 m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
156 m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
157 m_aSvpBitmaps.Show();
159 m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
160 m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None );
161 m_aImage.Show();
163 m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
164 m_aQuitButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) );
165 m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) );
166 m_aQuitButton.Show();
169 sal_Bool MyWin::Close()
171 sal_Bool bRet = WorkWindow::Close();
172 if( bRet )
173 Application::Quit();
174 return bRet;
177 void MyWin::parseList( const rtl::OString& rList )
179 sal_Int32 nTokenPos = 0;
180 rtl::OUString aElementType;
181 m_aSvpBitmaps.Clear();
182 while( nTokenPos >= 0 )
184 rtl::OString aLine = rList.getToken( 0, '\n', nTokenPos );
185 if( ! aLine.getLength() || *aLine.getStr() == '#' )
186 continue;
188 if( aLine.compareTo( "ElementType: ", 13 ) == 0 )
189 aElementType = rtl::OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
190 else
192 rtl::OUStringBuffer aNewElement( 64 );
193 aNewElement.append( aElementType );
194 aNewElement.appendAscii( ": " );
195 aNewElement.append( rtl::OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
196 m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() );
201 rtl::OString MyWin::processCommand( const rtl::OString& rCommand )
203 static const char* pEnv = getenv("SVP_LISTENER_PORT");
204 rtl::OStringBuffer aAnswer;
205 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
206 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
207 if( nSocket >= 0)
209 struct sockaddr_in addr;
210 memset(&addr, 0, sizeof(struct sockaddr_in));
211 addr.sin_family = AF_INET;
212 addr.sin_port = htons(nPort);
213 addr.sin_addr.s_addr = INADDR_ANY;
214 if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) )
216 perror( "SvpElementContainer: connect() failed" );
217 close(nSocket);
219 else
221 write( nSocket, rCommand.getStr(), rCommand.getLength() );
222 write( nSocket, "\n", 1 );
223 char buf[256];
224 ssize_t nBytes = 0;
227 nBytes = read( nSocket, buf, sizeof(buf) );
228 aAnswer.append( buf, nBytes );
229 } while( nBytes == sizeof( buf ) );
232 else
233 perror( "SvpElementContainer: socket() failed\n" );
234 return aAnswer.makeStringAndClear();
237 IMPL_LINK( MyWin, ListHdl, Button*, )
239 parseList( processCommand( "list" ) );
240 return 0;
243 IMPL_LINK( MyWin, QuitHdl, Button*, )
245 processCommand( "quit" );
246 return 0;
249 IMPL_LINK( MyWin, SelectHdl, ListBox*, )
251 String aEntry = m_aSvpBitmaps.GetSelectEntry();
252 sal_uInt16 nPos = aEntry.SearchAscii( ": " );
253 if( nPos != STRING_NOTFOUND )
255 OStringBuffer aCommand( 64 );
256 aCommand.append( "get " );
257 aCommand.append( rtl::OUStringToOString( aEntry.Copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
258 OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
259 SvMemoryStream aStream( aAnswer.getLength() );
260 aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
261 aStream.Seek( STREAM_SEEK_TO_BEGIN );
262 Bitmap aBitmap;
263 aStream >> aBitmap;
264 fprintf( stderr, "got bitmap of size %ldx%ld\n",
265 sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
266 sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
267 Size aFixedSize( aBitmap.GetSizePixel() );
268 aFixedSize.Width() += 10;
269 aFixedSize.Height() += 10;
270 m_aImage.SetSizePixel( aFixedSize );
271 m_aImage.SetImage( Image( BitmapEx( aBitmap ) ) );
273 return 0;
276 // -----------------------------------------------------------------------
278 void MyWin::MouseMove( const MouseEvent& rMEvt )
280 WorkWindow::MouseMove( rMEvt );
283 // -----------------------------------------------------------------------
285 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
287 WorkWindow::MouseButtonDown( rMEvt );
290 // -----------------------------------------------------------------------
292 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
294 WorkWindow::MouseButtonUp( rMEvt );
297 // -----------------------------------------------------------------------
299 void MyWin::KeyInput( const KeyEvent& rKEvt )
301 WorkWindow::KeyInput( rKEvt );
304 // -----------------------------------------------------------------------
306 void MyWin::KeyUp( const KeyEvent& rKEvt )
308 WorkWindow::KeyUp( rKEvt );
311 // -----------------------------------------------------------------------
313 void MyWin::Paint( const Rectangle& rRect )
315 WorkWindow::Paint( rRect );
318 // -----------------------------------------------------------------------
320 void MyWin::Resize()
322 WorkWindow::Resize();
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */