1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/mutex.hxx>
21 #include <cppuhelper/factory.hxx>
23 #include <cppuhelper/servicefactory.hxx>
25 #include <com/sun/star/uno/XNamingService.hpp>
27 #include <com/sun/star/registry/ImplementationRegistration.hpp>
29 #include <com/sun/star/connection/XConnector.hpp>
31 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
33 #include <com/sun/star/lang/XMain.hpp>
34 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/frame/Desktop.hpp>
37 #include <com/sun/star/frame/XComponentLoader.hpp>
39 #include <com/sun/star/text/XTextDocument.hpp>
41 #include <cppuhelper/implbase1.hxx>
43 using namespace ::cppu
;
44 using namespace ::osl
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::registry
;
48 using namespace ::com::sun::star::connection
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::com::sun::star::bridge
;
51 using namespace ::com::sun::star::text
;
52 using namespace ::com::sun::star::frame
;
56 namespace remotebridges_officeclient
{
58 class OfficeClientMain
: public WeakImplHelper1
< XMain
>
61 OfficeClientMain( const Reference
< XMultiServiceFactory
> &r
) :
67 virtual sal_Int32 SAL_CALL
run( const Sequence
< OUString
>& aArguments
)
68 throw(RuntimeException
);
71 private: // helper methods
72 void testWriter( const Reference
< XComponent
> & rComponent
);
73 void registerServices();
74 Reference
< XMultiServiceFactory
> m_xSMgr
;
77 void OfficeClientMain::testWriter( const Reference
< XComponent
> & rComponent
)
79 printf( "pasting some text into the writer document\n" );
81 Reference
< XTextDocument
> rTextDoc( rComponent
, UNO_QUERY
);
82 Reference
< XText
> rText
= rTextDoc
->getText();
83 Reference
< XTextCursor
> rCursor
= rText
->createTextCursor();
84 Reference
< XTextRange
> rRange ( rCursor
, UNO_QUERY
);
86 rText
->insertString( rRange
, OUString("This text has been posted by the officeclient component"), sal_False
);
90 * does necessary service registration ( this could be done also by a setup tool )
91 *********************/
92 void OfficeClientMain::registerServices( )
95 // Note : this needs to be done only once and is in general done by the setup
96 Reference
< XImplementationRegistration
> rImplementationRegistration(
97 ImplementationRegistration::create(m_xSMgr
) );
99 if( ! rImplementationRegistration
.is() )
101 printf( "Couldn't create registration component\n" );
105 OUString aSharedLibrary
[4];
107 OUString( "connector.uno" SAL_DLLEXTENSION
);
109 OUString( "remotebridge.uno" SAL_DLLEXTENSION
);
111 OUString( "bridgefac.uno" SAL_DLLEXTENSION
);
113 OUString( "uuresolver.uno" SAL_DLLEXTENSION
);
116 for( i
= 0 ; i
< 4 ; i
++ )
119 // build the system specific library name
120 OUString aDllName
= aSharedLibrary
[i
];
124 // register the needed services in the servicemanager
125 rImplementationRegistration
->registerImplementation(
126 OUString("com.sun.star.loader.SharedLibrary"),
128 Reference
< XSimpleRegistry
> () );
132 printf( "couldn't register dll %s\n" ,
133 OUStringToOString( aDllName
, RTL_TEXTENCODING_ASCII_US
).getStr() );
138 sal_Int32
OfficeClientMain::run( const Sequence
< OUString
> & aArguments
) throw ( RuntimeException
)
140 printf( "Connecting ....\n" );
142 if( aArguments
.getLength() == 1 )
146 Reference
< XInterface
> r
=
147 m_xSMgr
->createInstance("com.sun.star.bridge.UnoUrlResolver");
148 Reference
< XUnoUrlResolver
> rResolver( r
, UNO_QUERY
);
149 r
= rResolver
->resolve( aArguments
.getConstArray()[0] );
151 Reference
< XNamingService
> rNamingService( r
, UNO_QUERY
);
152 if( rNamingService
.is() )
154 printf( "got the remote NamingService\n" );
156 r
= rNamingService
->getRegisteredObject(OUString("StarOffice.ServiceManager"));
158 Reference
< XMultiServiceFactory
> rRemoteSMgr( r
, UNO_QUERY
);
160 Reference
< XDesktop2
> rLoader
= Desktop::create( comphelper::getComponentContext(r
) );
163 "private:factory/swriter",
164 "private:factory/sdraw",
165 "private:factory/simpress",
166 "private:factory/scalc"
170 "a new writer document ...\n",
171 "a new draw document ...\n",
172 "a new schedule document ...\n" ,
173 "a new calc document ...\n"
176 for( i
= 0 ; i
< 4 ; i
++ )
178 printf( "press any key to open %s\n" , docu
[i
] );
181 Reference
< XComponent
> rComponent
=
182 rLoader
->loadComponentFromURL(
183 OUString::createFromAscii( urls
[i
] ) ,
186 Sequence
< ::com::sun::star::beans::PropertyValue
>() );
190 testWriter( rComponent
);
192 printf( "press any key to close the document\n" );
194 rComponent
->dispose();
199 catch( const ConnectionSetupException
&e
)
201 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
202 printf( "%s\n", o
.pData
->buffer
);
203 printf( "couldn't access local resource ( possible security resons )\n" );
205 catch( const NoConnectException
&e
)
207 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
208 printf( "%s\n", o
.pData
->buffer
);
209 printf( "no server listening on the resource\n" );
211 catch( const IllegalArgumentException
&e
)
213 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
214 printf( "%s\n", o
.pData
->buffer
);
215 printf( "uno url invalid\n" );
217 catch( const RuntimeException
& e
)
219 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
220 printf( "%s\n", o
.pData
->buffer
);
221 printf( "a remote call was aborted\n" );
226 printf( "usage: (uno officeclient-component --) uno-url\n"
227 "e.g.: uno:socket,host=localhost,port=2002;urp;StarOffice.NamingService\n" );
233 Reference
< XInterface
> SAL_CALL
CreateInstance( const Reference
< XMultiServiceFactory
> &r
)
235 return Reference
< XInterface
> ( ( OWeakObject
* ) new OfficeClientMain(r
) );
238 Sequence
< OUString
> getSupportedServiceNames()
240 static Sequence
< OUString
> *pNames
= 0;
243 MutexGuard
guard( Mutex::getGlobalMutex() );
246 static Sequence
< OUString
> seqNames(2);
247 seqNames
[0] = "com.sun.star.bridge.example.OfficeClientExample";
256 using namespace remotebridges_officeclient
;
257 #define IMPLEMENTATION_NAME "com.sun.star.comp.remotebridges.example.OfficeClientSample"
263 sal_Bool SAL_CALL
component_writeInfo(
264 void * pServiceManager
, void * pRegistryKey
)
270 Reference
< XRegistryKey
> xNewKey(
271 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
272 OUString( "/" IMPLEMENTATION_NAME
"/UNO/SERVICES" ) ) );
274 const Sequence
< OUString
> & rSNL
= getSupportedServiceNames();
275 const OUString
* pArray
= rSNL
.getConstArray();
276 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
277 xNewKey
->createKey( pArray
[nPos
] );
281 catch (InvalidRegistryException
&)
283 OSL_FAIL( "### InvalidRegistryException!" );
289 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(
290 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
294 if (pServiceManager
&& rtl_str_compare( pImplName
, IMPLEMENTATION_NAME
) == 0)
296 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
297 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
298 OUString::createFromAscii( pImplName
),
299 CreateInstance
, getSupportedServiceNames() ) );
304 pRet
= xFactory
.get();
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */