diff --git a/docs/reference/gtk/Makefile.am b/docs/reference/gtk/Makefile.am index c6b3b85bf7..2e865efc17 100644 --- a/docs/reference/gtk/Makefile.am +++ b/docs/reference/gtk/Makefile.am @@ -296,6 +296,9 @@ HTML_IMAGES = \ $(srcdir)/images/label.png \ $(srcdir)/images/link-button.png \ $(srcdir)/images/list-and-tree.png \ + $(srcdir)/images/lockbutton-locked.png \ + $(srcdir)/images/lockbutton-unlocked.png \ + $(srcdir)/images/lockbutton-sorry.png \ $(srcdir)/images/menubar.png \ $(srcdir)/images/messagedialog.png \ $(srcdir)/images/multiline-text.png \ diff --git a/docs/reference/gtk/gtk-docs.sgml b/docs/reference/gtk/gtk-docs.sgml index 967d50ea66..a2bd08298d 100644 --- a/docs/reference/gtk/gtk-docs.sgml +++ b/docs/reference/gtk/gtk-docs.sgml @@ -115,6 +115,7 @@ + diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt index 6f3cf4c8c1..d995d71567 100644 --- a/docs/reference/gtk/gtk3-sections.txt +++ b/docs/reference/gtk/gtk3-sections.txt @@ -7048,3 +7048,25 @@ GTK_APP_CHOOSER_WIDGET_GET_CLASS GtkAppChooserWidgetPrivate gtk_app_chooser_widget_get_type + +
+gtklockbutton +GtkLockButton +GtkLockButton + +gtk_lock_button_new +gtk_lock_button_get_permission +gtk_lock_button_set_permission + + +GtkLockButtonClass +GTK_TYPE_LOCK_BUTTON +GTK_LOCK_BUTTON +GTK_IS_LOCK_BUTTON +GTK_LOCK_BUTTON_CLASS +GTK_IS_LOCK_BUTTON_CLASS +GTK_LOCK_BUTTON_GET_CLASS + +gtk_lock_button_get_type +GtkLockButtonPrivate +
diff --git a/docs/reference/gtk/gtk3.types b/docs/reference/gtk/gtk3.types index bf2ca649eb..2b9cd79a43 100644 --- a/docs/reference/gtk/gtk3.types +++ b/docs/reference/gtk/gtk3.types @@ -92,6 +92,7 @@ gtk_label_get_type gtk_layout_get_type gtk_link_button_get_type gtk_list_store_get_type +gtk_lock_button_get_type gtk_menu_bar_get_type gtk_menu_get_type gtk_menu_item_get_type diff --git a/docs/reference/gtk/images/lockbutton-locked.png b/docs/reference/gtk/images/lockbutton-locked.png new file mode 100644 index 0000000000..ae4386a65a Binary files /dev/null and b/docs/reference/gtk/images/lockbutton-locked.png differ diff --git a/docs/reference/gtk/images/lockbutton-sorry.png b/docs/reference/gtk/images/lockbutton-sorry.png new file mode 100644 index 0000000000..8c492531b8 Binary files /dev/null and b/docs/reference/gtk/images/lockbutton-sorry.png differ diff --git a/docs/reference/gtk/images/lockbutton-unlocked.png b/docs/reference/gtk/images/lockbutton-unlocked.png new file mode 100644 index 0000000000..3ea5afcba1 Binary files /dev/null and b/docs/reference/gtk/images/lockbutton-unlocked.png differ diff --git a/docs/reference/gtk/images/lockbutton.png b/docs/reference/gtk/images/lockbutton.png new file mode 100644 index 0000000000..77441269f6 Binary files /dev/null and b/docs/reference/gtk/images/lockbutton.png differ diff --git a/docs/reference/gtk/visual_index.xml b/docs/reference/gtk/visual_index.xml index b1e1fe1237..37cc0ec6cf 100644 --- a/docs/reference/gtk/visual_index.xml +++ b/docs/reference/gtk/visual_index.xml @@ -51,6 +51,9 @@ + + + diff --git a/docs/tools/widgets.c b/docs/tools/widgets.c index 7709e3c993..6731ef0143 100644 --- a/docs/tools/widgets.c +++ b/docs/tools/widgets.c @@ -221,6 +221,54 @@ create_link_button (void) return new_widget_info ("link-button", align, SMALL); } +#define G_TYPE_TEST_PERMISSION (g_test_permission_get_type ()) +#define G_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \ + G_TYPE_TEST_PERMISSION, \ + GTestPermission)) +#define G_IS_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \ + G_TYPE_TEST_PERMISSION)) + +typedef struct _GTestPermission GTestPermission; +typedef struct _GTestPermissionClass GTestPermissionClass; + +struct _GTestPermission +{ + GPermission parent; + + gboolean success; +}; + +struct _GTestPermissionClass +{ + GPermissionClass parent_class; +}; + +G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION) + +static void +g_test_permission_init (GTestPermission *test) +{ + g_permission_impl_update (G_PERMISSION (test), FALSE, TRUE, TRUE); +} + +static void +g_test_permission_class_init (GTestPermissionClass *class) +{ +} + +static WidgetInfo * +create_lockbutton (void) +{ + GtkWidget *widget; + GtkWidget *align; + + widget = gtk_lock_button_new (g_object_new (G_TYPE_TEST_PERMISSION, NULL)); + align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0); + gtk_container_add (GTK_CONTAINER (align), widget); + + return new_widget_info ("lock-button", align, SMALL); +} + static WidgetInfo * create_entry (void) { @@ -1152,6 +1200,7 @@ get_all_widgets (void) retval = g_list_prepend (retval, create_switch ()); retval = g_list_prepend (retval, create_appchooserbutton ()); retval = g_list_prepend (retval, create_appchooserdialog ()); + retval = g_list_prepend (retval, create_lockbutton ()); return retval; } diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 96817e8953..b5587635b7 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -241,6 +241,7 @@ gtk_public_h_sources = \ gtklayout.h \ gtklinkbutton.h \ gtkliststore.h \ + gtklockbutton.h \ gtkmain.h \ gtkmenu.h \ gtkmenubar.h \ @@ -562,6 +563,7 @@ gtk_base_c_sources = \ gtklayout.c \ gtklinkbutton.c \ gtkliststore.c \ + gtklockbutton.c \ gtkmain.c \ gtkmarshalers.c \ gtkmenu.c \ diff --git a/gtk/gtk.h b/gtk/gtk.h index 0a72d249cf..6d6b416c09 100644 --- a/gtk/gtk.h +++ b/gtk/gtk.h @@ -126,6 +126,7 @@ #include #include #include +#include #include #include #include diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols index 71c80802ee..71bf1887b9 100644 --- a/gtk/gtk.symbols +++ b/gtk/gtk.symbols @@ -1445,6 +1445,10 @@ gtk_list_store_set_valist gtk_list_store_set_value gtk_list_store_set_valuesv gtk_list_store_swap +gtk_lock_button_get_type G_GNUC_CONST +gtk_lock_button_new +gtk_lock_button_get_permission +gtk_lock_button_set_permission gtk_main gtk_main_do_event gtk_main_iteration diff --git a/gtk/gtklockbutton.c b/gtk/gtklockbutton.c new file mode 100644 index 0000000000..913cfc2aa9 --- /dev/null +++ b/gtk/gtklockbutton.c @@ -0,0 +1,703 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2010 Red Hat, Inc. + * Author: Matthias Clasen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include "gtklockbutton.h" + +#include "gtkbutton.h" +#include "gtkbox.h" +#include "gtkeventbox.h" +#include "gtklabel.h" +#include "gtknotebook.h" +#include "gtkintl.h" + +/** + * SECTION:gtklockbutton + * @title: GtkLockButton + * @short_description: A widget to unlock or lock privileged operations + * + * GtkLockButton is a widget that can be used in control panels or + * preference dialogs to allow users to obtain and revoke authorizations + * needed to operate the controls. The required authorization is represented + * by a #GPermission object. Concrete implementations of #GPermission may use + * PolicyKit or some other authorization framework. + * + * If the user lacks the authorization but authorization can be obtained + * through authentication, the widget looks like this + * + * and the user can click the button to obtain the authorization. Depending + * on the platform, this may pop up an authentication dialog or ask the user + * to authenticate in some other way. Once authorization is obtained, the + * widget changes to this + * + * and the authorization can be dropped by clicking the button. If the user + * is not able to obtain authorization at all, the widget looks like this + * + * If the user is authorized and cannot drop the authorization, the button + * is hidden. + * + * The text (and tooltips) that are shown in the various cases can be adjusted + * with the #GtkLockButton:text-lock, #GtkLockButton:text-unlock, + * #GtkLockButton:text-not-authorized, #GtkLockButton:tooltip-lock, + * #GtkLockButton:tooltip-unlock and #GtkLockButton:tooltip-not-authorized + * properties. + */ + +struct _GtkLockButtonPrivate +{ + GPermission *permission; + + gchar *tooltip_lock; + gchar *tooltip_unlock; + gchar *tooltip_not_authorized; + + GtkWidget *box; + GtkWidget *eventbox; + GtkWidget *image; + GtkWidget *button; + GtkWidget *notebook; + + GtkWidget *label_lock; + GtkWidget *label_unlock; + GtkWidget *label_not_authorized; + + GCancellable *cancellable; +}; + +enum +{ + PROP_0, + PROP_PERMISSION, + PROP_TEXT_LOCK, + PROP_TEXT_UNLOCK, + PROP_TEXT_NOT_AUTHORIZED, + PROP_TOOLTIP_LOCK, + PROP_TOOLTIP_UNLOCK, + PROP_TOOLTIP_NOT_AUTHORIZED +}; + +static void update_state (GtkLockButton *button); +static void update_tooltip (GtkLockButton *button); + +static void on_permission_changed (GPermission *permission, + GParamSpec *pspec, + gpointer user_data); + +static void on_clicked (GtkButton *button, + gpointer user_data); + +static void on_button_press (GtkWidget *widget, + GdkEventButton *event, + gpointer user_data); + +G_DEFINE_TYPE (GtkLockButton, gtk_lock_button, GTK_TYPE_BIN); + +static void +gtk_lock_button_finalize (GObject *object) +{ + GtkLockButton *button = GTK_LOCK_BUTTON (object); + GtkLockButtonPrivate *priv = button->priv; + + g_free (priv->tooltip_lock); + g_free (priv->tooltip_unlock); + g_free (priv->tooltip_not_authorized); + + if (priv->cancellable != NULL) + { + g_cancellable_cancel (priv->cancellable); + g_object_unref (priv->cancellable); + } + + if (priv->permission) + { + g_signal_handlers_disconnect_by_func (priv->permission, + on_permission_changed, + button); + g_object_unref (priv->permission); + } + + G_OBJECT_CLASS (gtk_lock_button_parent_class)->finalize (object); +} + +static void +gtk_lock_button_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + GtkLockButton *button = GTK_LOCK_BUTTON (object); + GtkLockButtonPrivate *priv = button->priv; + + switch (property_id) + { + case PROP_PERMISSION: + g_value_set_object (value, priv->permission); + break; + + case PROP_TEXT_LOCK: + g_value_set_string (value, + gtk_label_get_text (GTK_LABEL (priv->label_lock))); + break; + + case PROP_TEXT_UNLOCK: + g_value_set_string (value, + gtk_label_get_text (GTK_LABEL (priv->label_unlock))); + break; + + case PROP_TEXT_NOT_AUTHORIZED: + g_value_set_string (value, + gtk_label_get_text (GTK_LABEL (priv->label_not_authorized))); + break; + + case PROP_TOOLTIP_LOCK: + g_value_set_string (value, priv->tooltip_lock); + break; + + case PROP_TOOLTIP_UNLOCK: + g_value_set_string (value, priv->tooltip_unlock); + break; + + case PROP_TOOLTIP_NOT_AUTHORIZED: + g_value_set_string (value, priv->tooltip_not_authorized); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +gtk_lock_button_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + GtkLockButton *button = GTK_LOCK_BUTTON (object); + GtkLockButtonPrivate *priv = button->priv; + + switch (property_id) + { + case PROP_PERMISSION: + gtk_lock_button_set_permission (button, g_value_get_object (value)); + break; + + case PROP_TEXT_LOCK: + gtk_label_set_text (GTK_LABEL (priv->label_lock), g_value_get_string (value)); + break; + + case PROP_TEXT_UNLOCK: + gtk_label_set_text (GTK_LABEL (priv->label_unlock), g_value_get_string (value)); + break; + + case PROP_TEXT_NOT_AUTHORIZED: + gtk_label_set_text (GTK_LABEL (priv->label_not_authorized), g_value_get_string (value)); + break; + + case PROP_TOOLTIP_LOCK: + g_free (priv->tooltip_lock); + priv->tooltip_lock = g_value_dup_string (value); + update_tooltip (button); + break; + + case PROP_TOOLTIP_UNLOCK: + g_free (priv->tooltip_unlock); + priv->tooltip_unlock = g_value_dup_string (value); + update_tooltip (button); + break; + + case PROP_TOOLTIP_NOT_AUTHORIZED: + g_free (priv->tooltip_not_authorized); + priv->tooltip_not_authorized = g_value_dup_string (value); + update_tooltip (button); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +gtk_lock_button_init (GtkLockButton *button) +{ + GtkLockButtonPrivate *priv; + + button->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (button, + GTK_TYPE_LOCK_BUTTON, + GtkLockButtonPrivate); + + priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); + gtk_container_add (GTK_CONTAINER (button), priv->box); + + priv->eventbox = gtk_event_box_new (); + gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->eventbox), FALSE); + gtk_container_add (GTK_CONTAINER (priv->box), priv->eventbox); + gtk_widget_show (priv->eventbox); + priv->image = gtk_image_new (); + gtk_container_add (GTK_CONTAINER (priv->eventbox), priv->image); + gtk_widget_show (priv->image); + + priv->notebook = gtk_notebook_new (); + gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->notebook), FALSE); + gtk_notebook_set_show_border (GTK_NOTEBOOK (priv->notebook), FALSE); + gtk_widget_show (priv->notebook); + + priv->button = gtk_button_new (); + gtk_container_add (GTK_CONTAINER (priv->button), priv->notebook); + gtk_widget_show (priv->button); + + priv->label_lock = gtk_label_new (""); + gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->label_lock, NULL); + gtk_widget_show (priv->label_lock); + + priv->label_unlock = gtk_label_new (""); + gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->label_unlock, NULL); + gtk_widget_show (priv->label_unlock); + + priv->label_not_authorized = gtk_label_new (""); + gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->label_not_authorized, NULL); + gtk_widget_show (priv->label_not_authorized); + + gtk_box_pack_start (GTK_BOX (priv->box), priv->button, FALSE, FALSE, 0); + gtk_widget_show (priv->button); + + g_signal_connect (priv->eventbox, "button-press-event", + G_CALLBACK (on_button_press), button); + g_signal_connect (priv->button, "clicked", + G_CALLBACK (on_clicked), button); + + gtk_widget_set_no_show_all (priv->box, TRUE); + + update_state (button); +} + +static void +gtk_lock_button_get_preferred_width (GtkWidget *widget, + gint *minimum, + gint *natural) +{ + GtkLockButtonPrivate *priv = GTK_LOCK_BUTTON (widget)->priv; + + gtk_widget_get_preferred_width (priv->box, minimum, natural); +} + +static void +gtk_lock_button_get_preferred_height (GtkWidget *widget, + gint *minimum, + gint *natural) +{ + GtkLockButtonPrivate *priv = GTK_LOCK_BUTTON (widget)->priv; + + gtk_widget_get_preferred_height (priv->box, minimum, natural); +} + +static void +gtk_lock_button_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkLockButtonPrivate *priv = GTK_LOCK_BUTTON (widget)->priv; + GtkRequisition requisition; + GtkAllocation child_allocation; + + gtk_widget_set_allocation (widget, allocation); + gtk_widget_get_preferred_size (priv->box, &requisition, NULL); + child_allocation.x = allocation->x; + child_allocation.y = allocation->y; + child_allocation.width = requisition.width; + child_allocation.height = requisition.height; + gtk_widget_size_allocate (priv->box, &child_allocation); +} + +static void +gtk_lock_button_class_init (GtkLockButtonClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + gobject_class->finalize = gtk_lock_button_finalize; + gobject_class->get_property = gtk_lock_button_get_property; + gobject_class->set_property = gtk_lock_button_set_property; + + widget_class->get_preferred_width = gtk_lock_button_get_preferred_width; + widget_class->get_preferred_height = gtk_lock_button_get_preferred_height; + widget_class->size_allocate = gtk_lock_button_size_allocate; + + g_type_class_add_private (klass, sizeof (GtkLockButtonPrivate)); + + g_object_class_install_property (gobject_class, PROP_PERMISSION, + g_param_spec_object ("permission", + P_("Permission"), + P_("The GPermission object controlling this button"), + G_TYPE_PERMISSION, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TEXT_LOCK, + g_param_spec_string ("text-lock", + P_("Lock Text"), + P_("The text to display when prompting the user to lock"), + _("Lock"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TEXT_UNLOCK, + g_param_spec_string ("text-unlock", + P_("Unlock Text"), + P_("The text to display when prompting the user to unlock"), + _("Unlock"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TEXT_NOT_AUTHORIZED, + g_param_spec_string ("text-not-authorized", + P_("Not Authorized Text"), + P_("The text to display when prompting the user cannot obtain authorization"), + _("Locked"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TOOLTIP_LOCK, + g_param_spec_string ("tooltip-lock", + P_("Lock Tooltip"), + P_("The tooltip to display when prompting the user to lock"), + _("Dialog is unlocked.\nClick to prevent further changes"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TOOLTIP_UNLOCK, + g_param_spec_string ("tooltip-unlock", + P_("Unlock Tooltip"), + P_("The tooltip to display when prompting the user to unlock"), + _("Dialog is locked.\nClick to make changes"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property (gobject_class, PROP_TOOLTIP_NOT_AUTHORIZED, + g_param_spec_string ("tooltip-not-authorized", + P_("Not Authorized Tooltip"), + P_("The tooltip to display when prompting the user cannot obtain authorization"), + _("System policy prevents changes.\nContact your system administrator"), + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); +} + +static void +update_tooltip (GtkLockButton *button) +{ + GtkLockButtonPrivate *priv = button->priv; + const gchar *tooltip; + + switch (gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook))) + { + case 0: + tooltip = priv->tooltip_lock; + break; + case 1: + tooltip = priv->tooltip_unlock; + break; + case 2: + tooltip = priv->tooltip_not_authorized; + break; + default: + tooltip = ""; + break; + } + + gtk_widget_set_tooltip_markup (priv->box, tooltip); +} + +static void +update_state (GtkLockButton *button) +{ + GtkLockButtonPrivate *priv = button->priv; + gboolean allowed; + gboolean can_acquire; + gboolean can_release; + gint page; + gboolean sensitive; + gboolean visible; + GIcon *icon; + + if (priv->permission) + { + allowed = g_permission_get_allowed (priv->permission); + can_acquire = g_permission_get_can_acquire (priv->permission); + can_release = g_permission_get_can_release (priv->permission); + } + else + { + allowed = FALSE; + can_acquire = FALSE; + can_release = FALSE; + } + + visible = TRUE; + sensitive = TRUE; + + if (allowed) + { + if (can_release) + { + page = 0; + sensitive = TRUE; + } + else + { + page = 0; + visible = FALSE; + } + } + else + { + if (can_acquire) + { + page = 1; + sensitive = TRUE; + } + else + { + page = 2; + sensitive = FALSE; + } + } + + if (allowed) + { + gchar *names[3]; + + names[0] = "changes-allow-symbolic"; + names[1] = "changes-allow"; + names[2] = NULL; + icon = g_themed_icon_new_from_names (names, -1); + } + else + { + gchar *names[3]; + + names[0] = "changes-prevent-symbolic"; + names[1] = "changes-prevent"; + names[2] = NULL; + icon = g_themed_icon_new_from_names (names, -1); + } + + gtk_image_set_from_gicon (GTK_IMAGE (priv->image), icon, GTK_ICON_SIZE_BUTTON); + g_object_unref (icon); + + gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), page); + gtk_widget_set_sensitive (priv->box, sensitive); + gtk_widget_set_visible (priv->box, visible); + + update_tooltip (button); +} + +static void +on_permission_changed (GPermission *permission, + GParamSpec *pspec, + gpointer user_data) +{ + GtkLockButton *button = GTK_LOCK_BUTTON (user_data); + + update_state (button); +} + +static void +acquire_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + GtkLockButton *button = GTK_LOCK_BUTTON (user_data); + GtkLockButtonPrivate *priv = button->priv; + GError *error; + + error = NULL; + if (!g_permission_acquire_finish (priv->permission, result, &error)) + { + g_warning ("Error acquiring permission: %s", error->message); + g_error_free (error); + } + + g_object_unref (priv->cancellable); + priv->cancellable = NULL; + + update_state (button); +} + +static void +release_cb (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + GtkLockButton *button = GTK_LOCK_BUTTON (user_data); + GtkLockButtonPrivate *priv = button->priv; + GError *error; + + error = NULL; + if (!g_permission_release_finish (priv->permission, result, &error)) + { + g_warning ("Error releasing permission: %s", error->message); + g_error_free (error); + } + + g_object_unref (priv->cancellable); + priv->cancellable = NULL; + + update_state (button); +} + +static void +handle_click (GtkLockButton *button) +{ + GtkLockButtonPrivate *priv = button->priv; + + /* if we already have a pending interactive check, then do nothing */ + if (priv->cancellable != NULL) + return; + + if (g_permission_get_allowed (priv->permission)) + { + if (g_permission_get_can_release (priv->permission)) + { + priv->cancellable = g_cancellable_new (); + + g_permission_release_async (priv->permission, + priv->cancellable, + release_cb, + button); + } + } + else + { + if (g_permission_get_can_acquire (priv->permission)) + { + priv->cancellable = g_cancellable_new (); + + g_permission_acquire_async (priv->permission, + priv->cancellable, + acquire_cb, + button); + } + } +} + +static void +on_clicked (GtkButton *button, + gpointer user_data) + +{ + handle_click (GTK_LOCK_BUTTON (user_data)); +} + +static void +on_button_press (GtkWidget *widget, + GdkEventButton *event, + gpointer user_data) +{ + handle_click (GTK_LOCK_BUTTON (user_data)); +} + + +/** + * gtk_lock_button_new: + * @permission: (allow-none): a #GPermission + * + * Creates a new lock button which reflects the @permission. + * + * Returns: a new #GtkLockButton + * + * Since: 3.2 + */ +GtkWidget * +gtk_lock_button_new (GPermission *permission) +{ + return GTK_WIDGET (g_object_new (GTK_TYPE_LOCK_BUTTON, + "permission", permission, + NULL)); +} + +/** + * gtk_lock_button_get_permission: + * @button: a #GtkLockButton + * + * Obtains the #GPermission object that controls @button. + * + * Returns: the #GPermission of @button + * + * Since: 3.2 + */ +GPermission * +gtk_lock_button_get_permission (GtkLockButton *button) +{ + g_return_val_if_fail (GTK_IS_LOCK_BUTTON (button), NULL); + + return button->priv->permission; +} + +/** + * gtk_lock_button_set_permission: + * @button: a #GtkLockButton + * @permission: a #GPermission object + * + * Sets the #GPermission object that controls @button. + * + * Since: 3.2 + */ +void +gtk_lock_button_set_permission (GtkLockButton *button, + GPermission *permission) +{ + GtkLockButtonPrivate *priv; + + g_return_if_fail (GTK_IS_LOCK_BUTTON (button)); + g_return_if_fail (G_IS_PERMISSION (permission)); + + priv = button->priv; + + if (priv->permission != permission) + { + if (priv->permission) + { + g_signal_handlers_disconnect_by_func (priv->permission, + on_permission_changed, + button); + g_object_unref (priv->permission); + } + + priv->permission = permission; + + if (priv->permission) + { + g_object_ref (priv->permission); + g_signal_connect (priv->permission, "notify", + G_CALLBACK (on_permission_changed), button); + } + + update_state (button); + + g_object_notify (G_OBJECT (button), "permission"); + } +} diff --git a/gtk/gtklockbutton.h b/gtk/gtklockbutton.h new file mode 100644 index 0000000000..abc100ed22 --- /dev/null +++ b/gtk/gtklockbutton.h @@ -0,0 +1,70 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2010 Red Hat, Inc. + * Author: Matthias Clasen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GTK_LOCK_BUTTON_H__ +#define __GTK_LOCK_BUTTON_H__ + +#include +#include + +G_BEGIN_DECLS + +#define GTK_TYPE_LOCK_BUTTON (gtk_lock_button_get_type ()) +#define GTK_LOCK_BUTTON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_LOCK_BUTTON, GtkLockButton)) +#define GTK_LOCK_BUTTON_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GTK_LOCK_BUTTON, GtkLockButtonClass)) +#define GTK_IS_LOCK_BUTTON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_LOCK_BUTTON)) +#define GTK_IS_LOCK_BUTTON_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_LOCK_BUTTON)) +#define GTK_LOCK_BUTTON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_LOCK_BUTTON, GtkLockButtonClass)) + +typedef struct _GtkLockButton GtkLockButton; +typedef struct _GtkLockButtonClass GtkLockButtonClass; +typedef struct _GtkLockButtonPrivate GtkLockButtonPrivate; + +struct _GtkLockButton +{ + GtkBin parent; + + GtkLockButtonPrivate *priv; +}; + +struct _GtkLockButtonClass +{ + GtkBinClass parent_class; + + void (*reserved0) (void); + void (*reserved1) (void); + void (*reserved2) (void); + void (*reserved3) (void); + void (*reserved4) (void); + void (*reserved5) (void); + void (*reserved6) (void); + void (*reserved7) (void); +}; + +GType gtk_lock_button_get_type (void) G_GNUC_CONST; +GtkWidget *gtk_lock_button_new (GPermission *permission); +GPermission *gtk_lock_button_get_permission (GtkLockButton *button); +void gtk_lock_button_set_permission (GtkLockButton *button, + GPermission *permission); + + +G_END_DECLS + +#endif /* __GTK_LOCK_BUTTON_H__ */ diff --git a/po-properties/POTFILES.in b/po-properties/POTFILES.in index c252d5e279..dfb3b79e87 100644 --- a/po-properties/POTFILES.in +++ b/po-properties/POTFILES.in @@ -118,6 +118,7 @@ gtk/gtklabel.c gtk/gtklayout.c gtk/gtklinkbutton.c gtk/gtkliststore.c +gtk/gtklockbutton.c gtk/gtkmain.c gtk/gtkmenubar.c gtk/gtkmenu.c diff --git a/po/POTFILES.in b/po/POTFILES.in index a57514ba91..cb67139064 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -118,6 +118,7 @@ gtk/gtklabel.c gtk/gtklayout.c gtk/gtklinkbutton.c gtk/gtkliststore.c +gtk/gtklockbutton.c gtk/gtkmain.c gtk/gtkmenubar.c gtk/gtkmenu.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 3ea7981cc0..8c87a51460 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -59,6 +59,7 @@ noinst_PROGRAMS = $(TEST_PROGS) \ testicontheme \ testimage \ testinput \ + testlockbutton \ testmenubars \ testmountoperation \ testmultidisplay \ @@ -158,6 +159,7 @@ testgrid_DEPENDENCIES = $(TEST_DEPS) testgtk_DEPENDENCIES = $(TEST_DEPS) testinput_DEPENDENCIES = $(TEST_DEPS) testimage_DEPENDENCIES = $(TEST_DEPS) +testlockbutton_DEPENDENCIES = $(TEST_DEPS) testmenubars_DEPENDENCIES = $(TEST_DEPS) testmountoperation_DEPENDENCIES = $(TEST_DEPS) testmultidisplay_DEPENDENCIES = $(TEST_DEPS) @@ -241,6 +243,7 @@ testiconview_LDADD = $(LDADDS) testiconview_keynav_LDADD = $(LDADDS) testinput_LDADD = $(LDADDS) testimage_LDADD = $(LDADDS) +testlockbutton_LDADD = $(LDADDS) testmenubars_LDADD = $(LDADDS) testmountoperation_LDADD = $(LDADDS) testmultidisplay_LDADD = $(LDADDS) diff --git a/tests/testlockbutton.c b/tests/testlockbutton.c new file mode 100644 index 0000000000..f6bbbd4b71 --- /dev/null +++ b/tests/testlockbutton.c @@ -0,0 +1,277 @@ +/* testlockbutton.c + * Copyright (C) 2011 Red Hat, Inc. + * Authors: Matthias Clasen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include +#include + +/* a fake permission implementation */ + +#define G_TYPE_TEST_PERMISSION (g_test_permission_get_type ()) +#define G_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \ + G_TYPE_TEST_PERMISSION, \ + GTestPermission)) +#define G_IS_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \ + G_TYPE_TEST_PERMISSION)) + +typedef struct _GTestPermission GTestPermission; +typedef struct _GTestPermissionClass GTestPermissionClass; + +struct _GTestPermission +{ + GPermission parent; + + gboolean success; +}; + +struct _GTestPermissionClass +{ + GPermissionClass parent_class; +}; + +G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION) + +static void +g_test_permission_init (GTestPermission *test) +{ +} + +static gboolean +update_allowed (GTestPermission *test, + gboolean allowed, + GError **error) +{ + gboolean can_acquire, can_release; + + g_object_get (test, + "can-acquire", &can_acquire, + "can-release", &can_release, + NULL); + + if (test->success) + { + g_permission_impl_update (G_PERMISSION (test), + allowed, can_acquire, can_release); + return TRUE; + } + else + { + g_set_error_literal (error, + G_IO_ERROR, G_IO_ERROR_FAILED, "Sorry, no luck"); + return FALSE; + } +} + +static gboolean +acquire (GPermission *permission, + GCancellable *cancellable, + GError **error) +{ + GTestPermission *test = G_TEST_PERMISSION (permission); + return update_allowed (test, TRUE, error); +} + +static void +acquire_async (GPermission *permission, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + g_print ("GTestPermission::acquire_async\n"); + result = g_simple_async_result_new ((GObject*)permission, + callback, + user_data, + acquire_async); + g_simple_async_result_complete (result); + g_object_unref (result); +} + +gboolean +acquire_finish (GPermission *permission, + GAsyncResult *result, + GError **error) +{ + GTestPermission *test = G_TEST_PERMISSION (permission); + g_print ("GTestPermission::acquire_finish\n"); + return update_allowed (test, TRUE, error); +} + +static gboolean +release (GPermission *permission, + GCancellable *cancellable, + GError **error) +{ + GTestPermission *test = G_TEST_PERMISSION (permission); + return update_allowed (test, FALSE, error); +} + +static void +release_async (GPermission *permission, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + result = g_simple_async_result_new ((GObject*)permission, + callback, + user_data, + acquire_async); + g_simple_async_result_complete (result); + g_object_unref (result); +} + +gboolean +release_finish (GPermission *permission, + GAsyncResult *result, + GError **error) +{ + GTestPermission *test = G_TEST_PERMISSION (permission); + return update_allowed (test, FALSE, error); +} + +static void +g_test_permission_class_init (GTestPermissionClass *class) +{ + GPermissionClass *permission_class = G_PERMISSION_CLASS (class); + + permission_class->acquire = acquire; + permission_class->acquire_async = acquire_async; + permission_class->acquire_finish = acquire_finish; + + permission_class->release = release; + permission_class->release_async = release_async; + permission_class->release_finish = release_finish; +} + +void +g_test_permission_set_success (GTestPermission *permission, + gboolean success) +{ + permission->success = success; +} + +static GtkWidget *allowed_button; +static GtkWidget *can_acquire_button; +static GtkWidget *can_release_button; +static GtkWidget *success_button; + +static void +update_clicked (GtkButton *button, GtkLockButton *lockbutton) +{ + GPermission *permission; + gboolean allowed, can_acquire, can_release; + gboolean success; + + permission = gtk_lock_button_get_permission (lockbutton); + + allowed = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (allowed_button)); + can_acquire = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_acquire_button)); + can_release = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_release_button)); + success = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (success_button)); + g_permission_impl_update (permission, allowed, can_acquire, can_release); + g_test_permission_set_success (G_TEST_PERMISSION (permission), success); +} + +static GtkWidget *content; + +static void +permission_changed (GPermission *permission, + GParamSpec *pspec) +{ + gboolean allowed, can_acquire, can_release; + + g_object_get (permission, + "allowed", &allowed, + "can-acquire", &can_acquire, + "can-release", &can_release, + NULL); + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (allowed_button), allowed); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_acquire_button), can_acquire); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_release_button), can_release); + + gtk_widget_set_sensitive (content, allowed); +} + +int +main (int argc, char *argv[]) +{ + GtkWidget *window; + GtkWidget *dialog; + GtkWidget *button; + GtkWidget *box; + GtkWidget *bbox; + GtkWidget *update; + GPermission *permission; + + gtk_init (&argc, &argv); + + permission = g_object_new (G_TYPE_TEST_PERMISSION, NULL); + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_resizable (GTK_WINDOW (window), FALSE); + gtk_container_set_border_width (GTK_CONTAINER (window), 12); + + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), box); + + allowed_button = gtk_check_button_new_with_label ("Allowed"); + gtk_container_add (GTK_CONTAINER (box), allowed_button); + can_acquire_button = gtk_check_button_new_with_label ("Can acquire"); + gtk_container_add (GTK_CONTAINER (box), can_acquire_button); + can_release_button = gtk_check_button_new_with_label ("Can release"); + gtk_container_add (GTK_CONTAINER (box), can_release_button); + success_button = gtk_check_button_new_with_label ("Will succeed"); + gtk_container_add (GTK_CONTAINER (box), success_button); + update = gtk_button_new_with_label ("Update"); + gtk_container_add (GTK_CONTAINER (box), update); + g_signal_connect (permission, "notify", + G_CALLBACK (permission_changed), NULL); + + button = gtk_lock_button_new (permission); + + g_signal_connect (update, "clicked", + G_CALLBACK (update_clicked), button); + + dialog = gtk_dialog_new_with_buttons ("Dialog", NULL, 0, + "Close", GTK_RESPONSE_CLOSE, + "Some other action", GTK_RESPONSE_APPLY, + NULL); + gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE); + + bbox = gtk_dialog_get_action_area (GTK_DIALOG (dialog)); + gtk_container_add (GTK_CONTAINER (bbox), button); + gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox), button, TRUE); + gtk_button_box_set_child_non_homogeneous (GTK_BUTTON_BOX (bbox), button, TRUE); + + content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 1")); + gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 2")); + gtk_widget_set_sensitive (content, FALSE); + + gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), content); + + gtk_widget_show_all (window); + gtk_widget_show_all (dialog); + + gtk_main (); + + return 0; +}