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