[gnome-boxes] collection: Order machines by last access time



commit 563bf0d24da01e3167e1e45e785d8d7bde2a2bdc
Author: Marc-André Lureau <marcandre lureau gmail com>
Date:   Wed Oct 24 23:11:11 2012 +0200

    collection: Order machines by last access time
    
    Co-author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680826

 src/box-config.vala                  |   26 ++++++++++++++++++++++++++
 src/collection-view.vala             |    2 +-
 src/collection.vala                  |   19 +++++++++++++++++++
 src/gnome-boxes-search-provider.vala |   23 +----------------------
 src/machine.vala                     |    6 ++++++
 5 files changed, 53 insertions(+), 23 deletions(-)
---
diff --git a/src/box-config.vala b/src/box-config.vala
index b2513e3..7d8cfb3 100644
--- a/src/box-config.vala
+++ b/src/box-config.vala
@@ -181,4 +181,30 @@ public class Boxes.BoxConfig: GLib.Object, Boxes.IConfig {
         }
         return true;
     }
+
+    public int compare (BoxConfig other) {
+        // sort first by last time used
+        if (access_last_time > 0 && other.access_last_time > 0) {
+            if (access_last_time > other.access_last_time)
+                return -1;
+            else if (access_last_time < other.access_last_time)
+                return 1;
+        }
+
+        var name = last_seen_name;
+        var other_name = other.last_seen_name;
+
+        // then by name
+        if (is_set (name) && is_set (other_name))
+            return name.collate (other_name);
+
+        // Sort empty names last
+        if (is_set (name))
+            return -1;
+        if (is_set (other_name))
+            return 1;
+
+        return 0;
+    }
+
 }
diff --git a/src/collection-view.vala b/src/collection-view.vala
index b54d782..125143f 100644
--- a/src/collection-view.vala
+++ b/src/collection-view.vala
@@ -293,7 +293,7 @@ private class Boxes.CollectionView: Boxes.UI {
             if (item_a == null || item_b == null) // FIXME?!
                 return 0;
 
-            return item_a.name.collate (item_b.name);
+            return item_a.compare (item_b);
         });
         model.set_sort_column_id (Gtk.SortColumn.DEFAULT, Gtk.SortType.ASCENDING);
         model.row_deleted.connect (() => {
diff --git a/src/collection.vala b/src/collection.vala
index 7b5a4b1..91e64bf 100644
--- a/src/collection.vala
+++ b/src/collection.vala
@@ -2,6 +2,25 @@
 
 private abstract class Boxes.CollectionItem: Boxes.UI {
     public string name { set; get; }
+
+    public virtual int compare (CollectionItem other) {
+        // First machines before non-machines
+        if (other is Machine)
+            return 1;
+
+        // Then non-machine
+        // First by name
+        if (is_set (name) && is_set (other.name))
+            return name.collate (other.name);
+
+        // Sort empty names last
+        if (is_set (name))
+            return -1;
+        if (is_set (other.name))
+            return 1;
+
+        return 0;
+    }
 }
 
 private class Boxes.Collection: GLib.Object {
diff --git a/src/gnome-boxes-search-provider.vala b/src/gnome-boxes-search-provider.vala
index bea7e09..e7a5f1b 100644
--- a/src/gnome-boxes-search-provider.vala
+++ b/src/gnome-boxes-search-provider.vala
@@ -53,27 +53,6 @@ public class Boxes.SearchProvider: Object {
         loading = false;
     }
 
-    private static int compare_boxes (BoxConfig a, BoxConfig b) {
-        // sort first by last time used
-        if (a.access_last_time > b.access_last_time)
-            return -1;
-
-        var a_name = a.last_seen_name;
-        var b_name = b.last_seen_name;
-
-        // then by name
-        if (is_set (a_name) && is_set (b_name))
-            return a_name.collate (b_name);
-
-        // Sort empty names last
-        if (is_set (a_name))
-            return -1;
-        if (is_set (b_name))
-            return -1;
-
-        return 0;
-    }
-
     private async string[] search (owned string[] terms) {
         app.hold ();
         string[] normalized_terms = canonicalize_for_search (string.joinv(" ", terms)).split(" ");
@@ -88,7 +67,7 @@ public class Boxes.SearchProvider: Object {
                 matches.add (box);
         }
 
-        matches.sort((CompareFunc<BoxConfig>) compare_boxes);
+        matches.sort( (a, b) => { return a.compare (b); });
         var results = new string[matches.length];
         for (int i = 0; i < matches.length; i++)
             results[i] = matches[i].get_data ("search-id");
diff --git a/src/machine.vala b/src/machine.vala
index 195d032..85c1b3a 100644
--- a/src/machine.vala
+++ b/src/machine.vala
@@ -506,7 +506,13 @@ private abstract class Boxes.Machine: Boxes.CollectionItem, Boxes.IPropertiesPro
             machine_actor.update_thumbnail (null, false);
             disconnect_display ();
         }
+    }
 
+    public override int compare (CollectionItem other) {
+        if (other is Machine)
+            return config.compare ((other as Machine).config);
+        else
+            return -1; // Machines are listed before non-machines
     }
 }
 


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