| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2005 Olivier Goffart <ogoffart at kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only |
| 6 | */ |
| 7 | |
| 8 | #include "knotifyconfigwidget.h" |
| 9 | #include "knotifyconfigactionswidget.h" |
| 10 | #include "knotifyconfigelement.h" |
| 11 | #include "knotifyeventlist.h" |
| 12 | |
| 13 | #include <QDialog> |
| 14 | #include <QDialogButtonBox> |
| 15 | #include <QPushButton> |
| 16 | #include <QVBoxLayout> |
| 17 | |
| 18 | #ifdef HAVE_DBUS |
| 19 | #include <QDBusConnectionInterface> |
| 20 | #endif |
| 21 | |
| 22 | #include <KLocalizedString> |
| 23 | |
| 24 | class KNotifyConfigWidgetPrivate |
| 25 | { |
| 26 | public: |
| 27 | KNotifyEventList *eventList; |
| 28 | KNotifyConfigActionsWidget *actionsconfig; |
| 29 | KNotifyConfigElement *currentElement; |
| 30 | QString application; |
| 31 | }; |
| 32 | |
| 33 | KNotifyConfigWidget::KNotifyConfigWidget(QWidget *parent) |
| 34 | : QWidget(parent) |
| 35 | , d(new KNotifyConfigWidgetPrivate) |
| 36 | { |
| 37 | d->currentElement = nullptr; |
| 38 | d->eventList = new KNotifyEventList(this); |
| 39 | d->eventList->setFocus(); |
| 40 | d->actionsconfig = new KNotifyConfigActionsWidget(this); |
| 41 | d->actionsconfig->setEnabled(false); |
| 42 | connect(sender: d->eventList, SIGNAL(eventSelected(KNotifyConfigElement *)), receiver: this, SLOT(slotEventSelected(KNotifyConfigElement *))); |
| 43 | connect(sender: d->actionsconfig, SIGNAL(changed()), receiver: this, SLOT(slotActionChanged())); |
| 44 | |
| 45 | QVBoxLayout *layout = new QVBoxLayout(this); |
| 46 | layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
| 47 | layout->addWidget(d->eventList, stretch: 1); |
| 48 | layout->addWidget(d->actionsconfig); |
| 49 | } |
| 50 | |
| 51 | KNotifyConfigWidget::~KNotifyConfigWidget() = default; |
| 52 | |
| 53 | void KNotifyConfigWidget::setApplication(const QString &app) |
| 54 | { |
| 55 | d->currentElement = nullptr; |
| 56 | d->application = app.isEmpty() ? QCoreApplication::instance()->applicationName() : app; |
| 57 | d->eventList->fill(appname: d->application); |
| 58 | } |
| 59 | |
| 60 | void KNotifyConfigWidget::selectEvent(const QString &eventId) |
| 61 | { |
| 62 | d->eventList->selectEvent(eventId); |
| 63 | } |
| 64 | |
| 65 | void KNotifyConfigWidget::slotEventSelected(KNotifyConfigElement *e) |
| 66 | { |
| 67 | if (d->currentElement) { |
| 68 | d->actionsconfig->save(config: d->currentElement); |
| 69 | } |
| 70 | d->currentElement = e; |
| 71 | if (e) { |
| 72 | d->actionsconfig->setConfigElement(e); |
| 73 | d->actionsconfig->setEnabled(true); |
| 74 | } else { |
| 75 | d->actionsconfig->setEnabled(false); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void KNotifyConfigWidget::save() |
| 80 | { |
| 81 | if (d->currentElement) { |
| 82 | d->actionsconfig->save(config: d->currentElement); |
| 83 | } |
| 84 | |
| 85 | d->eventList->save(); |
| 86 | Q_EMIT changed(state: false); |
| 87 | |
| 88 | #ifdef HAVE_DBUS |
| 89 | // ask KNotification objects to reload their config |
| 90 | QDBusMessage message = |
| 91 | QDBusMessage::createSignal(QStringLiteral("/Config" ), QStringLiteral("org.kde.knotification" ), QStringLiteral("reparseConfiguration" )); |
| 92 | message.setArguments(QVariantList() << d->application); |
| 93 | QDBusConnection::sessionBus().send(message); |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | void KNotifyConfigWidget::revertToDefaults() |
| 98 | { |
| 99 | d->eventList->fill(appname: d->application, loadDefaults: true); |
| 100 | Q_EMIT changed(state: true); |
| 101 | } |
| 102 | |
| 103 | void KNotifyConfigWidget::disableAllSounds() |
| 104 | { |
| 105 | if (d->eventList->disableAllSounds()) { |
| 106 | if (d->currentElement) { |
| 107 | d->actionsconfig->setConfigElement(d->currentElement); |
| 108 | } |
| 109 | d->eventList->updateAllItems(); |
| 110 | Q_EMIT changed(state: true); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | KNotifyConfigWidget *KNotifyConfigWidget::configure(QWidget *parent, const QString &appname) |
| 115 | { |
| 116 | QDialog *dialog = new QDialog(parent); |
| 117 | dialog->setWindowTitle(i18n("Configure Notifications" )); |
| 118 | |
| 119 | KNotifyConfigWidget *w = new KNotifyConfigWidget(dialog); |
| 120 | |
| 121 | QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog); |
| 122 | buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel); |
| 123 | buttonBox->button(which: QDialogButtonBox::Apply)->setEnabled(false); |
| 124 | |
| 125 | QVBoxLayout *layout = new QVBoxLayout(dialog); |
| 126 | layout->addWidget(w); |
| 127 | layout->addWidget(buttonBox); |
| 128 | |
| 129 | connect(sender: buttonBox->button(which: QDialogButtonBox::Apply), SIGNAL(clicked()), receiver: w, SLOT(save())); |
| 130 | connect(sender: buttonBox->button(which: QDialogButtonBox::Ok), SIGNAL(clicked()), receiver: w, SLOT(save())); |
| 131 | connect(sender: w, SIGNAL(changed(bool)), receiver: buttonBox->button(which: QDialogButtonBox::Apply), SLOT(setEnabled(bool))); |
| 132 | |
| 133 | connect(sender: buttonBox, SIGNAL(accepted()), receiver: dialog, SLOT(accept())); |
| 134 | connect(sender: buttonBox, SIGNAL(rejected()), receiver: dialog, SLOT(reject())); |
| 135 | |
| 136 | w->setApplication(appname); |
| 137 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
| 138 | dialog->show(); |
| 139 | return w; |
| 140 | } |
| 141 | |
| 142 | void KNotifyConfigWidget::slotActionChanged() |
| 143 | { |
| 144 | Q_EMIT changed(state: true); // TODO |
| 145 | if (d->currentElement) { |
| 146 | d->actionsconfig->save(config: d->currentElement); |
| 147 | d->eventList->updateCurrentItem(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #include "moc_knotifyconfigwidget.cpp" |
| 152 | |