Simplify using designated initializers
[LibreOffice.git] / odk / examples / cpp / Convertor / Convertor.cxx
blob6841c58a3311c0c3f4157955e3764f98e31aace4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <iostream>
11 #include <sal/main.h>
12 #include <cppuhelper/bootstrap.hxx>
13 #include <rtl/bootstrap.hxx>
15 #include <osl/file.hxx>
16 #include <osl/process.h>
17 #include <rtl/process.h>
19 #include <com/sun/star/beans/XPropertySet.hpp>
20 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
21 #include <com/sun/star/frame/Desktop.hpp>
22 #include <com/sun/star/frame/XComponentLoader.hpp>
23 #include <com/sun/star/frame/XStorable.hpp>
24 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
25 #include <com/sun/star/text/XTextDocument.hpp>
27 using namespace cppu;
28 using namespace rtl;
29 using namespace css::uno;
30 using namespace css::beans;
31 using namespace css::bridge;
32 using namespace css::frame;
33 using namespace css::lang;
34 using namespace css::text;
36 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
38 OUString sInputFileName, sOutputFileName;
39 OUString sConnectionString("uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager");
41 sal_Int32 nCount = rtl_getAppCommandArgCount();
43 if (nCount < 1)
45 std::cout
46 << "using: Convertor -env:URE_MORE_TYPES=<office_types_rdb_url> <file_url> "
47 "[<uno_connection_url>]"
48 << std::endl
49 << std::endl
50 << "example: Convertor -env:URE_MORE_TYPES=\"file:///.../program/offapi.rdb\" test.odt"
51 "\"uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager\""
52 << std::endl;
53 exit(1);
56 Reference<XComponentContext> xComponentContext(defaultBootstrap_InitialComponentContext());
57 Reference<XMultiComponentFactory> xMultiComponentFactoryClient(
58 xComponentContext->getServiceManager());
59 Reference<XInterface> xInterface = xMultiComponentFactoryClient->createInstanceWithContext(
60 "com.sun.star.bridge.UnoUrlResolver", xComponentContext);
61 Reference<XUnoUrlResolver> resolver(xInterface, UNO_QUERY);
62 try
64 xInterface = Reference<XInterface>(resolver->resolve(sConnectionString), UNO_QUERY_THROW);
66 catch (Exception& e)
68 std::cout << "Error: cannot establish a connection using '" << sConnectionString << "'"
69 << std::endl
70 << e.Message << std::endl;
71 std::exit(1);
74 Reference<XPropertySet> xPropSet(xInterface, UNO_QUERY);
75 xPropSet->getPropertyValue("DefaultContext") >>= xComponentContext;
76 Reference<XMultiComponentFactory> xMultiComponentFactoryServer(
77 xComponentContext->getServiceManager());
78 Reference<XDesktop2> xComponentLoader = Desktop::create(xComponentContext);
79 Sequence<PropertyValue> loadProperties(1);
80 loadProperties[0].Name = "Hidden";
81 loadProperties[0].Value <<= true;
82 try
84 OUString sInputUrl, sAbsoluteInputUrl, sOutputUrl, sAbsoluteOutputUrl, sWorkingDir;
85 osl_getProcessWorkingDir(&sWorkingDir.pData);
86 rtl_getAppCommandArg(0, &sInputFileName.pData);
88 osl::FileBase::getFileURLFromSystemPath(sInputFileName, sInputUrl);
89 osl::FileBase::getAbsoluteFileURL(sWorkingDir, sInputUrl, sAbsoluteInputUrl);
90 std::cout << sAbsoluteInputUrl << std::endl;
92 osl::FileBase::getFileURLFromSystemPath("output.pdf", sOutputUrl);
93 osl::FileBase::getAbsoluteFileURL(sWorkingDir, sOutputUrl, sAbsoluteOutputUrl);
94 std::cout << sAbsoluteOutputUrl << std::endl;
96 Reference<XComponent> xComponent = xComponentLoader->loadComponentFromURL(
97 sAbsoluteInputUrl, "_blank", 0, loadProperties);
98 Reference<XTextDocument> xDocument(xComponent, UNO_QUERY_THROW);
99 Reference<XStorable> xStorable(xDocument, UNO_QUERY_THROW);
100 Sequence<PropertyValue> storeProps(3);
101 storeProps[0].Name = "FilterName";
102 storeProps[0].Value <<= OUString("writer_pdf_Export");
103 storeProps[1].Name = "Overwrite";
104 storeProps[1].Value <<= true;
105 storeProps[2].Name = "SelectPdfVersion";
106 storeProps[2].Value <<= sal_Int32(1);
107 xStorable->storeToURL(sAbsoluteOutputUrl, storeProps);
108 Reference<XComponent>::query(xMultiComponentFactoryClient)->dispose();
109 std::cout << "Output output.pdf generated." << std::endl;
111 catch (Exception& e)
113 std::cout << "Can not open the input file." << std::endl << e.Message << std::endl;
115 return 0;
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */