xref: /trunk/main/bridges/test/testoffice.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 #include <osl/time.h>
31 
32 #include <osl/mutex.hxx>
33 #include <osl/thread.h>
34 
35 #include <cppuhelper/servicefactory.hxx>
36 
37 #include <com/sun/star/connection/XConnector.hpp>
38 
39 #include <com/sun/star/bridge/XBridgeFactory.hpp>
40 
41 #include <com/sun/star/uno/XNamingService.hpp>
42 #include <com/sun/star/io/XInputStream.hpp>
43 #include <com/sun/star/io/XOutputStream.hpp>
44 
45 #include <com/sun/star/text/XTextDocument.hpp>
46 
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 
49 #include <com/sun/star/lang/XComponent.hpp>
50 
51 #include <com/sun/star/frame/XComponentLoader.hpp>
52 
53 #include <cppuhelper/weak.hxx>
54 
55 #include <test/XTestFactory.hpp>
56 
57 using namespace ::test;
58 using namespace ::rtl;
59 using namespace ::cppu;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::io;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::bridge;
64 using namespace ::com::sun::star::registry;
65 using namespace ::com::sun::star::connection;
66 using namespace ::com::sun::star::frame;
67 using namespace ::com::sun::star::text;
68 
69 #include "testcomp.h"
70 
71 #ifdef SAL_W32
72 #include <conio.h>
73 #endif
74 
75 
76 void mygetchar()
77 {
78 #ifdef SAL_W32
79     _getch();
80 #else
81     getchar();
82 #endif
83 }
84 
85 
86 void testPipe( const Reference < XMultiServiceFactory > & rSmgr )
87 {
88     Reference < XOutputStream > rOut(
89         rSmgr->createInstance( OUString::createFromAscii( "com.sun.star.io.Pipe" ) ),
90         UNO_QUERY );
91 
92     OSL_ASSERT( rOut.is() );
93 
94     {
95         Sequence < sal_Int8 > seq( 10 );
96         seq.getArray()[0] = 42;
97         rOut->writeBytes( seq );
98     }
99 
100 
101     {
102         Sequence < sal_Int8 > seq;
103         Reference < XInputStream > rIn( rOut , UNO_QUERY );
104         if( ! ( rIn->available() == 10) )
105             printf( "wrong bytes available\n" );
106         if( ! ( rIn->readBytes( seq , 10 ) == 10 ) )
107             printf( "wrong bytes read\n" );
108         if( ! ( 42 == seq.getArray()[0] ) )
109             printf( "wrong element in sequence\n" );
110 
111 //          OSL_ASSERT( 0 );
112     }
113 }
114 #include<stdio.h>
115 #include<string.h>
116 
117 void testWriter(  const Reference < XComponent > & rCmp )
118 {
119 
120     Reference< XTextDocument > rTextDoc( rCmp , UNO_QUERY );
121 
122     Reference< XText > rText = rTextDoc->getText();
123     Reference< XTextCursor > rCursor = rText->createTextCursor();
124     Reference< XTextRange > rRange ( rCursor , UNO_QUERY );
125 
126     char pcText[1024];
127     pcText[0] = 0;
128     printf( "pleast type any text\n" );
129     while( sal_True )
130     {
131         scanf( "%s" , pcText );
132 
133         if( !strcmp( pcText , "end" ) )
134         {
135             break;
136         }
137 
138         if ( strlen( pcText ) < sizeof(pcText)-1 )
139             strcat( pcText , " " ); // #100211# - checked
140 
141         rText->insertString( rRange , OUString::createFromAscii( pcText ) , sal_False );
142     }
143 }
144 
145 void testDocument( const Reference < XMultiServiceFactory > & rSmgr )
146 {
147     Reference < XComponentLoader > rLoader(
148         rSmgr->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ))),
149         UNO_QUERY );
150 
151     OSL_ASSERT( rLoader.is() );
152 
153     sal_Char *urls[] = {
154         "private:factory/swriter",
155         "private:factory/scalc",
156         "private:factory/sdraw",
157         "http://www.heise.de",
158         "file://h|/remote_interfaces.sdw"
159     };
160 
161     sal_Char *docu[]= {
162         "a new writer document ...\n",
163         "a new calc document ...\n",
164         "a new draw document ...\n",
165         "www.heise.de\n",
166         "the remote_interfaces.sdw doc\n"
167     };
168 
169     sal_Int32 i;
170     for( i = 0 ; i < 1 ; i ++ )
171     {
172         printf( "press any key to open %s\n" , docu[i] );
173         mygetchar();
174 
175         Reference< XComponent > rComponent =
176             rLoader->loadComponentFromURL(
177                 OUString::createFromAscii( urls[i] ) ,
178                 OUString( RTL_CONSTASCII_USTRINGPARAM("_blank")),
179                 0 ,
180                 Sequence < ::com::sun::star::beans::PropertyValue >() );
181 
182         testWriter( rComponent );
183         printf( "press any key to close the document\n" );
184         mygetchar();
185         rComponent->dispose();
186     }
187 
188 }
189 
190 void doSomething( const  Reference < XInterface > &r )
191 {
192     Reference < XNamingService > rName( r, UNO_QUERY );
193     if( rName.is() )
194     {
195         printf( "got the remote naming service !\n" );
196         Reference < XInterface > rXsmgr = rName->getRegisteredObject(
197             OUString::createFromAscii( "StarOffice.ServiceManager" ) );
198 
199         Reference < XMultiServiceFactory > rSmgr( rXsmgr , UNO_QUERY );
200         if( rSmgr.is() )
201         {
202             printf( "got the remote service manager !\n" );
203             testPipe( rSmgr );
204             testDocument( rSmgr );
205         }
206     }
207 }
208 
209 
210 int main( int argc, char *argv[] )
211 {
212     if( argc < 2 )
213     {
214         printf( "usage : testclient host:port" );
215         return 0;
216     }
217 
218     OUString sConnectionString;
219     OUString sProtocol;
220     sal_Bool bLatency = sal_False;
221     sal_Bool bReverse = sal_False;
222     parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
223     {
224         Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
225             OUString( RTL_CONSTASCII_USTRINGPARAM( "client.rdb" )  ) );
226 
227         // just ensure that it is registered
228 
229         Reference < XConnector > rConnector(
230             createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector")),
231                              OUString( RTL_CONSTASCII_USTRINGPARAM("connector.uno" SAL_DLLEXTENSION)),
232                              rSMgr ),
233             UNO_QUERY );
234 
235         createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
236                          OUString( RTL_CONSTASCII_USTRINGPARAM("remotebridge.uno" SAL_DLLEXTENSION)),
237                          rSMgr );
238 
239         Reference < XBridgeFactory > rFactory(
240             createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
241                              OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION)),
242                              rSMgr ),
243             UNO_QUERY );
244 
245         try
246         {
247             if( rFactory.is() && rConnector.is() )
248             {
249                 Reference < XConnection > rConnection =
250                     rConnector->connect( sConnectionString );
251 
252                 Reference < XBridge > rBridge = rFactory->createBridge(
253                     OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")),
254                     sProtocol,
255                     rConnection,
256                     Reference < XInstanceProvider > () );
257 
258                 Reference < XInterface > rInitialObject
259                     = rBridge->getInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("NamingService")) );
260 
261                 if( rInitialObject.is() )
262                 {
263                     printf( "got the remote object\n" );
264                     doSomething( rInitialObject );
265                 }
266                 TimeValue value={2,0};
267                 osl_waitThread( &value );
268             }
269         }
270         catch (... ) {
271             printf( "Exception thrown\n" );
272         }
273 
274         Reference < XComponent > rComp( rSMgr , UNO_QUERY );
275         rComp->dispose();
276     }
277     //_getch();
278     return 0;
279 }
280