2015-12-05 22:11:57 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Holger Kaelberer <holger.k@elberer.de>
|
2015-12-05 22:11:57 +00: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
|
2015-12-05 22:11:57 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-05 13:27:53 +01:00
|
|
|
#include "notifyingapplicationmodel.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QIcon>
|
|
|
|
#include <QString>
|
2016-07-05 13:27:53 +01:00
|
|
|
|
2015-12-05 22:11:57 +00:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2023-07-16 15:20:34 +01:00
|
|
|
// #include "modeltest.h"
|
2015-12-05 22:11:57 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
NotifyingApplicationModel::NotifyingApplicationModel(QObject *parent)
|
2015-12-05 22:11:57 +00:00
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<NotifyingApplication> NotifyingApplicationModel::apps()
|
|
|
|
{
|
|
|
|
return m_apps;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void NotifyingApplicationModel::appendApp(const NotifyingApplication &app)
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
|
|
|
if (app.name.isEmpty() || apps().contains(app))
|
|
|
|
return;
|
|
|
|
beginInsertRows(QModelIndex(), m_apps.size(), m_apps.size());
|
|
|
|
m_apps.append(app);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool NotifyingApplicationModel::containsApp(const QString &name) const
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const auto &a : m_apps)
|
2015-12-05 22:11:57 +00:00
|
|
|
if (a.name == name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Qt::ItemFlags NotifyingApplicationModel::flags(const QModelIndex &index) const
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
|
|
|
Qt::ItemFlags flags = Qt::ItemIsEnabled;
|
2022-09-10 22:23:52 +01:00
|
|
|
if (index.isValid() && index.row() >= 0 && index.row() < m_apps.size() && index.column() < 3) {
|
2015-12-05 22:11:57 +00:00
|
|
|
if (index.column() == 0)
|
2022-09-10 22:23:52 +01:00
|
|
|
flags |= Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
|
2015-12-05 22:11:57 +00:00
|
|
|
else if (index.column() == 2) {
|
|
|
|
if (m_apps[index.row()].active)
|
|
|
|
flags |= Qt::ItemIsEditable;
|
|
|
|
else
|
|
|
|
flags ^= Qt::ItemIsEnabled;
|
2022-09-10 22:23:52 +01:00
|
|
|
} else if (index.column() == 1) {
|
2015-12-05 22:11:57 +00:00
|
|
|
if (!m_apps[index.row()].active)
|
|
|
|
flags ^= Qt::ItemIsEnabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyingApplicationModel::clearApplications()
|
|
|
|
{
|
|
|
|
if (!m_apps.isEmpty()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_apps.size() - 1);
|
|
|
|
m_apps.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariant NotifyingApplicationModel::data(const QModelIndex &index, int role) const
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(role);
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_apps.size() || index.column() > 3) {
|
2015-12-05 22:11:57 +00:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (role) {
|
2022-09-10 22:23:52 +01:00
|
|
|
case Qt::TextAlignmentRole: {
|
|
|
|
if (index.column() == 0)
|
|
|
|
return int(Qt::AlignCenter | Qt::AlignVCenter);
|
|
|
|
else
|
|
|
|
return int(Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::DisplayRole: {
|
|
|
|
if (index.column() == 1)
|
|
|
|
return m_apps[index.row()].name;
|
|
|
|
else if (index.column() == 0)
|
|
|
|
return QVariant(); // m_apps[index.row()].active;
|
|
|
|
else if (index.column() == 2)
|
|
|
|
return m_apps[index.row()].blacklistExpression.pattern();
|
|
|
|
else
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::DecorationRole: {
|
|
|
|
if (index.column() == 1)
|
|
|
|
return QIcon::fromTheme(m_apps[index.row()].icon, QIcon::fromTheme(QStringLiteral("application-x-executable")));
|
|
|
|
else
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::EditRole: {
|
|
|
|
if (index.column() == 0)
|
|
|
|
return m_apps[index.row()].active ? Qt::Checked : Qt::Unchecked;
|
|
|
|
else if (index.column() == 2)
|
|
|
|
return m_apps[index.row()].blacklistExpression.pattern();
|
|
|
|
else
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::CheckStateRole: {
|
|
|
|
if (index.column() == 0)
|
|
|
|
return m_apps[index.row()].active ? Qt::Checked : Qt::Unchecked;
|
|
|
|
else
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool NotifyingApplicationModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!index.isValid() || (index.column() != 0 && index.column() != 2) || index.row() < 0 || index.row() >= m_apps.size())
|
2015-12-05 22:11:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool res = false;
|
|
|
|
QModelIndex bottomRight = createIndex(index.row(), index.column());
|
|
|
|
switch (role) {
|
2022-09-10 22:23:52 +01:00
|
|
|
case Qt::CheckStateRole: {
|
|
|
|
if (index.column() == 0) {
|
|
|
|
m_apps[index.row()].active = ((Qt::CheckState)value.toInt() == Qt::Checked);
|
|
|
|
bottomRight = createIndex(index.row(), index.column() + 1);
|
|
|
|
res = true;
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::EditRole: {
|
|
|
|
if (index.column() == 2) {
|
|
|
|
m_apps[index.row()].blacklistExpression.setPattern(value.toString());
|
|
|
|
res = true;
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
}
|
2015-12-05 22:11:57 +00:00
|
|
|
if (res) {
|
|
|
|
Q_EMIT dataChanged(index, bottomRight);
|
2022-09-10 22:23:52 +01:00
|
|
|
Q_EMIT applicationsChanged(); // -> notify config that we need to save
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyingApplicationModel::sort(int column, Qt::SortOrder order)
|
|
|
|
{
|
|
|
|
if (column != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (order == Qt::AscendingOrder)
|
2022-09-10 22:23:52 +01:00
|
|
|
std::sort(m_apps.begin(), m_apps.end(), [](const NotifyingApplication &a, const NotifyingApplication &b) {
|
|
|
|
return (a.name.compare(b.name, Qt::CaseInsensitive) < 1);
|
|
|
|
});
|
2015-12-05 22:11:57 +00:00
|
|
|
else
|
2022-09-10 22:23:52 +01:00
|
|
|
std::sort(m_apps.begin(), m_apps.end(), [](const NotifyingApplication &a, const NotifyingApplication &b) {
|
|
|
|
return (b.name.compare(a.name, Qt::CaseInsensitive) < 1);
|
|
|
|
});
|
2015-12-05 22:11:57 +00:00
|
|
|
Q_EMIT dataChanged(createIndex(0, 0), createIndex(m_apps.size(), 2));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariant NotifyingApplicationModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
|
|
|
switch (role) {
|
2022-09-10 22:23:52 +01:00
|
|
|
case Qt::DisplayRole: {
|
|
|
|
if (section == 1)
|
|
|
|
return i18n("Name");
|
|
|
|
else if (section == 0)
|
|
|
|
return QVariant(); // i18n("Sync");
|
|
|
|
else
|
|
|
|
return i18n("Blacklisted");
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole: {
|
|
|
|
if (section == 1)
|
|
|
|
return i18n("Name of a notifying application.");
|
|
|
|
else if (section == 0)
|
|
|
|
return i18n("Synchronize notifications of an application?");
|
|
|
|
else
|
|
|
|
return i18n(
|
|
|
|
"Regular expression defining which notifications should not be sent.\nThis pattern is applied to the summary and, if selected above, the body "
|
|
|
|
"of notifications.");
|
|
|
|
}
|
2015-12-05 22:11:57 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int NotifyingApplicationModel::columnCount(const QModelIndex &) const
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int NotifyingApplicationModel::rowCount(const QModelIndex &parent) const
|
2015-12-05 22:11:57 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (parent.isValid()) {
|
|
|
|
// Return size 0 if we are a child because this is not a tree
|
2015-12-05 22:11:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return m_apps.size();
|
|
|
|
}
|