1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svpclient.cxx,v $
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 ************************************************************************/
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>
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"
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
66 using namespace comphelper
;
67 using namespace ::com::sun::star::uno
;
68 using namespace ::com::sun::star::lang
;
69 // -----------------------------------------------------------------------
71 // Forward declaration
74 // -----------------------------------------------------------------------
78 tools::extendApplicationEnvironment();
80 //-------------------------------------------------
81 // create the global service-manager
82 //-------------------------------------------------
83 Reference
< XMultiServiceFactory
> xFactory
;
86 Reference
< XComponentContext
> xCtx
= defaultBootstrap_InitialComponentContext();
87 xFactory
= Reference
< XMultiServiceFactory
>( xCtx
->getServiceManager(), UNO_QUERY
);
89 setProcessServiceFactory( xFactory
);
91 catch( com::sun::star::uno::Exception
& rExc
)
97 fprintf( stderr
, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
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
110 ::ucbhelper::ContentBroker::initialize( xFactory
, aArgs
);
112 #if OSL_DEBUG_LEVEL > 1
115 fprintf( stderr
, "Error creating UCB, installation must be in disorder. Exiting.\n" );
127 // -----------------------------------------------------------------------
129 class MyWin
: public WorkWindow
131 PushButton m_aListButton
;
132 ListBox m_aSvpBitmaps
;
133 ImageControl m_aImage
;
134 PushButton m_aQuitButton
;
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
);
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 // -----------------------------------------------------------------------
160 MyWin
aMainWin( NULL
, WB_STDWORK
);
161 aMainWin
.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) );
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
);
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();
197 BOOL bRet
= WorkWindow::Close();
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() == '#' )
214 if( aLine
.compareTo( "ElementType: ", 13 ) == 0 )
215 aElementType
= rtl::OStringToOUString( aLine
.copy( 13 ), RTL_TEXTENCODING_ASCII_US
);
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 );
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" );
247 write( nSocket
, rCommand
.getStr(), rCommand
.getLength() );
248 write( nSocket
, "\n", 1 );
253 nBytes
= read( nSocket
, buf
, sizeof(buf
) );
254 aAnswer
.append( buf
, nBytes
);
255 } while( nBytes
== sizeof( buf
) );
259 perror( "SvpElementContainer: socket() failed\n" );
260 return aAnswer
.makeStringAndClear();
263 IMPL_LINK( MyWin
, ListHdl
, Button
*, )
265 parseList( processCommand( "list" ) );
269 IMPL_LINK( MyWin
, QuitHdl
, Button
*, )
271 processCommand( "quit" );
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
);
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
) );
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 // -----------------------------------------------------------------------
348 WorkWindow::Resize();