notificationsmodel.cpp 8.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * 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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21
 */

#include "notificationsmodel.h"
22
#include "interfaces_debug.h"
23

24
#include <QDebug>
25 26
#include <QDBusInterface>

27 28
#include <QIcon>

29 30
#include <dbushelper.h>

31 32
//#include "modeltest.h"

33 34 35
//In older Qt released, qAsConst isnt available
#include "core/qtcompat_p.h"

36
NotificationsModel::NotificationsModel(QObject* parent)
37
    : QAbstractListModel(parent)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
38
    , m_dbusInterface(nullptr)
39 40
{

41 42
    //new ModelTest(this, this);

43 44 45 46
    connect(this, &QAbstractItemModel::rowsInserted,
            this, &NotificationsModel::rowsChanged);
    connect(this, &QAbstractItemModel::rowsRemoved,
            this, &NotificationsModel::rowsChanged);
47

48 49 50 51
    connect(this, &QAbstractItemModel::dataChanged,
            this, &NotificationsModel::anyDismissableChanged);
    connect(this, &QAbstractItemModel::rowsInserted,
            this, &NotificationsModel::anyDismissableChanged);
52 53

    QDBusServiceWatcher* watcher = new QDBusServiceWatcher(DaemonDbusInterface::activatedService(),
54
                                                           DBusHelper::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
55 56
    connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &NotificationsModel::refreshNotificationList);
    connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &NotificationsModel::clearNotifications);
57
}
58

59 60
QHash<int, QByteArray> NotificationsModel::roleNames() const
{
61
    //Role names for QML
62
    QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
Nicolas Fella's avatar
Nicolas Fella committed
63 64 65
    names.insert(DbusInterfaceRole, "dbusInterface");
    names.insert(AppNameModelRole, "appName");
    names.insert(IdModelRole, "notificationId");
66
    names.insert(DismissableModelRole, "dismissable");
67 68
    names.insert(RepliableModelRole, "repliable");
    names.insert(IconPathModelRole, "appIcon");
69 70
    names.insert(TitleModelRole, "title");
    names.insert(TextModelRole, "notitext");
71
    return names;
72 73 74 75 76 77
}

NotificationsModel::~NotificationsModel()
{
}

78
QString NotificationsModel::deviceId() const
79 80 81 82 83 84 85 86
{
    return m_deviceId;
}

void NotificationsModel::setDeviceId(const QString& deviceId)
{
    m_deviceId = deviceId;

87 88 89 90
    if (m_dbusInterface) {
        delete m_dbusInterface;
    }

91 92
    m_dbusInterface = new DeviceNotificationsDbusInterface(deviceId, this);

93 94 95 96 97 98
    connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::notificationPosted,
            this, &NotificationsModel::notificationAdded);
    connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::notificationRemoved,
            this, &NotificationsModel::notificationRemoved);
    connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::allNotificationsRemoved,
            this, &NotificationsModel::clearNotifications);
99 100 101

    refreshNotificationList();

102
    Q_EMIT deviceIdChanged(deviceId);
103 104 105 106
}

void NotificationsModel::notificationAdded(const QString& id)
{
107
    beginInsertRows(QModelIndex(),  0, 0);
108
    NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, id, this);
109
    connect(dbusInterface, &NotificationDbusInterface::ready, this, &NotificationsModel::notificationUpdated);
110
    m_notificationList.prepend(dbusInterface);
111
    endInsertRows();
112 113 114 115
}

void NotificationsModel::notificationRemoved(const QString& id)
{
116 117 118 119 120 121 122 123 124
    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;
125 126 127 128
}

void NotificationsModel::refreshNotificationList()
{
129 130 131
    if (!m_dbusInterface) {
        return;
    }
132

133
    clearNotifications();
134

135
    if (!m_dbusInterface->isValid()) {
136
        qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
137 138
        return;
    }
139

140
    QDBusPendingReply<QStringList> pendingNotificationIds = m_dbusInterface->activeNotifications();
141
    QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(pendingNotificationIds, this);
142

143 144
    QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
                     this, &NotificationsModel::receivedNotifications);
145 146 147 148 149 150 151 152
}

