-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
UpdateNotifier.cpp
96 lines (85 loc) · 3.11 KB
/
UpdateNotifier.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
/*
* Copyright (C) 2019 Ashar Khan <ashar786khan@gmail.com>
*
* This file is part of CPEditor.
*
* CPEditor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* I will not be responsible if CPEditor behaves in unexpected way and
* causes your ratings to go down and or loose any important contest.
*
* Believe Software is "Software" and it isn't not immune to bugs.
*
*/
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>
#include <UpdateNotifier.hpp>
namespace Telemetry
{
UpdateNotifier::UpdateNotifier(bool useBeta)
{
manager = new QNetworkAccessManager();
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(managerFinished(QNetworkReply *)));
beta = useBeta;
}
UpdateNotifier::~UpdateNotifier()
{
delete manager;
}
void UpdateNotifier::setBeta(bool value)
{
beta = value;
}
void UpdateNotifier::checkUpdate(bool force)
{
this->force = force;
request.setUrl(QUrl("https://api.github.com/repos/coder3101/cp-editor/releases"));
manager->get(request);
}
QString UpdateNotifier::currentVersionStr()
{
std::string version = std::string(APP_VERSION_MAJOR) + "." + APP_VERSION_MINOR + "." + APP_VERSION_PATCH;
return QString::fromStdString(version);
}
void UpdateNotifier::managerFinished(QNetworkReply *reply)
{
if (reply->error())
{
qDebug() << reply->errorString();
return;
}
QString jsonReply = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(jsonReply.toUtf8());
doc = QJsonDocument::fromVariant(doc.array().at(0).toVariant());
QString latestRelease = doc["tag_name"].toString();
bool isBeta = doc["prerelease"].toBool();
QString downloadUrl = doc["html_url"].toString();
bool isUpdateAvailable = (latestRelease > currentVersionStr());
if (beta && isBeta && isUpdateAvailable)
{
QMessageBox::about(nullptr, QString::fromStdString("Beta Update available"),
QString::fromStdString("A new beta update " + latestRelease.toStdString() +
" is available. <a href = " + downloadUrl.toStdString() +
">Please Download" + "</a>"));
}
else if (!isBeta && isUpdateAvailable)
{
QMessageBox::about(nullptr, QString::fromStdString("New Update available"),
QString::fromStdString("A new stable update " + latestRelease.toStdString() +
" is available. <a href = " + downloadUrl.toStdString() +
">Please Download" + "</a>"));
}
else if (force)
{
QMessageBox::about(
nullptr, QString::fromStdString("No new update"),
QString::fromStdString(
"You are already running latest release. Keep checking so you dont miss on important update."));
force = false;
}
}
} // namespace Telemetry