~ubuntu-branches/ubuntu/lucid/network-manager-applet/lucid-updates

« back to all changes in this revision

Viewing changes to src/connection-editor/ce-polkit-button.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack, Alexander Sack, Tony Espy
  • Date: 2009-09-14 11:32:57 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090914113257-47x5o2kfwwm95w3l
Tags: 0.8~a~git.20090913t161448.cc2f6be-0ubuntu1
* upstream snapshot 2009-09-13 16:14:48 (GMT)
  + cc2f6bea12daec5f0caf535a3534f07ade5b5cf2

[ Alexander Sack <asac@ubuntu.com> ]
* build depend on libpolkit-gobject-1-dev instead of libpolkit-dbus-dev
  - update debian/control

[ Tony Espy <espy@ubuntu.com> ]
* adjust patches for upstream code base
  - update debian/patches/20_use_full_vpn_dialog_service_name_path.patch
  - update debian/patches/lp328572_dxteam_connect_text.patch
  - update debian/patches/lp337960_dxteam_notification_icon_names.diff
  - update debian/patches/lp341684_device_sensitive_disconnect_notify.patch
* adjust build and runtime depends due to ABI changes in latest NM
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2009 Red Hat, Inc.
 
21
 */
 
22
 
 
23
#include <string.h>
 
24
 
 
25
#include <gtk/gtk.h>
 
26
#include <glib/gi18n.h>
 
27
 
 
28
#include "ce-polkit-button.h"
 
29
 
 
30
G_DEFINE_TYPE (CEPolkitButton, ce_polkit_button, GTK_TYPE_BUTTON)
 
31
 
 
32
#define CE_POLKIT_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CE_TYPE_POLKIT_BUTTON, CEPolkitButtonPrivate))
 
33
 
 
34
typedef struct {
 
35
        char *label;
 
36
        char *tooltip;
 
37
        char *auth_label;
 
38
        char *auth_tooltip;
 
39
        gboolean master_sensitive;
 
40
 
 
41
        GtkWidget *stock;
 
42
        GtkWidget *auth;
 
43
 
 
44
        NMRemoteSettingsSystem *settings;
 
45
        NMSettingsSystemPermissions permission;
 
46
        gboolean use_polkit;
 
47
        /* authorized = TRUE if either explicitly authorized or if the action
 
48
         * could be performed if the user successfully authenticated to gain the
 
49
         * authorization.
 
50
         */
 
51
        gboolean authorized;
 
52
 
 
53
        guint check_id;
 
54
} CEPolkitButtonPrivate;
 
55
 
 
56
enum {
 
57
        ACTIONABLE,
 
58
        AUTHORIZED,
 
59
        LAST_SIGNAL
 
60
};
 
61
 
 
62
static guint signals[LAST_SIGNAL] = { 0 };
 
63
 
 
64
static void
 
65
update_button (CEPolkitButton *self, gboolean actionable)
 
66
{
 
67
        CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
 
68
 
 
69
        gtk_widget_set_sensitive (GTK_WIDGET (self), actionable);
 
70
 
 
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);
 
75
        } else {
 
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);
 
79
        }
 
80
}
 
81
 
 
82
static void
 
83
update_and_emit (CEPolkitButton *self, gboolean old_actionable)
 
84
{
 
85
        gboolean new_actionable;
 
86
 
 
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);
 
91
}
 
92
 
 
93
void
 
94
ce_polkit_button_set_use_polkit (CEPolkitButton *self, gboolean use_polkit)
 
95
{
 
96
        CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
 
97
        gboolean old_actionable;
 
98
 
 
99
        old_actionable = ce_polkit_button_get_actionable (self);
 
100
        priv->use_polkit = use_polkit;
 
101
        update_and_emit (self, old_actionable);
 
102
}
 
103
 
 
104
void
 
105
ce_polkit_button_set_master_sensitive (CEPolkitButton *self, gboolean sensitive)
 
106
{
 
107
        CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
 
108
        gboolean old_actionable;
 
109
 
 
110
        old_actionable = ce_polkit_button_get_actionable (self);
 
111
        priv->master_sensitive = sensitive;
 
112
        update_and_emit (self, old_actionable);
 
113
}
 
114
 
 
115
gboolean
 
116
ce_polkit_button_get_actionable (CEPolkitButton *self)
 
117
{
 
118
        CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
 
119
 
 
120
        if (!priv->master_sensitive)
 
121
                return FALSE;
 
122
 
 
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.
 
126
         */
 
127
        return priv->use_polkit ? priv->authorized : TRUE;
 
128
}
 
129
 
 
130
gboolean
 
131
ce_polkit_button_get_authorized (CEPolkitButton *self)
 
132
{
 
133
        return CE_POLKIT_BUTTON_GET_PRIVATE (self)->authorized;
 
134
}
 
135
 
 
136
static void
 
