Commit 74e7a90c authored by Nicolas Fella's avatar Nicolas Fella Committed by Nicolas Fella

Simplify notification handling

Summary:
Use QPointer for KNotification
Use ready signal for signalling updates

BUG: 400010

Test Plan: Spawned some notifications

Reviewers: #kde_connect, broulik, albertvaka

Reviewed By: #kde_connect, albertvaka

Subscribers: albertvaka, kdeconnect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D18354
parent a880e91d
...@@ -95,8 +95,6 @@ void NotificationsModel::setDeviceId(const QString& deviceId) ...@@ -95,8 +95,6 @@ void NotificationsModel::setDeviceId(const QString& deviceId)
this, &NotificationsModel::notificationRemoved); this, &NotificationsModel::notificationRemoved);
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::allNotificationsRemoved, connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::allNotificationsRemoved,
this, &NotificationsModel::clearNotifications); this, &NotificationsModel::clearNotifications);
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::notificationUpdated,
this, &NotificationsModel::notificationUpdated);
refreshNotificationList(); refreshNotificationList();
...@@ -107,6 +105,7 @@ void NotificationsModel::notificationAdded(const QString& id) ...@@ -107,6 +105,7 @@ void NotificationsModel::notificationAdded(const QString& id)
{ {
beginInsertRows(QModelIndex(), 0, 0); beginInsertRows(QModelIndex(), 0, 0);
NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, id, this); NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, id, this);
connect(dbusInterface, &NotificationDbusInterface::ready, this, &NotificationsModel::notificationUpdated);
m_notificationList.prepend(dbusInterface); m_notificationList.prepend(dbusInterface);
endInsertRows(); endInsertRows();
} }
...@@ -266,9 +265,7 @@ void NotificationsModel::clearNotifications() ...@@ -266,9 +265,7 @@ void NotificationsModel::clearNotifications()
} }
} }
void NotificationsModel::notificationUpdated(const QString& id) void NotificationsModel::notificationUpdated()
{ {
//TODO only emit the affected indices
Q_UNUSED(id);
Q_EMIT dataChanged(index(0,0), index(m_notificationList.size() - 1, 0)); Q_EMIT dataChanged(index(0,0), index(m_notificationList.size() - 1, 0));
} }
...@@ -71,7 +71,7 @@ public Q_SLOTS: ...@@ -71,7 +71,7 @@ public Q_SLOTS:
private Q_SLOTS: private Q_SLOTS:
void notificationAdded(const QString& id); void notificationAdded(const QString& id);
void notificationRemoved(const QString& id); void notificationRemoved(const QString& id);
void notificationUpdated(const QString& id); void notificationUpdated();
void refreshNotificationList(); void refreshNotificationList();
void receivedNotifications(QDBusPendingCallWatcher* watcher); void receivedNotifications(QDBusPendingCallWatcher* watcher);
void clearNotifications(); void clearNotifications();
......
...@@ -47,11 +47,10 @@ Notification::Notification(const NetworkPacket& np, QObject* parent) ...@@ -47,11 +47,10 @@ Notification::Notification(const NetworkPacket& np, QObject* parent)
m_imagesDir = QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect_") + username); m_imagesDir = QDir::temp().absoluteFilePath(QStringLiteral("kdeconnect_") + username);
m_imagesDir.mkpath(m_imagesDir.absolutePath()); m_imagesDir.mkpath(m_imagesDir.absolutePath());
QFile(m_imagesDir.absolutePath()).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner); QFile(m_imagesDir.absolutePath()).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
m_closed = false;
m_ready = false; m_ready = false;
parseNetworkPacket(np); parseNetworkPacket(np);
createKNotification(false, np); createKNotification(np);
} }
Notification::~Notification() Notification::~Notification()
...@@ -70,7 +69,6 @@ void Notification::show() ...@@ -70,7 +69,6 @@ void Notification::show()
m_ready = true; m_ready = true;
Q_EMIT ready(); Q_EMIT ready();
if (!m_silent) { if (!m_silent) {
m_closed = false;
m_notification->sendEvent(); m_notification->sendEvent();
} }
} }
...@@ -78,12 +76,12 @@ void Notification::show() ...@@ -78,12 +76,12 @@ void Notification::show()
void Notification::update(const NetworkPacket& np) void Notification::update(const NetworkPacket& np)
{ {
parseNetworkPacket(np); parseNetworkPacket(np);
createKNotification(!m_closed, np); createKNotification(np);
} }
KNotification* Notification::createKNotification(bool update, const NetworkPacket& np) KNotification* Notification::createKNotification(const NetworkPacket& np)
{ {
if (!update) { if (!m_notification) {
m_notification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this); m_notification = new KNotification(QStringLiteral("notification"), KNotification::CloseOnTimeout, this);
m_notification->setComponentName(QStringLiteral("kdeconnect")); m_notification->setComponentName(QStringLiteral("kdeconnect"));
} }
...@@ -121,8 +119,6 @@ KNotification* Notification::createKNotification(bool update, const NetworkPacke ...@@ -121,8 +119,6 @@ KNotification* Notification::createKNotification(bool update, const NetworkPacke
connect(m_notification, &KNotification::action1Activated, this, &Notification::reply); connect(m_notification, &KNotification::action1Activated, this, &Notification::reply);
} }
connect(m_notification, &KNotification::closed, this, &Notification::closed);
return m_notification; return m_notification;
} }
...@@ -171,11 +167,6 @@ void Notification::reply() ...@@ -171,11 +167,6 @@ void Notification::reply()
Q_EMIT replyRequested(); Q_EMIT replyRequested();
} }
void Notification::closed()
{
m_closed = true;
}
void Notification::parseNetworkPacket(const NetworkPacket& np) void Notification::parseNetworkPacket(const NetworkPacket& np)
{ {
m_internalId = np.get<QString>(QStringLiteral("id")); m_internalId = np.get<QString>(QStringLiteral("id"));
......
...@@ -62,17 +62,16 @@ public: ...@@ -62,17 +62,16 @@ public:
bool silent() const { return m_silent; } bool silent() const { return m_silent; }
void update(const NetworkPacket& np); void update(const NetworkPacket& np);
bool isReady() const { return m_ready; } bool isReady() const { return m_ready; }
KNotification* createKNotification(bool update, const NetworkPacket& np); KNotification* createKNotification(const NetworkPacket& np);
public Q_SLOTS: public Q_SLOTS:
Q_SCRIPTABLE void dismiss(); Q_SCRIPTABLE void dismiss();
Q_SCRIPTABLE void reply(); Q_SCRIPTABLE void reply();
void closed();
Q_SIGNALS: Q_SIGNALS:
void dismissRequested(const QString& m_internalId); void dismissRequested(const QString& m_internalId);
void replyRequested(); void replyRequested();
void ready(); Q_SCRIPTABLE void ready();
private: private:
QString m_internalId; QString m_internalId;
...@@ -84,10 +83,9 @@ private: ...@@ -84,10 +83,9 @@ private:
QString m_requestReplyId; QString m_requestReplyId;
bool m_dismissable; bool m_dismissable;
bool m_hasIcon; bool m_hasIcon;
KNotification* m_notification; QPointer<KNotification> m_notification;
QDir m_imagesDir; QDir m_imagesDir;
bool m_silent; bool m_silent;
bool m_closed;
QString m_payloadHash; QString m_payloadHash;
bool m_ready; bool m_ready;
......
...@@ -74,35 +74,26 @@ void NotificationsDbusInterface::processPacket(const NetworkPacket& np) ...@@ -74,35 +74,26 @@ void NotificationsDbusInterface::processPacket(const NetworkPacket& np)
if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::"))) if (id.startsWith(QLatin1String("org.kde.kdeconnect_tp::")))
id = id.mid(id.indexOf(QLatin1String("::")) + 2); id = id.mid(id.indexOf(QLatin1String("::")) + 2);
removeNotification(id); removeNotification(id);
} else { return;
QString id = np.get<QString>(QStringLiteral("id")); }
if (!m_internalIdToPublicId.contains(id)) { QString id = np.get<QString>(QStringLiteral("id"));
Notification* noti = new Notification(np, this);
if (noti->isReady()) { Notification* noti = nullptr;
addNotification(noti);
} else { if (!m_internalIdToPublicId.contains(id)) {
connect(noti, &Notification::ready, this, &NotificationsDbusInterface::notificationReady); noti = new Notification(np, this);
}
if (noti->isReady()) {
addNotification(noti);
} else { } else {
QString pubId = m_internalIdToPublicId.value(id); connect(noti, &Notification::ready, this, &NotificationsDbusInterface::notificationReady);
Notification* noti = m_notifications.value(pubId);
if (!noti)
return;
noti->update(np);
if (noti->isReady()) {
Q_EMIT notificationUpdated(pubId);
} else {
connect(noti, &Notification::ready, this, [this, pubId]{
Q_EMIT notificationUpdated(pubId);
});
}
} }
} else {
QString pubId = m_internalIdToPublicId.value(id);
noti = m_notifications.value(pubId);
} }
noti->update(np);
} }
void NotificationsDbusInterface::addNotification(Notification* noti) void NotificationsDbusInterface::addNotification(Notification* noti)
......
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