Commit 07dc5c49 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[Notifications] Report roleNames in the relevant places

QML needs it on the model it uses (Notifications) where they used to be but GammaRay seems to be taking them from the source (NotificationsModel).
This has both models (and jobs model) return the roleNames() and moves them to Utils so they're shared.

Differential Revision: https://phabricator.kde.org/D28657
parent e2cf1ef6
......@@ -22,6 +22,7 @@
#include "jobsmodel_p.h"
#include "notifications.h"
#include "utils_p.h"
#include <QDebug>
#include <QScopedPointer>
......@@ -176,6 +177,11 @@ int JobsModel::rowCount(const QModelIndex &parent) const
return d->m_jobViews.count();
}
QHash<int, QByteArray> JobsModel::roleNames() const
{
return Utils::roleNames();
}
void JobsModel::close(const QModelIndex &idx)
{
if (checkIndex(idx)) {
......
......@@ -57,6 +57,7 @@ public:
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
/**
* @brief Close a job
......
......@@ -843,26 +843,5 @@ int Notifications::rowCount(const QModelIndex &parent) const
QHash<int, QByteArray> Notifications::roleNames() const
{
static QHash<int, QByteArray> s_roles;
if (s_roles.isEmpty()) {
s_roles = QSortFilterProxyModel::roleNames();
// This generates role names from the Roles enum in the form of: FooRole -> foo
const QMetaEnum e = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("Roles"));
for (int i = 0; i < e.keyCount(); ++i) {
const int value = e.value(i);
QByteArray key(e.key(i));
key[0] = key[0] + 32; // lower case first letter
key.chop(4); // strip "Role" suffix
s_roles.insert(value, key);
}
s_roles.insert(IdRole, QByteArrayLiteral("notificationId")); // id is QML-reserved
}
return s_roles;
return Utils::roleNames();
}
......@@ -23,6 +23,7 @@
#include "debug.h"
#include "server.h"
#include "utils_p.h"
#include "notifications.h"
......@@ -350,6 +351,11 @@ int NotificationsModel::rowCount(const QModelIndex &parent) const
return d->notifications.count();
}
QHash<int, QByteArray> NotificationsModel::roleNames() const
{
return Utils::roleNames();
}
void NotificationsModel::expire(uint notificationId)
{
if (d->rowOfNotification(notificationId) > -1) {
......
......@@ -45,6 +45,7 @@ public:
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
void expire(uint notificationId);
void close(uint notificationId);
......
......@@ -20,12 +20,15 @@
#include "utils_p.h"
#include "notifications.h"
#include <QAbstractItemModel>
#include <QAbstractProxyModel>
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QFile>
#include <QMetaEnum>
#include <QTextStream>
#include <KConcatenateRowsProxyModel>
......@@ -34,6 +37,34 @@
using namespace NotificationManager;
QHash<int, QByteArray> Utils::roleNames()
{
static QHash<int, QByteArray> s_roles;
if (s_roles.isEmpty()) {
// This generates role names from the Roles enum in the form of: FooRole -> foo
const QMetaEnum e = QMetaEnum::fromType<Notifications::Roles>();
// Qt built-in roles we use
s_roles.insert(Qt::DisplayRole, QByteArrayLiteral("display"));
s_roles.insert(Qt::DecorationRole, QByteArrayLiteral("decoration"));
for (int i = 0; i < e.keyCount(); ++i) {
const int value = e.value(i);
QByteArray key(e.key(i));
key[0] = key[0] + 32; // lower case first letter
key.chop(4); // strip "Role" suffix
s_roles.insert(value, key);
}
s_roles.insert(Notifications::IdRole, QByteArrayLiteral("notificationId")); // id is QML-reserved
}
return s_roles;
}
QString Utils::processNameFromPid(uint pid)
{
auto processInfo = KProcessList::processInfo(pid);
......
......@@ -32,6 +32,8 @@ namespace NotificationManager
namespace Utils
{
QHash<int, QByteArray> roleNames();
QString processNameFromPid(uint pid);
QString desktopEntryFromPid(uint pid);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment