1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 28 #include <sal/main.h> 29 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 30 #include <com/sun/star/awt/ImageScaleMode.hpp> 31 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> 41 42 #include <rtl/strbuf.hxx> 43 #include <rtl/ustrbuf.hxx> 44 45 #include <math.h> 46 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" 52 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> 59 60 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 // ----------------------------------------------------------------------- 67 68 // Forward declaration 69 void Main(); 70 71 // ----------------------------------------------------------------------- 72 73 SAL_IMPLEMENT_MAIN() 74 { 75 tools::extendApplicationEnvironment(); 76 77 //------------------------------------------------- 78 // create the global service-manager 79 //------------------------------------------------- 80 Reference< XMultiServiceFactory > xFactory; 81 try 82 { 83 Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext(); 84 xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY ); 85 if( xFactory.is() ) 86 setProcessServiceFactory( xFactory ); 87 } 88 catch( com::sun::star::uno::Exception& rExc) 89 { 90 } 91 92 if( ! xFactory.is() ) 93 { 94 fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" ); 95 exit( 1 ); 96 } 97 98 /* 99 * Create UCB. 100 */ 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 ); 108 109 #if OSL_DEBUG_LEVEL > 1 110 if ( !bSuccess ) 111 { 112 fprintf( stderr, "Error creating UCB, installation must be in disorder. Exiting.\n" ); 113 exit( 1 ); 114 } 115 #endif 116 117 InitVCL( xFactory ); 118 ::Main(); 119 DeInitVCL(); 120 121 return 0; 122 } 123 124 // ----------------------------------------------------------------------- 125 126 class MyWin : public WorkWindow 127 { 128 PushButton m_aListButton; 129 ListBox m_aSvpBitmaps; 130 ImageControl m_aImage; 131 PushButton m_aQuitButton; 132 public: 133 MyWin( Window* pParent, WinBits nWinStyle ); 134 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(); 142 143 sal_Bool Close(); 144 145 void parseList( const rtl::OString& rList ); 146 rtl::OString processCommand( const rtl::OString& rCommand ); 147 148 DECL_LINK( ListHdl, Button* ); 149 DECL_LINK( SelectHdl, ListBox* ); 150 DECL_LINK( QuitHdl, Button* ); 151 }; 152 153 // ----------------------------------------------------------------------- 154 155 void Main() 156 { 157 MyWin aMainWin( NULL, WB_STDWORK ); 158 aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) ); 159 aMainWin.Show(); 160 161 Application::Execute(); 162 } 163 164 // ----------------------------------------------------------------------- 165 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 ) 172 { 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(); 177 178 m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) ); 179 m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) ); 180 m_aSvpBitmaps.Show(); 181 182 m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) ); 183 m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None ); 184 m_aImage.Show(); 185 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(); 190 } 191 192 sal_Bool MyWin::Close() 193 { 194 sal_Bool bRet = WorkWindow::Close(); 195 if( bRet ) 196 Application::Quit(); 197 return bRet; 198 } 199 200 void MyWin::parseList( const rtl::OString& rList ) 201 { 202 sal_Int32 nTokenPos = 0; 203 rtl::OUString aElementType; 204 m_aSvpBitmaps.Clear(); 205 while( nTokenPos >= 0 ) 206 { 207 rtl::OString aLine = rList.getToken( 0, '\n', nTokenPos ); 208 if( ! aLine.getLength() || *aLine.getStr() == '#' ) 209 continue; 210 211 if( aLine.compareTo( "ElementType: ", 13 ) == 0 ) 212 aElementType = rtl::OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US ); 213 else 214 { 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() ); 220 } 221 } 222 } 223 224 rtl::OString MyWin::processCommand( const rtl::OString& rCommand ) 225 { 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) 231 { 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) ) ) 238 { 239 perror( "SvpElementContainer: connect() failed" ); 240 close(nSocket); 241 } 242 else 243 { 244 write( nSocket, rCommand.getStr(), rCommand.getLength() ); 245 write( nSocket, "\n", 1 ); 246 char buf[256]; 247 ssize_t nBytes = 0; 248 do 249 { 250 nBytes = read( nSocket, buf, sizeof(buf) ); 251 aAnswer.append( buf, nBytes ); 252 } while( nBytes == sizeof( buf ) ); 253 } 254 } 255 else 256 perror( "SvpElementContainer: socket() failed\n" ); 257 return aAnswer.makeStringAndClear(); 258 } 259 260 IMPL_LINK( MyWin, ListHdl, Button*, ) 261 { 262 parseList( processCommand( "list" ) ); 263 return 0; 264 } 265 266 IMPL_LINK( MyWin, QuitHdl, Button*, ) 267 { 268 processCommand( "quit" ); 269 return 0; 270 } 271 272 IMPL_LINK( MyWin, SelectHdl, ListBox*, ) 273 { 274 String aEntry = m_aSvpBitmaps.GetSelectEntry(); 275 sal_uInt16 nPos = aEntry.SearchAscii( ": " ); 276 if( nPos != STRING_NOTFOUND ) 277 { 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 ) ) ); 295 } 296 return 0; 297 } 298 299 // ----------------------------------------------------------------------- 300 301 void MyWin::MouseMove( const MouseEvent& rMEvt ) 302 { 303 WorkWindow::MouseMove( rMEvt ); 304 } 305 306 // ----------------------------------------------------------------------- 307 308 void MyWin::MouseButtonDown( const MouseEvent& rMEvt ) 309 { 310 WorkWindow::MouseButtonDown( rMEvt ); 311 } 312 313 // ----------------------------------------------------------------------- 314 315 void MyWin::MouseButtonUp( const MouseEvent& rMEvt ) 316 { 317 WorkWindow::MouseButtonUp( rMEvt ); 318 } 319 320 // ----------------------------------------------------------------------- 321 322 void MyWin::KeyInput( const KeyEvent& rKEvt ) 323 { 324 WorkWindow::KeyInput( rKEvt ); 325 } 326 327 // ----------------------------------------------------------------------- 328 329 void MyWin::KeyUp( const KeyEvent& rKEvt ) 330 { 331 WorkWindow::KeyUp( rKEvt ); 332 } 333 334 // ----------------------------------------------------------------------- 335 336 void MyWin::Paint( const Rectangle& rRect ) 337 { 338 WorkWindow::Paint( rRect ); 339 } 340 341 // ----------------------------------------------------------------------- 342 343 void MyWin::Resize() 344 { 345 WorkWindow::Resize(); 346 } 347