[gnome-software] Sort using locale based algorithms
- From: Richard Hughes <rhughes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software] Sort using locale based algorithms
- Date: Thu, 5 Dec 2019 14:02:13 +0000 (UTC)
commit 7da315a0e3d7c8d686cc612db8e6cc833c6cf970
Author: Michael Terry <mike mterry name>
Date: Mon Nov 25 22:52:31 2019 -0500
Sort using locale based algorithms
lib/gs-plugin-loader.c | 13 +++----------
lib/gs-utils.c | 34 ++++++++++++++++++++++++++++++++++
lib/gs-utils.h | 3 +++
src/gs-category-page.c | 9 ++-------
src/gs-details-page.c | 7 ++++---
src/gs-extras-page.c | 9 ++++++---
src/gs-installed-page.c | 10 +++++-----
src/gs-removal-dialog.c | 3 ++-
src/gs-repos-dialog.c | 3 ++-
src/gs-updates-section.c | 6 +++++-
10 files changed, 66 insertions(+), 31 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index 5209b70f..4fb02d80 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -274,14 +274,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(GsPluginLoaderHelper, gs_plugin_loader_helper_free
static gint
gs_plugin_loader_app_sort_name_cb (GsApp *app1, GsApp *app2, gpointer user_data)
{
- g_autofree gchar *casefolded_name1 = NULL;
- g_autofree gchar *casefolded_name2 = NULL;
-
- if (gs_app_get_name (app1) != NULL)
- casefolded_name1 = g_utf8_casefold (gs_app_get_name (app1), -1);
- if (gs_app_get_name (app2) != NULL)
- casefolded_name2 = g_utf8_casefold (gs_app_get_name (app2), -1);
- return g_strcmp0 (casefolded_name1, casefolded_name2);
+ return gs_utils_sort_strcmp (gs_app_get_name (app1), gs_app_get_name (app2));
}
GsPlugin *
@@ -1470,8 +1463,8 @@ gs_plugin_loader_category_sort_cb (gconstpointer a, gconstpointer b)
return 1;
if (gs_category_get_score (cata) > gs_category_get_score (catb))
return -1;
- return g_strcmp0 (gs_category_get_name (cata),
- gs_category_get_name (catb));
+ return gs_utils_sort_strcmp (gs_category_get_name (cata),
+ gs_category_get_name (catb));
}
static void
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index 8cc0c7b0..92c36d62 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -343,6 +343,40 @@ gs_utils_strv_fnmatch (gchar **strv, const gchar *str)
return FALSE;
}
+/**
+ * gs_utils_sort_key:
+ * @str: A string to convert to a sort key
+ *
+ * Useful to sort strings in a locale-sensitive, presentational way.
+ * Case is ignored and utf8 collation is used (e.g. accents are ignored).
+ *
+ * Returns: a newly allocated string sort key
+ */
+gchar *
+gs_utils_sort_key (const gchar *str)
+{
+ g_autofree gchar *casefolded = g_utf8_casefold (str, -1);
+ return g_utf8_collate_key (casefolded, -1);
+}
+
+/**
+ * gs_utils_sort_strcmp:
+ * @str1: A string to compare
+ * @str2: A string to compare
+ *
+ * Compares two strings in a locale-sensitive, presentational way.
+ * Case is ignored and utf8 collation is used (e.g. accents are ignored).
+ *
+ * Returns: < 0 if str1 is before str2, 0 if equal, > 0 if str1 is after str2
+ */
+gint
+gs_utils_sort_strcmp (const gchar *str1, const gchar *str2)
+{
+ g_autofree gchar *key1 = gs_utils_sort_key (str1);
+ g_autofree gchar *key2 = gs_utils_sort_key (str2);
+ return g_strcmp0 (key1, key2);
+}
+
/**
* gs_utils_get_desktop_app_info:
* @id: A desktop ID, e.g. "gimp.desktop"
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index b98ede46..d7469c3e 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -54,6 +54,9 @@ GPermission *gs_utils_get_permission (const gchar *id,
GError **error);
gboolean gs_utils_strv_fnmatch (gchar **strv,
const gchar *str);
+gchar *gs_utils_sort_key (const gchar *str);
+gint gs_utils_sort_strcmp (const gchar *str1,
+ const gchar *str2);
GDesktopAppInfo *gs_utils_get_desktop_app_info (const gchar *id);
gboolean gs_utils_rmtree (const gchar *directory,
GError **error);
diff --git a/src/gs-category-page.c b/src/gs-category-page.c
index 4f777d92..a786d48d 100644
--- a/src/gs-category-page.c
+++ b/src/gs-category-page.c
@@ -17,6 +17,7 @@
#include "gs-summary-tile.h"
#include "gs-popular-tile.h"
#include "gs-category-page.h"
+#include "gs-utils.h"
typedef enum {
SUBCATEGORY_SORT_TYPE_RATING,
@@ -182,8 +183,6 @@ gs_category_page_sort_flow_box_sort_func (GtkFlowBoxChild *child1,
GsApp *app1 = gs_app_tile_get_app (GS_APP_TILE (gtk_bin_get_child (GTK_BIN (child1))));
GsApp *app2 = gs_app_tile_get_app (GS_APP_TILE (gtk_bin_get_child (GTK_BIN (child2))));
SubcategorySortType sort_type;
- g_autofree gchar *casefolded_name1 = NULL;
- g_autofree gchar *casefolded_name2 = NULL;
if (!GS_IS_APP (app1) || !GS_IS_APP (app2))
return 0;
@@ -199,11 +198,7 @@ gs_category_page_sort_flow_box_sort_func (GtkFlowBoxChild *child1,
return 1;
}
- if (gs_app_get_name (app1) != NULL)
- casefolded_name1 = g_utf8_casefold (gs_app_get_name (app1), -1);
- if (gs_app_get_name (app2) != NULL)
- casefolded_name2 = g_utf8_casefold (gs_app_get_name (app2), -1);
- return g_strcmp0 (casefolded_name1, casefolded_name2);
+ return gs_utils_sort_strcmp (gs_app_get_name (app1), gs_app_get_name (app2));
}
static void
diff --git a/src/gs-details-page.c b/src/gs-details-page.c
index d2d9255e..e4a98c49 100644
--- a/src/gs-details-page.c
+++ b/src/gs-details-page.c
@@ -15,6 +15,7 @@
#include "gs-common.h"
#include "gs-content-rating.h"
+#include "gs-utils.h"
#include "gs-details-page.h"
#include "gs-app-addon-row.h"
@@ -1415,8 +1416,8 @@ list_sort_func (GtkListBoxRow *a,
GsApp *a1 = gs_app_addon_row_get_addon (GS_APP_ADDON_ROW (a));
GsApp *a2 = gs_app_addon_row_get_addon (GS_APP_ADDON_ROW (b));
- return g_strcmp0 (gs_app_get_name (a1),
- gs_app_get_name (a2));
+ return gs_utils_sort_strcmp (gs_app_get_name (a1),
+ gs_app_get_name (a2));
}
static void gs_details_page_addon_selected_cb (GsAppAddonRow *row, GParamSpec *pspec, GsDetailsPage *self);
@@ -2027,7 +2028,7 @@ origin_popover_list_sort_func (GtkListBoxRow *a,
g_autofree gchar *a1_origin = gs_app_get_origin_ui (a1);
g_autofree gchar *a2_origin = gs_app_get_origin_ui (a2);
- return g_strcmp0 (a1_origin, a2_origin);
+ return gs_utils_sort_strcmp (a1_origin, a2_origin);
}
static void
diff --git a/src/gs-extras-page.c b/src/gs-extras-page.c
index d31ad774..c8900c25 100644
--- a/src/gs-extras-page.c
+++ b/src/gs-extras-page.c
@@ -14,6 +14,7 @@
#include "gs-language.h"
#include "gs-shell.h"
#include "gs-common.h"
+#include "gs-utils.h"
#include "gs-vendor.h"
#include <glib/gi18n.h>
@@ -1055,7 +1056,8 @@ row_activated_cb (GtkListBox *list_box,
static gchar *
get_app_sort_key (GsApp *app)
{
- g_autoptr(GString) key = NULL;
+ GString *key = NULL;
+ g_autofree gchar *sort_name = NULL;
key = g_string_sized_new (64);
@@ -1070,9 +1072,10 @@ get_app_sort_key (GsApp *app)
}
/* finally, sort by short name */
- g_string_append (key, gs_app_get_name (app));
+ sort_name = gs_utils_sort_key (gs_app_get_name (app));
+ g_string_append (key, sort_name);
- return g_utf8_casefold (key->str, (gssize) key->len);
+ return g_string_free (key, FALSE);
}
static gint
diff --git a/src/gs-installed-page.c b/src/gs-installed-page.c
index 187badf6..56ab3b8f 100644
--- a/src/gs-installed-page.c
+++ b/src/gs-installed-page.c
@@ -16,6 +16,7 @@
#include "gs-installed-page.h"
#include "gs-common.h"
#include "gs-app-row.h"
+#include "gs-utils.h"
struct _GsInstalledPage
{
@@ -315,7 +316,7 @@ static gchar *
gs_installed_page_get_app_sort_key (GsApp *app)
{
GString *key;
- g_autofree gchar *casefolded_name = NULL;
+ g_autofree gchar *sort_name = NULL;
key = g_string_sized_new (64);
@@ -376,10 +377,9 @@ gs_installed_page_get_app_sort_key (GsApp *app)
g_string_append (key, "2:");
/* finally, sort by short name */
- if (gs_app_get_name (app) != NULL) {
- casefolded_name = g_utf8_casefold (gs_app_get_name (app), -1);
- g_string_append (key, casefolded_name);
- }
+ sort_name = gs_utils_sort_key (gs_app_get_name (app));
+ g_string_append (key, sort_name);
+
return g_string_free (key, FALSE);
}
diff --git a/src/gs-removal-dialog.c b/src/gs-removal-dialog.c
index 7f22b6a3..b037a243 100644
--- a/src/gs-removal-dialog.c
+++ b/src/gs-removal-dialog.c
@@ -8,6 +8,7 @@
#include "config.h"
#include "gs-removal-dialog.h"
+#include "gs-utils.h"
#include <glib/gi18n.h>
#include <gtk/gtk.h>
@@ -64,7 +65,7 @@ add_app (GtkListBox *listbox, GsApp *app)
g_object_set_data_full (G_OBJECT (box),
"sort",
- g_utf8_casefold (gs_app_get_name (app), -1),
+ gs_utils_sort_key (gs_app_get_name (app)),
g_free);
gtk_list_box_prepend (listbox, box);
diff --git a/src/gs-repos-dialog.c b/src/gs-repos-dialog.c
index 478acb7d..2512a36f 100644
--- a/src/gs-repos-dialog.c
+++ b/src/gs-repos-dialog.c
@@ -16,6 +16,7 @@
#include "gs-os-release.h"
#include "gs-repo-row.h"
#include "gs-third-party-repo-row.h"
+#include "gs-utils.h"
#include <glib/gi18n.h>
struct _GsReposDialog
@@ -682,7 +683,7 @@ get_row_sort_key (GtkListBoxRow *row)
app = gs_repo_row_get_repo (GS_REPO_ROW (row));
}
- sort_key = g_utf8_casefold (gs_app_get_name (app), -1);
+ sort_key = gs_utils_sort_key (gs_app_get_name (app));
return g_strdup_printf ("%u:%s", sort_order, sort_key);
}
diff --git a/src/gs-updates-section.c b/src/gs-updates-section.c
index c4567b12..a3d6b893 100644
--- a/src/gs-updates-section.c
+++ b/src/gs-updates-section.c
@@ -18,6 +18,7 @@
#include "gs-progress-button.h"
#include "gs-update-dialog.h"
#include "gs-updates-section.h"
+#include "gs-utils.h"
struct _GsUpdatesSection
{
@@ -131,6 +132,7 @@ static gchar *
_get_app_sort_key (GsApp *app)
{
GString *key;
+ g_autofree gchar *sort_name = NULL;
key = g_string_sized_new (64);
@@ -169,7 +171,9 @@ _get_app_sort_key (GsApp *app)
}
/* finally, sort by short name */
- g_string_append (key, gs_app_get_name (app));
+ sort_name = gs_utils_sort_key (gs_app_get_name (app));
+ g_string_append (key, sort_name);
+
return g_string_free (key, FALSE);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]