rygel r277 - in trunk: . src/media-providers/tracker



Author: zeeshanak
Date: Mon Nov 10 21:33:21 2008
New Revision: 277
URL: http://svn.gnome.org/viewvc/rygel?rev=277&view=rev

Log:
Refactor: A more intelligent TrackerContainer.

As a result, MediaTracker class is now very slim.

Modified:
   trunk/ChangeLog
   trunk/src/media-providers/tracker/rygel-media-tracker.vala
   trunk/src/media-providers/tracker/rygel-tracker-container.vala

Modified: trunk/src/media-providers/tracker/rygel-media-tracker.vala
==============================================================================
--- trunk/src/media-providers/tracker/rygel-media-tracker.vala	(original)
+++ trunk/src/media-providers/tracker/rygel-media-tracker.vala	Mon Nov 10 21:33:21 2008
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 Zeeshan Ali <zeenix gmail com>.
+ * Copyright (C) 2008 Nokia Corporation, all rights reserved.
  *
  * Author: Zeeshan Ali <zeenix gmail com>
  *
@@ -26,14 +27,6 @@
 using DBus;
 
 public class Rygel.MediaTracker : MediaProvider {
-    /* class-wide constants */
-    public static const string TRACKER_SERVICE = "org.freedesktop.Tracker";
-    public static const string TRACKER_PATH = "/org/freedesktop/tracker";
-    public static const string TRACKER_IFACE = "org.freedesktop.Tracker";
-    public static const string FILES_IFACE = "org.freedesktop.Tracker.Files";
-    public static const string METADATA_IFACE =
-                                            "org.freedesktop.Tracker.Metadata";
-
     public static const int MAX_REQUESTED_COUNT = 128;
 
     private MediaContainer root_container;
@@ -41,10 +34,6 @@
     /* FIXME: Make this a static if you know how to initize it */
     private List<TrackerContainer> containers;
 
-    private dynamic DBus.Object metadata;
-    private dynamic DBus.Object files;
-    private dynamic DBus.Object tracker;
-
     private SearchCriteriaParser search_parser;
 
     construct {
@@ -56,37 +45,31 @@
         this.containers = new List<TrackerContainer> ();
         this.containers.append
                         (new TrackerContainer (this.root_id + ":" + "16",
-                                                this.root_id,
-                                                "All Images",
-                                                "Images",
-                                                MediaItem.IMAGE_CLASS));
+                                               this.root_id,
+                                               this.root_id,
+                                               "All Images",
+                                               "Images",
+                                               MediaItem.IMAGE_CLASS,
+                                               context));
         this.containers.append
                         (new TrackerContainer (this.root_id + ":" + "14",
-                                                this.root_id,
-                                                "All Music",
-                                                "Music",
-                                                MediaItem.MUSIC_CLASS));
+                                               this.root_id,
+                                               this.root_id,
+                                               "All Music",
+                                               "Music",
+                                               MediaItem.MUSIC_CLASS,
+                                               context));
         this.containers.append
                         (new TrackerContainer (this.root_id + ":" + "15",
-                                                this.root_id,
-                                                "All Videos",
-                                                "Videos",
-                                                MediaItem.VIDEO_CLASS));
+                                               this.root_id,
+                                               this.root_id,
+                                               "All Videos",
+                                               "Videos",
+                                               MediaItem.VIDEO_CLASS,
+                                               context));
 
         this.search_parser = new SearchCriteriaParser ();
 
-        DBus.Connection connection = DBus.Bus.get (DBus.BusType.SESSION);
-
-        this.metadata = connection.get_object (MediaTracker.TRACKER_SERVICE,
-                                               MediaTracker.TRACKER_PATH,
-                                               MediaTracker.METADATA_IFACE);
-        this.files = connection.get_object (MediaTracker.TRACKER_SERVICE,
-                                            MediaTracker.TRACKER_PATH,
-                                            MediaTracker.FILES_IFACE);
-        this.tracker = connection.get_object (MediaTracker.TRACKER_SERVICE,
-                                              MediaTracker.TRACKER_PATH,
-                                              MediaTracker.TRACKER_IFACE);
-
         weak string home_dir = Environment.get_home_dir ();
 
         /* Host the home dir of the user */
