[rygel] core: Reuse description file saving



commit 2dcc5e63a81b45c4eb4d7a6fbd14a642048bfa5b
Author: Jens Georg <mail jensge org>
Date:   Thu Dec 8 19:22:34 2011 +0100

    core: Reuse description file saving

 TODO                                     |    2 --
 src/rygel/rygel-description-file.vala    |    9 +++++++++
 src/rygel/rygel-root-device-factory.vala |   27 ++-------------------------
 3 files changed, 11 insertions(+), 27 deletions(-)
---
diff --git a/TODO b/TODO
index eccd876..992c026 100644
--- a/TODO
+++ b/TODO
@@ -100,8 +100,6 @@
 
   * Get rid of 'handle_error' in most places.
 
-  * save_modified_desc is in two classes.
-
   * Unit tests
     * HTTPGetHandler
       * HTTPIdentityHandler
diff --git a/src/rygel/rygel-description-file.vala b/src/rygel/rygel-description-file.vala
index 433b099..ee3e330 100644
--- a/src/rygel/rygel-description-file.vala
+++ b/src/rygel/rygel-description-file.vala
@@ -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
diff --git a/src/rygel/rygel-root-device-factory.vala b/src/rygel/rygel-root-device-factory.vala
index fa0155d..982ab75 100644
--- a/src/rygel/rygel-root-device-factory.vala
+++ b/src/rygel/rygel-root-device-factory.vala
@@ -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);



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