cleanup api and added a signall for notificationDisplayed

This commit is contained in:
Patrick von Reth 2015-04-26 15:25:29 +02:00
parent 7ba599d7b8
commit 4b0b6e128b
15 changed files with 77 additions and 81 deletions

View File

@ -266,6 +266,8 @@ public:
*/
static int defaultTimeout();
//TODO: find a better name.
void addActiveIn(const QObject *o);
bool isActiveIn(const QObject *o) const;
bool removeActiveIn(const QObject* o);

View File

@ -73,11 +73,6 @@ void NotificationData::setActionInvoked(const Snore::Action &action)
m_actionInvoked = action;
}
void NotificationData::setActionInvoked(const int &id)
{
m_actionInvoked = m_actions[id];
}
void NotificationData::setCloseReason(Snore::Notification::CloseReasons r)
{
m_closeReason = r;

View File

@ -43,7 +43,6 @@ public:
~NotificationData();
void setActionInvoked(const Action &action);
void setActionInvoked(const int &actionID);
void setCloseReason(Notification::CloseReasons r);

View File

@ -75,9 +75,7 @@ void SnoreBackend::closeNotification(Notification n, Notification::CloseReasons
if (!n.isValid()) {
return;
}
if (n.isActiveIn(this)) {
n.removeActiveIn(this);
}
n.removeActiveIn(this);
if (n.isUpdate() && n.old().isActiveIn(this)) {
n.old().removeActiveIn(this);
}
@ -172,13 +170,11 @@ bool SnoreBackend::deinitialize()
return false;
}
void SnoreBackend::startTimeout(Notification &notification)
void SnoreBackend::slotNotificationDisplayed(Notification notification)
{
if (thread() != QThread::currentThread()) {
snoreDebug(SNORE_WARNING) << "Plugin timeout in wrong thread.";
metaObject()->invokeMethod(this, "startTimeout", Qt::QueuedConnection, Q_ARG(Notification, notification));
return;
}
notification.addActiveIn(this);
SnoreCorePrivate::instance()->slotNotificationDisplayed(notification);
if (notification.isSticky()) {
return;
}
@ -203,3 +199,9 @@ void SnoreBackend::startTimeout(Notification &notification)
timer->start();
}
void SnoreBackend::slotNotificationActionInvoked(Notification notification, const Action &action)
{
notification.data()->setActionInvoked(action);
SnoreCorePrivate::instance()->slotNotificationActionInvoked(notification);
}

View File

@ -52,7 +52,8 @@ public slots:
virtual void slotCloseNotification(Snore::Notification notification);
protected slots:
void startTimeout(Notification &notification);
void slotNotificationDisplayed(Notification notification);
void slotNotificationActionInvoked(Notification notification, const Action& action = Action());
protected:
void closeNotification(Snore::Notification, Snore::Notification::CloseReasons);

View File

@ -120,7 +120,6 @@ void SnoreCore::broadcastNotification(Notification notification)
if (notification.isUpdate() && !d->m_notificationBackend->canUpdateNotification()) {
requestCloseNotification(notification.old(), Notification::REPLACED);
}
notification.addActiveIn(d->m_notificationBackend);
}
emit d->notify(notification);
}

View File

