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