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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
// Copyright 2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "PluginUpdater.h"
#include "PluginManager.h"
#include "Log.h"
#include "PluginInstaller.h"
#include "Global.h"
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QLabel>
#include <QtCore/QHashIterator>
#include <QtCore/QSignalBlocker>
#include <QtCore/QByteArray>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtConcurrent>
#include <QNetworkRequest>
#include <algorithm>
PluginUpdater::PluginUpdater(QWidget *parent)
: QDialog(parent),
m_wasInterrupted(false),
m_dataMutex(),
m_pluginsToUpdate(),
m_networkManager(),
m_pluginUpdateWidgets() {
QObject::connect(&m_networkManager, &QNetworkAccessManager::finished, this, &PluginUpdater::on_updateDownloaded);
}
PluginUpdater::~PluginUpdater() {
m_wasInterrupted.store(true);
}
void PluginUpdater::checkForUpdates() {
// Dispatch a thread in which each plugin can check for updates
QtConcurrent::run([this]() {
QMutexLocker lock(&m_dataMutex);
const QVector<const_plugin_ptr_t> plugins = Global::get().pluginManager->getPlugins();
for (int i = 0; i < plugins.size(); i++) {
const_plugin_ptr_t plugin = plugins[i];
if (plugin->hasUpdate()) {
QUrl updateURL = plugin->getUpdateDownloadURL();
if (updateURL.isValid() && !updateURL.isEmpty() && !updateURL.fileName().isEmpty()) {
UpdateEntry entry = { plugin->getID(), updateURL, updateURL.fileName(), 0 };
m_pluginsToUpdate << entry;
}
}
// if the update has been asked to be interrupted, exit here
if (m_wasInterrupted.load()) {
emit updateInterrupted();
return;
}
}
if (!m_pluginsToUpdate.isEmpty()) {
emit updatesAvailable();
}
});
}
void PluginUpdater::promptAndUpdate() {
setupUi(this);
populateUI();
setWindowIcon(QIcon(QLatin1String("skin:mumble.svg")));
QObject::connect(qcbSelectAll, &QCheckBox::stateChanged, this, &PluginUpdater::on_selectAll);
QObject::connect(this, &QDialog::finished, this, &PluginUpdater::on_finished);
if (exec() == QDialog::Accepted) {
update();
}
}
void PluginUpdater::update() {
QMutexLocker l(&m_dataMutex);
for (int i = 0; i < m_pluginsToUpdate.size(); i++) {
UpdateEntry currentEntry = m_pluginsToUpdate[i];
// The network manager will be emit a signal once the request has finished processing.
// Thus we can ignore the returned QNetworkReply* here.
m_networkManager.get(QNetworkRequest(currentEntry.updateURL));
}
}
void PluginUpdater::populateUI() {
clearUI();
QMutexLocker l(&m_dataMutex);
for (int i = 0; i < m_pluginsToUpdate.size(); i++) {
UpdateEntry currentEntry = m_pluginsToUpdate[i];
plugin_id_t pluginID = currentEntry.pluginID;
const_plugin_ptr_t plugin = Global::get().pluginManager->getPlugin(pluginID);
if (!plugin) {
continue;
}
QCheckBox *checkBox = new QCheckBox(qwContent);
checkBox->setText(plugin->getName());
checkBox->setToolTip(plugin->getDescription());
checkBox->setProperty("pluginID", pluginID);
QLabel *urlLabel = new QLabel(qwContent);
urlLabel->setText(currentEntry.updateURL.toString());
urlLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
UpdateWidgetPair pair = { checkBox, urlLabel };
m_pluginUpdateWidgets << pair;
QObject::connect(checkBox, &QCheckBox::stateChanged, this, &PluginUpdater::on_singleSelectionChanged);
}
// sort the plugins alphabetically
std::sort(m_pluginUpdateWidgets.begin(), m_pluginUpdateWidgets.end(), [](const UpdateWidgetPair &first, const UpdateWidgetPair &second) {
return first.pluginCheckBox->text().compare(second.pluginCheckBox->text(), Qt::CaseInsensitive) < 0;
});
// add the widgets to the layout
for (int i = 0; i < m_pluginUpdateWidgets.size(); i++) {
UpdateWidgetPair ¤tPair = m_pluginUpdateWidgets[i];
static_cast<QFormLayout*>(qwContent->layout())->addRow(currentPair.pluginCheckBox, currentPair.urlLabel);
}
}
void PluginUpdater::clearUI() {
// There are always as many checkboxes as there are labels
for (int i = 0; i < m_pluginUpdateWidgets.size(); i++) {
UpdateWidgetPair ¤tPair = m_pluginUpdateWidgets[i];
qwContent->layout()->removeWidget(currentPair.pluginCheckBox);
qwContent->layout()->removeWidget(currentPair.urlLabel);
delete currentPair.pluginCheckBox;
delete currentPair.urlLabel;
}
}
void PluginUpdater::on_selectAll(int checkState) {
// failsafe for partially selected state (shouldn't happen though)
if (checkState == Qt::PartiallyChecked) {
checkState = Qt::Unchecked;
}
// Select or deselect all plugins
for (int i = 0; i < m_pluginUpdateWidgets.size(); i++) {
UpdateWidgetPair ¤tPair = m_pluginUpdateWidgets[i];
currentPair.pluginCheckBox->setCheckState(static_cast<Qt::CheckState>(checkState));
}
}
void PluginUpdater::on_singleSelectionChanged(int checkState) {
bool isChecked = checkState == Qt::Checked;
// Block signals for the selectAll checkBox in order to not trigger its
// check-logic when changing its check-state here
const QSignalBlocker blocker(qcbSelectAll);
if (!isChecked) {
// If even a single item is unchecked, the selectAll checkbox has to be unchecked
qcbSelectAll->setCheckState(Qt::Unchecked);
return;
}
// iterate through all checkboxes to see whether we have to toggle the selectAll checkbox
for (int i = 0; i < m_pluginUpdateWidgets.size(); i++) {
const UpdateWidgetPair ¤tPair = m_pluginUpdateWidgets[i];
if (!currentPair.pluginCheckBox->isChecked()) {
// One unchecked checkBox is enough to know that the selectAll
// CheckBox can't be checked, so we can abort at this point
return;
}
}
qcbSelectAll->setCheckState(Qt::Checked);
}
void PluginUpdater::on_finished(int result) {
if (result == QDialog::Accepted) {
if (qcbSelectAll->isChecked()) {
// all plugins shall be updated, so we don't have to check them individually
return;
}
QMutexLocker l(&m_dataMutex);
// The user wants to update the selected plugins only
// remove the plugins that shouldn't be updated from m_pluginsToUpdate
auto it = m_pluginsToUpdate.begin();
while (it != m_pluginsToUpdate.end()) {
plugin_id_t id = it->pluginID;
// find the corresponding checkbox
bool updateCurrent = false;
for (int k = 0; k < m_pluginUpdateWidgets.size(); k++) {
QCheckBox *checkBox = m_pluginUpdateWidgets[k].pluginCheckBox;
QVariant idVariant = checkBox->property("pluginID");
if (idVariant.isValid() && static_cast<plugin_id_t>(idVariant.toInt()) == id) {
updateCurrent = checkBox->isChecked();
break;
}
}
if (!updateCurrent) {
// remove this entry from the update-vector
it = m_pluginsToUpdate.erase(it);
} else {
it++;
}
}
} else {
// Nothing to do as the user doesn't want to update anyways
}
}
void PluginUpdater::interrupt() {
m_wasInterrupted.store(true);
}
void PluginUpdater::on_updateDownloaded(QNetworkReply *reply) {
if (reply) {
// Schedule reply for deletion
reply->deleteLater();
if (m_wasInterrupted.load()) {
emit updateInterrupted();
return;
}
// Find the ID of the plugin this update is for by comparing the URLs
UpdateEntry entry;
bool foundID = false;
{
QMutexLocker l(&m_dataMutex);
for (int i = 0; i < m_pluginsToUpdate.size(); i++) {
if (m_pluginsToUpdate[i].updateURL == reply->url()) {
foundID = true;
// remove that entry from the vector as it is being updated right here
entry = m_pluginsToUpdate.takeAt(i);
break;
}
}
}
if (!foundID) {
// Can't match the URL to a pluginID
qWarning() << "PluginUpdater: Requested update for plugin from"
<< reply->url() << "but didn't find corresponding plugin again!";
return;
}
// Now get a handle to that plugin
const_plugin_ptr_t plugin = Global::get().pluginManager->getPlugin(entry.pluginID);
if (!plugin) {
// Can't find plugin with given ID
qWarning() << "PluginUpdater: Got update for plugin with id"
<< entry.pluginID << "but it doesn't seem to exist anymore!";
return;
}
// We can start actually checking the reply here
if (reply->error() != QNetworkReply::NoError) {
// There was an error during this request. Report it
Log::logOrDefer(Log::Warning,
tr("Unable to download plugin update for \"%1\" from \"%2\" (%3)").arg(
plugin->getName()).arg(reply->url().toString()).arg(
QString::fromLatin1(
QMetaEnum::fromType<QNetworkReply::NetworkError>().valueToKey(reply->error())
)
)
);
return;
}
// Check HTTP status code (just because the request was successful, doesn't
// mean the data was downloaded successfully
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (httpStatusCode >= 300 && httpStatusCode < 400) {
// We have been redirected
if (entry.redirects >= MAX_REDIRECTS - 1) {
// Maximum redirect count exceeded
Log::logOrDefer(Log::Warning, tr("Update for plugin \"%1\" failed due to too many redirects").arg(plugin->getName()));
return;
}
QUrl redirectedUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
// Because the redirection url can be relative,
// we have to use the previous one to resolve it
redirectedUrl = reply->url().resolved(redirectedUrl);
// Re-insert the current plugin into the list of updating plugins (using the
// new URL so that it will be associated with that instead of the old one)
entry.updateURL = redirectedUrl;
entry.redirects++;
{
QMutexLocker l(&m_dataMutex);
m_pluginsToUpdate << entry;
}
// Post a new request for the file to the new URL
m_networkManager.get(QNetworkRequest(redirectedUrl));
return;
}
if (httpStatusCode < 200 || httpStatusCode >= 300) {
// HTTP request has failed
Log::logOrDefer(Log::Warning,
tr("Unable to download plugin update for \"%1\" from \"%2\" (HTTP status code %3)").arg(
plugin->getName()).arg(reply->url().toString()).arg(httpStatusCode)
);
return;
}
// Reply seems fine -> write file to disk and fire installer
QByteArray content = reply->readAll();
// Write the content to a file in the temp-dir
if (content.isEmpty()) {
qWarning() << "PluginUpdater: Update for" << plugin->getName() << "from"
<< reply->url().toString() << "resulted in no content!";
return;
}
QFile file(QDir::temp().filePath(entry.fileName));
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "PluginUpdater: Can't open" << file.fileName() << "for writing!";
return;
}
file.write(content);
file.close();
try {
// Launch installer
PluginInstaller installer(QFileInfo(file.fileName()));
installer.install();
Log::logOrDefer(Log::Information, tr("Successfully updated plugin \"%1\"").arg(plugin->getName()));
// Make sure Mumble won't use the old version of the plugin
Global::get().pluginManager->rescanPlugins();
} catch (const PluginInstallException &e) {
Log::logOrDefer(Log::CriticalError, e.getMessage());
}
{
QMutexLocker l(&m_dataMutex);
if (m_pluginsToUpdate.isEmpty()) {
emit updatingFinished();
}
}
}
}
|