1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
39 #include <comphelper/processfactory.hxx>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <cppuhelper/bootstrap.hxx>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
52 using namespace comphelper
;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::lang
;
56 using ::rtl::OUString
;
58 using ::rtl::OUStringToOString
;
59 using ::rtl::OStringToOUString
;
60 using ::rtl::OUStringBuffer
;
61 using ::rtl::OStringBuffer
;
62 // -----------------------------------------------------------------------
64 // Forward declaration
67 // -----------------------------------------------------------------------
71 tools::extendApplicationEnvironment();
73 //-------------------------------------------------
74 // create the global service-manager
75 //-------------------------------------------------
76 Reference
< XMultiServiceFactory
> xFactory
;
79 Reference
< XComponentContext
> xCtx
= defaultBootstrap_InitialComponentContext();
80 xFactory
= Reference
< XMultiServiceFactory
>( xCtx
->getServiceManager(), UNO_QUERY
);
82 setProcessServiceFactory( xFactory
);
84 catch(const com::sun::star::uno::Exception
&)
90 fprintf( stderr
, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
101 // -----------------------------------------------------------------------
103 class MyWin
: public WorkWindow
105 PushButton m_aListButton
;
106 ListBox m_aSvpBitmaps
;
107 ImageControl m_aImage
;
108 PushButton m_aQuitButton
;
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
);
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 // -----------------------------------------------------------------------
134 MyWin
aMainWin( NULL
, WB_STDWORK
);
135 aMainWin
.SetText( rtl::OUString( "SvpClient" ) );
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
);
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();
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() == '#' )
188 if( aLine
.compareTo( "ElementType: ", 13 ) == 0 )
189 aElementType
= rtl::OStringToOUString( aLine
.copy( 13 ), RTL_TEXTENCODING_ASCII_US
);
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 );
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" );
221 write( nSocket
, rCommand
.getStr(), rCommand
.getLength() );
222 write( nSocket
, "\n", 1 );
227 nBytes
= read( nSocket
, buf
, sizeof(buf
) );
228 aAnswer
.append( buf
, nBytes
);
229 } while( nBytes
== sizeof( buf
) );
233 perror( "SvpElementContainer: socket() failed\n" );
234 return aAnswer
.makeStringAndClear();
237 IMPL_LINK( MyWin
, ListHdl
, Button
*, )
239 parseList( processCommand( "list" ) );
243 IMPL_LINK( MyWin
, QuitHdl
, Button
*, )
245 processCommand( "quit" );
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
);
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
) ) );
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 // -----------------------------------------------------------------------
322 WorkWindow::Resize();
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */