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 #include <stdio.h>
28*cdf0e10cSrcweir #include <osl/mutex.hxx>
29*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/lang/XMain.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace ::rtl;
51*cdf0e10cSrcweir using namespace ::cppu;
52*cdf0e10cSrcweir using namespace ::osl;
53*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
54*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
55*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
56*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
57*cdf0e10cSrcweir using namespace ::com::sun::star::container;
58*cdf0e10cSrcweir using namespace ::com::sun::star::bridge;
59*cdf0e10cSrcweir using namespace ::com::sun::star::text;
60*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir namespace remotebridges_officeclient {
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir class OfficeClientMain : public WeakImplHelper1< XMain >
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir public:
69*cdf0e10cSrcweir 	OfficeClientMain( const Reference< XMultiServiceFactory > &r ) :
70*cdf0e10cSrcweir 		m_xSMgr( r )
71*cdf0e10cSrcweir 		{}
72*cdf0e10cSrcweir public:     // Methods
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL run( const Sequence< OUString >& aArguments )
76*cdf0e10cSrcweir 		throw(RuntimeException);
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir private: // helper methods
80*cdf0e10cSrcweir 	void testWriter( const Reference < XComponent > & rComponent );
81*cdf0e10cSrcweir 	void registerServices();
82*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > m_xSMgr;
83*cdf0e10cSrcweir };
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir void OfficeClientMain::testWriter( const Reference< XComponent > & rComponent )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir 	printf( "pasting some text into the writer document\n" );
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	Reference< XTextDocument > rTextDoc( rComponent , UNO_QUERY );
90*cdf0e10cSrcweir 	Reference< XText > rText = rTextDoc->getText();
91*cdf0e10cSrcweir 	Reference< XTextCursor > rCursor = rText->createTextCursor();
92*cdf0e10cSrcweir 	Reference< XTextRange > rRange ( rCursor , UNO_QUERY );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	rText->insertString( rRange, OUString::createFromAscii( "This text has been posted by the officeclient component" ), sal_False );
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir /********************
98*cdf0e10cSrcweir  * does necessary service registration ( this could be done also by a setup tool )
99*cdf0e10cSrcweir  *********************/
100*cdf0e10cSrcweir void OfficeClientMain::registerServices( )
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir 	// register services.
103*cdf0e10cSrcweir 	// Note : this needs to be done only once and is in general done by the setup
104*cdf0e10cSrcweir 	Reference < XImplementationRegistration > rImplementationRegistration(
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 		m_xSMgr->createInstance(
107*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" )),
108*cdf0e10cSrcweir 		UNO_QUERY );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	if( ! rImplementationRegistration.is() )
111*cdf0e10cSrcweir 	{
112*cdf0e10cSrcweir 		printf( "Couldn't create registration component\n" );
113*cdf0e10cSrcweir 		exit(1);
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	OUString aSharedLibrary[4];
117*cdf0e10cSrcweir 	aSharedLibrary[0] =
118*cdf0e10cSrcweir         OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION );
119*cdf0e10cSrcweir 	aSharedLibrary[1] =
120*cdf0e10cSrcweir         OUString::createFromAscii( "remotebridge.uno" SAL_DLLEXTENSION );
121*cdf0e10cSrcweir 	aSharedLibrary[2] =
122*cdf0e10cSrcweir         OUString::createFromAscii( "bridgefac.uno" SAL_DLLEXTENSION );
123*cdf0e10cSrcweir 	aSharedLibrary[3] =
124*cdf0e10cSrcweir         OUString::createFromAscii( "uuresolver.uno" SAL_DLLEXTENSION );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	sal_Int32 i;
127*cdf0e10cSrcweir 	for( i = 0 ; i < 4 ; i ++ )
128*cdf0e10cSrcweir 	{
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		// build the system specific library name
131*cdf0e10cSrcweir 		OUString aDllName = aSharedLibrary[i];
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		try
134*cdf0e10cSrcweir 		{
135*cdf0e10cSrcweir 			// register the needed services in the servicemanager
136*cdf0e10cSrcweir 			rImplementationRegistration->registerImplementation(
137*cdf0e10cSrcweir 				OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
138*cdf0e10cSrcweir 				aDllName,
139*cdf0e10cSrcweir 				Reference< XSimpleRegistry > () );
140*cdf0e10cSrcweir 		}
141*cdf0e10cSrcweir 		catch( Exception & )
142*cdf0e10cSrcweir 		{
143*cdf0e10cSrcweir 			printf( "couldn't register dll %s\n" ,
144*cdf0e10cSrcweir 					OUStringToOString( aDllName, RTL_TEXTENCODING_ASCII_US ).getStr() );
145*cdf0e10cSrcweir 		}
146*cdf0e10cSrcweir 	}
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir sal_Int32 OfficeClientMain::run( const Sequence< OUString > & aArguments ) throw ( RuntimeException )
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	printf( "Connecting ....\n" );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	if( aArguments.getLength() == 1 )
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		try {
156*cdf0e10cSrcweir 			registerServices();
157*cdf0e10cSrcweir 			Reference < XInterface > r =
158*cdf0e10cSrcweir 				m_xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ) );
159*cdf0e10cSrcweir 			Reference < XUnoUrlResolver > rResolver( r , UNO_QUERY );
160*cdf0e10cSrcweir 			r = rResolver->resolve( aArguments.getConstArray()[0] );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 			Reference< XNamingService > rNamingService( r, UNO_QUERY );
163*cdf0e10cSrcweir 			if( rNamingService.is() )
164*cdf0e10cSrcweir 			{
165*cdf0e10cSrcweir 				printf( "got the remote NamingService\n" );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 				r = rNamingService->getRegisteredObject(OUString::createFromAscii("StarOffice.ServiceManager"));
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 				Reference< XMultiServiceFactory > rRemoteSMgr( r , UNO_QUERY );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 				Reference < XComponentLoader > rLoader(
172*cdf0e10cSrcweir 					rRemoteSMgr->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ))),
173*cdf0e10cSrcweir 					UNO_QUERY );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 				if( rLoader.is() )
176*cdf0e10cSrcweir 				{
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 					sal_Char *urls[] = {
179*cdf0e10cSrcweir 						"private:factory/swriter",
180*cdf0e10cSrcweir 						"private:factory/sdraw",
181*cdf0e10cSrcweir 						"private:factory/simpress",
182*cdf0e10cSrcweir 						"private:factory/scalc"
183*cdf0e10cSrcweir 					};
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 					sal_Char *docu[]= {
186*cdf0e10cSrcweir 						"a new writer document ...\n",
187*cdf0e10cSrcweir 						"a new draw document ...\n",
188*cdf0e10cSrcweir 						"a new schedule document ...\n" ,
189*cdf0e10cSrcweir 						"a new calc document ...\n"
190*cdf0e10cSrcweir 					};
191*cdf0e10cSrcweir 					sal_Int32 i;
192*cdf0e10cSrcweir 					for( i = 0 ; i < 4 ; i ++ )
193*cdf0e10cSrcweir 					{
194*cdf0e10cSrcweir 						printf( "press any key to open %s\n" , docu[i] );
195*cdf0e10cSrcweir 						getchar();
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 						Reference< XComponent > rComponent =
198*cdf0e10cSrcweir 							rLoader->loadComponentFromURL(
199*cdf0e10cSrcweir 								OUString::createFromAscii( urls[i] ) ,
200*cdf0e10cSrcweir 								OUString( RTL_CONSTASCII_USTRINGPARAM("_blank")),
201*cdf0e10cSrcweir 								0 ,
202*cdf0e10cSrcweir 								Sequence < ::com::sun::star::beans::PropertyValue >() );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 						if( 0 == i )
205*cdf0e10cSrcweir 						{
206*cdf0e10cSrcweir 							testWriter( rComponent );
207*cdf0e10cSrcweir 						}
208*cdf0e10cSrcweir 						printf( "press any key to close the document\n" );
209*cdf0e10cSrcweir 						getchar();
210*cdf0e10cSrcweir 						rComponent->dispose();
211*cdf0e10cSrcweir 					}
212*cdf0e10cSrcweir 				}
213*cdf0e10cSrcweir 			}
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 		}
216*cdf0e10cSrcweir 		catch( ConnectionSetupException &e )
217*cdf0e10cSrcweir 		{
218*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
219*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
220*cdf0e10cSrcweir 			printf( "couldn't access local resource ( possible security resons )\n" );
221*cdf0e10cSrcweir 		}
222*cdf0e10cSrcweir 		catch( NoConnectException &e )
223*cdf0e10cSrcweir 		{
224*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
225*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
226*cdf0e10cSrcweir 			printf( "no server listening on the resource\n" );
227*cdf0e10cSrcweir 		}
228*cdf0e10cSrcweir 		catch( IllegalArgumentException &e )
229*cdf0e10cSrcweir 		{
230*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
231*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
232*cdf0e10cSrcweir 			printf( "uno url invalid\n" );
233*cdf0e10cSrcweir 		}
234*cdf0e10cSrcweir 		catch( RuntimeException & e )
235*cdf0e10cSrcweir 		{
236*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
237*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
238*cdf0e10cSrcweir 			printf( "a remote call was aborted\n" );
239*cdf0e10cSrcweir 		}
240*cdf0e10cSrcweir 	}
241*cdf0e10cSrcweir 	else
242*cdf0e10cSrcweir 	{
243*cdf0e10cSrcweir 		printf( "usage: (uno officeclient-component --) uno-url\n"
244*cdf0e10cSrcweir 		        "e.g.:  uno:socket,host=localhost,port=2002;urp;StarOffice.NamingService\n" );
245*cdf0e10cSrcweir 		return 1;
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir 	return 0;
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir Reference< XInterface > SAL_CALL CreateInstance( const Reference< XMultiServiceFactory > &r)
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir 	return Reference< XInterface > ( ( OWeakObject * ) new OfficeClientMain(r) );
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir Sequence< OUString > getSupportedServiceNames()
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
258*cdf0e10cSrcweir 	if( ! pNames )
259*cdf0e10cSrcweir 	{
260*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
261*cdf0e10cSrcweir 		if( !pNames )
262*cdf0e10cSrcweir 		{
263*cdf0e10cSrcweir 			static Sequence< OUString > seqNames(2);
264*cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.bridge.example.OfficeClientExample" );
265*cdf0e10cSrcweir 			pNames = &seqNames;
266*cdf0e10cSrcweir 		}
267*cdf0e10cSrcweir 	}
268*cdf0e10cSrcweir 	return *pNames;
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir using namespace remotebridges_officeclient;
274*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.remotebridges.example.OfficeClientSample"
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir extern "C"
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir //==================================================================================================
280*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
281*cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir //==================================================================================================
286*cdf0e10cSrcweir sal_Bool SAL_CALL component_writeInfo(
287*cdf0e10cSrcweir 	void * pServiceManager, void * pRegistryKey )
288*cdf0e10cSrcweir {
289*cdf0e10cSrcweir 	if (pRegistryKey)
290*cdf0e10cSrcweir 	{
291*cdf0e10cSrcweir 		try
292*cdf0e10cSrcweir 		{
293*cdf0e10cSrcweir 			Reference< XRegistryKey > xNewKey(
294*cdf0e10cSrcweir 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
295*cdf0e10cSrcweir 					OUString::createFromAscii( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 			const Sequence< OUString > & rSNL = getSupportedServiceNames();
298*cdf0e10cSrcweir 			const OUString * pArray = rSNL.getConstArray();
299*cdf0e10cSrcweir 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
300*cdf0e10cSrcweir 				xNewKey->createKey( pArray[nPos] );
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 			return sal_True;
303*cdf0e10cSrcweir 		}
304*cdf0e10cSrcweir 		catch (InvalidRegistryException &)
305*cdf0e10cSrcweir 		{
306*cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
307*cdf0e10cSrcweir 		}
308*cdf0e10cSrcweir 	}
309*cdf0e10cSrcweir 	return sal_False;
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir //==================================================================================================
312*cdf0e10cSrcweir void * SAL_CALL component_getFactory(
313*cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir 	void * pRet = 0;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir 	if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
318*cdf0e10cSrcweir 	{
319*cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
320*cdf0e10cSrcweir 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
321*cdf0e10cSrcweir 			OUString::createFromAscii( pImplName ),
322*cdf0e10cSrcweir 			CreateInstance, getSupportedServiceNames() ) );
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 		if (xFactory.is())
325*cdf0e10cSrcweir 		{
326*cdf0e10cSrcweir 			xFactory->acquire();
327*cdf0e10cSrcweir 			pRet = xFactory.get();
328*cdf0e10cSrcweir 		}
329*cdf0e10cSrcweir 	}
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir 	return pRet;
332*cdf0e10cSrcweir }
333*cdf0e10cSrcweir }
334