merged tag ooo/DEV300_m102
[LibreOffice.git] / vcl / workben / svpclient.cxx
blobf35946fb11299fed07c645dfd17c9b26efb896ce
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <sal/main.h>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/awt/ImageScaleMode.hpp>
32 #include <vcl/event.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/wrkwin.hxx>
35 #include <vcl/button.hxx>
36 #include <vcl/lstbox.hxx>
37 #include <vcl/imgctrl.hxx>
38 #include <vcl/bitmapex.hxx>
39 #include <tools/extendapplicationenvironment.hxx>
40 #include <tools/stream.hxx>
42 #include <rtl/strbuf.hxx>
43 #include <rtl/ustrbuf.hxx>
45 #include <math.h>
47 #include <comphelper/processfactory.hxx>
48 #include <cppuhelper/servicefactory.hxx>
49 #include <cppuhelper/bootstrap.hxx>
50 #include "ucbhelper/contentbroker.hxx"
51 #include "ucbhelper/configurationkeys.hxx"
53 #include <errno.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
61 using namespace rtl;
62 using namespace cppu;
63 using namespace comphelper;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::lang;
66 // -----------------------------------------------------------------------
68 // Forward declaration
69 void Main();
71 // -----------------------------------------------------------------------
73 SAL_IMPLEMENT_MAIN()
75 tools::extendApplicationEnvironment();
77 //-------------------------------------------------
78 // create the global service-manager
79 //-------------------------------------------------
80 Reference< XMultiServiceFactory > xFactory;
81 try
83 Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext();
84 xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY );
85 if( xFactory.is() )
86 setProcessServiceFactory( xFactory );
88 catch( com::sun::star::uno::Exception& rExc)
92 if( ! xFactory.is() )
94 fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
95 exit( 1 );
99 * Create UCB.
101 Sequence< Any > aArgs( 2 );
102 aArgs[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
103 aArgs[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
104 #if OSL_DEBUG_LEVEL > 1
105 sal_Bool bSuccess =
106 #endif
107 ::ucbhelper::ContentBroker::initialize( xFactory, aArgs );
109 #if OSL_DEBUG_LEVEL > 1
110 if ( !bSuccess )
112 fprintf( stderr, "Error creating UCB, installation must be in disorder. Exiting.\n" );
113 exit( 1 );
115 #endif
117 InitVCL( xFactory );
118 ::Main();
119 DeInitVCL();
121 return 0;
124 // -----------------------------------------------------------------------
126 class MyWin : public WorkWindow
128 PushButton m_aListButton;
129 ListBox m_aSvpBitmaps;
130 ImageControl m_aImage;
131 PushButton m_aQuitButton;
132 public:
133 MyWin( Window* pParent, WinBits nWinStyle );
135 void MouseMove( const MouseEvent& rMEvt );
136 void MouseButtonDown( const MouseEvent& rMEvt );
137 void MouseButtonUp( const MouseEvent& rMEvt );
138 void KeyInput( const KeyEvent& rKEvt );
139 void KeyUp( const KeyEvent& rKEvt );
140 void Paint( const Rectangle& rRect );
141 void Resize();
143 sal_Bool Close();
145 void parseList( const rtl::OString& rList );
146 rtl::OString processCommand( const rtl::OString& rCommand );
148 DECL_LINK( ListHdl, Button* );
149 DECL_LINK( SelectHdl, ListBox* );
150 DECL_LINK( QuitHdl, Button* );
153 // -----------------------------------------------------------------------
155 void Main()
157 MyWin aMainWin( NULL, WB_STDWORK );
158 aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) );
159 aMainWin.Show();
161 Application::Execute();
164 // -----------------------------------------------------------------------
166 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
167 WorkWindow( pParent, nWinStyle ),
168 m_aListButton( this, 0 ),
169 m_aSvpBitmaps( this, WB_BORDER ),
170 m_aImage( this, WB_BORDER ),
171 m_aQuitButton( this, 0 )
173 m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
174 m_aListButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) );
175 m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) );
176 m_aListButton.Show();
178 m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
179 m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
180 m_aSvpBitmaps.Show();
182 m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
183 m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None );
184 m_aImage.Show();
186 m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
187 m_aQuitButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) );
188 m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) );
189 m_aQuitButton.Show();
192 sal_Bool MyWin::Close()
194 sal_Bool bRet = WorkWindow::Close();
195 if( bRet )
196 Application::Quit();
197 return bRet;
200 void MyWin::parseList( const rtl::OString& rList )
202 sal_Int32 nTokenPos = 0;
203 rtl::OUString aElementType;
204 m_aSvpBitmaps.Clear();
205 while( nTokenPos >= 0 )
207 rtl::OString aLine = rList.getToken( 0, '\n', nTokenPos );
208 if( ! aLine.getLength() || *aLine.getStr() == '#' )
209 continue;
211 if( aLine.compareTo( "ElementType: ", 13 ) == 0 )
212 aElementType = rtl::OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
213 else
215 rtl::OUStringBuffer aNewElement( 64 );
216 aNewElement.append( aElementType );
217 aNewElement.appendAscii( ": " );
218 aNewElement.append( rtl::OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
219 m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() );
224 rtl::OString MyWin::processCommand( const rtl::OString& rCommand )
226 static const char* pEnv = getenv("SVP_LISTENER_PORT");
227 rtl::OStringBuffer aAnswer;
228 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
229 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
230 if( nSocket >= 0)
232 struct sockaddr_in addr;
233 memset(&addr, 0, sizeof(struct sockaddr_in));
234 addr.sin_family = AF_INET;
235 addr.sin_port = htons(nPort);
236 addr.sin_addr.s_addr = INADDR_ANY;
237 if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) )
239 perror( "SvpElementContainer: connect() failed" );
240 close(nSocket);
242 else
244 write( nSocket, rCommand.getStr(), rCommand.getLength() );
245 write( nSocket, "\n", 1 );
246 char buf[256];
247 ssize_t nBytes = 0;
250 nBytes = read( nSocket, buf, sizeof(buf) );
251 aAnswer.append( buf, nBytes );
252 } while( nBytes == sizeof( buf ) );
255 else
256 perror( "SvpElementContainer: socket() failed\n" );
257 return aAnswer.makeStringAndClear();
260 IMPL_LINK( MyWin, ListHdl, Button*, )
262 parseList( processCommand( "list" ) );
263 return 0;
266 IMPL_LINK( MyWin, QuitHdl, Button*, )
268 processCommand( "quit" );
269 return 0;
272 IMPL_LINK( MyWin, SelectHdl, ListBox*, )
274 String aEntry = m_aSvpBitmaps.GetSelectEntry();
275 sal_uInt16 nPos = aEntry.SearchAscii( ": " );
276 if( nPos != STRING_NOTFOUND )
278 OStringBuffer aCommand( 64 );
279 aCommand.append( "get " );
280 aCommand.append( rtl::OUStringToOString( aEntry.Copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
281 OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
282 SvMemoryStream aStream( aAnswer.getLength() );
283 aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
284 aStream.Seek( STREAM_SEEK_TO_BEGIN );
285 Bitmap aBitmap;
286 aStream >> aBitmap;
287 fprintf( stderr, "got bitmap of size %ldx%ld\n",
288 sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
289 sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
290 Size aFixedSize( aBitmap.GetSizePixel() );
291 aFixedSize.Width() += 10;
292 aFixedSize.Height() += 10;
293 m_aImage.SetSizePixel( aFixedSize );
294 m_aImage.SetImage( Image( BitmapEx( aBitmap ) ) );
296 return 0;
299 // -----------------------------------------------------------------------
301 void MyWin::MouseMove( const MouseEvent& rMEvt )
303 WorkWindow::MouseMove( rMEvt );
306 // -----------------------------------------------------------------------
308 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
310 WorkWindow::MouseButtonDown( rMEvt );
313 // -----------------------------------------------------------------------
315 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
317 WorkWindow::MouseButtonUp( rMEvt );
320 // -----------------------------------------------------------------------
322 void MyWin::KeyInput( const KeyEvent& rKEvt )
324 WorkWindow::KeyInput( rKEvt );
327 // -----------------------------------------------------------------------
329 void MyWin::KeyUp( const KeyEvent& rKEvt )
331 WorkWindow::KeyUp( rKEvt );
334 // -----------------------------------------------------------------------
336 void MyWin::Paint( const Rectangle& rRect )
338 WorkWindow::Paint( rRect );
341 // -----------------------------------------------------------------------
343 void MyWin::Resize()
345 WorkWindow::Resize();