[rygel] external: Refactor properties retrieval code



commit fd6e50b4143c8b820745b8dbd86702a439e0b964
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Aug 11 02:48:34 2010 +0300

    external: Refactor properties retrieval code
    
    Refactor most of the code that gets different properties from hashtable.

 .../external/rygel-external-icon-factory.vala      |   27 +++---
 .../external/rygel-external-item-factory.vala      |  105 +++++++-------------
 .../external/rygel-external-thumbnail-factory.vala |   39 ++++----
 3 files changed, 70 insertions(+), 101 deletions(-)
---
diff --git a/src/plugins/external/rygel-external-icon-factory.vala b/src/plugins/external/rygel-external-icon-factory.vala
index 8b5ac45..5b4d902 100644
--- a/src/plugins/external/rygel-external-icon-factory.vala
+++ b/src/plugins/external/rygel-external-icon-factory.vala
@@ -70,20 +70,9 @@ public class Rygel.External.IconFactory {
             icon.size = (int64) value;
         }
 
-        value = item_props.lookup ("Width");
-        if (value != null) {
-            icon.width = (int) value;
-        }
-
-        value = item_props.lookup ("Height");
-        if (value != null) {
-            icon.height = (int) value;
-        }
-
-        value = item_props.lookup ("ColorDepth");
-        if (value != null) {
-            icon.depth = (int) value;
-        }
+        icon.width = this.get_int (item_props, "Width");
+        icon.height = this.get_int (item_props, "Height");
+        icon.depth = this.get_int (item_props, "ColorDepth");
 
         return icon;
     }
@@ -97,4 +86,14 @@ public class Rygel.External.IconFactory {
             return "png"; // Assume PNG
       }
     }
+
+    private int get_int (HashTable<string,Value?> props, string prop) {
+        var value = props.lookup (prop);
+
+        if (value != null) {
+            return (int) value;
+        } else {
+            return -1;
+        }
+    }
 }
diff --git a/src/plugins/external/rygel-external-item-factory.vala b/src/plugins/external/rygel-external-item-factory.vala
index bae3624..5ba9edc 100644
--- a/src/plugins/external/rygel-external-item-factory.vala
+++ b/src/plugins/external/rygel-external-item-factory.vala
@@ -55,10 +55,9 @@ public class Rygel.External.ItemFactory {
             item.parent_ref = parent;
         }
 
-        var value = props.lookup ("MIMEType");
-        item.mime_type = (string) value;
+        item.mime_type = this.get_string (props, "MIMEType");
 
