aboutsummaryrefslogtreecommitdiff
path: root/src/qt/mainwindow.cpp
blob: 15e5526f43208c557e2a5f3e79a8ba17b8ba77be (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <QApplication>
#include <QMessageBox>
#include <QFontDatabase>
#include <QDesktopServices>
#include <QUrl>

#include <limits.h>
#include <iostream>

#include "steam.h"

#include "mainwindow.hpp"
#include "./ui_mainwindow.h"
#include "workers.hpp"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    centralWidget()->layout()->setContentsMargins(0, 0, 0, 0);

    qRegisterMetaType<Worker::Tasks_t>("Task_t");
    qRegisterMetaType<Worker::Results_t>("Results_t");

    QFontDatabase::addApplicationFont (":/font/assets/tf2build.ttf");
    QFont playFont("TF2 Build", 20, QFont::Bold);
    QFont progressFont("TF2 Build", 10, QFont::Normal);

    ui->mainButton->setFont(playFont);
    ui->progressBar->setFont(progressFont);
    ui->statusLabel->setFont(progressFont);
    ui->infoLabel->setFont(progressFont);

    QPixmap bkgnd(":/background/assets/background.bmp");
    bkgnd = bkgnd.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

    QPalette palette;
    palette.setBrush(QPalette::Window, bkgnd);
    this->setPalette(palette);

    worker = new Worker();
    worker->moveToThread(&thread);

    connect(&thread, &QThread::finished, worker, &QObject::deleteLater);
    connect(this, &MainWindow::workerOperate, worker, &Worker::doWork);
    connect(worker, &Worker::resultReady, this, &MainWindow::workerResult);

    thread.start();

    //operateSVN(svnWorker::SVN_INSTALL);

    connect(ui->settingsButton, SIGNAL(clicked()), this, SLOT(settingsWindow()));

    settings = new Settings(worker, this);
    settings->setModal(true);
    //connect(settings, SIGNAL(visibleChanged()), this, SLOT(enable()));

    ui->mainButton->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(ui->mainButton, SIGNAL(clicked()), this, SLOT(updateButton()));
    connect(ui->mainButton, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showButtonContext(const QPoint&)));
    connect(ui->buttonDiscord, SIGNAL(clicked()), this, SLOT(openDiscordInvite()));
    connect(ui->buttonWebsite, SIGNAL(clicked()), this, SLOT(openWebsite()));

    ui->mainButton->setText("...");
    ui->statusLabel->setText("...");
    ui->infoLabel->setText("...");

    in_progress = false;
    installed = false;
    uptodate = false;
    workerOperate(Worker::TASK_INIT);
}

void MainWindow::workerResult(const enum Worker::Results_t& result)
{
    switch (result)
    {

        case Worker::RESULT_UNINSTALL_COMPLETE:
        case Worker::RESULT_UNINSTALL_FAILURE:
        // TODO find better use for these
        case Worker::RESULT_NONE:
            resetProgress();
            break;

        case Worker::RESULT_EXIT:
            QCoreApplication::quit();
            break;

        case Worker::RESULT_UPDATE_TEXT:
            ui->progressBar->setValue(worker->progress);
            ui->infoLabel->setText(worker->infoText);
            worker->infoText.clear();
            break;

        case Worker::RESULT_IS_INSTALLED:
            installed = true;
            workerOperate(Worker::TASK_IS_UPTODATE);
            break;

        case Worker::RESULT_IS_NOT_INSTALLED:
            ui->mainButton->setText("Install");
            installed = false;
            break;

        case Worker::RESULT_IS_UPTODATE:
            uptodate = true;
            ui->mainButton->setText("Play");
            ui->statusLabel->setText("Up to Date");
            break;

        case Worker::RESULT_IS_OUTDATED:
            uptodate = false;
            ui->mainButton->setText("Update");
            ui->statusLabel->setText(QString("Revision %1 is available").arg(worker->getRemoteRevision()));
            break;

        case Worker::RESULT_INIT_COMPLETE:
            ui->statusLabel->setText("");
            ui->infoLabel->setText("");
            workerOperate(Worker::TASK_IS_INSTALLED);
            break;

        case Worker::RESULT_INIT_FAILURE:
            QMessageBox::information(this, windowTitle(), "Could not find install location.\nIs Steam installed?");
            QCoreApplication::quit();
            break;

        case Worker::RESULT_INSTALL_COMPLETE:
            resetProgress();
            ui->statusLabel->setText("Installed");
            workerOperate(Worker::TASK_IS_INSTALLED);
            break;

        case Worker::RESULT_INSTALL_FAILURE:
            ui->progressBar->setFormat("Install failed");
            break;

        case Worker::RESULT_UPDATE_COMPLETE:
            resetProgress();
            ui->statusLabel->setText("Updated");
            workerOperate(Worker::TASK_IS_UPTODATE);
            break;

        case Worker::RESULT_UPDATE_RUN:
            ui->statusLabel->setText("Launching");
            workerOperate(Worker::TASK_RUN);
            break;

        case Worker::RESULT_UPDATE_FAILURE:
            ui->statusLabel->setText("Update failed");
            break;

        case Worker::RESULT_NO_STEAM:
            resetProgress();
            QMessageBox::information(this, windowTitle(), "Steam is not running" );
            break;

    }

    in_progress = false;
}

void MainWindow::settingsWindow()
{
    settings->refresh();
    settings->show();
    //this->setEnabled(false);
}

void MainWindow::setupButton()
{
    workerOperate(Worker::TASK_IS_INSTALLED);
}

void MainWindow::updateButton()
{
    if (in_progress)
        return;

    if (installed)
    {
        if (!uptodate)
        {
            workerOperate(Worker::TASK_UPDATE);
            ui->statusLabel->setText("Updating (may take a while)");
        }
        else
        {
            workerOperate(Worker::TASK_RUN);
        }
    }
    else
    {
        workerOperate(Worker::TASK_INSTALL);
        ui->statusLabel->setText("Installing (may take a while)");
    }

    in_progress = true;
}

void MainWindow::showButtonContext(const QPoint& pos)
{
    QPoint absPos = ui->mainButton->mapToGlobal(pos); 

    QMenu ctxMenu;
    ctxMenu.addAction("Run without Update");

    QAction* selectedItem = ctxMenu.exec(absPos);

    if (selectedItem)
    {
        workerOperate(Worker::TASK_RUN);
    }
}


void MainWindow::openDiscordInvite()
{
  QDesktopServices::openUrl(QUrl("https://discord.gg/mKjW2ACCrm", QUrl::TolerantMode));
}

void MainWindow::openWebsite()
{
  QDesktopServices::openUrl(QUrl("https://openfortress.fun/", QUrl::TolerantMode));
}

void MainWindow::resetProgress()
{
    ui->progressBar->setFormat("%p%");
    ui->progressBar->setValue(-1);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete settings;

    worker->stop_work();
    thread.quit();
    thread.wait();
}