[rygel/wip/didl-s: 4/9] server: Add generic Serializer class



commit 4d9d0b26e9ec359a16281d5c07acd4787ef4d7ba
Author: Jens Georg <jensg openismus com>
Date:   Wed Oct 31 20:04:51 2012 +0100

    server: Add generic Serializer class

 src/librygel-server/filelist.am                   |    1 +
 src/librygel-server/rygel-item-creator.vala       |    8 +-
 src/librygel-server/rygel-media-container.vala    |   12 ++-
 src/librygel-server/rygel-media-item.vala         |    8 +-
 src/librygel-server/rygel-media-object.vala       |    8 +-
 src/librygel-server/rygel-media-objects.vala      |    8 +-
 src/librygel-server/rygel-media-query-action.vala |   10 ++--
 src/librygel-server/rygel-music-item.vala         |    8 +-
 src/librygel-server/rygel-photo-item.vala         |    6 +-
 src/librygel-server/rygel-serializer.vala         |   77 +++++++++++++++++++++
 src/librygel-server/rygel-video-item.vala         |    8 +-
 tests/Makefile.am                                 |    3 +-
 tests/rygel-item-creator-test.vala                |    2 +-
 tests/rygel-serializer_item-creator.vala          |    1 +
 14 files changed, 122 insertions(+), 38 deletions(-)
---
diff --git a/src/librygel-server/filelist.am b/src/librygel-server/filelist.am
index b6c672c..d43e800 100644
--- a/src/librygel-server/filelist.am
+++ b/src/librygel-server/filelist.am
@@ -63,6 +63,7 @@ LIBRYGEL_SERVER_NONVAPI_SOURCE_FILES = \
 	rygel-panasonic-hacks.vala \
 	rygel-search-criteria-parser.vala \
 	rygel-search.vala \
+	rygel-serializer.vala \
 	rygel-source-connection-manager.vala \
 	rygel-subtitle-manager.vala \
 	rygel-thumbnailer.vala \
diff --git a/src/librygel-server/rygel-item-creator.vala b/src/librygel-server/rygel-item-creator.vala
index 1f5dd70..45f2952 100644
--- a/src/librygel-server/rygel-item-creator.vala
+++ b/src/librygel-server/rygel-item-creator.vala
@@ -41,7 +41,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
 
     private ContentDirectory content_dir;
     private ServiceAction action;
-    private DIDLLiteWriter didl_writer;
+    private Serializer serializer;
     private DIDLLiteParser didl_parser;
     private Regex title_regex;
 
@@ -52,7 +52,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         this.content_dir = content_dir;
         this.cancellable = content_dir.cancellable;
         this.action = (owned) action;
