NOISSUE fix notification checker

This commit is contained in:
Petr Mrázek 2015-02-07 12:55:18 +01:00
parent 4e94de413b
commit 7a71ecd8af
3 changed files with 52 additions and 30 deletions

View File

@ -619,10 +619,16 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
auto updater = MMC->updateChecker(); auto updater = MMC->updateChecker();
updater->checkForUpdate(MMC->settings()->get("UpdateChannel").toString(), false); updater->checkForUpdate(MMC->settings()->get("UpdateChannel").toString(), false);
} }
m_notificationChecker.reset(new NotificationChecker()); auto checker = new NotificationChecker();
checker->setNotificationsUrl(QUrl(BuildConfig.NOTIFICATION_URL));
checker->setApplicationChannel(BuildConfig.VERSION_CHANNEL);
checker->setApplicationPlatform(BuildConfig.BUILD_PLATFORM);
checker->setApplicationFullVersion(BuildConfig.FULL_VERSION_STR);
m_notificationChecker.reset(checker);
connect(m_notificationChecker.get(), connect(m_notificationChecker.get(),
&NotificationChecker::notificationCheckFinished, this, &NotificationChecker::notificationCheckFinished, this,
&MainWindow::notificationsChanged); &MainWindow::notificationsChanged);
checker->checkForNotifications();
} }
setSelectedInstanceById(MMC->settings()->get("SelectedInstance").toString()); setSelectedInstanceById(MMC->settings()->get("SelectedInstance").toString());
@ -959,7 +965,7 @@ void MainWindow::notificationsChanged()
for (auto it = entries.begin(); it != entries.end(); ++it) for (auto it = entries.begin(); it != entries.end(); ++it)
{ {
NotificationChecker::NotificationEntry entry = *it; NotificationChecker::NotificationEntry entry = *it;
if (!shownNotifications.contains(entry.id) && entry.applies()) if (!shownNotifications.contains(entry.id))
{ {
NotificationDialog dialog(entry, this); NotificationDialog dialog(entry, this);
if (dialog.exec() == NotificationDialog::DontShowAgain) if (dialog.exec() == NotificationDialog::DontShowAgain)

View File

@ -3,28 +3,37 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonArray> #include <QJsonArray>
#include "logic/Env.h"
#include "BuildConfig.h"
#include "logic/net/CacheDownload.h"
#include <QDebug> #include <QDebug>
#include "logic/Env.h"
#include "logic/net/CacheDownload.h"
NotificationChecker::NotificationChecker(QObject *parent) NotificationChecker::NotificationChecker(QObject *parent)
: QObject(parent), m_notificationsUrl(QUrl(BuildConfig.NOTIFICATION_URL)) : QObject(parent)
{ {
// this will call checkForNotifications once the event loop is running
QMetaObject::invokeMethod(this, "checkForNotifications", Qt::QueuedConnection);
} }
QUrl NotificationChecker::notificationsUrl() const
{
return m_notificationsUrl;
}
void NotificationChecker::setNotificationsUrl(const QUrl &notificationsUrl) void NotificationChecker::setNotificationsUrl(const QUrl &notificationsUrl)
{ {
m_notificationsUrl = notificationsUrl; m_notificationsUrl = notificationsUrl;
} }
void NotificationChecker::setApplicationChannel(QString channel)
{
m_appVersionChannel = channel;
}
void NotificationChecker::setApplicationFullVersion(QString version)
{
m_appFullVersion = version;
}
void NotificationChecker::setApplicationPlatform(QString platform)
{
m_appPlatform = platform;
}
QList<NotificationChecker::NotificationEntry> NotificationChecker::notificationEntries() const QList<NotificationChecker::NotificationEntry> NotificationChecker::notificationEntries() const
{ {
return m_entries; return m_entries;
@ -83,6 +92,7 @@ void NotificationChecker::downloadSucceeded(int)
{ {
entry.type = NotificationEntry::Information; entry.type = NotificationEntry::Information;
} }
if(entryApplies(entry))
m_entries.append(entry); m_entries.append(entry);
} }
} }
@ -92,19 +102,7 @@ void NotificationChecker::downloadSucceeded(int)
emit notificationCheckFinished(); emit notificationCheckFinished();
} }
bool NotificationChecker::NotificationEntry::applies() const bool versionLessThan(const QString &v1, const QString &v2)
{
bool channelApplies = channel.isEmpty() || channel == BuildConfig.VERSION_CHANNEL;
bool platformApplies = platform.isEmpty() || platform == BuildConfig.BUILD_PLATFORM;
bool fromApplies =
from.isEmpty() || from == BuildConfig.FULL_VERSION_STR || !versionLessThan(BuildConfig.FULL_VERSION_STR, from);
bool toApplies =
to.isEmpty() || to == BuildConfig.FULL_VERSION_STR || !versionLessThan(to, BuildConfig.FULL_VERSION_STR);
return channelApplies && platformApplies && fromApplies && toApplies;
}
bool NotificationChecker::NotificationEntry::versionLessThan(const QString &v1,
const QString &v2)
{ {
QStringList l1 = v1.split('.'); QStringList l1 = v1.split('.');
QStringList l2 = v2.split('.'); QStringList l2 = v2.split('.');
@ -119,3 +117,14 @@ bool NotificationChecker::NotificationEntry::versionLessThan(const QString &v1,
} }
return false; return false;
} }
bool NotificationChecker::entryApplies(const NotificationChecker::NotificationEntry& entry) const
{
bool channelApplies = entry.channel.isEmpty() || entry.channel == m_appVersionChannel;
bool platformApplies = entry.platform.isEmpty() || entry.platform == m_appPlatform;
bool fromApplies =
entry.from.isEmpty() || entry.from == m_appFullVersion || !versionLessThan(m_appFullVersion, entry.from);
bool toApplies =
entry.to.isEmpty() || entry.to == m_appFullVersion || !versionLessThan(entry.to, m_appFullVersion);
return channelApplies && platformApplies && fromApplies && toApplies;
}

View File

@ -12,8 +12,10 @@ class NotificationChecker : public QObject
public: public:
explicit NotificationChecker(QObject *parent = 0); explicit NotificationChecker(QObject *parent = 0);
QUrl notificationsUrl() const;
void setNotificationsUrl(const QUrl &notificationsUrl); void setNotificationsUrl(const QUrl &notificationsUrl);
void setApplicationPlatform(QString platform);
void setApplicationChannel(QString channel);
void setApplicationFullVersion(QString version);
struct NotificationEntry struct NotificationEntry
{ {
@ -29,8 +31,6 @@ public:
QString platform; QString platform;
QString from; QString from;
QString to; QString to;
bool applies() const;
static bool versionLessThan(const QString &v1, const QString &v2);
}; };
QList<NotificationEntry> notificationEntries() const; QList<NotificationEntry> notificationEntries() const;
@ -46,9 +46,16 @@ slots:
signals: signals:
void notificationCheckFinished(); void notificationCheckFinished();
private:
bool entryApplies(const NotificationEntry &entry) const;
private: private:
QList<NotificationEntry> m_entries; QList<NotificationEntry> m_entries;
QUrl m_notificationsUrl; QUrl m_notificationsUrl;
NetJobPtr m_checkJob; NetJobPtr m_checkJob;
CacheDownloadPtr m_download; CacheDownloadPtr m_download;
QString m_appVersionChannel;
QString m_appPlatform;
QString m_appFullVersion;
}; };