xref: /AOO41X/main/odk/examples/cpp/DocumentLoader/DocumentLoader.cxx (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /*****************************************************************************
25cdf0e10cSrcweir  *****************************************************************************
26cdf0e10cSrcweir  *
27cdf0e10cSrcweir  * Simple client application using the UnoUrlResolver service.
28cdf0e10cSrcweir  *
29cdf0e10cSrcweir  *****************************************************************************
30cdf0e10cSrcweir  *****************************************************************************/
31cdf0e10cSrcweir #include <stdio.h>
32cdf0e10cSrcweir #include <wchar.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <sal/main.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <osl/file.hxx>
39cdf0e10cSrcweir #include <osl/process.h>
40cdf0e10cSrcweir #include <rtl/process.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
43cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
44cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
45cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
46cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <string.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace rtl;
51cdf0e10cSrcweir using namespace com::sun::star::uno;
52cdf0e10cSrcweir using namespace com::sun::star::lang;
53cdf0e10cSrcweir using namespace com::sun::star::beans;
54cdf0e10cSrcweir using namespace com::sun::star::bridge;
55cdf0e10cSrcweir using namespace com::sun::star::frame;
56cdf0e10cSrcweir using namespace com::sun::star::registry;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //============================================================================
61cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"));
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     sal_Int32 nCount = (sal_Int32)rtl_getAppCommandArgCount();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	if (nCount < 1)
68cdf0e10cSrcweir 	{
69cdf0e10cSrcweir 		printf("using: DocumentLoader -env:URE_MORE_TYPES=<office_types_rdb_url> <file_url> [<uno_connection_url>]\n\n"
70cdf0e10cSrcweir 			   "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");
71cdf0e10cSrcweir 		exit(1);
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir  	if (nCount == 2)
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 		rtl_getAppCommandArg(1, &sConnectionString.pData);
76cdf0e10cSrcweir 	}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	Reference< XComponentContext > xComponentContext(::cppu::defaultBootstrap_InitialComponentContext());
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /* Gets the service manager instance to be used (or null). This method has
81cdf0e10cSrcweir        been added for convenience, because the service manager is a often used
82cdf0e10cSrcweir        object.
83cdf0e10cSrcweir     */
84cdf0e10cSrcweir 	Reference< XMultiComponentFactory > xMultiComponentFactoryClient(
85cdf0e10cSrcweir 		xComponentContext->getServiceManager() );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     /* Creates an instance of a component which supports the services specified
88cdf0e10cSrcweir        by the factory.
89cdf0e10cSrcweir     */
90cdf0e10cSrcweir     Reference< XInterface > xInterface =
91cdf0e10cSrcweir         xMultiComponentFactoryClient->createInstanceWithContext(
92cdf0e10cSrcweir             OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ),
93cdf0e10cSrcweir             xComponentContext );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     Reference< XUnoUrlResolver > resolver( xInterface, UNO_QUERY );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     // Resolves the component context from the office, on the uno URL given by argv[1].
98cdf0e10cSrcweir     try
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         xInterface = Reference< XInterface >(
101cdf0e10cSrcweir             resolver->resolve( sConnectionString ), UNO_QUERY );
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir     catch ( Exception& e )
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir 		printf("Error: cannot establish a connection using '%s':\n       %s\n",
106cdf0e10cSrcweir                OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
107cdf0e10cSrcweir                OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
108cdf0e10cSrcweir 		exit(1);
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     // gets the server component context as property of the office component factory
112cdf0e10cSrcweir     Reference< XPropertySet > xPropSet( xInterface, UNO_QUERY );
113cdf0e10cSrcweir     xPropSet->getPropertyValue( OUString::createFromAscii("DefaultContext") ) >>= xComponentContext;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     // gets the service manager from the office
116cdf0e10cSrcweir     Reference< XMultiComponentFactory > xMultiComponentFactoryServer(
117cdf0e10cSrcweir         xComponentContext->getServiceManager() );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     /* Creates an instance of a component which supports the services specified
120cdf0e10cSrcweir        by the factory. Important: using the office component context.
121cdf0e10cSrcweir     */
122cdf0e10cSrcweir     Reference < XComponentLoader > xComponentLoader(
123cdf0e10cSrcweir         xMultiComponentFactoryServer->createInstanceWithContext(
124cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ) ),
125cdf0e10cSrcweir             xComponentContext ), UNO_QUERY );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     /* Loads a component specified by an URL into the specified new or existing
128cdf0e10cSrcweir        frame.
129cdf0e10cSrcweir     */
130cdf0e10cSrcweir     OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl, sArgDocUrl;
131cdf0e10cSrcweir     rtl_getAppCommandArg(0, &sArgDocUrl.pData);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     osl_getProcessWorkingDir(&sWorkingDir.pData);
134cdf0e10cSrcweir     osl::FileBase::getFileURLFromSystemPath( sArgDocUrl, sDocPathUrl);
135cdf0e10cSrcweir     osl::FileBase::getAbsoluteFileURL( sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     Reference< XComponent > xComponent = xComponentLoader->loadComponentFromURL(
138cdf0e10cSrcweir         sAbsoluteDocUrl, OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ), 0,
139cdf0e10cSrcweir         Sequence < ::com::sun::star::beans::PropertyValue >() );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	// dispose the local service manager
142cdf0e10cSrcweir     Reference< XComponent >::query( xMultiComponentFactoryClient )->dispose();
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     return 0;
145cdf0e10cSrcweir }
146