2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gpermission.h"
31 * @short_description: An object representing the permission to perform
34 * A #GPermission represents the status of the caller's permission to
35 * perform a certain action.
37 * You can query if the action is currently allowed and if it is
38 * possible to acquire the permission so that the action will be allowed
41 * There is also an API to actually acquire the permission and one to
44 * As an example, a #GPermission might represent the ability for the
45 * user to write to a #GSettings object. This #GPermission object could
46 * then be used to decide if it is appropriate to show a "Click here to
47 * unlock" button in a dialog and to provide the mechanism to invoke
48 * when that button is clicked.
54 * #GPermission is an opaque data structure and can only be accessed
55 * using the following functions.
58 G_DEFINE_ABSTRACT_TYPE (GPermission, g_permission, G_TYPE_OBJECT)
60 struct _GPermissionPrivate
75 * g_permission_acquire:
76 * @permission: a #GPermission instance
77 * @cancellable: (allow-none): a #GCancellable, or %NULL
78 * @error: a pointer to a %NULL #GError, or %NULL
80 * Attempts to acquire the permission represented by @permission.
82 * The precise method by which this happens depends on the permission
83 * and the underlying authentication mechanism. A simple example is
84 * that a dialog may appear asking the user to enter their password.
86 * You should check with g_permission_get_can_acquire() before calling
89 * If the permission is acquired then %TRUE is returned. Otherwise,
90 * %FALSE is returned and @error is set appropriately.
92 * This call is blocking, likely for a very long time (in the case that
93 * user interaction is required). See g_permission_acquire_async() for
94 * the non-blocking version.
96 * Returns: %TRUE if the permission was successfully acquired
101 g_permission_acquire (GPermission *permission,
102 GCancellable *cancellable,
105 return G_PERMISSION_GET_CLASS (permission)
106 ->acquire (permission, cancellable, error);
110 * g_permission_acquire_async:
111 * @permission: a #GPermission instance
112 * @cancellable: (allow-none): a #GCancellable, or %NULL
113 * @callback: the #GAsyncReadyCallback to call when done
114 * @user_data: the user data to pass to @callback
116 * Attempts to acquire the permission represented by @permission.
118 * This is the first half of the asynchronous version of
119 * g_permission_acquire().
124 g_permission_acquire_async (GPermission *permission,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
129 G_PERMISSION_GET_CLASS (permission)
130 ->acquire_async (permission, cancellable, callback, user_data);
134 * g_permission_acquire_finish:
135 * @permission: a #GPermission instance
136 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
137 * @error: a pointer to a %NULL #GError, or %NULL
139 * Collects the result of attempting to acquire the permission
140 * represented by @permission.
142 * This is the second half of the asynchronous version of
143 * g_permission_acquire().
145 * Returns: %TRUE if the permission was successfully acquired
150 g_permission_acquire_finish (GPermission *permission,
151 GAsyncResult *result,
154 return G_PERMISSION_GET_CLASS (permission)
155 ->acquire_finish (permission, result, error);
159 * g_permission_release:
160 * @permission: a #GPermission instance
161 * @cancellable: (allow-none): a #GCancellable, or %NULL
162 * @error: a pointer to a %NULL #GError, or %NULL
164 * Attempts to release the permission represented by @permission.
166 * The precise method by which this happens depends on the permission
167 * and the underlying authentication mechanism. In most cases the
168 * permission will be dropped immediately without further action.
170 * You should check with g_permission_get_can_release() before calling
173 * If the permission is released then %TRUE is returned. Otherwise,
174 * %FALSE is returned and @error is set appropriately.
176 * This call is blocking, likely for a very long time (in the case that
177 * user interaction is required). See g_permission_release_async() for
178 * the non-blocking version.
180 * Returns: %TRUE if the permission was successfully released
185 g_permission_release (GPermission *permission,
186 GCancellable *cancellable,
189 return G_PERMISSION_GET_CLASS (permission)
190 ->release (permission, cancellable, error);
194 * g_permission_release_async:
195 * @permission: a #GPermission instance
196 * @cancellable: (allow-none): a #GCancellable, or %NULL
197 * @callback: the #GAsyncReadyCallback to call when done
198 * @user_data: the user data to pass to @callback
200 * Attempts to release the permission represented by @permission.
202 * This is the first half of the asynchronous version of
203 * g_permission_release().
208 g_permission_release_async (GPermission *permission,
209 GCancellable *cancellable,
210 GAsyncReadyCallback callback,
213 G_PERMISSION_GET_CLASS (permission)
214 ->release_async (permission, cancellable, callback, user_data);
218 * g_permission_release_finish:
219 * @permission: a #GPermission instance
220 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
221 * @error: a pointer to a %NULL #GError, or %NULL
223 * Collects the result of attempting to release the permission
224 * represented by @permission.
226 * This is the second half of the asynchronous version of
227 * g_permission_release().
229 * Returns: %TRUE if the permission was successfully released
234 g_permission_release_finish (GPermission *permission,
235 GAsyncResult *result,
238 return G_PERMISSION_GET_CLASS (permission)
239 ->release_finish (permission, result, error);
243 * g_permission_get_allowed:
244 * @permission: a #GPermission instance
246 * Gets the value of the 'allowed' property. This property is %TRUE if
247 * the caller currently has permission to perform the action that
248 * @permission represents the permission to perform.
250 * Returns: the value of the 'allowed' property
255 g_permission_get_allowed (GPermission *permission)
257 return permission->priv->allowed;
261 * g_permission_get_can_acquire:
262 * @permission: a #GPermission instance
264 * Gets the value of the 'can-acquire' property. This property is %TRUE
265 * if it is generally possible to acquire the permission by calling
266 * g_permission_acquire().
268 * Returns: the value of the 'can-acquire' property
273 g_permission_get_can_acquire (GPermission *permission)
275 return permission->priv->can_acquire;
279 * g_permission_get_can_release:
280 * @permission: a #GPermission instance
282 * Gets the value of the 'can-release' property. This property is %TRUE
283 * if it is generally possible to release the permission by calling
284 * g_permission_release().
286 * Returns: the value of the 'can-release' property
291 g_permission_get_can_release (GPermission *permission)
293 return permission->priv->can_release;
297 * g_permission_impl_update:
298 * @permission: a #GPermission instance
299 * @allowed: the new value for the 'allowed' property
300 * @can_acquire: the new value for the 'can-acquire' property
301 * @can_release: the new value for the 'can-release' property
303 * This function is called by the #GPermission implementation to update
304 * the properties of the permission. You should never call this
305 * function except from a #GPermission implementation.
307 * GObject notify signals are generated, as appropriate.
312 g_permission_impl_update (GPermission *permission,
314 gboolean can_acquire,
315 gboolean can_release)
317 GObject *object = G_OBJECT (permission);
319 g_object_freeze_notify (object);
321 if (allowed != permission->priv->allowed)
323 permission->priv->allowed = !!allowed;
324 g_object_notify (object, "allowed");
327 if (can_acquire != permission->priv->can_acquire)
329 permission->priv->can_acquire = !!can_acquire;
330 g_object_notify (object, "can-acquire");
333 if (can_release != permission->priv->can_release)
335 permission->priv->can_release = !!can_release;
336 g_object_notify (object, "can-release");
339 g_object_thaw_notify (object);
343 g_permission_get_property (GObject *object, guint prop_id,
344 GValue *value, GParamSpec *pspec)
346 GPermission *permission = G_PERMISSION (object);
351 g_value_set_boolean (value, permission->priv->allowed);
354 case PROP_CAN_ACQUIRE:
355 g_value_set_boolean (value, permission->priv->can_acquire);
358 case PROP_CAN_RELEASE:
359 g_value_set_boolean (value, permission->priv->can_release);
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368 g_permission_init (GPermission *permission)
370 permission->priv = G_TYPE_INSTANCE_GET_PRIVATE (permission,
376 g_permission_class_init (GPermissionClass *class)
378 GObjectClass *object_class = G_OBJECT_CLASS (class);
380 object_class->get_property = g_permission_get_property;
383 * GPermission:allowed:
385 * %TRUE if the caller currently has permission to perform the action that
386 * @permission represents the permission to perform.
388 g_object_class_install_property (object_class, PROP_ALLOWED,
389 g_param_spec_boolean ("allowed",
391 P_("If the caller is allowed to perform the action"),
393 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
396 * GPermission:can-acquire:
398 * %TRUE if it is generally possible to acquire the permission by calling
399 * g_permission_acquire().
401 g_object_class_install_property (object_class, PROP_CAN_ACQUIRE,
402 g_param_spec_boolean ("can-acquire",
404 P_("If calling g_permission_acquire() makes sense"),
406 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
409 * GPermission:can-release:
411 * %TRUE if it is generally possible to release the permission by calling
412 * g_permission_release().
414 g_object_class_install_property (object_class, PROP_CAN_RELEASE,
415 g_param_spec_boolean ("can-release",
417 P_("If calling g_permission_release() makes sense"),
419 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
421 g_type_class_add_private (class, sizeof (GPermissionPrivate));