1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* NetworkManager Connection editor -- Connection editor for NetworkManager
4
* Dan Williams <dcbw@redhat.com>
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.
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.
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.
20
* (C) Copyright 2009 Red Hat, Inc.
26
#include <glib/gi18n.h>
28
#include "ce-polkit-button.h"
30
G_DEFINE_TYPE (CEPolkitButton, ce_polkit_button, GTK_TYPE_BUTTON)
32
#define CE_POLKIT_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CE_TYPE_POLKIT_BUTTON, CEPolkitButtonPrivate))
39
gboolean master_sensitive;
44
NMRemoteSettingsSystem *settings;
45
NMSettingsSystemPermissions permission;
47
/* authorized = TRUE if either explicitly authorized or if the action
48
* could be performed if the user successfully authenticated to gain the
54
} CEPolkitButtonPrivate;
62
static guint signals[LAST_SIGNAL] = { 0 };
65
update_button (CEPolkitButton *self, gboolean actionable)
67
CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
69
gtk_widget_set_sensitive (GTK_WIDGET (self), actionable);
71
if (priv->use_polkit && priv->authorized) {
72
gtk_button_set_label (GTK_BUTTON (self), priv->auth_label);
73
gtk_widget_set_tooltip_text (GTK_WIDGET (self), priv->auth_tooltip);
74
gtk_button_set_image (GTK_BUTTON (self), priv->auth);
76
gtk_button_set_label (GTK_BUTTON (self), priv->label);
77
gtk_widget_set_tooltip_text (GTK_WIDGET (self), priv->tooltip);
78
gtk_button_set_image (GTK_BUTTON (self), priv->stock);
83
update_and_emit (CEPolkitButton *self, gboolean old_actionable)
85
gboolean new_actionable;
87
new_actionable = ce_polkit_button_get_actionable (self);
88
update_button (self, new_actionable);
89
if (new_actionable != old_actionable)
90
g_signal_emit (self, signals[ACTIONABLE], 0, new_actionable);
94
ce_polkit_button_set_use_polkit (CEPolkitButton *self, gboolean use_polkit)
96
CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
97
gboolean old_actionable;
99
old_actionable = ce_polkit_button_get_actionable (self);
100
priv->use_polkit = use_polkit;
101
update_and_emit (self, old_actionable);
105
ce_polkit_button_set_master_sensitive (CEPolkitButton *self, gboolean sensitive)
107
CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
108
gboolean old_actionable;
110
old_actionable = ce_polkit_button_get_actionable (self);
111
priv->master_sensitive = sensitive;
112
update_and_emit (self, old_actionable);
116
ce_polkit_button_get_actionable (CEPolkitButton *self)
118
CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
120
if (!priv->master_sensitive)
123
/* If polkit is in-use, the button is only actionable if the operation is
124
* authorized or able to be authorized via user authentication. If polkit
125
* isn't in-use, the button will always be actionable unless insensitive.
127
return priv->use_polkit ? priv->authorized : TRUE;
131
ce_polkit_button_get_authorized (CEPolkitButton *self)
133
return CE_POLKIT_BUTTON_GET_PRIVATE (self)->authorized;
137
get_permissions_cb (NMSettingsSystemInterface *settings,
138
NMSettingsSystemPermissions permissions,
142
CEPolkitButton *self = CE_POLKIT_BUTTON (user_data);
143
CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
144
gboolean old_actionable, old_authorized;
146
old_actionable = ce_polkit_button_get_actionable (self);
147
old_authorized = priv->authorized;
149
priv->authorized = (permissions & priv->permission);
150
if (priv->use_polkit)
151
update_and_emit (self, old_actionable);
153
if (priv->authorized != old_authorized)
154
g_signal_emit (self, signals[AUTHORIZED], 0, priv->authorized);
158
check_permissions_cb (NMRemoteSettingsSystem *settings, CEPolkitButton *self)
160
/* recheck permissions */
161
nm_settings_system_interface_get_permissions (NM_SETTINGS_SYSTEM_INTERFACE (settings),
167
ce_polkit_button_new (const char *label,
169
const char *auth_label,
170
const char *auth_tooltip,
171
const char *stock_icon,
172
NMRemoteSettingsSystem *settings,
173
NMSettingsSystemPermissions permission)
176
CEPolkitButtonPrivate *priv;
178
object = g_object_new (CE_TYPE_POLKIT_BUTTON, NULL);
182
priv = CE_POLKIT_BUTTON_GET_PRIVATE (object);
184
priv->label = g_strdup (label);
185
priv->tooltip = g_strdup (tooltip);
186
priv->auth_label = g_strdup (auth_label);
187
priv->auth_tooltip = g_strdup (auth_tooltip);
188
priv->permission = permission;
189
priv->use_polkit = FALSE;
191
priv->settings = g_object_ref (settings);
192
priv->check_id = g_signal_connect (settings,
193
NM_SETTINGS_SYSTEM_INTERFACE_CHECK_PERMISSIONS,
194
G_CALLBACK (check_permissions_cb),
197
priv->stock = gtk_image_new_from_stock (stock_icon, GTK_ICON_SIZE_BUTTON);
198
g_object_ref_sink (priv->stock);
199
priv->auth = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION, GTK_ICON_SIZE_BUTTON);
200
g_object_ref_sink (priv->auth);
202
update_button (CE_POLKIT_BUTTON (object),
203
ce_polkit_button_get_actionable (CE_POLKIT_BUTTON (object)));
205
check_permissions_cb (settings, CE_POLKIT_BUTTON (object));
207
return GTK_WIDGET (object);
211
finalize (GObject *object)
213
CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (object);
215
g_free (priv->label);
216
g_free (priv->auth_label);
217
g_free (priv->tooltip);
218
g_free (priv->auth_tooltip);
221
g_signal_handler_disconnect (priv->settings, priv->check_id);
223
g_object_unref (priv->settings);
224
g_object_unref (priv->auth);
225
g_object_unref (priv->stock);
227
G_OBJECT_CLASS (ce_polkit_button_parent_class)->finalize (object);
231
ce_polkit_button_init (CEPolkitButton *self)
236
ce_polkit_button_class_init (CEPolkitButtonClass *pb_class)
238
GObjectClass *object_class = G_OBJECT_CLASS (pb_class);
240
g_type_class_add_private (object_class, sizeof (CEPolkitButtonPrivate));
242
object_class->finalize = finalize;
244
signals[ACTIONABLE] = g_signal_new ("actionable",
245
G_OBJECT_CLASS_TYPE (object_class),
247
G_STRUCT_OFFSET (CEPolkitButtonClass, actionable),
249
g_cclosure_marshal_VOID__BOOLEAN,
250
G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
252
signals[AUTHORIZED] = g_signal_new ("authorized",
253
G_OBJECT_CLASS_TYPE (object_class),
255
G_STRUCT_OFFSET (CEPolkitButtonClass, authorized),
257
g_cclosure_marshal_VOID__BOOLEAN,
258
G_TYPE_NONE, 1, G_TYPE_BOOLEAN);