KNotifyConfig

knotifyconfigwidget.cpp
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 <QDBusConnectionInterface>
14 #include <QDialog>
15 #include <QDialogButtonBox>
16 #include <QPushButton>
17 #include <QVBoxLayout>
18 
19 #include <KLocalizedString>
20 
21 class KNotifyConfigWidgetPrivate
22 {
23 public:
24  KNotifyEventList *eventList;
25  KNotifyConfigActionsWidget *actionsconfig;
26  KNotifyConfigElement *currentElement;
27  QString application;
28  QString contextName;
29  QString contextValue;
30 };
31 
32 KNotifyConfigWidget::KNotifyConfigWidget(QWidget *parent)
33  : QWidget(parent)
34  , d(new KNotifyConfigWidgetPrivate)
35 {
36  d->currentElement = nullptr;
37  d->eventList = new KNotifyEventList(this);
38  d->eventList->setFocus();
39  d->actionsconfig = new KNotifyConfigActionsWidget(this);
40  d->actionsconfig->setEnabled(false);
41  connect(d->eventList, SIGNAL(eventSelected(KNotifyConfigElement *)), this, SLOT(slotEventSelected(KNotifyConfigElement *)));
42  connect(d->actionsconfig, SIGNAL(changed()), this, SLOT(slotActionChanged()));
43 
44  QVBoxLayout *layout = new QVBoxLayout(this);
45  layout->setContentsMargins(0, 0, 0, 0);
46  layout->addWidget(d->eventList, 1);
47  layout->addWidget(d->actionsconfig);
48 }
49 
50 KNotifyConfigWidget::~KNotifyConfigWidget()
51 {
52  delete d;
53 }
54 
55 void KNotifyConfigWidget::setApplication(const QString &app, const QString &context_name, const QString &context_value)
56 {
57  d->currentElement = nullptr;
58  d->application = app.isEmpty() ? QCoreApplication::instance()->applicationName() : app;
59  d->contextName = context_name;
60  d->contextValue = context_value;
61  d->eventList->fill(d->application, d->contextName, d->contextValue);
62 }
63 
65 {
66  d->eventList->selectEvent(eventId);
67 }
68 
69 void KNotifyConfigWidget::slotEventSelected(KNotifyConfigElement *e)
70 {
71  if (d->currentElement) {
72  d->actionsconfig->save(d->currentElement);
73  }
74  d->currentElement = e;
75  if (e) {
76  d->actionsconfig->setConfigElement(e);
77  d->actionsconfig->setEnabled(true);
78  } else {
79  d->actionsconfig->setEnabled(false);
80  }
81 }
82 
84 {
85  if (d->currentElement) {
86  d->actionsconfig->save(d->currentElement);
87  }
88 
89  d->eventList->save();
90  Q_EMIT changed(false);
91 
92  // ask KNotification objects to reload their config
93  QDBusMessage message =
94  QDBusMessage::createSignal(QStringLiteral("/Config"), QStringLiteral("org.kde.knotification"), QStringLiteral("reparseConfiguration"));
95  message.setArguments(QVariantList() << d->application);
97 }
98 
99 void KNotifyConfigWidget::revertToDefaults()
100 {
101  d->eventList->fill(d->application, d->contextName, d->contextValue, true);
102  Q_EMIT changed(true);
103 }
104 
105 void KNotifyConfigWidget::disableAllSounds()
106 {
107  if (d->eventList->disableAllSounds()) {
108  if (d->currentElement) {
109  d->actionsconfig->setConfigElement(d->currentElement);
110  }
111  d->eventList->updateAllItems();
112  Q_EMIT changed(true);
113  }
114 }
115 
117 {
118  QDialog *dialog = new QDialog(parent);
119  dialog->setWindowTitle(i18n("Configure Notifications"));
120 
121  KNotifyConfigWidget *w = new KNotifyConfigWidget(dialog);
122 
123  QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
125  buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
126 
127  QVBoxLayout *layout = new QVBoxLayout(dialog);
128  layout->addWidget(w);
129  layout->addWidget(buttonBox);
130 
131  connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), w, SLOT(save()));
132  connect(buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), w, SLOT(save()));
133  connect(w, SIGNAL(changed(bool)), buttonBox->button(QDialogButtonBox::Apply), SLOT(setEnabled(bool)));
134 
135  connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
136  connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
137 
138  w->setApplication(appname);
140  dialog->show();
141  return w;
142 }
143 
144 void KNotifyConfigWidget::slotActionChanged()
145 {
146  Q_EMIT changed(true); // TODO
147  if (d->currentElement) {
148  d->actionsconfig->save(d->currentElement);
149  d->eventList->updateCurrentItem();
150  }
151 }
Represent the config for an event.
void setContentsMargins(int left, int top, int right, int bottom)
QString & fill(QChar ch, int size)
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QDBusConnection sessionBus()
static KNotifyConfigWidget * configure(QWidget *parent=nullptr, const QString &appname=QString())
Show a dialog with the widget.
void setEnabled(bool)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
Represent the config for an event.
void save()
save to the config file
QDBusMessage createSignal(const QString &path, const QString &interface, const QString &name)
bool send(const QDBusMessage &message) const const
bool isEmpty() const const
Configure the notification for a given application / context.
QCoreApplication * instance()
WA_DeleteOnClose
QString i18n(const char *text, const TYPE &arg...)
void setStandardButtons(QDialogButtonBox::StandardButtons buttons)
void setWindowTitle(const QString &)
void setArguments(const QList< QVariant > &arguments)
QPushButton * button(QDialogButtonBox::StandardButton which) const const
void setApplication(const QString &appname=QString(), const QString &context_name=QString(), const QString &context_value=QString())
Change the application and the context.
void show()
void selectEvent(const QString &eventId)
Select a given notification in the current list.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 22:35:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.