view src/main.cpp @ 91:5b815897657d

Add icon for macos bundle
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 06 Oct 2016 15:58:04 +0200
parents 82d38421fdf3
children
line wrap: on
line source
/* 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 QApplication.
 */
#include "constants.h"

#include "strhelp.h"
#include "converter.h"
#include "cconvert_options.h"
#include "mainwindow.h"

#include <QApplication>
#include <QCommandLineParser>
#include <QtPlugin>
#include <QDebug>
#include <QTranslator>
#include <QTextCodec>
#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

bool g_debug = false;

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)
{
    /* QApplication setup */
    QApplication app (argc, argv);
    QApplication::setOrganizationName(QStringLiteral(APPNAME));
    QApplication::setApplicationName(QStringLiteral(APPNAME));
    QApplication::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);

#ifdef Q_OS_WIN
    auto oldCodec = QTextCodec::codecForLocale();
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#endif
    parser.process(app);
#ifdef Q_OS_WIN
    QTextCodec::setCodecForLocale(oldCodec);
#endif

    g_debug = parser.isSet("debug");
    g_default_msg_handler = qInstallMessageHandler(filterDebugOutput);


    const QStringList args = parser.positionalArguments();
    if (args.size() > 1) {
        parser.showHelp(1);
    }

    QString infile;
    if (args.size()) {
        infile = args.first();
    }

    MainWindow *mainWin = Q_NULLPTR;
    const QStringList outputs = parser.values("output");
    const QString title = parser.value("title");
    if (outputs.isEmpty() || title.isEmpty()) {
        mainWin = new MainWindow();
        mainWin->setTitle(title);
        if (!infile.isEmpty()) {
            mainWin->setInputFile(infile);
        }
        mainWin->show();
        app.exec();
        delete mainWin;
    } else {
        Converter conv(infile, parser.values("output"),
                       parser.value("title"));

        conv.start();
        conv.wait();
        const QStringList errors = conv.errors();
        if (!errors.isEmpty()) {
            Q_FOREACH (const QString err, errors) {
                qCritical() << err;
            }
            return 1;
        }
    }
    return 0;
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)