File indexing completed on 2024-04-21 03:56:30

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 };
0029 
0030 KNotifyConfigWidget::KNotifyConfigWidget(QWidget *parent)
0031     : QWidget(parent)
0032     , d(new KNotifyConfigWidgetPrivate)
0033 {
0034     d->currentElement = nullptr;
0035     d->eventList = new KNotifyEventList(this);
0036     d->eventList->setFocus();
0037     d->actionsconfig = new KNotifyConfigActionsWidget(this);
0038     d->actionsconfig->setEnabled(false);
0039     connect(d->eventList, SIGNAL(eventSelected(KNotifyConfigElement *)), this, SLOT(slotEventSelected(KNotifyConfigElement *)));
0040     connect(d->actionsconfig, SIGNAL(changed()), this, SLOT(slotActionChanged()));
0041 
0042     QVBoxLayout *layout = new QVBoxLayout(this);
0043     layout->setContentsMargins(0, 0, 0, 0);
0044     layout->addWidget(d->eventList, 1);
0045     layout->addWidget(d->actionsconfig);
0046 }
0047 
0048 KNotifyConfigWidget::~KNotifyConfigWidget() = default;
0049 
0050 void KNotifyConfigWidget::setApplication(const QString &app)
0051 {
0052     d->currentElement = nullptr;
0053     d->application = app.isEmpty() ? QCoreApplication::instance()->applicationName() : app;
0054     d->eventList->fill(d->application);
0055 }
0056 
0057 void KNotifyConfigWidget::selectEvent(const QString &eventId)
0058 {
0059     d->eventList->selectEvent(eventId);
0060 }
0061 
0062 void KNotifyConfigWidget::slotEventSelected(KNotifyConfigElement *e)
0063 {
0064     if (d->currentElement) {
0065         d->actionsconfig->save(d->currentElement);
0066     }
0067     d->currentElement = e;
0068     if (e) {
0069         d->actionsconfig->setConfigElement(e);
0070         d->actionsconfig->setEnabled(true);
0071     } else {
0072         d->actionsconfig->setEnabled(false);
0073     }
0074 }
0075 
0076 void KNotifyConfigWidget::save()
0077 {
0078     if (d->currentElement) {
0079         d->actionsconfig->save(d->currentElement);
0080     }
0081 
0082     d->eventList->save();
0083     Q_EMIT changed(false);
0084 
0085     // ask KNotification objects to reload their config
0086     QDBusMessage message =
0087         QDBusMessage::createSignal(QStringLiteral("/Config"), QStringLiteral("org.kde.knotification"), QStringLiteral("reparseConfiguration"));
0088     message.setArguments(QVariantList() << d->application);
0089     QDBusConnection::sessionBus().send(message);
0090 }
0091 
0092 void KNotifyConfigWidget::revertToDefaults()
0093 {
0094     d->eventList->fill(d->application, true);
0095     Q_EMIT changed(true);
0096 }
0097 
0098 void KNotifyConfigWidget::disableAllSounds()
0099 {
0100     if (d->eventList->disableAllSounds()) {
0101         if (d->currentElement) {
0102             d->actionsconfig->setConfigElement(d->currentElement);
0103         }
0104         d->eventList->updateAllItems();
0105         Q_EMIT changed(true);
0106     }
0107 }
0108 
0109 KNotifyConfigWidget *KNotifyConfigWidget::configure(QWidget *parent, const QString &appname)
0110 {
0111     QDialog *dialog = new QDialog(parent);
0112     dialog->setWindowTitle(i18n("Configure Notifications"));
0113 
0114     KNotifyConfigWidget *w = new KNotifyConfigWidget(dialog);
0115 
0116     QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
0117     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel);
0118     buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
0119 
0120     QVBoxLayout *layout = new QVBoxLayout(dialog);
0121     layout->addWidget(w);
0122     layout->addWidget(buttonBox);
0123 
0124     connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), w, SLOT(save()));
0125     connect(buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), w, SLOT(save()));
0126     connect(w, SIGNAL(changed(bool)), buttonBox->button(QDialogButtonBox::Apply), SLOT(setEnabled(bool)));
0127 
0128     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
0129     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
0130 
0131     w->setApplication(appname);
0132     dialog->setAttribute(Qt::WA_DeleteOnClose);
0133     dialog->show();
0134     return w;
0135 }
0136 
0137 void KNotifyConfigWidget::slotActionChanged()
0138 {
0139     Q_EMIT changed(true); // TODO
0140     if (d->currentElement) {
0141         d->actionsconfig->save(d->currentElement);
0142         d->eventList->updateCurrentItem();
0143     }
0144 }
0145 
0146 #include "moc_knotifyconfigwidget.cpp"