Repository navigation
Expand file tree
/
Copy pathupdatechecker.cpp
More file actions
201 lines (174 loc) · 6.33 KB
/
Copy pathupdatechecker.cpp
File metadata and controls
201 lines (174 loc) · 6.33 KB
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
#include "updatechecker.h"
#include "version.h"
#include "util.h"
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QMessageBox>
#include <QDesktopServices>
#include <QPushButton>
#include <QSettings>
#include <QDateTime>
#include <QUrl>
#include <QRegularExpression>
static constexpr int CHECK_INTERVAL_SECS = 86400; // 24 hours
UpdateChecker::UpdateChecker(QWidget *parent)
: QObject(parent)
, m_parentWidget(parent)
, m_manager(new QNetworkAccessManager(this))
, m_updateUrl(DEFAULT_UPDATE_URL)
, m_forceCheck(false)
{
connect(m_manager, &QNetworkAccessManager::finished, this, &UpdateChecker::onReplyFinished);
}
void UpdateChecker::setUpdateUrl(const QString &url)
{
m_updateUrl = url.isEmpty() ? DEFAULT_UPDATE_URL : url;
}
QString UpdateChecker::updateUrl() const
{
return m_updateUrl;
}
void UpdateChecker::checkForUpdates(bool force)
{
m_forceCheck = force;
if(!force)
{
QSettings settings(BuildUserPath("settings.ini"), QSettings::IniFormat);
qint64 lastCheck = settings.value("updater/lastCheckTime", 0).toLongLong();
qint64 now = QDateTime::currentSecsSinceEpoch();
if(now - lastCheck < CHECK_INTERVAL_SECS)
return;
}
QNetworkRequest request{QUrl(m_updateUrl)};
request.setRawHeader("Accept", "application/vnd.github.v3+json");
request.setRawHeader("User-Agent", "SourceAdminTool/" APP_VERSION_STRING);
m_manager->get(request);
}
void UpdateChecker::onReplyFinished(QNetworkReply *reply)
{
reply->deleteLater();
if(reply->error() != QNetworkReply::NoError)
{
if(m_forceCheck)
{
QMessageBox::warning(m_parentWidget, "Update Check Failed",
QString("Could not check for updates:\n%1").arg(reply->errorString()));
}
return;
}
// Update last check time
QSettings settings(BuildUserPath("settings.ini"), QSettings::IniFormat);
settings.setValue("updater/lastCheckTime", QDateTime::currentSecsSinceEpoch());
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
// Support both single object (/releases/latest) and array (/releases?per_page=1)
QJsonObject release;
if(doc.isArray())
{
QJsonArray arr = doc.array();
if(arr.isEmpty())
{
if(m_forceCheck)
QMessageBox::information(m_parentWidget, "No Updates", "No releases found.");
return;
}
release = arr[0].toObject();
}
else if(doc.isObject())
{
release = doc.object();
}
else
{
return;
}
QString tagName = release["tag_name"].toString();
QString releaseName = release["name"].toString();
QString htmlUrl = release["html_url"].toString();
QString body = release["body"].toString();
QString targetCommit = release["target_commitish"].toString();
// Extract version: try tag first (e.g. "v1.2.0"), then release name (e.g. "Dev Build v1.2.0 (abc1234)")
QString remoteVersion = tagName;
if(remoteVersion.startsWith('v') || remoteVersion.startsWith('V'))
remoteVersion = remoteVersion.mid(1);
// If tag isn't a valid semver (e.g. "dev"), extract version from release name
QRegularExpression versionRx("(\\d+\\.\\d+\\.\\d+)");
if(!versionRx.match(remoteVersion).hasMatch())
{
QRegularExpressionMatch m = versionRx.match(releaseName);
if(m.hasMatch())
remoteVersion = m.captured(1);
}
bool isNewer = isNewerVersion(remoteVersion, APP_VERSION_STRING);
// For same version (dev builds), compare commit hashes
bool isDifferentCommit = false;
if(!isNewer && !targetCommit.isEmpty())
{
QString localCommit = APP_COMMIT_HASH_FULL;
// target_commitish is the full SHA; compare with our full hash
if(localCommit != "unknown" && targetCommit != localCommit)
isDifferentCommit = true;
}
if(!isNewer && !isDifferentCommit)
{
if(m_forceCheck)
{
QMessageBox::information(m_parentWidget, "No Updates",
QString("You are running the latest version (%1 - %2).").arg(APP_VERSION_STRING, APP_COMMIT_HASH));
}
return;
}
// Build display string for the update
QString remoteDisplay = remoteVersion;
if(isDifferentCommit && !targetCommit.isEmpty())
remoteDisplay = QString("%1 (%2)").arg(remoteVersion, targetCommit.left(7));
QString localDisplay = QString("%1 (%2)").arg(APP_VERSION_STRING, APP_COMMIT_HASH);
// Check if user previously skipped this version/commit
if(!m_forceCheck)
{
QString skipped = settings.value("updater/skippedVersion").toString();
QString skipKey = isDifferentCommit ? targetCommit.left(7) : remoteVersion;
if(skipped == skipKey)
return;
}
// Show update dialog
QMessageBox msgBox(m_parentWidget);
msgBox.setWindowTitle("Update Available");
msgBox.setIcon(QMessageBox::Information);
msgBox.setText(QString("A new version <b>%1</b> is available! You are running %2.")
.arg(remoteDisplay, localDisplay));
if(!body.isEmpty())
{
// Truncate long release notes
QString notes = body.length() > 500 ? body.left(500) + "..." : body;
msgBox.setInformativeText(notes);
}
QPushButton *downloadBtn = msgBox.addButton("Download", QMessageBox::AcceptRole);
QPushButton *skipBtn = msgBox.addButton("Skip Version", QMessageBox::DestructiveRole);
msgBox.addButton("Later", QMessageBox::RejectRole);
msgBox.exec();
if(msgBox.clickedButton() == downloadBtn)
{
QDesktopServices::openUrl(QUrl(htmlUrl));
}
else if(msgBox.clickedButton() == skipBtn)
{
QString skipKey = isDifferentCommit ? targetCommit.left(7) : remoteVersion;
settings.setValue("updater/skippedVersion", skipKey);
}
}
bool UpdateChecker::isNewerVersion(const QString &remote, const QString &local) const
{
QStringList remoteParts = remote.split('.');
QStringList localParts = local.split('.');
for(int i = 0; i < qMax(remoteParts.size(), localParts.size()); i++)
{
int r = i < remoteParts.size() ? remoteParts[i].toInt() : 0;
int l = i < localParts.size() ? localParts[i].toInt() : 0;
if(r > l) return true;
if(r < l) return false;
}
return false;
}