andre@3: /* Copyright (C) 2016 by ETH Zürich andre@3: * Software engineering by Intevation GmbH andre@3: * andre@3: * This file is Free Software under the GNU GPL (v>=2) andre@3: * and comes with ABSOLUTELY NO WARRANTY! andre@3: * See LICENSE.txt for details. andre@3: */ andre@3: andre@3: /** @file Main entry point for the application. andre@3: * andre@3: * This file is the wrapper around the qt application. andre@23: * Does command line parsing and preparation of the QApplication. andre@3: */ andre@3: #include "constants.h" andre@3: andre@3: #include "strhelp.h" andre@3: #include "converter.h" andre@3: #include "cconvert_options.h" andre@50: #include "mainwindow.h" andre@3: andre@23: #include andre@3: #include andre@3: #include andre@3: #include andre@3: #include andre@3: #include andre@3: #include andre@3: andre@3: #ifdef Q_OS_WIN andre@3: andre@3: #include andre@3: #include andre@3: andre@3: Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) andre@3: #elif defined(Q_OS_MAC) andre@3: Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin) andre@3: #else andre@3: /* this is only necessary if we build statically for GNU/Linux */ andre@3: // Q_IMPORT_PLUGIN(QXcbIntegrationPlugin) andre@3: #endif andre@3: andre@3: bool g_debug = false; andre@3: andre@3: QtMessageHandler g_default_msg_handler = NULL; andre@3: andre@3: void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) andre@3: { andre@3: if (!g_debug && type == QtDebugMsg) { andre@3: return; andre@3: } andre@3: andre@3: if (g_default_msg_handler) { andre@3: (*g_default_msg_handler)(type, context, msg); andre@3: } andre@3: } andre@3: andre@3: int realMain(int argc, char **argv); andre@3: andre@3: #if defined(WIN32) && defined(UNICODE) andre@3: andre@3: /** @brief Unicode entry point. andre@3: * andre@3: * Converts arguments to UTF-8 and executes the real andre@3: * entry point realMain. andre@3: */ andre@3: int wmain(int argc, wchar_t **argv, wchar_t **envp) andre@3: { andre@3: char **utf8args = NULL; andre@3: andre@3: utf8args = (char**) xmalloc0 ((argc + 1) * sizeof(char*)); andre@3: andre@3: for (int i = 0; i < argc; i++) { andre@3: utf8args[i] = wchar_to_utf8(argv[i], wcslen(argv[i])); andre@3: if (utf8args[i] == NULL) { andre@3: printf ("Fatal: could not convert arguments to UTF-8.\n"); andre@3: exit(-1); andre@3: } andre@3: } andre@3: int ret = realMain(argc, utf8args); andre@3: strv_free(utf8args); andre@3: andre@3: return ret; andre@3: } andre@3: #else andre@3: int main(int argc, char **argv) andre@3: { andre@3: return realMain(argc, argv); andre@3: } andre@3: #endif andre@3: andre@3: /** @brief The real entry point to the application. andre@3: * andre@3: * @param [in] argc the count of the arguments. andre@3: * @param [in] argv On GNU/Linux this function expects argv to be in the andre@3: * native system encoding. On Windows the arguments andre@3: * shall be UTF-8 andre@3: * andre@3: * @returns 0 on success an error code otherwise. */ andre@3: int realMain(int argc, char **argv) andre@3: { andre@23: /* QApplication setup */ andre@23: QApplication app (argc, argv); andre@23: QApplication::setOrganizationName(QStringLiteral(APPNAME)); andre@23: QApplication::setApplicationName(QStringLiteral(APPNAME)); andre@23: QApplication::setApplicationVersion(QStringLiteral(VERSION)); andre@3: // QSettings::setDefaultFormat(QSettings::IniFormat); andre@3: andre@3: /* Setup translations */ andre@3: QTranslator translator; andre@3: if (QLocale::system().name() == "C") { andre@3: /* Useful for testing / development as the primary target is german */ andre@3: translator.load(":/l10n/main_de_DE"); andre@3: } else { andre@3: translator.load(":/l10n/main_" + QLocale::system().name()); andre@3: } andre@3: app.installTranslator(&translator); andre@3: andre@3: /* Parse the command line */ andre@3: QCommandLineParser parser; andre@3: cconvert_options(parser); andre@3: andre@3: parser.process(app); andre@3: andre@4: g_debug = parser.isSet("debug"); andre@3: g_default_msg_handler = qInstallMessageHandler(filterDebugOutput); andre@3: andre@3: andre@3: const QStringList args = parser.positionalArguments(); andre@3: if (args.size() > 1) { andre@3: parser.showHelp(1); andre@3: } andre@3: andre@3: QString infile; andre@3: if (args.size()) { andre@3: infile = args.first(); andre@3: } andre@3: andre@50: MainWindow *mainWin = Q_NULLPTR; andre@50: const QStringList outputs = parser.values("output"); andre@50: const QString title = parser.value("title"); andre@50: if (outputs.isEmpty() || title.isEmpty()) { andre@50: mainWin = new MainWindow(); andre@50: mainWin->setTitle(title); andre@50: mainWin->show(); andre@50: app.exec(); andre@50: delete mainWin; andre@3: } else { andre@50: Converter conv(infile, parser.values("output"), andre@50: parser.value("title")); andre@50: andre@50: conv.start(); andre@50: conv.wait(); andre@50: const QStringList errors = conv.errors(); andre@50: if (!errors.isEmpty()) { andre@50: Q_FOREACH (const QString err, errors) { andre@50: qCritical() << err; andre@50: } andre@50: return 1; andre@3: } andre@3: } andre@50: return 0; andre@3: }