external: Prefer factory usage over static methods
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Tue, 20 Oct 2009 20:21:29 +0000 (23:21 +0300)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Sat, 24 Oct 2009 15:51:14 +0000 (18:51 +0300)
src/plugins/external/rygel-external-plugin-factory.vala
src/plugins/external/rygel-external-plugin.vala

index def65ad..28cc4b3 100644 (file)
@@ -43,6 +43,10 @@ public class ExternalPluginFactory {
     private const string DBUS_SERVICE = "org.freedesktop.DBus";
     private const string DBUS_OBJECT = "/org/freedesktop/DBus";
 
+    private static string OBJECT_IFACE = "org.gnome.UPnP.MediaObject1";
+    private static string CONTAINER_IFACE = "org.gnome.UPnP.MediaContainer1";
+    private static string ITEM_IFACE = "org.gnome.UPnP.MediaItem1";
+
     private const string SERVICE_PREFIX = "org.gnome.UPnP.MediaServer1.";
 
     DBusObject      dbus_obj;
@@ -110,8 +114,99 @@ public class ExternalPluginFactory {
 
     private async void load_plugin (DBus.Connection connection,
                                     string          service_name) {
-        var plugin = yield ExternalPlugin.create (connection, service_name);
+        // org.gnome.UPnP.MediaServer1.NAME => /org/gnome/UPnP/MediaServer1/NAME
+        var root_object = "/" + service_name.replace (".", "/");
+
+        // Create proxy to MediaObject iface to get the display name through
+        var props = connection.get_object (service_name,
+                                           root_object)
+                                           as Properties;
+
+        HashTable<string,Value?> object_props;
+        HashTable<string,Value?> container_props;
+
+        try {
+            object_props = yield props.get_all (OBJECT_IFACE);
+            container_props = yield props.get_all (CONTAINER_IFACE);
+        } catch (DBus.Error err) {
+            warning ("Failed to fetch properties of plugin %s: %s.",
+                     service_name,
+                     err.message);
+
+            return;
+        }
+
+        var icon = yield fetch_icon (connection, service_name, container_props);
+
+        string title;
+        var value = object_props.lookup ("DisplayName");
+        if (value != null) {
+            title = value.get_string ();
+        } else {
+            title = service_name;
+        }
+
+        var plugin = new ExternalPlugin (service_name,
+                                         title,
+                                         root_object,
+                                         icon);
 
         this.loader.add_plugin (plugin);
     }
+
+    public async IconInfo? fetch_icon (DBus.Connection connection,
+                                       string          service_name,
+                                       HashTable<string,Value?>
+                                                       container_props) {
+        var value = container_props.lookup ("Icon");
+        if (value == null) {
+            // Seems no icon is provided, nevermind
+            return null;
+        }
+
+        var icon_path = value.get_string ();
+        var props = connection.get_object (service_name,
+                                           icon_path)
+                                           as Properties;
+
+        HashTable<string,Value?> item_props;
+        try {
+            item_props = yield props.get_all (ITEM_IFACE);
+        } catch (DBus.Error err) {
+            warning ("Error fetching icon properties from %s", service_name);
+
+            return null;
+        }
+
+        value = item_props.lookup ("MIMEType");
+        var icon = new IconInfo (value.get_string ());
+
+        value = item_props.lookup ("URLs");
+        weak string[] uris = (string[]) value.get_boxed ();
+        if (uris != null && uris[0] != null) {
+            icon.uri = uris[0];
+        }
+
+        value = item_props.lookup ("Size");
+        if (value != null) {
+            icon.size = value.get_int ();
+        }
+
+        value = item_props.lookup ("Width");
+        if (value != null) {
+            icon.width = value.get_int ();
+        }
+
+        value = item_props.lookup ("Height");
+        if (value != null) {
+            icon.height = value.get_int ();
+        }
+
+        value = item_props.lookup ("ColorDepth");
+        if (value != null) {
+            icon.depth = value.get_int ();
+        }
+
+        return icon;
+    }
 }
index 339fb7a..f38f4fe 100644 (file)
 using FreeDesktop;
 
 public class Rygel.ExternalPlugin : Rygel.Plugin {
-    private static string OBJECT_IFACE = "org.gnome.UPnP.MediaObject1";
-    private static string CONTAINER_IFACE = "org.gnome.UPnP.MediaContainer1";
-    private static string ITEM_IFACE = "org.gnome.UPnP.MediaItem1";
-
     public string service_name;
     public string root_object;
 
-    public static async ExternalPlugin? create (DBus.Connection connection,
-                                                string          service_name)
-                                                throws DBus.Error {
-        // org.gnome.UPnP.MediaServer1.NAME => /org/gnome/UPnP/MediaServer1/NAME
-        var root_object = "/" + service_name.replace (".", "/");
-
-        // Create proxy to MediaObject iface to get the display name through
-        var props = connection.get_object (service_name,
-                                           root_object)
-                                           as Properties;
-
-        var object_props = yield props.get_all (OBJECT_IFACE);
-        var container_props = yield props.get_all (CONTAINER_IFACE);
-
-        var icon = yield fetch_icon (connection, service_name, container_props);
-
-        string title;
-        var value = object_props.lookup ("DisplayName");
-        if (value != null) {
-            title = value.get_string ();
-        } else {
-            title = service_name;
-        }
-
-        return new ExternalPlugin (service_name,
-                                   title,
-                                   root_object,
-                                   icon);
-    }
-
     public ExternalPlugin (string    service_name,
                            string    title,
                            string    root_object,
@@ -76,60 +42,4 @@ public class Rygel.ExternalPlugin : Rygel.Plugin {
             this.add_icon (icon);
         }
     }
-
-    public static async IconInfo? fetch_icon (DBus.Connection connection,
-                                              string          service_name,
-                                              HashTable<string,Value?>
-                                                              container_props) {
-        var value = container_props.lookup ("Icon");
-        if (value == null) {
-            // Seems no icon is provided, nevermind
-            return null;
-        }
-
-        var icon_path = value.get_string ();
-        var props = connection.get_object (service_name,
-                                           icon_path)
-                                           as Properties;
-
-        HashTable<string,Value?> item_props;
-        try {
-            item_props = yield props.get_all (ITEM_IFACE);
-        } catch (DBus.Error err) {
-            warning ("Error fetching icon properties from %s", service_name);
-
-            return null;
-        }
-
-        value = item_props.lookup ("MIMEType");
-        var icon = new IconInfo (value.get_string ());
-
-        value = item_props.lookup ("URLs");
-        weak string[] uris = (string[]) value.get_boxed ();
-        if (uris != null && uris[0] != null) {
-            icon.uri = uris[0];
-        }
-
-        value = item_props.lookup ("Size");
-        if (value != null) {
-            icon.size = value.get_int ();
-        }
-
-        value = item_props.lookup ("Width");
-        if (value != null) {
-            icon.width = value.get_int ();
-        }
-
-        value = item_props.lookup ("Height");
-        if (value != null) {
-            icon.height = value.get_int ();
-        }
-
-        value = item_props.lookup ("ColorDepth");
-        if (value != null) {
-            icon.depth = value.get_int ();
-        }
-
-        return icon;
-    }
 }