[rygel] core,tracker: CreateObject should use URI when provided



commit b274d483a78778075222669f63b955756dc82472
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Fri Oct 15 19:56:43 2010 +0300

    core,tracker: CreateObject should use URI when provided

 .../tracker/rygel-tracker-item-creation.vala       |   20 ++++++---
 src/rygel/rygel-item-creator.vala                  |   35 ++++++++++++++-
 src/rygel/rygel-media-container.vala               |   45 ++++++++++++--------
 3 files changed, 72 insertions(+), 28 deletions(-)
---
diff --git a/src/plugins/tracker/rygel-tracker-item-creation.vala b/src/plugins/tracker/rygel-tracker-item-creation.vala
index 5fd97fe..66d0f46 100644
--- a/src/plugins/tracker/rygel-tracker-item-creation.vala
+++ b/src/plugins/tracker/rygel-tracker-item-creation.vala
@@ -49,15 +49,21 @@ public class Rygel.Tracker.ItemCreation : GLib.Object, Rygel.StateMachine {
 
     public async void run () {
         try {
-            var file = yield this.prepare_file ();
+            string urn;
 
-            var urn = yield this.create_entry_in_store ();
+            if (this.item.uris.size == 0) {
+                var file = yield this.prepare_file ();
 
-            var uris = new string[] { this.item.uris[0] };
-            yield this.miner.ignore_next_update (uris);
-            yield file.create_async (FileCreateFlags.NONE,
-                                     Priority.DEFAULT,
-                                     cancellable);
+                urn = yield this.create_entry_in_store ();
+
+                var uris = new string[] { this.item.uris[0] };
+                yield this.miner.ignore_next_update (uris);
+                yield file.create_async (FileCreateFlags.NONE,
+                                         Priority.DEFAULT,
+                                         cancellable);
+            } else {
+                urn = yield this.create_entry_in_store ();
+            }
 
             this.item.id = this.container.create_child_id_for_urn (urn);
             this.item.parent = this.container;
diff --git a/src/rygel/rygel-item-creator.vala b/src/rygel/rygel-item-creator.vala
index 313f127..7ea5e57 100644
--- a/src/rygel/rygel-item-creator.vala
+++ b/src/rygel/rygel-item-creator.vala
@@ -77,8 +77,9 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
                                           didl_item.upnp_class);
 
             var resources = didl_item.get_resources ();
-            if (resources != null) {
-                var info = resources.nth (0).data.protocol_info;
+            if (resources != null && resources.length () > 0) {
+                var resource = resources.nth (0).data;
+                var info = resource.protocol_info;
 
                 if (info != null) {
                     if (info.dlna_profile != null) {
@@ -89,13 +90,23 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
                         this.item.mime_type = info.mime_type;
                     }
                 }
+
+                if (this.is_valid_uri (resource.uri)) {
+                    this.item.add_uri (resource.uri);
+                }
+
+                if (resource.size >= 0) {
+                    this.item.size = resource.size;
+                }
             }
 
             if (item.mime_type == null) {
                 this.item.mime_type = this.get_generic_mime_type ();
             }
 
-            this.item.size = 0;
+            if (item.size < 0) {
+                this.item.size = 0;
+            }
 
             yield container.add_item (this.item, this.cancellable);
             this.item.serialize (didl_writer, this.content_dir.http_server);
@@ -220,5 +231,23 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
                                          upnp_class);
         }
     }
+
+    // FIXME: This function is hardly completely. Perhaps we should just make
+    // use of a regex here.
+    private bool is_valid_uri (string? uri) {
+        if (uri == null || uri == "") {
+            return false;
+        }
+
+        for (var next = uri.next_char ();
+             next != "";
+             next = next.next_char ()) {
+            if (next.get_char ().isspace ()) {
+                return false;
+            }
+        }
+
+        return true;
+    }
 }
 
diff --git a/src/rygel/rygel-media-container.vala b/src/rygel/rygel-media-container.vala
index 5152ad1..ecb6331 100644
--- a/src/rygel/rygel-media-container.vala
+++ b/src/rygel/rygel-media-container.vala
@@ -204,30 +204,22 @@ public abstract class Rygel.MediaContainer : MediaObject {
      *
      * return nothing.
      *
-     * This implementation is very basic: It only creates the file under the
-     * first writable URI it can find for this container & sets the ID of the
-     * item to that of the URI of the newly created item. If your subclass
-     * doesn't ID the items by their original URIs, you definitely want to
-     * override this method.
+     * This implementation is very basic: If no URI is provided for the item,
+     * it creates the file under the first writable URI it can find for this
+     * container. It also sets the ID of the item to that of the URI of the
+     * item. If your subclass doesn't ID the items by their original URIs, you
+     * definitely want to override this method.
      */
     public async virtual void add_item (MediaItem    item,
                                         Cancellable? cancellable)
                                         throws Error {
-        var dir = yield this.get_writable (cancellable);
-        if (dir == null) {
-           throw new ContentDirectoryError.RESTRICTED_PARENT (
-                                        _("Object creation in %s not allowed"),
-                                        this.id);
+        if (item.uris.size == 0) {
+            var file = yield create_child (item.title, cancellable);
+            item.uris.add (file.get_uri ());
+            item.size = 0;
         }
 
-        var file = dir.get_child_for_display_name (item.title);
-        yield file.create_async (FileCreateFlags.NONE,
-                                 Priority.DEFAULT,
-                                 cancellable);
-        var uri = file.get_uri ();
-        item.id = uri;
-        item.size = 0;
-        item.uris.add (uri);
+        item.id = item.uris[0];
     }
 
     /**
@@ -339,5 +331,22 @@ public abstract class Rygel.MediaContainer : MediaObject {
             this.parent.container_updated (updated_container);
         }
     }
+
+    private async File create_child (string title, Cancellable? cancellable)
+                                     throws Error {
+        var dir = yield this.get_writable (cancellable);
+        if (dir == null) {
+           throw new ContentDirectoryError.RESTRICTED_PARENT (
+                                        _("Object creation in %s not allowed"),
+                                        this.id);
+        }
+
+        var file = dir.get_child_for_display_name (title);
+        yield file.create_async (FileCreateFlags.NONE,
+                                 Priority.DEFAULT,
+                                 cancellable);
+
+        return file;
+    }
 }
 



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