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"
28 #include "gasyncresult.h"
29 #include "gsimpleasyncresult.h"
36 * @short_description: An object representing the permission to perform
39 * A #GPermission represents the status of the caller's permission to
40 * perform a certain action.
42 * You can query if the action is currently allowed and if it is
43 * possible to acquire the permission so that the action will be allowed
46 * There is also an API to actually acquire the permission and one to
49 * As an example, a #GPermission might represent the ability for the
50 * user to write to a #GSettings object. This #GPermission object could
51 * then be used to decide if it is appropriate to show a "Click here to
52 * unlock" button in a dialog and to provide the mechanism to invoke
53 * when that button is clicked.
59 * #GPermission is an opaque data structure and can only be accessed
60 * using the following functions.
63 struct _GPermissionPrivate
77 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GPermission
, g_permission
, G_TYPE_OBJECT
)
80 * g_permission_acquire:
81 * @permission: a #GPermission instance
82 * @cancellable: (allow-none): a #GCancellable, or %NULL
83 * @error: a pointer to a %NULL #GError, or %NULL
85 * Attempts to acquire the permission represented by @permission.
87 * The precise method by which this happens depends on the permission
88 * and the underlying authentication mechanism. A simple example is
89 * that a dialog may appear asking the user to enter their password.
91 * You should check with g_permission_get_can_acquire() before calling
94 * If the permission is acquired then %TRUE is returned. Otherwise,
95 * %FALSE is returned and @error is set appropriately.
97 * This call is blocking, likely for a very long time (in the case that
98 * user interaction is required). See g_permission_acquire_async() for
99 * the non-blocking version.
101 * Returns: %TRUE if the permission was successfully acquired
106 g_permission_acquire (GPermission
*permission
,
107 GCancellable
*cancellable
,
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_PERMISSION_GET_CLASS (permission
)
135 ->acquire_async (permission
, cancellable
, callback
, user_data
);
139 * g_permission_acquire_finish:
140 * @permission: a #GPermission instance
141 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
142 * @error: a pointer to a %NULL #GError, or %NULL
144 * Collects the result of attempting to acquire the permission
145 * represented by @permission.
147 * This is the second half of the asynchronous version of
148 * g_permission_acquire().
150 * Returns: %TRUE if the permission was successfully acquired
155 g_permission_acquire_finish (GPermission
*permission
,
156 GAsyncResult
*result
,
159 return G_PERMISSION_GET_CLASS (permission
)
160 ->acquire_finish (permission
, result
, error
);
164 * g_permission_release:
165 * @permission: a #GPermission instance
166 * @cancellable: (allow-none): a #GCancellable, or %NULL
167 * @error: a pointer to a %NULL #GError, or %NULL
169 * Attempts to release the permission represented by @permission.
171 * The precise method by which this happens depends on the permission
172 * and the underlying authentication mechanism. In most cases the
173 * permission will be dropped immediately without further action.
175 * You should check with g_permission_get_can_release() before calling
178 * If the permission is released then %TRUE is returned. Otherwise,
179 * %FALSE is returned and @error is set appropriately.
181 * This call is blocking, likely for a very long time (in the case that
182 * user interaction is required). See g_permission_release_async() for
183 * the non-blocking version.
185 * Returns: %TRUE if the permission was successfully released
190 g_permission_release (GPermission
*permission
,
191 GCancellable
*cancellable
,
194 return G_PERMISSION_GET_CLASS (permission
)
195 ->release (permission
, cancellable
, error
);
199 * g_permission_release_async:
200 * @permission: a #GPermission instance
201 * @cancellable: (allow-none): a #GCancellable, or %NULL
202 * @callback: the #GAsyncReadyCallback to call when done
203 * @user_data: the user data to pass to @callback
205 * Attempts to release the permission represented by @permission.
207 * This is the first half of the asynchronous version of
208 * g_permission_release().
213 g_permission_release_async (GPermission
*permission
,
214 GCancellable
*cancellable
,
215 GAsyncReadyCallback callback
,
218 G_PERMISSION_GET_CLASS (permission
)
219 ->release_async (permission
, cancellable
, callback
, user_data
);
223 * g_permission_release_finish:
224 * @permission: a #GPermission instance
225 * @result: the #GAsyncResult given to the #GAsyncReadyCallback
226 * @error: a pointer to a %NULL #GError, or %NULL
228 * Collects the result of attempting to release the permission
229 * represented by @permission.
231 * This is the second half of the asynchronous version of
232 * g_permission_release().
234 * Returns: %TRUE if the permission was successfully released
239 g_permission_release_finish (GPermission
*permission
,
240 GAsyncResult
*result
,
243 return G_PERMISSION_GET_CLASS (permission
)
244 ->release_finish (permission
, result
, error
);
248 * g_permission_get_allowed:
249 * @permission: a #GPermission instance
251 * Gets the value of the 'allowed' property. This property is %TRUE if
252 * the caller currently has permission to perform the action that
253 * @permission represents the permission to perform.
255 * Returns: the value of the 'allowed' property
260 g_permission_get_allowed (GPermission
*permission
)
262 return permission
->priv
->allowed
;
266 * g_permission_get_can_acquire:
267 * @permission: a #GPermission instance
269 * Gets the value of the 'can-acquire' property. This property is %TRUE
270 * if it is generally possible to acquire the permission by calling
271 * g_permission_acquire().
273 * Returns: the value of the 'can-acquire' property
278 g_permission_get_can_acquire (GPermission
*permission
)
280 return permission
->priv
->can_acquire
;
284 * g_permission_get_can_release:
285 * @permission: a #GPermission instance
287 * Gets the value of the 'can-release' property. This property is %TRUE
288 * if it is generally possible to release the permission by calling
289 * g_permission_release().
291 * Returns: the value of the 'can-release' property
296 g_permission_get_can_release (GPermission
*permission
)
298 return permission
->priv
->can_release
;
302 * g_permission_impl_update:
303 * @permission: a #GPermission instance
304 * @allowed: the new value for the 'allowed' property
305 * @can_acquire: the new value for the 'can-acquire' property
306 * @can_release: the new value for the 'can-release' property
308 * This function is called by the #GPermission implementation to update
309 * the properties of the permission. You should never call this
310 * function except from a #GPermission implementation.
312 * GObject notify signals are generated, as appropriate.
317 g_permission_impl_update (GPermission
*permission
,
319 gboolean can_acquire
,
320 gboolean can_release
)
322 GObject
*object
= G_OBJECT (permission
);
324 g_object_freeze_notify (object
);
326 allowed
= allowed
!= FALSE
;
327 if (allowed
!= permission
->priv
->allowed
)
329 permission
->priv
->allowed
= allowed
;
330 g_object_notify (object
, "allowed");
333 can_acquire
= can_acquire
!= FALSE
;
334 if (can_acquire
!= permission
->priv
->can_acquire
)
336 permission
->priv
->can_acquire
= can_acquire
;
337 g_object_notify (object
, "can-acquire");
340 can_release
= can_release
!= FALSE
;
341 if (can_release
!= permission
->priv
->can_release
)
343 permission
->priv
->can_release
= can_release
;
344 g_object_notify (object
, "can-release");
347 g_object_thaw_notify (object
);
351 g_permission_get_property (GObject
*object
, guint prop_id
,
352 GValue
*value
, GParamSpec
*pspec
)
354 GPermission
*permission
= G_PERMISSION (object
);
359 g_value_set_boolean (value
, permission
->priv
->allowed
);
362 case PROP_CAN_ACQUIRE
:
363 g_value_set_boolean (value
, permission
->priv
->can_acquire
);
366 case PROP_CAN_RELEASE
:
367 g_value_set_boolean (value
, permission
->priv
->can_release
);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
376 g_permission_init (GPermission
*permission
)
378 permission
->priv
= g_permission_get_instance_private (permission
);
382 acquire_or_release (GPermission
*permission
,
383 GCancellable
*cancellable
,
386 g_set_error_literal (error
,
387 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
388 "Can't acquire or release permission");
393 acquire_or_release_async (GPermission
*permission
,
394 GCancellable
*cancellable
,
395 GAsyncReadyCallback callback
,
398 g_simple_async_report_error_in_idle (G_OBJECT (permission
),
400 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
401 "Can't acquire or release permission");
405 acquire_or_release_finish (GPermission
*permission
,
406 GAsyncResult
*result
,
409 g_async_result_legacy_propagate_error (result
, error
);
414 g_permission_class_init (GPermissionClass
*class)
416 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
418 object_class
->get_property
= g_permission_get_property
;
420 class->acquire
= acquire_or_release
;
421 class->release
= acquire_or_release
;
422 class->acquire_async
= acquire_or_release_async
;
423 class->release_async
= acquire_or_release_async
;
424 class->acquire_finish
= acquire_or_release_finish
;
425 class->release_finish
= acquire_or_release_finish
;
428 * GPermission:allowed:
430 * %TRUE if the caller currently has permission to perform the action that
431 * @permission represents the permission to perform.
433 g_object_class_install_property (object_class
, PROP_ALLOWED
,
434 g_param_spec_boolean ("allowed",
436 P_("If the caller is allowed to perform the action"),
438 G_PARAM_STATIC_STRINGS
| G_PARAM_READABLE
));
441 * GPermission:can-acquire:
443 * %TRUE if it is generally possible to acquire the permission by calling
444 * g_permission_acquire().
446 g_object_class_install_property (object_class
, PROP_CAN_ACQUIRE
,
447 g_param_spec_boolean ("can-acquire",
449 P_("If calling g_permission_acquire() makes sense"),
451 G_PARAM_STATIC_STRINGS
| G_PARAM_READABLE
));
454 * GPermission:can-release:
456 * %TRUE if it is generally possible to release the permission by calling
457 * g_permission_release().
459 g_object_class_install_property (object_class
, PROP_CAN_RELEASE
,
460 g_param_spec_boolean ("can-release",
462 P_("If calling g_permission_release() makes sense"),
464 G_PARAM_STATIC_STRINGS
| G_PARAM_READABLE
));