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-26 02:07:56 +03:00
|
|
|
#include <QHeaderView>
|
2023-08-26 07:55:22 +03:00
|
|
|
#include <QComboBox>
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(QTreeWidgetItem*);
|
|
|
|
|
2023-08-28 18:36:14 +03:00
|
|
|
static const QStringList s_soundextensions = QStringList()
|
|
|
|
<< ".wav"
|
|
|
|
<< ".ogg"
|
|
|
|
<< ".oga";
|
|
|
|
|
2023-08-26 16:32:54 +03:00
|
|
|
class KNotificationConfigDialog : public KDialog
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
KNotificationConfigDialog(const QString ¬ification, QWidget *parent = nullptr);
|
|
|
|
~KNotificationConfigDialog();
|
|
|
|
};
|
|
|
|
|
|
|
|
KNotificationConfigDialog::KNotificationConfigDialog(const QString ¬ification, QWidget *parent)
|
|
|
|
: KDialog(parent, 0)
|
|
|
|
{
|
|
|
|
setCaption(i18n("Configure Notifications"));
|
|
|
|
KNotificationConfigWidget *widget = new KNotificationConfigWidget(notification, this);
|
|
|
|
setMainWidget(widget);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(applyClicked()), widget, SLOT(save()));
|
|
|
|
connect(this, SIGNAL(okClicked()), widget, SLOT(save()));
|
|
|
|
connect(widget, SIGNAL(changed(bool)), this , SLOT(enableButtonApply(bool)));
|
|
|
|
|
2023-08-26 22:08:56 +03:00
|
|
|
setInitialSize(QSize(620, 310));
|
2023-08-26 16:32:54 +03:00
|
|
|
KConfigGroup kconfiggroup(KGlobal::config(), "KNotificationConfigDialog");
|
|
|
|
restoreDialogSize(kconfiggroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
KNotificationConfigDialog::~KNotificationConfigDialog()
|
|
|
|
{
|
|
|
|
KConfigGroup kconfiggroup(KGlobal::config(), "KNotificationConfigDialog");
|
|
|
|
saveDialogSize(kconfiggroup);
|
|
|
|
KGlobal::config()->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-26 07:55:22 +03:00
|
|
|
struct KNotificationChanges
|
|
|
|
{
|
|
|
|
QStringList eventactions;
|
|
|
|
QString eventsound;
|
|
|
|
};
|
2023-08-25 19:11:07 +03:00
|
|
|
|
2023-08-26 16:32:54 +03:00
|
|
|
|
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
|
|
|
void _k_slotItemChanged(QTreeWidgetItem *item, int column);
|
2023-08-26 07:55:22 +03:00
|
|
|
void _k_slotSoundChanged(int index);
|
2023-08-25 19:11:07 +03:00
|
|
|
|
|
|
|
KNotificationConfigWidget* parent;
|
2023-08-25 20:33:54 +03:00
|
|
|
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-26 07:55:22 +03:00
|
|
|
QMap<QString,KNotificationChanges> notificationchanges;
|
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");
|
2023-08-25 23:48:26 +03:00
|
|
|
taskbartooltipi18n = i18n("Mark taskbar entry");
|
2023-08-24 19:53:51 +03:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:33:54 +03:00
|
|
|
void KNotificationConfigWidgetPrivate::_k_slotItemChanged(QTreeWidgetItem *item, int column)
|
|
|
|
{
|
|
|
|
emit parent->changed(true);
|
2023-08-25 23:12:14 +03:00
|
|
|
if (!item) {
|
|
|
|
kWarning() << "null tree item";
|
|
|
|
return;
|
|
|
|
}
|
2023-08-26 07:55:22 +03:00
|
|
|
if (column == 1 || column == 3) {
|
|
|
|
item->setText(column, item->checkState(column) == Qt::Checked ? enabledi18n : disabledi18n);
|
2023-08-25 20:33:54 +03:00
|
|
|
}
|
2023-08-25 23:12:14 +03:00
|
|
|
const QString eventgroup = item->data(0, Qt::UserRole).toString();
|
|
|
|
QStringList eventactions;
|
2023-08-26 04:11:05 +03:00
|
|
|
const bool eventactionpopup = (item->checkState(1) == Qt::Checked);
|
2023-08-26 07:55:22 +03:00
|
|
|
const QComboBox* eventbox = qobject_cast<QComboBox*>(item->treeWidget()->itemWidget(item, 2));
|
|
|
|
Q_ASSERT(eventbox != nullptr);
|
|
|
|
const bool eventactionsound = (eventbox->currentIndex() != 0);
|
2023-08-26 04:11:05 +03:00
|
|
|
const bool eventactiontaskbar = (item->checkState(3) == Qt::Checked);
|
|
|
|
if (eventactionpopup) {
|
2023-08-25 23:12:14 +03:00
|
|
|
eventactions.append(QString::fromLatin1("Popup"));
|
|
|
|
}
|
2023-08-26 04:11:05 +03:00
|
|
|
if (eventactionsound) {
|
2023-08-25 23:12:14 +03:00
|
|
|
eventactions.append(QString::fromLatin1("Sound"));
|
|
|
|
}
|
2023-08-26 04:11:05 +03:00
|
|
|
if (eventactiontaskbar) {
|
2023-08-25 23:12:14 +03:00
|
|
|
eventactions.append(QString::fromLatin1("Taskbar"));
|
|
|
|
}
|
2023-08-26 07:55:22 +03:00
|
|
|
KNotificationChanges eventchanges;
|
|
|
|
eventchanges.eventactions = eventactions;
|
|
|
|
eventchanges.eventsound = eventbox->itemData(eventbox->currentIndex()).toString();
|
|
|
|
notificationchanges.insert(eventgroup, eventchanges);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KNotificationConfigWidgetPrivate::_k_slotSoundChanged(int index)
|
|
|
|
{
|
|
|
|
QComboBox* eventbox = qobject_cast<QComboBox*>(parent->sender());
|
|
|
|
if (index != 0) {
|
|
|
|
// update the current sound data
|
|
|
|
eventbox->setItemData(0, eventbox->itemText(index));
|
|
|
|
}
|
|
|
|
// and trigger item changes
|
|
|
|
QTreeWidgetItem* eventitem = qvariant_cast<QTreeWidgetItem*>(eventbox->property("_k_eventitem"));
|
|
|
|
_k_slotItemChanged(eventitem, 2);
|
2023-08-25 20:33:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2023-08-26 08:31:28 +03:00
|
|
|
d->treewidget->header()->setStretchLastSection(false);
|
|
|
|
d->treewidget->header()->setResizeMode(0, QHeaderView::Stretch);
|
|
|
|
d->treewidget->header()->setResizeMode(2, QHeaderView::Stretch);
|
2023-08-25 20:33:54 +03:00
|
|
|
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()
|
|
|
|
{
|
2023-08-25 23:12:14 +03:00
|
|
|
KConfig notificationsconfig("knotificationrc", KConfig::NoGlobals);
|
2023-08-26 07:55:22 +03:00
|
|
|
QMapIterator<QString,KNotificationChanges> iter(d->notificationchanges);
|
2023-08-25 23:12:14 +03:00
|
|
|
while (iter.hasNext()) {
|
|
|
|
iter.next();
|
|
|
|
KConfigGroup eventgroupconfig(¬ificationsconfig, iter.key());
|
2023-08-26 17:15:15 +03:00
|
|
|
const KNotificationChanges eventchanges = iter.value();
|
2023-08-26 07:55:22 +03:00
|
|
|
eventgroupconfig.writeEntry("Actions", eventchanges.eventactions);
|
|
|
|
eventgroupconfig.writeEntry("Sound", eventchanges.eventsound);
|
2023-08-25 23:12:14 +03:00
|
|
|
}
|
|
|
|
notificationsconfig.sync();
|
|
|
|
d->notificationchanges.clear();
|
2023-08-25 19:11:07 +03:00
|
|
|
emit changed(false);
|
|
|
|
}
|
|
|
|
|
2023-08-25 20:33:54 +03:00
|
|
|
void KNotificationConfigWidget::setNotification(const QString ¬ification)
|
|
|
|
{
|
|
|
|
d->treewidget->clear();
|
|
|
|
|
2023-08-26 17:15:15 +03:00
|
|
|
const QString notifyconfig = KStandardDirs::locate("config", "notifications/" + notification + ".notifyrc");
|
2023-08-25 23:12:14 +03:00
|
|
|
if (notifyconfig.isEmpty()) {
|
|
|
|
kWarning() << "invalid notification" << notification;
|
|
|
|
return;
|
2023-08-25 20:33:54 +03:00
|
|
|
}
|
2023-08-26 07:55:22 +03:00
|
|
|
|
2023-08-26 16:25:09 +03:00
|
|
|
QStringList sounds = KGlobal::dirs()->findAllResources("sound", "*", KStandardDirs::Recursive);
|
|
|
|
sounds.sort();
|
2023-08-28 18:36:14 +03:00
|
|
|
QMutableListIterator<QString> iter(sounds);
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
const QString soundfile = iter.next();
|
|
|
|
bool isvalid = false;
|
|
|
|
foreach (const QString &soundextension, s_soundextensions) {
|
|
|
|
if (soundfile.endsWith(soundextension)) {
|
|
|
|
isvalid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isvalid) {
|
|
|
|
// could be a theme.index file
|
|
|
|
kDebug() << "unsupported sound file" << soundfile;
|
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 23:12:14 +03:00
|
|
|
KConfig notificationconfig("knotificationrc", KConfig::NoGlobals);
|
|
|
|
notificationconfig.addConfigSources(QStringList() << notifyconfig);
|
|
|
|
KConfigGroup globalgroupconfig(¬ificationconfig, notification);
|
|
|
|
foreach (const QString &eventgroup, notificationconfig.groupList()) {
|
|
|
|
if (!eventgroup.startsWith(notification + QLatin1Char('/'))) {
|
2023-08-25 20:33:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-25 23:12:14 +03:00
|
|
|
KConfigGroup eventgroupconfig(¬ificationconfig, eventgroup);
|
2023-08-25 20:33:54 +03:00
|
|
|
QString eventtext = eventgroupconfig.readEntry("Name");
|
|
|
|
if (eventtext.isEmpty()) {
|
|
|
|
eventtext = globalgroupconfig.readEntry("Name");
|
|
|
|
}
|
|
|
|
QString eventicon = eventgroupconfig.readEntry("IconName");
|
|
|
|
if (eventicon.isEmpty()) {
|
|
|
|
eventicon = globalgroupconfig.readEntry("IconName");
|
|
|
|
}
|
2023-08-26 04:11:05 +03:00
|
|
|
QString eventsound = eventgroupconfig.readEntry("Sound");
|
|
|
|
if (eventsound.isEmpty()) {
|
|
|
|
eventsound = globalgroupconfig.readEntry("Sound");
|
|
|
|
}
|
2023-08-25 20:33:54 +03:00
|
|
|
QStringList eventactions = eventgroupconfig.readEntry("Actions", QStringList());
|
|
|
|
if (eventactions.isEmpty()) {
|
|
|
|
eventactions = globalgroupconfig.readEntry("Actions", QStringList());
|
|
|
|
}
|
2023-08-26 04:11:05 +03:00
|
|
|
const bool eventactionpopup = eventactions.contains(QString::fromLatin1("Popup"));
|
|
|
|
const bool eventactionsound = eventactions.contains(QString::fromLatin1("Sound"));
|
|
|
|
const bool eventactiontaskbar = eventactions.contains(QString::fromLatin1("Taskbar"));
|
2023-08-25 20:33:54 +03:00
|
|
|
QTreeWidgetItem* eventitem = new QTreeWidgetItem();
|
|
|
|
eventitem->setData(0, Qt::UserRole, eventgroup);
|
|
|
|
eventitem->setIcon(0, KIcon(eventicon));
|
|
|
|
eventitem->setText(0, eventtext);
|
2023-08-26 04:11:05 +03:00
|
|
|
eventitem->setText(1, eventactionpopup ? d->enabledi18n : d->disabledi18n);
|
|
|
|
eventitem->setCheckState(1, eventactionpopup ? Qt::Checked : Qt::Unchecked);
|
2023-08-25 20:33:54 +03:00
|
|
|
eventitem->setToolTip(1, d->popuptooltipi18n);
|
2023-08-26 07:55:22 +03:00
|
|
|
eventitem->setToolTip(2, d->soundtooltipi18n);
|
2023-08-26 04:11:05 +03:00
|
|
|
eventitem->setText(3, eventactiontaskbar ? d->enabledi18n : d->disabledi18n);
|
|
|
|
eventitem->setCheckState(3, eventactiontaskbar ? Qt::Checked : Qt::Unchecked);
|
2023-08-25 20:33:54 +03:00
|
|
|
eventitem->setToolTip(3, d->taskbartooltipi18n);
|
|
|
|
d->treewidget->addTopLevelItem(eventitem);
|
2023-08-26 07:55:22 +03:00
|
|
|
QComboBox* eventbox = new QComboBox(d->treewidget);
|
|
|
|
eventbox->setProperty("_k_eventitem", QVariant::fromValue(eventitem));
|
|
|
|
foreach (const QString &sound, sounds) {
|
|
|
|
const QString soundfilename = QFileInfo(sound).fileName();
|
|
|
|
eventbox->addItem(soundfilename, soundfilename);
|
|
|
|
}
|
|
|
|
if (eventactionsound) {
|
|
|
|
// lookup has to be done before inserting the "Disabled" item with the current sound
|
|
|
|
// data
|
|
|
|
const int eventsoundindex = eventbox->findData(eventsound);
|
|
|
|
if (eventsoundindex >= 0) {
|
|
|
|
eventbox->setCurrentIndex(eventsoundindex);
|
|
|
|
} else {
|
|
|
|
kWarning() << "event sound not found" << eventsound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eventbox->insertItem(0, d->disabledi18n, eventsound);
|
|
|
|
if (!eventactionsound) {
|
|
|
|
eventbox->setCurrentIndex(0);
|
|
|
|
}
|
|
|
|
connect(eventbox, SIGNAL(currentIndexChanged(int)), this, SLOT(_k_slotSoundChanged(int)));
|
|
|
|
d->treewidget->setItemWidget(eventitem, 2, eventbox);
|
2023-08-25 20:33:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 16:32:54 +03:00
|
|
|
void KNotificationConfigWidget::configure(const QString ¬ification, QWidget *parent)
|
2023-08-24 19:53:51 +03:00
|
|
|
{
|
2023-08-26 16:32:54 +03:00
|
|
|
KNotificationConfigDialog *dialog = new KNotificationConfigDialog(notification, parent);
|
2023-08-24 19:53:51 +03:00
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_knotificationconfigwidget.cpp"
|
2023-08-26 16:32:54 +03:00
|
|
|
#include "knotificationconfigwidget.moc"
|