2013-08-21 17:25:44 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notificationsmodel.h"
|
2014-09-21 23:16:39 +01:00
|
|
|
#include "interfaces_debug.h"
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2014-09-21 23:16:39 +01:00
|
|
|
#include <QDebug>
|
2013-08-21 17:25:44 +01:00
|
|
|
#include <QDBusInterface>
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include <KSharedConfig>
|
2013-08-21 17:25:44 +01:00
|
|
|
#include <KConfigGroup>
|
2014-09-13 00:04:48 +01:00
|
|
|
#include <QIcon>
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2014-07-01 00:25:30 +01:00
|
|
|
//#include "modeltest.h"
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
NotificationsModel::NotificationsModel(QObject* parent)
|
2013-08-21 17:25:44 +01:00
|
|
|
: QAbstractListModel(parent)
|
2015-09-08 09:46:59 +01:00
|
|
|
, m_dbusInterface(nullptr)
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
//new ModelTest(this, this);
|
|
|
|
|
2015-08-21 17:38:54 +01:00
|
|
|
connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
2013-08-22 02:21:08 +01:00
|
|
|
this, SIGNAL(rowsChanged()));
|
2015-08-21 17:38:54 +01:00
|
|
|
connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
2013-08-28 18:33:46 +01:00
|
|
|
this, SIGNAL(rowsChanged()));
|
|
|
|
|
2015-08-21 17:38:54 +01:00
|
|
|
connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
2013-08-28 18:33:46 +01:00
|
|
|
this, SIGNAL(anyDismissableChanged()));
|
2015-11-12 11:27:30 +00:00
|
|
|
connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
|
|
|
this, SIGNAL(anyDismissableChanged()));
|
2015-06-25 20:19:23 +01:00
|
|
|
|
|
|
|
QDBusServiceWatcher* watcher = new QDBusServiceWatcher(DaemonDbusInterface::activatedService(),
|
|
|
|
QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
|
|
|
|
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &NotificationsModel::refreshNotificationList);
|
|
|
|
connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &NotificationsModel::clearNotifications);
|
2014-09-23 18:27:47 +01:00
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2014-09-23 18:27:47 +01:00
|
|
|
QHash<int, QByteArray> NotificationsModel::roleNames() const
|
|
|
|
{
|
2013-08-22 02:21:08 +01:00
|
|
|
//Role names for QML
|
2014-09-23 18:27:47 +01:00
|
|
|
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
|
2015-01-21 06:22:14 +00:00
|
|
|
names.insert(DbusInterfaceRole, "dbusInterface");
|
|
|
|
names.insert(AppNameModelRole, "appName");
|
|
|
|
names.insert(IdModelRole, "notificationId");
|
2013-08-22 02:21:08 +01:00
|
|
|
names.insert(DismissableModelRole, "dismissable");
|
2014-09-23 18:27:47 +01:00
|
|
|
return names;
|
2013-08-22 02:21:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationsModel::~NotificationsModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-12 20:47:28 +01:00
|
|
|
QString NotificationsModel::deviceId() const
|
2013-08-22 02:21:08 +01:00
|
|
|
{
|
|
|
|
return m_deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsModel::setDeviceId(const QString& deviceId)
|
|
|
|
{
|
|
|
|
m_deviceId = deviceId;
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
if (m_dbusInterface) {
|
|
|
|
delete m_dbusInterface;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
m_dbusInterface = new DeviceNotificationsDbusInterface(deviceId, this);
|
|
|
|
|
2013-08-21 17:25:44 +01:00
|
|
|
connect(m_dbusInterface, SIGNAL(notificationPosted(QString)),
|
|
|
|
this, SLOT(notificationAdded(QString)));
|
|
|
|
connect(m_dbusInterface, SIGNAL(notificationRemoved(QString)),
|
|
|
|
this, SLOT(notificationRemoved(QString)));
|
2016-04-05 23:25:12 +01:00
|
|
|
connect(m_dbusInterface, SIGNAL(allNotificationsRemoved()),
|
|
|
|
this, SLOT(clearNotifications()));
|
2013-08-21 17:25:44 +01:00
|
|
|
|
|
|
|
refreshNotificationList();
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
Q_EMIT deviceIdChanged(deviceId);
|
2013-08-21 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsModel::notificationAdded(const QString& id)
|
|
|
|
{
|
2016-04-03 15:48:28 +01:00
|
|
|
int currentSize = m_notificationList.size();
|
|
|
|
beginInsertRows(QModelIndex(), currentSize, currentSize);
|
|
|
|
NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, id, this);
|
|
|
|
m_notificationList.append(dbusInterface);
|
|
|
|
endInsertRows();
|
2013-08-21 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsModel::notificationRemoved(const QString& id)
|
|
|
|
{
|
2016-04-03 15:48:28 +01:00
|
|
|
for (int i = 0; i < m_notificationList.size(); ++i) {
|
|
|
|
if (m_notificationList[i]->notificationId() == id) {
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
m_notificationList.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qCWarning(KDECONNECT_INTERFACES) << "Attempted to remove unknown notification: " << id;
|
2013-08-21 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsModel::refreshNotificationList()
|
|
|
|
{
|
2014-06-14 12:31:02 +01:00
|
|
|
if (!m_dbusInterface) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2015-03-14 04:37:05 +00:00
|
|
|
clearNotifications();
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
if (!m_dbusInterface->isValid()) {
|
2015-03-14 04:37:05 +00:00
|
|
|
qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
|
2014-06-14 12:31:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2013-08-28 18:33:46 +01:00
|
|
|
QDBusPendingReply<QStringList> pendingNotificationIds = m_dbusInterface->activeNotifications();
|
2015-03-14 04:37:05 +00:00
|
|
|
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;
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
if (pendingNotificationIds.isError()) {
|
2015-03-14 04:37:05 +00:00
|
|
|
qCWarning(KDECONNECT_INTERFACES) << pendingNotificationIds.error();
|
2014-06-14 12:31:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-08-28 18:33:46 +01:00
|
|
|
|
2015-03-14 04:37:05 +00:00
|
|
|
const QStringList notificationIds = pendingNotificationIds.value();
|
2014-06-14 12:31:02 +01:00
|
|
|
if (notificationIds.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
beginInsertRows(QModelIndex(), 0, notificationIds.size() - 1);
|
|
|
|
Q_FOREACH (const QString& notificationId, notificationIds) {
|
2013-08-22 02:21:08 +01:00
|
|
|
NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, notificationId, this);
|
2013-08-21 17:25:44 +01:00
|
|
|
m_notificationList.append(dbusInterface);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
QVariant NotificationsModel::data(const QModelIndex& index, int role) const
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
|
|
|
if (!index.isValid()
|
|
|
|
|| index.row() < 0
|
|
|
|
|| index.row() >= m_notificationList.count()
|
|
|
|
|| !m_notificationList[index.row()]->isValid())
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-08-21 17:25:44 +01:00
|
|
|
NotificationDbusInterface* notification = m_notificationList[index.row()];
|
|
|
|
|
2015-01-21 06:22:14 +00:00
|
|
|
//FIXME: This function gets called lots of times, producing lots of dbus calls. Add a cache?
|
2013-08-21 17:25:44 +01:00
|
|
|
switch (role) {
|
|
|
|
case IconModelRole:
|
2015-04-20 06:21:37 +01:00
|
|
|
return QIcon::fromTheme("device-notifier");
|
2013-08-21 17:25:44 +01:00
|
|
|
case IdModelRole:
|
2014-09-09 16:54:16 +01:00
|
|
|
return notification->internalId();
|
2013-08-21 17:25:44 +01:00
|
|
|
case NameModelRole:
|
2014-09-09 16:54:16 +01:00
|
|
|
return notification->ticker();
|
2013-08-22 02:21:08 +01:00
|
|
|
case ContentModelRole:
|
2013-08-28 18:33:46 +01:00
|
|
|
return QString(); //To implement in the Android side
|
2013-08-22 02:21:08 +01:00
|
|
|
case AppNameModelRole:
|
2014-09-09 16:54:16 +01:00
|
|
|
return notification->appName();
|
2013-08-22 02:21:08 +01:00
|
|
|
case DbusInterfaceRole:
|
|
|
|
return qVariantFromValue<QObject*>(notification);
|
|
|
|
case DismissableModelRole:
|
|
|
|
return notification->dismissable();
|
2013-08-21 17:25:44 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
NotificationDbusInterface* NotificationsModel::getNotification(const QModelIndex& index) const
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
2015-09-08 09:46:59 +01:00
|
|
|
return nullptr;
|
2013-08-21 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
if (row < 0 || row >= m_notificationList.size()) {
|
2015-09-08 09:46:59 +01:00
|
|
|
return nullptr;
|
2013-08-21 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_notificationList[row];
|
|
|
|
}
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
int NotificationsModel::rowCount(const QModelIndex& parent) const
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
2014-06-14 12:31:02 +01:00
|
|
|
if (parent.isValid()) {
|
2013-08-22 02:21:08 +01:00
|
|
|
//Return size 0 if we are a child because this is not a tree
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-21 17:25:44 +01:00
|
|
|
|
|
|
|
return m_notificationList.count();
|
|
|
|
}
|
|
|
|
|
2014-04-12 20:47:28 +01:00
|
|
|
bool NotificationsModel::isAnyDimissable() const
|
2013-08-28 18:33:46 +01:00
|
|
|
{
|
2014-06-14 12:31:02 +01:00
|
|
|
Q_FOREACH (NotificationDbusInterface* notification, m_notificationList) {
|
2013-08-28 18:33:46 +01:00
|
|
|
if (notification->dismissable()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationsModel::dismissAll()
|
2013-08-22 02:21:08 +01:00
|
|
|
{
|
2014-06-14 12:31:02 +01:00
|
|
|
Q_FOREACH (NotificationDbusInterface* notification, m_notificationList) {
|
2013-08-22 02:21:08 +01:00
|
|
|
if (notification->dismissable()) {
|
|
|
|
notification->dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 04:37:05 +00:00
|
|
|
|
|
|
|
void NotificationsModel::clearNotifications()
|
|
|
|
{
|
|
|
|
if (!m_notificationList.isEmpty()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_notificationList.size() - 1);
|
|
|
|
qDeleteAll(m_notificationList);
|
|
|
|
m_notificationList.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
}
|