@ -59,12 +59,17 @@ Application SnoreCorePrivate::defaultApplication()
return m_defaultApp;
}
void SnoreCorePrivate::notificationActionInvoked(Notification notification)
void SnoreCorePrivate::slotNotificationActionInvoked(Notification notification)
{
Q_Q(SnoreCore);
emit q->actionInvoked(notification);
}
void SnoreCorePrivate::slotNotificationDisplayed(Notification notification)
{
emit notificationDisplayed(notification);
}
bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend)
{
Q_Q(SnoreCore);

View File

@ -49,8 +49,6 @@ public:
~SnoreCorePrivate();
Application defaultApplication();
void notificationActionInvoked(Notification notification);
bool setBackendIfAvailible(const QString &backend);
/**
@ -84,10 +82,18 @@ public:
void init();
private slots:
//TODO: find a better solutinon for the slots in this section
friend class Snore::SnoreBackend;
void slotNotificationActionInvoked(Notification notification);
void slotNotificationDisplayed(Notification notification);
signals:
void applicationRegistered(const Snore::Application &);
void applicationDeregistered(const Snore::Application &);
void notify(Snore::Notification noti);
void notificationDisplayed(Snore::Notification notification);
private slots:
void slotNotificationClosed(Snore::Notification);
@ -112,6 +118,7 @@ private:
QHash<uint,Snore::Notification> m_activeNotifications;
friend class Snore::Notification;
};
}

View File

@ -57,7 +57,7 @@ bool GrowlBackend::initialize()
r = Notification::DISMISSED;
} else if (reason == "CLICK") {
r = Notification::ACTIVATED;
SnoreCorePrivate::instance()->notificationActionInvoked(n);
s_instance->slotNotificationActionInvoked(n);
}
s_instance->closeNotification(n, r);
};
@ -119,7 +119,7 @@ void GrowlBackend::slotNotify(Notification notification)
data.setCallbackData("1");
growl->Notify(data);
startTimeout(notification);
slotNotificationDisplayed(notification);
}
PluginSettingsWidget *GrowlBackend::settingsWidget()

View File

@ -39,6 +39,7 @@ void OSXNotificationCenter::slotNotify(Snore::Notification notification)
NSUserNotification *osx_notification = [[NSUserNotification alloc] init];
osx_notification.title = NSStringFromQString(Utils::toPlainText(notification.title()));
osx_notification.informativeText = NSStringFromQString(Utils::toPlainText(notification.text()));
slotNotificationDisplayed(notification);
[notification_center deliverNotification: osx_notification];

View File

@ -73,8 +73,7 @@ public:
case SnarlEnums::NotifyAction:
reason = Notification::ACTIVATED;
if (notification.isValid()) {
notification.data()->setActionInvoked(data);
SnoreCorePrivate::instance()->notificationActionInvoked(notification);
m_snarl->slotNotificationActionInvoked(notification, notification.actions().value(data));
}
break;
case SnarlEnums::CallbackClosed:
@ -244,7 +243,7 @@ void SnarlBackend::slotNotify(Notification notification)
}
notification.hints().setPrivateValue(this, "id", id);
startTimeout(notification);//if dnd or away snarl does not timeout atomatically
slotNotificationDisplayed(notification);//if dnd or away snarl does not timeout atomatically
}

View File

@ -33,7 +33,25 @@ SnoreNotifier::SnoreNotifier():
{
setDefaultValue("Position", Qt::TopRightCorner);
m_timer->setInterval(500);
connect(m_timer, &QTimer::timeout, this, &SnoreNotifier::slotProcessQueue);
connect(m_timer, &QTimer::timeout, [this](){
if (m_queue.isEmpty()) {
snoreDebug(SNORE_DEBUG) << "queue is empty";
m_timer->stop();
} else {
foreach(NotifyWidget * w, m_widgets) {
if (w->acquire()) {
Notification notification = m_queue.takeFirst();
w->display(notification);
notification.hints().setPrivateValue(this, "id", w->id());
slotNotificationDisplayed(notification);
if (m_queue.isEmpty()) {
m_timer->stop();
return;
}
}
}
}
});
}
@ -46,15 +64,18 @@ SnoreNotifier::~SnoreNotifier()
void SnoreNotifier::slotNotify(Snore::Notification notification)
{
auto display = [this](NotifyWidget *w, Snore::Notification notification){
w->display(notification);
notification.hints().setPrivateValue(this, "id", w->id());
slotNotificationDisplayed(notification);
};
if (notification.isUpdate()) {
if (notification.old().hints().privateValue(this, "id").isValid()) {
NotifyWidget *w = m_widgets[notification.old().hints().privateValue(this, "id").toInt()];
if (w->isVisible() && w->notification().isValid() && w->notification().id() == notification.old().id()) {
snoreDebug(SNORE_DEBUG) << "replacing notification" << w->notification().id() << notification.id();
w->display(notification);
notification.hints().setPrivateValue(this, "id", w->id());
startTimeout(notification);
display(w, notification);
}
} else {
for (int i = 0; i < m_queue.length(); ++i) {
@ -69,10 +90,8 @@ void SnoreNotifier::slotNotify(Snore::Notification notification)
if (m_queue.isEmpty()) {
foreach(NotifyWidget * w, m_widgets) {
if (w->acquire()) {
w->display(notification);
notification.hints().setPrivateValue(this, "id", w->id());
startTimeout(notification);
return;
display(w, notification);
return;
}
}
}
@ -88,52 +107,24 @@ void SnoreNotifier::slotCloseNotification(Snore::Notification notification)
//the timer will show the next
}
void SnoreNotifier::slotDismissed()
{
NotifyWidget *widget = qobject_cast<NotifyWidget *>(sender());
Notification notification = widget->notification();
closeNotification(notification, Notification::DISMISSED);
slotCloseNotification(notification);
}
void SnoreNotifier::slotInvoked()
{
NotifyWidget *widget = qobject_cast<NotifyWidget *>(sender());
Notification notification = widget->notification();
SnoreCorePrivate::instance()->notificationActionInvoked(notification);
closeNotification(notification, Notification::ACTIVATED);
slotCloseNotification(notification);
}
void SnoreNotifier::slotProcessQueue()
{
if (m_queue.isEmpty()) {
snoreDebug(SNORE_DEBUG) << "queue is empty";
m_timer->stop();
} else {
foreach(NotifyWidget * w, m_widgets) {
if (w->acquire()) {
Notification notification = m_queue.takeFirst();
w->display(notification);
notification.hints().setPrivateValue(this, "id", w->id());
startTimeout(notification);
if (m_queue.isEmpty()) {
m_timer->stop();
return;
}
}
}
}
}
bool SnoreNotifier::initialize()
{
if (SnoreBackend::initialize()) {
for (int i = 0; i < m_widgets.size(); ++i) {
NotifyWidget *w = new NotifyWidget(i, this);
m_widgets[i] = w;
connect(w, &NotifyWidget::dismissed, this, &SnoreNotifier::slotDismissed);
connect(w, &NotifyWidget::invoked, this, &SnoreNotifier::slotInvoked);
connect(w, &NotifyWidget::dismissed,[this, w](){
Notification notification = w->notification();
closeNotification(notification, Notification::DISMISSED);
slotCloseNotification(notification);
});
connect(w, &NotifyWidget::invoked, [this, w](){
Notification notification = w->notification();
slotNotificationActionInvoked(notification);
closeNotification(notification, Notification::ACTIVATED);
slotCloseNotification(notification);
});
}
return true;
}

View File

@ -41,11 +41,6 @@ public slots:
virtual void slotNotify(Snore::Notification notification) override;
virtual void slotCloseNotification(Snore::Notification notification) override;
private slots:
void slotDismissed();
void slotInvoked();
void slotProcessQueue();
private:
QList<Snore::Notification> m_queue;

View File

@ -111,7 +111,7 @@ void SnoreToast::slotToastNotificationClosed(int code, QProcess::ExitStatus)
switch (code) {
case 0:
reason = Notification::ACTIVATED;
SnoreCorePrivate::instance()->notificationActionInvoked(n);
slotNotificationActionInvoked(n);
break;
case 1:
//hidden;

View File

@ -84,7 +84,7 @@ void TrayIconNotifer::displayNotification(QSystemTrayIcon *icon)
Notification notification = m_notificationQue.takeFirst();
m_displayed = notification;
icon->showMessage(Utils::toPlainText(notification.title()), Utils::toPlainText(notification.text()), QSystemTrayIcon::NoIcon, notification.timeout() * 1000);
startTimeout(notification);
slotNotificationDisplayed(notification);
}
void TrayIconNotifer::actionInvoked()
@ -92,7 +92,7 @@ void TrayIconNotifer::actionInvoked()
Notification n = m_displayed;
QSystemTrayIcon *icon = trayIcon(n.application());
if (icon && n.isValid()) {
SnoreCorePrivate::instance()->notificationActionInvoked(n);
slotNotificationActionInvoked(n);
closeNotification(n, Notification::ACTIVATED);
m_currentlyDisplaying = false;
displayNotification(icon);