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