[rygel/wip/update-object: 12/15] wip: Finish ItemUpdater.



commit 90de19deaead29b66ddcbf6459a3f610494696dd
Author: Krzesimir Nowak <krnowak openismus com>
Date:   Mon Oct 22 15:09:46 2012 +0200

    wip: Finish ItemUpdater.

 src/librygel-server/rygel-item-updater.vala |   60 ++++++++++++++++-----------
 src/librygel-server/rygel-media-item.vala   |    6 +++
 src/librygel-server/rygel-media-object.vala |   27 ++++++++++++
 src/librygel-server/rygel-music-item.vala   |   17 ++++++++
 src/librygel-server/rygel-photo-item.vala   |   12 +++++
 src/librygel-server/rygel-video-item.vala   |   12 +++++
 6 files changed, 110 insertions(+), 24 deletions(-)
---
diff --git a/src/librygel-server/rygel-item-updater.vala b/src/librygel-server/rygel-item-updater.vala
index 37098a9..f635162 100644
--- a/src/librygel-server/rygel-item-updater.vala
+++ b/src/librygel-server/rygel-item-updater.vala
@@ -57,20 +57,12 @@ internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
                 throw new ContentDirectoryError.NO_SUCH_OBJECT
                                         (_("No such object"));
             }
-            // I have no idea what to throw here.
-            // For now I just treat it as empty strings.
-            if (this.current_tag_value == null) {
-                this.current_tag_value = "";
-            }
-            if (this.new_tag_value == null) {
-                this.new_tag_value = "";
-            }
 
             yield this.update_object ();
 
             this.action.return ();
 
