gtklockbutton.c 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
Javier Jardon's avatar
Javier Jardon committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 */

#include "config.h"

21
#include "gtklockbuttonprivate.h"
22
#include "gtkbox.h"
Matthias Clasen's avatar
Matthias Clasen committed
23
#include "gtkimage.h"
24
#include "gtklabel.h"
Matthias Clasen's avatar
Matthias Clasen committed
25
#include "gtksizegroup.h"
26
#include "gtkintl.h"
27
#include "a11y/gtklockbuttonaccessibleprivate.h"
28 29 30 31 32 33 34 35 36 37

/**
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
38 39
 * PolicyKit or some other authorization framework. To obtain a PolicyKit-based
 * #GPermission, use polkit_permission_new().
40
 *
Matthias Clasen's avatar
Matthias Clasen committed
41
 * If the user is not currently allowed to perform the action, but can obtain
42 43 44 45
 * the permission, the widget looks like this:
 *
 * ![](lockbutton-locked.png)
 *
Matthias Clasen's avatar
Matthias Clasen committed
46
 * and the user can click the button to request the permission. Depending
47
 * on the platform, this may pop up an authentication dialog or ask the user
Matthias Clasen's avatar
Matthias Clasen committed
48
 * to authenticate in some other way. Once the user has obtained the permission,
49 50 51 52
 * the widget changes to this:
 *
 * ![](lockbutton-unlocked.png)
 *
Matthias Clasen's avatar
Matthias Clasen committed
53
 * and the permission can be dropped again by clicking the button. If the user
54 55 56 57
 * is not able to obtain the permission at all, the widget looks like this:
 *
 * ![](lockbutton-sorry.png)
 *
Matthias Clasen's avatar
Matthias Clasen committed
58
 * If the user has the permission and cannot drop it, the button is hidden.
59 60 61
 *
 * The text (and tooltips) that are shown in the various cases can be adjusted
 * with the #GtkLockButton:text-lock, #GtkLockButton:text-unlock,
Matthias Clasen's avatar
Matthias Clasen committed
62 63
 * #GtkLockButton:tooltip-lock, #GtkLockButton:tooltip-unlock and
 * #GtkLockButton:tooltip-not-authorized properties.
64 65 66 67 68
 */

struct _GtkLockButtonPrivate
{
  GPermission *permission;
Matthias Clasen's avatar
Matthias Clasen committed
69
  GCancellable *cancellable;
70 71 72 73

  gchar *tooltip_lock;
  gchar *tooltip_unlock;
  gchar *tooltip_not_authorized;
Matthias Clasen's avatar
Matthias Clasen committed
74 75
  GIcon *icon_lock;
  GIcon *icon_unlock;
76 77 78

  GtkWidget *box;
  GtkWidget *image;
79
  GtkWidget *stack;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  GtkWidget *label_lock;
  GtkWidget *label_unlock;
};

enum
{
  PROP_0,
  PROP_PERMISSION,
  PROP_TEXT_LOCK,
  PROP_TEXT_UNLOCK,
  PROP_TOOLTIP_LOCK,
  PROP_TOOLTIP_UNLOCK,
  PROP_TOOLTIP_NOT_AUTHORIZED
};

Matthias Clasen's avatar
Matthias Clasen committed
95 96
static void update_state (GtkLockButton *button);
static void gtk_lock_button_clicked (GtkButton *button);
97 98 99 100 101

static void on_permission_changed (GPermission *permission,
                                   GParamSpec  *pspec,
                                   gpointer     user_data);

102
G_DEFINE_TYPE_WITH_PRIVATE (GtkLockButton, gtk_lock_button, GTK_TYPE_BUTTON)
103 104 105 106 107 108 109 110 111 112 113

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);

Matthias Clasen's avatar
Matthias Clasen committed
114 115 116
  g_object_unref (priv->icon_lock);
  g_object_unref (priv->icon_unlock);

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  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:
Matthias Clasen's avatar
Matthias Clasen committed
150
      g_value_set_string (value, gtk_label_get_text (GTK_LABEL (priv->label_lock)));
151 152 153
      break;

    case PROP_TEXT_UNLOCK:
Matthias Clasen's avatar
Matthias Clasen committed
154
      g_value_set_string (value, gtk_label_get_text (GTK_LABEL (priv->label_unlock)));
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      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));
192
      _gtk_lock_button_accessible_name_changed (button);
193 194 195 196
      break;

    case PROP_TEXT_UNLOCK:
      gtk_label_set_text (GTK_LABEL (priv->label_unlock), g_value_get_string (value));
197
      _gtk_lock_button_accessible_name_changed (button);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
      break;

    case PROP_TOOLTIP_LOCK:
      g_free (priv->tooltip_lock);
      priv->tooltip_lock = g_value_dup_string (value);
      break;

    case PROP_TOOLTIP_UNLOCK:
      g_free (priv->tooltip_unlock);
      priv->tooltip_unlock = g_value_dup_string (value);
      break;

    case PROP_TOOLTIP_NOT_AUTHORIZED:
      g_free (priv->tooltip_not_authorized);
      priv->tooltip_not_authorized = g_value_dup_string (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
Matthias Clasen's avatar
Matthias Clasen committed
219 220

  update_state (button);
221 222 223 224 225 226
}

