Update ooo320-m1
[ooovba.git] / vcl / workben / svpclient.cxx
blob6e205b7644182a3aa2fc4db604594316748edd11
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svpclient.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/main.h>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/awt/ImageScaleMode.hpp>
35 #include <vcl/event.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/wrkwin.hxx>
38 #include <vcl/button.hxx>
39 #include <vcl/lstbox.hxx>
40 #include <vcl/imgctrl.hxx>
41 #include <vcl/bitmapex.hxx>
42 #include <tools/extendapplicationenvironment.hxx>
43 #include <tools/stream.hxx>
45 #include <rtl/strbuf.hxx>
46 #include <rtl/ustrbuf.hxx>
48 #include <math.h>
50 #include <comphelper/processfactory.hxx>
51 #include <cppuhelper/servicefactory.hxx>
52 #include <cppuhelper/bootstrap.hxx>
53 #include "ucbhelper/contentbroker.hxx"
54 #include "ucbhelper/configurationkeys.hxx"
56 #include <errno.h>
57 #include <unistd.h>
58 #include <stdio.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
64 using namespace rtl;
65 using namespace cppu;
66 using namespace comphelper;
67 using namespace ::com::sun::star::uno;
68 using namespace ::com::sun::star::lang;
69 // -----------------------------------------------------------------------
71 // Forward declaration
72 void Main();
74 // -----------------------------------------------------------------------
76 SAL_IMPLEMENT_MAIN()
78 tools::extendApplicationEnvironment();
80 //-------------------------------------------------
81 // create the global service-manager
82 //-------------------------------------------------
83 Reference< XMultiServiceFactory > xFactory;
84 try
86 Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext();
87 xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY );
88 if( xFactory.is() )
89 setProcessServiceFactory( xFactory );
91 catch( com::sun::star::uno::Exception& rExc)
95 if( ! xFactory.is() )
97 fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
98 exit( 1 );
102 * Create UCB.
104 Sequence< Any > aArgs( 2 );
105 aArgs[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
106 aArgs[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
107 #if OSL_DEBUG_LEVEL > 1
108 sal_Bool bSuccess =
109 #endif
110 ::ucbhelper::ContentBroker::initialize( xFactory, aArgs );
112 #if OSL_DEBUG_LEVEL > 1
113 if ( !bSuccess )
115 fprintf( stderr, "Error creating UCB, installation must be in disorder. Exiting.\n" );
116 exit( 1 );
118 #endif
120 InitVCL( xFactory );
121 ::Main();
122 DeInitVCL();
124 return 0;
127 // -----------------------------------------------------------------------
129 class MyWin : public WorkWindow
131 PushButton m_aListButton;
132 ListBox m_aSvpBitmaps;
133 ImageControl m_aImage;
134 PushButton m_aQuitButton;
135 public:
136 MyWin( Window* pParent, WinBits nWinStyle );
138 void MouseMove( const MouseEvent& rMEvt );
139 void MouseButtonDown( const MouseEvent& rMEvt );
140 void MouseButtonUp( const MouseEvent& rMEvt );
141 void KeyInput( const KeyEvent& rKEvt );
142 void KeyUp( const KeyEvent& rKEvt );
143 void Paint( const Rectangle& rRect );
144 void Resize();
146 BOOL Close();
148 void parseList( const rtl::OString& rList );
149 rtl::OString processCommand( const rtl::OString& rCommand );
151 DECL_LINK( ListHdl, Button* );
152 DECL_LINK( SelectHdl, ListBox* );
153 DECL_LINK( QuitHdl, Button* );
156 // -----------------------------------------------------------------------
158 void Main()
160 MyWin aMainWin( NULL, WB_STDWORK );
161 aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) );
162 aMainWin.Show();
164 Application::Execute();
167 // -----------------------------------------------------------------------
169 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
170 WorkWindow( pParent, nWinStyle ),
171 m_aListButton( this, 0 ),
172 m_aSvpBitmaps( this, WB_BORDER ),
173 m_aImage( this, WB_BORDER ),
174 m_aQuitButton( this, 0 )
176 m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
177 m_aListButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) );
178 m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) );
179 m_aListButton.Show();
181 m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
182 m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
183 m_aSvpBitmaps.Show();
185 m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
186 m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None );
187 m_aImage.Show();
189 m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
190 m_aQuitButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) );
191 m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) );
192 m_aQuitButton.Show();
195 BOOL MyWin::Close()
197 BOOL bRet = WorkWindow::Close();
198 if( bRet )
199 Application::Quit();
200 return bRet;
203 void MyWin::parseList( const rtl::OString& rList )
205 sal_Int32 nTokenPos = 0;
206 rtl::OUString aElementType;
207 m_aSvpBitmaps.Clear();
208 while( nTokenPos >= 0 )
210 rtl::OString aLine = rList.getToken( 0, '\n', nTokenPos );
211 if( ! aLine.getLength() || *aLine.getStr() == '#' )
212 continue;
214 if( aLine.compareTo( "ElementType: ", 13 ) == 0 )
215 aElementType = rtl::OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
216 else
218 rtl::OUStringBuffer aNewElement( 64 );
219 aNewElement.append( aElementType );
220 aNewElement.appendAscii( ": " );
221 aNewElement.append( rtl::OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
222 m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() );
227 rtl::OString MyWin::processCommand( const rtl::OString& rCommand )
229 static const char* pEnv = getenv("SVP_LISTENER_PORT");
230 rtl::OStringBuffer aAnswer;
231 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
232 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
233 if( nSocket >= 0)
235 struct sockaddr_in addr;
236 memset(&addr, 0, sizeof(struct sockaddr_in));
237 addr.sin_family = AF_INET;
238 addr.sin_port = htons(nPort);
239 addr.sin_addr.s_addr = INADDR_ANY;
240 if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) )
242 perror( "SvpElementContainer: connect() failed" );
243 close(nSocket);
245 else
247 write( nSocket, rCommand.getStr(), rCommand.getLength() );
248 write( nSocket, "\n", 1 );
249 char buf[256];
250 ssize_t nBytes = 0;
253 nBytes = read( nSocket, buf, sizeof(buf) );
254 aAnswer.append( buf, nBytes );
255 } while( nBytes == sizeof( buf ) );
258 else
259 perror( "SvpElementContainer: socket() failed\n" );
260 return aAnswer.makeStringAndClear();
263 IMPL_LINK( MyWin, ListHdl, Button*, )
265 parseList( processCommand( "list" ) );
266 return 0;
269 IMPL_LINK( MyWin, QuitHdl, Button*, )
271 processCommand( "quit" );
272 return 0;
275 IMPL_LINK( MyWin, SelectHdl, ListBox*, )
277 String aEntry = m_aSvpBitmaps.GetSelectEntry();
278 USHORT nPos = aEntry.SearchAscii( ": " );
279 if( nPos != STRING_NOTFOUND )
281 OStringBuffer aCommand( 64 );
282 aCommand.append( "get " );
283 aCommand.append( rtl::OUStringToOString( aEntry.Copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
284 OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
285 SvMemoryStream aStream( aAnswer.getLength() );
286 aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
287 aStream.Seek( STREAM_SEEK_TO_BEGIN );
288 Bitmap aBitmap;
289 aStream >> aBitmap;
290 fprintf( stderr, "got bitmap of size %ldx%ld\n",
291 sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
292 sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
293 Size aFixedSize( aBitmap.GetSizePixel() );
294 aFixedSize.Width() += 10;
295 aFixedSize.Height() += 10;
296 m_aImage.SetSizePixel( aFixedSize );
297 m_aImage.SetBitmap( BitmapEx( aBitmap ) );
299 return 0;
302 // -----------------------------------------------------------------------
304 void MyWin::MouseMove( const MouseEvent& rMEvt )
306 WorkWindow::MouseMove( rMEvt );
309 // -----------------------------------------------------------------------
311 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
313 WorkWindow::MouseButtonDown( rMEvt );
316 // -----------------------------------------------------------------------
318 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
320 WorkWindow::MouseButtonUp( rMEvt );
323 // -----------------------------------------------------------------------
325 void MyWin::KeyInput( const KeyEvent& rKEvt )
327 WorkWindow::KeyInput( rKEvt );
330 // -----------------------------------------------------------------------
332 void MyWin::KeyUp( const KeyEvent& rKEvt )
334 WorkWindow::KeyUp( rKEvt );
337 // -----------------------------------------------------------------------
339 void MyWin::Paint( const Rectangle& rRect )
341 WorkWindow::Paint( rRect );
344 // -----------------------------------------------------------------------
346 void MyWin::Resize()
348 WorkWindow::Resize();