[rygel] tracker: Use SparqlCursor directly



commit 59d16044d86368a0d2a88b00ad6276ee0b2e9779
Author: Jens Georg <mail jensge org>
Date:   Thu Mar 29 19:44:27 2012 +0100

    tracker: Use SparqlCursor directly
    
    Intermediate code copied strings around.

 configure.ac                                       |    1 -
 .../tracker/rygel-tracker-item-factory.vala        |   40 ++++++++------
 .../tracker/rygel-tracker-music-item-factory.vala  |   55 ++++++++++----------
 .../rygel-tracker-picture-item-factory.vala        |   17 +++---
 .../tracker/rygel-tracker-search-container.vala    |   14 ++---
 .../tracker/rygel-tracker-video-item-factory.vala  |   18 ++++---
 6 files changed, 76 insertions(+), 69 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 17a5db2..ef540f1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -229,7 +229,6 @@ AS_IF([test "x$enable_valadoc" != "xno"], [
 
 AM_CONDITIONAL(ENABLE_VALADOC, test x$found_valadoc = xyes)
 
-
 AC_CONFIG_FILES([
 Makefile
 src/Makefile
diff --git a/src/plugins/tracker/rygel-tracker-item-factory.vala b/src/plugins/tracker/rygel-tracker-item-factory.vala
index 97a2f61..630642b 100644
--- a/src/plugins/tracker/rygel-tracker-item-factory.vala
+++ b/src/plugins/tracker/rygel-tracker-item-factory.vala
@@ -27,12 +27,14 @@
 using Gee;
 using GUPnP;
 using Gst;
+using Tracker;
 
 /**
  * Abstract Tracker item factory.
  */
 public abstract class Rygel.Tracker.ItemFactory {
     protected enum Metadata {
+        TRACKER_ID,
         URL,
         PLACE_HOLDER,
         FILE_NAME,
@@ -87,7 +89,7 @@ public abstract class Rygel.Tracker.ItemFactory {
     public abstract MediaItem create (string          id,
                                       string          uri,
                                       SearchContainer parent,
-                                      string[]        metadata)
+                                      Sparql.Cursor   metadata)
                                       throws GLib.Error;
 
     protected void set_ref_id (MediaItem item, string prefix) {
@@ -103,38 +105,42 @@ public abstract class Rygel.Tracker.ItemFactory {
         item.ref_id = prefix + "," + split_id[1];
     }
 
-    protected virtual void set_metadata (MediaItem item,
-                                         string    uri,
-                                         string[]  metadata) throws GLib.Error {
-        if (metadata[Metadata.TITLE] != "")
-            item.title = metadata[Metadata.TITLE];
-        else
+    protected virtual void set_metadata (MediaItem     item,
+                                         string        uri,
+                                         Sparql.Cursor metadata)
+                                         throws GLib.Error {
+        if (metadata.is_bound (Metadata.TITLE)) {
+            item.title = metadata.get_string (Metadata.TITLE);
+        } else {
             /* If title wasn't provided, use filename instead */
-            item.title = metadata[Metadata.FILE_NAME];
+            item.title = metadata.get_string (Metadata.FILE_NAME);
+        }
 
-        if (metadata[Metadata.SIZE] != "")
-            item.size = int64.parse (metadata[Metadata.SIZE]);
-        else
+        if (metadata.is_bound (Metadata.SIZE)) {
+            item.size = metadata.get_integer (Metadata.SIZE);
+        } else {
             // If its in tracker store and size is unknown, it most probably
             // means the size is 0 (i-e a place-holder empty item that we
             // created).
             item.size = 0;
+        }
 
-        item.place_holder = bool.parse (metadata[Metadata.PLACE_HOLDER]);
+        item.place_holder = metadata.get_boolean (Metadata.PLACE_HOLDER);
 
-        if (metadata[Metadata.DATE] != "")
-            item.date = metadata[Metadata.DATE];
+        if (metadata.is_bound (Metadata.DATE)) {
+            item.date = metadata.get_string (Metadata.DATE);
+        }
 
         var profile = null as DLNAProfile;
-        if (metadata[Metadata.DLNA_PROFILE] != "") {
-            item.dlna_profile = metadata[Metadata.DLNA_PROFILE];
+        if (metadata.is_bound (Metadata.DLNA_PROFILE)) {
+            item.dlna_profile = metadata.get_string (Metadata.DLNA_PROFILE);
             profile = this.discoverer.get_profile (item.dlna_profile);
         }
 
         if (profile != null) {
             item.mime_type = profile.mime;
         } else {
-            item.mime_type = metadata[Metadata.MIME];
+            item.mime_type = metadata.get_string (Metadata.MIME);
         }
 
         item.add_uri (uri);
diff --git a/src/plugins/tracker/rygel-tracker-music-item-factory.vala b/src/plugins/tracker/rygel-tracker-music-item-factory.vala
index e2e1fc5..fbd491d 100644
--- a/src/plugins/tracker/rygel-tracker-music-item-factory.vala
+++ b/src/plugins/tracker/rygel-tracker-music-item-factory.vala
@@ -25,6 +25,7 @@
  */
 
 using Gee;
+using Tracker;
 
 /**
  * Tracker music item factory.
@@ -73,7 +74,7 @@ public class Rygel.Tracker.MusicItemFactory : ItemFactory {
     public override MediaItem create (string          id,
                                       string          uri,
                                       SearchContainer parent,
-                                      string[]        metadata)
+                                      Sparql.Cursor   metadata)
                                       throws GLib.Error {
         var item = new MusicItem (id, parent, "");
 
@@ -84,7 +85,7 @@ public class Rygel.Tracker.MusicItemFactory : ItemFactory {
 
     protected override void set_metadata (MediaItem item,
                                           string    uri,
-                                          string[]  metadata)
+                                          Sparql.Cursor metadata)
                                           throws GLib.Error {
         base.set_metadata (item, uri, metadata);
 
@@ -92,47 +93,47 @@ public class Rygel.Tracker.MusicItemFactory : ItemFactory {
 
         var music = item as MusicItem;
 
-        if (metadata[MusicMetadata.DURATION] != "" &&
-            metadata[MusicMetadata.DURATION] != "0") {
-            music.duration = int.parse (metadata[MusicMetadata.DURATION]);
+        if (metadata.is_bound (MusicMetadata.DURATION) &&
+            metadata.get_string (MusicMetadata.DURATION) != "0") {
+            music.duration = (long) metadata.get_integer
+                                        (MusicMetadata.DURATION);
         }
 
-        if (metadata[MusicMetadata.SAMPLE_RATE] != "") {
-            music.sample_freq = int.parse
-                                        (metadata[MusicMetadata.SAMPLE_RATE]);
+        if (metadata.is_bound (MusicMetadata.SAMPLE_RATE)) {
+            music.sample_freq = (int) metadata.get_integer
+                                        (MusicMetadata.SAMPLE_RATE);
         }
 
-        if (metadata[MusicMetadata.CHANNELS] != "") {
-            music.channels = int.parse (metadata[MusicMetadata.CHANNELS]);
+        if (metadata.is_bound (MusicMetadata.CHANNELS)) {
+            music.channels = (int) metadata.get_integer
+                                        (MusicMetadata.CHANNELS);
         }
 
-        if (metadata[MusicMetadata.BITS_PER_SAMPLE] != "") {
-            var bits_per_sample = metadata[MusicMetadata.BITS_PER_SAMPLE];
-            music.bits_per_sample = int.parse (bits_per_sample);
+        if (metadata.is_bound (MusicMetadata.BITS_PER_SAMPLE)) {
+            music.bits_per_sample = (int) metadata.get_integer
+                                        (MusicMetadata.BITS_PER_SAMPLE);
         }
 
-        if (metadata[MusicMetadata.BITRATE] != "") {
-            music.bitrate = int.parse (metadata[MusicMetadata.BITRATE]) / 8;
+        if (metadata.is_bound (MusicMetadata.BITRATE)) {
+            music.bitrate = (int) metadata.get_integer
+                                        (MusicMetadata.BITRATE) / 8;
         }
 
-        if (metadata[MusicMetadata.AUDIO_TRACK_NUM] != "") {
-            var track_number = metadata[MusicMetadata.AUDIO_TRACK_NUM];
-            music.track_number = int.parse (track_number);
+        if (metadata.is_bound (MusicMetadata.AUDIO_TRACK_NUM)) {
+            music.track_number = (int) metadata.get_integer
+                                        (MusicMetadata.AUDIO_TRACK_NUM);
         }
 
-        // FIXME: For the following three properties:
-        // Once converted to libtracker-sparql, check for null again.
-        // DBus translates a (null) to ''
-        if (metadata[MusicMetadata.AUDIO_ARTIST] != "") {
-            music.artist = metadata[MusicMetadata.AUDIO_ARTIST];
+        if (metadata.is_bound (MusicMetadata.AUDIO_ARTIST)) {
+            music.artist = metadata.get_string (MusicMetadata.AUDIO_ARTIST);
         }
 
-        if (metadata[MusicMetadata.AUDIO_ALBUM] != "") {
-            music.album = metadata[MusicMetadata.AUDIO_ALBUM];
+        if (metadata.is_bound (MusicMetadata.AUDIO_ALBUM)) {
+            music.album = metadata.get_string (MusicMetadata.AUDIO_ALBUM);
         }
 
-        if (metadata[MusicMetadata.AUDIO_GENRE] != "") {
-            music.genre = metadata[MusicMetadata.AUDIO_GENRE];
+        if (metadata.is_bound (MusicMetadata.AUDIO_GENRE)) {
+            music.genre = metadata.get_string (MusicMetadata.AUDIO_GENRE);
         }
 
         music.lookup_album_art ();
diff --git a/src/plugins/tracker/rygel-tracker-picture-item-factory.vala b/src/plugins/tracker/rygel-tracker-picture-item-factory.vala
index 60096ce..4cf398e 100644
--- a/src/plugins/tracker/rygel-tracker-picture-item-factory.vala
+++ b/src/plugins/tracker/rygel-tracker-picture-item-factory.vala
@@ -24,6 +24,7 @@
  */
 
 using Gee;
+using Tracker;
 
 /**
  * Tracker picture item factory.
@@ -58,7 +59,7 @@ public class Rygel.Tracker.PictureItemFactory : ItemFactory {
     public override MediaItem create (string          id,
                                       string          uri,
                                       SearchContainer parent,
-                                      string[]        metadata)
+                                      Sparql.Cursor   metadata)
                                       throws GLib.Error {
         var item = new PhotoItem (id, parent, "");
 
@@ -67,9 +68,9 @@ public class Rygel.Tracker.PictureItemFactory : ItemFactory {
         return item;
     }
 
-    protected override void set_metadata (MediaItem item,
-                                          string    uri,
-                                          string[]  metadata)
+    protected override void set_metadata (MediaItem     item,
+                                          string        uri,
+                                          Sparql.Cursor metadata)
                                           throws GLib.Error {
         base.set_metadata (item, uri, metadata);
 
@@ -77,12 +78,12 @@ public class Rygel.Tracker.PictureItemFactory : ItemFactory {
 
         var photo = item as PhotoItem;
 
-        if (metadata[PictureMetadata.WIDTH] != "") {
-            photo.width = int.parse (metadata[PictureMetadata.WIDTH]);
+        if (metadata.is_bound (PictureMetadata.WIDTH)) {
+            photo.width = (int) metadata.get_integer (PictureMetadata.WIDTH);
         }
 
-        if (metadata[PictureMetadata.HEIGHT] != "") {
-            photo.height = int.parse (metadata[PictureMetadata.HEIGHT]);
+        if (metadata.is_bound (PictureMetadata.HEIGHT)) {
+            photo.height = (int) metadata.get_integer (PictureMetadata.HEIGHT);
         }
     }
 }
diff --git a/src/plugins/tracker/rygel-tracker-search-container.vala b/src/plugins/tracker/rygel-tracker-search-container.vala
index c3eaf02..e8642ad 100644
--- a/src/plugins/tracker/rygel-tracker-search-container.vala
+++ b/src/plugins/tracker/rygel-tracker-search-container.vala
@@ -140,16 +140,14 @@ public class Rygel.Tracker.SearchContainer : SimpleContainer {
 
             /* Iterate through all items */
             while (yield query.result.next_async ()) {
-                var id = this.create_child_id_for_urn
-                                        (query.result.get_string (0));
+                var id = query.result.get_string (0);
+                id = this.create_child_id_for_urn (id);
                 var uri = query.result.get_string (1);
 
-                string[] metadata = new string[0];
-                for (int i = 1; i < query.result.n_columns; ++i) {
-                    metadata += query.result.get_string (i);
-                }
-
-                var item = this.item_factory.create (id, uri, this, metadata);
+                var item = this.item_factory.create (id,
+                                                     uri,
+                                                     this,
+                                                     query.result);
                 results.add (item);
             }
         }
diff --git a/src/plugins/tracker/rygel-tracker-video-item-factory.vala b/src/plugins/tracker/rygel-tracker-video-item-factory.vala
index 6ef06b0..e2f79c3 100644
--- a/src/plugins/tracker/rygel-tracker-video-item-factory.vala
+++ b/src/plugins/tracker/rygel-tracker-video-item-factory.vala
@@ -25,6 +25,7 @@
  */
 
 using Gee;
+using Tracker;
 
 /**
  * Tracker video item factory.
@@ -61,7 +62,7 @@ public class Rygel.Tracker.VideoItemFactory : ItemFactory {
     public override MediaItem create (string          id,
                                       string          uri,
                                       SearchContainer parent,
-                                      string[]        metadata)
+                                      Sparql.Cursor   metadata)
                                       throws GLib.Error {
         var item = new VideoItem (id, parent, "");
 
@@ -72,7 +73,7 @@ public class Rygel.Tracker.VideoItemFactory : ItemFactory {
 
     protected override void set_metadata (MediaItem item,
                                           string    uri,
-                                          string[]  metadata)
+                                          Sparql.Cursor  metadata)
                                           throws GLib.Error {
         base.set_metadata (item, uri, metadata);
 
@@ -80,14 +81,15 @@ public class Rygel.Tracker.VideoItemFactory : ItemFactory {
 
         var video = item as VideoItem;
 
-        if (metadata[VideoMetadata.WIDTH] != "")
-            video.width = int.parse (metadata[VideoMetadata.WIDTH]);
+        if (metadata.is_bound (VideoMetadata.WIDTH))
+            video.width = (int) metadata.get_integer (VideoMetadata.WIDTH);
 
-        if (metadata[VideoMetadata.HEIGHT] != "")
-            video.height = int.parse (metadata[VideoMetadata.HEIGHT]);
+        if (metadata.is_bound (VideoMetadata.HEIGHT))
+            video.height = (int) metadata.get_integer (VideoMetadata.HEIGHT);
 
-        if (metadata[VideoMetadata.DURATION] != "")
-            video.duration = int.parse (metadata[VideoMetadata.DURATION]);
+        if (metadata.is_bound (VideoMetadata.DURATION))
+            video.duration = (int) metadata.get_integer
+                                        (VideoMetadata.DURATION);
     }
 }
 



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