Make the NotificationsModel also async

The code was almost identical to the one in DevicesModel, so I did the same
This commit is contained in:
Aleix Pol 2015-03-14 05:37:05 +01:00
parent 02a2990720
commit 28bc226e5d
2 changed files with 30 additions and 12 deletions

View file

@ -106,26 +106,33 @@ void NotificationsModel::refreshNotificationList()
return; return;
} }
if (!m_notificationList.isEmpty()) { clearNotifications();
beginRemoveRows(QModelIndex(), 0, m_notificationList.size() - 1);
qDeleteAll(m_notificationList);
m_notificationList.clear();
endRemoveRows();
}
if (!m_dbusInterface->isValid()) { if (!m_dbusInterface->isValid()) {
qCDebug(KDECONNECT_INTERFACES) << "dbus interface not valid"; qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
return; return;
} }
QDBusPendingReply<QStringList> pendingNotificationIds = m_dbusInterface->activeNotifications(); QDBusPendingReply<QStringList> pendingNotificationIds = m_dbusInterface->activeNotifications();
pendingNotificationIds.waitForFinished(); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingNotificationIds, this);
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
this, SLOT(receivedNotifications(QDBusPendingCallWatcher*)));
}
void NotificationsModel::receivedNotifications(QDBusPendingCallWatcher* watcher)
{
watcher->deleteLater();
clearNotifications();
QDBusPendingReply<QStringList> pendingNotificationIds = *watcher;
clearNotifications();
if (pendingNotificationIds.isError()) { if (pendingNotificationIds.isError()) {
qCDebug(KDECONNECT_INTERFACES) << pendingNotificationIds.error(); qCWarning(KDECONNECT_INTERFACES) << pendingNotificationIds.error();
return; return;
} }
const QStringList& notificationIds = pendingNotificationIds.value();
const QStringList notificationIds = pendingNotificationIds.value();
if (notificationIds.isEmpty()) { if (notificationIds.isEmpty()) {
return; return;
} }
@ -136,8 +143,6 @@ void NotificationsModel::refreshNotificationList()
m_notificationList.append(dbusInterface); m_notificationList.append(dbusInterface);
} }
endInsertRows(); endInsertRows();
Q_EMIT dataChanged(index(0), index(notificationIds.size() - 1));
} }
QVariant NotificationsModel::data(const QModelIndex& index, int role) const QVariant NotificationsModel::data(const QModelIndex& index, int role) const
@ -219,3 +224,13 @@ void NotificationsModel::dismissAll()
} }
} }
} }
void NotificationsModel::clearNotifications()
{
if (!m_notificationList.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, m_notificationList.size() - 1);
qDeleteAll(m_notificationList);
m_notificationList.clear();
endRemoveRows();
}
}

View file

@ -68,6 +68,7 @@ private Q_SLOTS:
void notificationAdded(const QString& id); void notificationAdded(const QString& id);
void notificationRemoved(const QString& id); void notificationRemoved(const QString& id);
void refreshNotificationList(); void refreshNotificationList();
void receivedNotifications(QDBusPendingCallWatcher* watcher);
Q_SIGNALS: Q_SIGNALS:
void deviceIdChanged(const QString& value); void deviceIdChanged(const QString& value);
@ -75,6 +76,8 @@ Q_SIGNALS:
void rowsChanged(); void rowsChanged();
private: private:
void clearNotifications();
DeviceNotificationsDbusInterface* m_dbusInterface; DeviceNotificationsDbusInterface* m_dbusInterface;
QList<NotificationDbusInterface*> m_notificationList; QList<NotificationDbusInterface*> m_notificationList;
QString m_deviceId; QString m_deviceId;