view ui/main.cpp @ 633:6c090638b2b4

Use static buffer for module file name. According to the msdn examle the return value of getmodulefilename should be used to indicate success and not the size. And according to comments on that function on Windows 8.1 it does not return the needed size. So better be more robust and just use max_path as a limit.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Jun 2014 15:29:48 +0200
parents edf269b6e499
children 80d1a80b3e8d
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * 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.
 */
#include "mainwindow.h"
#include "processhelp.h"
#include "binverify.h"
#include "logging.h"
#include "strhelp.h"

#include <QApplication>
#include <QSystemTrayIcon>
#include <QtPlugin>
#include <QMessageBox>
#include <QSettings>
#include <QDebug>
#include <QTranslator>

#ifndef VERSION
#define VERSION "0.0.1"
#endif

#ifndef APPNAME
#define APPNAME "TrustBridge"
#endif

#ifndef ORGANIZATION
#define ORGANIZATION "BSI"
#endif

#ifdef Q_OS_WIN
 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
#else
 Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
#endif

int main(int argc, char **argv)
{
    /* First verify integrity even before calling QApplication*/
#ifdef Q_OS_WIN
    {
      wchar_t wPath[MAX_PATH];
      char *utf8path = NULL;

      if (!GetModuleFileNameW (NULL, wPath, MAX_PATH - 1)) {
          PRINTLASTERROR ("Failed to obtain module file name. Path too long?");
          syslog_error_printf ("Integrity check failed.");
          return -1;
      }

      /* wPath might not be 0 terminated */
      wPath[MAX_PATH - 1] = '\0';

      utf8path = wchar_to_utf8 (wPath, wcsnlen(wPath, MAX_PATH));

      if (utf8path == NULL) {
          ERRORPRINTF ("Failed to convert module path to utf-8");
          syslog_error_printf ("Integrity check failed.");
          return -1;
      }

      if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid)
        {
          ERRORPRINTF ("Verification of the binary failed");
          syslog_error_printf ("Integrity check failed.");
          xfree(utf8path);
#ifdef RELEASE_BUILD
          return -1;
#endif
        }

      xfree(utf8path);
    }
#else
    if (!verify_binary ("/proc/self/exe", 14) != VerifyValid)
      {
        syslog_error_printf ("Integrity check failed.");
#ifdef RELEASE_BUILD
        return -1;
#endif
      }
#endif

    QApplication app (argc, argv);

    QApplication::setQuitOnLastWindowClosed(false);
    QApplication::setOrganizationName(QString::fromLatin1(ORGANIZATION));
    QApplication::setApplicationName(QString::fromLatin1(APPNAME));
    QApplication::setApplicationVersion(QString::fromLatin1(VERSION));
    QSettings::setDefaultFormat(QSettings::IniFormat);

    QStringList arguments = QApplication::arguments();
    bool trayMode = arguments.contains("--tray");

    QSettings settings;
    settings.beginGroup("settings");
    bool autoStart = settings.value("autostart", true).toBool();
    settings.endGroup();

    if (trayMode && !autoStart) {
        return 0;
    }

    if (ProcessHelp::otherProcessesExist(APPNAME)) {
        qDebug() << "Another " << APPNAME << " process is already running. Exiting.";
        ProcessHelp::activateWindowForProcess(APPNAME);
        return 0;
    }

    QTranslator translator;
    if (QLocale::system().name() == "C") {
        /* Useful for testing / development as the primary target is german */
        translator.load(":/l10n/trustbridge_de_DE");
    } else {
        translator.load(":/l10n/trustbridge_" + QLocale::system().name());
        qDebug() << "Loading translations for: " << "trustbridge_" +
            QLocale::system().name();
    }
    app.installTranslator(&translator);

    if (!QSystemTrayIcon::isSystemTrayAvailable() ||
            !QSystemTrayIcon::supportsMessages()) {
        QMessageBox::critical(0, QString::fromLatin1(APPNAME),
                              QObject::tr("Couldn't detect any system tray "
                                          "on this system. This software can only "
                                          "be used in a desktop environment."));
        return 1;
    }

    MainWindow mainWin(trayMode);

    return app.exec();
}

http://wald.intevation.org/projects/trustbridge/