GCC Code Coverage Report


Directory: ./
File: panels/common/cc-permission-infobar.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 65 0.0%
Functions: 0 12 0.0%
Branches: 0 41 0.0%

Line Branch Exec Source
1 /* cc-permission-infobar.c
2 *
3 * Copyright (C) 2020 Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author(s):
19 * Felipe Borges <felipeborges@gnome.org>
20 *
21 */
22
23 #undef G_LOG_DOMAIN
24 #define G_LOG_DOMAIN "cc-permission-infobar"
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <glib/gi18n.h>
31 #include <adwaita.h>
32
33 #include "cc-permission-infobar.h"
34
35 struct _CcPermissionInfobar
36 {
37 AdwBin parent_instance;
38
39 AdwBanner *banner;
40 GPermission *permission;
41
42 GCancellable *cancellable;
43 };
44
45 G_DEFINE_TYPE (CcPermissionInfobar, cc_permission_infobar, ADW_TYPE_BIN)
46
47 static void
48 on_permission_changed (CcPermissionInfobar *self)
49 {
50 gboolean is_authorized = g_permission_get_allowed (self->permission);
51
52 adw_banner_set_revealed (self->banner, !is_authorized);
53 }
54
55 static void
56 acquire_cb (GObject *source,
57 GAsyncResult *result,
58 gpointer user_data)
59 {
60 CcPermissionInfobar *self = CC_PERMISSION_INFOBAR (user_data);
61 g_autoptr (GError) error = NULL;
62
63 if (!g_permission_acquire_finish (self->permission, result, &error))
64 {
65 g_warning ("Error acquiring permission: %s", error->message);
66 }
67
68 g_clear_object (&self->cancellable);
69 }
70
71 static void
72 release_cb (GObject *source,
73 GAsyncResult *result,
74 gpointer user_data)
75 {
76 CcPermissionInfobar *self = CC_PERMISSION_INFOBAR (user_data);
77 g_autoptr (GError) error = NULL;
78
79 if (!g_permission_release_finish (self->permission, result, &error))
80 {
81 g_warning ("Error releasing permission: %s", error->message);
82 g_error_free (error);
83 }
84
85 g_clear_object (&self->cancellable);
86 }
87
88 static void
89 banner_button_clicked_cb (CcPermissionInfobar *self)
90 {
91 /* if we already have a pending interactive check or permission is not set,
92 * then do nothing
93 */
94 if (self->cancellable != NULL || self->permission == NULL)
95 return;
96
97 if (g_permission_get_allowed (self->permission))
98 {
99 if (g_permission_get_can_release (self->permission))
100 {
101 self->cancellable = g_cancellable_new ();
102
103 g_permission_release_async (self->permission,
104 self->cancellable,
105 release_cb,
106 self);
107 }
108 }
109 else
110 {
111 if (g_permission_get_can_acquire (self->permission))
112 {
113 self->cancellable = g_cancellable_new ();
114
115 g_permission_acquire_async (self->permission,
116 self->cancellable,
117 acquire_cb,
118 self);
119 }
120 }
121 }
122
123 static void
124 cc_permission_infobar_dispose (GObject *object)
125 {
126 CcPermissionInfobar *self = CC_PERMISSION_INFOBAR (object);
127
128 if (self->cancellable != NULL)
129 {
130 g_cancellable_cancel (self->cancellable);
131 }
132
133 g_clear_object (&self->cancellable);
134
135 G_OBJECT_CLASS (cc_permission_infobar_parent_class)->dispose (object);
136 }
137
138 static void
139 cc_permission_infobar_class_init (CcPermissionInfobarClass *klass)
140 {
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
142 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
143
144 object_class->dispose = cc_permission_infobar_dispose;
145
146 gtk_widget_class_set_template_from_resource (widget_class,
147 "/org/gnome/control-center/"
148 "common/cc-permission-infobar.ui");
149
150 gtk_widget_class_bind_template_child (widget_class, CcPermissionInfobar, banner);
151 gtk_widget_class_bind_template_callback (widget_class,
152 banner_button_clicked_cb);
153 }
154
155 static void
156 cc_permission_infobar_init (CcPermissionInfobar *self)
157 {
158 gtk_widget_init_template (GTK_WIDGET (self));
159
160 /* Set the default title. */
161 cc_permission_infobar_set_title (self, NULL);
162 }
163
164 void
165 cc_permission_infobar_set_permission (CcPermissionInfobar *self,
166 GPermission *permission)
167 {
168 g_return_if_fail (CC_IS_PERMISSION_INFOBAR (self));
169
170 if (permission == NULL)
171 {
172 g_warning ("Missing GPermission object");
173 return;
174 }
175
176 self->permission = permission;
177
178 g_signal_connect_object (permission, "notify",
179 G_CALLBACK (on_permission_changed),
180 self,
181 G_CONNECT_SWAPPED);
182 on_permission_changed (self);
183 }
184
185 /**
186 * cc_permission_infobar_set_title:
187 * @self: a #CcPermissionInfobar
188 * @title: (nullable): title to display in the infobar, or %NULL for the default
189 *
190 * Set the title text to display in the infobar.
191 */
192 void
193 cc_permission_infobar_set_title (CcPermissionInfobar *self,
194 const gchar *title)
195 {
196 g_return_if_fail (CC_IS_PERMISSION_INFOBAR (self));
197
198 if (self->permission == NULL)
199 title = _("Error — some settings cannot be unlocked");
200
201 if (title == NULL)
202 title = _("Unlock to Change Settings");
203
204 adw_banner_set_title (self->banner, title);
205 adw_banner_set_button_label (self->banner, self->permission ? _("_Unlock…") : NULL);
206 }
207