-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "updater.h" | ||
|
||
#include <QDesktopServices> | ||
#include <QMessageBox> | ||
#include <QObject> | ||
#include <QUrl> | ||
#include <QVersionNumber> | ||
#include <QtNetwork/QNetworkAccessManager> | ||
#include <QtNetwork/QNetworkReply> | ||
#include <QtNetwork/QNetworkRequest> | ||
#include "version.h" | ||
|
||
Updater::Updater(QObject* parent) : QObject(parent) {} | ||
|
||
void Updater::startUpdateQuery() { | ||
manager = new QNetworkAccessManager(this); | ||
QNetworkRequest request; | ||
QUrl url("https://api.github.com/repos/Niakr1s/rrt/releases/latest"); | ||
request.setUrl(url); | ||
QNetworkReply* reply = manager->get(request); | ||
connect(reply, &QNetworkReply::finished, this, | ||
&Updater::onUpdateQueryFinished); | ||
} | ||
|
||
void Updater::onUpdateQueryFinished() { | ||
QNetworkReply* reply = qobject_cast<QNetworkReply*>(QObject::sender()); | ||
if (reply->error() != QNetworkReply::NoError) { | ||
qDebug() << "MainWindow::onUpdateQueryFinished error: " | ||
<< reply->errorString(); | ||
return; | ||
} | ||
reply->deleteLater(); | ||
auto json = QJsonDocument::fromJson(reply->readAll()); | ||
parseJson(json); | ||
} | ||
|
||
void Updater::parseJson(const QJsonDocument json) { | ||
auto netVer = json["tag_name"].toString(); | ||
if (netVer.isNull()) { | ||
qDebug() << "Updater::parseJson: tag_name is empty"; | ||
return; | ||
} | ||
while (netVer[0].isLetter()) { | ||
netVer.remove(0, 1); | ||
} | ||
auto netVerVec = netVer.split("."); | ||
QVersionNumber netVerNum(netVerVec[0].toInt(), netVerVec[1].toInt(), | ||
netVerVec[2].toInt()); | ||
QVersionNumber localVerNum(std::stoi(PROJECT_VER_MAJOR), | ||
std::stoi(PROJECT_VER_MINOR), | ||
std::stoi(PROJECT_VER_PATCH)); | ||
qDebug() << "Updater::parseJson: got versions: local = " << localVerNum | ||
<< ", net = " << netVerNum; | ||
if (netVerNum > localVerNum) { | ||
showUpdateNotification(json["html_url"].toString()); | ||
} | ||
} | ||
|
||
void Updater::showUpdateNotification(const QString& updateUrl) { | ||
auto msg = new QMessageBox(); | ||
msg->setWindowTitle(tr("Got new update!")); | ||
msg->setText(QString("<div>%1 <a href=%2>%3</a> %4</div>") | ||
.arg(tr("Please, visit")) | ||
.arg(updateUrl) | ||
.arg("github") | ||
.arg(tr("to update"))); | ||
msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); | ||
|
||
if (auto btn = msg->exec(); btn == QMessageBox::Ok) { | ||
QDesktopServices::openUrl(QUrl(updateUrl)); | ||
} | ||
delete msg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef UPDATER_H | ||
#define UPDATER_H | ||
|
||
#include <QJsonDocument> | ||
#include <QNetworkAccessManager> | ||
#include <QObject> | ||
|
||
class Updater : public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
Updater(QObject* parent = nullptr); | ||
virtual ~Updater() {} | ||
|
||
void startUpdateQuery(); | ||
|
||
public slots: | ||
void onUpdateQueryFinished(); | ||
|
||
private: | ||
QNetworkAccessManager* manager; | ||
|
||
void parseJson(const QJsonDocument json); | ||
void showUpdateNotification(const QString& updateUrl); | ||
}; | ||
|
||
#endif // UPDATER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef VERSION_H | ||
#define VERSION_H | ||
|
||
#define PROJECT_NAME "@PROJECT_NAME@" | ||
#define PROJECT_VER "@PROJECT_VERSION@" | ||
#define PROJECT_VER_MAJOR "@PROJECT_VERSION_MAJOR@" | ||
#define PROJECT_VER_MINOR "@PROJECT_VERSION_MINOR@" | ||
#define PROJECT_VER_PATCH "@PROJECT_VERSION_PATCH@" | ||
|
||
#endif // VERSION_H |