-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from AIRLegend/dev
Dev
- Loading branch information
Showing
19 changed files
with
262 additions
and
29 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
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
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,52 @@ | ||
#include "UpdateChecker.h" | ||
|
||
|
||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
#include <QJsonArray> | ||
#include <iostream> | ||
#include <QObject> | ||
|
||
|
||
UpdateChecker::UpdateChecker(std::string& version, IUpdateSub *obs): | ||
request(), | ||
manager(), | ||
current_version(version) | ||
{ | ||
this->observer = obs; | ||
} | ||
|
||
void UpdateChecker::callback(QNetworkReply* reply) | ||
{ | ||
if (reply->error()) { | ||
qDebug() << reply->errorString(); | ||
return; | ||
} | ||
QString answer = reply->readAll(); | ||
QJsonDocument doc = QJsonDocument::fromJson(answer.toUtf8()); | ||
QJsonArray json_array = doc.array(); | ||
QJsonObject latest_update = json_array[0].toObject(); | ||
|
||
Version v(latest_update["tag_name"].toString().toStdString()); | ||
|
||
qDebug() << latest_update["tag_name"].toString(); | ||
|
||
this->observer->on_update_check_completed((current_version < v)); | ||
} | ||
|
||
|
||
void UpdateChecker::get_latest_update(std::string& repo) | ||
{ | ||
QObject::connect( | ||
&manager, | ||
SIGNAL(finished(QNetworkReply*)), | ||
this, | ||
SLOT(callback(QNetworkReply*)) | ||
); | ||
|
||
std::cout << " REQUEST "<<std::endl; | ||
|
||
QString url = QString("https://api.github.com/repos/%1/releases").arg(QString::fromStdString(repo)); | ||
request.setUrl(QUrl(url)); | ||
manager.get(request); | ||
} |
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,86 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <QNetworkAccessManager> | ||
#include <QNetworkReply> | ||
|
||
|
||
class Version | ||
{ | ||
public: | ||
int stage; // alpha, beta or normal | ||
std::string ver; | ||
|
||
Version(std::string version): ver(), stage(10) | ||
{ | ||
int index_dash = -1; | ||
|
||
for (int i = 0; i < version.size(); i++) | ||
{ | ||
if (version.at(i) == *".") | ||
continue; | ||
if (version.at(i) == *"-") | ||
{ | ||
index_dash = i; | ||
break; | ||
} | ||
ver.push_back(version.at(i)); | ||
} | ||
|
||
if (index_dash > 0) | ||
{ | ||
// Version id contains alpha/beta | ||
if (version.at(index_dash + 1) == *"a") // alpha | ||
stage = 0; | ||
if (version.at(index_dash + 1) == *"b") // beta | ||
stage = 1; | ||
// Else, its a normal version | ||
} | ||
}; | ||
|
||
bool operator<(Version const& rhs) const | ||
{ | ||
int numbers_comparison = rhs.ver.compare(ver); | ||
if (numbers_comparison == 0) | ||
{ | ||
return stage < rhs.stage; | ||
} | ||
else | ||
{ | ||
return numbers_comparison > 0; | ||
} | ||
} | ||
}; | ||
|
||
class IUpdateSub | ||
{ | ||
public: | ||
/* | ||
* Callback to check whether a newer version is available. | ||
*/ | ||
virtual void on_update_check_completed(bool update_exists) = 0; | ||
}; | ||
|
||
|
||
|
||
class UpdateChecker : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private: | ||
QNetworkRequest request; | ||
QNetworkAccessManager manager; | ||
IUpdateSub* observer; | ||
Version current_version; | ||
|
||
public: | ||
UpdateChecker(std::string &version, IUpdateSub *obs); | ||
void get_latest_update(std::string &repo); | ||
|
||
|
||
private slots: | ||
void callback(QNetworkReply* reply); | ||
}; | ||
|
||
|
||
|
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
Oops, something went wrong.