GCC Code Coverage Report


Directory: ./
File: panels/notifications/cc-notifications-panel.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 196 0.0%
Functions: 0 23 0.0%
Branches: 0 99 0.0%

Line Branch Exec Source
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include "config.h"
21
22 #include <adwaita.h>
23 #include <string.h>
24 #include <glib/gi18n-lib.h>
25 #include <glib.h>
26 #include <gio/gio.h>
27 #include <gio/gdesktopappinfo.h>
28
29 #include "cc-list-row.h"
30 #include "cc-notifications-panel.h"
31 #include "cc-notifications-resources.h"
32 #include "cc-app-notifications-dialog.h"
33
34 #define MASTER_SCHEMA "org.gnome.desktop.notifications"
35 #define APP_SCHEMA MASTER_SCHEMA ".application"
36 #define APP_PREFIX "/org/gnome/desktop/notifications/application/"
37
38 struct _CcNotificationsPanel {
39 CcPanel parent_instance;
40
41 GtkListBox *app_listbox;
42 AdwSwitchRow *lock_screen_row;
43 AdwSwitchRow *dnd_row;
44
45 GSettings *master_settings;
46
47 GCancellable *cancellable;
48
49 GHashTable *known_applications;
50
51 GDBusProxy *perm_store;
52 };
53
54 struct _CcNotificationsPanelClass {
55 CcPanelClass parent;
56 };
57
58 typedef struct {
59 char *canonical_app_id;
60 GAppInfo *app_info;
61 GSettings *settings;
62
63 /* Temporary pointer, to pass from the loading thread
64 to the app */
65 CcNotificationsPanel *self;
66 } Application;
67
68 static void build_app_store (CcNotificationsPanel *self);
69 static void select_app (CcNotificationsPanel *self, GtkListBoxRow *row);
70 static int sort_apps (gconstpointer one, gconstpointer two, gpointer user_data);
71
72 CC_PANEL_REGISTER (CcNotificationsPanel, cc_notifications_panel);
73
74 static gboolean
75 keynav_failed_cb (CcNotificationsPanel *self,
76 GtkDirectionType direction)
77 {
78 GtkWidget *toplevel = GTK_WIDGET (gtk_widget_get_root (GTK_WIDGET (self)));
79
80 if (!toplevel)
81 return FALSE;
82
83 if (direction != GTK_DIR_UP && direction != GTK_DIR_DOWN)
84 return FALSE;
85
86 return gtk_widget_child_focus (toplevel, direction == GTK_DIR_UP ?
87 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
88 }
89
90 static void
91 cc_notifications_panel_dispose (GObject *object)
92 {
93 CcNotificationsPanel *self = CC_NOTIFICATIONS_PANEL (object);
94
95 g_clear_object (&self->master_settings);
96 g_clear_pointer (&self->known_applications, g_hash_table_unref);
97
98 G_OBJECT_CLASS (cc_notifications_panel_parent_class)->dispose (object);
99 }
100
101 static void
102 cc_notifications_panel_finalize (GObject *object)
103 {
104 CcNotificationsPanel *self = CC_NOTIFICATIONS_PANEL (object);
105
106 g_clear_object (&self->perm_store);
107
108 G_OBJECT_CLASS (cc_notifications_panel_parent_class)->finalize (object);
109 }
110
111 static void
112 on_perm_store_ready (GObject *source_object,
113 GAsyncResult *res,
114 gpointer user_data)
115 {
116 CcNotificationsPanel *self;
117 GDBusProxy *proxy;
118 g_autoptr(GError) error = NULL;
119
120 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
121 if (proxy == NULL)
122 {
123 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
124 g_warning ("Failed to connect to xdg-app permission store: %s",
125 error->message);
126 return;
127 }
128 self = user_data;
129 self->perm_store = proxy;
130 }
131
132 static void
133 cc_notifications_panel_init (CcNotificationsPanel *self)
134 {
135 g_resources_register (cc_notifications_get_resource ());
136
137 gtk_widget_init_template (GTK_WIDGET (self));
138
139 self->known_applications = g_hash_table_new_full (g_str_hash, g_str_equal,
140 NULL, g_free);
141
142 self->master_settings = g_settings_new (MASTER_SCHEMA);
143
144 g_settings_bind (self->master_settings, "show-banners",
145 self->dnd_row,
146 "active", G_SETTINGS_BIND_INVERT_BOOLEAN);
147 g_settings_bind (self->master_settings, "show-in-lock-screen",
148 self->lock_screen_row,
149 "active", G_SETTINGS_BIND_DEFAULT);
150
151 gtk_list_box_set_sort_func (self->app_listbox, (GtkListBoxSortFunc)sort_apps, NULL, NULL);
152
153 build_app_store (self);
154
155 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
156 G_DBUS_PROXY_FLAGS_NONE,
157 NULL,
158 "org.freedesktop.impl.portal.PermissionStore",
159 "/org/freedesktop/impl/portal/PermissionStore",
160 "org.freedesktop.impl.portal.PermissionStore",
161 cc_panel_get_cancellable (CC_PANEL (self)),
162 on_perm_store_ready,
163 self);
164 }
165
166 static const char *
167 cc_notifications_panel_get_help_uri (CcPanel *panel)
168 {
169 return "help:gnome-help/shell-notifications";
170 }
171
172 static void
173 cc_notifications_panel_class_init (CcNotificationsPanelClass *klass)
174 {
175 GObjectClass *object_class = G_OBJECT_CLASS (klass);
176 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
177 CcPanelClass *panel_class = CC_PANEL_CLASS (klass);
178
179 panel_class->get_help_uri = cc_notifications_panel_get_help_uri;
180
181 /* Separate dispose() and finalize() functions are necessary
182 * to make sure we cancel the running thread before the panel
183 * gets finalized */
184 object_class->dispose = cc_notifications_panel_dispose;
185 object_class->finalize = cc_notifications_panel_finalize;
186
187 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/notifications/cc-notifications-panel.ui");
188
189 gtk_widget_class_bind_template_child (widget_class, CcNotificationsPanel, app_listbox);
190 gtk_widget_class_bind_template_child (widget_class, CcNotificationsPanel, lock_screen_row);
191 gtk_widget_class_bind_template_child (widget_class, CcNotificationsPanel, dnd_row);
192
193 gtk_widget_class_bind_template_callback (widget_class, select_app);
194 gtk_widget_class_bind_template_callback (widget_class, keynav_failed_cb);
195 }
196
197 static inline GQuark
198 application_quark (void)
199 {
200 static GQuark quark;
201
202 if (G_UNLIKELY (quark == 0))
203 quark = g_quark_from_static_string ("cc-application");
204
205 return quark;
206 }
207
208 static gboolean
209 on_off_label_mapping_get (GValue *value,
210 GVariant *variant,
211 gpointer user_data)
212 {
213 g_value_set_string (value, g_variant_get_boolean (variant) ? _("On") : _("Off"));
214
215 return TRUE;
216 }
217
218 static void
219 application_free (Application *app)
220 {
221 g_free (app->canonical_app_id);
222 g_object_unref (app->app_info);
223 g_object_unref (app->settings);
224 g_object_unref (app->self);
225
226 g_slice_free (Application, app);
227 }
228
229 G_DEFINE_AUTOPTR_CLEANUP_FUNC (Application, application_free)
230
231 static void
232 add_application (CcNotificationsPanel *self,
233 Application *app)
234 {
235 CcListRow *row;
236 GtkWidget *w;
237 g_autoptr(GIcon) icon = NULL;
238 const gchar *app_name;
239
240 app_name = g_app_info_get_name (app->app_info);
241 if (app_name == NULL || *app_name == '\0')
242 return;
243
244 icon = g_app_info_get_icon (app->app_info);
245 if (icon == NULL)
246 icon = g_themed_icon_new ("application-x-executable");
247 else
248 g_object_ref (icon);
249
250 row = g_object_new (CC_TYPE_LIST_ROW, "show-arrow", TRUE, NULL);
251 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (row),
252 g_markup_escape_text (app_name, -1));
253
254 g_object_set_qdata_full (G_OBJECT (row), application_quark (),
255 app, (GDestroyNotify) application_free);
256
257 gtk_list_box_append (self->app_listbox, GTK_WIDGET (row));
258
259 w = gtk_image_new_from_gicon (icon);
260 gtk_widget_add_css_class (w, "lowres-icon");
261 gtk_image_set_icon_size (GTK_IMAGE (w), GTK_ICON_SIZE_LARGE);
262 adw_action_row_add_prefix (ADW_ACTION_ROW (row), w);
263
264 g_settings_bind_with_mapping (app->settings, "enable",
265 row, "secondary-label",
266 G_SETTINGS_BIND_GET |
267 G_SETTINGS_BIND_NO_SENSITIVITY,
268 on_off_label_mapping_get,
269 NULL,
270 NULL,
271 NULL);
272
273 g_hash_table_add (self->known_applications, g_strdup (app->canonical_app_id));
274 }
275
276 static gboolean
277 app_is_system_service (GDesktopAppInfo *app)
278 {
279 g_auto(GStrv) split = NULL;
280 const gchar *categories;
281
282 categories = g_desktop_app_info_get_categories (app);
283 if (categories == NULL || categories[0] == '\0')
284 return FALSE;
285
286 split = g_strsplit (categories, ";", -1);
287 if (g_strv_contains ((const gchar* const*) split, "X-GNOME-Settings-Panel") ||
288 g_strv_contains ((const gchar* const*) split, "Settings") ||
289 g_strv_contains ((const gchar* const*) split, "System")) {
290 return TRUE;
291 }
292
293 return FALSE;
294 }
295
296 static void
297 maybe_add_app_id (CcNotificationsPanel *self,
298 const char *canonical_app_id)
299 {
300 Application *app;
301 g_autofree gchar *path = NULL;
302 g_autofree gchar *full_app_id = NULL;
303 g_autoptr(GSettings) settings = NULL;
304 g_autoptr(GAppInfo) app_info = NULL;
305
306 if (*canonical_app_id == '\0')
307 return;
308
309 if (g_hash_table_contains (self->known_applications,
310 canonical_app_id))
311 return;
312
313 path = g_strconcat (APP_PREFIX, canonical_app_id, "/", NULL);
314 settings = g_settings_new_with_path (APP_SCHEMA, path);
315
316 full_app_id = g_settings_get_string (settings, "application-id");
317 app_info = G_APP_INFO (g_desktop_app_info_new (full_app_id));
318
319 if (app_info == NULL) {
320 g_debug ("Not adding application '%s' (canonical app ID: %s)",
321 full_app_id, canonical_app_id);
322 /* The application cannot be found, probably it was uninstalled */
323 return;
324 }
325
326 if (app_is_system_service (G_DESKTOP_APP_INFO (app_info))) {
327 /* We don't want to show system services in the notification list */
328 return;
329 }
330
331 app = g_slice_new (Application);
332 app->canonical_app_id = g_strdup (canonical_app_id);
333 app->settings = g_object_ref (settings);
334 app->app_info = g_object_ref (app_info);
335
336 g_debug ("Adding application '%s' (canonical app ID: %s)",
337 full_app_id, canonical_app_id);
338
339 add_application (self, app);
340 }
341
342 static char *
343 app_info_get_id (GAppInfo *app_info)
344 {
345 const char *desktop_id;
346 g_autofree gchar *ret = NULL;
347 const char *filename;
348 int l;
349
350 desktop_id = g_app_info_get_id (app_info);
351 if (desktop_id != NULL)
352 {
353 ret = g_strdup (desktop_id);
354 }
355 else
356 {
357 filename = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (app_info));
358 ret = g_path_get_basename (filename);
359 }
360
361 if (G_UNLIKELY (g_str_has_suffix (ret, ".desktop") == FALSE))
362 return NULL;
363
364 l = strlen (ret);
365 *(ret + l - strlen(".desktop")) = '\0';
366 return g_steal_pointer (&ret);
367 }
368
369 static void
370 process_app_info (CcNotificationsPanel *self,
371 GAppInfo *app_info)
372 {
373 Application *app;
374 g_autofree gchar *app_id = NULL;
375 g_autofree gchar *path = NULL;
376 g_autoptr(GSettings) settings = NULL;
377 guint i;
378
379 app_id = app_info_get_id (app_info);
380 g_strcanon (app_id,
381 "0123456789"
382 "abcdefghijklmnopqrstuvwxyz"
383 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
384 "-",
385 '-');
386 for (i = 0; app_id[i] != '\0'; i++)
387 app_id[i] = g_ascii_tolower (app_id[i]);
388
389 path = g_strconcat (APP_PREFIX, app_id, "/", NULL);
390 settings = g_settings_new_with_path (APP_SCHEMA, path);
391
392 app = g_slice_new (Application);
393 app->canonical_app_id = g_steal_pointer (&app_id);
394 app->settings = g_object_ref (settings);
395 app->app_info = g_object_ref (app_info);
396 app->self = g_object_ref (self);
397
398 if (g_hash_table_contains (self->known_applications,
399 app->canonical_app_id))
400 return;
401
402 g_debug ("Processing queued application %s", app->canonical_app_id);
403
404 add_application (self, app);
405 }
406
407 static void
408 load_apps (CcNotificationsPanel *self)
409 {
410 GList *iter, *apps;
411
412 apps = g_app_info_get_all ();
413
414 for (iter = apps; iter; iter = iter->next)
415 {
416 GDesktopAppInfo *app;
417
418 app = iter->data;
419 if (g_desktop_app_info_get_boolean (app, "X-GNOME-UsesNotifications")) {
420 if (app_is_system_service (app)) {
421 g_debug ("Skipped app '%s', as it is a system service", g_app_info_get_id (G_APP_INFO (app)));
422 continue;
423 }
424
425 process_app_info (self, G_APP_INFO (app));
426 g_debug ("Processing app '%s'", g_app_info_get_id (G_APP_INFO (app)));
427 } else {
428 g_debug ("Skipped app '%s', doesn't use notifications", g_app_info_get_id (G_APP_INFO (app)));
429 }
430 }
431
432 g_list_free_full (apps, g_object_unref);
433 }
434
435 static void
436 children_changed (CcNotificationsPanel *self,
437 const char *key)
438 {
439 int i;
440 g_auto (GStrv) new_app_ids = NULL;
441
442 g_settings_get (self->master_settings,
443 "application-children",
444 "^as", &new_app_ids);
445 for (i = 0; new_app_ids[i]; i++)
446 maybe_add_app_id (self, new_app_ids[i]);
447 }
448
449 static void
450 build_app_store (CcNotificationsPanel *self)
451 {
452 /* Build application entries for known applications */
453 children_changed (self, NULL);
454 g_signal_connect_object (self->master_settings,
455 "changed::application-children",
456 G_CALLBACK (children_changed), self, G_CONNECT_SWAPPED);
457
458 /* Scan applications that statically declare to show notifications */
459 load_apps (self);
460 }
461
462 static void
463 select_app (CcNotificationsPanel *self,
464 GtkListBoxRow *row)
465 {
466 Application *app;
467 g_autofree gchar *app_id = NULL;
468 CcAppNotificationsDialog *dialog;
469
470 app = g_object_get_qdata (G_OBJECT (row), application_quark ());
471
472 app_id = g_strdup (g_app_info_get_id (app->app_info));
473 if (g_str_has_suffix (app_id, ".desktop"))
474 app_id[strlen (app_id) - strlen (".desktop")] = '\0';
475
476 dialog = cc_app_notifications_dialog_new (app_id, g_app_info_get_name (app->app_info), app->settings, self->master_settings, self->perm_store);
477 adw_dialog_present (ADW_DIALOG (dialog), GTK_WIDGET (self));
478 }
479
480 static int
481 sort_apps (gconstpointer one,
482 gconstpointer two,
483 gpointer user_data)
484 {
485 Application *a1, *a2;
486
487 a1 = g_object_get_qdata (G_OBJECT (one), application_quark ());
488 a2 = g_object_get_qdata (G_OBJECT (two), application_quark ());
489
490 return g_utf8_collate (g_app_info_get_name (a1->app_info),
491 g_app_info_get_name (a2->app_info));
492 }
493