core: Reuse description file saving
authorJens Georg <mail@jensge.org>
Thu, 8 Dec 2011 18:22:34 +0000 (19:22 +0100)
committerJens Georg <mail@jensge.org>
Wed, 14 Dec 2011 12:55:52 +0000 (13:55 +0100)
TODO
src/rygel/rygel-description-file.vala
src/rygel/rygel-root-device-factory.vala

diff --git a/TODO b/TODO
index eccd876..992c026 100644 (file)
--- a/TODO
+++ b/TODO
 
   * Get rid of 'handle_error' in most places.
 
-  * save_modified_desc is in two classes.
-
   * Unit tests
     * HTTPGetHandler
       * HTTPIdentityHandler
index 433b099..ee3e330 100644 (file)
@@ -50,6 +50,15 @@ internal class DescriptionFile : Object {
     }
 
     /**
+     * Constructor which wraps an existing GUPnP.XMLDoc as a description file.
+     *
+     * @param doc is the GUPnP.XMLDoc to wrap.
+     */
+    public DescriptionFile.from_xml_document (XMLDoc doc) {
+        this.doc = doc;
+    }
+
+    /**
      * Change the type of a service.
      *
      * Usually used to modify the device version, e.g. default device type is
index fa0155d..982ab75 100644 (file)
@@ -93,7 +93,8 @@ internal class Rygel.RootDeviceFactory {
         /* Modify description to include Plugin-specific stuff */
         this.prepare_desc_for_plugin (doc, plugin);
 
-        save_modified_desc (doc, desc_path);
+        var file = new DescriptionFile.from_xml_document (doc);
+        file.save (desc_path);
 
         return doc;
     }
@@ -289,30 +290,6 @@ internal class Rygel.RootDeviceFactory {
         }
     }
 
-    private void save_modified_desc (XMLDoc doc,
-                                     string desc_path) throws GLib.Error {
-        var file = FileStream.open (desc_path, "w+");
-
-        if (unlikely (file == null)) {
-            var message = _("Failed to write modified description to %s");
-
-            throw new IOError.FAILED (message, desc_path);
-        }
-
-        string mem = null;
-        int len = -1;
-        doc.doc.dump_memory_enc (out mem, out len, "UTF-8");
-
-        if (unlikely (len <= 0)) {
-            var message = _("Failed to write modified description to %s");
-
-            throw new IOError.FAILED (message, desc_path);
-        }
-
-        // Make sure we don't have any newlines
-        file.puts (mem.replace ("\n", ""));
-    }
-
     private XMLDoc get_latest_doc (string path1,
                                    string path2) throws GLib.Error {
         var file = File.new_for_path (path1);