[rygel] core: Refactor client hacks



commit 5126066b8c5565ce22eeb355ae0d867a325dcfd6
Author: Jens Georg <mail jensge org>
Date:   Thu Dec 8 19:30:58 2011 +0100

    core: Refactor client hacks
    
    Unify both constructors to operate on SoupMessage.

 src/rygel/rygel-client-hacks.vala       |   55 ++++++++++++-------------------
 src/rygel/rygel-http-get.vala           |   10 ++---
 src/rygel/rygel-http-request.vala       |    5 +++
 src/rygel/rygel-media-query-action.vala |    2 +-
 src/rygel/rygel-panasonic-hacks.vala    |   15 +-------
 src/rygel/rygel-v1-hacks.vala           |   15 +-------
 src/rygel/rygel-xbmc-hacks.vala         |   15 +-------
 src/rygel/rygel-xbox-hacks.vala         |   22 ++----------
 tests/rygel-http-get-test.vala          |    2 +-
 9 files changed, 42 insertions(+), 99 deletions(-)
---
diff --git a/src/rygel/rygel-client-hacks.vala b/src/rygel/rygel-client-hacks.vala
index a867d8e..6b84e86 100644
--- a/src/rygel/rygel-client-hacks.vala
+++ b/src/rygel/rygel-client-hacks.vala
@@ -31,40 +31,16 @@ internal errordomain Rygel.ClientHacksError {
 internal abstract class Rygel.ClientHacks : GLib.Object {
     private const string CORRECT_OBJECT_ID = "ObjectID";
 
-    public unowned string object_id { get; protected set; }
-
+    public unowned string object_id { get;
+                                      protected set;
+                                      default = CORRECT_OBJECT_ID; }
     protected Regex agent_regex;
 
-    public static ClientHacks create_for_action (ServiceAction action)
-                                                 throws ClientHacksError {
-        try {
-            return new XBoxHacks.for_action (action);
-        } catch {}
-
-        try {
-            return new PanasonicHacks.for_action (action);
-        } catch {}
-
-        return new XBMCHacks.for_action (action);
-    }
-
-    public static ClientHacks create_for_headers (MessageHeaders headers)
-                                                  throws ClientHacksError {
-        try {
-            return new XBoxHacks.for_headers (headers);
-        } catch {}
-
-        try {
-            return new PanasonicHacks.for_headers (headers);
-        } catch {};
-
-        return new XBMCHacks.for_headers (headers);
-    }
-
-    protected ClientHacks (string agent_pattern, MessageHeaders? headers = null)
+    protected ClientHacks (string   agent,
+                           Message? message)
                            throws ClientHacksError {
         try {
-            this.agent_regex = new Regex (agent_pattern,
+            this.agent_regex = new Regex (agent,
                                           RegexCompileFlags.CASELESS,
                                           0);
         } catch (RegexError error) {
@@ -72,11 +48,9 @@ internal abstract class Rygel.ClientHacks : GLib.Object {
             assert_not_reached ();
         }
 
-        if (headers != null) {
-            this.check_headers (headers);
+        if (message != null) {
+            this.check_headers (message.request_headers);
         }
-
-        this.object_id = CORRECT_OBJECT_ID;
     }
 
     public bool is_album_art_request (Soup.Message message) {
@@ -92,6 +66,19 @@ internal abstract class Rygel.ClientHacks : GLib.Object {
         return (album_art != null) && bool.parse (album_art);
     }
 
+    public static ClientHacks create (Message? message)
+                                      throws ClientHacksError {
+        try {
+            return new PanasonicHacks (message);
+        } catch (Error error) { }
+
+        try {
+            return new XBoxHacks (message);
+        } catch (Error error) { }
+
+        return new XBMCHacks (message);
+    }
+
     public virtual void translate_container_id (MediaQueryAction action,
                                                 ref string       container_id) {}
 
diff --git a/src/rygel/rygel-http-get.vala b/src/rygel/rygel-http-get.vala
index 97fa2a2..cbc8d6c 100644
--- a/src/rygel/rygel-http-get.vala
+++ b/src/rygel/rygel-http-get.vala
@@ -83,10 +83,8 @@ internal class Rygel.HTTPGet : HTTPRequest {
                                                   this.item.id);
         }
 
-        try {
-            var hack = ClientHacks.create_for_headers
-                                        (this.msg.request_headers);
-            if (hack.is_album_art_request (this.msg) &&
+        if (this.hack != null) {
+            if (this.hack.is_album_art_request (this.msg) &&
                 this.item is VisualItem) {
                 var visual_item = this.item as VisualItem;
 
@@ -101,9 +99,9 @@ internal class Rygel.HTTPGet : HTTPRequest {
 
                 return;
             } else {
-                hack.apply (this.item);
+                this.hack.apply (this.item);
             }
-        } catch (ClientHacksError error) {}
+        }
 
         if (this.uri.thumbnail_index >= 0) {
             if (this.item is MusicItem) {
diff --git a/src/rygel/rygel-http-request.vala b/src/rygel/rygel-http-request.vala
index 93ef830..6f95ed4 100644
--- a/src/rygel/rygel-http-request.vala
+++ b/src/rygel/rygel-http-request.vala
@@ -44,6 +44,8 @@ internal abstract class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
     protected HTTPItemURI uri;
     public MediaItem item;
 
+    protected ClientHacks hack;
+
     public HTTPRequest (HTTPServer   http_server,
                         Soup.Server  server,
                         Soup.Message msg) {
@@ -52,6 +54,9 @@ internal abstract class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
         this.root_container = http_server.root_container;
         this.server = server;
         this.msg = msg;
+        try {
+            this.hack = ClientHacks.create (msg);
+        } catch (Error error) { }
     }
 
     public async void run () {
diff --git a/src/rygel/rygel-media-query-action.vala b/src/rygel/rygel-media-query-action.vala
index 417a205..24b1acf 100644
--- a/src/rygel/rygel-media-query-action.vala
+++ b/src/rygel/rygel-media-query-action.vala
@@ -63,7 +63,7 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
         this.didl_writer = new DIDLLiteWriter (null);
 
         try {
-            this.hacks = ClientHacks.create_for_action (this.action);
+            this.hacks = ClientHacks.create (this.action.get_message ());
         } catch { /* This just means we need no hacks, yay! */ }
     }
 
diff --git a/src/rygel/rygel-panasonic-hacks.vala b/src/rygel/rygel-panasonic-hacks.vala
index e13a6a1..f77a14d 100644
--- a/src/rygel/rygel-panasonic-hacks.vala
+++ b/src/rygel/rygel-panasonic-hacks.vala
@@ -38,19 +38,8 @@ internal class Rygel.PanasonicHacks : ClientHacks {
         }
     }
 
-    public PanasonicHacks () throws ClientHacksError, RegexError {
-        base (AGENT);
-    }
-
-    public PanasonicHacks.for_action (ServiceAction action)
-                                      throws ClientHacksError {
-        unowned MessageHeaders headers = action.get_message ().request_headers;
-        this.for_headers (headers);
-    }
-
-    public PanasonicHacks.for_headers (MessageHeaders headers)
-                                       throws ClientHacksError {
-        base (AGENT, headers);
+    public PanasonicHacks (Message? message = null) throws ClientHacksError {
+        base (AGENT, message);
     }
 
     public override void apply (MediaItem item) {
diff --git a/src/rygel/rygel-v1-hacks.vala b/src/rygel/rygel-v1-hacks.vala
index 68d67f6..173dfae 100644
--- a/src/rygel/rygel-v1-hacks.vala
+++ b/src/rygel/rygel-v1-hacks.vala
@@ -71,19 +71,8 @@ internal class Rygel.V1Hacks : ClientHacks {
         return agent_pattern;
     }
 
-    public V1Hacks () throws ClientHacksError, RegexError {
-        base (generate_agent_pattern ());
-    }
-
-    public V1Hacks.for_action (ServiceAction action)
-                                 throws ClientHacksError {
-        unowned MessageHeaders headers = action.get_message ().request_headers;
-        this.for_headers (headers);
-    }
-
-    public V1Hacks.for_headers (MessageHeaders headers)
-                                  throws ClientHacksError {
-        base (generate_agent_pattern (), headers);
+    public V1Hacks () throws ClientHacksError {
+        base (generate_agent_pattern (), null);
     }
 
     public void apply_on_device (RootDevice device,
diff --git a/src/rygel/rygel-xbmc-hacks.vala b/src/rygel/rygel-xbmc-hacks.vala
index 44d9ad7..8659c9b 100644
--- a/src/rygel/rygel-xbmc-hacks.vala
+++ b/src/rygel/rygel-xbmc-hacks.vala
@@ -28,19 +28,8 @@ internal class Rygel.XBMCHacks : ClientHacks {
     // promised by developers.
     private const string AGENT = ".*Platinum/.*|.*XBMC/.*";
 
-    public XBMCHacks () throws ClientHacksError, RegexError {
-        base (AGENT);
-    }
-
-    public XBMCHacks.for_action (ServiceAction action)
-                                 throws ClientHacksError {
-        unowned MessageHeaders headers = action.get_message ().request_headers;
-        this.for_headers (headers);
-    }
-
-    public XBMCHacks.for_headers (MessageHeaders headers)
-                                  throws ClientHacksError {
-        base (AGENT, headers);
+    public XBMCHacks (Message? message = null) throws ClientHacksError {
+        base (AGENT, message);
     }
 
     public override void apply (MediaItem item) {
diff --git a/src/rygel/rygel-xbox-hacks.vala b/src/rygel/rygel-xbox-hacks.vala
index d81cbe9..95e11c9 100644
--- a/src/rygel/rygel-xbox-hacks.vala
+++ b/src/rygel/rygel-xbox-hacks.vala
@@ -25,8 +25,7 @@ using Soup;
 using GUPnP;
 
 internal class Rygel.XBoxHacks : ClientHacks {
-    private const string AGENT =
-        ".*Xbox.*";
+    private const string AGENT = ".*Xbox.*";
     private const string DMS = "urn:schemas-upnp-org:device:MediaServer";
     private const string DMS_V1 = DMS + ":1";
     private const string FRIENDLY_NAME_POSTFIX = ":";
@@ -34,23 +33,10 @@ internal class Rygel.XBoxHacks : ClientHacks {
     private const string MODEL_VERSION = "11";
     private const string CONTAINER_ID = "ContainerID";
 
-    public XBoxHacks () throws ClientHacksError {
-        base (AGENT);
-    }
-
-    public XBoxHacks.for_action (ServiceAction action) throws ClientHacksError {
-        unowned MessageHeaders headers = action.get_message ().request_headers;
-        this.for_headers (headers);
-    }
+    public XBoxHacks (Message? message = null) throws ClientHacksError {
+        base (AGENT, message);
 
-    public XBoxHacks.for_headers (MessageHeaders headers)
-                                  throws ClientHacksError {
-        base (AGENT, headers);
-
-        var agent = headers.get_one ("User-Agent");
-        if (agent.contains ("Xbox")) {
-            this.object_id = CONTAINER_ID;
-        }
+        this.object_id = CONTAINER_ID;
     }
 
     public void apply_on_device (RootDevice device,
diff --git a/tests/rygel-http-get-test.vala b/tests/rygel-http-get-test.vala
index 40ce123..d57028e 100644
--- a/tests/rygel-http-get-test.vala
+++ b/tests/rygel-http-get-test.vala
@@ -34,7 +34,7 @@ public errordomain Rygel.ClientHacksError {
 }
 
 public class Rygel.ClientHacks {
-    public static ClientHacks create_for_headers (MessageHeaders headers) throws Error {
+    public static ClientHacks create (Message? message) throws Error {
         throw new ClientHacksError.NA ("");
     }
 



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