File indexing completed on 2022-11-23 10:35:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2005 Olivier Goffart <ogoffart at kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "knotifyconfigwidget.h"
0009 #include "knotifyconfigactionswidget.h"
0010 #include "knotifyconfigelement.h"
0011 #include "knotifyeventlist.h"
0012 
0013 #include <QDBusConnectionInterface>
0014 #include <QDialog>
0015 #include <QDialogButtonBox>
0016 #include <QPushButton>
0017 #include <QVBoxLayout>
0018 
0019 #include <KLocalizedString>
0020 
0021 class KNotifyConfigWidgetPrivate
0022 {
0023 public:
0024     KNotifyEventList *eventList;
0025     KNotifyConfigActionsWidget *actionsconfig;
0026     KNotifyConfigElement *currentElement;
0027     QString application;
0028     QString contextName;
0029     QString contextValue;
0030 };
0031 
0032 KNotifyConfigWidget::KNotifyConfigWidget(QWidget *parent)
0033     : QWidget(parent)
0034     , d(new KNotifyConfigWidgetPrivate)
0035 {
0036     d->currentElement = nullptr;
0037     d->eventList = new KNotifyEventList(this);
0038     d->eventList->setFocus();
0039     d->actionsconfig = new KNotifyConfigActionsWidget(this);
0040     d->actionsconfig->setEnabled(false);
0041     connect(d->eventList, SIGNAL(eventSelected(KNotifyConfigElement *)), this, SLOT(slotEventSelected(KNotifyConfigElement *)));
0042     connect(d->actionsconfig, SIGNAL(changed()), this, SLOT(slotActionChanged()));
0043 
0044     QVBoxLayout *layout = new QVBoxLayout(this);
0045     layout->setContentsMargins(0, 0, 0, 0);
0046     layout->addWidget(d->eventList, 1);
0047     layout->addWidget(d->actionsconfig);
0048 }
0049 
0050 KNotifyConfigWidget::~KNotifyConfigWidget()
0051 {
0052     delete d;
0053 }
0054 
0055 void KNotifyConfigWidget::setApplication(const QString &app, const QString &context_name, const QString &context_value)
0056 {
0057     d->currentElement = nullptr;
0058     d->application = app.isEmpty() ? QCoreApplication::instance()->applicationName() : app;
0059     d->contextName = context_name;
0060     d->contextValue = context_value;
0061     d->eventList->fill(d->application, d->contextName, d->contextValue);
0062 }
0063 
0064 void KNotifyConfigWidget::selectEvent(const QString &eventId)
0065 {
0066     d->eventList->selectEvent(eventId);
0067 }
0068 
0069 void KNotifyConfigWidget::slotEventSelected(KNotifyConfigElement *e)
0070 {
0071     if (d->currentElement) {
0072         d->actionsconfig->save(d->currentElement);
0073     }
0074     d->currentElement = e;
0075     if (e) {
0076         d->actionsconfig->setConfigElement(e);
0077         d->actionsconfig->setEnabled(true);
0078     } else {
0079         d->actionsconfig->setEnabled(false);
0080     }
0081 }
0082 
0083 void KNotifyConfigWidget::save()
0084 {
0085     if (d->currentElement) {
0086         d->actionsconfig->save(d->currentElement);
0087     }
0088 
0089     d->eventList->save();
0090     Q_EMIT changed(false);
0091 
0092     // ask KNotification objects to reload their config
0093     QDBusMessage message =
0094         QDBusMessage::createSignal(QStringLiteral("/Config"), QStringLiteral("org.kde.knotification"), QStringLiteral("reparseConfiguration"));
0095     message.setArguments(QVariantList() << d->application);
0096     QDBusConnection::sessionBus().send(message);
0097 }
0098 
0099 void KNotifyConfigWidget::revertToDefaults()
0100 {
0101     d->eventList->fill(d->application, d->contextName, d->contextValue, true);
0102     Q_EMIT changed(true);
0103 }
0104 
0105 void KNotifyConfigWidget::disableAllSounds()
0106 {
0107     if (d->eventList->disableAllSounds()) {
0108         if (d->currentElement) {
0109             d->actionsconfig->setConfigElement(d->currentElement);
0110         }
0111         d->eventList->updateAllItems();
0112         Q_EMIT changed(true);
0113     }
0114 }
0115 
0116 KNotifyConfigWidget *KNotifyConfigWidget::configure(QWidget *parent, const QString &appname)
0117 {
0118     QDialog *dialog = new QDialog(parent);
0119     dialog->setWindowTitle(i18n("Configure Notifications"));
0120 
0121     KNotifyConfigWidget *w = new KNotifyConfigWidget(dialog);
0122 
0123     QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
0124     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel);
0125     buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
0126 
0127     QVBoxLayout *layout = new QVBoxLayout(dialog);
0128     layout->addWidget(w);
0129     layout->addWidget(buttonBox);
0130 
0131     connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), w, SLOT(save()));
0132     connect(buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), w, SLOT(save()));
0133     connect(w, SIGNAL(changed(bool)), buttonBox->button(QDialogButtonBox::Apply), SLOT(setEnabled(bool)));
0134 
0135     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
0136     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
0137 
0138     w->setApplication(appname);
0139     dialog->setAttribute(Qt::WA_DeleteOnClose);
0140     dialog->show();
0141     return w;
0142 }
0143 
0144 void KNotifyConfigWidget::slotActionChanged()
0145 {
0146     Q_EMIT changed(true); // TODO
0147     if (d->currentElement) {
0148         d->actionsconfig->save(d->currentElement);
0149         d->eventList->updateCurrentItem();
0150     }
0151 }