2023-08-24 19:53:51 +03:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "knotificationconfigwidget.h"
|
|
|
|
#include "kdialog.h"
|
2023-08-25 19:11:07 +03:00
|
|
|
#include "klineedit.h"
|
2023-08-25 20:33:54 +03:00
|
|
|
#include "kstandarddirs.h"
|
2023-08-24 19:53:51 +03:00
|
|
|
#include "klocale.h"
|
2023-08-25 20:33:54 +03:00
|
|
|
#include "kdebug.h"
|
2023-08-24 19:53:51 +03:00
|
|
|
|
2023-08-25 20:33:54 +03:00
|
|
|
#include <QVBoxLayout>
|
2023-08-25 19:11:07 +03:00
|
|
|
#include <QTreeWidget>
|
|
|
|
|
2023-08-24 19:53:51 +03:00
|
|
|
class KNotificationConfigWidgetPrivate
|
|
|
|
{
|
|
|
|
public:
|
2023-08-25 19:11:07 +03:00
|
|
|
KNotificationConfigWidgetPrivate(KNotificationConfigWidget *q);
|
2023-08-25 20:33:54 +03:00
|
|
|
~KNotificationConfigWidgetPrivate();
|
2023-08-25 19:11:07 +03:00
|
|
|
|
2023-08-25 20:33:54 +03:00
|
|
|
void _k_slotItemChanged(QTreeWidgetItem *item, int column);
|
2023-08-25 19:11:07 +03:00
|
|
|
|
|
|
|
KNotificationConfigWidget* parent;
|
2023-08-25 20:33:54 +03:00
|
|
|
QMap<QString,KConfig*> configs;
|
|
|
|
QVBoxLayout* layout;
|
2023-08-25 19:11:07 +03:00
|
|
|
QTreeWidget* treewidget;
|
2023-08-25 20:33:54 +03:00
|
|
|
QString enabledi18n;
|
|
|
|
QString disabledi18n;
|
|
|
|
QString popuptooltipi18n;
|
|
|
|
QString soundtooltipi18n;
|
|
|
|
QString taskbartooltipi18n;
|
2023-08-24 19:53:51 +03:00
|
|
|
};
|
|
|
|
|
2023-08-25 19:11:07 +03:00
|
|
|
KNotificationConfigWidgetPrivate::KNotificationConfigWidgetPrivate(KNotificationConfigWidget *q)
|
|
|
|
: parent(q),
|
|
|
|
layout(nullptr),
|
2023-08-25 20:33:54 +03:00
|
|
|
treewidget(nullptr)
|
2023-08-24 19:53:51 +03:00
|
|
|
{
|
2023-08-25 20:33:54 +03:00
|
|
|
// translate once
|
|
|
|
enabledi18n = i18n("Enabled");
|
|
|
|
disabledi18n = i18n("Disabled");
|
|
|
|
popuptooltipi18n = i18n("Show a message in a popup");
|
|
|
|
soundtooltipi18n = i18n("Play a sound");
|
|
|
|
taskbartooltipi18n = i18n("Mark tasbar entry");
|
2023-08-24 19:53:51 +03:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:33:54 +03:00
|
|
|
KNotificationConfigWidgetPrivate::~KNotificationConfigWidgetPrivate()
|
|
|
|
{
|
|
|
|
QMutableMapIterator<QString,KConfig*> iter(configs);
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
iter.next();
|
|
|
|
KConfig* config = iter.value();
|
|
|
|
delete config;
|
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KNotificationConfigWidgetPrivate::_k_slotItemChanged(QTreeWidgetItem *item, int column)
|
|
|
|
{
|
|
|
|
emit parent->changed(true);
|
|
|
|
if (item && item->flags() & Qt::ItemIsUserCheckable) {
|
|
|
|
item->setText(column, item->checkState(column) == Qt::Checked ? enabledi18n : disabledi18n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KNotificationConfigWidget::KNotificationConfigWidget(const QString ¬ification, QWidget *parent)
|
2023-08-24 19:53:51 +03:00
|
|
|
: QWidget(parent),
|
2023-08-25 19:11:07 +03:00
|
|
|
d(new KNotificationConfigWidgetPrivate(this))
|
2023-08-24 19:53:51 +03:00
|
|
|
{
|
2023-08-25 20:33:54 +03:00
|
|
|
d->layout = new QVBoxLayout(this);
|
2023-08-25 19:11:07 +03:00
|
|
|
setLayout(d->layout);
|
|
|
|
|
|
|
|
d->treewidget = new QTreeWidget(this);
|
2023-08-25 20:33:54 +03:00
|
|
|
d->treewidget->setColumnCount(4);
|
|
|
|
QStringList treeheaders = QStringList()
|
|
|
|
<< i18n("Event")
|
|
|
|
<< i18n("Popup")
|
|
|
|
<< i18n("Sound")
|
|
|
|
<< i18n("Taskbar");
|
|
|
|
d->treewidget->setHeaderLabels(treeheaders);
|
|
|
|
d->treewidget->setRootIsDecorated(false);
|
|
|
|
connect(
|
|
|
|
d->treewidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
|
|
|
|
this, SLOT(_k_slotItemChanged(QTreeWidgetItem*,int))
|
|
|
|
);
|
|
|
|
d->layout->addWidget(d->treewidget);
|
|
|
|
|
|
|
|
setNotification(notification);
|
2023-08-24 19:53:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
KNotificationConfigWidget::~KNotificationConfigWidget()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2023-08-25 19:11:07 +03:00
|
|
|
void KNotificationConfigWidget::save()
|
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
emit changed(false);
|
|
|
|
}
|
|
|
|
|
2023-08-25 20:33:54 +03:00
|
|
|
void KNotificationConfigWidget::setNotification(const QString ¬ification)
|
|
|
|
{
|
|
|
|
d->treewidget->clear();
|
|
|
|
|
|
|
|
KConfig* notificationconfig = d->configs.value(notification, nullptr);
|
|
|
|
if (!notificationconfig) {
|
|
|
|
const QString notifyconfig = KStandardDirs::locate("config", "notifications/" + notification+ ".notifyrc");
|
|
|
|
if (notifyconfig.isEmpty()) {
|
|
|
|
kWarning() << "invalid notification" << notification;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
notificationconfig = new KConfig(notifyconfig, KConfig::NoGlobals);
|
|
|
|
d->configs.insert(notification, notificationconfig);
|
|
|
|
}
|
|
|
|
KConfigGroup globalgroupconfig(notificationconfig, notification);
|
|
|
|
KIcon soundicon = KIcon("media-playback-start");
|
|
|
|
foreach (const QString &eventgroup, notificationconfig->groupList()) {
|
|
|
|
if (!eventgroup.contains(QLatin1Char('/'))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
KConfigGroup eventgroupconfig(notificationconfig, eventgroup);
|
|
|
|
QString eventtext = eventgroupconfig.readEntry("Name");
|
|
|
|
if (eventtext.isEmpty()) {
|
|
|
|
eventtext = globalgroupconfig.readEntry("Name");
|
|
|
|
}
|
|
|
|
QString eventicon = eventgroupconfig.readEntry("IconName");
|
|
|
|
if (eventicon.isEmpty()) {
|
|
|
|
eventicon = globalgroupconfig.readEntry("IconName");
|
|
|
|
}
|
|
|
|
QStringList eventactions = eventgroupconfig.readEntry("Actions", QStringList());
|
|
|
|
if (eventactions.isEmpty()) {
|
|
|
|
eventactions = globalgroupconfig.readEntry("Actions", QStringList());
|
|
|
|
}
|
|
|
|
const bool eventpopup = eventactions.contains(QString::fromLatin1("Popup"));
|
|
|
|
const bool eventsound = eventactions.contains(QString::fromLatin1("Sound"));
|
|
|
|
const bool eventtaskbar = eventactions.contains(QString::fromLatin1("Taskbar"));
|
|
|
|
QTreeWidgetItem* eventitem = new QTreeWidgetItem();
|
|
|
|
eventitem->setData(0, Qt::UserRole, eventgroup);
|
|
|
|
eventitem->setIcon(0, KIcon(eventicon));
|
|
|
|
eventitem->setText(0, eventtext);
|
|
|
|
eventitem->setText(1, eventpopup ? d->enabledi18n : d->disabledi18n);
|
|
|
|
eventitem->setCheckState(1, eventpopup ? Qt::Checked : Qt::Unchecked);
|
|
|
|
eventitem->setToolTip(1, d->popuptooltipi18n);
|
|
|
|
eventitem->setText(2, eventsound ? d->enabledi18n : d->disabledi18n);
|
|
|
|
eventitem->setCheckState(2, eventsound ? Qt::Checked : Qt::Unchecked);
|
|
|
|
eventitem->setToolTip(2, d->soundtooltipi18n);
|
|
|
|
eventitem->setText(3, eventtaskbar ? d->enabledi18n : d->disabledi18n);
|
|
|
|
eventitem->setCheckState(3, eventtaskbar ? Qt::Checked : Qt::Unchecked);
|
|
|
|
eventitem->setToolTip(3, d->taskbartooltipi18n);
|
|
|
|
d->treewidget->addTopLevelItem(eventitem);
|
|
|
|
}
|
|
|
|
d->treewidget->resizeColumnToContents(0);
|
|
|
|
d->treewidget->resizeColumnToContents(1);
|
|
|
|
}
|
|
|
|
|
2023-08-24 19:53:51 +03:00
|
|
|
void KNotificationConfigWidget::configure(const QString &app, QWidget *parent)
|
|
|
|
{
|
|
|
|
KDialog *dialog = new KDialog(parent);
|
|
|
|
dialog->setCaption(i18n("Configure Notifications"));
|
|
|
|
KNotificationConfigWidget *widget = new KNotificationConfigWidget(app, dialog);
|
|
|
|
dialog->setMainWidget(widget);
|
|
|
|
|
|
|
|
connect(dialog, SIGNAL(applyClicked()), widget, SLOT(save()));
|
|
|
|
connect(dialog, SIGNAL(okClicked()), widget, SLOT(save()));
|
|
|
|
connect(widget, SIGNAL(changed(bool)), dialog , SLOT(enableButtonApply(bool)));
|
|
|
|
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_knotificationconfigwidget.cpp"
|