core: Support sorting by essential item properties
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 26 Jul 2010 18:49:11 +0000 (21:49 +0300)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 2 Aug 2010 15:55:48 +0000 (18:55 +0300)
src/rygel/rygel-media-item.vala

index 439b875..b1b10ef 100644 (file)
@@ -237,6 +237,24 @@ public class Rygel.MediaItem : MediaObject {
         return res;
     }
 
+    internal override int compare_by_property (MediaObject media_object,
+                                               string      property) {
+        var item = media_object as MediaItem;
+
+        switch (property) {
+        case "dc:creator":
+        case "dc:artist":
+        case "dc:author":
+            return this.compare_string_props (this.author, item.author);
+        case "upnp:album":
+            return this.compare_string_props (this.album, item.album);
+        case "dc:date":
+            return this.compare_by_date (item);
+        default:
+            return base.compare_by_property (item, property);
+        }
+    }
+
     private ProtocolInfo get_protocol_info (string? uri,
                                             string  protocol) {
         var protocol_info = new ProtocolInfo ();
@@ -285,4 +303,35 @@ public class Rygel.MediaItem : MediaObject {
             return scheme;
         }
     }
+
+    private int compare_by_date (MediaItem item) {
+        if (this.date == null) {
+            return -1;
+        } else if (item.date == null) {
+            return 1;
+        } else {
+            var tv1 = TimeVal ();
+            assert (tv1.from_iso8601 (this.date));
+
+            var tv2 = TimeVal ();
+            assert (tv2.from_iso8601 (item.date));
+
+            var ret = this.compare_long (tv1.tv_sec, tv2.tv_sec);
+            if (ret == 0) {
+                ret = this.compare_long (tv1.tv_usec, tv2.tv_usec);
+            }
+
+            return ret;
+        }
+    }
+
+    private int compare_long (long a, long b) {
+        if (a < b) {
+            return -1;
+        } else if (a > b) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
 }