diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main.cpp	Tue Mar 22 10:39:19 2016 +0100
@@ -0,0 +1,164 @@
+/* Copyright (C) 2016 by ETH Zürich
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=2)
+ * and comes with ABSOLUTELY NO WARRANTY!
+ * See LICENSE.txt for details.
+ */
+
+/** @file Main entry point for the application.
+ *
+ * This file is the wrapper around the qt application.
+ * Does command line parsing and preparation of the QCoreApplication.
+ */
+#include "constants.h"
+
+#include "strhelp.h"
+#include "converter.h"
+#include "cconvert_options.h"
+
+#include <QCoreApplication>
+#include <QCommandLineParser>
+#include <QtPlugin>
+#include <QDebug>
+#include <QTranslator>
+#include <QSettings>
+#include <QTimer>
+
+#ifdef Q_OS_WIN
+
+ #include <windows.h>
+ #include <shlobj.h>
+
+ Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
+#elif defined(Q_OS_MAC)
+ Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin)
+#else
+/* this is only necessary if we build statically for GNU/Linux */
+// Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
+#endif
+
+#ifdef IS_TAG_BUILD
+bool g_debug = false;
+#else
+bool g_debug = true;
+#endif
+
+QtMessageHandler g_default_msg_handler = NULL;
+
+void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+    if (!g_debug && type == QtDebugMsg) {
+        return;
+    }
+
+    if (g_default_msg_handler) {
+        (*g_default_msg_handler)(type, context, msg);
+    }
+}
+
+int realMain(int argc, char **argv);
+
+#if defined(WIN32) && defined(UNICODE)
+
+/** @brief Unicode entry point.
+ *
+ * Converts arguments to UTF-8 and executes the real
+ * entry point realMain.
+ */
+int wmain(int argc, wchar_t **argv, wchar_t **envp)
+{
+    char **utf8args = NULL;
+
+    utf8args = (char**) xmalloc0 ((argc + 1) * sizeof(char*));
+
+    for (int i = 0; i < argc; i++) {
+        utf8args[i] = wchar_to_utf8(argv[i], wcslen(argv[i]));
+        if (utf8args[i] == NULL) {
+            printf ("Fatal: could not convert arguments to UTF-8.\n");
+            exit(-1);
+        }
+    }
+    int ret = realMain(argc, utf8args);
+    strv_free(utf8args);
+
+    return ret;
+}
+#else
+int main(int argc, char **argv)
+{
+    return realMain(argc, argv);
+}
+#endif
+
+/** @brief The real entry point to the application.
+ *
+ * @param [in] argc the count of the arguments.
+ * @param [in] argv On GNU/Linux this function expects argv to be in the
+ * native system encoding. On Windows the arguments
+ * shall be UTF-8
+ *
+ * @returns 0 on success an error code otherwise. */
+int realMain(int argc, char **argv)
+{
+    /* QCoreApplication setup */
+    QCoreApplication app (argc, argv);
+    QCoreApplication::setOrganizationName(QStringLiteral(APPNAME));
+    QCoreApplication::setApplicationName(QStringLiteral(APPNAME));
+    QCoreApplication::setApplicationVersion(QStringLiteral(VERSION));
+//    QSettings::setDefaultFormat(QSettings::IniFormat);
+
+    /* Setup translations */
+    QTranslator translator;
+    if (QLocale::system().name() == "C") {
+        /* Useful for testing / development as the primary target is german */
+        translator.load(":/l10n/main_de_DE");
+    } else {
+        translator.load(":/l10n/main_" + QLocale::system().name());
+    }
+    app.installTranslator(&translator);
+
+    /* Parse the command line */
+    QCommandLineParser parser;
+    cconvert_options(parser);
+
+    parser.process(app);
+
+#ifdef IS_TAG_BUILD
+    g_debug = parser.isSet(debugOpt);
+#else
+    g_debug = true;
+#endif
+    g_default_msg_handler = qInstallMessageHandler(filterDebugOutput);
+
+
+    const QStringList args = parser.positionalArguments();
+    if (args.size() > 1) {
+        parser.showHelp(1);
+    }
+
+    ConvertFormat fmt = Format_XLSX;
+    /* Initialize the converter. */
+    if (parser.isSet(QStringLiteral("pdf"))) {
+        fmt = Format_PDF;
+    }
+    QString infile;
+    if (args.size()) {
+        infile = args.first();
+    }
+    Converter conv(infile, parser.value("output"), fmt,
+                   parser.value("title"));
+
+    conv.start();
+    conv.wait();
+
+    const QStringList errors = conv.errors();
+    if (errors.isEmpty()) {
+        return 0;
+    } else {
+        Q_FOREACH (const QString err, errors) {
+            qCritical() << err;
+        }
+    }
+    return 1;
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)