[rygel/wip/create-container: 14/14] wip



commit 264c9a9592022e6bb3122bcb6d05352f6a4a6b50
Author: Jens Georg <jensg openismus com>
Date:   Mon Feb 25 17:24:27 2013 +0100

    wip

 src/librygel-server/rygel-item-creator.vala       |  162 +++++++++++++--------
 src/librygel-server/rygel-item-removal-queue.vala |    6 +-
 2 files changed, 101 insertions(+), 67 deletions(-)
---
diff --git a/src/librygel-server/rygel-item-creator.vala b/src/librygel-server/rygel-item-creator.vala
index dd486e7..99d3cef 100644
--- a/src/librygel-server/rygel-item-creator.vala
+++ b/src/librygel-server/rygel-item-creator.vala
@@ -37,9 +37,9 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
     private string elements;
 
     private DIDLLiteObject didl_object;
-    private DIDLLiteItem didl_item;
+//    private DIDLLiteItem didl_item;
     private MediaObject object;
-    private MediaItem item;
+//    private MediaItem item;
 
     private ContentDirectory content_dir;
     private ServiceAction action;
@@ -78,30 +78,38 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
              * modify the UPnP class to something we support and
              * fetch_container took care of this already.
              */
-            if (!container.can_create (this.didl_item.upnp_class) &&
+            if (!container.can_create (this.didl_object.upnp_class) &&
                 this.container_id != MediaContainer.ANY) {
                 throw new ContentDirectoryError.BAD_METADATA
                                         ("Creating of objects with class %s " +
                                          "is not supported in %s",
-                                         this.didl_item.upnp_class,
+                                         this.didl_object.upnp_class,
                                          container.id);
             }
 
             yield this.create_item_from_didl (container);
-            yield container.add_item (this.item, this.cancellable);
+            if (this.object is MediaItem) {
+                yield container.add_item (this.object as MediaItem,
+                                          this.cancellable);
+            } else {
+                yield container.add_container (this.object as MediaContainer,
+                                               this.cancellable);
+            }
 
             yield this.wait_for_item (container);
 
-            this.item.serialize (serializer, this.content_dir.http_server);
+            this.object.serialize (serializer, this.content_dir.http_server);
 
             // Conclude the successful action
             this.conclude ();
 
             if (this.container_id == MediaContainer.ANY &&
-                this.item.place_holder) {
+                ((this.object is MediaContainer) ||
+                (this.object is MediaItem && (this.object as
+                                              MediaItem).place_holder))) {
                 var queue = ItemRemovalQueue.get_default ();
 
-                queue.queue (this.item, this.cancellable);
+                queue.queue (this.object, this.cancellable);
             }
         } catch (Error err) {
             this.handle_error (err);
@@ -144,9 +152,9 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
             this.didl_object = didl_item;
         });
 
