Changeset View
Changeset View
Standalone View
Standalone View
kcms/notifications/kcm.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "kcm.h" | ||||
22 | | ||||
23 | #include <QCommandLineParser> | ||||
24 | #include <QDialog> | ||||
25 | #include <QDialogButtonBox> | ||||
26 | #include <QPushButton> | ||||
27 | #include <QQuickItem> | ||||
28 | #include <QQuickRenderControl> | ||||
29 | #include <QQuickWindow> | ||||
30 | #include <QStandardPaths> | ||||
31 | #include <QVBoxLayout> | ||||
32 | #include <QWindow> | ||||
33 | | ||||
34 | #include <KAboutData> | ||||
35 | #include <KConfigGroup> | ||||
36 | #include <KLocalizedString> | ||||
37 | #include <KNotifyConfigWidget> | ||||
38 | #include <KPluginFactory> | ||||
39 | | ||||
40 | #include <algorithm> | ||||
41 | | ||||
42 | #include "sourcesmodel.h" | ||||
43 | #include "filterproxymodel.h" | ||||
44 | | ||||
45 | #include <notificationmanager/settings.h> | ||||
46 | | ||||
47 | K_PLUGIN_FACTORY_WITH_JSON(KCMNotificationsFactory, "kcm_notifications.json", registerPlugin<KCMNotifications>();) | ||||
48 | | ||||
49 | KCMNotifications::KCMNotifications(QObject *parent, const QVariantList &args) | ||||
50 | : KQuickAddons::ConfigModule(parent, args) | ||||
51 | , m_sourcesModel(new SourcesModel(this)) | ||||
52 | , m_filteredModel(new FilterProxyModel(this)) | ||||
53 | , m_settings(new NotificationManager::Settings(this)) | ||||
54 | { | ||||
55 | | ||||
56 | const char uri[] = "org.kde.private.kcms.notifications"; | ||||
57 | qmlRegisterUncreatableType<SourcesModel>(uri, 1, 0, "SourcesModel", | ||||
58 | QStringLiteral("Cannot create instances of SourcesModel")); | ||||
59 | qmlRegisterType<FilterProxyModel>(); | ||||
60 | qmlProtectModule(uri, 1); | ||||
61 | | ||||
62 | KAboutData *about = new KAboutData(QStringLiteral("kcm_notifications"), i18n("Notifications"), | ||||
63 | QStringLiteral("5.0"), QString(), KAboutLicense::GPL); | ||||
64 | about->addAuthor(i18n("Kai Uwe Broulik"), QString(), QStringLiteral("kde@privat.broulik.de")); | ||||
65 | setAboutData(about); | ||||
66 | | ||||
67 | m_filteredModel->setSourceModel(m_sourcesModel); | ||||
68 | | ||||
69 | QStringList stringArgs; | ||||
70 | stringArgs.reserve(args.count() + 1); | ||||
71 | // need to add a fake argv[0] for QCommandLineParser | ||||
72 | stringArgs.append(QStringLiteral("kcm_notifications")); | ||||
73 | for (const QVariant &arg : args) { | ||||
74 | stringArgs.append(arg.toString()); | ||||
75 | } | ||||
76 | | ||||
77 | QCommandLineParser parser; | ||||
78 | | ||||
79 | QCommandLineOption desktopEntryOption(QStringLiteral("desktop-entry"), QString(), QStringLiteral("desktop-entry")); | ||||
80 | parser.addOption(desktopEntryOption); | ||||
81 | QCommandLineOption notifyRcNameOption(QStringLiteral("notifyrc"), QString(), QStringLiteral("notifyrcname")); | ||||
82 | parser.addOption(notifyRcNameOption); | ||||
83 | QCommandLineOption eventIdOption(QStringLiteral("event-id"), QString(), QStringLiteral("event-id")); | ||||
84 | parser.addOption(eventIdOption); | ||||
85 | | ||||
86 | parser.parse(stringArgs); | ||||
87 | | ||||
88 | setInitialDesktopEntry(parser.value(desktopEntryOption)); | ||||
89 | setInitialNotifyRcName(parser.value(notifyRcNameOption)); | ||||
90 | setInitialEventId(parser.value(eventIdOption)); | ||||
91 | } | ||||
92 | | ||||
93 | KCMNotifications::~KCMNotifications() | ||||
94 | { | ||||
95 | | ||||
96 | } | ||||
97 | | ||||
98 | SourcesModel *KCMNotifications::sourcesModel() const | ||||
99 | { | ||||
100 | return m_sourcesModel; | ||||
101 | } | ||||
102 | | ||||
103 | FilterProxyModel *KCMNotifications::filteredModel() const | ||||
104 | { | ||||
105 | return m_filteredModel; | ||||
106 | } | ||||
107 | | ||||
108 | NotificationManager::Settings *KCMNotifications::settings() const | ||||
109 | { | ||||
110 | return m_settings; | ||||
111 | } | ||||
112 | | ||||
113 | QString KCMNotifications::initialDesktopEntry() const | ||||
114 | { | ||||
115 | return m_initialDesktopEntry; | ||||
116 | } | ||||
117 | | ||||
118 | void KCMNotifications::setInitialDesktopEntry(const QString &desktopEntry) | ||||
119 | { | ||||
120 | if (m_initialDesktopEntry != desktopEntry) { | ||||
121 | m_initialDesktopEntry = desktopEntry; | ||||
122 | emit initialDesktopEntryChanged(); | ||||
123 | } | ||||
124 | } | ||||
125 | | ||||
126 | QString KCMNotifications::initialNotifyRcName() const | ||||
127 | { | ||||
128 | return m_initialNotifyRcName; | ||||
129 | } | ||||
130 | | ||||
131 | void KCMNotifications::setInitialNotifyRcName(const QString ¬ifyRcName) | ||||
132 | { | ||||
133 | if (m_initialNotifyRcName != notifyRcName) { | ||||
134 | m_initialNotifyRcName = notifyRcName; | ||||
135 | emit initialNotifyRcNameChanged(); | ||||
136 | } | ||||
137 | } | ||||
138 | | ||||
139 | QString KCMNotifications::initialEventId() const | ||||
140 | { | ||||
141 | return m_initialEventId; | ||||
142 | } | ||||
143 | | ||||
144 | void KCMNotifications::setInitialEventId(const QString &eventId) | ||||
145 | { | ||||
146 | if (m_initialEventId != eventId) { | ||||
147 | m_initialEventId = eventId; | ||||
148 | emit initialEventIdChanged(); | ||||
149 | } | ||||
150 | } | ||||
151 | | ||||
152 | void KCMNotifications::configureEvents(const QString ¬ifyRcName, const QString &eventId, QQuickItem *ctx) | ||||
153 | { | ||||
154 | // We're not using KNotifyConfigWidget::configure here as we want to handle the | ||||
155 | // saving ourself (so we Apply with all other KCM settings) but there's no way | ||||
156 | // to access the config object :( | ||||
157 | // We also need access to the QDialog so we can set the KCM as transient parent. | ||||
158 | | ||||
159 | QDialog *dialog = new QDialog(nullptr); | ||||
160 | dialog->setAttribute(Qt::WA_DeleteOnClose); | ||||
161 | dialog->setWindowTitle(i18n("Configure Notifications")); | ||||
162 | | ||||
163 | if (ctx && ctx->window()) { | ||||
164 | dialog->winId(); // so it creates windowHandle | ||||
165 | dialog->windowHandle()->setTransientParent(QQuickRenderControl::renderWindowFor(ctx->window())); | ||||
166 | dialog->setModal(true); | ||||
167 | } | ||||
168 | | ||||
169 | KNotifyConfigWidget *w = new KNotifyConfigWidget(dialog); | ||||
170 | | ||||
171 | QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog); | ||||
172 | buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel); | ||||
173 | buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); | ||||
174 | | ||||
175 | QVBoxLayout *layout = new QVBoxLayout; | ||||
176 | layout->addWidget(w); | ||||
177 | layout->addWidget(buttonBox); | ||||
178 | dialog->setLayout(layout); | ||||
179 | | ||||
180 | // TODO we should only save settings when clicking Apply in the main UI | ||||
181 | connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, w, &KNotifyConfigWidget::save); | ||||
182 | connect(buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, w, &KNotifyConfigWidget::save); | ||||
183 | connect(w, &KNotifyConfigWidget::changed, buttonBox->button(QDialogButtonBox::Apply), &QPushButton::setEnabled); | ||||
184 | | ||||
185 | connect(buttonBox, &QDialogButtonBox::accepted, dialog, &QDialog::accept); | ||||
186 | connect(buttonBox, &QDialogButtonBox::rejected, dialog, &QDialog::reject); | ||||
187 | | ||||
188 | w->setApplication(notifyRcName); | ||||
189 | w->selectEvent(eventId); | ||||
190 | | ||||
191 | dialog->show(); | ||||
192 | } | ||||
193 | | ||||
194 | void KCMNotifications::load() | ||||
195 | { | ||||
196 | m_settings->load(); | ||||
197 | } | ||||
198 | | ||||
199 | void KCMNotifications::save() | ||||
200 | { | ||||
201 | m_settings->save(); | ||||
202 | } | ||||
203 | | ||||
204 | void KCMNotifications::defaults() | ||||
205 | { | ||||
206 | m_settings->defaults(); | ||||
207 | } | ||||
208 | | ||||
209 | #include "kcm.moc" |