GCC Code Coverage Report


Directory: ./
File: panels/network/connection-editor/ce-page-8021x-security.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 78 0.0%
Functions: 0 14 0.0%
Branches: 0 27 0.0%

Line Branch Exec Source
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager Connection editor -- Connection editor for NetworkManager
3 *
4 * Dan Williams <dcbw@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * (C) Copyright 2008 - 2012 Red Hat, Inc.
21 */
22
23 #include "config.h"
24
25 #include <glib/gi18n.h>
26 #include <NetworkManager.h>
27 #include <string.h>
28
29 #include "ce-page.h"
30 #include "ce-page-ethernet.h"
31 #include "ce-page-8021x-security.h"
32 #include "nma-ws.h"
33
34 struct _CEPage8021xSecurity {
35 AdwBin parent;
36
37 GtkBox *box;
38 GtkSwitch *enable_8021x_switch;
39 GtkLabel *security_label;
40
41 NMConnection *connection;
42 NMAWs8021x *security;
43 GtkSizeGroup *group;
44 gboolean initial_have_8021x;
45 };
46
47 static void ce_page_iface_init (CEPageInterface *);
48
49 G_DEFINE_TYPE_WITH_CODE (CEPage8021xSecurity, ce_page_8021x_security, ADW_TYPE_BIN,
50 G_IMPLEMENT_INTERFACE (ce_page_get_type (), ce_page_iface_init))
51
52 static void
53 enable_toggled (CEPage8021xSecurity *self)
54 {
55 gtk_widget_set_sensitive (GTK_WIDGET (self->security), gtk_switch_get_active (self->enable_8021x_switch));
56 ce_page_changed (CE_PAGE (self));
57 }
58
59 static void
60 security_item_changed_cb (CEPage8021xSecurity *self)
61 {
62 ce_page_changed (CE_PAGE (self));
63 }
64
65 static void
66 finish_setup (CEPage8021xSecurity *self, gpointer unused, GError *error, gpointer user_data)
67 {
68 if (error)
69 return;
70
71 self->group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
72
73 self->security = nma_ws_802_1x_new (self->connection, FALSE, FALSE);
74 if (!self->security) {
75 g_warning ("Could not load 802.1x user interface.");
76 return;
77 }
78
79 g_signal_connect_object (NMA_WS (self->security), "ws-changed", G_CALLBACK (security_item_changed_cb), self, G_CONNECT_SWAPPED);
80 if (gtk_widget_get_parent (GTK_WIDGET (self->security)))
81 gtk_box_remove (self->box, GTK_WIDGET (self->security));
82
83 gtk_switch_set_active (self->enable_8021x_switch, self->initial_have_8021x);
84 g_signal_connect_object (self->enable_8021x_switch, "notify::active", G_CALLBACK (enable_toggled), self, G_CONNECT_SWAPPED);
85 gtk_widget_set_sensitive (GTK_WIDGET (self->security), self->initial_have_8021x);
86
87 gtk_size_group_add_widget (self->group, GTK_WIDGET (self->security_label));
88 nma_ws_add_to_size_group (NMA_WS (self->security), self->group);
89
90 gtk_box_append (self->box, GTK_WIDGET (self->security));
91
92 }
93
94 static const gchar *
95 ce_page_8021x_security_get_security_setting (CEPage *page)
96 {
97 CEPage8021xSecurity *self = CE_PAGE_8021X_SECURITY (page);
98
99 if (self->initial_have_8021x)
100 return NM_SETTING_802_1X_SETTING_NAME;
101
102 return NULL;
103 }
104
105 static const gchar *
106 ce_page_8021x_security_get_title (CEPage *page)
107 {
108 return _("Security");
109 }
110
111 static gboolean
112 ce_page_8021x_security_validate (CEPage *cepage, NMConnection *connection, GError **error)
113 {
114 CEPage8021xSecurity *self = CE_PAGE_8021X_SECURITY (cepage);
115 gboolean valid = TRUE;
116
117 if (gtk_switch_get_active (self->enable_8021x_switch)) {
118 NMSetting *s_8021x;
119
120 /* FIXME: get failed property and error out of wireless security objects */
121 valid = nma_ws_validate (NMA_WS (self->security), error);
122 if (valid) {
123 g_autoptr(NMConnection) tmp_connection = NULL;
124
125 /* Here's a nice hack to work around the fact that ws_802_1x_fill_connection needs wireless setting. */
126 tmp_connection = nm_simple_connection_new_clone (connection);
127 nm_connection_add_setting (tmp_connection, nm_setting_wireless_new ());
128
129 nma_ws_fill_connection (NMA_WS (self->security), tmp_connection);
130
131 /* NOTE: It is important we create a copy of the settings, as the
132 * secrets might be cleared otherwise.
133 */
134 s_8021x = nm_connection_get_setting (tmp_connection, NM_TYPE_SETTING_802_1X);
135 nm_connection_add_setting (connection, nm_setting_duplicate (NM_SETTING (s_8021x)));
136 }
137 } else {
138 nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
139 valid = TRUE;
140 }
141
142 return valid;
143 }
144
145 static void
146 ce_page_8021x_security_init (CEPage8021xSecurity *self)
147 {
148 gtk_widget_init_template (GTK_WIDGET (self));
149 }
150
151 static void
152 ce_page_8021x_security_dispose (GObject *object)
153 {
154 CEPage8021xSecurity *self = CE_PAGE_8021X_SECURITY (object);
155
156 g_clear_object (&self->connection);
157 g_clear_pointer ((GtkWidget **) &self->security, gtk_widget_unparent);
158 g_clear_object (&self->group);
159
160 G_OBJECT_CLASS (ce_page_8021x_security_parent_class)->dispose (object);
161 }
162
163 static void
164 ce_page_8021x_security_class_init (CEPage8021xSecurityClass *klass)
165 {
166 GObjectClass *object_class = G_OBJECT_CLASS (klass);
167 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
168
169 object_class->dispose = ce_page_8021x_security_dispose;
170
171 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/network/8021x-security-page.ui");
172
173 gtk_widget_class_bind_template_child (widget_class, CEPage8021xSecurity, box);
174 gtk_widget_class_bind_template_child (widget_class, CEPage8021xSecurity, enable_8021x_switch);
175 gtk_widget_class_bind_template_child (widget_class, CEPage8021xSecurity, security_label);
176 }
177
178 static void
179 ce_page_iface_init (CEPageInterface *iface)
180 {
181 iface->get_security_setting = ce_page_8021x_security_get_security_setting;
182 iface->get_title = ce_page_8021x_security_get_title;
183 iface->validate = ce_page_8021x_security_validate;
184 }
185
186 CEPage8021xSecurity *
187 ce_page_8021x_security_new (NMConnection *connection)
188 {
189 CEPage8021xSecurity *self;
190
191 self = CE_PAGE_8021X_SECURITY (g_object_new (ce_page_8021x_security_get_type (), NULL));
192
193 self->connection = g_object_ref (connection);
194
195 if (nm_connection_get_setting_802_1x (connection))
196 self->initial_have_8021x = TRUE;
197
198 g_signal_connect (self, "initialized", G_CALLBACK (finish_setup), NULL);
199
200 return self;
201 }
202