Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/main.h>
21 :
22 : #include <cppuhelper/bootstrap.hxx>
23 : #include <comphelper/processfactory.hxx>
24 :
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/uno/XComponentContext.hpp>
27 : #include <com/sun/star/awt/ImageScaleMode.hpp>
28 :
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>
40 :
41 : #include <rtl/strbuf.hxx>
42 : #include <rtl/ustrbuf.hxx>
43 :
44 : #include <math.h>
45 :
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>
52 :
53 : using namespace ::com::sun::star::uno;
54 : using namespace ::com::sun::star::lang;
55 : using namespace cppu;
56 :
57 : // Forward declaration
58 : void Main();
59 :
60 0 : SAL_IMPLEMENT_MAIN()
61 : {
62 0 : tools::extendApplicationEnvironment();
63 :
64 : // create the global service-manager
65 0 : Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
66 0 : Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
67 :
68 0 : if( !xServiceManager.is() )
69 0 : Application::Abort( "Failed to bootstrap" );
70 :
71 0 : comphelper::setProcessServiceFactory( xServiceManager );
72 :
73 0 : InitVCL();
74 0 : ::Main();
75 0 : DeInitVCL();
76 :
77 0 : return 0;
78 : }
79 :
80 0 : class MyWin : public WorkWindow
81 : {
82 : PushButton m_aListButton;
83 : ListBox m_aSvpBitmaps;
84 : ImageControl m_aImage;
85 : PushButton m_aQuitButton;
86 : public:
87 : MyWin( vcl::Window* pParent, WinBits nWinStyle );
88 :
89 : virtual void MouseMove( const MouseEvent& rMEvt ) SAL_OVERRIDE;
90 : virtual void MouseButtonDown( const MouseEvent& rMEvt ) SAL_OVERRIDE;
91 : virtual void MouseButtonUp( const MouseEvent& rMEvt ) SAL_OVERRIDE;
92 : virtual void KeyInput( const KeyEvent& rKEvt ) SAL_OVERRIDE;
93 : virtual void KeyUp( const KeyEvent& rKEvt ) SAL_OVERRIDE;
94 : virtual void Paint( const Rectangle& rRect ) SAL_OVERRIDE;
95 : virtual void Resize() SAL_OVERRIDE;
96 :
97 : virtual bool Close() SAL_OVERRIDE;
98 :
99 : void parseList( const OString& rList );
100 : OString processCommand( const OString& rCommand );
101 :
102 : DECL_LINK( ListHdl, Button* );
103 : DECL_LINK( SelectHdl, ListBox* );
104 : DECL_LINK( QuitHdl, Button* );
105 : };
106 :
107 0 : void Main()
108 : {
109 0 : MyWin aMainWin( NULL, WB_STDWORK );
110 0 : aMainWin.SetText( OUString( "SvpClient" ) );
111 0 : aMainWin.Show();
112 :
113 0 : Application::Execute();
114 0 : }
115 :
116 0 : MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
117 : WorkWindow( pParent, nWinStyle ),
118 : m_aListButton( this, 0 ),
119 : m_aSvpBitmaps( this, WB_BORDER ),
120 : m_aImage( this, WB_BORDER ),
121 0 : m_aQuitButton( this, 0 )
122 : {
123 0 : m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
124 0 : m_aListButton.SetText( OUString( "List Elements" ) );
125 0 : m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) );
126 0 : m_aListButton.Show();
127 :
128 0 : m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
129 0 : m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
130 0 : m_aSvpBitmaps.Show();
131 :
132 0 : m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
133 0 : m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::NONE );
134 0 : m_aImage.Show();
135 :
136 0 : m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
137 0 : m_aQuitButton.SetText( OUString( "Quit SVP server" ) );
138 0 : m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) );
139 0 : m_aQuitButton.Show();
140 0 : }
141 :
142 0 : bool MyWin::Close()
143 : {
144 0 : bool bRet = WorkWindow::Close();
145 0 : if( bRet )
146 0 : Application::Quit();
147 0 : return bRet;
148 : }
149 :
150 0 : void MyWin::parseList( const OString& rList )
151 : {
152 0 : sal_Int32 nTokenPos = 0;
153 0 : OUString aElementType;
154 0 : m_aSvpBitmaps.Clear();
155 0 : while( nTokenPos >= 0 )
156 : {
157 0 : OString aLine = rList.getToken( 0, '\n', nTokenPos );
158 0 : if( ! aLine.getLength() || *aLine.getStr() == '#' )
159 0 : continue;
160 :
161 0 : if( aLine.startsWith( "ElementType: " ) )
162 0 : aElementType = OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
163 : else
164 : {
165 0 : OUStringBuffer aNewElement( 64 );
166 0 : aNewElement.append( aElementType );
167 0 : aNewElement.appendAscii( ": " );
168 0 : aNewElement.append( OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
169 0 : m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() );
170 : }
171 0 : }
172 0 : }
173 :
174 0 : OString MyWin::processCommand( const OString& rCommand )
175 : {
176 0 : static const char* pEnv = getenv("SVP_LISTENER_PORT");
177 0 : OStringBuffer aAnswer;
178 0 : int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
179 0 : int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
180 0 : if( nSocket >= 0)
181 : {
182 : struct sockaddr_in addr;
183 0 : memset(&addr, 0, sizeof(struct sockaddr_in));
184 0 : addr.sin_family = AF_INET;
185 0 : addr.sin_port = htons(nPort);
186 0 : addr.sin_addr.s_addr = INADDR_ANY;
187 0 : if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) )
188 : {
189 0 : perror( "SvpElementContainer: connect() failed" );
190 : }
191 : else
192 : {
193 0 : ssize_t nBytes = 0;
194 0 : nBytes = write( nSocket, rCommand.getStr(), rCommand.getLength() );
195 0 : nBytes = write( nSocket, "\n", 1 );
196 0 : nBytes = 0;
197 : char buf[256];
198 0 : do
199 : {
200 0 : nBytes = read( nSocket, buf, sizeof(buf) );
201 0 : aAnswer.append( buf, nBytes );
202 0 : } while( nBytes == sizeof( buf ) );
203 : }
204 0 : close(nSocket);
205 : }
206 : else
207 0 : perror( "SvpElementContainer: socket() failed\n" );
208 0 : return aAnswer.makeStringAndClear();
209 : }
210 :
211 0 : IMPL_LINK( MyWin, ListHdl, Button*, )
212 : {
213 0 : parseList( processCommand( "list" ) );
214 0 : return 0;
215 : }
216 :
217 0 : IMPL_LINK( MyWin, QuitHdl, Button*, )
218 : {
219 0 : processCommand( "quit" );
220 0 : return 0;
221 : }
222 :
223 0 : IMPL_LINK( MyWin, SelectHdl, ListBox*, )
224 : {
225 0 : OUString aEntry = m_aSvpBitmaps.GetSelectEntry();
226 0 : sal_Int32 nPos = aEntry.indexOf( ": " );
227 0 : if( nPos != -1 )
228 : {
229 0 : OStringBuffer aCommand( 64 );
230 0 : aCommand.append( "get " );
231 0 : aCommand.append( OUStringToOString( aEntry.copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
232 0 : OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
233 0 : SvMemoryStream aStream( aAnswer.getLength() );
234 0 : aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
235 0 : aStream.Seek( STREAM_SEEK_TO_BEGIN );
236 :
237 0 : Graphic aGraphicResult;
238 0 : GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
239 0 : rFilter.ImportGraphic( aGraphicResult, OUString("import"), aStream );
240 :
241 0 : Bitmap aBitmap = aGraphicResult.GetBitmap();
242 :
243 : fprintf( stderr, "got bitmap of size %ldx%ld\n",
244 0 : sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
245 0 : sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
246 0 : Size aFixedSize( aBitmap.GetSizePixel() );
247 0 : aFixedSize.Width() += 10;
248 0 : aFixedSize.Height() += 10;
249 0 : m_aImage.SetSizePixel( aFixedSize );
250 0 : m_aImage.SetImage( Image( BitmapEx( aBitmap ) ) );
251 : }
252 0 : return 0;
253 : }
254 :
255 0 : void MyWin::MouseMove( const MouseEvent& rMEvt )
256 : {
257 0 : WorkWindow::MouseMove( rMEvt );
258 0 : }
259 :
260 0 : void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
261 : {
262 0 : WorkWindow::MouseButtonDown( rMEvt );
263 0 : }
264 :
265 0 : void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
266 : {
267 0 : WorkWindow::MouseButtonUp( rMEvt );
268 0 : }
269 :
270 0 : void MyWin::KeyInput( const KeyEvent& rKEvt )
271 : {
272 0 : WorkWindow::KeyInput( rKEvt );
273 0 : }
274 :
275 0 : void MyWin::KeyUp( const KeyEvent& rKEvt )
276 : {
277 0 : WorkWindow::KeyUp( rKEvt );
278 0 : }
279 :
280 0 : void MyWin::Paint( const Rectangle& rRect )
281 : {
282 0 : WorkWindow::Paint( rRect );
283 0 : }
284 :
285 0 : void MyWin::Resize()
286 : {
287 0 : WorkWindow::Resize();
288 0 : }
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|