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