1*9f62ea84SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*9f62ea84SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*9f62ea84SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*9f62ea84SAndrew Rist * distributed with this work for additional information 6*9f62ea84SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*9f62ea84SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*9f62ea84SAndrew Rist * "License"); you may not use this file except in compliance 9*9f62ea84SAndrew Rist * with the License. You may obtain a copy of the License at 10*9f62ea84SAndrew Rist * 11*9f62ea84SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*9f62ea84SAndrew Rist * 13*9f62ea84SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*9f62ea84SAndrew Rist * software distributed under the License is distributed on an 15*9f62ea84SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*9f62ea84SAndrew Rist * KIND, either express or implied. See the License for the 17*9f62ea84SAndrew Rist * specific language governing permissions and limitations 18*9f62ea84SAndrew Rist * under the License. 19*9f62ea84SAndrew Rist * 20*9f62ea84SAndrew Rist *************************************************************/ 21*9f62ea84SAndrew Rist 22*9f62ea84SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include <sal/main.h> 25cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 26cdf0e10cSrcweir #include <com/sun/star/awt/ImageScaleMode.hpp> 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <vcl/event.hxx> 29cdf0e10cSrcweir #include <vcl/svapp.hxx> 30cdf0e10cSrcweir #include <vcl/wrkwin.hxx> 31cdf0e10cSrcweir #include <vcl/button.hxx> 32cdf0e10cSrcweir #include <vcl/lstbox.hxx> 33cdf0e10cSrcweir #include <vcl/imgctrl.hxx> 34cdf0e10cSrcweir #include <vcl/bitmapex.hxx> 35cdf0e10cSrcweir #include <tools/extendapplicationenvironment.hxx> 36cdf0e10cSrcweir #include <tools/stream.hxx> 37cdf0e10cSrcweir 38cdf0e10cSrcweir #include <rtl/strbuf.hxx> 39cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 40cdf0e10cSrcweir 41cdf0e10cSrcweir #include <math.h> 42cdf0e10cSrcweir 43cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 44cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx> 45cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx> 46cdf0e10cSrcweir #include "ucbhelper/contentbroker.hxx" 47cdf0e10cSrcweir #include "ucbhelper/configurationkeys.hxx" 48cdf0e10cSrcweir 49cdf0e10cSrcweir #include <errno.h> 50cdf0e10cSrcweir #include <unistd.h> 51cdf0e10cSrcweir #include <stdio.h> 52cdf0e10cSrcweir #include <sys/types.h> 53cdf0e10cSrcweir #include <sys/socket.h> 54cdf0e10cSrcweir #include <netinet/in.h> 55cdf0e10cSrcweir 56cdf0e10cSrcweir 57cdf0e10cSrcweir using namespace rtl; 58cdf0e10cSrcweir using namespace cppu; 59cdf0e10cSrcweir using namespace comphelper; 60cdf0e10cSrcweir using namespace ::com::sun::star::uno; 61cdf0e10cSrcweir using namespace ::com::sun::star::lang; 62cdf0e10cSrcweir // ----------------------------------------------------------------------- 63cdf0e10cSrcweir 64cdf0e10cSrcweir // Forward declaration 65cdf0e10cSrcweir void Main(); 66cdf0e10cSrcweir 67cdf0e10cSrcweir // ----------------------------------------------------------------------- 68cdf0e10cSrcweir 69cdf0e10cSrcweir SAL_IMPLEMENT_MAIN() 70cdf0e10cSrcweir { 71cdf0e10cSrcweir tools::extendApplicationEnvironment(); 72cdf0e10cSrcweir 73cdf0e10cSrcweir //------------------------------------------------- 74cdf0e10cSrcweir // create the global service-manager 75cdf0e10cSrcweir //------------------------------------------------- 76cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory; 77cdf0e10cSrcweir try 78cdf0e10cSrcweir { 79cdf0e10cSrcweir Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext(); 80cdf0e10cSrcweir xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY ); 81cdf0e10cSrcweir if( xFactory.is() ) 82cdf0e10cSrcweir setProcessServiceFactory( xFactory ); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir catch( com::sun::star::uno::Exception& rExc) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir if( ! xFactory.is() ) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" ); 91cdf0e10cSrcweir exit( 1 ); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir /* 95cdf0e10cSrcweir * Create UCB. 96cdf0e10cSrcweir */ 97cdf0e10cSrcweir Sequence< Any > aArgs( 2 ); 98cdf0e10cSrcweir aArgs[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL ); 99cdf0e10cSrcweir aArgs[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE ); 100cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 101cdf0e10cSrcweir sal_Bool bSuccess = 102cdf0e10cSrcweir #endif 103cdf0e10cSrcweir ::ucbhelper::ContentBroker::initialize( xFactory, aArgs ); 104cdf0e10cSrcweir 105cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 106cdf0e10cSrcweir if ( !bSuccess ) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir fprintf( stderr, "Error creating UCB, installation must be in disorder. Exiting.\n" ); 109cdf0e10cSrcweir exit( 1 ); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir #endif 112cdf0e10cSrcweir 113cdf0e10cSrcweir InitVCL( xFactory ); 114cdf0e10cSrcweir ::Main(); 115cdf0e10cSrcweir DeInitVCL(); 116cdf0e10cSrcweir 117cdf0e10cSrcweir return 0; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir // ----------------------------------------------------------------------- 121cdf0e10cSrcweir 122cdf0e10cSrcweir class MyWin : public WorkWindow 123cdf0e10cSrcweir { 124cdf0e10cSrcweir PushButton m_aListButton; 125cdf0e10cSrcweir ListBox m_aSvpBitmaps; 126cdf0e10cSrcweir ImageControl m_aImage; 127cdf0e10cSrcweir PushButton m_aQuitButton; 128cdf0e10cSrcweir public: 129cdf0e10cSrcweir MyWin( Window* pParent, WinBits nWinStyle ); 130cdf0e10cSrcweir 131cdf0e10cSrcweir void MouseMove( const MouseEvent& rMEvt ); 132cdf0e10cSrcweir void MouseButtonDown( const MouseEvent& rMEvt ); 133cdf0e10cSrcweir void MouseButtonUp( const MouseEvent& rMEvt ); 134cdf0e10cSrcweir void KeyInput( const KeyEvent& rKEvt ); 135cdf0e10cSrcweir void KeyUp( const KeyEvent& rKEvt ); 136cdf0e10cSrcweir void Paint( const Rectangle& rRect ); 137cdf0e10cSrcweir void Resize(); 138cdf0e10cSrcweir 139cdf0e10cSrcweir sal_Bool Close(); 140cdf0e10cSrcweir 141cdf0e10cSrcweir void parseList( const rtl::OString& rList ); 142cdf0e10cSrcweir rtl::OString processCommand( const rtl::OString& rCommand ); 143cdf0e10cSrcweir 144cdf0e10cSrcweir DECL_LINK( ListHdl, Button* ); 145cdf0e10cSrcweir DECL_LINK( SelectHdl, ListBox* ); 146cdf0e10cSrcweir DECL_LINK( QuitHdl, Button* ); 147cdf0e10cSrcweir }; 148cdf0e10cSrcweir 149cdf0e10cSrcweir // ----------------------------------------------------------------------- 150cdf0e10cSrcweir 151cdf0e10cSrcweir void Main() 152cdf0e10cSrcweir { 153cdf0e10cSrcweir MyWin aMainWin( NULL, WB_STDWORK ); 154cdf0e10cSrcweir aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) ); 155cdf0e10cSrcweir aMainWin.Show(); 156cdf0e10cSrcweir 157cdf0e10cSrcweir Application::Execute(); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir // ----------------------------------------------------------------------- 161cdf0e10cSrcweir 162cdf0e10cSrcweir MyWin::MyWin( Window* pParent, WinBits nWinStyle ) : 163cdf0e10cSrcweir WorkWindow( pParent, nWinStyle ), 164cdf0e10cSrcweir m_aListButton( this, 0 ), 165cdf0e10cSrcweir m_aSvpBitmaps( this, WB_BORDER ), 166cdf0e10cSrcweir m_aImage( this, WB_BORDER ), 167cdf0e10cSrcweir m_aQuitButton( this, 0 ) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) ); 170cdf0e10cSrcweir m_aListButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) ); 171cdf0e10cSrcweir m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) ); 172cdf0e10cSrcweir m_aListButton.Show(); 173cdf0e10cSrcweir 174cdf0e10cSrcweir m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) ); 175cdf0e10cSrcweir m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) ); 176cdf0e10cSrcweir m_aSvpBitmaps.Show(); 177cdf0e10cSrcweir 178cdf0e10cSrcweir m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) ); 179cdf0e10cSrcweir m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None ); 180cdf0e10cSrcweir m_aImage.Show(); 181cdf0e10cSrcweir 182cdf0e10cSrcweir m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) ); 183cdf0e10cSrcweir m_aQuitButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) ); 184cdf0e10cSrcweir m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) ); 185cdf0e10cSrcweir m_aQuitButton.Show(); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir sal_Bool MyWin::Close() 189cdf0e10cSrcweir { 190cdf0e10cSrcweir sal_Bool bRet = WorkWindow::Close(); 191cdf0e10cSrcweir if( bRet ) 192cdf0e10cSrcweir Application::Quit(); 193cdf0e10cSrcweir return bRet; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir void MyWin::parseList( const rtl::OString& rList ) 197cdf0e10cSrcweir { 198cdf0e10cSrcweir sal_Int32 nTokenPos = 0; 199cdf0e10cSrcweir rtl::OUString aElementType; 200cdf0e10cSrcweir m_aSvpBitmaps.Clear(); 201cdf0e10cSrcweir while( nTokenPos >= 0 ) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir rtl::OString aLine = rList.getToken( 0, '\n', nTokenPos ); 204cdf0e10cSrcweir if( ! aLine.getLength() || *aLine.getStr() == '#' ) 205cdf0e10cSrcweir continue; 206cdf0e10cSrcweir 207cdf0e10cSrcweir if( aLine.compareTo( "ElementType: ", 13 ) == 0 ) 208cdf0e10cSrcweir aElementType = rtl::OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US ); 209cdf0e10cSrcweir else 210cdf0e10cSrcweir { 211cdf0e10cSrcweir rtl::OUStringBuffer aNewElement( 64 ); 212cdf0e10cSrcweir aNewElement.append( aElementType ); 213cdf0e10cSrcweir aNewElement.appendAscii( ": " ); 214cdf0e10cSrcweir aNewElement.append( rtl::OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) ); 215cdf0e10cSrcweir m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() ); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir } 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir rtl::OString MyWin::processCommand( const rtl::OString& rCommand ) 221cdf0e10cSrcweir { 222cdf0e10cSrcweir static const char* pEnv = getenv("SVP_LISTENER_PORT"); 223cdf0e10cSrcweir rtl::OStringBuffer aAnswer; 224cdf0e10cSrcweir int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000; 225cdf0e10cSrcweir int nSocket = socket( PF_INET, SOCK_STREAM, 0 ); 226cdf0e10cSrcweir if( nSocket >= 0) 227cdf0e10cSrcweir { 228cdf0e10cSrcweir struct sockaddr_in addr; 229cdf0e10cSrcweir memset(&addr, 0, sizeof(struct sockaddr_in)); 230cdf0e10cSrcweir addr.sin_family = AF_INET; 231cdf0e10cSrcweir addr.sin_port = htons(nPort); 232cdf0e10cSrcweir addr.sin_addr.s_addr = INADDR_ANY; 233cdf0e10cSrcweir if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) ) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir perror( "SvpElementContainer: connect() failed" ); 236cdf0e10cSrcweir close(nSocket); 237cdf0e10cSrcweir } 238cdf0e10cSrcweir else 239cdf0e10cSrcweir { 240cdf0e10cSrcweir write( nSocket, rCommand.getStr(), rCommand.getLength() ); 241cdf0e10cSrcweir write( nSocket, "\n", 1 ); 242cdf0e10cSrcweir char buf[256]; 243cdf0e10cSrcweir ssize_t nBytes = 0; 244cdf0e10cSrcweir do 245cdf0e10cSrcweir { 246cdf0e10cSrcweir nBytes = read( nSocket, buf, sizeof(buf) ); 247cdf0e10cSrcweir aAnswer.append( buf, nBytes ); 248cdf0e10cSrcweir } while( nBytes == sizeof( buf ) ); 249cdf0e10cSrcweir } 250cdf0e10cSrcweir } 251cdf0e10cSrcweir else 252cdf0e10cSrcweir perror( "SvpElementContainer: socket() failed\n" ); 253cdf0e10cSrcweir return aAnswer.makeStringAndClear(); 254cdf0e10cSrcweir } 255cdf0e10cSrcweir 256cdf0e10cSrcweir IMPL_LINK( MyWin, ListHdl, Button*, ) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir parseList( processCommand( "list" ) ); 259cdf0e10cSrcweir return 0; 260cdf0e10cSrcweir } 261cdf0e10cSrcweir 262cdf0e10cSrcweir IMPL_LINK( MyWin, QuitHdl, Button*, ) 263cdf0e10cSrcweir { 264cdf0e10cSrcweir processCommand( "quit" ); 265cdf0e10cSrcweir return 0; 266cdf0e10cSrcweir } 267cdf0e10cSrcweir 268cdf0e10cSrcweir IMPL_LINK( MyWin, SelectHdl, ListBox*, ) 269cdf0e10cSrcweir { 270cdf0e10cSrcweir String aEntry = m_aSvpBitmaps.GetSelectEntry(); 271cdf0e10cSrcweir sal_uInt16 nPos = aEntry.SearchAscii( ": " ); 272cdf0e10cSrcweir if( nPos != STRING_NOTFOUND ) 273cdf0e10cSrcweir { 274cdf0e10cSrcweir OStringBuffer aCommand( 64 ); 275cdf0e10cSrcweir aCommand.append( "get " ); 276cdf0e10cSrcweir aCommand.append( rtl::OUStringToOString( aEntry.Copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) ); 277cdf0e10cSrcweir OString aAnswer( processCommand( aCommand.makeStringAndClear() ) ); 278cdf0e10cSrcweir SvMemoryStream aStream( aAnswer.getLength() ); 279cdf0e10cSrcweir aStream.Write( aAnswer.getStr(), aAnswer.getLength() ); 280cdf0e10cSrcweir aStream.Seek( STREAM_SEEK_TO_BEGIN ); 281cdf0e10cSrcweir Bitmap aBitmap; 282cdf0e10cSrcweir aStream >> aBitmap; 283cdf0e10cSrcweir fprintf( stderr, "got bitmap of size %ldx%ld\n", 284cdf0e10cSrcweir sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()), 285cdf0e10cSrcweir sal::static_int_cast< long >(aBitmap.GetSizePixel().Height())); 286cdf0e10cSrcweir Size aFixedSize( aBitmap.GetSizePixel() ); 287cdf0e10cSrcweir aFixedSize.Width() += 10; 288cdf0e10cSrcweir aFixedSize.Height() += 10; 289cdf0e10cSrcweir m_aImage.SetSizePixel( aFixedSize ); 290cdf0e10cSrcweir m_aImage.SetImage( Image( BitmapEx( aBitmap ) ) ); 291cdf0e10cSrcweir } 292cdf0e10cSrcweir return 0; 293cdf0e10cSrcweir } 294cdf0e10cSrcweir 295cdf0e10cSrcweir // ----------------------------------------------------------------------- 296cdf0e10cSrcweir 297cdf0e10cSrcweir void MyWin::MouseMove( const MouseEvent& rMEvt ) 298cdf0e10cSrcweir { 299cdf0e10cSrcweir WorkWindow::MouseMove( rMEvt ); 300cdf0e10cSrcweir } 301cdf0e10cSrcweir 302cdf0e10cSrcweir // ----------------------------------------------------------------------- 303cdf0e10cSrcweir 304cdf0e10cSrcweir void MyWin::MouseButtonDown( const MouseEvent& rMEvt ) 305cdf0e10cSrcweir { 306cdf0e10cSrcweir WorkWindow::MouseButtonDown( rMEvt ); 307cdf0e10cSrcweir } 308cdf0e10cSrcweir 309cdf0e10cSrcweir // ----------------------------------------------------------------------- 310cdf0e10cSrcweir 311cdf0e10cSrcweir void MyWin::MouseButtonUp( const MouseEvent& rMEvt ) 312cdf0e10cSrcweir { 313cdf0e10cSrcweir WorkWindow::MouseButtonUp( rMEvt ); 314cdf0e10cSrcweir } 315cdf0e10cSrcweir 316cdf0e10cSrcweir // ----------------------------------------------------------------------- 317cdf0e10cSrcweir 318cdf0e10cSrcweir void MyWin::KeyInput( const KeyEvent& rKEvt ) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir WorkWindow::KeyInput( rKEvt ); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir 323cdf0e10cSrcweir // ----------------------------------------------------------------------- 324cdf0e10cSrcweir 325cdf0e10cSrcweir void MyWin::KeyUp( const KeyEvent& rKEvt ) 326cdf0e10cSrcweir { 327cdf0e10cSrcweir WorkWindow::KeyUp( rKEvt ); 328cdf0e10cSrcweir } 329cdf0e10cSrcweir 330cdf0e10cSrcweir // ----------------------------------------------------------------------- 331cdf0e10cSrcweir 332cdf0e10cSrcweir void MyWin::Paint( const Rectangle& rRect ) 333cdf0e10cSrcweir { 334cdf0e10cSrcweir WorkWindow::Paint( rRect ); 335cdf0e10cSrcweir } 336cdf0e10cSrcweir 337cdf0e10cSrcweir // ----------------------------------------------------------------------- 338cdf0e10cSrcweir 339cdf0e10cSrcweir void MyWin::Resize() 340cdf0e10cSrcweir { 341cdf0e10cSrcweir WorkWindow::Resize(); 342cdf0e10cSrcweir } 343