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