Update ooo320-m1
[ooovba.git] / odk / examples / cpp / DocumentLoader / DocumentLoader.cxx
blob2a6841abc3178af449ea1e9947bad549bc6ae10b
1 /*************************************************************************
3 * $RCSfile: DocumentLoader.cxx,v $
5 * $Revision: 1.13 $
7 * last change: $Author: rt $ $Date: 2008-07-11 14:25:41 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 /*****************************************************************************
42 *****************************************************************************
44 * Simple client application using the UnoUrlResolver service.
46 *****************************************************************************
47 *****************************************************************************/
48 #include <stdio.h>
49 #include <wchar.h>
51 #include <cppuhelper/bootstrap.hxx>
53 #include <osl/file.hxx>
54 #include <osl/process.h>
56 #include <com/sun/star/beans/XPropertySet.hpp>
57 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
58 #include <com/sun/star/frame/XComponentLoader.hpp>
59 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
60 #include <com/sun/star/registry/XSimpleRegistry.hpp>
62 #include <string.h>
64 using namespace rtl;
65 using namespace com::sun::star::uno;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::beans;
68 using namespace com::sun::star::bridge;
69 using namespace com::sun::star::frame;
70 using namespace com::sun::star::registry;
73 //============================================================================
74 int SAL_CALL main( int argc, char **argv )
76 OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"));
77 if (argc < 2)
79 printf("using: DocumentLoader <file_url> [<uno_connection_url>]\n\n"
80 "example: DocumentLoader \"file:///e:/temp/test.odt\" \"uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager\"\n");
81 exit(1);
83 if (argc == 3)
85 sConnectionString = OUString::createFromAscii(argv[2]);
88 // Creates a simple registry service instance.
89 Reference< XSimpleRegistry > xSimpleRegistry(
90 ::cppu::createSimpleRegistry() );
92 // Connects the registry to a persistent data source represented by an URL.
93 xSimpleRegistry->open( OUString( RTL_CONSTASCII_USTRINGPARAM(
94 "DocumentLoader.rdb") ), sal_True, sal_False );
96 /* Bootstraps an initial component context with service manager upon a given
97 registry. This includes insertion of initial services:
98 - (registry) service manager, shared lib loader,
99 - simple registry, nested registry,
100 - implementation registration
101 - registry typedescription provider, typedescription manager (also
102 installs it into cppu core)
104 Reference< XComponentContext > xComponentContext(
105 ::cppu::bootstrap_InitialComponentContext( xSimpleRegistry ) );
107 /* Gets the service manager instance to be used (or null). This method has
108 been added for convenience, because the service manager is a often used
109 object.
111 Reference< XMultiComponentFactory > xMultiComponentFactoryClient(
112 xComponentContext->getServiceManager() );
114 /* Creates an instance of a component which supports the services specified
115 by the factory.
117 Reference< XInterface > xInterface =
118 xMultiComponentFactoryClient->createInstanceWithContext(
119 OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ),
120 xComponentContext );
122 Reference< XUnoUrlResolver > resolver( xInterface, UNO_QUERY );
124 // Resolves the component context from the office, on the uno URL given by argv[1].
127 xInterface = Reference< XInterface >(
128 resolver->resolve( sConnectionString ), UNO_QUERY );
130 catch ( Exception& e )
132 printf("Error: cannot establish a connection using '%s':\n %s\n",
133 OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
134 OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
135 exit(1);
138 // gets the server component context as property of the office component factory
139 Reference< XPropertySet > xPropSet( xInterface, UNO_QUERY );
140 xPropSet->getPropertyValue( OUString::createFromAscii("DefaultContext") ) >>= xComponentContext;
142 // gets the service manager from the office
143 Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
144 xComponentContext->getServiceManager() );
146 /* Creates an instance of a component which supports the services specified
147 by the factory. Important: using the office component context.
149 Reference < XComponentLoader > xComponentLoader(
150 xMultiComponentFactoryServer->createInstanceWithContext(
151 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ) ),
152 xComponentContext ), UNO_QUERY );
154 /* Loads a component specified by an URL into the specified new or existing
155 frame.
157 OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl;
158 osl_getProcessWorkingDir(&sWorkingDir.pData);
159 osl::FileBase::getFileURLFromSystemPath( OUString::createFromAscii(argv[1]), sDocPathUrl);
160 osl::FileBase::getAbsoluteFileURL( sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
162 Reference< XComponent > xComponent = xComponentLoader->loadComponentFromURL(
163 sAbsoluteDocUrl, OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ), 0,
164 Sequence < ::com::sun::star::beans::PropertyValue >() );
166 // dispose the local service manager
167 Reference< XComponent >::query( xMultiComponentFactoryClient )->dispose();
169 return 0;