-        this.didl_writer = new DIDLLiteWriter (null);
+        this.serializer = new Serializer (SerializerType.GENERIC_DIDL);
         this.didl_parser = new DIDLLiteParser ();
         try {
             var pattern = "[" + Regex.escape_string (INVALID_CHARS) + "]";
@@ -90,7 +90,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
 
             yield this.wait_for_item (container);
 
-            this.item.serialize (didl_writer, this.content_dir.http_server);
+            this.item.serialize (serializer, this.content_dir.http_server);
 
             // Conclude the successful action
             this.conclude ();
@@ -293,7 +293,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
 
     private void conclude () {
         /* Retrieve generated string */
-        string didl = this.didl_writer.get_string ();
+        string didl = this.serializer.get_string ();
 
         /* Set action return arguments */
         this.action.set ("ObjectID", typeof (string), this.item.id,
diff --git a/src/librygel-server/rygel-media-container.vala b/src/librygel-server/rygel-media-container.vala
index 4c379ba..e4b6d11 100644
--- a/src/librygel-server/rygel-media-container.vala
+++ b/src/librygel-server/rygel-media-container.vala
@@ -193,10 +193,14 @@ public abstract class Rygel.MediaContainer : MediaObject {
         this.container_updated (this, object, event_type, sub_tree_update);
     }
 
-    internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
-                                                HTTPServer     http_server)
-                                                throws Error {
-        var didl_container = writer.add_container ();
+    internal override DIDLLiteObject? serialize (Serializer serializer,
+                                                 HTTPServer http_server)
+                                                 throws Error {
+        var didl_container = serializer.add_container ();
+        if (didl_container == null) {
+            return null;
+        }
+
         if (this.parent != null) {
             didl_container.parent_id = this.parent.id;
         } else {
diff --git a/src/librygel-server/rygel-media-item.vala b/src/librygel-server/rygel-media-item.vala
index c031294..d6b3add 100644
--- a/src/librygel-server/rygel-media-item.vala
+++ b/src/librygel-server/rygel-media-item.vala
@@ -202,10 +202,10 @@ public abstract class Rygel.MediaItem : MediaObject {
         this.description = didl_object.description;
     }
 
-    internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
-                                                HTTPServer     http_server)
-                                                throws Error {
-        var didl_item = writer.add_item ();
+    internal override DIDLLiteObject? serialize (Serializer serializer,
+                                                 HTTPServer http_server)
+                                                 throws Error {
+        var didl_item = serializer.add_item ();
 
         didl_item.id = this.id;
 
diff --git a/src/librygel-server/rygel-media-object.vala b/src/librygel-server/rygel-media-object.vala
index 70249bc..e183960 100644
--- a/src/librygel-server/rygel-media-object.vala
+++ b/src/librygel-server/rygel-media-object.vala
@@ -154,9 +154,9 @@ public abstract class Rygel.MediaObject : GLib.Object {
         return writables;
     }
 
-    internal abstract DIDLLiteObject serialize (DIDLLiteWriter writer,
-                                                HTTPServer     http_server)
-                                                throws Error;
+    internal abstract DIDLLiteObject? serialize (Serializer serializer,
+                                                 HTTPServer http_server)
+                                                 throws Error;
 
     internal virtual void apply_didl_lite (DIDLLiteObject didl_object) {
         this.title = didl_object.title;
@@ -169,7 +169,7 @@ public abstract class Rygel.MediaObject : GLib.Object {
         var result = DIDLLiteFragmentResult.UNKNOWN_ERROR;
 
         try {
-            var writer = new DIDLLiteWriter (null);
+            var writer = new Serializer (SerializerType.GENERIC_DIDL);
             var didl_object = this.serialize (writer, http_server);
 
             result = didl_object.apply_fragments
diff --git a/src/librygel-server/rygel-media-objects.vala b/src/librygel-server/rygel-media-objects.vala
index f61d333..ea020f4 100644
--- a/src/librygel-server/rygel-media-objects.vala
+++ b/src/librygel-server/rygel-media-objects.vala
@@ -54,15 +54,15 @@ public class Rygel.MediaObjects : ArrayList<MediaObject> {
         });
     }
 
-    internal void serialize (DIDLLiteWriter didl_writer,
-                             HTTPServer     http_server,
-                             ClientHacks?   hacks) throws Error {
+    internal void serialize (Serializer   serializer,
+                             HTTPServer   http_server,
+                             ClientHacks? hacks) throws Error {
         foreach (var result in this) {
             if (result is MediaItem && hacks != null) {
                 hacks.apply (result as MediaItem);
             }
 
-            result.serialize (didl_writer, http_server);
+            result.serialize (serializer, http_server);
         }
     }
 
diff --git a/src/librygel-server/rygel-media-query-action.vala b/src/librygel-server/rygel-media-query-action.vala
index f7b1aa1..c816f16 100644
--- a/src/librygel-server/rygel-media-query-action.vala
+++ b/src/librygel-server/rygel-media-query-action.vala
@@ -46,7 +46,7 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
     protected HTTPServer http_server;
     protected uint32 system_update_id;
     protected ServiceAction action;
-    protected DIDLLiteWriter didl_writer;
+    protected Serializer serializer;
     protected ClientHacks hacks;
     protected string object_id_arg;
 
@@ -58,7 +58,7 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
         this.cancellable = content_dir.cancellable;
         this.action = (owned) action;
 
-        this.didl_writer = new DIDLLiteWriter (null);
+        this.serializer = new Serializer (SerializerType.GENERIC_DIDL);
 
         try {
             this.hacks = ClientHacks.create (this.action.get_message ());
@@ -80,7 +80,7 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
             }
 
 
-            results.serialize (this.didl_writer,
+            results.serialize (this.serializer,
                                this.http_server,
                                this.hacks);
 
@@ -189,10 +189,10 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
 
     private void conclude () {
         // Apply the filter from the client
-        this.didl_writer.filter (this.filter);
+        this.serializer.filter (this.filter);
 
         /* Retrieve generated string */
-        string didl = this.didl_writer.get_string ();
+        string didl = this.serializer.get_string ();
 
         if (this.update_id == uint32.MAX) {
             this.update_id = this.system_update_id;
diff --git a/src/librygel-server/rygel-music-item.vala b/src/librygel-server/rygel-music-item.vala
index 4594f1c..a1acb0e 100644
--- a/src/librygel-server/rygel-music-item.vala
+++ b/src/librygel-server/rygel-music-item.vala
@@ -113,10 +113,10 @@ public class Rygel.MusicItem : AudioItem {
         //this.album_art.uri = didl_object.album_art
     }
 
-    internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
-                                                HTTPServer     http_server)
-                                                throws Error {
-        var didl_item = base.serialize (writer, http_server);
+    internal override DIDLLiteObject? serialize (Serializer serializer,
+                                                 HTTPServer http_server)
+                                                 throws Error {
+        var didl_item = base.serialize (serializer, http_server);
 
         if (this.artist != null && this.artist != "") {
             var contributor = didl_item.add_artist ();
diff --git a/src/librygel-server/rygel-photo-item.vala b/src/librygel-server/rygel-photo-item.vala
index 4e8b7f5..b344e08 100644
--- a/src/librygel-server/rygel-photo-item.vala
+++ b/src/librygel-server/rygel-photo-item.vala
@@ -72,10 +72,10 @@ public class Rygel.PhotoItem : ImageItem {
         this.creator = get_first (didl_object.get_creators ());
     }
 
-    internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
-                                                HTTPServer     http_server)
+    internal override DIDLLiteObject? serialize (Serializer serializer,
+                                                HTTPServer  http_server)
                                                 throws Error {
-        var didl_item = base.serialize (writer, http_server);
+        var didl_item = base.serialize (serializer, http_server);
 
         if (this.creator != null && this.creator != "") {
             var contributor = didl_item.add_creator ();
diff --git a/src/librygel-server/rygel-serializer.vala b/src/librygel-server/rygel-serializer.vala
new file mode 100644
index 0000000..e7a411b
--- /dev/null
+++ b/src/librygel-server/rygel-serializer.vala
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2012 Intel Corporation.
+ *
+ * Author: Jens Georg <jensg openismus com>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using GUPnP;
+
+internal enum SerializerType {
+    GENERIC_DIDL,
+    DIDL_S
+}
+
+internal class Rygel.Serializer : Object {
+    private DIDLLiteWriter writer;
+    private MediaCollection collection;
+
+    public Serializer (SerializerType type) {
+        switch (type) {
+            case SerializerType.GENERIC_DIDL:
+                this.writer = new DIDLLiteWriter (null);
+                break;
+            case SerializerType.DIDL_S:
+                this.collection = new MediaCollection ();
+                break;
+            default:
+                assert_not_reached ();
+        }
+    }
+
+    public DIDLLiteItem? add_item () {
+        if (writer != null) {
+            return this.writer.add_item ();
+        } else {
+            return this.collection.add_item ();
+        }
+    }
+
+    public DIDLLiteContainer? add_container () {
+        if (writer != null) {
+            return this.writer.add_container ();
+        } else {
+            // MediaCollection does not support this.
+            return null;
+        }
+    }
+
+    public void filter (string filter_string) {
+        if (writer != null) {
+            this.writer.filter (filter_string);
+        }
+    }
+
+    public string get_string () {
+        if (writer != null) {
+            return this.writer.get_string ();
+        } else {
+            return this.collection.get_string ();
+        }
+    }
+}
diff --git a/src/librygel-server/rygel-video-item.vala b/src/librygel-server/rygel-video-item.vala
index dbacc4e..679b700 100644
--- a/src/librygel-server/rygel-video-item.vala
+++ b/src/librygel-server/rygel-video-item.vala
@@ -151,10 +151,10 @@ public class Rygel.VideoItem : AudioItem, VisualItem {
         this.author = get_first (didl_object.get_authors ());
     }
 
-    internal override DIDLLiteObject serialize (DIDLLiteWriter writer,
-                                                HTTPServer     http_server)
-                                                throws Error {
-        var didl_item = base.serialize (writer, http_server);
+    internal override DIDLLiteObject? serialize (Serializer serializer,
+                                                 HTTPServer  http_server)
+                                                 throws Error {
+        var didl_item = base.serialize (serializer, http_server);
 
         if (this.author != null && this.author != "") {
             var contributor = didl_item.add_author ();
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2928eac..1be669d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -99,7 +99,8 @@ rygel_item_creator_test_SOURCES = rygel-item-creator-test.vala \
 				  rygel-state-machine_item-creator.vala \
 				  rygel-relational-expression.vala \
 				  rygel-search-expression.vala \
-				  rygel-media-engine.vala
+				  rygel-media-engine.vala \
+				  rygel-serializer_item-creator.vala
 
 rygel_user_config_test_SOURCES = rygel-configuration.vala \
 				 rygel-user-config.vala \
diff --git a/tests/rygel-item-creator-test.vala b/tests/rygel-item-creator-test.vala
index 973a7f0..237dc20 100644
--- a/tests/rygel-item-creator-test.vala
+++ b/tests/rygel-item-creator-test.vala
@@ -140,7 +140,7 @@ public class Rygel.MediaItem : Rygel.MediaObject {
         this.title = title;
     }
 
-    public void serialize (GUPnP.DIDLLiteWriter writer, HTTPServer server) {
+    internal void serialize (Rygel.Serializer serializer, HTTPServer server) {
     }
 }
 
diff --git a/tests/rygel-serializer_item-creator.vala b/tests/rygel-serializer_item-creator.vala
new file mode 120000
index 0000000..a8cce1b
--- /dev/null
+++ b/tests/rygel-serializer_item-creator.vala
@@ -0,0 +1 @@
+../src/librygel-server/rygel-serializer.vala
\ No newline at end of file



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