-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateChecker.cpp
More file actions
163 lines (132 loc) · 4.96 KB
/
UpdateChecker.cpp
File metadata and controls
163 lines (132 loc) · 4.96 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
#include "UpdateChecker.h"
#include "version.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QNetworkRequest>
#include <QDebug>
#ifdef Q_OS_MACOS
#include <QSysInfo>
#endif
UpdateChecker::UpdateChecker(QObject *parent)
: QObject(parent)
, networkManager_(new QNetworkAccessManager(this))
{
}
void UpdateChecker::checkForUpdates() {
// GitHub API endpoint for latest release
const QString apiUrl = QStringLiteral("https://api.github.com/repos/LookAtWhatAiCanDo/Phraims/releases/latest");
QNetworkRequest request(apiUrl);
request.setHeader(QNetworkRequest::UserAgentHeader,
QString("Phraims/%1").arg(PHRAIMS_VERSION));
QNetworkReply *reply = networkManager_->get(request);
connect(reply, &QNetworkReply::finished, this, &UpdateChecker::onNetworkReplyFinished);
}
void UpdateChecker::onNetworkReplyFinished() {
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) {
return;
}
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
emit updateCheckFailed(tr("Failed to check for updates: %1").arg(reply->errorString()));
return;
}
const QByteArray data = reply->readAll();
const UpdateInfo info = parseGitHubResponse(data);
if (info.latestVersion.isEmpty()) {
emit updateCheckFailed(tr("Could not parse update information from GitHub"));
return;
}
emit updateCheckCompleted(info);
}
UpdateChecker::UpdateInfo UpdateChecker::parseGitHubResponse(const QByteArray &jsonData) {
UpdateInfo info;
info.currentVersion = QString(PHRAIMS_VERSION);
QJsonParseError parseError;
const QJsonDocument doc = QJsonDocument::fromJson(jsonData, &parseError);
if (parseError.error != QJsonParseError::NoError) {
qWarning() << "Failed to parse GitHub API response:" << parseError.errorString();
return info;
}
const QJsonObject root = doc.object();
// Extract version from tag_name (e.g., "v0.56" -> "0.56")
QString tagName = root.value("tag_name").toString();
if (tagName.startsWith('v') || tagName.startsWith('V')) {
tagName = tagName.mid(1);
}
info.latestVersion = tagName;
// Extract release information
info.releaseUrl = root.value("html_url").toString();
info.releaseNotes = root.value("body").toString();
// Find download URL for current platform
const QJsonArray assets = root.value("assets").toArray();
info.downloadUrl = getDownloadUrlForPlatform(assets);
// Compare versions
const int comparison = compareVersions(info.currentVersion, info.latestVersion);
info.updateAvailable = (comparison < 0);
return info;
}
QString UpdateChecker::getDownloadUrlForPlatform(const QJsonArray &assets) {
// Determine the appropriate file pattern for this platform
QString platformPattern;
#ifdef Q_OS_MACOS
// macOS: Look for .dmg file matching current architecture
const QString arch = QSysInfo::currentCpuArchitecture();
// On Apple Silicon, QSysInfo returns "arm64"
// Also check "aarch64" for compatibility with older Qt versions
if (arch == "arm64" || arch == "aarch64") {
platformPattern = "macOS-arm64.dmg";
} else {
platformPattern = "macOS-x86_64.dmg";
}
#elif defined(Q_OS_WIN)
// Windows: Look for .exe installer
// Use Qt's cross-compiler architecture detection for better portability
const QString winArch = QSysInfo::currentCpuArchitecture();
if (winArch == "arm64" || winArch == "aarch64") {
platformPattern = "Windows-arm64.exe";
} else {
platformPattern = "Windows-x64.exe";
}
#elif defined(Q_OS_LINUX)
// Linux: Look for appropriate package (AppImage, deb, etc.)
// For now, just return the release page URL
platformPattern = ""; // Will return empty, causing fallback to release page
#endif
// Search assets for matching file
for (const QJsonValue &assetValue : assets) {
const QJsonObject asset = assetValue.toObject();
const QString name = asset.value("name").toString();
if (!platformPattern.isEmpty() && name.contains(platformPattern, Qt::CaseInsensitive)) {
return asset.value("browser_download_url").toString();
}
}
return QString(); // Return empty if no matching asset found
}
int UpdateChecker::compareVersions(const QString &version1, const QString &version2) {
// Strip any leading 'v' or 'V' prefix
QString v1 = version1;
QString v2 = version2;
if (v1.startsWith('v') || v1.startsWith('V')) {
v1 = v1.mid(1);
}
if (v2.startsWith('v') || v2.startsWith('V')) {
v2 = v2.mid(1);
}
// Split into components
const QStringList parts1 = v1.split('.');
const QStringList parts2 = v2.split('.');
// Compare each component
const int maxParts = qMax(parts1.size(), parts2.size());
for (int i = 0; i < maxParts; ++i) {
const int num1 = (i < parts1.size()) ? parts1[i].toInt() : 0;
const int num2 = (i < parts2.size()) ? parts2[i].toInt() : 0;
if (num1 < num2) {
return -1;
} else if (num1 > num2) {
return 1;
}
}
return 0; // versions are equal
}