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