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;
}
if (!m_notificationList.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, m_notificationList.size() - 1);
qDeleteAll(m_notificationList);
m_notificationList.clear();
endRemoveRows();
}
clearNotifications();
if (!m_dbusInterface->isValid()) {
qCDebug(KDECONNECT_INTERFACES) << "dbus interface not valid";
qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
return;
}
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()) {
qCDebug(KDECONNECT_INTERFACES) << pendingNotificationIds.error();
qCWarning(KDECONNECT_INTERFACES) << pendingNotificationIds.error();
return;
}
const QStringList& notificationIds = pendingNotificationIds.value();
const QStringList notificationIds = pendingNotificationIds.value();
if (notificationIds.isEmpty()) {
return;
}
@ -136,8 +143,6 @@ void NotificationsModel::refreshNotificationList()
m_notificationList.append(dbusInterface);
}
endInsertRows();
Q_EMIT dataChanged(index(0), index(notificationIds.size() - 1));
}
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 notificationRemoved(const QString& id);
void refreshNotificationList();
void receivedNotifications(QDBusPendingCallWatcher* watcher);
Q_SIGNALS:
void deviceIdChanged(const QString& value);
@ -75,6 +76,8 @@ Q_SIGNALS:
void rowsChanged();
private:
void clearNotifications();
DeviceNotificationsDbusInterface* m_dbusInterface;
QList<NotificationDbusInterface*> m_notificationList;
QString m_deviceId;