aboutsummaryrefslogtreecommitdiff
path: root/installer/main.cpp
blob: 194807accca88563f19803415a5fbd10a37c23cd (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
#include "mainwindow.h"
#include "process/installworker.h"
#include "process/removeworker.h"
#include "maintainwindow.h"
#include <QApplication>
#include <QTranslator>
#include <QLibraryInfo>
#include <QFile>
#include <QTemporaryFile>
#include <QMessageBox>
#include <QDesktopWidget>

FILE _iob[] = {*stdin, *stdout, *stderr};

extern "C" FILE * __cdecl __iob_func(void)
{
    return _iob;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTranslator qtTranslator;
    qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    a.installTranslator(&qtTranslator);

    QTranslator myappTranslator;
    myappTranslator.load(QLocale(), ":/translations/");
    a.installTranslator(&myappTranslator);

    qsrand(QDateTime::currentMSecsSinceEpoch());

    qDebug() << a.arguments();
    if (a.arguments().contains("--update-from-app")) {
        //Install UI mode (triggered from update action inside app)

        MainWindow w;
        w.setAutoProgress(true);
        w.show();

        return a.exec();
    } else if (a.arguments().contains("--install")) {
        //Installer mode
        InstallWorker worker;
        if (!worker.startWork()) return 1;

        a.setQuitOnLastWindowClosed(false);
        return a.exec();
    } else if (a.arguments().contains("--remove")) {
        //Remove mode
        RemoveWorker worker;
        if (!worker.startWork()) return 1;

        a.setQuitOnLastWindowClosed(false);
        return a.exec();
    } else if (a.arguments().contains("--uninstallmetadata")) {
        //Modify UI mode
        MaintainWindow w;
        w.show();

        return a.exec();
    } else if (a.arguments().contains("--update")) {
        //Prepare update mode
        QString tempInstallerPath = QDir::tempPath() + "/theinstaller.exe";
        if (QFile::exists(tempInstallerPath)) {
            QFile::remove(tempInstallerPath);
        }
        if (QFile::copy(QApplication::applicationFilePath(), tempInstallerPath)) {
            QProcess::startDetached(tempInstallerPath);
            return 0;
        } else {
            QMessageBox box;
            box.setWindowTitle("Error");
            box.setText("Couldn't prepare for update. We won't be able to update at this time.");
            box.setDetailedText("Here are some things you can try:\n"
                                "- Your antivirus software may be blocking the updater. Try disabling any antivirus software while you update\n"
                                "- Your temporary folder is not able to be written to");
            box.setIcon(QMessageBox::Critical);
            box.exec();
            return 1;
        }
    } else if (QFile(a.applicationDirPath() + "/uninstall.json").exists()) {
        //Prepare uninstall mode
        QString tempInstallerPath = QDir::tempPath() + "/theinstaller.exe";
        if (QFile::exists(tempInstallerPath)) {
            QFile::remove(tempInstallerPath);
        }
        if (QFile::copy(QApplication::applicationFilePath(), tempInstallerPath)) {
            QProcess::startDetached(tempInstallerPath, QStringList() << "--uninstallmetadata" << a.applicationDirPath() + "/uninstall.json");
            return 0;
        } else {
            QMessageBox box;
            box.setWindowTitle("Error");
            box.setText("Couldn't prepare for uninstallation. We won't be able to uninstall at this time.");
            box.setDetailedText("Here are some things you can try:\n"
                                "- Your antivirus software may be blocking the uninstaller. Try disabling any antivirus software while you uninstall\n"
                                "- Your temporary folder is not able to be written to");
            box.setIcon(QMessageBox::Critical);
            box.exec();
            return 1;
        }
    } else {
        //Install UI mode
        MainWindow w;
        w.show();

        return a.exec();
    }
}

QString calculateSize(quint64 size) {
    QString ret;
    if (size > 1073741824) {
        ret = QString::number(((float) size / 1024 / 1024 / 1024), 'f', 2).append(" GiB");
    } else if (size > 1048576) {
        ret = QString::number(((float) size / 1024 / 1024), 'f', 2).append(" MiB");
    } else if (size > 1024) {
        ret = QString::number(((float) size / 1024), 'f', 2).append(" KiB");
    } else {
        ret = QString::number((float) size, 'f', 2).append(" B");
    }

    return ret;
}

float getDPIScaling() {
    float currentDPI = QApplication::desktop()->logicalDpiX();
    return currentDPI / (float) 96;
}