From fbdfe1fc333f160daa18ec0f57e9e11820d7e8c2 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 14 Jan 2019 14:19:01 -0800 Subject: [PATCH] #87 Notify if update is available --- src/precompiled.h | 1 + src/rpc.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ src/rpc.h | 1 + 3 files changed, 55 insertions(+) diff --git a/src/precompiled.h b/src/precompiled.h index 2938652..9d0fe75 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc.cpp b/src/rpc.cpp index 10aae7c..5b5cfef 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -4,6 +4,7 @@ #include "settings.h" #include "senttxstore.h" #include "turnstile.h" +#include "version.h" using json = nlohmann::json; @@ -85,6 +86,7 @@ void RPC::setConnection(Connection* c) { ui->statusBar->showMessage("Ready!"); refreshZECPrice(); + checkForUpdate(); // Force update, because this might be coming from a settings update // where we need to immediately refresh @@ -903,6 +905,57 @@ void RPC::watchTxStatus() { }); } +void RPC::checkForUpdate() { + if (conn == nullptr) + return noConnection(); + + QUrl cmcURL("https://api.github.com/repos/ZcashFoundation/zec-qt-wallet/releases"); + + QNetworkRequest req; + req.setUrl(cmcURL); + + QNetworkReply *reply = conn->restclient->get(req); + + QObject::connect(reply, &QNetworkReply::finished, [=] { + reply->deleteLater(); + + try { + if (reply->error() == QNetworkReply::NoError) { + + auto releases = QJsonDocument::fromJson(reply->readAll()).array(); + QVersionNumber maxVersion(0, 0, 0); + + for (QJsonValue rel : releases) { + QString tag = rel.toObject()["tag_name"].toString(); + if (tag.startsWith("v")) + tag = tag.right(tag.length() - 1); + + auto v = QVersionNumber::fromString(tag); + if (v > maxVersion) + maxVersion = v; + } + + auto currentVersion = QVersionNumber::fromString(APP_VERSION); + qDebug() << "Version check: Current " << currentVersion << ", Available " << maxVersion; + if (maxVersion > currentVersion) { + auto ans = QMessageBox::information(main, QObject::tr("Update Available"), + QObject::tr("A new release v%1 is available! You have v%2.\n\nWould you like to visit the releases page?") + .arg(maxVersion.toString()) + .arg(currentVersion.toString()), + QMessageBox::Yes, QMessageBox::Cancel); + if (ans == QMessageBox::Yes) { + QDesktopServices::openUrl(QUrl("https://github.com/ZcashFoundation/zec-qt-wallet/releases")); + } + } + } + } + catch (...) { + // If anything at all goes wrong, just set the price to 0 and move on. + qDebug() << QString("Caught something nasty"); + } + }); +} + // Get the ZEC->USD price from coinmarketcap using their API void RPC::refreshZECPrice() { if (conn == nullptr) diff --git a/src/rpc.h b/src/rpc.h index 358031c..7f0c18d 100644 --- a/src/rpc.h +++ b/src/rpc.h @@ -38,6 +38,7 @@ public: void refreshAddresses(); + void checkForUpdate(); void refreshZECPrice(); void getZboardTopics(std::function)> cb);