void NotificationsModel::receivedNotifications(QDBusPendingCallWatcher* watcher)
{
    watcher->deleteLater();
    clearNotifications();
    QDBusPendingReply<QStringList> pendingNotificationIds = *watcher;

153
    if (pendingNotificationIds.isError()) {
154
        qCWarning(KDECONNECT_INTERFACES) << pendingNotificationIds.error();
155 156
        return;
    }
157

158
    const QStringList notificationIds = pendingNotificationIds.value();
159 160 161
    if (notificationIds.isEmpty()) {
        return;
    }
162

163
    beginInsertRows(QModelIndex(), 0, notificationIds.size() - 1);
164
    for (const QString& notificationId : notificationIds) {
165
        NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, notificationId, this);
166 167 168 169 170
        m_notificationList.append(dbusInterface);
    }
    endInsertRows();
}

171
QVariant NotificationsModel::data(const QModelIndex& index, int role) const
172 173 174 175 176 177 178 179 180
{
    if (!index.isValid()
        || index.row() < 0
        || index.row() >= m_notificationList.count()
        || !m_notificationList[index.row()]->isValid())
    {
        return QVariant();
    }

181 182 183 184
    if (!m_dbusInterface || !m_dbusInterface->isValid()) {
        return QVariant();
    }

185 186
    NotificationDbusInterface* notification = m_notificationList[index.row()];

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
187
    //FIXME: This function gets called lots of times, producing lots of dbus calls. Add a cache?
188
    switch (role) {
189 190
        case IconModelRole:
            return QIcon::fromTheme(QStringLiteral("device-notifier"));
191
        case IdModelRole:
Lukáš Tinkl's avatar
Lukáš Tinkl committed
192
            return notification->internalId();
193
        case NameModelRole:
Lukáš Tinkl's avatar
Lukáš Tinkl committed
194
            return notification->ticker();
195
        case ContentModelRole:
196
            return QString(); //To implement in the Android side
197
        case AppNameModelRole:
Lukáš Tinkl's avatar
Lukáš Tinkl committed
198
            return notification->appName();
199
        case DbusInterfaceRole:
200
            return QVariant::fromValue<QObject*>(notification);
201 202
        case DismissableModelRole:
            return notification->dismissable();
203 204 205 206
        case RepliableModelRole:
            return !notification->replyId().isEmpty();
        case IconPathModelRole:
            return notification->iconPath();
207 208 209 210
        case TitleModelRole:
            return notification->title();
        case TextModelRole:
            return notification->text();
211 212 213 214 215
        default:
             return QVariant();
    }
}

216
NotificationDbusInterface* NotificationsModel::getNotification(const QModelIndex& index) const
217 218
{
    if (!index.isValid()) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
219
        return nullptr;
220 221 222 223
    }

    int row = index.row();
    if (row < 0 || row >= m_notificationList.size()) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
224
        return nullptr;
225 226 227 228 229
    }

    return m_notificationList[row];
}

230
int NotificationsModel::rowCount(const QModelIndex& parent) const
231
{
232
    if (parent.isValid()) {
233 234 235
        //Return size 0 if we are a child because this is not a tree
        return 0;
    }
236 237 238 239

    return m_notificationList.count();
}

240
bool NotificationsModel::isAnyDimissable() const
241
{
242
    for (NotificationDbusInterface* notification : qAsConst(m_notificationList)) {
243 244 245 246 247 248 249 250
        if (notification->dismissable()) {
            return true;
        }
    }
    return false;
}

void NotificationsModel::dismissAll()
251
{
252
    for (NotificationDbusInterface* notification : qAsConst(m_notificationList)) {
253 254 255 256 257
        if (notification->dismissable()) {
            notification->dismiss();
        }
    }
}
258 259 260 261 262 263 264 265 266 267

void NotificationsModel::clearNotifications()
{
    if (!m_notificationList.isEmpty()) {
        beginRemoveRows(QModelIndex(), 0, m_notificationList.size() - 1);
        qDeleteAll(m_notificationList);
        m_notificationList.clear();
        endRemoveRows();
    }
}
268

269
void NotificationsModel::notificationUpdated()
270 271 272
{
    Q_EMIT dataChanged(index(0,0), index(m_notificationList.size() - 1, 0));
}