static void
gtk_lock_button_init (GtkLockButton *button)
{
  GtkLockButtonPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
227
  gchar *names[3];
228
  GtkStyleContext *context;
229

230
  button->priv = priv = gtk_lock_button_get_instance_private (button);
231

232
  gtk_widget_init_template (GTK_WIDGET (button));
233

Matthias Clasen's avatar
Matthias Clasen committed
234 235 236 237
  names[0] = "changes-allow-symbolic";
  names[1] = "changes-allow";
  names[2] = NULL;
  priv->icon_unlock = g_themed_icon_new_from_names (names, -1);
238

Matthias Clasen's avatar
Matthias Clasen committed
239 240 241 242
  names[0] = "changes-prevent-symbolic";
  names[1] = "changes-prevent";
  names[2] = NULL;
  priv->icon_lock = g_themed_icon_new_from_names (names, -1);
243 244

  update_state (button);
245 246 247

  context = gtk_widget_get_style_context (GTK_WIDGET (button));
  gtk_style_context_add_class (context, "lock");
248 249 250 251 252 253
}

static void
gtk_lock_button_class_init (GtkLockButtonClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
254
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
Matthias Clasen's avatar
Matthias Clasen committed
255
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
256 257 258 259 260

  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;

Matthias Clasen's avatar
Matthias Clasen committed
261
  button_class->clicked = gtk_lock_button_clicked;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

  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));
315

316 317
  /* Bind class to template
   */
318
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtklockbutton.ui");
319 320 321 322
  gtk_widget_class_bind_template_child_private (widget_class, GtkLockButton, box);
  gtk_widget_class_bind_template_child_private (widget_class, GtkLockButton, image);
  gtk_widget_class_bind_template_child_private (widget_class, GtkLockButton, label_lock);
  gtk_widget_class_bind_template_child_private (widget_class, GtkLockButton, label_unlock);
323
  gtk_widget_class_bind_template_child_private (widget_class, GtkLockButton, stack);
324

325
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LOCK_BUTTON_ACCESSIBLE);
326
  gtk_widget_class_set_css_name (widget_class, "button");
327 328 329 330 331 332 333 334 335 336 337 338
}

static void
update_state (GtkLockButton *button)
{
  GtkLockButtonPrivate *priv = button->priv;
  gboolean allowed;
  gboolean can_acquire;
  gboolean can_release;
  gboolean sensitive;
  gboolean visible;
  GIcon *icon;
Matthias Clasen's avatar
Matthias Clasen committed
339
  const gchar *tooltip;
340 341 342 343 344 345 346 347 348

  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
    {
Matthias Clasen's avatar
Matthias Clasen committed
349
      allowed = TRUE;
350 351 352 353
      can_acquire = FALSE;
      can_release = FALSE;
    }

Matthias Clasen's avatar
Matthias Clasen committed
354
  if (allowed && can_release)
355
    {
Matthias Clasen's avatar
Matthias Clasen committed
356 357 358 359
      visible = TRUE;
      sensitive = TRUE;
      icon = priv->icon_lock;
      tooltip = priv->tooltip_lock;
360
    }
Matthias Clasen's avatar
Matthias Clasen committed
361
  else if (allowed && !can_release)
362
    {
Matthias Clasen's avatar
Matthias Clasen committed
363 364 365 366
      visible = FALSE;
      sensitive = TRUE;
      icon = priv->icon_lock;
      tooltip = priv->tooltip_lock;
367
    }
Matthias Clasen's avatar
Matthias Clasen committed
368
  else if (!allowed && can_acquire)
369
    {
Matthias Clasen's avatar
Matthias Clasen committed
370 371 372 373 374 375 376 377 378 379 380
      visible = TRUE;
      sensitive = TRUE;
      icon = priv->icon_unlock;
      tooltip = priv->tooltip_unlock;
    }
  else if (!allowed && !can_acquire)
    {
      visible = TRUE;
      sensitive = FALSE;
      icon = priv->icon_unlock;
      tooltip = priv->tooltip_not_authorized;
381 382 383
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
384
      g_assert_not_reached ();
385 386
    }

387
  gtk_image_set_from_gicon (GTK_IMAGE (priv->image), icon, GTK_ICON_SIZE_MENU);
388 389 390
  gtk_stack_set_visible_child (GTK_STACK (priv->stack),
                               allowed ? priv->label_lock : priv->label_unlock);
  _gtk_lock_button_accessible_name_changed (button);
Matthias Clasen's avatar
Matthias Clasen committed
391 392 393
  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);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
}

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
Matthias Clasen's avatar
Matthias Clasen committed
451
gtk_lock_button_clicked (GtkButton *button)
452
{
Matthias Clasen's avatar
Matthias Clasen committed
453
  GtkLockButtonPrivate *priv = GTK_LOCK_BUTTON (button)->priv;
454

455 456 457 458
  /* if we already have a pending interactive check or permission is not set,
   * then do nothing
   */
  if (priv->cancellable != NULL || priv->permission == NULL)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    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);
        }
    }
}

/**
 * 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.
 *
511
 * Returns: (transfer none): the #GPermission of @button
512 513 514 515 516 517 518 519 520 521 522 523 524 525
 *
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
526
 * @permission: (allow-none): a #GPermission object, or %NULL
527 528 529 530 531 532 533 534 535 536 537 538
 *
 * 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));
Matthias Clasen's avatar
Matthias Clasen committed
539
  g_return_if_fail (permission == NULL || G_IS_PERMISSION (permission));
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

  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");
    }
}
567 568 569 570

const char *
_gtk_lock_button_get_current_text (GtkLockButton *button)
{
571
  GtkWidget *label;
572 573 574

  g_return_val_if_fail (GTK_IS_LOCK_BUTTON (button), NULL);

575
  label = gtk_stack_get_visible_child (GTK_STACK (button->priv->stack));
576

577
  return gtk_label_get_text (GTK_LABEL (label));
578 579
}