-        value = props.lookup ("URLs");
+        var value = props.lookup ("URLs");
         var uris = (string[]) value;
 
         for (var i = 0; uris[i] != null; i++) {
@@ -69,57 +68,24 @@ public class Rygel.External.ItemFactory {
 
         // Optional properties
 
-        value = props.lookup ("DLNAProfile");
-        if (value != null) {
-            item.dlna_profile = (string) value;
-        }
+        item.dlna_profile  = this.get_string (props, "DLNAProfile");
 
         value = props.lookup ("Size");
         if (value != null) {
             item.size = (int64) value;
         }
 
-        value = props.lookup ("Artist");
-        if (value != null) {
-            item.author = (string) value;
-        }
-
-        value = props.lookup ("Album");
-        if (value != null) {
-            item.album = (string) value;
-        }
-
-        value = props.lookup ("Genre");
-        if (value != null) {
-            item.genre = (string) value;
-        }
-
-        value = props.lookup ("Date");
-        if (value != null) {
-            item.date = (string) value;
-        }
+        item.author = this.get_string (props, "Artist");
+        item.album = this.get_string (props, "Album");
+        item.genre = this.get_string (props, "Genre");
+        item.date = this.get_string (props, "Date");
 
         // Properties specific to video and audio/music
 
-        value = props.lookup ("Duration");
-        if (value != null) {
-            item.duration = (int) value;
-        }
-
-        value = props.lookup ("Bitrate");
-        if (value != null) {
-            item.bitrate = (int) value;
-        }
-
-        value = props.lookup ("SampleRate");
-        if (value != null) {
-            item.sample_freq = (int) value;
-        }
-
-        value = props.lookup ("BitsPerSample");
-        if (value != null) {
-            item.bits_per_sample = (int) value;
-        }
+        item.duration = this.get_int (props, "Duration");
+        item.bitrate = this.get_int (props, "Bitrate");
+        item.sample_freq = this.get_int (props, "SampleRate");
+        item.bits_per_sample = this.get_int (props, "BitsPerSample");
 
         value = props.lookup ("AlbumArt");
         if (value != null) {
@@ -132,30 +98,11 @@ public class Rygel.External.ItemFactory {
 
         // Properties specific to video and image
 
-        value = props.lookup ("Width");
-        if (value != null) {
-            item.width = (int) value;
-        }
-
-        value = props.lookup ("Height");
-        if (value != null) {
-            item.height = (int) value;
-        }
-
-        value = props.lookup ("ColorDepth");
-        if (value != null) {
-            item.color_depth = (int) value;
-        }
-
-        value = props.lookup ("PixelWidth");
-        if (value != null) {
-            item.pixel_width = (int) value;
-        }
-
-        value = props.lookup ("PixelHeight");
-        if (value != null) {
-            item.pixel_height = (int) value;
-        }
+        item.width = this.get_int (props, "Width");
+        item.height = this.get_int (props, "Height");
+        item.color_depth = this.get_int (props, "ColorDepth");
+        item.pixel_width = this.get_int (props, "PixelWidth");
+        item.pixel_height = this.get_int (props, "PixelHeight");
 
         value = props.lookup ("Thumbnail");
         if (value != null) {
@@ -168,5 +115,25 @@ public class Rygel.External.ItemFactory {
 
         return item;
     }
+
+    private string? get_string (HashTable<string,Value?> props, string prop) {
+        var value = props.lookup (prop);
+
+        if (value != null) {
+            return (string) value;
+        } else {
+            return null;
+        }
+    }
+
+    private int get_int (HashTable<string,Value?> props, string prop) {
+        var value = props.lookup (prop);
+
+        if (value != null) {
+            return (int) value;
+        } else {
+            return -1;
+        }
+    }
 }
 
diff --git a/src/plugins/external/rygel-external-thumbnail-factory.vala b/src/plugins/external/rygel-external-thumbnail-factory.vala
index 69e74c4..121340c 100644
--- a/src/plugins/external/rygel-external-thumbnail-factory.vala
+++ b/src/plugins/external/rygel-external-thumbnail-factory.vala
@@ -43,41 +43,44 @@ public class Rygel.External.ThumbnailFactory {
 
         var thumbnail = new Thumbnail ();
 
-        var value = item_props.lookup ("MIMEType");
-        thumbnail.mime_type = (string) value;
+        thumbnail.mime_type = this.get_string (item_props, "MIMEType");
+        thumbnail.dlna_profile = this.get_string (item_props, "DLNAProfile");
+        thumbnail.width = this.get_int (item_props, "Width");
+        thumbnail.height = this.get_int (item_props, "Height");
+        thumbnail.depth = this.get_int (item_props, "ColorDepth");
 
-        value = item_props.lookup ("URLs");
+        var value = item_props.lookup ("URLs");
         var uris = (string[]) value;
         if (uris != null && uris[0] != null) {
             thumbnail.uri = uris[0].replace ("@ADDRESS@", host_ip);
         }
 
-        value = item_props.lookup ("DLNAProfile");
-        if (value != null) {
-            thumbnail.dlna_profile = (string) value;
-        }
-
         value = item_props.lookup ("Size");
         if (value != null) {
             thumbnail.size = (int64) value;
         }
 
-        value = item_props.lookup ("Width");
-        if (value != null) {
-            thumbnail.width = (int) value;
-        }
+        return thumbnail;
+    }
+
+    private string? get_string (HashTable<string,Value?> props, string prop) {
+        var value = props.lookup (prop);
 
-        value = item_props.lookup ("Height");
         if (value != null) {
-            thumbnail.height = (int) value;
+            return (string) value;
+        } else {
+            return null;
         }
+    }
+
+    private int get_int (HashTable<string,Value?> props, string prop) {
+        var value = props.lookup (prop);
 
-        value = item_props.lookup ("ColorDepth");
         if (value != null) {
-            thumbnail.depth = (int) value;
+            return (int) value;
+        } else {
+            return -1;
         }
-
-        return thumbnail;
     }
 }
 



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