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