-            debug (_("Successfully destroyed object '%s'"), this.object_id);
+            debug (_("Successfully updated object '%s'"), this.object_id);
         } catch (Error error) {
             if (error is ContentDirectoryError) {
                 this.action.return_error (error.code, error.message);
@@ -86,16 +78,16 @@ internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
         this.completed ();
     }
 
-    private static LinkedList<string> csv_split (string tag_values) {
+    private static LinkedList<string> csv_split (string? tag_values) {
         var list = new LinkedList<string> ();
-        /*
         var escape = false;
         var token_start = 0;
         var token_length = 0;
-        */
+        var len = (tag_values != null ? tag_values.length : 0);
+
+        for (int iter = 0; iter < len; ++iter) {
+            var c = tag_values[iter];
 
-        /* TODO: Find out how to iterate over chars in string.
-        foreach (var c in tag_values) {
             if (escape) {
                 escape = false;
             } else {
@@ -113,7 +105,6 @@ internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
             }
             ++token_length;
         }
-        */
 
         return list;
     }
@@ -123,16 +114,37 @@ internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
         var current_list = csv_split (this.current_tag_value);
         var new_list = csv_split (this.new_tag_value);
 
-        if (current_list.size != new_list.size) {
+
+        switch (media_object.apply_fragments (current_list,
+                                              new_list,
+                                              this.content_dir.http_server)) {
+        case DIDLLiteFragmentResult.OK:
+            break;
+
+        case DIDLLiteFragmentResult.CURRENT_BAD_XML:
+        case DIDLLiteFragmentResult.CURRENT_INVALID:
+            throw new ContentDirectoryError.INVALID_CURRENT_TAG_VALUE
+                                        ("Bad current tag value.");
+
+        case DIDLLiteFragmentResult.NEW_BAD_XML:
+        case DIDLLiteFragmentResult.NEW_INVALID:
+            throw new ContentDirectoryError.INVALID_NEW_TAG_VALUE
+                                        ("Bad current tag value.");
+
+        case DIDLLiteFragmentResult.REQUIRED_TAG:
+            throw new ContentDirectoryError.REQUIRED_TAG
+                                        ("Tried to delete required tag.");
+
+        case DIDLLiteFragmentResult.READONLY_TAG:
+            throw new ContentDirectoryError.READ_ONLY_TAG
+                                        ("Tried to change read-only property.");
+
+        case DIDLLiteFragmentResult.MISMATCH:
             throw new ContentDirectoryError.PARAMETER_MISMATCH
-                (_("CurrentTagValue should have the same number of elements as " +
-                   "NewTagValue (%s vs %s)."),
-                 current_list.size,
-                 new_list.size);
-        }
-        /* Just to avoid some unused warning. */
-        if (media_object == null) {
-            return;
+                                        ("Parameter count mismatch.");
+
+        default:
+            throw new ContentDirectoryError.NO_SUCH_OBJECT ("Unknown error.");
         }
     }
 
diff --git a/src/librygel-server/rygel-media-item.vala b/src/librygel-server/rygel-media-item.vala
index 82200d9..29d9d08 100644
--- a/src/librygel-server/rygel-media-item.vala
+++ b/src/librygel-server/rygel-media-item.vala
@@ -181,6 +181,12 @@ public abstract class Rygel.MediaItem : MediaObject {
         }
     }
 
+    internal override void apply_didl_lite (DIDLLiteObject didl_object) {
+        base.apply_didl_lite (didl_object);
+        this.date = didl_object.date;
+        this.description = didl_object.description;
+    }
+
     internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
                                                 HTTPServer     http_server)
                                                 throws Error {
diff --git a/src/librygel-server/rygel-media-object.vala b/src/librygel-server/rygel-media-object.vala
index d8d9ca9..787d77f 100644
--- a/src/librygel-server/rygel-media-object.vala
+++ b/src/librygel-server/rygel-media-object.vala
@@ -157,6 +157,33 @@ public abstract class Rygel.MediaObject : GLib.Object {
                                                 HTTPServer     http_server)
                                                 throws Error;
 
+    internal virtual void apply_didl_lite (DIDLLiteObject didl_object) {
+        this.title = didl_object.title;
+    }
+
+    internal DIDLLiteFragmentResult apply_fragments
+                                        (LinkedList<string> current_fragments,
+                                         LinkedList<string> new_fragments,
+                                         HTTPServer         http_server) {
+        var result = DIDLLiteFragmentResult.UNKNOWN_ERROR;
+
+        try {
+            var writer = new DIDLLiteWriter (null);
+            var didl_object = this.serialize (writer, http_server);
+
+            result = didl_object.apply_fragments (current_fragments.to_array (),
+                                                  current_fragments.size,
+                                                  new_fragments.to_array (),
+                                                  new_fragments.size);
+            if (result == DIDLLiteFragmentResult.OK) {
+                this.apply_didl_lite (didl_object);
+            }
+
+        } catch (Error e) {}
+
+        return result;
+    }
+
     internal virtual int compare_by_property (MediaObject media_object,
                                               string      property) {
         switch (property) {
diff --git a/src/librygel-server/rygel-music-item.vala b/src/librygel-server/rygel-music-item.vala
index 48e4bf8..dd3cc84 100644
--- a/src/librygel-server/rygel-music-item.vala
+++ b/src/librygel-server/rygel-music-item.vala
@@ -94,6 +94,23 @@ public class Rygel.MusicItem : AudioItem {
         }
     }
 
+    private string get_first (GLib.List<GUPnP.DIDLLiteContributor>? contributors) {
+        if (contributors != null) {
+            return contributors.data.name;
+        }
+        return "";
+    }
+
+    internal override void apply_didl_lite (DIDLLiteObject didl_object) {
+        base.apply_didl_lite (didl_object);
+        this.artist = get_first (didl_object.get_artists ());
+        this.track_number = didl_object.track_number;
+        this.album = didl_object.album;
+        this.genre = didl_object.genre;
+        // TODO: Not sure about it.
+        //this.album_art.uri = didl_object.album_art
+    }
+
     internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
                                                 HTTPServer     http_server)
                                                 throws Error {
diff --git a/src/librygel-server/rygel-photo-item.vala b/src/librygel-server/rygel-photo-item.vala
index e2465b0..a419c2c 100644
--- a/src/librygel-server/rygel-photo-item.vala
+++ b/src/librygel-server/rygel-photo-item.vala
@@ -58,6 +58,18 @@ public class Rygel.PhotoItem : ImageItem {
         }
     }
 
+    private string get_first (GLib.List<GUPnP.DIDLLiteContributor>? contributors) {
+        if (contributors != null) {
+            return contributors.data.name;
+        }
+        return "";
+    }
+
+    internal override void apply_didl_lite (DIDLLiteObject didl_object) {
+        base.apply_didl_lite (didl_object);
+        this.creator = get_first (didl_object.get_creators ());
+    }
+
     internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
                                                 HTTPServer     http_server)
                                                 throws Error {
diff --git a/src/librygel-server/rygel-video-item.vala b/src/librygel-server/rygel-video-item.vala
index 7818973..676f75f 100644
--- a/src/librygel-server/rygel-video-item.vala
+++ b/src/librygel-server/rygel-video-item.vala
@@ -137,6 +137,18 @@ public class Rygel.VideoItem : AudioItem, VisualItem {
         }
     }
 
+    private string get_first (GLib.List<GUPnP.DIDLLiteContributor>? contributors) {
+        if (contributors != null) {
+            return contributors.data.name;
+        }
+        return "";
+    }
+
+    internal override void apply_didl_lite (DIDLLiteObject didl_object) {
+        base.apply_didl_lite (didl_object);
+        this.author = get_first (didl_object.get_authors ());
+    }
+
     internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
                                                 HTTPServer     http_server)
                                                 throws Error {



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