Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
#48 Check for update mechanism
Browse files Browse the repository at this point in the history
Link UpdateToolBar with UpdaterViewModel and add a check for update
button.
  • Loading branch information
ColinDuquesnoy committed Jul 18, 2017
1 parent 9d3b2eb commit 5e9d6d6
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QtCore/QJsonDocument>
#include <MellowPlayer/Application/Logging/LoggingManager.hpp>
#include <MellowPlayer/Application/Updater/Release.hpp>
#include "GithubReleaseQuerier.hpp"

Expand All @@ -8,32 +9,47 @@
using namespace MellowPlayer::Application;

GithubReleaseQuerier::GithubReleaseQuerier(IHttpClient& httpClient):
httpClient_(httpClient) {
connect(&httpClient, &IHttpClient::replyReceived,
this, &GithubReleaseQuerier::onReplyReceived);
logger_(LoggingManager::instance().getLogger("GithubReleaseQuerier")), httpClient_(httpClient)
{
connect(&httpClient, &IHttpClient::replyReceived, this, &GithubReleaseQuerier::onReplyReceived);
connect(&replyParser_, &GithubReleasesReplyParser::ready, this, &GithubReleaseQuerier::onReleaseReady);
}

void GithubReleaseQuerier::setChannel(UpdateChannel channel) {
void GithubReleaseQuerier::setChannel(UpdateChannel channel)
{
channel_ = channel;
}

void GithubReleaseQuerier::getLatest() {
void GithubReleaseQuerier::getLatest()
{
LOG_DEBUG(logger_, "getting releases");
found_ = false;
httpClient_.get("https://api.github.com/repos/ColinDuquesnoy/MellowPlayer/releases");
}

void GithubReleaseQuerier::onReplyReceived(const QByteArray& replyData) {
void GithubReleaseQuerier::onReplyReceived(const QByteArray& replyData)
{
LOG_DEBUG(logger_, "Reply recevied");
LOG_TRACE(logger_, "reply data: " << replyData.toStdString());
replyParser_.parse(replyData);
if (!found_) {
LOG_DEBUG(logger_, "no release found");
emit latestReceived(nullptr);
}
}

void GithubReleaseQuerier::onReleaseReady(const Release* release) {
void GithubReleaseQuerier::onReleaseReady(const Release* release)
{
if (release != nullptr && accept(release)) {
LOG_DEBUG(logger_, "Latest release found: " << release->getName());
emit latestReceived(release);
replyParser_.stop();
found_ = true;
}
}

bool GithubReleaseQuerier::accept(const Release* release) {
bool GithubReleaseQuerier::accept(const Release* release)
{
bool accepted = false;

switch (channel_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace MellowPlayer::Application {

class ILogger;

class GithubReleaseQuerier: public IReleaseQuerier {
public:
GithubReleaseQuerier(IHttpClient& httpClient);
Expand All @@ -21,8 +23,10 @@ namespace MellowPlayer::Application {
bool accept(const Application::Release* release);

private:
ILogger& logger_;
GithubReleasesReplyParser replyParser_;
IHttpClient& httpClient_;
Application::UpdateChannel channel_ = Application::UpdateChannel::Stable;
bool found_ = false;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ using namespace MellowPlayer::Application;
void GithubReleasesReplyParser::parse(const QByteArray& replyData) {
stopRequested_ = false;
QJsonDocument jsonDocument = QJsonDocument::fromJson(replyData);
assert(jsonDocument.isArray());
if (!jsonDocument.isArray())
return;
QJsonArray array = jsonDocument.array();
for(int releaseIndex=0; releaseIndex < array.count(); ++releaseIndex) {
QJsonObject obj = array.at(releaseIndex).toObject();
Expand Down
4 changes: 4 additions & 0 deletions lib/MellowPlayer/Application/Updater/Release.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Release::Release(const QString& url, const QString& version, const QDate& date,
assets_(assets) {
}

QString Release::getUrl() const {
return url_;
}

QString Release::getName() const {
return name_;
}
Expand Down
1 change: 1 addition & 0 deletions lib/MellowPlayer/Application/Updater/Release.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace MellowPlayer::Application {
Release(const QString& url, const QString& version, const QDate& date, const QString& description,
const AssetList& assets, bool preRelease=false, QObject* parent=nullptr);

QString getUrl() const;
QString getName() const;
QString getDate() const;
const QString& getDescription() const;
Expand Down
15 changes: 11 additions & 4 deletions lib/MellowPlayer/Application/Updater/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ Updater::Updater(IReleaseQuerier& releaseQuerier, Settings& settings):
releaseQuerier_(releaseQuerier),
autoCheckEnabledSetting_(settings.get(SettingKey::MAIN_CHECK_FOR_UPDATES)),
updateChannelSetting_(settings.get(SettingKey::MAIN_UPDATE_CHANNEL)),
latestRelease_(&Release::current()) {
currentRelease_(&Release::current()) {
releaseQuerier.setChannel(getChannel());
connect(&releaseQuerier, &IReleaseQuerier::latestReceived, this, &Updater::onLatestReleaseReceived);
connect(&updateChannelSetting_, &Setting::valueChanged, this, &Updater::check);

// Release* r = new Release("1.95.0", QDate::fromString("2017-06-15"), this);
// setCurrentRelease(r);
Expand Down Expand Up @@ -57,16 +58,22 @@ const Release* Updater::getLatestRelease() const {
void Updater::onLatestReleaseReceived(const Release* release) {
LOG_DEBUG(logger_, "Latest release received: " + release->getName());

if (*release > *latestRelease_) {
if (release != nullptr && *release > *currentRelease_) {
LOG_DEBUG(logger_, QString("Latest release is an update (%1 < %2)").arg(
latestRelease_->getName()).arg(release->getName()));
currentRelease_->getName()).arg(release->getName()));
latestRelease_ = release;
isUpdateAvailable_ = true;
emit updateAvailable();
}
else {
LOG_DEBUG(logger_, QString("No release found"));
latestRelease_ = nullptr;
isUpdateAvailable_ = false;
emit noUpdateAvailable();
}
}

void Updater::setCurrentRelease(const Release* currentRelease) {
latestRelease_ = currentRelease;
currentRelease_ = currentRelease;

}
20 changes: 16 additions & 4 deletions lib/MellowPlayer/Application/Updater/Updater.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ namespace MellowPlayer::Application {

class Updater: public QObject {
Q_OBJECT
// Q_ENUMS()
public:
Updater(IReleaseQuerier& releaseQuerier, Settings& settings);

void setCurrentRelease(const Release* currentRelease);
// enum class Status {
// None,
// Checking,
// Downloading,
// Installing
// };

void check();
void download();
void install();
void setCurrentRelease(const Release* currentRelease);

bool isUpdateAvailable() const;
bool canInstall() const;
const Release* getLatestRelease() const;

public slots:
void check();
void download();
void install();

signals:
void updateAvailable();
void noUpdateAvailable();

private slots:
void onLatestReleaseReceived(const Release* release);
Expand All @@ -41,9 +51,11 @@ namespace MellowPlayer::Application {
Setting& updateChannelSetting_;
bool isUpdateAvailable_ = false;
bool isDownloading_ = false;
const Release* currentRelease_;
const Release* latestRelease_ = nullptr;

MellowPlayer::Application::UpdateChannel getChannel() const;
// Status status_ = Status::None;

};
}
46 changes: 46 additions & 0 deletions lib/MellowPlayer/Presentation/ViewModels/UpdaterViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ using namespace MellowPlayer::Presentation;

UpdaterViewModel::UpdaterViewModel(Updater& updater): updater_(updater) {
connect(&updater, &Updater::updateAvailable, this, &UpdaterViewModel::onUpdateAvailable);
connect(&updater, &Updater::noUpdateAvailable, this, &UpdaterViewModel::onNoUpdateAvailable);
}

QString UpdaterViewModel::getMessage() const {
return message_;
}

bool UpdaterViewModel::isVisible() const {
Expand All @@ -20,15 +25,25 @@ int UpdaterViewModel::getProgress() const {
return progress_;
}

bool UpdaterViewModel::isProgressVisible() const
{
return progressVisible_;
}

void UpdaterViewModel::close() {
setVisible(false);
}

void UpdaterViewModel::check() {
setProgressVisible(true);
setProgress(-1);
updater_.check();
}

void UpdaterViewModel::install() {
setMessage("Downloading update...");
setProgressVisible(true);
setProgress(-1);
updater_.install();
}

Expand Down Expand Up @@ -56,8 +71,39 @@ void UpdaterViewModel::setProgress(int progress) {
emit progressChanged();
}

void UpdaterViewModel::setProgressVisible(bool progressVisible)
{
if (progressVisible_ == progressVisible)
return;

progressVisible_ = progressVisible;
emit progressVisibleChanged();
}

void UpdaterViewModel::onUpdateAvailable() {
setCanInstall(updater_.canInstall());
setProgressVisible(false);
setProgress(-1);
setVisible(true);
setMessage(tr("A new version of <b>MellowPlayer</b> is available (%1)").arg(updater_.getLatestRelease()->getName()));
}

void UpdaterViewModel::onNoUpdateAvailable()
{
setCanInstall(false);
setProgressVisible(false);
setProgress(-1);
setVisible(false);
setMessage("");
}

void UpdaterViewModel::setMessage(const QString& message) {
if (message_ == message)
return;
message_ = message;
emit messageChanged();
}

QString UpdaterViewModel::getUrl() const {
return updater_.getLatestRelease()->getUrl();
}
15 changes: 14 additions & 1 deletion lib/MellowPlayer/Presentation/ViewModels/UpdaterViewModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,48 @@ namespace MellowPlayer::Presentation {

class UpdaterViewModel: public QObject {
Q_OBJECT
Q_PROPERTY(QString message READ getMessage NOTIFY messageChanged)
Q_PROPERTY(QString url READ getUrl CONSTANT)
Q_PROPERTY(bool visible READ isVisible NOTIFY visibleChanged)
Q_PROPERTY(bool canInstall READ canInstall NOTIFY canInstallChanged)
Q_PROPERTY(int progress READ getProgress NOTIFY progressChanged)
Q_PROPERTY(bool progressVisible READ isProgressVisible NOTIFY progressVisibleChanged)
public:
UpdaterViewModel(Application::Updater& updater);

QString getMessage() const;
QString getUrl() const;
bool isVisible() const;
bool canInstall() const;
int getProgress() const;
bool isProgressVisible() const;

Q_INVOKABLE void close();
Q_INVOKABLE void check();
void install();
Q_INVOKABLE void install();

signals:
void messageChanged();
void visibleChanged();
void canInstallChanged();
void progressChanged();
void progressVisibleChanged();

private slots:
void setMessage(const QString& message);
void setVisible(bool visible);
void setCanInstall(bool canInstall);
void setProgress(int progress);
void setProgressVisible(bool progressVisible);
void onUpdateAvailable();
void onNoUpdateAvailable();

private:
Application::Updater& updater_;
QString message_;
bool visible_ = false;
bool canInstall_ = false;
int progress_ = -1;
bool progressVisible_ = true;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,19 @@ ToolBar {

MenuSeparator { }

MenuIconItem {
icon: MaterialIcons.icon_update
text: "Check for updates"
onClicked: _updater.check()

ProgressBar {
anchors{ bottom: parent.bottom; horizontalCenter: parent.left; right: parent.right }
indeterminate: _updater.progress == -1
visible: _updater.progressVisible
}
}


MenuIconItem {
id: menuItemAbout

Expand Down
Loading

0 comments on commit 5e9d6d6

Please sign in to comment.