[rygel] media-export: Simplify variant checking



commit 9e2e4614aa6183897a02b77dfe0c71a1a6a62de2
Author: Jens Georg <mail jensge org>
Date:   Mon Sep 14 07:09:53 2015 +0200

    media-export: Simplify variant checking
    
    Signed-off-by: Jens Georg <mail jensge org>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=754420

 .../rygel-media-export-harvesting-task.vala        |   32 ++++---
 .../rygel-media-export-item-factory.vala           |  103 ++++++++------------
 2 files changed, 61 insertions(+), 74 deletions(-)
---
diff --git a/src/plugins/media-export/rygel-media-export-harvesting-task.vala 
b/src/plugins/media-export/rygel-media-export-harvesting-task.vala
index 3977b16..ac25505 100644
--- a/src/plugins/media-export/rygel-media-export-harvesting-task.vala
+++ b/src/plugins/media-export/rygel-media-export-harvesting-task.vala
@@ -306,20 +306,26 @@ public class Rygel.MediaExport.HarvestingTask : Rygel.StateMachine,
             this.completed ();
         }
 
-        var item = ItemFactory.create_from_variant (this.containers.peek_head (),
-                                                    file,
-                                                    info);
-
-        if (item != null) {
-            item.parent_ref = this.containers.peek_head ();
-            // This is only necessary to generate the proper <objAdd LastChange
-            // entry
-            if (this.files.peek ().known) {
-                (item as UpdatableObject).non_overriding_commit.begin ();
-            } else {
-                var container = item.parent as TrackableContainer;
-                container.add_child_tracked.begin (item) ;
+        try {
+            var parent = this.containers.peek_head ();
+            var item = ItemFactory.create_from_variant (parent,
+                                                        file,
+                                                        info);
+
+            if (item != null) {
+                item.parent_ref = parent;
+                // This is only necessary to generate the proper <objAdd LastChange
+                // entry
+                if (this.files.peek ().known) {
+                    (item as UpdatableObject).non_overriding_commit.begin ();
+                } else {
+                    var container = item.parent as TrackableContainer;
+                    container.add_child_tracked.begin (item) ;
+                }
             }
+        } catch (Error error) {
+            warning (/*_*/"Failed to extract meta-data for file %s",
+                     error.message);
         }
 
         this.files.poll ();
diff --git a/src/plugins/media-export/rygel-media-export-item-factory.vala 
b/src/plugins/media-export/rygel-media-export-item-factory.vala
index 3accbc3..701759d 100644
--- a/src/plugins/media-export/rygel-media-export-item-factory.vala
+++ b/src/plugins/media-export/rygel-media-export-item-factory.vala
@@ -33,6 +33,23 @@ using Gst.PbUtils;
  * Represents MediaExport item.
  */
 namespace Rygel.MediaExport.ItemFactory {
+    internal errordomain ItemFactoryError {
+        DESERIALIZATION,
+        MISMATCH
+    }
+
+    private static bool check_variant_type (Variant v,
+                                            string typestring) throws Error {
+        if (!v.is_of_type (new VariantType (typestring))) {
+            var msg = "Variant type mismatch, expected %s, got %s";
+            throw new ItemFactoryError.DESERIALIZATION (msg,
+                                                        v.get_type_string (),
+                                                        typestring);
+        }
+
+        return true;
+    }
+
     private static MediaFileItem? create_playlist_item (File file,
                                                         MediaContainer parent,
                                                         string fallback_title) {
@@ -85,13 +102,9 @@ namespace Rygel.MediaExport.ItemFactory {
 
     static MediaFileItem? create_from_variant (MediaContainer parent,
                                                File           file,
-                                               Variant        v) {
-        if (!v.is_of_type (new VariantType ("(smvmvmvmvmvmv)"))) {
-            warning (_("Invalid metadata serialisation, cannot process %s"),
-                     v.get_type_string ());
-
-            return null;
-        }
+                                               Variant        v)
+                                               throws Error {
+        ItemFactory.check_variant_type (v,"(smvmvmvmvmvmv)");
 
         Variant? upnp_class,
                  file_info,
@@ -102,14 +115,6 @@ namespace Rygel.MediaExport.ItemFactory {
                  meta_data;
 
         var it = v.iterator ();
-        if (it.n_children () != 7) {
-            warning (ngettext("Invalid metadata serialisation: expected 7 children, got %d",
-                              "Invalid metadata serialisation: expected 7 children, got %d",
-                              (int) it.n_children ()),
-                     (int) it.n_children ());
-
-            return null;
-        }
 
         var id = MediaCache.get_id (file);
 
@@ -199,13 +204,9 @@ namespace Rygel.MediaExport.ItemFactory {
         return item as MediaFileItem;
     }
 
-    private static void apply_meta_data (MediaFileItem item, Variant v) {
-        if (!v.is_of_type (new VariantType ("(msmsmsiii)"))) {
-            warning (_("Invalid metadata serialisation of metadata; %s"),
-                     v.get_type_string ());
-
-            return;
-        }
+    private static void apply_meta_data (MediaFileItem item, Variant v)
+                                         throws Error {
+        ItemFactory.check_variant_type (v, "(msmsmsiii)");
 
         var it = v.iterator ();
         var val = it.next_value ().get_maybe ();
@@ -235,16 +236,13 @@ namespace Rygel.MediaExport.ItemFactory {
         }
     }
 
-    private static void apply_video_info (MediaFileItem item, Variant v) {
-        if (!v.is_of_type (new VariantType ("(iii)"))) {
-            warning (_("Invalid metadata serialisation of video info; %s"),
-                     v.get_type_string ());
-
-            return;
-        }
+    private static void apply_video_info (MediaFileItem item, Variant v)
+                                          throws Error {
+        ItemFactory.check_variant_type (v, "(iii)");
 
         if (!(item is VisualItem)) {
-            return;
+            var msg = "UPnP class does not match supplied meta data";
+            throw new ItemFactoryError.MISMATCH (msg);
         }
 
         var visual_item = item as VisualItem;
@@ -254,16 +252,13 @@ namespace Rygel.MediaExport.ItemFactory {
         visual_item.color_depth = it.next_value ().get_int32 ();
     }
 
-    private static void apply_audio_info (MediaFileItem item, Variant v) {
-        if (!v.is_of_type (new VariantType ("(ii)"))) {
-            warning (_("Invalid metadata serialisation of audio info; %s"),
-                     v.get_type_string ());
-
-            return;
-        }
+    private static void apply_audio_info (MediaFileItem item, Variant v)
+                                          throws Error {
+        ItemFactory.check_variant_type (v, "(ii)");
 
         if (!(item is AudioItem)) {
-            return;
+            var msg = "UPnP class does not match supplied meta data";
+            throw new ItemFactoryError.MISMATCH (msg);
         }
 
         var audio_item = item as AudioItem;
@@ -272,10 +267,9 @@ namespace Rygel.MediaExport.ItemFactory {
         audio_item.sample_freq = it.next_value ().get_int32 ();
     }
 
-    private static void apply_info (MediaFileItem item, Variant v) {
-        if (!v.is_of_type (new VariantType ("(msmsi)"))) {
-            warning (_("Invalid metadata serialisation of general info"));
-        }
+    private static void apply_info (MediaFileItem item, Variant v)
+                                    throws Error {
+        ItemFactory.check_variant_type (v, "(msmsi)");
 
         var it = v.iterator ();
         var val = it.next_value ().get_maybe ();
@@ -293,33 +287,20 @@ namespace Rygel.MediaExport.ItemFactory {
         }
     }
 
-    private static void apply_dlna_profile (MediaFileItem item, Variant v) {
-        if (!v.is_of_type (new VariantType ("(ss)"))) {
-            warning (_("Invalid metadata serialisation of DLNA profile %s"),
-                     v.get_type_string ());
-
-            return;
-        }
+    private static void apply_dlna_profile (MediaFileItem item, Variant v)
+                                            throws Error {
+        ItemFactory.check_variant_type (v, "(ss)");
 
         var it = v.iterator ();
         item.dlna_profile = it.next_value ().dup_string ();
         item.mime_type = it.next_value ().dup_string ();
     }
 
-    private static void apply_file_info (MediaFileItem item, Variant v) {
-        if (!v.is_of_type (new VariantType ("(sstt)"))) {
-            warning (_("Invalid metadata serialisation of file info %s"),
-                     v.get_type_string ());
-
-            return;
-        }
+    private static void apply_file_info (MediaFileItem item, Variant v)
+                                         throws Error {
+        ItemFactory.check_variant_type (v, "(sstt)");
 
         var it = v.iterator ();
-        if (it.n_children () != 4) {
-            warning (_("Invalid metadata serialisation of file info"));
-
-            return;
-        }
 
         Variant display_name;
         display_name = it.next_value ();


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