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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gpermission.h"
26 #include "gasyncresult.h"
27 #include "gsimpleasyncresult.h"
34 * @short_description: An object representing the permission
35 * to perform a certain action
38 * A #GPermission represents the status of the caller's permission to
39 * perform a certain action.
41 * You can query if the action is currently allowed and if it is
42 * possible to acquire the permission so that the action will be allowed
45 * There is also an API to actually acquire the permission and one to
48 * As an example, a #GPermission might represent the ability for the
49 * user to write to a #GSettings object. This #GPermission object could
50 * then be used to decide if it is appropriate to show a "Click here to
51 * unlock" button in a dialog and to provide the mechanism to invoke
52 * when that button is clicked.
58 * #GPermission is an opaque data structure and can only be accessed
59 * using the following functions.
62 struct _GPermissionPrivate
76 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GPermission, g_permission, G_TYPE_OBJECT)
79 * g_permission_acquire:
80 * @permission: a #GPermission instance
81 * @cancellable: (allow-none): a #GCancellable, or %NULL
82 * @error: a pointer to a %NULL #GError, or %NULL
84 * Attempts to acquire the permission represented by @permission.
86 * The precise method by which this happens depends on the permission
87 * and the underlying authentication mechanism. A simple example is
88 * that a dialog may appear asking the user to enter their password.
90 * You should check with g_permission_get_can_acquire() before calling
93 * If the permission is acquired then %TRUE is returned. Otherwise,
94 * %FALSE is returned and @error is set appropriately.
96 * This call is blocking, likely for a very long time (in the case that
97 * user interaction is required). See g_permission_acquire_async() for
98 * the non-blocking version.
100 * Returns: %TRUE if the permission was successfully acquired
105 g_permission_acquire (GPermission *permission,
106 GCancellable *cancellable,
109 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
110 return G_PERMISSION_GET_CLASS (permission)
111 ->acquire (permission, cancellable, error);
115 * g_permission_acquire_async:
116 * @permission: a #GPermission instance
117 * @cancellable: (allow-none): a #GCancellable, or %NULL
118 * @callback: the #GAsyncReadyCallback to call when done
119 * @user_data: the user data to pass to @callback
121 * Attempts to acquire the permission represented by @permission.
123 * This is the first half of the asynchronous version of
124 * g_permission_acquire().
129 g_permission_acquire_async (GPermission *permission,
130 GCancellable *cancellable,
131 GAsyncReadyCallback callback,
134 g_return_if_fail (G_IS_PERMISSION (permission));
135 G_PERMISSION_GET_CLASS (permission)
136 ->acquire_async (permission, cancellable, callback, user_data);
140 * g_permission_acquire_finish:
141 * @permission: a #GPermission instance
142 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
143 * @error: a pointer to a %NULL #GError, or %NULL
145 * Collects the result of attempting to acquire the permission
146 * represented by @permission.
148 * This is the second half of the asynchronous version of
149 * g_permission_acquire().
151 * Returns: %TRUE if the permission was successfully acquired
156 g_permission_acquire_finish (GPermission *permission,
157 GAsyncResult *result,
160 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
161 return G_PERMISSION_GET_CLASS (permission)
162 ->acquire_finish (permission, result, error);
166 * g_permission_release:
167 * @permission: a #GPermission instance
168 * @cancellable: (allow-none): a #GCancellable, or %NULL
169 * @error: a pointer to a %NULL #GError, or %NULL
171 * Attempts to release the permission represented by @permission.
173 * The precise method by which this happens depends on the permission
174 * and the underlying authentication mechanism. In most cases the
175 * permission will be dropped immediately without further action.
177 * You should check with g_permission_get_can_release() before calling
180 * If the permission is released then %TRUE is returned. Otherwise,
181 * %FALSE is returned and @error is set appropriately.
183 * This call is blocking, likely for a very long time (in the case that
184 * user interaction is required). See g_permission_release_async() for
185 * the non-blocking version.
187 * Returns: %TRUE if the permission was successfully released
192 g_permission_release (GPermission *permission,
193 GCancellable *cancellable,
196 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
197 return G_PERMISSION_GET_CLASS (permission)
198 ->release (permission, cancellable, error);
202 * g_permission_release_async:
203 * @permission: a #GPermission instance
204 * @cancellable: (allow-none): a #GCancellable, or %NULL
205 * @callback: the #GAsyncReadyCallback to call when done
206 * @user_data: the user data to pass to @callback
208 * Attempts to release the permission represented by @permission.
210 * This is the first half of the asynchronous version of
211 * g_permission_release().
216 g_permission_release_async (GPermission *permission,
217 GCancellable *cancellable,
218 GAsyncReadyCallback callback,
221 g_return_if_fail (G_IS_PERMISSION (permission));
222 G_PERMISSION_GET_CLASS (permission)
223 ->release_async (permission, cancellable, callback, user_data);
227 * g_permission_release_finish:
228 * @permission: a #GPermission instance
229 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
230 * @error: a pointer to a %NULL #GError, or %NULL
232 * Collects the result of attempting to release the permission
233 * represented by @permission.
235 * This is the second half of the asynchronous version of
236 * g_permission_release().
238 * Returns: %TRUE if the permission was successfully released
243 g_permission_release_finish (GPermission *permission,
244 GAsyncResult *result,
247 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
248 return G_PERMISSION_GET_CLASS (permission)
249 ->release_finish (permission, result, error);
253 * g_permission_get_allowed:
254 * @permission: a #GPermission instance
256 * Gets the value of the 'allowed' property. This property is %TRUE if
257 * the caller currently has permission to perform the action that
258 * @permission represents the permission to perform.
260 * Returns: the value of the 'allowed' property
265 g_permission_get_allowed (GPermission *permission)
267 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
268 return permission->priv->allowed;
272 * g_permission_get_can_acquire:
273 * @permission: a #GPermission instance
275 * Gets the value of the 'can-acquire' property. This property is %TRUE
276 * if it is generally possible to acquire the permission by calling
277 * g_permission_acquire().
279 * Returns: the value of the 'can-acquire' property
284 g_permission_get_can_acquire (GPermission *permission)
286 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
287 return permission->priv->can_acquire;
291 * g_permission_get_can_release:
292 * @permission: a #GPermission instance
294 * Gets the value of the 'can-release' property. This property is %TRUE
295 * if it is generally possible to release the permission by calling
296 * g_permission_release().
298 * Returns: the value of the 'can-release' property
303 g_permission_get_can_release (GPermission *permission)
305 g_return_val_if_fail (G_IS_PERMISSION (permission), FALSE);
306 return permission->priv->can_release;
310 * g_permission_impl_update:
311 * @permission: a #GPermission instance
312 * @allowed: the new value for the 'allowed' property
313 * @can_acquire: the new value for the 'can-acquire' property
314 * @can_release: the new value for the 'can-release' property
316 * This function is called by the #GPermission implementation to update
317 * the properties of the permission. You should never call this
318 * function except from a #GPermission implementation.
320 * GObject notify signals are generated, as appropriate.
325 g_permission_impl_update (GPermission *permission,
327 gboolean can_acquire,
328 gboolean can_release)
332 g_return_if_fail (G_IS_PERMISSION (permission));
334 object = G_OBJECT (permission);
335 g_object_freeze_notify (object);
337 allowed = allowed != FALSE;
338 if (allowed != permission->priv->allowed)
340 permission->priv->allowed = allowed;
341 g_object_notify (object, "allowed");
344 can_acquire = can_acquire != FALSE;
345 if (can_acquire != permission->priv->can_acquire)
347 permission->priv->can_acquire = can_acquire;
348 g_object_notify (object, "can-acquire");
351 can_release = can_release != FALSE;
352 if (can_release != permission->priv->can_release)
354 permission->priv->can_release = can_release;
355 g_object_notify (object, "can-release");
358 g_object_thaw_notify (object);
362 g_permission_get_property (GObject *object, guint prop_id,
363 GValue *value, GParamSpec *pspec)
365 GPermission *permission = G_PERMISSION (object);
370 g_value_set_boolean (value, permission->priv->allowed);
373 case PROP_CAN_ACQUIRE:
374 g_value_set_boolean (value, permission->priv->can_acquire);
377 case PROP_CAN_RELEASE:
378 g_value_set_boolean (value, permission->priv->can_release);
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 g_permission_init (GPermission *permission)
389 permission->priv = g_permission_get_instance_private (permission);
393 acquire_or_release (GPermission *permission,
394 GCancellable *cancellable,
397 g_set_error_literal (error,
398 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
399 "Can't acquire or release permission");
404 acquire_or_release_async (GPermission *permission,
405 GCancellable *cancellable,
406 GAsyncReadyCallback callback,
409 g_simple_async_report_error_in_idle (G_OBJECT (permission),
411 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
412 "Can't acquire or release permission");
416 acquire_or_release_finish (GPermission *permission,
417 GAsyncResult *result,
420 g_async_result_legacy_propagate_error (result, error);
425 g_permission_class_init (GPermissionClass *class)
427 GObjectClass *object_class = G_OBJECT_CLASS (class);
429 object_class->get_property = g_permission_get_property;
431 class->acquire = acquire_or_release;
432 class->release = acquire_or_release;
433 class->acquire_async = acquire_or_release_async;
434 class->release_async = acquire_or_release_async;
435 class->acquire_finish = acquire_or_release_finish;
436 class->release_finish = acquire_or_release_finish;
439 * GPermission:allowed:
441 * %TRUE if the caller currently has permission to perform the action that
442 * @permission represents the permission to perform.
444 g_object_class_install_property (object_class, PROP_ALLOWED,
445 g_param_spec_boolean ("allowed",
447 P_("If the caller is allowed to perform the action"),
449 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
452 * GPermission:can-acquire:
454 * %TRUE if it is generally possible to acquire the permission by calling
455 * g_permission_acquire().
457 g_object_class_install_property (object_class, PROP_CAN_ACQUIRE,
458 g_param_spec_boolean ("can-acquire",
460 P_("If calling g_permission_acquire() makes sense"),
462 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
465 * GPermission:can-release:
467 * %TRUE if it is generally possible to release the permission by calling
468 * g_permission_release().
470 g_object_class_install_property (object_class, PROP_CAN_RELEASE,
471 g_param_spec_boolean ("can-release",
473 P_("If calling g_permission_release() makes sense"),
475 G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));