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