-
Notifications
You must be signed in to change notification settings - Fork 5
/
updateform.cpp
276 lines (225 loc) · 9.41 KB
/
updateform.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <QMessageBox>
#include <QtNetwork/QNetworkReply>
#include <QFile>
#include <QtWidgets>
#include "Windows.h"
#include "x64dbg_updater.h"
#include "updateform.h"
#include "ui_updateform.h"
#include "pluginmain.h"
#define EXCEPTION_NETWORK 0
#define EXCEPTION_JSON 1
// Pluginmanager: console_source\main_console.cpp
enum PLGMNGREXITCODE
{
PLGMNGREXITCODE_NOARGS=0,
PLGMNGREXITCODE_ERROR,
PLGMNGREXITCODE_DATAERROR,
PLGMNGREXITCODE_INSTALLFILES,
PLGMNGREXITCODE_INSTALLPLUGINS,
PLGMNGREXITCODE_LISTNAMEISEMPTY,
PLGMNGREXITCODE_NOINPUTFILES,
PLGMNGREXITCODE_NOPLUGINSINSTALLED,
PLGMNGREXITCODE_PLUGINCREATED,
PLGMNGREXITCODE_CANNOTCREATESERVERLIST,
PLGMNGREXITCODE_SHOWSERVERLIST,
PLGMNGREXITCODE_SERVERLISTCREATED,
PLGMNGREXITCODE_SERVERLISTISEMPTY,
PLGMNGREXITCODE_SERVERLISTUPDATED,
PLGMNGREXITCODE_SHOWINSTALLED,
PLGMNGREXITCODE_SHOWUPDATES,
PLGMNGREXITCODE_UPDATEALL,
PLGMNGREXITCODE_UPDATEPLUGINS,
PLGMNGREXITCODE_REMOVEPLUGINS,
PLGMNGREXITCODE_NOUPDATESAVAILABLE,
};
UpdateForm::UpdateForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::UpdateForm)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/icons/images/update.png"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
ui->trwCommits->setHeaderLabel("Latest commits");
QPalette p = ui->pteUpdaterConsole->palette();
p.setColor(QPalette::Base, QColor(0, 0, 0));
p.setColor(QPalette::Text, QColor(0xCC, 0xCC, 0xCC));
ui->pteUpdaterConsole->setPalette(p);
// Setting the font(family) in the designer makes us crash after plugin unload O_o
QFont font;
font.setFamily("Consolas");
font.setPointSize(10);
ui->pteUpdaterConsole->setFont(font);
foundCommitDate = false;
ui->pbUpdateOnExit->setChecked(globalSettings.updateOnExit);
manager = new QNetworkAccessManager(this);
updaterProcess = new QProcess(this);
}
void UpdateForm::checkUpdate() {
// Save the date ;)
globalSettings.updateDelayTimestamp = QDateTime::currentDateTimeUtc().toTime_t();
BridgeSettingSetUint("UpdaterPlugin", "DelayTimestamp", globalSettings.updateDelayTimestamp);
// Let PluginManager update the server list
disconnect(updaterProcess, SIGNAL(finished(int, QProcess::ExitStatus)), 0, 0);
connect(updaterProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(plgmgrUpdateServerListFinished(int, QProcess::ExitStatus)));
updaterProcess->start(("\"" + QDir(globalSettings.managerPath).filePath("x64plgmnrc.exe").append("\" --updateserverlist")).toStdString().c_str());
// Get list of commits from GitHub
disconnect(manager, SIGNAL(finished(QNetworkReply*)), 0, 0);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished_commits(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("https://api.github.com/repos/x64dbg/x64dbg/commits")));
}
void UpdateForm::plgmgrUpdateServerListFinished(int exitCode, QProcess::ExitStatus exitStatus) {
// Server list is updated, check for updates
disconnect(updaterProcess, SIGNAL(finished(int, QProcess::ExitStatus)), 0, 0);
connect(updaterProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(plgmgrShowUpdatesFinished(int, QProcess::ExitStatus)));
updaterProcess->start(("\"" + QDir(globalSettings.managerPath).filePath("x64plgmnrc.exe").append("\" --showupdates")).toStdString().c_str());
}
void UpdateForm::plgmgrShowUpdatesFinished(int exitCode, QProcess::ExitStatus exitStatus) {
QString s = updaterProcess->readAllStandardOutput();
if (exitCode == PLGMNGREXITCODE_SHOWUPDATES) {
ui->lblProgress->setText("Status: PluginManager found new updates.");
_plugin_logputs("x64dbg Updater: PluginManager found new updates.");
show();
} else {
ui->lblProgress->setText("Status: PluginManager found no new updates.");
_plugin_logputs("x64dbg Updater: PluginManager found no new updates.");
}
s = "> x64plgmnrc.exe --showupdates\x0d\x0a\x0d\x0a" + s;
ui->pteUpdaterConsole->setPlainText(s);
}
void UpdateForm::replyFinished_commits(QNetworkReply *reply) {
reply->deleteLater();
json_t *json = NULL;
ui->trwCommits->clear();
char theme[MAX_SETTING_SIZE];
bool darkMode = false;
if (BridgeSettingGet("Theme", "Selected", theme) == true) {
if (strcmp(theme, "Dark") == 0) {
darkMode = true;
}
}
try {
if(reply->error() != QNetworkReply::NoError) {
throw EXCEPTION_NETWORK;
}
json_error_t error;
json = json_loads(reply->readAll().data(), 0, &error);
reply->deleteLater();
if (!json) { throw EXCEPTION_JSON; }
if (!json_is_array(json)) { throw EXCEPTION_JSON; }
QDate lastDate = QDate();
QString latestCommitHash;
QTreeWidgetItem *treeRoot = nullptr;
bool alternatingFlag = false;
for (size_t i = 0; i < json_array_size(json); i++) {
json_t *commit;
commit = json_array_get(json, i);
if (!json_is_object(commit)) { throw EXCEPTION_JSON; }
json_t *commithash;
commithash = json_object_get(commit, "sha");
if (!json_is_string(commithash)) { throw EXCEPTION_JSON; }
QString sCommitHash = json_string_value(commithash);
json_t *commit_;
commit_ = json_object_get(commit, "commit");
if (!json_is_object(commit_)) { throw EXCEPTION_JSON; }
json_t *message;
message = json_object_get(commit_, "message");
if (!json_is_string(message)) { throw EXCEPTION_JSON; }
QString sMessage = json_string_value(message);
json_t *committer;
committer = json_object_get(commit_, "committer");
if (!json_is_object(committer)) { throw EXCEPTION_JSON; }
json_t *date;
date = json_object_get(committer, "date");
if (!json_is_string(date)) { throw EXCEPTION_JSON; }
QDateTime datetime = QDateTime::fromString(json_string_value(date), Qt::ISODate);
if (lastDate != datetime.date()) {
lastDate = datetime.date();
treeRoot = new QTreeWidgetItem(ui->trwCommits);
treeRoot->setText(0, lastDate.toString());
treeRoot->setExpanded(true);
}
QTreeWidgetItem *treeItem = new QTreeWidgetItem();
treeItem->setText(0, sMessage);
if (sCommitHash == currentCommitHash) {
treeItem->setBackgroundColor(0, QColor(255, 128, 64));
ui->lblCurrentVersion->setText("Current commit:\t" + currentCommitHash.left(7) + " - " + datetime.toLocalTime().toString());
foundCommitDate = true;
} else {
if (alternatingFlag) {
if (darkMode) {
treeItem->setBackgroundColor(0, QColor(137, 162, 246));
} else {
treeItem->setBackgroundColor(0, QColor(153, 217, 234));
}
} else {
if (darkMode) {
treeItem->setBackgroundColor(0, QColor(132, 107, 244));
} else {
treeItem->setBackgroundColor(0, QColor(213, 240, 247));
}
}
alternatingFlag = !alternatingFlag;
}
treeRoot->addChild(treeItem);
if (i == 0) {
latestCommitHash = sCommitHash;
ui->lblLatestVersion->setText("Latest commit:\t" + sCommitHash.left(7) + " - " + datetime.toLocalTime().toString());
}
}
json_decref(json);
} catch (int e) {
switch (e) {
case EXCEPTION_NETWORK: {
_plugin_logprint("x64dbg Updater: network error -> ");
_plugin_logputs(reply->errorString().toStdString().c_str());
break;
}
case EXCEPTION_JSON: {
_plugin_logputs("x64dbg Updater: JSON error");
break;
}
}
if (json) {
json_decref(json);
}
}
}
void UpdateForm::showEvent(QShowEvent *event) {
if (!foundCommitDate) {
ui->lblCurrentVersion->setText("Current commit:\t" + currentCommitHash.left(7));
}
}
UpdateForm::~UpdateForm() {
delete ui;
delete manager;
delete updaterProcess;
}
void UpdateForm::on_pbShowPluginManager_clicked()
{
updateOnExit(false);
ShellExecuteW(0, L"open", QDir(globalSettings.managerPath).filePath("x64plgmnr.exe").toStdWString().c_str(), NULL, NULL, SW_SHOWNORMAL);
close();
}
void UpdateForm::on_pbUpdateAll_clicked()
{
updateOnExit(false);
updateAll(true);
PostMessage(hwndDlg, WM_CLOSE, 0, 0);
close();
}
void UpdateForm::updateOnExit(bool enabled) {
globalSettings.updateOnExit = enabled;
ui->pbUpdateOnExit->setChecked(enabled);
}
void UpdateForm::on_pbUpdateOnExit_clicked()
{
globalSettings.updateOnExit = !globalSettings.updateOnExit;
}
void UpdateForm::on_pbForceCoreUpdate_clicked()
{
updateOnExit(false);
forceUpdate();
PostMessage(hwndDlg, WM_CLOSE, 0, 0);
close();
}