[gnome-software: 7/29] lib: Add async version of gs_utils_get_permission()




commit 06a637e173eadc9ee59aba871aaf65ebbffc99eb
Author: Philip Withnall <pwithnall endlessos org>
Date:   Fri Oct 15 13:43:38 2021 +0100

    lib: Add async version of gs_utils_get_permission()
    
    It will be used in upcoming commits.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1472

 lib/gs-utils.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/gs-utils.h |  6 ++++++
 2 files changed, 61 insertions(+)
---
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index b88da279b..3f95f325a 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -275,6 +275,61 @@ gs_utils_get_permission (const gchar *id, GCancellable *cancellable, GError **er
 #endif
 }
 
+/**
+ * gs_utils_get_permission_async:
+ * @id: a polkit action ID, for example `org.freedesktop.packagekit.trigger-offline-update`
+ * @cancellable: (nullable): a #GCancellable, or %NULL
+ * @callback: callback for when the asynchronous operation is complete
+ * @user_data: data to pass to @callback
+ *
+ * Asynchronously gets a #GPermission object representing the given polkit
+ * action @id.
+ *
+ * Since: 42
+ */
+void
+gs_utils_get_permission_async (const gchar         *id,
+                               GCancellable        *cancellable,
+                               GAsyncReadyCallback  callback,
+                               gpointer             user_data)
+{
+       g_return_if_fail (id != NULL);
+       g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+
+#ifdef HAVE_POLKIT
+       polkit_permission_new (id, NULL, cancellable, callback, user_data);
+#else
+       g_task_report_new_error (NULL, callback, user_data, gs_utils_get_permission_async,
+                                GS_PLUGIN_ERROR,
+                                GS_PLUGIN_ERROR_NOT_SUPPORTED,
+                                "no PolicyKit, so can't return GPermission for %s", id);
+#endif
+}
+
+/**
+ * gs_utils_get_permission_finish:
+ * @result: result of the asynchronous operation
+ * @error: return location for a #GError, or %NULL
+ *
+ * Finish an asynchronous operation started with gs_utils_get_permission_async().
+ *
+ * Returns: (transfer full): a #GPermission representing the given action ID
+ * Since: 42
+ */
+GPermission *
+gs_utils_get_permission_finish (GAsyncResult  *result,
+                                GError       **error)
+{
+       g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+#ifdef HAVE_POLKIT
+       return polkit_permission_new_finish (result, error);
+#else
+       return g_task_propagate_pointer (G_TASK (result), error);
+#endif
+}
+
 /**
  * gs_utils_get_content_type:
  * @file: A GFile
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index 38fcf7dbc..b5f590df8 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -54,6 +54,12 @@ gchar                *gs_utils_get_user_hash         (GError         **error);
 GPermission    *gs_utils_get_permission        (const gchar    *id,
                                                 GCancellable   *cancellable,
                                                 GError         **error);
+void            gs_utils_get_permission_async  (const gchar            *id,
+                                                GCancellable           *cancellable,
+                                                GAsyncReadyCallback     callback,
+                                                gpointer                user_data);
+GPermission    *gs_utils_get_permission_finish (GAsyncResult   *result,
+                                                GError         **error);
 gboolean        gs_utils_strv_fnmatch          (gchar          **strv,
                                                 const gchar    *str);
 gchar           *gs_utils_sort_key             (const gchar    *str);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]