forked from ckaiser/Lightscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomfoptionswidget.cpp
98 lines (77 loc) · 3.38 KB
/
pomfoptionswidget.cpp
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
#include <QJsonObject>
#include <QInputDialog>
#include <QNetworkReply>
#include <QDesktopServices>
#include <QMessageBox>
#include <QRegExpValidator>
#include "pomfoptionswidget.h"
#include "../uploader/uploader.h"
#include "../uploader/pomfuploader.h"
#include "../screenshotmanager.h"
#include "../os.h"
PomfOptionsWidget::PomfOptionsWidget(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
ui.progressIndicatorBar->setVisible(false);
connect(ui.verifyButton, &QPushButton::clicked, this, [&]() {
ui.verifyButton->setEnabled(false);
ui.downloadListButton->setEnabled(false);
ui.progressIndicatorBar->setVisible(true);
QPointer<QWidget> guard(parentWidget());
PomfUploader::verify(ui.pomfUrlComboBox->currentText(), [&, guard](bool result) {
if (guard.isNull()) return;
ui.verifyButton->setEnabled(true);
ui.downloadListButton->setEnabled(true);
ui.progressIndicatorBar->setVisible(false);
if (result) {
ui.verifyButton->setText(tr("Valid uploader!"));
ui.verifyButton->setStyleSheet("color: green;");
ui.verifyButton->setIcon(os::icon("yes"));
} else {
ui.verifyButton->setStyleSheet("color: red;");
ui.verifyButton->setIcon(os::icon("no"));
ui.verifyButton->setText(tr("Invalid uploader :("));
}
});
});
connect(ui.pomfUrlComboBox, &QComboBox::currentTextChanged, [&](const QString &text) {
bool validUrl = false;
if (text.startsWith("http://") || text.startsWith("https://")) { // TODO: Something a bit more complex
validUrl = true;
}
ui.verifyButton->setEnabled(validUrl);
if (ui.verifyButton->styleSheet().count() > 0) {
ui.verifyButton->setStyleSheet("");
ui.verifyButton->setIcon(QIcon());
ui.verifyButton->setText(tr("Verify"));
}
});
connect(ui.helpLabel, &QLabel::linkActivated, this, [&](const QString &url) {
QDesktopServices::openUrl(url);
});
connect(ui.downloadListButton, &QPushButton::clicked, this, [&]() {
ui.verifyButton->setEnabled(false);
ui.downloadListButton->setEnabled(false);
ui.progressIndicatorBar->setVisible(true);
auto pomflistReply = Uploader::instance()->nam()->get(QNetworkRequest(QUrl("https://lightscreen.com.ar/pomf.json")));
QPointer<QWidget> guard(parentWidget());
connect(pomflistReply, &QNetworkReply::finished, [&, guard, pomflistReply] {
if (guard.isNull()) return;
ui.verifyButton->setEnabled(true);
ui.downloadListButton->setEnabled(true);
ui.progressIndicatorBar->setVisible(false);
if (pomflistReply->error() != QNetworkReply::NoError) {
QMessageBox::warning(parentWidget(), tr("Connection error"), pomflistReply->errorString());
return;
}
auto pomfListData = QJsonDocument::fromJson(pomflistReply->readAll()).object();
if (pomfListData.contains("url")) {
auto urlList = pomfListData["url"].toArray();
for (auto url : qAsConst(urlList)) {
ui.pomfUrlComboBox->addItem(url.toString());
}
ui.pomfUrlComboBox->showPopup();
}
});
});
}