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 
24 /*****************************************************************************
25  *****************************************************************************
26  *
27  * Simple client application using the UnoUrlResolver service.
28  *
29  *****************************************************************************
30  *****************************************************************************/
31 #include <stdio.h>
32 #include <wchar.h>
33 
34 #include <sal/main.h>
35 
36 #include <cppuhelper/bootstrap.hxx>
37 
38 #include <osl/file.hxx>
39 #include <osl/process.h>
40 #include <rtl/process.h>
41 
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
44 #include <com/sun/star/frame/XComponentLoader.hpp>
45 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
46 #include <com/sun/star/registry/XSimpleRegistry.hpp>
47 
48 #include <string.h>
49 
50 using namespace rtl;
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::lang;
53 using namespace com::sun::star::beans;
54 using namespace com::sun::star::bridge;
55 using namespace com::sun::star::frame;
56 using namespace com::sun::star::registry;
57 
58 
59 
60 //============================================================================
61 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
62 {
63     OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"));
64 
65     sal_Int32 nCount = (sal_Int32)rtl_getAppCommandArgCount();
66 
67 	if (nCount < 1)
68 	{
69 		printf("using: DocumentLoader -env:URE_MORE_TYPES=<office_types_rdb_url> <file_url> [<uno_connection_url>]\n\n"
70 			   "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");
71 		exit(1);
72 	}
73  	if (nCount == 2)
74 	{
75 		rtl_getAppCommandArg(1, &sConnectionString.pData);
76 	}
77 
78 	Reference< XComponentContext > xComponentContext(::cppu::defaultBootstrap_InitialComponentContext());
79 
80     /* Gets the service manager instance to be used (or null). This method has
81        been added for convenience, because the service manager is a often used
82        object.
83     */
84 	Reference< XMultiComponentFactory > xMultiComponentFactoryClient(
85 		xComponentContext->getServiceManager() );
86 
87     /* Creates an instance of a component which supports the services specified
88        by the factory.
89     */
90     Reference< XInterface > xInterface =
91         xMultiComponentFactoryClient->createInstanceWithContext(
92             OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ),
93             xComponentContext );
94 
95     Reference< XUnoUrlResolver > resolver( xInterface, UNO_QUERY );
96 
97     // Resolves the component context from the office, on the uno URL given by argv[1].
98     try
99     {
100         xInterface = Reference< XInterface >(
101             resolver->resolve( sConnectionString ), UNO_QUERY );
102     }
103     catch ( Exception& e )
104     {
105 		printf("Error: cannot establish a connection using '%s':\n       %s\n",
106                OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
107                OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
108 		exit(1);
109     }
110 
111     // gets the server component context as property of the office component factory
112     Reference< XPropertySet > xPropSet( xInterface, UNO_QUERY );
113     xPropSet->getPropertyValue( OUString::createFromAscii("DefaultContext") ) >>= xComponentContext;
114 
115     // gets the service manager from the office
116     Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
117         xComponentContext->getServiceManager() );
118 
119     /* Creates an instance of a component which supports the services specified
120        by the factory. Important: using the office component context.
121     */
122     Reference < XComponentLoader > xComponentLoader(
123         xMultiComponentFactoryServer->createInstanceWithContext(
124             OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ) ),
125             xComponentContext ), UNO_QUERY );
126 
127     /* Loads a component specified by an URL into the specified new or existing
128        frame.
129     */
130     OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl, sArgDocUrl;
131     rtl_getAppCommandArg(0, &sArgDocUrl.pData);
132 
133     osl_getProcessWorkingDir(&sWorkingDir.pData);
134     osl::FileBase::getFileURLFromSystemPath( sArgDocUrl, sDocPathUrl);
135     osl::FileBase::getAbsoluteFileURL( sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
136 
137     Reference< XComponent > xComponent = xComponentLoader->loadComponentFromURL(
138         sAbsoluteDocUrl, OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ), 0,
139         Sequence < ::com::sun::star::beans::PropertyValue >() );
140 
141 	// dispose the local service manager
142     Reference< XComponent >::query( xMultiComponentFactoryClient )->dispose();
143 
144     return 0;
145 }
146