1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: officeclient.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/mutex.hxx>
32 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/servicefactory.hxx>
36 #include <com/sun/star/uno/XNamingService.hpp>
38 #include <com/sun/star/registry/XImplementationRegistration.hpp>
40 #include <com/sun/star/connection/XConnector.hpp>
42 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
44 #include <com/sun/star/lang/XMain.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
47 #include <com/sun/star/frame/XComponentLoader.hpp>
49 #include <com/sun/star/text/XTextDocument.hpp>
51 #include <cppuhelper/implbase1.hxx>
53 using namespace ::rtl
;
54 using namespace ::cppu
;
55 using namespace ::osl
;
56 using namespace ::com::sun::star::uno
;
57 using namespace ::com::sun::star::lang
;
58 using namespace ::com::sun::star::registry
;
59 using namespace ::com::sun::star::connection
;
60 using namespace ::com::sun::star::container
;
61 using namespace ::com::sun::star::bridge
;
62 using namespace ::com::sun::star::text
;
63 using namespace ::com::sun::star::frame
;
67 namespace remotebridges_officeclient
{
69 class OfficeClientMain
: public WeakImplHelper1
< XMain
>
72 OfficeClientMain( const Reference
< XMultiServiceFactory
> &r
) :
78 virtual sal_Int32 SAL_CALL
run( const Sequence
< OUString
>& aArguments
)
79 throw(RuntimeException
);
82 private: // helper methods
83 void testWriter( const Reference
< XComponent
> & rComponent
);
84 void registerServices();
85 Reference
< XMultiServiceFactory
> m_xSMgr
;
88 void OfficeClientMain::testWriter( const Reference
< XComponent
> & rComponent
)
90 printf( "pasting some text into the writer document\n" );
92 Reference
< XTextDocument
> rTextDoc( rComponent
, UNO_QUERY
);
93 Reference
< XText
> rText
= rTextDoc
->getText();
94 Reference
< XTextCursor
> rCursor
= rText
->createTextCursor();
95 Reference
< XTextRange
> rRange ( rCursor
, UNO_QUERY
);
97 rText
->insertString( rRange
, OUString::createFromAscii( "This text has been posted by the officeclient component" ), sal_False
);
100 /********************
101 * does necessary service registration ( this could be done also by a setup tool )
102 *********************/
103 void OfficeClientMain::registerServices( )
105 // register services.
106 // Note : this needs to be done only once and is in general done by the setup
107 Reference
< XImplementationRegistration
> rImplementationRegistration(
109 m_xSMgr
->createInstance(
110 OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" )),
113 if( ! rImplementationRegistration
.is() )
115 printf( "Couldn't create registration component\n" );
119 OUString aSharedLibrary
[4];
121 OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION
);
123 OUString::createFromAscii( "remotebridge.uno" SAL_DLLEXTENSION
);
125 OUString::createFromAscii( "bridgefac.uno" SAL_DLLEXTENSION
);
127 OUString::createFromAscii( "uuresolver.uno" SAL_DLLEXTENSION
);
130 for( i
= 0 ; i
< 4 ; i
++ )
133 // build the system specific library name
134 OUString aDllName
= aSharedLibrary
[i
];
138 // register the needed services in the servicemanager
139 rImplementationRegistration
->registerImplementation(
140 OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
142 Reference
< XSimpleRegistry
> () );
146 printf( "couldn't register dll %s\n" ,
147 OUStringToOString( aDllName
, RTL_TEXTENCODING_ASCII_US
).getStr() );
152 sal_Int32
OfficeClientMain::run( const Sequence
< OUString
> & aArguments
) throw ( RuntimeException
)
154 printf( "Connecting ....\n" );
156 if( aArguments
.getLength() == 1 )
160 Reference
< XInterface
> r
=
161 m_xSMgr
->createInstance( OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ) );
162 Reference
< XUnoUrlResolver
> rResolver( r
, UNO_QUERY
);
163 r
= rResolver
->resolve( aArguments
.getConstArray()[0] );
165 Reference
< XNamingService
> rNamingService( r
, UNO_QUERY
);
166 if( rNamingService
.is() )
168 printf( "got the remote NamingService\n" );
170 r
= rNamingService
->getRegisteredObject(OUString::createFromAscii("StarOffice.ServiceManager"));
172 Reference
< XMultiServiceFactory
> rRemoteSMgr( r
, UNO_QUERY
);
174 Reference
< XComponentLoader
> rLoader(
175 rRemoteSMgr
->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ))),
182 "private:factory/swriter",
183 "private:factory/sdraw",
184 "private:factory/simpress",
185 "private:factory/scalc"
189 "a new writer document ...\n",
190 "a new draw document ...\n",
191 "a new schedule document ...\n" ,
192 "a new calc document ...\n"
195 for( i
= 0 ; i
< 4 ; i
++ )
197 printf( "press any key to open %s\n" , docu
[i
] );
200 Reference
< XComponent
> rComponent
=
201 rLoader
->loadComponentFromURL(
202 OUString::createFromAscii( urls
[i
] ) ,
203 OUString( RTL_CONSTASCII_USTRINGPARAM("_blank")),
205 Sequence
< ::com::sun::star::beans::PropertyValue
>() );
209 testWriter( rComponent
);
211 printf( "press any key to close the document\n" );
213 rComponent
->dispose();
219 catch( ConnectionSetupException
&e
)
221 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
222 printf( "%s\n", o
.pData
->buffer
);
223 printf( "couldn't access local resource ( possible security resons )\n" );
225 catch( NoConnectException
&e
)
227 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
228 printf( "%s\n", o
.pData
->buffer
);
229 printf( "no server listening on the resource\n" );
231 catch( IllegalArgumentException
&e
)
233 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
234 printf( "%s\n", o
.pData
->buffer
);
235 printf( "uno url invalid\n" );
237 catch( RuntimeException
& e
)
239 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
240 printf( "%s\n", o
.pData
->buffer
);
241 printf( "a remote call was aborted\n" );
246 printf( "usage: (uno officeclient-component --) uno-url\n"
247 "e.g.: uno:socket,host=localhost,port=2002;urp;StarOffice.NamingService\n" );
253 Reference
< XInterface
> SAL_CALL
CreateInstance( const Reference
< XMultiServiceFactory
> &r
)
255 return Reference
< XInterface
> ( ( OWeakObject
* ) new OfficeClientMain(r
) );
258 Sequence
< OUString
> getSupportedServiceNames()
260 static Sequence
< OUString
> *pNames
= 0;
263 MutexGuard
guard( Mutex::getGlobalMutex() );
266 static Sequence
< OUString
> seqNames(2);
267 seqNames
.getArray()[0] = OUString::createFromAscii( "com.sun.star.bridge.example.OfficeClientExample" );
276 using namespace remotebridges_officeclient
;
277 #define IMPLEMENTATION_NAME "com.sun.star.comp.remotebridges.example.OfficeClientSample"
282 //==================================================================================================
283 void SAL_CALL
component_getImplementationEnvironment(
284 const sal_Char
** ppEnvTypeName
, uno_Environment
** ppEnv
)
286 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
288 //==================================================================================================
289 sal_Bool SAL_CALL
component_writeInfo(
290 void * pServiceManager
, void * pRegistryKey
)
296 Reference
< XRegistryKey
> xNewKey(
297 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
298 OUString::createFromAscii( "/" IMPLEMENTATION_NAME
"/UNO/SERVICES" ) ) );
300 const Sequence
< OUString
> & rSNL
= getSupportedServiceNames();
301 const OUString
* pArray
= rSNL
.getConstArray();
302 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
303 xNewKey
->createKey( pArray
[nPos
] );
307 catch (InvalidRegistryException
&)
309 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
314 //==================================================================================================
315 void * SAL_CALL
component_getFactory(
316 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
320 if (pServiceManager
&& rtl_str_compare( pImplName
, IMPLEMENTATION_NAME
) == 0)
322 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
323 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
324 OUString::createFromAscii( pImplName
),
325 CreateInstance
, getSupportedServiceNames() ) );
330 pRet
= xFactory
.get();