xref: /AOO41X/main/odk/examples/cpp/DocumentLoader/DocumentLoader.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /*****************************************************************************
36*cdf0e10cSrcweir  *****************************************************************************
37*cdf0e10cSrcweir  *
38*cdf0e10cSrcweir  * Simple client application using the UnoUrlResolver service.
39*cdf0e10cSrcweir  *
40*cdf0e10cSrcweir  *****************************************************************************
41*cdf0e10cSrcweir  *****************************************************************************/
42*cdf0e10cSrcweir #include <stdio.h>
43*cdf0e10cSrcweir #include <wchar.h>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <sal/main.h>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <osl/file.hxx>
50*cdf0e10cSrcweir #include <osl/process.h>
51*cdf0e10cSrcweir #include <rtl/process.h>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir #include <string.h>
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir using namespace rtl;
62*cdf0e10cSrcweir using namespace com::sun::star::uno;
63*cdf0e10cSrcweir using namespace com::sun::star::lang;
64*cdf0e10cSrcweir using namespace com::sun::star::beans;
65*cdf0e10cSrcweir using namespace com::sun::star::bridge;
66*cdf0e10cSrcweir using namespace com::sun::star::frame;
67*cdf0e10cSrcweir using namespace com::sun::star::registry;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir //============================================================================
72*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"));
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     sal_Int32 nCount = (sal_Int32)rtl_getAppCommandArgCount();
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	if (nCount < 1)
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		printf("using: DocumentLoader -env:URE_MORE_TYPES=<office_types_rdb_url> <file_url> [<uno_connection_url>]\n\n"
81*cdf0e10cSrcweir 			   "example: DocumentLoader -env:URE_MORE_TYPES=\"file:///.../basis-link/program/offapi.rdb\" \"file:///e:/temp/test.odt\" \"uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager\"\n");
82*cdf0e10cSrcweir 		exit(1);
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir  	if (nCount == 2)
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		rtl_getAppCommandArg(1, &sConnectionString.pData);
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	Reference< XComponentContext > xComponentContext(::cppu::defaultBootstrap_InitialComponentContext());
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     /* Gets the service manager instance to be used (or null). This method has
92*cdf0e10cSrcweir        been added for convenience, because the service manager is a often used
93*cdf0e10cSrcweir        object.
94*cdf0e10cSrcweir     */
95*cdf0e10cSrcweir 	Reference< XMultiComponentFactory > xMultiComponentFactoryClient(
96*cdf0e10cSrcweir 		xComponentContext->getServiceManager() );
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     /* Creates an instance of a component which supports the services specified
99*cdf0e10cSrcweir        by the factory.
100*cdf0e10cSrcweir     */
101*cdf0e10cSrcweir     Reference< XInterface > xInterface =
102*cdf0e10cSrcweir         xMultiComponentFactoryClient->createInstanceWithContext(
103*cdf0e10cSrcweir             OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ),
104*cdf0e10cSrcweir             xComponentContext );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     Reference< XUnoUrlResolver > resolver( xInterface, UNO_QUERY );
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     // Resolves the component context from the office, on the uno URL given by argv[1].
109*cdf0e10cSrcweir     try
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         xInterface = Reference< XInterface >(
112*cdf0e10cSrcweir             resolver->resolve( sConnectionString ), UNO_QUERY );
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir     catch ( Exception& e )
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir 		printf("Error: cannot establish a connection using '%s':\n       %s\n",
117*cdf0e10cSrcweir                OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
118*cdf0e10cSrcweir                OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
119*cdf0e10cSrcweir 		exit(1);
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     // gets the server component context as property of the office component factory
123*cdf0e10cSrcweir     Reference< XPropertySet > xPropSet( xInterface, UNO_QUERY );
124*cdf0e10cSrcweir     xPropSet->getPropertyValue( OUString::createFromAscii("DefaultContext") ) >>= xComponentContext;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     // gets the service manager from the office
127*cdf0e10cSrcweir     Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
128*cdf0e10cSrcweir         xComponentContext->getServiceManager() );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     /* Creates an instance of a component which supports the services specified
131*cdf0e10cSrcweir        by the factory. Important: using the office component context.
132*cdf0e10cSrcweir     */
133*cdf0e10cSrcweir     Reference < XComponentLoader > xComponentLoader(
134*cdf0e10cSrcweir         xMultiComponentFactoryServer->createInstanceWithContext(
135*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ) ),
136*cdf0e10cSrcweir             xComponentContext ), UNO_QUERY );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     /* Loads a component specified by an URL into the specified new or existing
139*cdf0e10cSrcweir        frame.
140*cdf0e10cSrcweir     */
141*cdf0e10cSrcweir     OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl, sArgDocUrl;
142*cdf0e10cSrcweir     rtl_getAppCommandArg(0, &sArgDocUrl.pData);
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     osl_getProcessWorkingDir(&sWorkingDir.pData);
145*cdf0e10cSrcweir     osl::FileBase::getFileURLFromSystemPath( sArgDocUrl, sDocPathUrl);
146*cdf0e10cSrcweir     osl::FileBase::getAbsoluteFileURL( sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     Reference< XComponent > xComponent = xComponentLoader->loadComponentFromURL(
149*cdf0e10cSrcweir         sAbsoluteDocUrl, OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ), 0,
150*cdf0e10cSrcweir         Sequence < ::com::sun::star::beans::PropertyValue >() );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	// dispose the local service manager
153*cdf0e10cSrcweir     Reference< XComponent >::query( xMultiComponentFactoryClient )->dispose();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     return 0;
156*cdf0e10cSrcweir }
157