From eb2b1b284d08d045cf393bcbf2045965f263d781 Mon Sep 17 00:00:00 2001
From: Ethan0456 <amethhac@gmail.com>
Date: Thu, 25 Nov 2021 13:15:30 +0100
Subject: [PATCH] Use alphabetical sorting for devices in the side-pane (Issue
 #610)

As well fixes Issue #296

MR: !172
---
 thunar/thunar-device.c          | 29 +++++++++++++++++++++++++----
 thunar/thunar-device.h          |  2 +-
 thunar/thunar-shortcuts-model.c |  3 +--
 thunar/thunar-tree-model.c      |  3 +--
 4 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/thunar/thunar-device.c b/thunar/thunar-device.c
index 101caff24..6e6d91249 100644
--- a/thunar/thunar-device.c
+++ b/thunar/thunar-device.c
@@ -328,6 +328,18 @@ thunar_device_emit_pre_unmount (ThunarDevice *device,
 
 
 
+/**
+ * thunar_device_get_name:
+ * @device     : a #ThunarDevice instance.
+ *
+ * Returns the name of the @device if the @device
+ * is mounted.
+ *
+ * The caller is responsible to free the returned string
+ * using g_free() when no longer needed.
+ *
+ * Return value: the name of @device if @device is mounted
+ **/
 gchar *
 thunar_device_get_name (const ThunarDevice *device)
 {
@@ -664,9 +676,12 @@ thunar_device_get_root (const ThunarDevice *device)
 
 
 gint
-thunar_device_sort (const ThunarDevice *device1,
-                    const ThunarDevice *device2)
+thunar_device_compare_by_name (const ThunarDevice *device1,
+                               const ThunarDevice *device2)
 {
+  gchar* name1;
+  gchar* name2;
+
   _thunar_return_val_if_fail (THUNAR_IS_DEVICE (device1), 0);
   _thunar_return_val_if_fail (THUNAR_IS_DEVICE (device2), 0);
 
@@ -674,8 +689,14 @@ thunar_device_sort (const ThunarDevice *device1,
   if (G_OBJECT_TYPE (device1->device) != G_OBJECT_TYPE (device2->device))
     return G_IS_MOUNT (device1->device) ? 1 : -1;
 
-  /* sort by detect stamp */
-  return device1->stamp > device2->stamp ? 1 : -1;
+  name1 = thunar_device_get_name (device1);
+  name2 = thunar_device_get_name (device2);
+
+  /* code which compares device names */
+  return g_strcmp0 (name1, name2);
+
+  g_free (name1);
+  g_free (name2);
 }
 
 
diff --git a/thunar/thunar-device.h b/thunar/thunar-device.h
index 9189364bc..df2c5f7c4 100644
--- a/thunar/thunar-device.h
+++ b/thunar/thunar-device.h
@@ -72,7 +72,7 @@ gboolean             thunar_device_is_mounted       (const ThunarDevice   *devic
 
 GFile               *thunar_device_get_root         (const ThunarDevice   *device);
 
-gint                 thunar_device_sort             (const ThunarDevice   *device1,
+gint                 thunar_device_compare_by_name  (const ThunarDevice   *device1,
                                                      const ThunarDevice   *device2);
 
 void                 thunar_device_mount            (ThunarDevice         *device,
diff --git a/thunar/thunar-shortcuts-model.c b/thunar/thunar-shortcuts-model.c
index 5e015e091..55e5aa8fa 100644
--- a/thunar/thunar-shortcuts-model.c
+++ b/thunar/thunar-shortcuts-model.c
@@ -1227,9 +1227,8 @@ thunar_shortcuts_model_sort_func (gconstpointer shortcut_a,
   if (a->sort_id != b->sort_id)
     return a->sort_id > b->sort_id ? 1 : -1;
 
-  /* properly sort devices by timestamp */
   if (a->device != NULL && b->device != NULL)
-    return thunar_device_sort (a->device, b->device);
+    return thunar_device_compare_by_name (a->device, b->device);
 
   return g_strcmp0 (a->name, b->name);
 }
diff --git a/thunar/thunar-tree-model.c b/thunar/thunar-tree-model.c
index b961eac25..fcd49cd87 100644
--- a/thunar/thunar-tree-model.c
+++ b/thunar/thunar-tree-model.c
@@ -1110,8 +1110,7 @@ thunar_tree_model_device_added (ThunarDeviceMonitor *device_monitor,
       if (item->device == NULL)
         break;
 
-      /* sort devices by timestamp */
-      if (thunar_device_sort (item->device, device) > 0)
+      if (thunar_device_compare_by_name (item->device, device) > 0)
         break;
     }
 
-- 
GitLab