1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Red Hat, Inc.
3 * Author: Matthias Clasen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 #include "gtklockbuttonprivate.h"
25 #include "gtksizegroup.h"
27 #include "a11y/gtklockbuttonaccessibleprivate.h"
30 * SECTION:gtklockbutton
31 * @title: GtkLockButton
32 * @short_description: A widget to unlock or lock privileged operations
34 * GtkLockButton is a widget that can be used in control panels or
35 * preference dialogs to allow users to obtain and revoke authorizations
36 * needed to operate the controls. The required authorization is represented
37 * by a #GPermission object. Concrete implementations of #GPermission may use
38 * PolicyKit or some other authorization framework. To obtain a PolicyKit-based
39 * #GPermission, use polkit_permission_new().
41 * If the user is not currently allowed to perform the action, but can obtain
42 * the permission, the widget looks like this
43 * <informalexample><inlinegraphic fileref="lockbutton-locked.png"></inlinegraphic></informalexample>
44 * and the user can click the button to request the permission. Depending
45 * on the platform, this may pop up an authentication dialog or ask the user
46 * to authenticate in some other way. Once the user has obtained the permission,
47 * the widget changes to this
48 * <informalexample><inlinegraphic fileref="lockbutton-unlocked.png"></inlinegraphic></informalexample>
49 * and the permission can be dropped again by clicking the button. If the user
50 * is not able to obtain the permission at all, the widget looks like this
51 * <informalexample><inlinegraphic fileref="lockbutton-sorry.png"></inlinegraphic></informalexample>
52 * If the user has the permission and cannot drop it, the button is hidden.
54 * The text (and tooltips) that are shown in the various cases can be adjusted
55 * with the #GtkLockButton:text-lock, #GtkLockButton:text-unlock,
56 * #GtkLockButton:tooltip-lock, #GtkLockButton:tooltip-unlock and
57 * #GtkLockButton:tooltip-not-authorized properties.
60 struct _GtkLockButtonPrivate
62 GPermission *permission;
63 GCancellable *cancellable;
66 gchar *tooltip_unlock;
67 gchar *tooltip_not_authorized;
73 GtkWidget *label_lock;
74 GtkWidget *label_unlock;
76 GtkSizeGroup *label_group;
87 PROP_TOOLTIP_NOT_AUTHORIZED
90 static void update_state (GtkLockButton *button);
91 static void gtk_lock_button_clicked (GtkButton *button);
93 static void on_permission_changed (GPermission *permission,
97 G_DEFINE_TYPE (GtkLockButton, gtk_lock_button, GTK_TYPE_BUTTON);
100 gtk_lock_button_finalize (GObject *object)
102 GtkLockButton *button = GTK_LOCK_BUTTON (object);
103 GtkLockButtonPrivate *priv = button->priv;
105 g_free (priv->tooltip_lock);
106 g_free (priv->tooltip_unlock);
107 g_free (priv->tooltip_not_authorized);
109 g_object_unref (priv->icon_lock);
110 g_object_unref (priv->icon_unlock);
111 g_object_unref (priv->label_group);
113 if (priv->cancellable != NULL)
115 g_cancellable_cancel (priv->cancellable);
116 g_object_unref (priv->cancellable);
119 if (priv->permission)
121 g_signal_handlers_disconnect_by_func (priv->permission,
122 on_permission_changed,
124 g_object_unref (priv->permission);
127 G_OBJECT_CLASS (gtk_lock_button_parent_class)->finalize (object);
131 gtk_lock_button_get_property (GObject *object,
136 GtkLockButton *button = GTK_LOCK_BUTTON (object);
137 GtkLockButtonPrivate *priv = button->priv;
141 case PROP_PERMISSION:
142 g_value_set_object (value, priv->permission);
146 g_value_set_string (value, gtk_label_get_text (GTK_LABEL (priv->label_lock)));
149 case PROP_TEXT_UNLOCK:
150 g_value_set_string (value, gtk_label_get_text (GTK_LABEL (priv->label_unlock)));
153 case PROP_TOOLTIP_LOCK:
154 g_value_set_string (value, priv->tooltip_lock);
157 case PROP_TOOLTIP_UNLOCK:
158 g_value_set_string (value, priv->tooltip_unlock);
161 case PROP_TOOLTIP_NOT_AUTHORIZED:
162 g_value_set_string (value, priv->tooltip_not_authorized);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
172 gtk_lock_button_set_property (GObject *object,
177 GtkLockButton *button = GTK_LOCK_BUTTON (object);
178 GtkLockButtonPrivate *priv = button->priv;
182 case PROP_PERMISSION:
183 gtk_lock_button_set_permission (button, g_value_get_object (value));
187 gtk_label_set_text (GTK_LABEL (priv->label_lock), g_value_get_string (value));
188 _gtk_lock_button_accessible_name_changed (button);
191 case PROP_TEXT_UNLOCK:
192 gtk_label_set_text (GTK_LABEL (priv->label_unlock), g_value_get_string (value));
193 _gtk_lock_button_accessible_name_changed (button);
196 case PROP_TOOLTIP_LOCK:
197 g_free (priv->tooltip_lock);
198 priv->tooltip_lock = g_value_dup_string (value);
201 case PROP_TOOLTIP_UNLOCK:
202 g_free (priv->tooltip_unlock);
203 priv->tooltip_unlock = g_value_dup_string (value);
206 case PROP_TOOLTIP_NOT_AUTHORIZED:
207 g_free (priv->tooltip_not_authorized);
208 priv->tooltip_not_authorized = g_value_dup_string (value);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
216 update_state (button);
220 gtk_lock_button_init (GtkLockButton *button)
222 GtkLockButtonPrivate *priv;
225 button->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
226 GTK_TYPE_LOCK_BUTTON,
227 GtkLockButtonPrivate);
229 priv->label_group = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
230 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
231 gtk_widget_set_halign (priv->box, GTK_ALIGN_CENTER);
232 gtk_widget_set_valign (priv->box, GTK_ALIGN_CENTER);
233 gtk_widget_show (priv->box);
234 gtk_container_add (GTK_CONTAINER (button), priv->box);
235 priv->image = gtk_image_new ();
236 gtk_box_pack_start (GTK_BOX (priv->box), priv->image, FALSE, FALSE, 0);
237 gtk_widget_show (priv->image);
238 priv->label_lock = gtk_label_new ("");
239 gtk_misc_set_alignment (GTK_MISC (priv->label_lock), 0, 0.5);
240 gtk_widget_set_no_show_all (priv->label_lock, TRUE);
241 gtk_widget_show (priv->label_lock);
242 gtk_box_pack_start (GTK_BOX (priv->box), priv->label_lock, FALSE, FALSE, 0);
243 gtk_size_group_add_widget (priv->label_group, priv->label_lock);
244 priv->label_unlock = gtk_label_new ("");
245 gtk_misc_set_alignment (GTK_MISC (priv->label_unlock), 0, 0.5);
246 gtk_widget_set_no_show_all (priv->label_unlock, TRUE);
247 gtk_box_pack_start (GTK_BOX (priv->box), priv->label_unlock, FALSE, FALSE, 0);
248 gtk_size_group_add_widget (priv->label_group, priv->label_unlock);
250 names[0] = "changes-allow-symbolic";
251 names[1] = "changes-allow";
253 priv->icon_unlock = g_themed_icon_new_from_names (names, -1);
255 names[0] = "changes-prevent-symbolic";
256 names[1] = "changes-prevent";
258 priv->icon_lock = g_themed_icon_new_from_names (names, -1);
260 update_state (button);
264 gtk_lock_button_class_init (GtkLockButtonClass *klass)
266 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
267 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
268 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
270 gobject_class->finalize = gtk_lock_button_finalize;
271 gobject_class->get_property = gtk_lock_button_get_property;
272 gobject_class->set_property = gtk_lock_button_set_property;
274 button_class->clicked = gtk_lock_button_clicked;
276 g_type_class_add_private (klass, sizeof (GtkLockButtonPrivate));
278 g_object_class_install_property (gobject_class, PROP_PERMISSION,
279 g_param_spec_object ("permission",
281 P_("The GPermission object controlling this button"),
284 G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_TEXT_LOCK,
287 g_param_spec_string ("text-lock",
289 P_("The text to display when prompting the user to lock"),
293 G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (gobject_class, PROP_TEXT_UNLOCK,
296 g_param_spec_string ("text-unlock",
298 P_("The text to display when prompting the user to unlock"),
302 G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_TOOLTIP_LOCK,
305 g_param_spec_string ("tooltip-lock",
307 P_("The tooltip to display when prompting the user to lock"),
308 _("Dialog is unlocked.\nClick to prevent further changes"),
311 G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_TOOLTIP_UNLOCK,
314 g_param_spec_string ("tooltip-unlock",
315 P_("Unlock Tooltip"),
316 P_("The tooltip to display when prompting the user to unlock"),
317 _("Dialog is locked.\nClick to make changes"),
320 G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_TOOLTIP_NOT_AUTHORIZED,
323 g_param_spec_string ("tooltip-not-authorized",
324 P_("Not Authorized Tooltip"),
325 P_("The tooltip to display when prompting the user cannot obtain authorization"),
326 _("System policy prevents changes.\nContact your system administrator"),
329 G_PARAM_STATIC_STRINGS));
331 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LOCK_BUTTON_ACCESSIBLE);
335 update_state (GtkLockButton *button)
337 GtkLockButtonPrivate *priv = button->priv;
339 gboolean can_acquire;
340 gboolean can_release;
344 const gchar *tooltip;
346 if (priv->permission)
348 allowed = g_permission_get_allowed (priv->permission);
349 can_acquire = g_permission_get_can_acquire (priv->permission);
350 can_release = g_permission_get_can_release (priv->permission);
359 if (allowed && can_release)
363 icon = priv->icon_lock;
364 tooltip = priv->tooltip_lock;
366 else if (allowed && !can_release)
370 icon = priv->icon_lock;
371 tooltip = priv->tooltip_lock;
373 else if (!allowed && can_acquire)
377 icon = priv->icon_unlock;
378 tooltip = priv->tooltip_unlock;
380 else if (!allowed && !can_acquire)
384 icon = priv->icon_unlock;
385 tooltip = priv->tooltip_not_authorized;
389 g_assert_not_reached ();
392 gtk_image_set_from_gicon (GTK_IMAGE (priv->image), icon, GTK_ICON_SIZE_MENU);
393 if (gtk_widget_get_visible (priv->label_lock) != allowed)
395 gtk_widget_set_visible (priv->label_lock, allowed);
396 gtk_widget_set_visible (priv->label_unlock, !allowed);
397 _gtk_lock_button_accessible_name_changed (button);
399 gtk_widget_set_tooltip_markup (GTK_WIDGET (button), tooltip);
400 gtk_widget_set_sensitive (GTK_WIDGET (button), sensitive);
401 gtk_widget_set_visible (GTK_WIDGET (button), visible);
405 on_permission_changed (GPermission *permission,
409 GtkLockButton *button = GTK_LOCK_BUTTON (user_data);
411 update_state (button);
415 acquire_cb (GObject *source,
416 GAsyncResult *result,
419 GtkLockButton *button = GTK_LOCK_BUTTON (user_data);
420 GtkLockButtonPrivate *priv = button->priv;
424 if (!g_permission_acquire_finish (priv->permission, result, &error))
426 g_warning ("Error acquiring permission: %s", error->message);
427 g_error_free (error);
430 g_object_unref (priv->cancellable);
431 priv->cancellable = NULL;
433 update_state (button);
437 release_cb (GObject *source,
438 GAsyncResult *result,
441 GtkLockButton *button = GTK_LOCK_BUTTON (user_data);
442 GtkLockButtonPrivate *priv = button->priv;
446 if (!g_permission_release_finish (priv->permission, result, &error))
448 g_warning ("Error releasing permission: %s", error->message);
449 g_error_free (error);
452 g_object_unref (priv->cancellable);
453 priv->cancellable = NULL;
455 update_state (button);
459 gtk_lock_button_clicked (GtkButton *button)
461 GtkLockButtonPrivate *priv = GTK_LOCK_BUTTON (button)->priv;
463 /* if we already have a pending interactive check, then do nothing */
464 if (priv->cancellable != NULL)
467 if (g_permission_get_allowed (priv->permission))
469 if (g_permission_get_can_release (priv->permission))
471 priv->cancellable = g_cancellable_new ();
473 g_permission_release_async (priv->permission,
481 if (g_permission_get_can_acquire (priv->permission))
483 priv->cancellable = g_cancellable_new ();
485 g_permission_acquire_async (priv->permission,
494 * gtk_lock_button_new:
495 * @permission: (allow-none): a #GPermission
497 * Creates a new lock button which reflects the @permission.
499 * Returns: a new #GtkLockButton
504 gtk_lock_button_new (GPermission *permission)
506 return GTK_WIDGET (g_object_new (GTK_TYPE_LOCK_BUTTON,
507 "permission", permission,
512 * gtk_lock_button_get_permission:
513 * @button: a #GtkLockButton
515 * Obtains the #GPermission object that controls @button.
517 * Returns: (transfer none): the #GPermission of @button
522 gtk_lock_button_get_permission (GtkLockButton *button)
524 g_return_val_if_fail (GTK_IS_LOCK_BUTTON (button), NULL);
526 return button->priv->permission;
530 * gtk_lock_button_set_permission:
531 * @button: a #GtkLockButton
532 * @permission: (allow-none): a #GPermission object, or %NULL
534 * Sets the #GPermission object that controls @button.
539 gtk_lock_button_set_permission (GtkLockButton *button,
540 GPermission *permission)
542 GtkLockButtonPrivate *priv;
544 g_return_if_fail (GTK_IS_LOCK_BUTTON (button));
545 g_return_if_fail (permission == NULL || G_IS_PERMISSION (permission));
549 if (priv->permission != permission)
551 if (priv->permission)
553 g_signal_handlers_disconnect_by_func (priv->permission,
554 on_permission_changed,
556 g_object_unref (priv->permission);
559 priv->permission = permission;
561 if (priv->permission)
563 g_object_ref (priv->permission);
564 g_signal_connect (priv->permission, "notify",
565 G_CALLBACK (on_permission_changed), button);
568 update_state (button);
570 g_object_notify (G_OBJECT (button), "permission");
575 _gtk_lock_button_get_current_text (GtkLockButton *button)
577 GtkLockButtonPrivate *priv;
579 g_return_val_if_fail (GTK_IS_LOCK_BUTTON (button), NULL);
583 if (gtk_widget_get_visible (priv->label_lock))
584 return gtk_label_get_text (GTK_LABEL (priv->label_lock));
586 return gtk_label_get_text (GTK_LABEL (priv->label_unlock));