137
get_permissions_cb (NMSettingsSystemInterface *settings,
 
138
                    NMSettingsSystemPermissions permissions,
 
139
                    GError *error,
 
140
                    gpointer user_data)
 
141
{
 
142
        CEPolkitButton *self = CE_POLKIT_BUTTON (user_data);
 
143
        CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (self);
 
144
        gboolean old_actionable, old_authorized;
 
145
 
 
146
        old_actionable = ce_polkit_button_get_actionable (self);
 
147
        old_authorized = priv->authorized;
 
148
 
 
149
        priv->authorized = (permissions & priv->permission);
 
150
        if (priv->use_polkit)
 
151
                update_and_emit (self, old_actionable);
 
152
 
 
153
        if (priv->authorized != old_authorized)
 
154
                g_signal_emit (self, signals[AUTHORIZED], 0, priv->authorized);
 
155
}
 
156
 
 
157
static void
 
158
check_permissions_cb (NMRemoteSettingsSystem *settings, CEPolkitButton *self)
 
159
{
 
160
        /* recheck permissions */
 
161
        nm_settings_system_interface_get_permissions (NM_SETTINGS_SYSTEM_INTERFACE (settings),
 
162
                                                      get_permissions_cb,
 
163
                                                      self);
 
164
}
 
165
 
 
166
GtkWidget *
 
167
ce_polkit_button_new (const char *label,
 
168
                      const char *tooltip,
 
169
                      const char *auth_label,
 
170
                      const char *auth_tooltip,
 
171
                      const char *stock_icon,
 
172
                      NMRemoteSettingsSystem *settings,
 
173
                      NMSettingsSystemPermissions permission)
 
174
{
 
175
        GObject *object;
 
176
        CEPolkitButtonPrivate *priv;
 
177
 
 
178
        object = g_object_new (CE_TYPE_POLKIT_BUTTON, NULL);
 
179
        if (!object)
 
180
                return NULL;
 
181
 
 
182
        priv = CE_POLKIT_BUTTON_GET_PRIVATE (object);
 
183
 
 
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;
 
190
 
 
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),
 
195
                                           object);
 
196
 
 
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);
 
201
 
 
202
        update_button (CE_POLKIT_BUTTON (object),
 
203
                       ce_polkit_button_get_actionable (CE_POLKIT_BUTTON (object)));
 
204
 
 
205
        check_permissions_cb (settings, CE_POLKIT_BUTTON (object));
 
206
 
 
207
        return GTK_WIDGET (object);
 
208
}
 
209
 
 
210
static void
 
211
finalize (GObject *object)
 
212
{
 
213
        CEPolkitButtonPrivate *priv = CE_POLKIT_BUTTON_GET_PRIVATE (object);
 
214
 
 
215
        g_free (priv->label);
 
216
        g_free (priv->auth_label);
 
217
        g_free (priv->tooltip);
 
218
        g_free (priv->auth_tooltip);
 
219
 
 
220
        if (priv->check_id)
 
221
                g_signal_handler_disconnect (priv->settings, priv->check_id);
 
222
 
 
223
        g_object_unref (priv->settings);
 
224
        g_object_unref (priv->auth);
 
225
        g_object_unref (priv->stock);
 
226
 
 
227
        G_OBJECT_CLASS (ce_polkit_button_parent_class)->finalize (object);
 
228
}
 
229
 
 
230
static void
 
231
ce_polkit_button_init (CEPolkitButton *self)
 
232
{
 
233
}
 
234
 
 
235
static void
 
236
ce_polkit_button_class_init (CEPolkitButtonClass *pb_class)
 
237
{
 
238
        GObjectClass *object_class = G_OBJECT_CLASS (pb_class);
 
239
 
 
240
        g_type_class_add_private (object_class, sizeof (CEPolkitButtonPrivate));
 
241
 
 
242
        object_class->finalize = finalize;
 
243
 
 
244
        signals[ACTIONABLE] = g_signal_new ("actionable",
 
245
                                            G_OBJECT_CLASS_TYPE (object_class),
 
246
                                            G_SIGNAL_RUN_FIRST,
 
247
                                            G_STRUCT_OFFSET (CEPolkitButtonClass, actionable),
 
248
                                            NULL, NULL,
 
249
                                            g_cclosure_marshal_VOID__BOOLEAN,
 
250
                                            G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
 
251
 
 
252
        signals[AUTHORIZED] = g_signal_new ("authorized",
 
253
                                            G_OBJECT_CLASS_TYPE (object_class),
 
254
                                            G_SIGNAL_RUN_FIRST,
 
255
                                            G_STRUCT_OFFSET (CEPolkitButtonClass, authorized),
 
256
                                            NULL, NULL,
 
257
                                            g_cclosure_marshal_VOID__BOOLEAN,
 
258
                                            G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
 
259
}
 
260