-
Notifications
You must be signed in to change notification settings - Fork 13
/
updatechecker.cpp
171 lines (158 loc) · 5.72 KB
/
updatechecker.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
#include "updatechecker.h"
#include <QNetworkReply>
#include <sstream>
#include <QObject>
UpdateChecker::UpdateChecker(QObject* parent) : QObject(parent) {
connect(&netAccessManager, &QNetworkAccessManager::finished, this, &UpdateChecker::onRequestFinished);
m_active = false;
}
#ifndef SPARKLE_UPDATE_CHECK
void UpdateChecker::checkForUpdates() {
#ifdef APPIMAGE_UPDATE_CHECK
auto updater = this->updater;
auto oldthread = std::make_shared<std::thread>();
std::swap(*oldthread, updatethread);
updatethread = std::thread([this, updater, oldthread]() mutable {
try {
if (oldthread->joinable()) {
oldthread->join();
}
if (!updater) {
char * appimage = getenv("APPIMAGE");
if (appimage) {
#ifndef NDEBUG
printf("Appimage create updater\n");
#endif
this->updater = updater = std::make_shared<appimage::update::Updater>(appimage, true);
} else {
#ifndef NDEBUG
printf("Appimage cannot be updated\n");
#endif
emit updateError(QObject::tr("Appimage cannot be updated<br/>Expected Environmentvariable 'APPIMAGE' to be set to the path of the AppImage"));
return;
}
}
bool _updateAvailable = false;
#ifndef NDEBUG
printf("Appimage check for changes\n");
#endif
if (!updater->checkForChanges(_updateAvailable)) {
#ifndef NDEBUG
printf("Appimage Error\n");
#endif
std::stringstream errorstream;
std::string nextMessage;
while (updater->nextStatusMessage(nextMessage)) {
#ifndef NDEBUG
printf("appimage update error %s\n", nextMessage.data());
#endif
errorstream << nextMessage << "<br/>";
}
emit updateError(QObject::tr("Appimage cannot be updated") + "<br/>" + QString::fromStdString(errorstream.str()));
return;
}
#ifndef NDEBUG
printf("Appimage Found Update? %d\n", (int)_updateAvailable);
#endif
if (_updateAvailable) {
emit updateAvailable("");
}
emit updateCheck(_updateAvailable);
} catch (...) {
emit updateError(QObject::tr("Appimage cannot be updated<br/>Unknown Error"));
}
});
#elif defined(UPDATE_CHECK)
QNetworkRequest request(QStringLiteral(UPDATE_CHECK_URL));
netAccessManager.get(request);
#else
emit updateError(QObject::tr("Launcher cannot be updated<br/>You have to check your packagemanager for updates or recompile your Open Source build with newer sources"));
#endif
}
#endif
void UpdateChecker::onRequestFinished(QNetworkReply* reply) {
#ifdef UPDATE_CHECK
if (reply->error() != QNetworkReply::NoError) {
emit updateError(QObject::tr("Failed to check for update<br/>Failed to connect to update server"));
return;
}
auto redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (redirect.isValid()) {
QNetworkRequest request(redirect);
netAccessManager.get(request);
return;
}
QMap<QString, QString> props;
QString replyText = QString::fromUtf8(reply->readAll());
for (QStringRef const& line : replyText.splitRef('\n')) {
auto iof = line.indexOf('=');
if (iof == -1)
continue;
props[line.left(iof).toString()] = line.mid(iof + 1).trimmed().toString();
}
#ifndef NDEBUG
printf("server build id: %i\n", props["build_id"].toInt());
#endif
bool updateavailable = props["build_id"].toInt() > UPDATE_CHECK_BUILD_ID;
if (updateavailable)
emit updateAvailable(props["download_url"]);
emit updateCheck(updateavailable);
#endif
}
void UpdateChecker::startUpdate() {
#ifdef APPIMAGE_UPDATE_CHECK
if (updater) {
auto oldthread = std::make_shared<std::thread>();
std::swap(*oldthread, updatethread);
updatethread = std::thread([this, oldthread] {
if (oldthread->joinable()) {
oldthread->join();
}
m_active = true;
emit activeChanged();
updater->start();
std::stringstream errorstream;
while (!updater->isDone()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
double _progress;
if (!updater->progress(_progress)) {
#ifndef NDEBUG
printf("appimage startUpdate Call to progress() failed\n");
#endif
m_active = false;
emit activeChanged();
return;
}
emit progress(_progress);
// fetch all status messages
// this is basically the same as before
std::string nextMessage;
while (updater->nextStatusMessage(nextMessage)) {
#ifndef NDEBUG
printf("appimage startUpdate %s\n", nextMessage.data());
#endif
errorstream << nextMessage << "<br/>";
}
}
m_active = false;
emit activeChanged();
if (updater->hasError()) {
#ifndef NDEBUG
printf("Error occurred. See previous messages for details.\n");
#endif
emit updateError(QObject::tr("Appimage cannot be updated") + "<br/>" + QString::fromStdString(errorstream.str()));
} else {
updater->copyPermissionsToNewFile();
emit requestRestart();
}
});
}
#endif
}
#ifdef APPIMAGE_UPDATE_CHECK
UpdateChecker::~UpdateChecker() {
if (updatethread.joinable()) {
updatethread.join();
}
}
#endif