andre@25: /* Copyright (C) 2015 by ETH Zürich andre@25: * Software engineering by Intevation GmbH andre@0: * andre@0: * This file is Free Software under the GNU GPL (v>=2) andre@0: * and comes with ABSOLUTELY NO WARRANTY! andre@0: * See LICENSE.txt for details. andre@0: */ andre@0: andre@0: /** @file Main entry point for the application. andre@0: * andre@0: * This file is the wrapper around the qt application. andre@0: * Does command line parsing and preparation of the QApplication. andre@0: */ andre@0: #include "constants.h" andre@0: andre@0: #include "mainwindow.h" andre@0: andre@0: #include "strhelp.h" andre@0: #include "util.h" andre@0: andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: andre@0: #ifdef Q_OS_WIN andre@0: andre@0: #include andre@0: #include andre@0: andre@0: Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) andre@62: #elif defined(Q_OS_MAC) andre@62: Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin) andre@0: #else andre@0: /* this is only necessary if we build statically for GNU/Linux */ andre@0: // Q_IMPORT_PLUGIN(QXcbIntegrationPlugin) andre@0: #endif andre@0: andre@0: #ifdef IS_TAG_BUILD andre@0: bool g_debug = false; andre@0: #else andre@0: bool g_debug = true; andre@0: #endif andre@0: andre@0: QtMessageHandler g_default_msg_handler = NULL; andre@0: andre@0: void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) andre@0: { andre@0: if (!g_debug && type == QtDebugMsg) { andre@0: return; andre@0: } andre@0: andre@0: if (g_default_msg_handler) { andre@0: (*g_default_msg_handler)(type, context, msg); andre@0: } andre@0: } andre@0: andre@0: int realMain(int argc, char **argv); andre@0: andre@112: #if defined(_WIN32) && defined(UNICODE) andre@0: andre@0: /** @brief Unicode entry point. andre@0: * andre@0: * Converts arguments to UTF-8 and executes the real andre@0: * entry point realMain. andre@0: */ andre@0: int wmain(int argc, wchar_t **argv, wchar_t **envp) andre@0: { andre@0: char **utf8args = NULL; andre@0: andre@0: utf8args = (char**) xmalloc0 ((argc + 1) * sizeof(char*)); andre@0: andre@0: for (int i = 0; i < argc; i++) { andre@0: utf8args[i] = wchar_to_utf8(argv[i], wcslen(argv[i])); andre@0: if (utf8args[i] == NULL) { andre@0: printf ("Fatal: could not convert arguments to UTF-8.\n"); andre@0: exit(-1); andre@0: } andre@0: } andre@0: int ret = realMain(argc, utf8args); andre@0: strv_free(utf8args); andre@0: andre@0: return ret; andre@0: } andre@0: #else andre@0: int main(int argc, char **argv) andre@0: { andre@0: return realMain(argc, argv); andre@0: } andre@0: #endif andre@0: andre@0: /** @brief The real entry point to the application. andre@0: * andre@0: * @param [in] argc the count of the arguments. andre@0: * @param [in] argv On GNU/Linux this function expects argv to be in the andre@0: * native system encoding. On Windows the arguments andre@0: * shall be UTF-8 andre@0: * andre@0: * @returns 0 on success an error code otherwise. */ andre@0: int realMain(int argc, char **argv) andre@0: { andre@0: /* QApplication setup */ andre@0: QApplication app (argc, argv); andre@0: QApplication::setQuitOnLastWindowClosed(true); andre@31: QApplication::setOrganizationName(QString::fromLatin1(APPNAME)); andre@0: QApplication::setApplicationName(QString::fromLatin1(APPNAME)); andre@0: QApplication::setApplicationVersion(QString::fromLatin1(VERSION)); andre@0: QSettings::setDefaultFormat(QSettings::IniFormat); andre@0: andre@0: /* Setup translations */ andre@0: QTranslator translator; andre@0: if (QLocale::system().name() == "C") { andre@0: /* Useful for testing / development as the primary target is german */ andre@0: translator.load(":/l10n/main_de_DE"); andre@0: } else { andre@0: translator.load(":/l10n/main_" + QLocale::system().name()); andre@0: } andre@0: app.installTranslator(&translator); andre@0: andre@0: /* Install static fonts */ andre@0: andre@0: /* The putenv here works around a bug in qt (#29192). Qt thinks it is a fatal andre@0: * error if the font directory does not exist. Will be fixed in Qt 5.4 */ andre@0: char * inst_dir = get_install_dir(); andre@0: if (inst_dir) { andre@0: qputenv("QT_QPA_FONTDIR", inst_dir); andre@0: xfree(inst_dir); andre@0: } andre@0: int fontId = QFontDatabase::addApplicationFont(":/fonts/DejaVuSans.ttf"); andre@0: if (fontId != -1) andre@0: { andre@0: QFont font("DejaVuSans"); andre@0: font.setPointSize(9); andre@0: app.setFont(font); andre@0: } andre@0: andre@0: /* Parse the command line */ andre@0: QCommandLineParser parser; andre@0: parser.setApplicationDescription(QObject::tr(DESCRIPTION) + "\n" + andre@0: QObject::tr(COPYRIGHT)); andre@0: parser.addHelpOption(); andre@0: andre@0: parser.addPositionalArgument("folder", QCoreApplication::translate("main", "The folder containing the data to replay.")); andre@0: QCommandLineOption debugOpt(QStringList() << "debug", andre@0: QCoreApplication::translate("main", "Print debug output.")); andre@0: parser.addOption(debugOpt); andre@0: andre@0: parser.process(app); andre@0: andre@0: #ifdef IS_TAG_BUILD andre@0: g_debug = parser.isSet(debugOpt); andre@0: #else andre@0: g_debug = true; andre@0: #endif andre@0: g_default_msg_handler = qInstallMessageHandler(filterDebugOutput); andre@0: andre@0: andre@0: /* Initialize the main window */ andre@0: MainWindow mainWin; andre@0: andre@0: const QStringList args = parser.positionalArguments(); andre@9: if (args.size() > 1) { andre@0: parser.showHelp(1); andre@0: } andre@9: if (args.size() == 1) { andre@9: mainWin.setFolder(args.first()); andre@9: } else { andre@9: mainWin.showWithFolderSelect(); andre@9: } andre@0: andre@0: return app.exec(); /* Go event loop */ andre@0: }