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
;
57 // Forward declaration
62 tools::extendApplicationEnvironment();
64 // create the global service-manager
65 Reference
< XMultiServiceFactory
> xFactory
;
68 Reference
< XComponentContext
> xCtx
= defaultBootstrap_InitialComponentContext();
69 xFactory
= Reference
< XMultiServiceFactory
>( xCtx
->getServiceManager(), UNO_QUERY
);
71 setProcessServiceFactory( xFactory
);
73 catch(const com::sun::star::uno::Exception
&)
79 fprintf( stderr
, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
90 class MyWin
: public WorkWindow
92 PushButton m_aListButton
;
93 ListBox m_aSvpBitmaps
;
94 ImageControl m_aImage
;
95 PushButton m_aQuitButton
;
97 MyWin( Window
* pParent
, WinBits nWinStyle
);
99 void MouseMove( const MouseEvent
& rMEvt
);
100 void MouseButtonDown( const MouseEvent
& rMEvt
);
101 void MouseButtonUp( const MouseEvent
& rMEvt
);
102 void KeyInput( const KeyEvent
& rKEvt
);
103 void KeyUp( const KeyEvent
& rKEvt
);
104 void Paint( const Rectangle
& rRect
);
109 void parseList( const OString
& rList
);
110 OString
processCommand( const OString
& rCommand
);
112 DECL_LINK( ListHdl
, Button
* );
113 DECL_LINK( SelectHdl
, ListBox
* );
114 DECL_LINK( QuitHdl
, Button
* );
119 MyWin
aMainWin( NULL
, WB_STDWORK
);
120 aMainWin
.SetText( OUString( "SvpClient" ) );
123 Application::Execute();
126 MyWin::MyWin( Window
* pParent
, WinBits nWinStyle
) :
127 WorkWindow( pParent
, nWinStyle
),
128 m_aListButton( this, 0 ),
129 m_aSvpBitmaps( this, WB_BORDER
),
130 m_aImage( this, WB_BORDER
),
131 m_aQuitButton( this, 0 )
133 m_aListButton
.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
134 m_aListButton
.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) );
135 m_aListButton
.SetClickHdl( LINK( this, MyWin
, ListHdl
) );
136 m_aListButton
.Show();
138 m_aSvpBitmaps
.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
139 m_aSvpBitmaps
.SetSelectHdl( LINK( this, MyWin
, SelectHdl
) );
140 m_aSvpBitmaps
.Show();
142 m_aImage
.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
143 m_aImage
.SetScaleMode( com::sun::star::awt::ImageScaleMode::None
);
146 m_aQuitButton
.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
147 m_aQuitButton
.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) );
148 m_aQuitButton
.SetClickHdl( LINK( this, MyWin
, QuitHdl
) );
149 m_aQuitButton
.Show();
152 sal_Bool
MyWin::Close()
154 sal_Bool bRet
= WorkWindow::Close();
160 void MyWin::parseList( const OString
& rList
)
162 sal_Int32 nTokenPos
= 0;
163 OUString aElementType
;
164 m_aSvpBitmaps
.Clear();
165 while( nTokenPos
>= 0 )
167 OString aLine
= rList
.getToken( 0, '\n', nTokenPos
);
168 if( ! aLine
.getLength() || *aLine
.getStr() == '#' )
171 if( aLine
.startsWith( "ElementType: " ) )
172 aElementType
= OStringToOUString( aLine
.copy( 13 ), RTL_TEXTENCODING_ASCII_US
);
175 OUStringBuffer
aNewElement( 64 );
176 aNewElement
.append( aElementType
);
177 aNewElement
.appendAscii( ": " );
178 aNewElement
.append( OStringToOUString( aLine
, RTL_TEXTENCODING_ASCII_US
) );
179 m_aSvpBitmaps
.InsertEntry( aNewElement
.makeStringAndClear() );
184 OString
MyWin::processCommand( const OString
& rCommand
)
186 static const char* pEnv
= getenv("SVP_LISTENER_PORT");
187 OStringBuffer aAnswer
;
188 int nPort
= (pEnv
&& *pEnv
) ? atoi(pEnv
) : 8000;
189 int nSocket
= socket( PF_INET
, SOCK_STREAM
, 0 );
192 struct sockaddr_in addr
;
193 memset(&addr
, 0, sizeof(struct sockaddr_in
));
194 addr
.sin_family
= AF_INET
;
195 addr
.sin_port
= htons(nPort
);
196 addr
.sin_addr
.s_addr
= INADDR_ANY
;
197 if( connect( nSocket
, (const sockaddr
*)&addr
, sizeof(addr
) ) )
199 perror( "SvpElementContainer: connect() failed" );
204 write( nSocket
, rCommand
.getStr(), rCommand
.getLength() );
205 write( nSocket
, "\n", 1 );
210 nBytes
= read( nSocket
, buf
, sizeof(buf
) );
211 aAnswer
.append( buf
, nBytes
);
212 } while( nBytes
== sizeof( buf
) );
216 perror( "SvpElementContainer: socket() failed\n" );
217 return aAnswer
.makeStringAndClear();
220 IMPL_LINK( MyWin
, ListHdl
, Button
*, )
222 parseList( processCommand( "list" ) );
226 IMPL_LINK( MyWin
, QuitHdl
, Button
*, )
228 processCommand( "quit" );
232 IMPL_LINK( MyWin
, SelectHdl
, ListBox
*, )
234 String aEntry
= m_aSvpBitmaps
.GetSelectEntry();
235 sal_uInt16 nPos
= aEntry
.SearchAscii( ": " );
236 if( nPos
!= STRING_NOTFOUND
)
238 OStringBuffer
aCommand( 64 );
239 aCommand
.append( "get " );
240 aCommand
.append( OUStringToOString( aEntry
.Copy( nPos
+2 ), RTL_TEXTENCODING_ASCII_US
) );
241 OString
aAnswer( processCommand( aCommand
.makeStringAndClear() ) );
242 SvMemoryStream
aStream( aAnswer
.getLength() );
243 aStream
.Write( aAnswer
.getStr(), aAnswer
.getLength() );
244 aStream
.Seek( STREAM_SEEK_TO_BEGIN
);
247 fprintf( stderr
, "got bitmap of size %ldx%ld\n",
248 sal::static_int_cast
< long >(aBitmap
.GetSizePixel().Width()),
249 sal::static_int_cast
< long >(aBitmap
.GetSizePixel().Height()));
250 Size
aFixedSize( aBitmap
.GetSizePixel() );
251 aFixedSize
.Width() += 10;
252 aFixedSize
.Height() += 10;
253 m_aImage
.SetSizePixel( aFixedSize
);
254 m_aImage
.SetImage( Image( BitmapEx( aBitmap
) ) );
260 void MyWin::MouseMove( const MouseEvent
& rMEvt
)
262 WorkWindow::MouseMove( rMEvt
);
266 void MyWin::MouseButtonDown( const MouseEvent
& rMEvt
)
268 WorkWindow::MouseButtonDown( rMEvt
);
272 void MyWin::MouseButtonUp( const MouseEvent
& rMEvt
)
274 WorkWindow::MouseButtonUp( rMEvt
);
278 void MyWin::KeyInput( const KeyEvent
& rKEvt
)
280 WorkWindow::KeyInput( rKEvt
);
284 void MyWin::KeyUp( const KeyEvent
& rKEvt
)
286 WorkWindow::KeyUp( rKEvt
);
290 void MyWin::Paint( const Rectangle
& rRect
)
292 WorkWindow::Paint( rRect
);
298 WorkWindow::Resize();
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */