comparison src/main.cpp @ 3:8b4c49c92451

Add initial implementation that handles choices
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 10:39:19 +0100
parents
children 5c256892042c
comparison
equal deleted inserted replaced
2:4926d626fe15 3:8b4c49c92451
1 /* Copyright (C) 2016 by ETH Zürich
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU GPL (v>=2)
5 * and comes with ABSOLUTELY NO WARRANTY!
6 * See LICENSE.txt for details.
7 */
8
9 /** @file Main entry point for the application.
10 *
11 * This file is the wrapper around the qt application.
12 * Does command line parsing and preparation of the QCoreApplication.
13 */
14 #include "constants.h"
15
16 #include "strhelp.h"
17 #include "converter.h"
18 #include "cconvert_options.h"
19
20 #include <QCoreApplication>
21 #include <QCommandLineParser>
22 #include <QtPlugin>
23 #include <QDebug>
24 #include <QTranslator>
25 #include <QSettings>
26 #include <QTimer>
27
28 #ifdef Q_OS_WIN
29
30 #include <windows.h>
31 #include <shlobj.h>
32
33 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
34 #elif defined(Q_OS_MAC)
35 Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin)
36 #else
37 /* this is only necessary if we build statically for GNU/Linux */
38 // Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
39 #endif
40
41 #ifdef IS_TAG_BUILD
42 bool g_debug = false;
43 #else
44 bool g_debug = true;
45 #endif
46
47 QtMessageHandler g_default_msg_handler = NULL;
48
49 void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
50 {
51 if (!g_debug && type == QtDebugMsg) {
52 return;
53 }
54
55 if (g_default_msg_handler) {
56 (*g_default_msg_handler)(type, context, msg);
57 }
58 }
59
60 int realMain(int argc, char **argv);
61
62 #if defined(WIN32) && defined(UNICODE)
63
64 /** @brief Unicode entry point.
65 *
66 * Converts arguments to UTF-8 and executes the real
67 * entry point realMain.
68 */
69 int wmain(int argc, wchar_t **argv, wchar_t **envp)
70 {
71 char **utf8args = NULL;
72
73 utf8args = (char**) xmalloc0 ((argc + 1) * sizeof(char*));
74
75 for (int i = 0; i < argc; i++) {
76 utf8args[i] = wchar_to_utf8(argv[i], wcslen(argv[i]));
77 if (utf8args[i] == NULL) {
78 printf ("Fatal: could not convert arguments to UTF-8.\n");
79 exit(-1);
80 }
81 }
82 int ret = realMain(argc, utf8args);
83 strv_free(utf8args);
84
85 return ret;
86 }
87 #else
88 int main(int argc, char **argv)
89 {
90 return realMain(argc, argv);
91 }
92 #endif
93
94 /** @brief The real entry point to the application.
95 *
96 * @param [in] argc the count of the arguments.
97 * @param [in] argv On GNU/Linux this function expects argv to be in the
98 * native system encoding. On Windows the arguments
99 * shall be UTF-8
100 *
101 * @returns 0 on success an error code otherwise. */
102 int realMain(int argc, char **argv)
103 {
104 /* QCoreApplication setup */
105 QCoreApplication app (argc, argv);
106 QCoreApplication::setOrganizationName(QStringLiteral(APPNAME));
107 QCoreApplication::setApplicationName(QStringLiteral(APPNAME));
108 QCoreApplication::setApplicationVersion(QStringLiteral(VERSION));
109 // QSettings::setDefaultFormat(QSettings::IniFormat);
110
111 /* Setup translations */
112 QTranslator translator;
113 if (QLocale::system().name() == "C") {
114 /* Useful for testing / development as the primary target is german */
115 translator.load(":/l10n/main_de_DE");
116 } else {
117 translator.load(":/l10n/main_" + QLocale::system().name());
118 }
119 app.installTranslator(&translator);
120
121 /* Parse the command line */
122 QCommandLineParser parser;
123 cconvert_options(parser);
124
125 parser.process(app);
126
127 #ifdef IS_TAG_BUILD
128 g_debug = parser.isSet(debugOpt);
129 #else
130 g_debug = true;
131 #endif
132 g_default_msg_handler = qInstallMessageHandler(filterDebugOutput);
133
134
135 const QStringList args = parser.positionalArguments();
136 if (args.size() > 1) {
137 parser.showHelp(1);
138 }
139
140 ConvertFormat fmt = Format_XLSX;
141 /* Initialize the converter. */
142 if (parser.isSet(QStringLiteral("pdf"))) {
143 fmt = Format_PDF;
144 }
145 QString infile;
146 if (args.size()) {
147 infile = args.first();
148 }
149 Converter conv(infile, parser.value("output"), fmt,
150 parser.value("title"));
151
152 conv.start();
153 conv.wait();
154
155 const QStringList errors = conv.errors();
156 if (errors.isEmpty()) {
157 return 0;
158 } else {
159 Q_FOREACH (const QString err, errors) {
160 qCritical() << err;
161 }
162 }
163 return 1;
164 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)