2013-08-21 17:25:44 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-08-21 17:25:44 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-08-21 17:25:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notificationsmodel.h"
|
|
|
|
|
2014-09-21 23:16:39 +01:00
|
|
|
#include <QDebug>
|
2019-03-24 16:10:18 +00:00
|
|
|
#include <QIcon>
|
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "interfaces_debug.h"
|
Build kdeconnect on sailfish and port some simple plugins
Summary:
Below is a lost of the commits, but, in summary
Port the build system for Sailfish, which means selectively building only the bits we need/can, and only against the KF5 libs that are available.
Allow to build on Qt 5.6
Switch from knotification to nemo notification (not complete!)
Add a very simple example sailfish app.
Note, there is still much missing functionality. Notifications dont work, pairing sort of works but not really, but when it is paired you can send a ping to the desktop client
Dont build kio for Sailfish
Port core build system
Port daemon buld system
Require CoreAddons on Sailfish
Port plugins build for sailfish and include the ping plugin for now
Final build changes for sailfish.
Disable tests and other not needed parts
Add includes for QCA
Fix build errors on sailfish
Get core/ to build on sailfish
Get interfaces/ to build on sailfish
Build daemon on sailfish
On sailfish, dont install the kcm file
Start port plugin to sailfish
Fixup installed files
Add sfos app
Hack declarative plugin to give a public interface
Build sfos app
Compile declarativeplugin into the sfos app for now
Redefine qAsConst for qt 5.6
Packaging fixes
Use official icon
Package .desktop
Reviewers: #kde_connect, apol, nicolasfella, albertvaka
Reviewed By: #kde_connect, apol, nicolasfella, albertvaka
Subscribers: kdeconnect, andyholmes, albertvaka, kossebau, mtijink, vonreth, apol, #kde_connect, nicolasfella
Tags: #kde_connect
Differential Revision: https://phabricator.kde.org/D10703
2018-08-02 20:10:59 +01:00
|
|
|
|
2022-09-10 22:23:52 +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
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(this, &QAbstractItemModel::rowsInserted, this, &NotificationsModel::rowsChanged);
|
|
|
|
connect(this, &QAbstractItemModel::rowsRemoved, this, &NotificationsModel::rowsChanged);
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(this, &QAbstractItemModel::dataChanged, this, &NotificationsModel::anyDismissableChanged);
|
|
|
|
connect(this, &QAbstractItemModel::rowsInserted, this, &NotificationsModel::anyDismissableChanged);
|
2013-08-28 18:33:46 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusServiceWatcher *watcher =
|
|
|
|
new QDBusServiceWatcher(DaemonDbusInterface::activatedService(), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
|
2015-06-25 20:19:23 +01:00
|
|
|
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
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Role names for QML
|
2014-09-23 18:27:47 +01:00
|
|
|
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
|
2019-10-20 00:49:14 +01: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");
|
2017-06-01 15:17:37 +01:00
|
|
|
names.insert(RepliableModelRole, "repliable");
|
|
|
|
names.insert(IconPathModelRole, "appIcon");
|
2017-08-22 17:16:42 +01:00
|
|
|
names.insert(TitleModelRole, "title");
|
|
|
|
names.insert(TextModelRole, "notitext");
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsModel::setDeviceId(const QString &deviceId)
|
2013-08-22 02:21:08 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::notificationPosted, this, &NotificationsModel::notificationAdded);
|
|
|
|
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::notificationRemoved, this, &NotificationsModel::notificationRemoved);
|
|
|
|
connect(m_dbusInterface, &OrgKdeKdeconnectDeviceNotificationsInterface::allNotificationsRemoved, this, &NotificationsModel::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
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsModel::notificationAdded(const QString &id)
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
|
|
|
NotificationDbusInterface *dbusInterface = new NotificationDbusInterface(m_deviceId, id, this);
|
2019-01-24 08:11:24 +00:00
|
|
|
connect(dbusInterface, &NotificationDbusInterface::ready, this, &NotificationsModel::notificationUpdated);
|
2018-07-12 15:16:52 +01:00
|
|
|
m_notificationList.prepend(dbusInterface);
|
2016-04-03 15:48:28 +01:00
|
|
|
endInsertRows();
|
2013-08-21 17:25:44 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsModel::notificationRemoved(const QString &id)
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
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();
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingNotificationIds, this);
|
2015-03-14 04:37:05 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &NotificationsModel::receivedNotifications);
|
2015-03-14 04:37:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotificationsModel::receivedNotifications(QDBusPendingCallWatcher *watcher)
|
2015-03-14 04:37:05 +00:00
|
|
|
{
|
|
|
|
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);
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const QString ¬ificationId : notificationIds) {
|
|
|
|
NotificationDbusInterface *dbusInterface = new NotificationDbusInterface(m_deviceId, notificationId, this);
|
2013-08-21 17:25:44 +01:00
|
|
|
m_notificationList.append(dbusInterface);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariant NotificationsModel::data(const QModelIndex &index, int role) const
|
2013-08-21 17:25:44 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_notificationList.count() || !m_notificationList[index.row()]->isValid()) {
|
2013-08-21 17:25:44 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
if (!m_dbusInterface || !m_dbusInterface->isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
NotificationDbusInterface *notification = m_notificationList[index.row()];
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2022-09-10 22:23:52 +01: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) {
|
2022-09-10 22:23:52 +01:00
|
|
|
case IconModelRole:
|
|
|
|
return QIcon::fromTheme(QStringLiteral("device-notifier"));
|
|
|
|
case IdModelRole:
|
|
|
|
return notification->internalId();
|
|
|
|
case NameModelRole:
|
|
|
|
return notification->ticker();
|
|
|
|
case ContentModelRole:
|
|
|
|
return QString(); // To implement in the Android side
|
|
|
|
case AppNameModelRole:
|
|
|
|
return notification->appName();
|
|
|
|
case DbusInterfaceRole:
|
|
|
|
return QVariant::fromValue<QObject *>(notification);
|
|
|
|
case DismissableModelRole:
|
|
|
|
return notification->dismissable();
|
|
|
|
case RepliableModelRole:
|
|
|
|
return !notification->replyId().isEmpty();
|
|
|
|
case IconPathModelRole:
|
|
|
|
return notification->iconPath();
|
|
|
|
case TitleModelRole:
|
|
|
|
return notification->title();
|
|
|
|
case TextModelRole:
|
|
|
|
return notification->text();
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +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()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
// Return size 0 if we are a child because this is not a tree
|
2013-08-22 02:21:08 +01:00
|
|
|
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
|
|
|
{
|
2024-04-21 16:45:08 +01:00
|
|
|
for (NotificationDbusInterface *notification : std::as_const(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
|
|
|
{
|
2024-04-21 16:45:08 +01:00
|
|
|
for (NotificationDbusInterface *notification : std::as_const(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();
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 03:12:16 +00:00
|
|
|
|
2019-01-24 08:11:24 +00:00
|
|
|
void NotificationsModel::notificationUpdated()
|
2017-11-06 03:12:16 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
Q_EMIT dataChanged(index(0, 0), index(m_notificationList.size() - 1, 0));
|
2017-11-06 03:12:16 +00:00
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_notificationsmodel.cpp"
|