1
/* Copyright (C) 2007 Carlos Garnacho
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU Lesser General Public License as
5
* published by the Free Software Foundation; either version 2 of the
6
* License, or (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
* Authors: Carlos Garnacho <carlosg@gnome.org>
20
#include <glib/gi18n.h>
21
#include "gst-polkit-action.h"
22
#include "gst-polkit-button.h"
24
#define GST_POLKIT_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_POLKIT_BUTTON, GstPolKitButtonPriv))
26
typedef struct GstPolKitButtonPriv GstPolKitButtonPriv;
28
struct GstPolKitButtonPriv {
29
GstPolKitAction *action;
45
static void gst_polkit_button_class_init (GstPolKitButtonClass *class);
46
static void gst_polkit_button_init (GstPolKitButton *button);
47
static void gst_polkit_button_finalize (GObject *object);
49
static void gst_polkit_button_get_property (GObject *object,
53
static void gst_polkit_button_set_property (GObject *object,
58
static void gst_polkit_button_clicked (GtkButton *button);
61
static guint signals [LAST_SIGNAL] = { 0 };
63
G_DEFINE_TYPE (GstPolKitButton, gst_polkit_button, GTK_TYPE_BUTTON)
66
gst_polkit_button_class_init (GstPolKitButtonClass *class)
68
GObjectClass *object_class = G_OBJECT_CLASS (class);
69
GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
71
object_class->finalize = gst_polkit_button_finalize;
72
object_class->get_property = gst_polkit_button_get_property;
73
object_class->set_property = gst_polkit_button_set_property;
75
button_class->clicked = gst_polkit_button_clicked;
77
g_object_class_install_property (object_class,
79
g_param_spec_string ("action",
81
"PolicyKit action to manipulate",
84
g_object_class_install_property (object_class,
86
g_param_spec_string ("label",
91
g_object_class_install_property (object_class,
93
g_param_spec_boolean ("authenticated",
95
"Whether the action is authenticated",
99
g_signal_new ("changed",
100
G_OBJECT_CLASS_TYPE (object_class),
102
G_STRUCT_OFFSET (GstPolKitButtonClass, changed),
104
g_cclosure_marshal_VOID__VOID,
107
g_type_class_add_private (object_class,
108
sizeof (GstPolKitButtonPriv));
112
update_button_state (GstPolKitButton *button)
114
GstPolKitButtonPriv *priv;
116
GtkWidget *image = NULL;
117
const gchar *tooltip = NULL;
118
gboolean sensitive = FALSE;
120
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
121
result = gst_polkit_action_get_result (priv->action);
124
case POLKIT_RESULT_YES:
125
image = gtk_image_new_from_stock (GTK_STOCK_YES, GTK_ICON_SIZE_BUTTON);
126
tooltip = N_("This action is allowed");
128
case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH:
129
case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_SESSION:
130
case POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_ALWAYS:
131
case POLKIT_RESULT_ONLY_VIA_SELF_AUTH:
132
case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_SESSION:
133
case POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_ALWAYS:
134
case POLKIT_RESULT_UNKNOWN:
135
image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION, GTK_ICON_SIZE_BUTTON);
138
case POLKIT_RESULT_NO:
139
image = gtk_image_new_from_stock (GTK_STOCK_NO, GTK_ICON_SIZE_BUTTON);
140
tooltip = N_("This action is not allowed");
143
g_warning ("Unhandled PolKitResult");
146
gtk_button_set_image (GTK_BUTTON (button), image);
147
gtk_widget_set_tooltip_text (GTK_WIDGET (button), _(tooltip));
148
gtk_widget_set_sensitive (GTK_WIDGET (button), sensitive);
152
action_state_changed (GstPolKitButton *button)
154
update_button_state (button);
155
g_signal_emit (button, signals[CHANGED], 0);
159
gst_polkit_button_init (GstPolKitButton *button)
161
GstPolKitButtonPriv *priv;
163
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
164
priv->action = gst_polkit_action_new (NULL, GTK_WIDGET (button));
166
g_signal_connect_swapped (priv->action, "changed",
167
G_CALLBACK (action_state_changed), button);
169
update_button_state (button);
173
gst_polkit_button_finalize (GObject *object)
175
GstPolKitButtonPriv *priv;
177
priv = GST_POLKIT_BUTTON_GET_PRIVATE (object);
179
g_object_unref (priv->action);
180
g_free (priv->label);
182
G_OBJECT_CLASS (gst_polkit_button_parent_class)->finalize (object);
186
gst_polkit_button_get_property (GObject *object,
191
GstPolKitButtonPriv *priv;
193
priv = GST_POLKIT_BUTTON_GET_PRIVATE (object);
197
g_value_set_string (value, gst_polkit_action_get_action (priv->action));
200
g_value_set_string (value, priv->label);
203
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208
gst_polkit_button_set_property (GObject *object,
213
GstPolKitButton *button;
215
button = GST_POLKIT_BUTTON (object);
219
gst_polkit_button_set_action (button, g_value_get_string (value));
222
gst_polkit_button_set_label (button, g_value_get_string (value));
230
gst_polkit_button_clicked (GtkButton *button)
232
GstPolKitButtonPriv *priv;
235
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
236
gst_polkit_action_authenticate (priv->action);
237
result = gst_polkit_action_get_result (priv->action);
239
if (result == POLKIT_RESULT_UNKNOWN) {
240
GtkWidget *dialog, *toplevel;
242
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
243
dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
247
_("Could not authenticate"));
248
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
249
_("An unexpected error has occurred."));
251
gtk_dialog_run (GTK_DIALOG (dialog));
252
gtk_widget_destroy (dialog);
255
update_button_state (GST_POLKIT_BUTTON (button));
259
gst_polkit_button_new (const gchar *action,
262
return g_object_new (GST_TYPE_POLKIT_BUTTON,
268
G_CONST_RETURN gchar *
269
gst_polkit_button_get_action (GstPolKitButton *button)
271
GstPolKitButtonPriv *priv;
273
g_return_val_if_fail (GST_IS_POLKIT_BUTTON (button), NULL);
275
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
276
return gst_polkit_action_get_action (priv->action);
280
gst_polkit_button_set_action (GstPolKitButton *button,
283
GstPolKitButtonPriv *priv;
286
g_return_if_fail (GST_IS_POLKIT_BUTTON (button));
288
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
289
gst_polkit_action_set_action (priv->action, action);
290
update_button_state (button);
292
g_object_notify (G_OBJECT (button), "action");
295
G_CONST_RETURN gchar *
296
gst_polkit_button_get_label (GstPolKitButton *button)
298
GstPolKitButtonPriv *priv;
300
g_return_val_if_fail (GST_IS_POLKIT_BUTTON (button), NULL);
302
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
307
gst_polkit_button_set_label (GstPolKitButton *button,
310
GstPolKitButtonPriv *priv;
313
g_return_if_fail (GST_IS_POLKIT_BUTTON (button));
315
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
317
str = g_strdup (label);
318
g_free (priv->label);
321
gtk_button_set_label (GTK_BUTTON (button), priv->label);
323
g_object_notify (G_OBJECT (button), "label");
327
gst_polkit_button_get_authenticated (GstPolKitButton *button)
329
GstPolKitButtonPriv *priv;
331
g_return_val_if_fail (GST_IS_POLKIT_BUTTON (button), FALSE);
333
priv = GST_POLKIT_BUTTON_GET_PRIVATE (button);
334
return gst_polkit_action_get_authenticated (priv->action);