[gnome-software: 1/20] lib: Factor out some common sort functions for apps
- From: Milan Crha <mcrha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software: 1/20] lib: Factor out some common sort functions for apps
- Date: Mon, 11 Jul 2022 08:41:47 +0000 (UTC)
commit 67c717850b8bb7d48aa2da06d2e8f43c90d4a26f
Author: Philip Withnall <pwithnall endlessos org>
Date: Fri Jul 8 11:40:59 2022 +0100
lib: Factor out some common sort functions for apps
These will be used more in upcoming commits.
Signed-off-by: Philip Withnall <pwithnall endlessos org>
lib/gs-cmd.c | 10 +-------
lib/gs-plugin-loader.c | 22 ++---------------
lib/gs-utils.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
lib/gs-utils.h | 10 ++++++++
src/gs-category-page.c | 10 +-------
5 files changed, 80 insertions(+), 38 deletions(-)
---
diff --git a/lib/gs-cmd.c b/lib/gs-cmd.c
index 6aa0819d2..793bda209 100644
--- a/lib/gs-cmd.c
+++ b/lib/gs-cmd.c
@@ -285,14 +285,6 @@ gs_cmd_self_free (GsCmdSelf *self)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GsCmdSelf, gs_cmd_self_free)
-static gint
-app_sort_name_cb (GsApp *app1,
- GsApp *app2,
- gpointer user_data)
-{
- return gs_utils_sort_strcmp (gs_app_get_name (app1), gs_app_get_name (app2));
-}
-
static gint
app_sort_kind_cb (GsApp *app1, GsApp *app2, gpointer user_data)
{
@@ -770,7 +762,7 @@ main (int argc, char **argv)
query = gs_app_query_new ("category", category,
"refine-flags", self->refine_flags,
"max-results", self->max_results,
- "sort-func", app_sort_name_cb,
+ "sort-func", gs_utils_app_sort_name,
NULL);
if (self->interactive)
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index a7f86d5f2..fecba821c 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -1080,24 +1080,6 @@ gs_plugin_loader_get_app_is_compatible (GsApp *app,
/******************************************************************************/
-static gint
-gs_plugin_loader_app_sort_match_value_cb (GsApp *app1, GsApp *app2, gpointer user_data)
-{
- if (gs_app_get_match_value (app1) > gs_app_get_match_value (app2))
- return -1;
- if (gs_app_get_match_value (app1) < gs_app_get_match_value (app2))
- return 1;
- return 0;
-}
-
-static gint
-gs_plugin_loader_app_sort_prio_cb (GsApp *app1, GsApp *app2, gpointer user_data)
-{
- return gs_app_compare_priority (app1, app2);
-}
-
-/******************************************************************************/
-
/**
* gs_plugin_loader_job_process_finish:
* @plugin_loader: A #GsPluginLoader
@@ -4103,13 +4085,13 @@ job_process_cb (GTask *task)
case GS_PLUGIN_ACTION_SEARCH:
if (gs_plugin_job_get_sort_func (plugin_job, NULL) == NULL) {
gs_plugin_job_set_sort_func (plugin_job,
- gs_plugin_loader_app_sort_match_value_cb, NULL);
+ gs_utils_app_sort_match_value, NULL);
}
break;
case GS_PLUGIN_ACTION_GET_ALTERNATES:
if (gs_plugin_job_get_sort_func (plugin_job, NULL) == NULL) {
gs_plugin_job_set_sort_func (plugin_job,
- gs_plugin_loader_app_sort_prio_cb, NULL);
+ gs_utils_app_sort_priority, NULL);
}
break;
default:
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index b14e24de7..73743008e 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -39,6 +39,7 @@
#endif
#include "gs-app.h"
+#include "gs-app-private.h"
#include "gs-utils.h"
#include "gs-plugin.h"
@@ -1627,3 +1628,68 @@ gs_utils_get_upgrade_background (const gchar *version)
return NULL;
}
+
+/**
+ * gs_utils_app_sort_name:
+ * @app1: a #GsApp
+ * @app2: another #GsApp
+ * @user_data: data passed to the sort function
+ *
+ * Comparison function to sort apps in increasing alphabetical order of name.
+ *
+ * This is suitable for passing to gs_app_list_sort().
+ *
+ * Returns: a strcmp()-style sort value comparing @app1 to @app2
+ * Since: 43
+ */
+gint
+gs_utils_app_sort_name (GsApp *app1,
+ GsApp *app2,
+ gpointer user_data)
+{
+ return gs_utils_sort_strcmp (gs_app_get_name (app1), gs_app_get_name (app2));
+}
+
+/**
+ * gs_utils_app_sort_match_value:
+ * @app1: a #GsApp
+ * @app2: another #GsApp
+ * @user_data: data passed to the sort function
+ *
+ * Comparison function to sort apps in decreasing order of match value
+ * (#GsApp:match-value).
+ *
+ * This is suitable for passing to gs_app_list_sort().
+ *
+ * Returns: a strcmp()-style sort value comparing @app1 to @app2
+ * Since: 43
+ */
+gint
+gs_utils_app_sort_match_value (GsApp *app1,
+ GsApp *app2,
+ gpointer user_data)
+{
+ return gs_app_get_match_value (app2) - gs_app_get_match_value (app1);
+}
+
+/**
+ * gs_utils_app_sort_priority:
+ * @app1: a #GsApp
+ * @app2: another #GsApp
+ * @user_data: data passed to the sort function
+ *
+ * Comparison function to sort apps in increasing order of their priority
+ * (#GsApp:priority).
+ *
+ * This is suitable for passing to gs_app_list_sort().
+ *
+ * Returns: a strcmp()-style sort value comparing @app1 to @app2
+ * Since: 43
+ */
+gint
+gs_utils_app_sort_priority (GsApp *app1,
+ GsApp *app2,
+ gpointer user_data)
+{
+ return gs_app_compare_priority (app1, app2);
+}
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index 7d1ce10ab..855e9845a 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -142,4 +142,14 @@ gboolean gs_utils_set_file_etag (GFile *file,
gchar *gs_utils_get_upgrade_background (const gchar *version);
+gint gs_utils_app_sort_name (GsApp *app1,
+ GsApp *app2,
+ gpointer user_data);
+gint gs_utils_app_sort_match_value (GsApp *app1,
+ GsApp *app2,
+ gpointer user_data);
+gint gs_utils_app_sort_priority (GsApp *app1,
+ GsApp *app2,
+ gpointer user_data);
+
G_END_DECLS
diff --git a/src/gs-category-page.c b/src/gs-category-page.c
index e60c4c6b1..ba57ed2c8 100644
--- a/src/gs-category-page.c
+++ b/src/gs-category-page.c
@@ -75,14 +75,6 @@ top_carousel_app_clicked_cb (GsFeaturedCarousel *carousel,
g_signal_emit (self, obj_signals[SIGNAL_APP_CLICKED], 0, app);
}
-static gint
-app_sort_name_cb (GsApp *app1,
- GsApp *app2,
- gpointer user_data)
-{
- return gs_utils_sort_strcmp (gs_app_get_name (app1), gs_app_get_name (app2));
-}
-
static gint
_max_results_sort_cb (GsApp *app1, GsApp *app2, gpointer user_data)
{
@@ -443,7 +435,7 @@ gs_category_page_load_category (GsCategoryPage *self)
featured_query = gs_app_query_new ("category", featured_subcat,
"refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_KUDOS,
- "sort-func", app_sort_name_cb,
+ "sort-func", gs_utils_app_sort_name,
NULL);
featured_plugin_job = gs_plugin_job_list_apps_new (featured_query,
GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);
gs_plugin_loader_job_process_async (self->plugin_loader,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]