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@3: * Does command line parsing and preparation of the QCoreApplication. 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@3: andre@3: #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@3: /* QCoreApplication setup */ andre@3: QCoreApplication app (argc, argv); andre@3: QCoreApplication::setOrganizationName(QStringLiteral(APPNAME)); andre@3: QCoreApplication::setApplicationName(QStringLiteral(APPNAME)); andre@3: QCoreApplication::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: ConvertFormat fmt = Format_XLSX; andre@3: /* Initialize the converter. */ andre@3: if (parser.isSet(QStringLiteral("pdf"))) { andre@3: fmt = Format_PDF; andre@3: } andre@3: QString infile; andre@3: if (args.size()) { andre@3: infile = args.first(); andre@3: } andre@3: Converter conv(infile, parser.value("output"), fmt, andre@3: parser.value("title")); andre@3: andre@3: conv.start(); andre@3: conv.wait(); andre@3: andre@3: const QStringList errors = conv.errors(); andre@3: if (errors.isEmpty()) { andre@3: return 0; andre@3: } else { andre@3: Q_FOREACH (const QString err, errors) { andre@3: qCritical() << err; andre@3: } andre@3: } andre@3: return 1; andre@3: }