diff --git a/chatterino.pro b/chatterino.pro index c2f93815e..10d5c130c 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -179,7 +179,8 @@ SOURCES += \ src/util/rapidjson-helpers.cpp \ src/singletons/helper/pubsubhelpers.cpp \ src/singletons/helper/pubsubactions.cpp \ - src/widgets/selectchanneldialog.cpp + src/widgets/selectchanneldialog.cpp \ + src/singletons/updatemanager.cpp HEADERS += \ src/precompiled_header.hpp \ @@ -301,7 +302,8 @@ HEADERS += \ src/util/rapidjson-helpers.hpp \ src/singletons/helper/pubsubhelpers.hpp \ src/singletons/helper/pubsubactions.hpp \ - src/widgets/selectchanneldialog.hpp + src/widgets/selectchanneldialog.hpp \ + src/singletons/updatemanager.hpp RESOURCES += \ resources/resources.qrc diff --git a/src/singletons/updatemanager.cpp b/src/singletons/updatemanager.cpp new file mode 100644 index 000000000..4c7a5d4c7 --- /dev/null +++ b/src/singletons/updatemanager.cpp @@ -0,0 +1,54 @@ +#include "updatemanager.hpp" + +#include "util/networkrequest.hpp" +#include "version.hpp" + +namespace chatterino { +namespace singletons { + +UpdateManager::UpdateManager() + : currentVersion(CHATTERINO_VERSION) +{ +} + +UpdateManager &UpdateManager::getInstance() +{ + static UpdateManager instance; + return instance; +} + +const QString &UpdateManager::getCurrentVersion() const +{ + return this->getCurrentVersion(); +} + +const QString &UpdateManager::getOnlineVersion() const +{ + return this->getOnlineVersion(); +} + +void UpdateManager::checkForUpdates() +{ + QString url = "https://notitia.chatterino.com/version/chatterino/" CHATTERINO_OS "/stable"; + + util::NetworkRequest req(url); + req.setTimeout(20000); + req.getJSON([this](QJsonDocument &document) { + if (document.isEmpty()) { + qDebug() << "error updating"; + return; + } + + QJsonValue version_val = document.object().value("version"); + if (!version_val.isString()) { + qDebug() << "error updating"; + return; + } + + this->onlineVersion = version_val.toString(); + this->onlineVersionUpdated.invoke(); + }); +} + +} // namespace singletons +} // namespace chatterino diff --git a/src/singletons/updatemanager.hpp b/src/singletons/updatemanager.hpp new file mode 100644 index 000000000..3f4bba2b1 --- /dev/null +++ b/src/singletons/updatemanager.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include ; +#include + +namespace chatterino { +namespace singletons { + +class UpdateManager +{ + UpdateManager(); + +public: + static UpdateManager &getInstance(); + + void checkForUpdates(); + const QString &getCurrentVersion() const; + const QString &getOnlineVersion() const; + + pajlada::Signals::Signal onlineVersionUpdated; + +private: + QString currentVersion; + QString onlineVersion; +}; + +} // namespace singletons +} // namespace chatterino diff --git a/src/version.hpp b/src/version.hpp index d382a5211..131c467be 100644 --- a/src/version.hpp +++ b/src/version.hpp @@ -1,3 +1,13 @@ #pragma once +#include + #define CHATTERINO_VERSION "2.0.0" + +#if defined(Q_OS_WIN) +#define CHATTERINO_OS "win" +#elif defined(Q_OS_MACOS) +#define CHATTERINO_OS "macos" +#elif defined(Q_OS_LINUX) +#define CHATTERINO_OS "linux" +#endif