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