[gnome-software] trivial: Add gs_app_compare_priority()



commit 2a027eb5c67c4ab9014c4ac8c0db7a8253f77617
Author: Richard Hughes <richard hughsie com>
Date:   Fri Sep 28 10:59:50 2018 +0100

    trivial: Add gs_app_compare_priority()
    
    This moves out the priority comparison logic into common code.

 lib/gs-app-private.h   |  2 ++
 lib/gs-app.c           | 33 +++++++++++++++++++++++++++++++++
 lib/gs-plugin-loader.c | 13 +------------
 3 files changed, 36 insertions(+), 12 deletions(-)
---
diff --git a/lib/gs-app-private.h b/lib/gs-app-private.h
index 6e7c6e00..e6ba9aa8 100644
--- a/lib/gs-app-private.h
+++ b/lib/gs-app-private.h
@@ -38,6 +38,8 @@ GCancellable  *gs_app_get_cancellable         (GsApp          *app);
 GsPluginAction  gs_app_get_pending_action      (GsApp          *app);
 void            gs_app_set_pending_action      (GsApp          *app,
                                                 GsPluginAction  action);
+gint            gs_app_compare_priority        (GsApp          *app1,
+                                                GsApp          *app2);
 
 G_END_DECLS
 
diff --git a/lib/gs-app.c b/lib/gs-app.c
index 3247966c..74580b2b 100644
--- a/lib/gs-app.c
+++ b/lib/gs-app.c
@@ -276,6 +276,39 @@ gs_app_get_unique_id_unlocked (GsApp *app)
        return priv->unique_id;
 }
 
+/**
+ * gs_app_compare_priority:
+ * @app1: a #GsApp
+ * @app2: a #GsApp
+ *
+ * Compares two applications using thier priority.
+ *
+ * Use `gs_plugin_add_rule(plugin,GS_PLUGIN_RULE_BETTER_THAN,"plugin-name")`
+ * to set the application priority values.
+ *
+ * Returns: a negative value if @app1 is less than @app2, a positive value if
+ *          @app1 is greater than @app2, and zero if @app1 is equal to @app2
+ **/
+gint
+gs_app_compare_priority (GsApp *app1, GsApp *app2)
+{
+       GsAppPrivate *priv1 = gs_app_get_instance_private (app1);
+       GsAppPrivate *priv2 = gs_app_get_instance_private (app2);
+
+       /* prefer prio */
+       if (priv1->priority > priv2->priority)
+               return -1;
+       if (priv1->priority < priv2->priority)
+               return 1;
+
+       /* fall back to bundle kind */
+       if (priv1->bundle_kind < priv2->bundle_kind)
+               return -1;
+       if (priv1->bundle_kind > priv2->bundle_kind)
+               return 1;
+       return 0;
+}
+
 /**
  * _as_app_quirk_to_string:
  * @quirk: a #AsAppQuirk
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index fd79e884..30f51d94 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -1402,18 +1402,7 @@ gs_plugin_loader_app_sort_match_value_cb (GsApp *app1, GsApp *app2, gpointer use
 static gint
 gs_plugin_loader_app_sort_prio_cb (GsApp *app1, GsApp *app2, gpointer user_data)
 {
-       /* prefer prio */
-       if (gs_app_get_priority (app1) > gs_app_get_priority (app2))
-               return -1;
-       if (gs_app_get_priority (app1) < gs_app_get_priority (app2))
-               return 1;
-
-       /* fall back to bundle kind */
-       if (gs_app_get_bundle_kind (app1) < gs_app_get_bundle_kind (app2))
-               return -1;
-       if (gs_app_get_bundle_kind (app1) > gs_app_get_bundle_kind (app2))
-               return 1;
-       return 0;
+       return gs_app_compare_priority (app1, app2);
 }
 
 /******************************************************************************/


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