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: 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 throw(RuntimeException); 73 74 75 private: // helper methods 76 void testWriter( const Reference < XComponent > & rComponent ); 77 void registerServices(); 78 Reference< XMultiServiceFactory > m_xSMgr; 79 }; 80 81 void OfficeClientMain::testWriter( const Reference< XComponent > & rComponent ) 82 { 83 printf( "pasting some text into the writer document\n" ); 84 85 Reference< XTextDocument > rTextDoc( rComponent , UNO_QUERY ); 86 Reference< XText > rText = rTextDoc->getText(); 87 Reference< XTextCursor > rCursor = rText->createTextCursor(); 88 Reference< XTextRange > rRange ( rCursor , UNO_QUERY ); 89 90 rText->insertString( rRange, OUString::createFromAscii( "This text has been posted by the officeclient component" ), sal_False ); 91 } 92 93 /******************** 94 * does necessary service registration ( this could be done also by a setup tool ) 95 *********************/ 96 void OfficeClientMain::registerServices( ) 97 { 98 // register services. 99 // Note : this needs to be done only once and is in general done by the setup 100 Reference < XImplementationRegistration > rImplementationRegistration( 101 102 m_xSMgr->createInstance( 103 OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" )), 104 UNO_QUERY ); 105 106 if( ! rImplementationRegistration.is() ) 107 { 108 printf( "Couldn't create registration component\n" ); 109 exit(1); 110 } 111 112 OUString aSharedLibrary[4]; 113 aSharedLibrary[0] = 114 OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION ); 115 aSharedLibrary[1] = 116 OUString::createFromAscii( "remotebridge.uno" SAL_DLLEXTENSION ); 117 aSharedLibrary[2] = 118 OUString::createFromAscii( "bridgefac.uno" SAL_DLLEXTENSION ); 119 aSharedLibrary[3] = 120 OUString::createFromAscii( "uuresolver.uno" SAL_DLLEXTENSION ); 121 122 sal_Int32 i; 123 for( i = 0 ; i < 4 ; i ++ ) 124 { 125 126 // build the system specific library name 127 OUString aDllName = aSharedLibrary[i]; 128 129 try 130 { 131 // register the needed services in the servicemanager 132 rImplementationRegistration->registerImplementation( 133 OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ), 134 aDllName, 135 Reference< XSimpleRegistry > () ); 136 } 137 catch( Exception & ) 138 { 139 printf( "couldn't register dll %s\n" , 140 OUStringToOString( aDllName, RTL_TEXTENCODING_ASCII_US ).getStr() ); 141 } 142 } 143 } 144 145 sal_Int32 OfficeClientMain::run( const Sequence< OUString > & aArguments ) throw ( RuntimeException ) 146 { 147 printf( "Connecting ....\n" ); 148 149 if( aArguments.getLength() == 1 ) 150 { 151 try { 152 registerServices(); 153 Reference < XInterface > r = 154 m_xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ) ); 155 Reference < XUnoUrlResolver > rResolver( r , UNO_QUERY ); 156 r = rResolver->resolve( aArguments.getConstArray()[0] ); 157 158 Reference< XNamingService > rNamingService( r, UNO_QUERY ); 159 if( rNamingService.is() ) 160 { 161 printf( "got the remote NamingService\n" ); 162 163 r = rNamingService->getRegisteredObject(OUString::createFromAscii("StarOffice.ServiceManager")); 164 165 Reference< XMultiServiceFactory > rRemoteSMgr( r , UNO_QUERY ); 166 167 Reference < XComponentLoader > rLoader( 168 rRemoteSMgr->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ))), 169 UNO_QUERY ); 170 171 if( rLoader.is() ) 172 { 173 174 sal_Char *urls[] = { 175 "private:factory/swriter", 176 "private:factory/sdraw", 177 "private:factory/simpress", 178 "private:factory/scalc" 179 }; 180 181 sal_Char *docu[]= { 182 "a new writer document ...\n", 183 "a new draw document ...\n", 184 "a new schedule document ...\n" , 185 "a new calc document ...\n" 186 }; 187 sal_Int32 i; 188 for( i = 0 ; i < 4 ; i ++ ) 189 { 190 printf( "press any key to open %s\n" , docu[i] ); 191 getchar(); 192 193 Reference< XComponent > rComponent = 194 rLoader->loadComponentFromURL( 195 OUString::createFromAscii( urls[i] ) , 196 OUString( RTL_CONSTASCII_USTRINGPARAM("_blank")), 197 0 , 198 Sequence < ::com::sun::star::beans::PropertyValue >() ); 199 200 if( 0 == i ) 201 { 202 testWriter( rComponent ); 203 } 204 printf( "press any key to close the document\n" ); 205 getchar(); 206 rComponent->dispose(); 207 } 208 } 209 } 210 211 } 212 catch( ConnectionSetupException &e ) 213 { 214 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); 215 printf( "%s\n", o.pData->buffer ); 216 printf( "couldn't access local resource ( possible security resons )\n" ); 217 } 218 catch( NoConnectException &e ) 219 { 220 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); 221 printf( "%s\n", o.pData->buffer ); 222 printf( "no server listening on the resource\n" ); 223 } 224 catch( IllegalArgumentException &e ) 225 { 226 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); 227 printf( "%s\n", o.pData->buffer ); 228 printf( "uno url invalid\n" ); 229 } 230 catch( RuntimeException & e ) 231 { 232 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); 233 printf( "%s\n", o.pData->buffer ); 234 printf( "a remote call was aborted\n" ); 235 } 236 } 237 else 238 { 239 printf( "usage: (uno officeclient-component --) uno-url\n" 240 "e.g.: uno:socket,host=localhost,port=2002;urp;StarOffice.NamingService\n" ); 241 return 1; 242 } 243 return 0; 244 } 245 246 Reference< XInterface > SAL_CALL CreateInstance( const Reference< XMultiServiceFactory > &r) 247 { 248 return Reference< XInterface > ( ( OWeakObject * ) new OfficeClientMain(r) ); 249 } 250 251 Sequence< OUString > getSupportedServiceNames() 252 { 253 static Sequence < OUString > *pNames = 0; 254 if( ! pNames ) 255 { 256 MutexGuard guard( Mutex::getGlobalMutex() ); 257 if( !pNames ) 258 { 259 static Sequence< OUString > seqNames(2); 260 seqNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.bridge.example.OfficeClientExample" ); 261 pNames = &seqNames; 262 } 263 } 264 return *pNames; 265 } 266 267 } 268 269 using namespace remotebridges_officeclient; 270 #define IMPLEMENTATION_NAME "com.sun.star.comp.remotebridges.example.OfficeClientSample" 271 272 273 extern "C" 274 { 275 //================================================================================================== 276 void SAL_CALL component_getImplementationEnvironment( 277 const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv ) 278 { 279 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 280 } 281 //================================================================================================== 282 sal_Bool SAL_CALL component_writeInfo( 283 void * pServiceManager, void * pRegistryKey ) 284 { 285 if (pRegistryKey) 286 { 287 try 288 { 289 Reference< XRegistryKey > xNewKey( 290 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey( 291 OUString::createFromAscii( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) ); 292 293 const Sequence< OUString > & rSNL = getSupportedServiceNames(); 294 const OUString * pArray = rSNL.getConstArray(); 295 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; ) 296 xNewKey->createKey( pArray[nPos] ); 297 298 return sal_True; 299 } 300 catch (InvalidRegistryException &) 301 { 302 OSL_ENSURE( sal_False, "### InvalidRegistryException!" ); 303 } 304 } 305 return sal_False; 306 } 307 //================================================================================================== 308 void * SAL_CALL component_getFactory( 309 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) 310 { 311 void * pRet = 0; 312 313 if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0) 314 { 315 Reference< XSingleServiceFactory > xFactory( createSingleFactory( 316 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ), 317 OUString::createFromAscii( pImplName ), 318 CreateInstance, getSupportedServiceNames() ) ); 319 320 if (xFactory.is()) 321 { 322 xFactory->acquire(); 323 pRet = xFactory.get(); 324 } 325 } 326 327 return pRet; 328 } 329 } 330