Improve queuing

This commit is contained in:
Hannah von Reth 2017-07-31 16:47:46 +02:00
parent cce9f8c76a
commit 45f837654c
9 changed files with 42 additions and 56 deletions

View File

@ -99,6 +99,16 @@ bool SnoreBackend::canUpdateNotification() const
return false;
}
int SnoreBackend::maxNumberOfActiveNotifications() const
{
return 3;
}
SnorePlugin::PluginTypes SnoreBackend::type() const
{
return Backend;
}
void SnoreBackend::slotRegisterApplication(const Application &application)
{
Q_UNUSED(application);

View File

@ -38,11 +38,12 @@ public:
virtual bool canCloseNotification() const;
virtual bool canUpdateNotification() const;
virtual int maxNumberOfActiveNotifications() const;
PluginTypes type() const override;
PluginTypes type() const override
{
return Backend;
}
Q_SIGNALS:
void notificationClosed(Snore::Notification);

View File

@ -116,7 +116,7 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
void SnoreCore::broadcastNotification(Notification notification)
{
Q_D(SnoreCore);
if (d->m_activeNotifications.size() > d->maxNumberOfActiveNotifications()) {
if (d->m_activeNotifications.size() >= d->maxNumberOfActiveNotifications()) {
qCDebug(SNORE) << "queue size:" << d->m_notificationQue.size() << "active size:" << d->m_activeNotifications.size();
d->m_notificationQue.append(notification);
return;

View File

@ -219,6 +219,11 @@ QString SnoreCorePrivate::tempPath()
#endif
}
int SnoreCorePrivate::maxNumberOfActiveNotifications() const
{
return m_notificationBackend ? m_notificationBackend->maxNumberOfActiveNotifications() : 3;
}
// TODO: this is somehow horrible code
SnoreCorePrivate *SnoreCorePrivate::instance()
{

View File

@ -44,10 +44,6 @@ public:
*/
static QString tempPath();
static Q_DECL_CONSTEXPR int maxNumberOfActiveNotifications()
{
return 3;
}
public:
static SnoreCorePrivate *instance();
@ -62,7 +58,7 @@ public:
*/
bool primaryBackendCanUpdateNotification() const;
QString normalizeSettingsKey(const QString &key, SettingsType type) const
inline QString normalizeSettingsKey(const QString &key, SettingsType type) const
{
return Snore::Utils::normalizeSettingsKey(key, type, m_localSettingsPrefix);
}
@ -82,6 +78,8 @@ public:
QSettings &settings();
int maxNumberOfActiveNotifications() const;
private Q_SLOTS:
//TODO: find a better solutinon for the slots in this section
friend class Snore::SnoreBackend;

View File

@ -23,11 +23,8 @@
#include <QThread>
SnorePlugin::Snore::Snore():
m_timer(new QTimer(this))
SnorePlugin::Snore::Snore()
{
m_timer->setInterval(1000);
connect(m_timer, &QTimer::timeout, this, &Snore::slotQueueTimeout);
}
@ -51,18 +48,9 @@ void SnorePlugin::Snore::slotNotify(::Snore::Notification notification)
qCDebug(SNORE) << "replacing notification" << w->notification().id() << notification.id();
display(w, notification);
}
} else {
for (int i = 0; i < m_queue.length(); ++i) {
::Snore::Notification n = m_queue.at(i);
if (n.id() == notification.old().id()) {
qCDebug(SNORE) << "replacing qued notification" << n.id() << notification.id();
m_queue.replace(i, notification);
}
}
}
return;
}
if (m_queue.isEmpty()) {
foreach(NotifyWidget * w, m_widgets) {
if (w->acquire(notification.timeout())) {
display(w, notification);
@ -70,18 +58,11 @@ void SnorePlugin::Snore::slotNotify(::Snore::Notification notification)
}
}
}
m_queue.append(notification);
qCDebug(SNORE) << "queueing" << m_queue.size();
if (!m_timer->isActive()) {
m_timer->start();
}
}
void SnorePlugin::Snore::slotCloseNotification(::Snore::Notification notification)
{
NotifyWidget *w = m_widgets[notification.hints().privateValue(this, "id").toInt()];
w->release();
slotQueueTimeout();
}
void SnorePlugin::Snore::slotRegisterApplication(const ::Snore::Application &)
@ -109,23 +90,6 @@ void SnorePlugin::Snore::slotRegisterApplication(const ::Snore::Application &)
}
void SnorePlugin::Snore::slotQueueTimeout()
{
if (m_queue.isEmpty()) {
qCDebug(SNORE) << "queue is empty";
m_timer->stop();
} else {
foreach(NotifyWidget * w, m_widgets) {
if (!m_queue.isEmpty() && w->acquire(m_queue.first().timeout())) {
::Snore::Notification notification = m_queue.takeFirst();
notification.hints().setPrivateValue(this, "id", w->id());
w->display(notification);
slotNotificationDisplayed(notification);
}
}
}
}
bool SnorePlugin::Snore::canCloseNotification() const
{
return true;
@ -136,6 +100,11 @@ bool SnorePlugin::Snore::canUpdateNotification() const
return true;
}
int SnorePlugin::Snore::maxNumberOfActiveNotifications() const
{
return m_widgets.size();
}
void SnorePlugin::Snore::setDefaultSettings()
{
setDefaultSettingsValue(QStringLiteral("Position"), Qt::TopRightCorner);

View File

@ -36,6 +36,7 @@ public:
bool canCloseNotification() const override;
bool canUpdateNotification() const override;
int maxNumberOfActiveNotifications() const override;
protected:
void setDefaultSettings() override;
@ -44,13 +45,8 @@ public Q_SLOTS:
virtual void slotCloseNotification(::Snore::Notification notification) override;
void slotRegisterApplication(const ::Snore::Application &application);
private Q_SLOTS:
void slotQueueTimeout();
private:
QList<::Snore::Notification> m_queue;
QVector<NotifyWidget *> m_widgets;
QTimer *m_timer;
};
}

View File

@ -26,6 +26,11 @@ bool SnorePlugin::WindowsToast::canCloseNotification() const
return true;
}
int SnorePlugin::WindowsToast::maxNumberOfActiveNotifications() const
{
return 1;
}
void SnorePlugin::WindowsToast::slotNotify(Snore::Notification notification)
{
QProcess *p = createProcess(notification);
@ -74,6 +79,7 @@ void SnorePlugin::WindowsToast::slotNotify(Snore::Notification notification)
closeNotification(notification, reason);
});
p->start(QLatin1String("SnoreToast"), arguements);
slotNotificationDisplayed(notification);
}
void SnorePlugin::WindowsToast::slotRegisterApplication(const Snore::Application &application)

View File

@ -16,6 +16,7 @@ public:
~WindowsToast() = default;
virtual bool canCloseNotification() const override;
int maxNumberOfActiveNotifications() const override;
bool isReady() override;
public Q_SLOTS: