Changeset View
Changeset View
Standalone View
Standalone View
kcms/notifications/kcm.cpp
Show All 15 Lines | |||||
16 | * | 16 | * | ||
17 | * You should have received a copy of the GNU General Public License | 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/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "kcm.h" | 21 | #include "kcm.h" | ||
22 | 22 | | |||
23 | #include <QCommandLineParser> | 23 | #include <QCommandLineParser> | ||
24 | #include <QDBusConnection> | ||||
25 | #include <QDBusConnectionInterface> | ||||
26 | #include <QDBusMessage> | ||||
27 | #include <QDBusPendingCallWatcher> | ||||
28 | #include <QDBusPendingReply> | ||||
29 | #include <QDBusServiceWatcher> | ||||
24 | #include <QDialog> | 30 | #include <QDialog> | ||
25 | #include <QDialogButtonBox> | 31 | #include <QDialogButtonBox> | ||
26 | #include <QPushButton> | 32 | #include <QPushButton> | ||
27 | #include <QQuickItem> | 33 | #include <QQuickItem> | ||
28 | #include <QQuickRenderControl> | 34 | #include <QQuickRenderControl> | ||
29 | #include <QQuickWindow> | 35 | #include <QQuickWindow> | ||
30 | #include <QStandardPaths> | 36 | #include <QStandardPaths> | ||
31 | #include <QVBoxLayout> | 37 | #include <QVBoxLayout> | ||
32 | #include <QWindow> | 38 | #include <QWindow> | ||
33 | 39 | | |||
34 | #include <KAboutData> | 40 | #include <KAboutData> | ||
35 | #include <KConfigGroup> | 41 | #include <KConfigGroup> | ||
36 | #include <KLocalizedString> | 42 | #include <KLocalizedString> | ||
37 | #include <KNotifyConfigWidget> | 43 | #include <KNotifyConfigWidget> | ||
38 | #include <KPluginFactory> | 44 | #include <KPluginFactory> | ||
39 | 45 | | |||
40 | #include <algorithm> | 46 | #include <algorithm> | ||
41 | 47 | | |||
42 | #include "sourcesmodel.h" | 48 | #include "sourcesmodel.h" | ||
43 | #include "filterproxymodel.h" | 49 | #include "filterproxymodel.h" | ||
44 | 50 | | |||
45 | #include <notificationmanager/settings.h> | 51 | #include <notificationmanager/settings.h> | ||
46 | 52 | | |||
53 | static QString s_notificationServiceName = QStringLiteral("org.freedesktop.Notifications"); | ||||
54 | | ||||
47 | K_PLUGIN_FACTORY_WITH_JSON(KCMNotificationsFactory, "kcm_notifications.json", registerPlugin<KCMNotifications>();) | 55 | K_PLUGIN_FACTORY_WITH_JSON(KCMNotificationsFactory, "kcm_notifications.json", registerPlugin<KCMNotifications>();) | ||
48 | 56 | | |||
49 | KCMNotifications::KCMNotifications(QObject *parent, const QVariantList &args) | 57 | KCMNotifications::KCMNotifications(QObject *parent, const QVariantList &args) | ||
50 | : KQuickAddons::ConfigModule(parent, args) | 58 | : KQuickAddons::ConfigModule(parent, args) | ||
51 | , m_sourcesModel(new SourcesModel(this)) | 59 | , m_sourcesModel(new SourcesModel(this)) | ||
52 | , m_filteredModel(new FilterProxyModel(this)) | 60 | , m_filteredModel(new FilterProxyModel(this)) | ||
53 | , m_settings(new NotificationManager::Settings(this)) | 61 | , m_settings(new NotificationManager::Settings(this)) | ||
54 | { | 62 | { | ||
55 | 63 | | |||
56 | const char uri[] = "org.kde.private.kcms.notifications"; | 64 | const char uri[] = "org.kde.private.kcms.notifications"; | ||
65 | qmlRegisterUncreatableType<KCMNotifications>(uri, 1, 0, "KCM", | ||||
66 | QStringLiteral("Cannot create instances of KCMNotifications")); | ||||
57 | qmlRegisterUncreatableType<SourcesModel>(uri, 1, 0, "SourcesModel", | 67 | qmlRegisterUncreatableType<SourcesModel>(uri, 1, 0, "SourcesModel", | ||
58 | QStringLiteral("Cannot create instances of SourcesModel")); | 68 | QStringLiteral("Cannot create instances of SourcesModel")); | ||
59 | qmlRegisterType<FilterProxyModel>(); | 69 | qmlRegisterType<FilterProxyModel>(); | ||
60 | qmlProtectModule(uri, 1); | 70 | qmlProtectModule(uri, 1); | ||
61 | 71 | | |||
62 | KAboutData *about = new KAboutData(QStringLiteral("kcm_notifications"), i18n("Notifications"), | 72 | KAboutData *about = new KAboutData(QStringLiteral("kcm_notifications"), i18n("Notifications"), | ||
63 | QStringLiteral("5.0"), QString(), KAboutLicense::GPL); | 73 | QStringLiteral("5.0"), QString(), KAboutLicense::GPL); | ||
64 | about->addAuthor(i18n("Kai Uwe Broulik"), QString(), QStringLiteral("kde@privat.broulik.de")); | 74 | about->addAuthor(i18n("Kai Uwe Broulik"), QString(), QStringLiteral("kde@privat.broulik.de")); | ||
Show All 18 Lines | |||||
83 | QCommandLineOption eventIdOption(QStringLiteral("event-id"), QString(), QStringLiteral("event-id")); | 93 | QCommandLineOption eventIdOption(QStringLiteral("event-id"), QString(), QStringLiteral("event-id")); | ||
84 | parser.addOption(eventIdOption); | 94 | parser.addOption(eventIdOption); | ||
85 | 95 | | |||
86 | parser.parse(stringArgs); | 96 | parser.parse(stringArgs); | ||
87 | 97 | | |||
88 | setInitialDesktopEntry(parser.value(desktopEntryOption)); | 98 | setInitialDesktopEntry(parser.value(desktopEntryOption)); | ||
89 | setInitialNotifyRcName(parser.value(notifyRcNameOption)); | 99 | setInitialNotifyRcName(parser.value(notifyRcNameOption)); | ||
90 | setInitialEventId(parser.value(eventIdOption)); | 100 | setInitialEventId(parser.value(eventIdOption)); | ||
101 | | ||||
102 | auto *watcher = new QDBusServiceWatcher(s_notificationServiceName, QDBusConnection::sessionBus(), | ||||
103 | QDBusServiceWatcher::WatchForOwnerChange, this); | ||||
104 | connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, [this]() { | ||||
105 | updateServerName(); | ||||
106 | }); | ||||
107 | | ||||
108 | updateServerName(); | ||||
91 | } | 109 | } | ||
92 | 110 | | |||
93 | KCMNotifications::~KCMNotifications() | 111 | KCMNotifications::~KCMNotifications() | ||
94 | { | 112 | { | ||
95 | 113 | | |||
96 | } | 114 | } | ||
97 | 115 | | |||
98 | SourcesModel *KCMNotifications::sourcesModel() const | 116 | SourcesModel *KCMNotifications::sourcesModel() const | ||
99 | { | 117 | { | ||
100 | return m_sourcesModel; | 118 | return m_sourcesModel; | ||
101 | } | 119 | } | ||
102 | 120 | | |||
103 | FilterProxyModel *KCMNotifications::filteredModel() const | 121 | FilterProxyModel *KCMNotifications::filteredModel() const | ||
104 | { | 122 | { | ||
105 | return m_filteredModel; | 123 | return m_filteredModel; | ||
106 | } | 124 | } | ||
107 | 125 | | |||
108 | NotificationManager::Settings *KCMNotifications::settings() const | 126 | NotificationManager::Settings *KCMNotifications::settings() const | ||
109 | { | 127 | { | ||
110 | return m_settings; | 128 | return m_settings; | ||
111 | } | 129 | } | ||
112 | 130 | | |||
131 | KCMNotifications::ServerStatus KCMNotifications::serverStatus() const | ||||
132 | { | ||||
133 | return m_serverStatus; | ||||
134 | } | ||||
135 | | ||||
136 | void KCMNotifications::setServerStatus(KCMNotifications::ServerStatus status) | ||||
137 | { | ||||
138 | if (m_serverStatus != status) { | ||||
139 | m_serverStatus = status; | ||||
140 | emit serverStatusChanged(); | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
144 | QString KCMNotifications::serverVendor() const | ||||
145 | { | ||||
146 | return m_serverVendor; | ||||
147 | } | ||||
148 | | ||||
149 | QString KCMNotifications::serverName() const | ||||
150 | { | ||||
151 | return m_serverName; | ||||
152 | } | ||||
153 | | ||||
154 | void KCMNotifications::setServerInformation(const QString &vendor, const QString &name) | ||||
155 | { | ||||
156 | if (m_serverVendor != vendor) { | ||||
157 | m_serverVendor = vendor; | ||||
158 | emit serverVendorChanged(); | ||||
159 | } | ||||
160 | if (m_serverName != name) { | ||||
161 | m_serverName = name; | ||||
162 | emit serverNameChanged(); | ||||
163 | } | ||||
164 | } | ||||
165 | | ||||
166 | void KCMNotifications::updateServerName() | ||||
167 | { | ||||
168 | // This explicit check is so we don't end up launching the notification service | ||||
169 | // and more importantly not get stuck on our own waitforname process. | ||||
170 | if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(s_notificationServiceName)) { | ||||
171 | setServerStatus(ServerStatus::NotRunning); | ||||
172 | setServerInformation(QString(), QString()); | ||||
173 | return; | ||||
174 | } | ||||
175 | | ||||
176 | QDBusMessage msg = QDBusMessage::createMethodCall(s_notificationServiceName, | ||||
177 | QStringLiteral("/org/freedesktop/Notifications"), | ||||
178 | QStringLiteral("org.freedesktop.Notifications"), | ||||
179 | QStringLiteral("GetServerInformation")); | ||||
180 | auto call = QDBusConnection::sessionBus().asyncCall(msg); | ||||
181 | | ||||
182 | auto *watcher = new QDBusPendingCallWatcher(call, this); | ||||
183 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) { | ||||
184 | QDBusPendingReply<QString, QString, QString, QString> reply = *watcher; | ||||
185 | watcher->deleteLater(); | ||||
186 | | ||||
187 | if (reply.isError()) { | ||||
188 | qWarning() << "Failed to determine notification server information" << reply.error().message(); | ||||
189 | setServerStatus(ServerStatus::Unknown); | ||||
190 | setServerInformation(QString(), QString()); | ||||
191 | return; | ||||
192 | } | ||||
193 | | ||||
194 | const QString name = reply.argumentAt(0).toString(); | ||||
195 | const QString vendor = reply.argumentAt(1).toString(); | ||||
196 | | ||||
197 | setServerInformation(vendor, name); | ||||
198 | setServerStatus(ServerStatus::Running); | ||||
199 | }); | ||||
200 | } | ||||
201 | | ||||
113 | QString KCMNotifications::initialDesktopEntry() const | 202 | QString KCMNotifications::initialDesktopEntry() const | ||
114 | { | 203 | { | ||
115 | return m_initialDesktopEntry; | 204 | return m_initialDesktopEntry; | ||
116 | } | 205 | } | ||
117 | 206 | | |||
118 | void KCMNotifications::setInitialDesktopEntry(const QString &desktopEntry) | 207 | void KCMNotifications::setInitialDesktopEntry(const QString &desktopEntry) | ||
119 | { | 208 | { | ||
120 | if (m_initialDesktopEntry != desktopEntry) { | 209 | if (m_initialDesktopEntry != desktopEntry) { | ||
▲ Show 20 Lines • Show All 89 Lines • Show Last 20 Lines |