@@ -127,11 +110,10 @@
                 number_returned = 0;
             else {
                 number_returned =
-                    this.add_container_children_from_db (didl_writer,
-                                                         container,
-                                                         starting_index,
-                                                         requested_count,
-                                                         out total_matches);
+                    container.add_children_from_db (didl_writer,
+                                                    starting_index,
+                                                    requested_count,
+                                                    out total_matches);
             }
         }
 
@@ -161,7 +143,7 @@
             container = find_container_by_id (object_id);
 
             if (container != null) {
-                add_container_from_db (didl_writer, container);
+                container.serialize (didl_writer);
 
                 found = true;
             } else {
@@ -171,9 +153,7 @@
                 container = get_item_parent (id);
 
                 if (container != null)
-                    found = add_item_from_db (didl_writer,
-                                              container,
-                                              id);
+                    found = container.add_item_from_db (didl_writer, id);
             }
         }
 
@@ -187,39 +167,11 @@
     /* Private methods */
     private uint add_root_container_children (DIDLLiteWriter didl_writer) {
         foreach (TrackerContainer container in this.containers)
-            this.add_container_from_db (didl_writer, container);
+            container.serialize (didl_writer);
 
         return this.containers.length ();
     }
 
-    private void add_container_from_db (DIDLLiteWriter    didl_writer,
-                                        TrackerContainer container) {
-        /* Update the child count */
-        container.child_count = get_container_children_count (container);
-
-        container.serialize (didl_writer);
-    }
-
-    private uint get_container_children_count (TrackerContainer container) {
-        string[][] stats;
-
-        try {
-                stats = this.tracker.GetStats ();
-        } catch (GLib.Error error) {
-            critical ("error getting tracker statistics: %s", error.message);
-
-            return 0;
-        }
-
-        uint count = 0;
-        for (uint i = 0; i < stats.length; i++) {
-            if (stats[i][0] == container.tracker_category)
-                count = stats[i][1].to_int ();
-        }
-
-        return count;
-    }
-
     private TrackerContainer? find_container_by_id (string container_id) {
         TrackerContainer container;
 
@@ -235,242 +187,12 @@
         return container;
     }
 
