Commit 40613c2b authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Simplify notifier logic

There was some redundant API, not anymore.
Also cache the number of updates, no need to query all the time, it's also
useful to figure out if there were changes
parent a2be6848
......@@ -35,11 +35,6 @@ void DummyNotifier::recheckSystemUpdateNeeded()
emit foundUpdates();
}
bool DummyNotifier::isSystemUpToDate() const
{
return true;
}
uint DummyNotifier::securityUpdatesCount()
{
return 0;
......
......@@ -31,7 +31,6 @@ public:
explicit DummyNotifier(QObject* parent = nullptr);
~DummyNotifier() override;
bool isSystemUpToDate() const override;
void recheckSystemUpdateNeeded() override;
uint securityUpdatesCount() override;
uint updatesCount() override;
......
......@@ -71,11 +71,6 @@ void FlatpakNotifier::recheckSystemUpdateNeeded()
checkUpdates();
}
bool FlatpakNotifier::isSystemUpToDate() const
{
return !m_systemInstallationUpdates && !m_userInstallationUpdates;
}
uint FlatpakNotifier::securityUpdatesCount()
{
return 0;
......
......@@ -36,7 +36,6 @@ public:
explicit FlatpakNotifier(QObject* parent = nullptr);
~FlatpakNotifier() override;
bool isSystemUpToDate() const override;
void recheckSystemUpdateNeeded() override;
uint securityUpdatesCount() override;
uint updatesCount() override;
......
......@@ -179,11 +179,6 @@ void PackageKitNotifier::finished(PackageKit::Transaction::Exit /*exit*/, uint)
}
}
bool PackageKitNotifier::isSystemUpToDate() const
{
return m_securityUpdates == 0 && m_normalUpdates == 0;
}
uint PackageKitNotifier::securityUpdatesCount()
{
return m_securityUpdates;
......
......@@ -38,7 +38,6 @@ public:
explicit PackageKitNotifier(QObject* parent = nullptr);
~PackageKitNotifier() override;
bool isSystemUpToDate() const override;
uint securityUpdatesCount() override;
uint updatesCount() override;
void recheckSystemUpdateNeeded() override;
......
......@@ -31,9 +31,6 @@ public:
explicit BackendNotifierModule(QObject* parent = nullptr);
~BackendNotifierModule() override;
/*** @returns \c true if there are any updates, \c false if not **/
virtual bool isSystemUpToDate() const = 0;
/*** Check for new updates. Emits @see foundUpdates when it finds something. **/
virtual void recheckSystemUpdateNeeded() = 0;
......
......@@ -22,6 +22,7 @@
#include "BackendNotifierFactory.h"
#include <KConfig>
#include <KConfigGroup>
#include <QDebug>
#include <KRun>
#include <KLocalizedString>
#include <KNotification>
......@@ -58,15 +59,6 @@ void DiscoverNotifier::showMuon()
KRun::runCommand(QStringLiteral("plasma-discover --mode update"), nullptr);
}
bool DiscoverNotifier::isSystemUpToDate() const
{
for(BackendNotifierModule* module : m_backends) {
if(!module->isSystemUpToDate())
return false;
}
return true;
}
void DiscoverNotifier::showUpdatesNotification()
{
if (state()==NoUpdates) {
......@@ -85,7 +77,21 @@ void DiscoverNotifier::showUpdatesNotification()
void DiscoverNotifier::updateStatusNotifier()
{
if (!isSystemUpToDate()) {
uint securityCount = 0;
for (BackendNotifierModule* module: m_backends)
securityCount += module->securityUpdatesCount();
uint count = securityUpdatesCount();
foreach(BackendNotifierModule* module, m_backends)
count += module->updatesCount();
if (m_count == count && m_securityCount == securityCount)
return;
m_securityCount = securityCount;
m_count = count;
if (state() != NoUpdates) {
m_timer.start();
}
emit updatesChanged();
......@@ -93,17 +99,9 @@ void DiscoverNotifier::updateStatusNotifier()
DiscoverNotifier::State DiscoverNotifier::state() const
{
bool security = false, normal = false;
for(BackendNotifierModule* module : m_backends) {
security |= module->securityUpdatesCount()>0;
normal |= security || module->updatesCount()>0;
if (security)
break;
}
if (security)
if (m_securityCount)
return SecurityUpdates;
else if (normal)
else if (m_count)
return NormalUpdates;
else
return NoUpdates;
......@@ -137,21 +135,19 @@ QString DiscoverNotifier::message() const
QString DiscoverNotifier::extendedMessage() const
{
uint securityCount = securityUpdatesCount();
uint count = updatesCount();
if (count > 0 && securityCount > 0) {
if (m_count > 0 && m_securityCount > 0) {
QString allUpdates = i18ncp("First part of '%1, %2'",
"1 package to update", "%1 packages to update", count);
"1 package to update", "%1 packages to update", m_count);
QString securityUpdates = i18ncp("Second part of '%1, %2'",
"of which 1 is security update", "of which %1 are security updates", securityCount);
"of which 1 is security update", "of which %1 are security updates", m_securityCount);
return i18nc("%1 is '%1 packages to update' and %2 is 'of which %1 is security updates'",
"%1, %2", allUpdates, securityUpdates);
} else if (count > 0) {
return i18np("1 package to update", "%1 packages to update", count);
} else if (securityCount > 0) {
return i18np("1 security update", "%1 security updates", securityCount);
} else if (m_count > 0) {
return i18np("1 package to update", "%1 packages to update", m_count);
} else if (m_securityCount > 0) {
return i18np("1 security update", "%1 security updates", m_securityCount);
} else {
return i18n("No packages to update");
}
......@@ -165,18 +161,12 @@ void DiscoverNotifier::recheckSystemUpdateNeeded()
uint DiscoverNotifier::securityUpdatesCount() const
{
uint ret = 0;
foreach(BackendNotifierModule* module, m_backends)
ret += module->securityUpdatesCount();
return ret;
return m_securityCount;
}
uint DiscoverNotifier::updatesCount() const
{
uint ret = 0;
foreach(BackendNotifierModule* module, m_backends)
ret += module->updatesCount();
return ret + securityUpdatesCount();
return m_count;
}
QStringList DiscoverNotifier::loadedModules() const
......
......@@ -47,7 +47,7 @@ public:
explicit DiscoverNotifier(QObject* parent = nullptr);
~DiscoverNotifier() override;
bool isSystemUpToDate() const;
bool isSystemUpToDate() const { return state() == NoUpdates; }
State state() const;
QString iconName() const;
......@@ -76,6 +76,8 @@ private:
QList<BackendNotifierModule*> m_backends;
bool m_verbose;
QTimer m_timer;
uint m_securityCount = 0;
uint m_count = 0;
};
#endif //ABSTRACTKDEDMODULE_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment