Blame gio/gpermission.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2010 Codethink Limited
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gpermission.h"
Packit ae235b
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gioenums.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gpermission
Packit ae235b
 * @title: GPermission
Packit ae235b
 * @short_description: An object representing the permission
Packit ae235b
 *     to perform a certain action
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * A #GPermission represents the status of the caller's permission to
Packit ae235b
 * perform a certain action.
Packit ae235b
 *
Packit ae235b
 * You can query if the action is currently allowed and if it is
Packit ae235b
 * possible to acquire the permission so that the action will be allowed
Packit ae235b
 * in the future.
Packit ae235b
 *
Packit ae235b
 * There is also an API to actually acquire the permission and one to
Packit ae235b
 * release it.
Packit ae235b
 *
Packit ae235b
 * As an example, a #GPermission might represent the ability for the
Packit ae235b
 * user to write to a #GSettings object.  This #GPermission object could
Packit ae235b
 * then be used to decide if it is appropriate to show a "Click here to
Packit ae235b
 * unlock" button in a dialog and to provide the mechanism to invoke
Packit ae235b
 * when that button is clicked.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GPermission:
Packit ae235b
 *
Packit ae235b
 * #GPermission is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
struct _GPermissionPrivate
Packit ae235b
{
Packit ae235b
  gboolean allowed;
Packit ae235b
  gboolean can_acquire;
Packit ae235b
  gboolean can_release;
Packit ae235b
};
Packit ae235b
Packit ae235b
enum  {
Packit ae235b
  PROP_NONE,
Packit ae235b
  PROP_ALLOWED,
Packit ae235b
  PROP_CAN_ACQUIRE,
Packit ae235b
  PROP_CAN_RELEASE
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GPermission, g_permission, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_acquire:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: a pointer to a %NULL #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Attempts to acquire the permission represented by @permission.
Packit ae235b
 *
Packit ae235b
 * The precise method by which this happens depends on the permission
Packit ae235b
 * and the underlying authentication mechanism.  A simple example is
Packit ae235b
 * that a dialog may appear asking the user to enter their password.
Packit ae235b
 *
Packit ae235b
 * You should check with g_permission_get_can_acquire() before calling
Packit ae235b
 * this function.
Packit ae235b
 *
Packit ae235b
 * If the permission is acquired then %TRUE is returned.  Otherwise,
Packit ae235b
 * %FALSE is returned and @error is set appropriately.
Packit ae235b
 *
Packit ae235b
 * This call is blocking, likely for a very long time (in the case that
Packit ae235b
 * user interaction is required).  See g_permission_acquire_async() for
Packit ae235b
 * the non-blocking version.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the permission was successfully acquired
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_permission_acquire (GPermission   *permission,
Packit ae235b
                      GCancellable  *cancellable,
Packit ae235b
                      GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return G_PERMISSION_GET_CLASS (permission)
Packit ae235b
    ->acquire (permission, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_acquire_async:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: the #GAsyncReadyCallback to call when done
Packit ae235b
 * @user_data: the user data to pass to @callback
Packit ae235b
 *
Packit ae235b
 * Attempts to acquire the permission represented by @permission.
Packit ae235b
 *
Packit ae235b
 * This is the first half of the asynchronous version of
Packit ae235b
 * g_permission_acquire().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_permission_acquire_async (GPermission         *permission,
Packit ae235b
                            GCancellable        *cancellable,
Packit ae235b
                            GAsyncReadyCallback  callback,
Packit ae235b
                            gpointer             user_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_PERMISSION (permission));
Packit ae235b
  G_PERMISSION_GET_CLASS (permission)
Packit ae235b
    ->acquire_async (permission, cancellable, callback, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_acquire_finish:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
Packit ae235b
 * @error: a pointer to a %NULL #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Collects the result of attempting to acquire the permission
Packit ae235b
 * represented by @permission.
Packit ae235b
 *
Packit ae235b
 * This is the second half of the asynchronous version of
Packit ae235b
 * g_permission_acquire().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the permission was successfully acquired
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_permission_acquire_finish (GPermission   *permission,
Packit ae235b
                             GAsyncResult  *result,
Packit ae235b
                             GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return G_PERMISSION_GET_CLASS (permission)
Packit ae235b
    ->acquire_finish (permission, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_release:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: a pointer to a %NULL #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Attempts to release the permission represented by @permission.
Packit ae235b
 *
Packit ae235b
 * The precise method by which this happens depends on the permission
Packit ae235b
 * and the underlying authentication mechanism.  In most cases the
Packit ae235b
 * permission will be dropped immediately without further action.
Packit ae235b
 *
Packit ae235b
 * You should check with g_permission_get_can_release() before calling
Packit ae235b
 * this function.
Packit ae235b
 *
Packit ae235b
 * If the permission is released then %TRUE is returned.  Otherwise,
Packit ae235b
 * %FALSE is returned and @error is set appropriately.
Packit ae235b
 *
Packit ae235b
 * This call is blocking, likely for a very long time (in the case that
Packit ae235b
 * user interaction is required).  See g_permission_release_async() for
Packit ae235b
 * the non-blocking version.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the permission was successfully released
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_permission_release (GPermission   *permission,
Packit ae235b
                      GCancellable  *cancellable,
Packit ae235b
                      GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return G_PERMISSION_GET_CLASS (permission)
Packit ae235b
    ->release (permission, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_release_async:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @callback: the #GAsyncReadyCallback to call when done
Packit ae235b
 * @user_data: the user data to pass to @callback
Packit ae235b
 *
Packit ae235b
 * Attempts to release the permission represented by @permission.
Packit ae235b
 *
Packit ae235b
 * This is the first half of the asynchronous version of
Packit ae235b
 * g_permission_release().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_permission_release_async (GPermission         *permission,
Packit ae235b
                            GCancellable        *cancellable,
Packit ae235b
                            GAsyncReadyCallback  callback,
Packit ae235b
                            gpointer             user_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_PERMISSION (permission));
Packit ae235b
  G_PERMISSION_GET_CLASS (permission)
Packit ae235b
    ->release_async (permission, cancellable, callback, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_release_finish:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
Packit ae235b
 * @error: a pointer to a %NULL #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Collects the result of attempting to release the permission
Packit ae235b
 * represented by @permission.
Packit ae235b
 *
Packit ae235b
 * This is the second half of the asynchronous version of
Packit ae235b
 * g_permission_release().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the permission was successfully released
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_permission_release_finish (GPermission   *permission,
Packit ae235b
                             GAsyncResult  *result,
Packit ae235b
                             GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return G_PERMISSION_GET_CLASS (permission)
Packit ae235b
    ->release_finish (permission, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_get_allowed:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 *
Packit ae235b
 * Gets the value of the 'allowed' property.  This property is %TRUE if
Packit ae235b
 * the caller currently has permission to perform the action that
Packit ae235b
 * @permission represents the permission to perform.
Packit ae235b
 *
Packit ae235b
 * Returns: the value of the 'allowed' property
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_permission_get_allowed (GPermission *permission)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return permission->priv->allowed;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_get_can_acquire:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 *
Packit ae235b
 * Gets the value of the 'can-acquire' property.  This property is %TRUE
Packit ae235b
 * if it is generally possible to acquire the permission by calling
Packit ae235b
 * g_permission_acquire().
Packit ae235b
 *
Packit ae235b
 * Returns: the value of the 'can-acquire' property
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_permission_get_can_acquire (GPermission *permission)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return permission->priv->can_acquire;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_get_can_release:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 *
Packit ae235b
 * Gets the value of the 'can-release' property.  This property is %TRUE
Packit ae235b
 * if it is generally possible to release the permission by calling
Packit ae235b
 * g_permission_release().
Packit ae235b
 *
Packit ae235b
 * Returns: the value of the 'can-release' property
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_permission_get_can_release (GPermission *permission)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
Packit ae235b
  return permission->priv->can_release;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_permission_impl_update:
Packit ae235b
 * @permission: a #GPermission instance
Packit ae235b
 * @allowed: the new value for the 'allowed' property
Packit ae235b
 * @can_acquire: the new value for the 'can-acquire' property
Packit ae235b
 * @can_release: the new value for the 'can-release' property
Packit ae235b
 *
Packit ae235b
 * This function is called by the #GPermission implementation to update
Packit ae235b
 * the properties of the permission.  You should never call this
Packit ae235b
 * function except from a #GPermission implementation.
Packit ae235b
 *
Packit ae235b
 * GObject notify signals are generated, as appropriate.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_permission_impl_update (GPermission *permission,
Packit ae235b
                          gboolean     allowed,
Packit ae235b
                          gboolean     can_acquire,
Packit ae235b
                          gboolean     can_release)
Packit ae235b
{
Packit ae235b
  GObject *object;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_PERMISSION (permission));
Packit ae235b
Packit ae235b
  object = G_OBJECT (permission);
Packit ae235b
  g_object_freeze_notify (object);
Packit ae235b
Packit ae235b
  allowed = allowed != FALSE;
Packit ae235b
  if (allowed != permission->priv->allowed)
Packit ae235b
    {
Packit ae235b
      permission->priv->allowed = allowed;
Packit ae235b
      g_object_notify (object, "allowed");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  can_acquire = can_acquire != FALSE;
Packit ae235b
  if (can_acquire != permission->priv->can_acquire)
Packit ae235b
    {
Packit ae235b
      permission->priv->can_acquire = can_acquire;
Packit ae235b
      g_object_notify (object, "can-acquire");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  can_release = can_release != FALSE;
Packit ae235b
  if (can_release != permission->priv->can_release)
Packit ae235b
    {
Packit ae235b
      permission->priv->can_release = can_release;
Packit ae235b
      g_object_notify (object, "can-release");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_thaw_notify (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_permission_get_property (GObject *object, guint prop_id,
Packit ae235b
                           GValue *value, GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GPermission *permission = G_PERMISSION (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_ALLOWED:
Packit ae235b
      g_value_set_boolean (value, permission->priv->allowed);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_CAN_ACQUIRE:
Packit ae235b
      g_value_set_boolean (value, permission->priv->can_acquire);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_CAN_RELEASE:
Packit ae235b
      g_value_set_boolean (value, permission->priv->can_release);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
  }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_permission_init (GPermission *permission)
Packit ae235b
{
Packit ae235b
  permission->priv = g_permission_get_instance_private (permission);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
acquire_or_release (GPermission   *permission,
Packit ae235b
                    GCancellable  *cancellable,
Packit ae235b
                    GError       **error)
Packit ae235b
{
Packit ae235b
  g_set_error_literal  (error,
Packit ae235b
                        G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                        "Can't acquire or release permission");
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
acquire_or_release_async (GPermission         *permission,
Packit ae235b
                          GCancellable        *cancellable,
Packit ae235b
                          GAsyncReadyCallback  callback,
Packit ae235b
                          gpointer             user_data)
Packit ae235b
{
Packit ae235b
  g_task_report_new_error (permission,
Packit ae235b
                           callback, user_data,
Packit ae235b
                           NULL,
Packit ae235b
                           G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                           "Can't acquire or release permission");
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
acquire_or_release_finish (GPermission   *permission,
Packit ae235b
                           GAsyncResult  *result,
Packit ae235b
                           GError       **error)
Packit ae235b
{
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_permission_class_init (GPermissionClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  object_class->get_property = g_permission_get_property;
Packit ae235b
Packit ae235b
  class->acquire = acquire_or_release;
Packit ae235b
  class->release = acquire_or_release;
Packit ae235b
  class->acquire_async = acquire_or_release_async;
Packit ae235b
  class->release_async = acquire_or_release_async;
Packit ae235b
  class->acquire_finish = acquire_or_release_finish;
Packit ae235b
  class->release_finish = acquire_or_release_finish;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GPermission:allowed:
Packit ae235b
   *
Packit ae235b
   * %TRUE if the caller currently has permission to perform the action that
Packit ae235b
   * @permission represents the permission to perform.
Packit ae235b
   */
Packit ae235b
   g_object_class_install_property (object_class, PROP_ALLOWED,
Packit ae235b
     g_param_spec_boolean ("allowed",
Packit ae235b
                           P_("Is allowed"),
Packit ae235b
                           P_("If the caller is allowed to perform the action"),
Packit ae235b
                           FALSE,
Packit ae235b
                           G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GPermission:can-acquire:
Packit ae235b
   *
Packit ae235b
   * %TRUE if it is generally possible to acquire the permission by calling
Packit ae235b
   * g_permission_acquire().
Packit ae235b
   */
Packit ae235b
   g_object_class_install_property (object_class, PROP_CAN_ACQUIRE,
Packit ae235b
     g_param_spec_boolean ("can-acquire",
Packit ae235b
                           P_("Can acquire"),
Packit ae235b
                           P_("If calling g_permission_acquire() makes sense"),
Packit ae235b
                           FALSE,
Packit ae235b
                           G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GPermission:can-release:
Packit ae235b
   *
Packit ae235b
   * %TRUE if it is generally possible to release the permission by calling
Packit ae235b
   * g_permission_release().
Packit ae235b
   */
Packit ae235b
   g_object_class_install_property (object_class, PROP_CAN_RELEASE,
Packit ae235b
     g_param_spec_boolean ("can-release",
Packit ae235b
                           P_("Can release"),
Packit ae235b
                           P_("If calling g_permission_release() makes sense"),
Packit ae235b
                           FALSE,
Packit ae235b
                           G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
Packit ae235b
}