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 ************************************************************************/
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>
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"
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
63 using namespace comphelper
;
64 using namespace ::com::sun::star::uno
;
65 using namespace ::com::sun::star::lang
;
66 // -----------------------------------------------------------------------
68 // Forward declaration
71 // -----------------------------------------------------------------------
75 tools::extendApplicationEnvironment();
77 //-------------------------------------------------
78 // create the global service-manager
79 //-------------------------------------------------
80 Reference
< XMultiServiceFactory
> xFactory
;
83 Reference
< XComponentContext
> xCtx
= defaultBootstrap_InitialComponentContext();
84 xFactory
= Reference
< XMultiServiceFactory
>( xCtx
->getServiceManager(), UNO_QUERY
);
86 setProcessServiceFactory( xFactory
);
88 catch( com::sun::star::uno::Exception
& rExc
)
94 fprintf( stderr
, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
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
107 ::ucbhelper::ContentBroker::initialize( xFactory
, aArgs
);
109 #if OSL_DEBUG_LEVEL > 1
112 fprintf( stderr
, "Error creating UCB, installation must be in disorder. Exiting.\n" );
124 // -----------------------------------------------------------------------
126 class MyWin
: public WorkWindow
128 PushButton m_aListButton
;
129 ListBox m_aSvpBitmaps
;
130 ImageControl m_aImage
;
131 PushButton m_aQuitButton
;
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
);
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 // -----------------------------------------------------------------------
157 MyWin
aMainWin( NULL
, WB_STDWORK
);
158 aMainWin
.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "SvpClient" ) ) );
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
);
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();
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() == '#' )
211 if( aLine
.compareTo( "ElementType: ", 13 ) == 0 )
212 aElementType
= rtl::OStringToOUString( aLine
.copy( 13 ), RTL_TEXTENCODING_ASCII_US
);
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 );
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" );
244 write( nSocket
, rCommand
.getStr(), rCommand
.getLength() );
245 write( nSocket
, "\n", 1 );
250 nBytes
= read( nSocket
, buf
, sizeof(buf
) );
251 aAnswer
.append( buf
, nBytes
);
252 } while( nBytes
== sizeof( buf
) );
256 perror( "SvpElementContainer: socket() failed\n" );
257 return aAnswer
.makeStringAndClear();
260 IMPL_LINK( MyWin
, ListHdl
, Button
*, )
262 parseList( processCommand( "list" ) );
266 IMPL_LINK( MyWin
, QuitHdl
, Button
*, )
268 processCommand( "quit" );
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
);
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
) ) );
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 // -----------------------------------------------------------------------
345 WorkWindow::Resize();