-    private uint add_container_children_from_db
-                    (DIDLLiteWriter    didl_writer,
-                     TrackerContainer container,
-                     uint              offset,
-                     uint              max_count,
-                     out uint          child_count) {
-        string[] children;
-
-        children = this.get_container_children_from_db (container,
-                                                        offset,
-                                                        max_count,
-                                                        out child_count);
-        if (children == null)
-            return 0;
-
-        /* Iterate through all items */
-        for (uint i = 0; i < children.length; i++)
-            this.add_item_from_db (didl_writer,
-                                   container,
-                                   children[i]);
-
-        return children.length;
-    }
-
-    private string[]? get_container_children_from_db
-                            (TrackerContainer container,
-                             uint              offset,
-                             uint              max_count,
-                             out uint          child_count) {
-        string[] children = null;
-
-        child_count = get_container_children_count (container);
-
-        try {
-            children = this.files.GetByServiceType (0,
-                                                    container.tracker_category,
-                                                    (int) offset,
-                                                    (int) max_count);
-        } catch (GLib.Error error) {
-            critical ("error: %s", error.message);
-
-            return null;
-        }
-
-        return children;
-    }
-
-    private bool add_item_from_db (DIDLLiteWriter    didl_writer,
-                                   TrackerContainer parent,
-                                   string            path) {
-        if (parent.child_class == MediaItem.VIDEO_CLASS) {
-            return this.add_video_item_from_db (didl_writer, parent, path);
-        } else if (parent.child_class == MediaItem.IMAGE_CLASS) {
-            return this.add_image_item_from_db (didl_writer, parent, path);
-        } else {
-            return this.add_music_item_from_db (didl_writer, parent, path);
-        }
-    }
-
-    private bool add_video_item_from_db (DIDLLiteWriter    didl_writer,
-                                         TrackerContainer parent,
-                                         string            path) {
-        string[] keys = new string[] {"File:Name",
-                                      "File:Mime",
-                                      "Video:Title",
-                                      "Video:Author",
-                                      "Video:Width",
-                                      "Video:Height",
-                                      "DC:Date"};
-
-        string[] values = null;
-
-        /* TODO: make this async */
-        try {
-            values = this.metadata.Get (parent.tracker_category, path, keys);
-        } catch (GLib.Error error) {
-            critical ("failed to get metadata for %s: %s\n",
-                      path,
-                      error.message);
-
-            return false;
-        }
-
-        string title;
-        if (values[2] != "")
-            title = values[2];
-        else
-            /* If title wasn't provided, use filename instead */
-            title = values[0];
-
-        MediaItem item = new MediaItem (this.root_id + ":" + path,
-                                        parent.id,
-                                        title,
-                                        parent.child_class);
-
-        if (values[4] != "")
-            item.width = values[4].to_int ();
-
-        if (values[5] != "")
-            item.height = values[5].to_int ();
-
-        item.date = seconds_to_iso8601 (values[6]);
-        item.mime = values[1];
-        item.author = values[3];
-        item.uri = uri_from_path (path);
-
-        item.serialize (didl_writer);
-
-        return true;
-    }
-
-    private bool add_image_item_from_db (DIDLLiteWriter    didl_writer,
-                                         TrackerContainer parent,
-                                         string            path) {
-        string[] keys = new string[] {"File:Name",
-                                      "File:Mime",
-                                      "Image:Title",
-                                      "Image:Creator",
-                                      "Image:Width",
-                                      "Image:Height",
-                                      "Image:Album",
-                                      "Image:Date",
-                                      "DC:Date"};
-
-        string[] values = null;
-
-        /* TODO: make this async */
-        try {
-            values = this.metadata.Get (parent.tracker_category, path, keys);
-        } catch (GLib.Error error) {
-            critical ("failed to get metadata for %s: %s\n",
-                      path,
-                      error.message);
-
-            return false;
-        }
-
-        string title;
-        if (values[2] != "")
-            title = values[2];
-        else
-            /* If title wasn't provided, use filename instead */
-            title = values[0];
-
-        MediaItem item = new MediaItem (this.root_id + ":" + path,
-                                        parent.id,
-                                        title,
-                                        parent.child_class);
-
-        if (values[4] != "")
-            item.width = values[4].to_int ();
-
-        if (values[5] != "")
-            item.height = values[5].to_int ();
-
-        if (values[8] != "") {
-            item.date = seconds_to_iso8601 (values[8]);
-        } else {
-            item.date = seconds_to_iso8601 (values[7]);
-        }
-
-        item.mime = values[1];
-        item.author = values[3];
-        item.album = values[6];
-        item.uri = uri_from_path (path);
-
-        item.serialize (didl_writer);
-
-        return true;
-    }
-
-    private bool add_music_item_from_db (DIDLLiteWriter   didl_writer,
-                                        TrackerContainer parent,
-                                         string           path) {
-        string[] keys = new string[] {"File:Name",
-                                      "File:Mime",
-                                      "Audio:Title",
-                                      "Audio:Artist",
-                                      "Audio:TrackNo",
-                                      "Audio:Album",
-                                      "Audio:ReleaseDate",
-                                      "Audio:DateAdded",
-                                      "DC:Date"};
-
-        string[] values = null;
-
-        /* TODO: make this async */
-        try {
-            values = this.metadata.Get (parent.tracker_category, path, keys);
-        } catch (GLib.Error error) {
-            critical ("failed to get metadata for %s: %s\n",
-                      path,
-                      error.message);
-
-            return false;
-        }
-
-        string title;
-        if (values[2] != "")
-            title = values[2];
-        else
-            /* If title wasn't provided, use filename instead */
-            title = values[0];
-
-        MediaItem item = new MediaItem (this.root_id + ":" + path,
-                                        parent.id,
-                                        title,
-                                        parent.child_class);
-
-        if (values[4] != "")
-            item.track_number = values[4].to_int ();
-
-        if (values[8] != "") {
-            item.date = seconds_to_iso8601 (values[8]);
-        } else if (values[6] != "") {
-            item.date = seconds_to_iso8601 (values[6]);
-        } else {
-            item.date = seconds_to_iso8601 (values[7]);
-        }
-
-        item.mime = values[1];
-        item.author = values[3];
-        item.album = values[5];
-        item.uri = uri_from_path (path);
-
-        item.serialize (didl_writer);
-
-        return true;
-    }
-
     private TrackerContainer? get_item_parent (string uri) {
         TrackerContainer container = null;
         string category;
 
         try {
-            category = this.files.GetServiceType (uri);
+            category = TrackerContainer.files.GetServiceType (uri);
         } catch (GLib.Error error) {
             critical ("failed to find service type for %s: %s",
                       uri,
@@ -490,24 +212,6 @@
         return container;
     }
 
-    string seconds_to_iso8601 (string seconds) {
-
-        string date;
-
-        if (seconds != "") {
-            TimeVal tv;
-
-            tv.tv_sec = seconds.to_int ();
-            tv.tv_usec = 0;
-
-            date = tv.to_iso8601 ();
-        } else {
-            date = "";
-        }
-
-        return date;
-    }
-
     string remove_root_id_prefix (string id) {
         string[] tokens;
 
@@ -518,13 +222,5 @@
         else
             return tokens[0];
     }
-
-    private string uri_from_path (string path) {
-        string escaped_path = Uri.escape_string (path, "/", true);
-
-        return "http://%s:%u%s".printf (context.host_ip,
-                                        context.port,
-                                        escaped_path);
-    }
 }
 

Modified: trunk/src/media-providers/tracker/rygel-tracker-container.vala
==============================================================================
--- trunk/src/media-providers/tracker/rygel-tracker-container.vala	(original)
+++ trunk/src/media-providers/tracker/rygel-tracker-container.vala	Mon Nov 10 21:33:21 2008
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 Zeeshan Ali <zeenix gmail com>.
+ * Copyright (C) 2008 Nokia Corporation, all rights reserved.
  *
  * Author: Zeeshan Ali <zeenix gmail com>
  *
@@ -22,23 +23,348 @@
  * version 2 of the License, or (at your option) any later version.
  */
 
+using Rygel;
 using GUPnP;
+using DBus;
 
 public class Rygel.TrackerContainer : MediaContainer {
+    /* class-wide constants */
+    public static const string TRACKER_SERVICE = "org.freedesktop.Tracker";
+    public static const string TRACKER_PATH = "/org/freedesktop/tracker";
+    public static const string TRACKER_IFACE = "org.freedesktop.Tracker";
+    public static const string FILES_IFACE = "org.freedesktop.Tracker.Files";
+    public static const string METADATA_IFACE =
+                                            "org.freedesktop.Tracker.Metadata";
+
+    public static dynamic DBus.Object metadata;
+    public static dynamic DBus.Object files;
+    public static dynamic DBus.Object tracker;
+
+    private Context context;
     public string tracker_category;
 
     /* UPnP class of items under this container */
     public string child_class;
 
-    public TrackerContainer (string id,
-                             string parent_id,
-                             string title,
-                             string tracker_category,
-                             string child_class) {
+    // FIXME: We should not need this
+    private string root_id;
+
+    // Class constructor
+    static construct {
+        DBus.Connection connection;
+        try {
+            connection = DBus.Bus.get (DBus.BusType.SESSION);
+        } catch (DBus.Error error) {
+            critical ("Failed to connect to Session bus: %s\n",
+                      error.message);
+        }
+
+        TrackerContainer.metadata =
+                    connection.get_object (TrackerContainer.TRACKER_SERVICE,
+                                           TrackerContainer.TRACKER_PATH,
+                                           TrackerContainer.METADATA_IFACE);
+        TrackerContainer.files =
+                    connection.get_object (TrackerContainer.TRACKER_SERVICE,
+                                           TrackerContainer.TRACKER_PATH,
+                                           TrackerContainer.FILES_IFACE);
+        TrackerContainer.tracker =
+                    connection.get_object (TrackerContainer.TRACKER_SERVICE,
+                                           TrackerContainer.TRACKER_PATH,
+                                           TrackerContainer.TRACKER_IFACE);
+    }
+
+    public TrackerContainer (string  id,
+                             string  parent_id,
+                             string  root_id,
+                             string  title,
+                             string  tracker_category,
+                             string  child_class,
+                             Context context) {
         this.id = id;
         this.parent_id = parent_id;
+        this.root_id = root_id;
         this.title = title;
         this.tracker_category = tracker_category;
         this.child_class = child_class;
+        this.context = context;
+    }
+
+    public override void serialize (DIDLLiteWriter didl_writer) {
+        /* Update the child count */
+        this.child_count = this.get_children_count ();
+
+        base.serialize (didl_writer);
+    }
+
+    private uint get_children_count () {
+        string[][] stats;
+
+        try {
+                stats = TrackerContainer.tracker.GetStats ();
+        } catch (GLib.Error error) {
+            critical ("error getting tracker statistics: %s", error.message);
+
+            return 0;
+        }
+
+        uint count = 0;
+        for (uint i = 0; i < stats.length; i++) {
+            if (stats[i][0] == this.tracker_category)
+                count = stats[i][1].to_int ();
+        }
+
+        return count;
+    }
+
+    public uint add_children_from_db (DIDLLiteWriter didl_writer,
+                                       uint           offset,
+                                       uint           max_count,
+                                       out uint       child_count) {
+        string[] children;
+
+        children = this.get_children_from_db (offset,
+                                              max_count,
+                                              out child_count);
+        if (children == null)
+            return 0;
+
+        /* Iterate through all items */
+        for (uint i = 0; i < children.length; i++)
+            this.add_item_from_db (didl_writer, children[i]);
+
+        return children.length;
+    }
+
+    private string[]? get_children_from_db (uint     offset,
+                                            uint     max_count,
+                                            out uint child_count) {
+        string[] children = null;
+
+        child_count = this.get_children_count ();
+
+        try {
+            children =
+                TrackerContainer.files.GetByServiceType (0,
+                                                         this.tracker_category,
+                                                         (int) offset,
+                                                         (int) max_count);
+        } catch (GLib.Error error) {
+            critical ("error: %s", error.message);
+
+            return null;
+        }
+
+        return children;
+    }
+
+    public bool add_item_from_db (DIDLLiteWriter didl_writer,
+                                   string         path) {
+        if (this.child_class == MediaItem.VIDEO_CLASS) {
+            return this.add_video_item_from_db (didl_writer, path);
+        } else if (this.child_class == MediaItem.IMAGE_CLASS) {
+            return this.add_image_item_from_db (didl_writer, path);
+        } else {
+            return this.add_music_item_from_db (didl_writer, path);
+        }
+    }
+
+    private bool add_video_item_from_db (DIDLLiteWriter didl_writer,
+                                         string         path) {
+        string[] keys = new string[] {"File:Name",
+                                      "File:Mime",
+                                      "Video:Title",
+                                      "Video:Author",
+                                      "Video:Width",
+                                      "Video:Height",
+                                      "DC:Date"};
+
+        string[] values = null;
+
+        /* TODO: make this async */
+        try {
+            values = TrackerContainer.metadata.Get (this.tracker_category,
+                                                    path,
+                                                    keys);
+        } catch (GLib.Error error) {
+            critical ("failed to get metadata for %s: %s\n",
+                      path,
+                      error.message);
+
+            return false;
+        }
+
+        string title;
+        if (values[2] != "")
+            title = values[2];
+        else
+            /* If title wasn't provided, use filename instead */
+            title = values[0];
+
+        MediaItem item = new MediaItem (this.root_id + ":" + path,
+                                        this.id,
+                                        title,
+                                        this.child_class);
+
+        if (values[4] != "")
+            item.width = values[4].to_int ();
+
+        if (values[5] != "")
+            item.height = values[5].to_int ();
+
+        item.date = seconds_to_iso8601 (values[6]);
+        item.mime = values[1];
+        item.author = values[3];
+        item.uri = uri_from_path (path);
+
+        item.serialize (didl_writer);
+
+        return true;
+    }
+
+    private bool add_image_item_from_db (DIDLLiteWriter didl_writer,
+                                         string         path) {
+        string[] keys = new string[] {"File:Name",
+                                      "File:Mime",
+                                      "Image:Title",
+                                      "Image:Creator",
+                                      "Image:Width",
+                                      "Image:Height",
+                                      "Image:Album",
+                                      "Image:Date",
+                                      "DC:Date"};
+
+        string[] values = null;
+
+        /* TODO: make this async */
+        try {
+            values = TrackerContainer.metadata.Get (this.tracker_category,
+                                                    path,
+                                                    keys);
+        } catch (GLib.Error error) {
+            critical ("failed to get metadata for %s: %s\n",
+                      path,
+                      error.message);
+
+            return false;
+        }
+
+        string title;
+        if (values[2] != "")
+            title = values[2];
+        else
+            /* If title wasn't provided, use filename instead */
+            title = values[0];
+
+        MediaItem item = new MediaItem (this.root_id + ":" + path,
+                                        this.id,
+                                        title,
+                                        this.child_class);
+
+        if (values[4] != "")
+            item.width = values[4].to_int ();
+
+        if (values[5] != "")
+            item.height = values[5].to_int ();
+
+        if (values[8] != "") {
+            item.date = seconds_to_iso8601 (values[8]);
+        } else {
+            item.date = seconds_to_iso8601 (values[7]);
+        }
+
+        item.mime = values[1];
+        item.author = values[3];
+        item.album = values[6];
+        item.uri = uri_from_path (path);
+
+        item.serialize (didl_writer);
+
+        return true;
+    }
+
+    private bool add_music_item_from_db (DIDLLiteWriter didl_writer,
+                                         string         path) {
+        string[] keys = new string[] {"File:Name",
+                                      "File:Mime",
+                                      "Audio:Title",
+                                      "Audio:Artist",
+                                      "Audio:TrackNo",
+                                      "Audio:Album",
+                                      "Audio:ReleaseDate",
+                                      "Audio:DateAdded",
+                                      "DC:Date"};
+
+        string[] values = null;
+
+        /* TODO: make this async */
+        try {
+            values = TrackerContainer.metadata.Get (this.tracker_category,
+                                                    path,
+                                                    keys);
+        } catch (GLib.Error error) {
+            critical ("failed to get metadata for %s: %s\n",
+                      path,
+                      error.message);
+
+            return false;
+        }
+
+        string title;
+        if (values[2] != "")
+            title = values[2];
+        else
+            /* If title wasn't provided, use filename instead */
+            title = values[0];
+
+        MediaItem item = new MediaItem (this.root_id + ":" + path,
+                                        this.id,
+                                        title,
+                                        this.child_class);
+
+        if (values[4] != "")
+            item.track_number = values[4].to_int ();
+
+        if (values[8] != "") {
+            item.date = seconds_to_iso8601 (values[8]);
+        } else if (values[6] != "") {
+            item.date = seconds_to_iso8601 (values[6]);
+        } else {
+            item.date = seconds_to_iso8601 (values[7]);
+        }
+
+        item.mime = values[1];
+        item.author = values[3];
+        item.album = values[5];
+        item.uri = uri_from_path (path);
+
+        item.serialize (didl_writer);
+
+        return true;
+    }
+
+    static string seconds_to_iso8601 (string seconds) {
+        string date;
+
+        if (seconds != "") {
+            TimeVal tv;
+
+            tv.tv_sec = seconds.to_int ();
+            tv.tv_usec = 0;
+
+            date = tv.to_iso8601 ();
+        } else {
+            date = "";
+        }
+
+        return date;
+    }
+
+    private string uri_from_path (string path) {
+        string escaped_path = Uri.escape_string (path, "/", true);
+
+        return "http://%s:%u%s".printf (this.context.host_ip,
+                                        this.context.port,
+                                        escaped_path);
     }
 }
+



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