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