forked from RTMilliken/EDFbrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_for_updates.cpp
143 lines (102 loc) · 3.79 KB
/
check_for_updates.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
#include "check_for_updates.h"
Check_for_updates::Check_for_updates()
{
QNetworkRequest request;
QNetworkReply* reply;
manager = new QNetworkAccessManager(this);
qDebug() << QSslSocket::sslLibraryBuildVersionString();
qDebug() << QSslSocket::sslLibraryVersionString();
request.setUrl(QUrl("https://api.github.com/repos/andrasfuchs/edfbrowser/releases"));
request.setRawHeader("User-Agent", PROGRAM_NAME " " PROGRAM_VERSION " OS_UNKNOWN");
#ifdef Q_OS_LINUX
request.setRawHeader("User-Agent", PROGRAM_NAME " " PROGRAM_VERSION " Q_OS_LINUX");
#elif defined (Q_OS_MAC)
request.setRawHeader("User-Agent", PROGRAM_NAME " " PROGRAM_VERSION " Q_OS_MAC");
#elif defined (Q_OS_WIN32)
request.setRawHeader("User-Agent", PROGRAM_NAME " " PROGRAM_VERSION " Q_OS_WIN32");
#else
request.setRawHeader("User-Agent", PROGRAM_NAME " " PROGRAM_VERSION " Q_OS_OTHER");
#endif
request.setRawHeader("Accept", "application/vnd.github+json");
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
reply = manager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
void Check_for_updates::replyFinished()
{
int this_major_version,
this_minor_version,
this_path_version,
latest_major_version,
latest_minor_version,
latest_patch_version;
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (reply->error() != QNetworkReply::NoError)
{
reply->deleteLater();
return;
}
// read all data from the reply as a string
QString replyString = (QString)reply->readAll();
if (replyString.isEmpty())
{
reply->deleteLater();
return;
}
// parse replyString as JSON
QJsonDocument jsonResponse = QJsonDocument::fromJson(replyString.toUtf8());
// get the most recent non-draft, non-prerelese release
//QJsonObject jsonRoot = jsonResponse.object();
QJsonArray jsonReleases = jsonResponse.array();
// check if there are any releases in the array
if (jsonReleases.isEmpty())
{
reply->deleteLater();
return;
}
// remove all draft and prerelease releases
for (int i = 0; i < jsonReleases.size(); i++)
{
if (jsonReleases.at(i).toObject()["draft"].toBool() || jsonReleases.at(i).toObject()["prerelease"].toBool())
{
jsonReleases.removeAt(i);
i--;
}
}
// reorder the jsonReleases array descending by release date
for (int i = 0; i < jsonReleases.size(); i++)
{
for (int j = i + 1; j < jsonReleases.size(); j++)
{
if (jsonReleases.at(i).toObject()["published_at"].toString() < jsonReleases.at(j).toObject()["published_at"].toString())
{
QJsonObject temp = jsonReleases.at(i).toObject();
jsonReleases[i] = jsonReleases.at(j);
jsonReleases[j] = temp;
}
}
}
// get the tag of the most recent release
QString latest_version_string = jsonReleases.first()["tag_name"].toString();
// covert version string to numbers
sscanf(PROGRAM_VERSION, "%d.%d.%d", &this_major_version, &this_minor_version, &this_path_version);
sscanf(latest_version_string.toLocal8Bit().data(), "v%d.%d.%d", &latest_major_version, &latest_minor_version, &latest_patch_version);
// return if the this version is the latest
if ((this_major_version > latest_major_version)
|| ((this_major_version == latest_major_version) && (this_minor_version > latest_minor_version))
|| ((this_major_version == latest_major_version) && (this_minor_version == latest_minor_version) && (this_path_version >= latest_patch_version)))
{
reply->deleteLater();
return;
}
QMessageBox messagewindow(QMessageBox::Information,
"New version available",
"A newer version of EDFbrowser is available.\n"
"Do you want to download the new version now?",
QMessageBox::Yes | QMessageBox::No);
if (messagewindow.exec() != QMessageBox::Yes)
{
return;
}
QDesktopServices::openUrl(QUrl("https://www.github.com/andrasfuchs/edfbrowser/releases"));
}