2011-04-30 03:40:47 +00:00
|
|
|
/* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-04-30 03:40:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-07-09 00:58:22 +00:00
|
|
|
#include "gtklockbuttonprivate.h"
|
2018-02-08 23:58:59 +00:00
|
|
|
|
2011-04-30 03:40:47 +00:00
|
|
|
#include "gtkbox.h"
|
2011-05-26 00:19:08 +00:00
|
|
|
#include "gtkimage.h"
|
2018-02-08 23:58:59 +00:00
|
|
|
#include "gtkintl.h"
|
2011-04-30 03:40:47 +00:00
|
|
|
#include "gtklabel.h"
|
2011-05-26 00:19:08 +00:00
|
|
|
#include "gtksizegroup.h"
|
2018-02-08 23:58:59 +00:00
|
|
|
#include "gtkstack.h"
|
|
|
|
#include "gtkstylecontext.h"
|
|
|
|
|
2011-04-30 03:40:47 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2012-05-26 17:29:48 +00:00
|
|
|
* PolicyKit or some other authorization framework. To obtain a PolicyKit-based
|
|
|
|
* #GPermission, use polkit_permission_new().
|
2011-04-30 03:40:47 +00:00
|
|
|
*
|
2012-05-26 17:29:48 +00:00
|
|
|
* If the user is not currently allowed to perform the action, but can obtain
|
2014-02-03 22:19:27 +00:00
|
|
|
* the permission, the widget looks like this:
|
|
|
|
*
|
|
|
|
* ![](lockbutton-locked.png)
|
|
|
|
*
|
2012-05-26 17:29:48 +00:00
|
|
|
* and the user can click the button to request the permission. Depending
|
2011-04-30 03:40:47 +00:00
|
|
|
* on the platform, this may pop up an authentication dialog or ask the user
|
2012-05-26 17:29:48 +00:00
|
|
|
* to authenticate in some other way. Once the user has obtained the permission,
|
2014-02-03 22:19:27 +00:00
|
|
|
* the widget changes to this:
|
|
|
|
*
|
|
|
|
* ![](lockbutton-unlocked.png)
|
|
|
|
*
|
2012-05-26 17:29:48 +00:00
|
|
|
* and the permission can be dropped again by clicking the button. If the user
|
2014-02-03 22:19:27 +00:00
|
|
|
* is not able to obtain the permission at all, the widget looks like this:
|
|
|
|
*
|
|
|
|
* ![](lockbutton-sorry.png)
|
|
|
|
*
|
2012-05-26 17:29:48 +00:00
|
|
|
* If the user has the permission and cannot drop it, the button is hidden.
|
2011-04-30 03:40:47 +00:00
|
|
|
*
|
|
|
|
* The text (and tooltips) that are shown in the various cases can be adjusted
|
|
|
|
* with the #GtkLockButton:text-lock, #GtkLockButton:text-unlock,
|
2011-05-26 00:19:08 +00:00
|
|
|
* #GtkLockButton:tooltip-lock, #GtkLockButton:tooltip-unlock and
|
|
|
|
* #GtkLockButton:tooltip-not-authorized properties.
|
2011-04-30 03:40:47 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-19 21:12:24 +00:00
|
|
|
struct _GtkLockButton
|
|
|
|
{
|
|
|
|
GtkButton parent_instance;
|
|
|
|
|
2011-04-30 03:40:47 +00:00
|
|
|
GPermission *permission;
|
2011-05-26 00:19:08 +00:00
|
|
|
GCancellable *cancellable;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char *tooltip_lock;
|
|
|
|
char *tooltip_unlock;
|
|
|
|
char *tooltip_not_authorized;
|
2011-05-26 00:19:08 +00:00
|
|
|
GIcon *icon_lock;
|
|
|
|
GIcon *icon_unlock;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *image;
|
2016-01-03 14:55:30 +00:00
|
|
|
GtkWidget *stack;
|
2011-04-30 03:40:47 +00:00
|
|
|
GtkWidget *label_lock;
|
|
|
|
GtkWidget *label_unlock;
|
|
|
|
};
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
typedef struct _GtkLockButtonClass GtkLockButtonClass;
|
|
|
|
struct _GtkLockButtonClass
|
|
|
|
{
|
|
|
|
GtkButtonClass parent_class;
|
|
|
|
};
|
|
|
|
|
2011-04-30 03:40:47 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_PERMISSION,
|
|
|
|
PROP_TEXT_LOCK,
|
|
|
|
PROP_TEXT_UNLOCK,
|
|
|
|
PROP_TOOLTIP_LOCK,
|
|
|
|
PROP_TOOLTIP_UNLOCK,
|
|
|
|
PROP_TOOLTIP_NOT_AUTHORIZED
|
|
|
|
};
|
|
|
|
|
2011-05-26 00:19:08 +00:00
|
|
|
static void update_state (GtkLockButton *button);
|
|
|
|
static void gtk_lock_button_clicked (GtkButton *button);
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
static void on_permission_changed (GPermission *permission,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer user_data);
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
G_DEFINE_TYPE (GtkLockButton, gtk_lock_button, GTK_TYPE_BUTTON)
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_lock_button_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkLockButton *button = GTK_LOCK_BUTTON (object);
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
g_free (button->tooltip_lock);
|
|
|
|
g_free (button->tooltip_unlock);
|
|
|
|
g_free (button->tooltip_not_authorized);
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
g_object_unref (button->icon_lock);
|
|
|
|
g_object_unref (button->icon_unlock);
|
2011-05-26 00:19:08 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->cancellable != NULL)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
g_cancellable_cancel (button->cancellable);
|
|
|
|
g_object_unref (button->cancellable);
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->permission)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (button->permission,
|
2011-04-30 03:40:47 +00:00
|
|
|
on_permission_changed,
|
|
|
|
button);
|
2020-04-13 23:17:11 +00:00
|
|
|
g_object_unref (button->permission);
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_PERMISSION:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_value_set_object (value, button->permission);
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TEXT_LOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_value_set_string (value, gtk_label_get_text (GTK_LABEL (button->label_lock)));
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TEXT_UNLOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_value_set_string (value, gtk_label_get_text (GTK_LABEL (button->label_unlock)));
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TOOLTIP_LOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_value_set_string (value, button->tooltip_lock);
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TOOLTIP_UNLOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_value_set_string (value, button->tooltip_unlock);
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TOOLTIP_NOT_AUTHORIZED:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_value_set_string (value, button->tooltip_not_authorized);
|
2011-04-30 03:40:47 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_PERMISSION:
|
|
|
|
gtk_lock_button_set_permission (button, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TEXT_LOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
gtk_label_set_text (GTK_LABEL (button->label_lock), g_value_get_string (value));
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TEXT_UNLOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
gtk_label_set_text (GTK_LABEL (button->label_unlock), g_value_get_string (value));
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TOOLTIP_LOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_free (button->tooltip_lock);
|
|
|
|
button->tooltip_lock = g_value_dup_string (value);
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TOOLTIP_UNLOCK:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_free (button->tooltip_unlock);
|
|
|
|
button->tooltip_unlock = g_value_dup_string (value);
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TOOLTIP_NOT_AUTHORIZED:
|
2020-04-13 23:17:11 +00:00
|
|
|
g_free (button->tooltip_not_authorized);
|
|
|
|
button->tooltip_not_authorized = g_value_dup_string (value);
|
2011-04-30 03:40:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2011-05-26 00:19:08 +00:00
|
|
|
|
|
|
|
update_state (button);
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_lock_button_init (GtkLockButton *button)
|
|
|
|
{
|
2016-10-15 20:32:51 +00:00
|
|
|
const char *names[3];
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2013-03-23 08:52:48 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (button));
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2011-05-26 00:19:08 +00:00
|
|
|
names[0] = "changes-allow-symbolic";
|
|
|
|
names[1] = "changes-allow";
|
|
|
|
names[2] = NULL;
|
2020-04-13 23:17:11 +00:00
|
|
|
button->icon_unlock = g_themed_icon_new_from_names ((char **) names, -1);
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2011-05-26 00:19:08 +00:00
|
|
|
names[0] = "changes-prevent-symbolic";
|
|
|
|
names[1] = "changes-prevent";
|
|
|
|
names[2] = NULL;
|
2020-04-13 23:17:11 +00:00
|
|
|
button->icon_lock = g_themed_icon_new_from_names ((char **) names, -1);
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
update_state (button);
|
2015-10-30 04:26:04 +00:00
|
|
|
|
2020-02-06 16:32:26 +00:00
|
|
|
gtk_widget_add_css_class (GTK_WIDGET (button), I_("lock"));
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_lock_button_class_init (GtkLockButtonClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2012-07-09 00:29:16 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2011-05-26 00:19:08 +00:00
|
|
|
GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-05-26 00:19:08 +00:00
|
|
|
button_class->clicked = gtk_lock_button_clicked;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
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_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));
|
2012-07-09 00:29:16 +00:00
|
|
|
|
2013-03-23 08:52:48 +00:00
|
|
|
/* Bind class to template
|
|
|
|
*/
|
2014-01-23 23:59:20 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtklockbutton.ui");
|
2020-04-13 23:17:11 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkLockButton, box);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkLockButton, image);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkLockButton, label_lock);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkLockButton, label_unlock);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkLockButton, stack);
|
2013-03-23 08:52:48 +00:00
|
|
|
|
2017-11-18 03:49:57 +00:00
|
|
|
gtk_widget_class_set_css_name (widget_class, I_("button"));
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_state (GtkLockButton *button)
|
|
|
|
{
|
|
|
|
gboolean allowed;
|
|
|
|
gboolean can_acquire;
|
|
|
|
gboolean can_release;
|
|
|
|
gboolean sensitive;
|
|
|
|
gboolean visible;
|
|
|
|
GIcon *icon;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *tooltip;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->permission)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
allowed = g_permission_get_allowed (button->permission);
|
|
|
|
can_acquire = g_permission_get_can_acquire (button->permission);
|
|
|
|
can_release = g_permission_get_can_release (button->permission);
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-26 00:19:08 +00:00
|
|
|
allowed = TRUE;
|
2011-04-30 03:40:47 +00:00
|
|
|
can_acquire = FALSE;
|
|
|
|
can_release = FALSE;
|
|
|
|
}
|
|
|
|
|
2011-05-26 00:19:08 +00:00
|
|
|
if (allowed && can_release)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2011-05-26 00:19:08 +00:00
|
|
|
visible = TRUE;
|
|
|
|
sensitive = TRUE;
|
2020-04-13 23:17:11 +00:00
|
|
|
icon = button->icon_lock;
|
|
|
|
tooltip = button->tooltip_lock;
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
2011-05-26 00:19:08 +00:00
|
|
|
else if (allowed && !can_release)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2011-05-26 00:19:08 +00:00
|
|
|
visible = FALSE;
|
|
|
|
sensitive = TRUE;
|
2020-04-13 23:17:11 +00:00
|
|
|
icon = button->icon_lock;
|
|
|
|
tooltip = button->tooltip_lock;
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
2011-05-26 00:19:08 +00:00
|
|
|
else if (!allowed && can_acquire)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2011-05-26 00:19:08 +00:00
|
|
|
visible = TRUE;
|
|
|
|
sensitive = TRUE;
|
2020-04-13 23:17:11 +00:00
|
|
|
icon = button->icon_unlock;
|
|
|
|
tooltip = button->tooltip_unlock;
|
2011-05-26 00:19:08 +00:00
|
|
|
}
|
|
|
|
else if (!allowed && !can_acquire)
|
|
|
|
{
|
|
|
|
visible = TRUE;
|
|
|
|
sensitive = FALSE;
|
2020-04-13 23:17:11 +00:00
|
|
|
icon = button->icon_unlock;
|
|
|
|
tooltip = button->tooltip_not_authorized;
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-26 00:19:08 +00:00
|
|
|
g_assert_not_reached ();
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
gtk_image_set_from_gicon (GTK_IMAGE (button->image), icon);
|
|
|
|
gtk_stack_set_visible_child (GTK_STACK (button->stack),
|
|
|
|
allowed ? button->label_lock : button->label_unlock);
|
2011-05-26 00:19:08 +00:00
|
|
|
gtk_widget_set_tooltip_markup (GTK_WIDGET (button), tooltip);
|
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (button), sensitive);
|
|
|
|
gtk_widget_set_visible (GTK_WIDGET (button), visible);
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
2020-04-13 23:17:11 +00:00
|
|
|
if (!g_permission_acquire_finish (button->permission, result, &error))
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
|
|
|
g_warning ("Error acquiring permission: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
g_object_unref (button->cancellable);
|
|
|
|
button->cancellable = NULL;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
update_state (button);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
release_cb (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkLockButton *button = GTK_LOCK_BUTTON (user_data);
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
2020-04-13 23:17:11 +00:00
|
|
|
if (!g_permission_release_finish (button->permission, result, &error))
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
|
|
|
g_warning ("Error releasing permission: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
g_object_unref (button->cancellable);
|
|
|
|
button->cancellable = NULL;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
|
|
|
update_state (button);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-13 23:17:11 +00:00
|
|
|
gtk_lock_button_clicked (GtkButton *widget)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
GtkLockButton *button = GTK_LOCK_BUTTON (widget);
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2014-04-25 22:18:28 +00:00
|
|
|
/* if we already have a pending interactive check or permission is not set,
|
|
|
|
* then do nothing
|
|
|
|
*/
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->cancellable != NULL || button->permission == NULL)
|
2011-04-30 03:40:47 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
if (g_permission_get_allowed (button->permission))
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
if (g_permission_get_can_release (button->permission))
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
button->cancellable = g_cancellable_new ();
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
g_permission_release_async (button->permission,
|
|
|
|
button->cancellable,
|
2011-04-30 03:40:47 +00:00
|
|
|
release_cb,
|
|
|
|
button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
if (g_permission_get_can_acquire (button->permission))
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
button->cancellable = g_cancellable_new ();
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
g_permission_acquire_async (button->permission,
|
|
|
|
button->cancellable,
|
2011-04-30 03:40:47 +00:00
|
|
|
acquire_cb,
|
|
|
|
button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_lock_button_new:
|
|
|
|
* @permission: (allow-none): a #GPermission
|
|
|
|
*
|
|
|
|
* Creates a new lock button which reflects the @permission.
|
|
|
|
*
|
|
|
|
* Returns: a new #GtkLockButton
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2012-04-13 00:07:28 +00:00
|
|
|
* Returns: (transfer none): the #GPermission of @button
|
2011-04-30 03:40:47 +00:00
|
|
|
*/
|
|
|
|
GPermission *
|
|
|
|
gtk_lock_button_get_permission (GtkLockButton *button)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_LOCK_BUTTON (button), NULL);
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
return button->permission;
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_lock_button_set_permission:
|
|
|
|
* @button: a #GtkLockButton
|
2011-05-21 13:12:02 +00:00
|
|
|
* @permission: (allow-none): a #GPermission object, or %NULL
|
2011-04-30 03:40:47 +00:00
|
|
|
*
|
|
|
|
* Sets the #GPermission object that controls @button.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_lock_button_set_permission (GtkLockButton *button,
|
|
|
|
GPermission *permission)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_LOCK_BUTTON (button));
|
2011-05-21 13:12:02 +00:00
|
|
|
g_return_if_fail (permission == NULL || G_IS_PERMISSION (permission));
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->permission != permission)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->permission)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (button->permission,
|
2011-04-30 03:40:47 +00:00
|
|
|
on_permission_changed,
|
|
|
|
button);
|
2020-04-13 23:17:11 +00:00
|
|
|
g_object_unref (button->permission);
|
2011-04-30 03:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
button->permission = permission;
|
2011-04-30 03:40:47 +00:00
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
if (button->permission)
|
2011-04-30 03:40:47 +00:00
|
|
|
{
|
2020-04-13 23:17:11 +00:00
|
|
|
g_object_ref (button->permission);
|
|
|
|
g_signal_connect (button->permission, "notify",
|
2011-04-30 03:40:47 +00:00
|
|
|
G_CALLBACK (on_permission_changed), button);
|
|
|
|
}
|
|
|
|
|
|
|
|
update_state (button);
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (button), "permission");
|
|
|
|
}
|
|
|
|
}
|
2012-07-09 00:58:22 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
_gtk_lock_button_get_current_text (GtkLockButton *button)
|
|
|
|
{
|
2016-01-03 14:55:30 +00:00
|
|
|
GtkWidget *label;
|
2012-07-09 00:58:22 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_LOCK_BUTTON (button), NULL);
|
|
|
|
|
2020-04-13 23:17:11 +00:00
|
|
|
label = gtk_stack_get_visible_child (GTK_STACK (button->stack));
|
2012-07-09 00:58:22 +00:00
|
|
|
|
2016-01-03 14:55:30 +00:00
|
|
|
return gtk_label_get_text (GTK_LABEL (label));
|
2012-07-09 00:58:22 +00:00
|
|
|
}
|
|
|
|
|