-        this.didl_parser.item_available.connect ((didl_item) => {
+/*        this.didl_parser.item_available.connect ((didl_item) => {
             this.didl_item = didl_item;
-        });
+        }); */
 
         try {
             this.didl_parser.parse_didl (this.elements);
@@ -154,27 +162,28 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
             throw new ContentDirectoryError.BAD_METADATA ("Bad metadata");
         }
 
-        if (this.didl_item == null) {
+        if (this.didl_object == null) {
+            // FIXME: Change to object after string freeze
             var message = _("No items in DIDL-Lite from client: '%s'");
 
             throw new ContentDirectoryError.BAD_METADATA
                                         (message, this.elements);
         }
 
-        if (didl_item.id == null || didl_item.id != "") {
+        if (didl_object.id == null || didl_object.id != "") {
             throw new ContentDirectoryError.BAD_METADATA
                                         ("@id must be set to \"\" in " +
                                          "CreateItem");
         }
 
-        if (didl_item.title == null) {
+        if (didl_object.title == null) {
             throw new ContentDirectoryError.BAD_METADATA
                                     ("dc:title must be set in " +
                                      "CreateItem");
         }
 
         // FIXME: Is this check really necessary? 7.3.118.4 passes without it.
-        if ((didl_item.dlna_managed &
+        if ((didl_object.dlna_managed &
             (OCMFlags.UPLOAD |
              OCMFlags.CREATE_CONTAINER |
              OCMFlags.UPLOAD_DESTROYABLE)) != 0) {
@@ -183,14 +192,16 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
                                          "were found in 'dlnaManaged'");
         }
 
-        if (didl_item.upnp_class == null ||
-            didl_item.upnp_class == "" ||
-            !didl_item.upnp_class.has_prefix ("object")) {
+        warning ("UPnP Class: %s", didl_object.upnp_class);
+
+        if (didl_object.upnp_class == null ||
+            didl_object.upnp_class == "" ||
+            !didl_object.upnp_class.has_prefix ("object")) {
             throw new ContentDirectoryError.BAD_METADATA
                                         ("Invalid upnp:class given ");
         }
 
-        if (didl_item.restricted) {
+        if (didl_object.restricted) {
             throw new ContentDirectoryError.INVALID_ARGS
                                         ("Cannot create restricted item");
         }
@@ -230,7 +241,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
             return null;
         }
 
-        var upnp_class = this.didl_item.upnp_class;
+        var upnp_class = this.didl_object.upnp_class;
 
         var expression = new RelationalExpression ();
         expression.op = SearchCriteriaOp.DERIVED_FROM;
@@ -247,7 +258,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
                                                       root_container.sort_criteria,
                                                       this.cancellable);
             if (result.size > 0) {
-                this.didl_item.upnp_class = upnp_class;
+                this.didl_object.upnp_class = upnp_class;
 
                 return result[0];
             } else {
@@ -256,9 +267,10 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         }
 
         if (upnp_class == "object.item") {
+            // FIXME: Mark translatable.
             throw new ContentDirectoryError.BAD_METADATA
                                     ("'%s' UPnP class unsupported",
-                                     this.didl_item.upnp_class);
+                                     this.didl_object.upnp_class);
         }
 
         return null;
@@ -304,7 +316,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         string didl = this.serializer.get_string ();
 
         /* Set action return arguments */
-        this.action.set ("ObjectID", typeof (string), this.item.id,
+        this.action.set ("ObjectID", typeof (string), this.object.id,
                          "Result", typeof (string), didl);
 
         this.action.return ();
@@ -326,9 +338,15 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
     }
 
     private string get_generic_mime_type () {
-        if (this.item is ImageItem) {
+        if (!(this.object is MediaItem)) {
+            return "";
+        }
+
+        var item = this.object as MediaItem;
+
+        if (item is ImageItem) {
             return "image";
-        } else if (this.item is VideoItem) {
+        } else if (item is VideoItem) {
             return "video";
         } else {
             return "audio";
@@ -347,97 +365,113 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
      */
     private async void create_item_from_didl (WritableContainer container)
                                                    throws Error {
-        this.object = this.create_item (this.didl_item.id,
-                                      container,
-                                      this.didl_item.title,
-                                      this.didl_item.upnp_class);
+        this.object = this.create_item (this.didl_object.id,
+                                        container,
+                                        this.didl_object.title,
+                                        this.didl_object.upnp_class);
+
+        var didl_item = this.didl_object as DIDLLiteItem;
 
-        var resources = this.didl_item.get_resources ();
+        var resources = this.didl_object.get_resources ();
         if (resources != null && resources.length () > 0) {
             var resource = resources.nth (0).data;
             var info = resource.protocol_info;
 
-            if (info != null) {
+            if (info != null && didl_item != null) {
                 if (info.dlna_profile != null) {
                     if (!this.is_profile_valid (info.dlna_profile)) {
+                        // FIXME: Missing translation
                         throw new ContentDirectoryError.BAD_METADATA
                                     ("'%s' DLNA profile unsupported",
                                      info.dlna_profile);
                     }
 
-                    this.item.dlna_profile = info.dlna_profile;
+                    (this.object as MediaItem).dlna_profile = info.dlna_profile;
                 }
 
                 if (info.mime_type != null) {
-                    this.item.mime_type = info.mime_type;
+                    (this.object as MediaItem).mime_type = info.mime_type;
                 }
             }
 
-            string sanitized_uri;
-            if (this.is_valid_uri (resource.uri, out sanitized_uri)) {
-                this.item.add_uri (sanitized_uri);
+            string sanitized_uri = null;
+            if ((this.object is MediaItem) && this.is_valid_uri (resource.uri, out sanitized_uri)) {
+                (this.object as MediaItem).add_uri (sanitized_uri);
             }
 
-            if (resource.size >= 0) {
-                this.item.size = resource.size;
+            if (resource.size >= 0 && this.object is MediaItem) {
+                (this.object as MediaItem).size = resource.size;
             }
         }
 
-        if (this.item.mime_type == null) {
-            this.item.mime_type = this.get_generic_mime_type ();
-        }
+        var item = this.object as MediaItem;
+        if (this.object is MediaItem) {
+            if (item.mime_type == null) {
+                item.mime_type = this.get_generic_mime_type ();
+            }
 
-        if (this.item.size < 0) {
-            this.item.size = 0;
+            if (item.size < 0) {
+                item.size = 0;
+            }
         }
 
-        if (this.item.uris.size == 0) {
-            var uri = yield this.create_uri (container, this.item.title);
-            this.item.uris.add (uri);
-            this.item.place_holder = true;
-        } else {
-            var file = File.new_for_uri (this.item.uris[0]);
-            this.item.place_holder = !file.is_native ();
-        }
+            if (this.object.uris.size == 0) {
+                var uri = yield this.create_uri (container, this.object.title);
+                this.object.uris.add (uri);
+                if (this.object is MediaItem) {
+                    item.place_holder = true;
+                }
+            } else {
+                var file = File.new_for_uri (item.uris[0]);
+                if (this.object is MediaItem) {
+                    item.place_holder = !file.is_native ();
+                }
+            }
 
-        this.item.id = this.item.uris[0];
+        this.object.id = this.object.uris[0];
 
         this.parse_and_verify_didl_date ();
     }
 
     private void parse_and_verify_didl_date () throws Error {
-        if (this.didl_item.date == null) {
+        if (!(this.didl_object is DIDLLiteItem)) {
+            return;
+        }
+
+        var didl_item = this.didl_object as DIDLLiteItem;
+        if (didl_item.date == null) {
             return;
         }
 
-        var parsed_date = new Soup.Date.from_string (this.didl_item.date);
+        var parsed_date = new Soup.Date.from_string (didl_item.date);
         if (parsed_date != null) {
-            this.item.date = parsed_date.to_string (Soup.DateFormat.ISO8601);
+            (this.object as MediaItem).date = parsed_date.to_string (Soup.DateFormat.ISO8601);
 
             return;
         }
 
         int year = 0, month = 0, day = 0;
 
-        if (this.didl_item.date.scanf ("%4d-%02d-%02d",
+        if (didl_item.date.scanf ("%4d-%02d-%02d",
                                        out year,
                                        out month,
                                        out day) != 3) {
             throw new ContentDirectoryError.BAD_METADATA
                                     ("Invalid date format: %s",
-                                     this.didl_item.date);
+                                     didl_item.date);
         }
 
         var date = GLib.Date ();
         date.set_dmy ((DateDay) day, (DateMonth) month, (DateYear) year);
 
         if (!date.valid ()) {
+            // FIXME: Add translation.
             throw new ContentDirectoryError.BAD_METADATA
                                     ("Invalid date: %s",
-                                     this.didl_item.date);
+                                     didl_item.date);
         }
 
-        this.item.date = this.didl_item.date + "T00:00:00";
+        (this.object as MediaItem).date = didl_item.date + "T00:00:00";
     }
 
     private MediaObject create_item (string            id,
@@ -559,21 +593,21 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         debug ("Waiting for new item to appear under container '%s'..",
                container.id);
 
-        MediaItem item = null;
+        MediaObject object = null;
 
-        while (item == null) {
+        while (object == null) {
             try {
-                item = (yield container.find_object (this.item.id,
-                                                     this.cancellable))
+                object = (yield container.find_object (this.object.id,
+                                                       this.cancellable))
                        as MediaItem;
             } catch (Error error) {
                 warning ("Error from container '%s' on trying to find newly " +
                          "added child item '%s' in it",
                          container.id,
-                         this.item.id);
+                         this.object.id);
             }
 
-            if (item == null) {
+            if (object == null) {
                 var id = container.container_updated.connect ((container) => {
                     this.wait_for_item.callback ();
                 });
diff --git a/src/librygel-server/rygel-item-removal-queue.vala 
b/src/librygel-server/rygel-item-removal-queue.vala
index 282b1a1..5ba3c4e 100644
--- a/src/librygel-server/rygel-item-removal-queue.vala
+++ b/src/librygel-server/rygel-item-removal-queue.vala
@@ -40,7 +40,7 @@ internal class Rygel.ItemRemovalQueue: GLib.Object {
         return removal_queue;
     }
 
-    public void queue (MediaItem item, Cancellable? cancellable) {
+    public void queue (MediaObject item, Cancellable? cancellable) {
         if (item.parent_ref == null) {
             item.parent_ref = item.parent;
         }
@@ -55,7 +55,7 @@ internal class Rygel.ItemRemovalQueue: GLib.Object {
         item_timeouts.set (item.id, timeout);
     }
 
-    public bool dequeue (MediaItem item) {
+    public bool dequeue (MediaObject item) {
         uint timeout;
 
         if (item_timeouts.unset (item.id, out timeout)) {
@@ -67,7 +67,7 @@ internal class Rygel.ItemRemovalQueue: GLib.Object {
         }
     }
 
-    public async void remove_now (MediaItem item, Cancellable? cancellable) {
+    public async void remove_now (MediaObject item, Cancellable? cancellable) {
         item_timeouts.unset (item.id);
 
         var parent = item.parent as WritableContainer;


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