Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / vcl / workben / svpclient.cxx
bloba059735ae8cba360a6e4b7af9d4721be4c9a6b93
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <sal/main.h>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/awt/ImageScaleMode.hpp>
33 #include <vcl/event.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/wrkwin.hxx>
36 #include <vcl/button.hxx>
37 #include <vcl/lstbox.hxx>
38 #include <vcl/imgctrl.hxx>
39 #include <vcl/bitmapex.hxx>
40 #include <tools/extendapplicationenvironment.hxx>
41 #include <tools/stream.hxx>
43 #include <rtl/strbuf.hxx>
44 #include <rtl/ustrbuf.hxx>
46 #include <math.h>
48 #include <comphelper/processfactory.hxx>
49 #include <cppuhelper/servicefactory.hxx>
50 #include <cppuhelper/bootstrap.hxx>
51 #include "ucbhelper/contentbroker.hxx"
52 #include "ucbhelper/configurationkeys.hxx"
54 #include <errno.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
62 using namespace cppu;
63 using namespace comphelper;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::lang;
67 using ::rtl::OUString;
68 using ::rtl::OString;
69 using ::rtl::OUStringToOString;
70 using ::rtl::OStringToOUString;
71 using ::rtl::OUStringBuffer;
72 using ::rtl::OStringBuffer;
73 // -----------------------------------------------------------------------
75 // Forward declaration
76 void Main();
78 // -----------------------------------------------------------------------
80 SAL_IMPLEMENT_MAIN()
82 tools::extendApplicationEnvironment();
84 //-------------------------------------------------
85 // create the global service-manager
86 //-------------------------------------------------
87 Reference< XMultiServiceFactory > xFactory;
88 try
90 Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext();
91 xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY );
92 if( xFactory.is() )
93 setProcessServiceFactory( xFactory );
95 catch(const com::sun::star::uno::Exception&)
99 if( ! xFactory.is() )
101 fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
102 exit( 1 );
106 * Create UCB.
108 Sequence< Any > aArgs( 2 );
109 aArgs[ 0 ] <<= OUString(UCB_CONFIGURATION_KEY1_LOCAL );
110 aArgs[ 1 ] <<= OUString(UCB_CONFIGURATION_KEY2_OFFICE );
111 #if OSL_DEBUG_LEVEL > 1
112 sal_Bool bSuccess =
113 #endif
114 ::ucbhelper::ContentBroker::initialize( xFactory, aArgs );
116 #if OSL_DEBUG_LEVEL > 1
117 if ( !bSuccess )
119 fprintf( stderr, "Error creating UCB, installation must be in disorder. Exiting.\n" );
120 exit( 1 );
122 #endif
124 InitVCL( xFactory );
125 ::Main();
126 DeInitVCL();
128 return 0;
131 // -----------------------------------------------------------------------
133 class MyWin : public WorkWindow
135 PushButton m_aListButton;
136 ListBox m_aSvpBitmaps;
137 ImageControl m_aImage;
138 PushButton m_aQuitButton;
139 public:
140 MyWin( Window* pParent, WinBits nWinStyle );
142 void MouseMove( const MouseEvent& rMEvt );
143 void MouseButtonDown( const MouseEvent& rMEvt );
144 void MouseButtonUp( const MouseEvent& rMEvt );
145 void KeyInput( const KeyEvent& rKEvt );
146 void KeyUp( const KeyEvent& rKEvt );
147 void Paint( const Rectangle& rRect );
148 void Resize();
150 sal_Bool Close();
152 void parseList( const rtl::OString& rList );
153 rtl::OString processCommand( const rtl::OString& rCommand );
155 DECL_LINK( ListHdl, Button* );
156 DECL_LINK( SelectHdl, ListBox* );
157 DECL_LINK( QuitHdl, Button* );
160 // -----------------------------------------------------------------------
162 void Main()
164 MyWin aMainWin( NULL, WB_STDWORK );
165 aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) );
166 aMainWin.Show();
168 Application::Execute();
171 // -----------------------------------------------------------------------
173 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
174 WorkWindow( pParent, nWinStyle ),
175 m_aListButton( this, 0 ),
176 m_aSvpBitmaps( this, WB_BORDER ),
177 m_aImage( this, WB_BORDER ),
178 m_aQuitButton( this, 0 )
180 m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
181 m_aListButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) );
182 m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) );
183 m_aListButton.Show();
185 m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
186 m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
187 m_aSvpBitmaps.Show();
189 m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
190 m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None );
191 m_aImage.Show();
193 m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
194 m_aQuitButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) );
195 m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) );
196 m_aQuitButton.Show();
199 sal_Bool MyWin::Close()
201 sal_Bool bRet = WorkWindow::Close();
202 if( bRet )
203 Application::Quit();
204 return bRet;
207 void MyWin::parseList( const rtl::OString& rList )
209 sal_Int32 nTokenPos = 0;
210 rtl::OUString aElementType;
211 m_aSvpBitmaps.Clear();
212 while( nTokenPos >= 0 )
214 rtl::OString aLine = rList.getToken( 0, '\n', nTokenPos );
215 if( ! aLine.getLength() || *aLine.getStr() == '#' )
216 continue;
218 if( aLine.compareTo( "ElementType: ", 13 ) == 0 )
219 aElementType = rtl::OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
220 else
222 rtl::OUStringBuffer aNewElement( 64 );
223 aNewElement.append( aElementType );
224 aNewElement.appendAscii( ": " );
225 aNewElement.append( rtl::OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
226 m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() );
231 rtl::OString MyWin::processCommand( const rtl::OString& rCommand )
233 static const char* pEnv = getenv("SVP_LISTENER_PORT");
234 rtl::OStringBuffer aAnswer;
235 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
236 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
237 if( nSocket >= 0)
239 struct sockaddr_in addr;
240 memset(&addr, 0, sizeof(struct sockaddr_in));
241 addr.sin_family = AF_INET;
242 addr.sin_port = htons(nPort);
243 addr.sin_addr.s_addr = INADDR_ANY;
244 if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) )
246 perror( "SvpElementContainer: connect() failed" );
247 close(nSocket);
249 else
251 write( nSocket, rCommand.getStr(), rCommand.getLength() );
252 write( nSocket, "\n", 1 );
253 char buf[256];
254 ssize_t nBytes = 0;
257 nBytes = read( nSocket, buf, sizeof(buf) );
258 aAnswer.append( buf, nBytes );
259 } while( nBytes == sizeof( buf ) );
262 else
263 perror( "SvpElementContainer: socket() failed\n" );
264 return aAnswer.makeStringAndClear();
267 IMPL_LINK( MyWin, ListHdl, Button*, )
269 parseList( processCommand( "list" ) );
270 return 0;
273 IMPL_LINK( MyWin, QuitHdl, Button*, )
275 processCommand( "quit" );
276 return 0;
279 IMPL_LINK( MyWin, SelectHdl, ListBox*, )
281 String aEntry = m_aSvpBitmaps.GetSelectEntry();
282 sal_uInt16 nPos = aEntry.SearchAscii( ": " );
283 if( nPos != STRING_NOTFOUND )
285 OStringBuffer aCommand( 64 );
286 aCommand.append( "get " );
287 aCommand.append( rtl::OUStringToOString( aEntry.Copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
288 OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
289 SvMemoryStream aStream( aAnswer.getLength() );
290 aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
291 aStream.Seek( STREAM_SEEK_TO_BEGIN );
292 Bitmap aBitmap;
293 aStream >> aBitmap;
294 fprintf( stderr, "got bitmap of size %ldx%ld\n",
295 sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
296 sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
297 Size aFixedSize( aBitmap.GetSizePixel() );
298 aFixedSize.Width() += 10;
299 aFixedSize.Height() += 10;
300 m_aImage.SetSizePixel( aFixedSize );
301 m_aImage.SetImage( Image( BitmapEx( aBitmap ) ) );
303 return 0;
306 // -----------------------------------------------------------------------
308 void MyWin::MouseMove( const MouseEvent& rMEvt )
310 WorkWindow::MouseMove( rMEvt );
313 // -----------------------------------------------------------------------
315 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
317 WorkWindow::MouseButtonDown( rMEvt );
320 // -----------------------------------------------------------------------
322 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
324 WorkWindow::MouseButtonUp( rMEvt );
327 // -----------------------------------------------------------------------
329 void MyWin::KeyInput( const KeyEvent& rKEvt )
331 WorkWindow::KeyInput( rKEvt );
334 // -----------------------------------------------------------------------
336 void MyWin::KeyUp( const KeyEvent& rKEvt )
338 WorkWindow::KeyUp( rKEvt );
341 // -----------------------------------------------------------------------
343 void MyWin::Paint( const Rectangle& rRect )
345 WorkWindow::Paint( rRect );
348 // -----------------------------------------------------------------------
350 void MyWin::Resize()
352 WorkWindow::Resize();
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */