[rygel] core: Add support for sorted Browse/Search



commit bd9c0b159f1712fa2a8ff5204ae7117f900b94d3
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Fri Jul 23 19:58:10 2010 +0300

    core: Add support for sorted Browse/Search

 src/rygel/rygel-media-object.vala       |   16 +++++++++++++
 src/rygel/rygel-media-query-action.vala |   37 +++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 0 deletions(-)
---
diff --git a/src/rygel/rygel-media-object.vala b/src/rygel/rygel-media-object.vala
index a026b92..1039781 100644
--- a/src/rygel/rygel-media-object.vala
+++ b/src/rygel/rygel-media-object.vala
@@ -94,4 +94,20 @@ public abstract class Rygel.MediaObject : GLib.Object {
 
         return null;
     }
+
+    internal virtual int compare_by_property (MediaObject media_object,
+                                              string      property) {
+        switch (property) {
+        case "@id":
+            return strcmp (this.id, media_object.id);
+        case "@parentID":
+            return strcmp (this.parent, media_object.parent);
+        case "dc:title":
+            return strcmp (this.title, media_object.title);
+        case "upnp:class":
+            return strcmp (this.upnp_class, media_object.upnp_class);
+        default:
+            return 0;
+        }
+    }
 }
diff --git a/src/rygel/rygel-media-query-action.vala b/src/rygel/rygel-media-query-action.vala
index 600544e..5518a79 100644
--- a/src/rygel/rygel-media-query-action.vala
+++ b/src/rygel/rygel-media-query-action.vala
@@ -49,6 +49,7 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
     protected XBoxHacks xbox_hacks;
     protected string object_id_arg;
 
+    private string[] sort_props;
 
     protected MediaQueryAction (ContentDirectory    content_dir,
                                 owned ServiceAction action) {
@@ -78,6 +79,8 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
                 this.update_id = uint32.MAX;
             }
 
+            this.sort_media_objects (results);
+
             foreach (var result in results) {
                 if (result is MediaItem && this.xbox_hacks != null) {
                     this.xbox_hacks.apply (result as MediaItem);
@@ -180,4 +183,38 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
 
         this.completed ();
     }
+
+    private void sort_media_objects (Gee.List<MediaObject> media_objects) {
+        if (this.sort_criteria == null) {
+            return;
+        }
+
+        this.sort_props = this.sort_criteria.split (",");
+        if (this.sort_props.length == 0) {
+            return;
+        }
+
+        media_objects.sort_with_data (this.compare_media_objects);
+    }
+
+    private int compare_media_objects (void *a, void *b) {
+        var object_a = a as MediaObject;
+        var object_b = b as MediaObject;
+
+        int i;
+        int ret = 0;
+
+        for (i = 0; ret == 0 && i < this.sort_props.length; i++) {
+            var property = this.sort_props [i].substring (1);
+
+            ret = object_a.compare_by_property (object_b, property);
+
+            if (this.sort_props [i][0] == '-') {
+                // Need it in descending order so reverse the comparison
+                ret = 0 - ret;
+            }
+        }
+
+        return ret;
+    }
 }



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