summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 9874f007abdee0faa105e010d74446c66d41ad21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <QMap>
#include <QString>
#include <QSettings>
#include <QTranslator>
#include <QtAlgorithms>
#include <QtWidgets/QApplication>
#include <QDesktopWidget>
#include <QLocale>
#include <QtDebug>
#include <QMessageBox>

#include <cstdlib>
#include <iostream>
#include <string>

#include "config.h"
#include "dialog.h"
#include "globals.h"
#include "xsession.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load(":" + QLocale::system().name());
    a.installTranslator(&translator);

    if (!Config::init(a, Config::CONFIG)) {
        qDebug() << "Error initializing config. Invalid --config passed?";
        return 1;
    }

    if (Config::isSet(Config::DUMP_CONFIG)) {
        Config::dump();
        return 0;
    }

    if (Config::isSet(Config::INSECURE)) {
        qDebug() << "Warning: Certificate validation disabled";
        QSslConfiguration sslConf = QSslConfiguration::defaultConfiguration();
        sslConf.setPeerVerifyMode(QSslSocket::VerifyNone);
        QSslConfiguration::setDefaultConfiguration(sslConf);
    }

    if (Config::isSet(Config::DEBUG)) {
        g_debugMode = true;
    }

    if (Config::isSet(Config::LOCATION_MODE)) {
        QString mode = Config::get(Config::LOCATION_MODE);
        if (mode == "IGNORE") {
            g_forLocationHandling = LOCATION_IGNORE;
        } else if (mode == "BUMP") {
            g_forLocationHandling = LOCATION_BUMP;
        } else if (mode == "EXCLUSIVE") {
            g_forLocationHandling = LOCATION_EXCLUSIVE;
        } else {
            qDebug() << "Invalid location mode: " << mode;
            QMessageBox::critical(nullptr, "Error", "Invalid location mode: " + mode);
            return 1;
        }
    }

    if (Config::isSet(Config::TEMPLATE_MODE)) {
        QString mode = Config::get(Config::TEMPLATE_MODE);
        if (mode == "IGNORE") {
            g_templateHandling = TEMPLATES_IGNORE;
        } else if (mode == "BUMP") {
            g_templateHandling = TEMPLATES_BUMP;
        } else {
            qDebug() << "Invalid template mode: " << mode;
            QMessageBox::critical(nullptr, "Error", "Invalid template mode: " + mode);
            return 1;
        }
    }

    g_noVtx = Config::isSet(Config::NO_VTX);

    if (Config::isSet(Config::AUTOQUIT)) {
        bool ok = false;
        g_autoQuitSeconds = Config::get(Config::AUTOQUIT).toInt(&ok, 10);
        if (!ok) {
            g_autoQuitSeconds = 0;
        }
    }

    /* read session files */
    QList<Session*> xsessions(XSession::readSessions(Config::get(Config::XSESSION_PATH)));

    Dialog w;

    QRect desktopRect = QApplication::desktop()->availableGeometry(&w);
    int width = VMCHOOSER_DEFAULT_WIDTH, height = VMCHOOSER_DEFAULT_HEIGHT;
    if (Config::isSet(Config::FULLSCREEN) || Config::get(Config::WINDOW_SIZE) == QLatin1String("fullscreen")) {
        width = desktopRect.width();
        height = desktopRect.height();
        g_fullscreen = true;
    } else {
        QString size(Config::get(Config::WINDOW_SIZE));
        if (!size.isEmpty()) {
            QRegExp rx("^(\\d+)x(\\d+)$");
            if (rx.indexIn(size) != -1) {
                QStringList list = rx.capturedTexts();
                width = list.value(1).toInt();
                height = list.value(2).toInt();
            } else {
                std::cerr
                        << a.translate("Console", "vmchooser: invalid size argument").toUtf8().data()
                        << std::endl;
                return EXIT_FAILURE;
            }
        }
    }

    w.downloadData(Config::get(Config::LOCATIONS));
    w.setTheme();
    w.setWindowFlag(Qt::FramelessWindowHint, true);
    w.resize(width, height);

    if (xsessions.size()) {
        qSort(xsessions.begin(), xsessions.end(), myLessThan);
        w.addItems(xsessions, 0);
    }

    w.show();

    if (Config::isSet(Config::AUTOSTART_UUID)) {
        qDebug() << "using startSession() from main.cpp";
        w.startSession(Config::get(Config::AUTOSTART_UUID));
    }



    // center dialog on primary screen
    QPoint center = desktopRect.center();
    w.move(center.x() - w.width() / 2, center.y() - w.height() / 2);
    a.setActiveWindow(&w);

    return a.exec();
}