[gnome-boxes] Use keyfiles for sources information



commit 254335d56c1c31ff2d4b907f26f347951f64ee8d
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Fri Oct 21 17:49:45 2011 +0200

    Use keyfiles for sources information

 data/Makefile.am                    |    3 ++
 data/QEMU_Session                   |    4 ++
 data/org.gnome.boxes.gschema.xml.in |   10 -----
 src/Makefile.am                     |    2 +-
 src/app.vala                        |   34 +++++++++++++++---
 src/collection.vala                 |   54 +++++++++++++++++++++++++++
 src/util.vala                       |   69 +++++++++++++++++++++++++++++++++--
 src/wizard.vala                     |   24 +++++++++++-
 8 files changed, 178 insertions(+), 22 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index 2f67bf1..131a472 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -13,6 +13,9 @@ gsettings_SCHEMAS = org.gnome.boxes.gschema.xml
 styledir         = $(datadir)/gnome-boxes/style
 style_DATA       = gtk-style.css
 
+sourcedir         = $(datadir)/gnome-boxes/sources
+source_DATA       = QEMU_Session
+
 iconsdir = $(pkgdatadir)/icons
 
 EXTRA_DIST =					\
diff --git a/data/QEMU_Session b/data/QEMU_Session
new file mode 100644
index 0000000..c3709d1
--- /dev/null
+++ b/data/QEMU_Session
@@ -0,0 +1,4 @@
+[source]
+name=QEMU Session
+type=libvirt
+uri=qemu+unix:///session
diff --git a/data/org.gnome.boxes.gschema.xml.in b/data/org.gnome.boxes.gschema.xml.in
index 16ad322..8c7b300 100644
--- a/data/org.gnome.boxes.gschema.xml.in
+++ b/data/org.gnome.boxes.gschema.xml.in
@@ -1,16 +1,6 @@
 <schemalist>
   <schema id="org.gnome.boxes" path="/org/gnome/boxes/" gettext-domain="gnome-boxes">
 
-    <key name="broker-uris" type="as">
-      <summary>Broker URIs</summary>
-      <description>
-        List broker URIs to connect to. A broker URI can only be a
-        libvirt URI for now. The default value is:
-        qemu+unix:///session.
-      </description>
-      <default>[ 'qemu+unix:///session' ]</default>
-    </key>
-
     <key name="collections" type="as">
       <summary>Collections</summary>
       <description>
diff --git a/src/Makefile.am b/src/Makefile.am
index 5935a97..cd3cca2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -13,11 +13,11 @@ AM_VALAFLAGS =						\
 	--pkg clutter-gtk-1.0				\
 	--pkg cogl-1.0					\
 	--pkg config					\
-	--pkg posix					\
 	--pkg gdk-pixbuf-2.0				\
 	--pkg glib-2.0					\
 	--pkg libvirt-gobject-1.0			\
 	--pkg libxml-2.0				\
+	--pkg posix					\
 	--pkg spice-client-gtk-3.0			\
 	$(NULL)
 
diff --git a/src/app.vala b/src/app.vala
index 2ebddf6..f0423c7 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -57,14 +57,14 @@ private class Boxes.App: Boxes.UI {
             view.add_item (item);
         });
 
-        setup_brokers ();
+        setup_sources.begin ();
     }
 
     public void set_category (Category category) {
         topbar.label.set_text (category.name);
     }
 
-    private async void setup_broker (string uri) {
+    private async void setup_libvirt (string uri) {
         var connection = new GVir.Connection (uri);
 
         try {
@@ -80,9 +80,33 @@ private class Boxes.App: Boxes.UI {
         }
     }
 
-    private async void setup_brokers () {
-        foreach (var uri in settings.get_strv ("broker-uris")) {
-            setup_broker.begin (uri);
+    private async void setup_sources () {
+        if (!has_pkgconfig_sources ()) {
+            var src = File.new_for_path (get_pkgdata_source ("QEMU_Session"));
+            var dst = File.new_for_path (get_pkgconfig_source ("QEMU Session"));
+            try {
+                yield src.copy_async (dst, FileCopyFlags.NONE);
+            } catch (GLib.Error e) {
+                critical ("Can't setup default sources: %s", e.message);
+            }
+        }
+
+        var dir = File.new_for_path (get_pkgconfig_source ());
+        try {
+            var e = yield dir.enumerate_children_async (FILE_ATTRIBUTE_STANDARD_NAME,
+                                                        0, Priority.DEFAULT);
+            while (true) {
+                var files = yield e.next_files_async (10, Priority.DEFAULT);
+                if (files == null)
+                    break;
+                foreach (var file in files) {
+                    var source = new CollectionSource.with_file (file.get_name ());
+                    if (source.source_type == "libvirt")
+                        setup_libvirt (source.uri);
+                }
+            }
+        } catch (GLib.Error e) {
+            warning (e.message);
         }
     }
 
diff --git a/src/collection.vala b/src/collection.vala
index c7a6b6e..4ac3a86 100644
--- a/src/collection.vala
+++ b/src/collection.vala
@@ -24,6 +24,60 @@ private class Boxes.Collection: GLib.Object {
     }
 }
 
+private class Boxes.CollectionSource: GLib.Object {
+    private KeyFile keyfile;
+    public string? name {
+        owned get { return get_string ("source", "name"); }
+        set { keyfile.set_string ("source", "name", value); }
+    }
+    public string? source_type {
+        owned get { return get_string ("source", "type"); }
+        set { keyfile.set_string ("source", "type", value); }
+    }
+    public string? uri {
+        owned get { return get_string ("source", "uri"); }
+        set { keyfile.set_string ("source", "uri", value); }
+    }
+    private string? filename;
+
+    construct {
+        keyfile = new KeyFile ();
+    }
+
+    public CollectionSource (string name, string source_type, string uri) {
+        this.name = name;
+        this.source_type = source_type;
+        this.uri = uri;
+    }
+
+    public CollectionSource.with_file (string filename) throws GLib.Error {
+        this.filename = filename;
+        load ();
+    }
+
+    private void load () throws GLib.Error {
+        keyfile.load_from_file (get_pkgconfig_source (filename),
+                                KeyFileFlags.KEEP_COMMENTS | KeyFileFlags.KEEP_TRANSLATIONS);
+    }
+
+    private void save () {
+        if (filename == null) {
+            filename = make_filename (name);
+            keyfile_save (keyfile, get_pkgconfig_source (filename));
+        } else {
+            keyfile_save (keyfile, get_pkgconfig_source (filename), true);
+        }
+    }
+
+    private string? get_string (string group, string key) {
+        try {
+            return keyfile.get_string (group, key);
+        } catch (GLib.KeyFileError e) {
+            return null;
+        }
+    }
+}
+
 private class Boxes.Category: GLib.Object {
     public string name;
 
diff --git a/src/util.vala b/src/util.vala
index ff5a25b..8bcbe1c 100644
--- a/src/util.vala
+++ b/src/util.vala
@@ -21,18 +21,47 @@ namespace Boxes {
         return Path.build_filename (get_pkgdata (), "pixmaps", file_name);
     }
 
+    private string get_pkgdata_source (string? file_name = null) {
+        return Path.build_filename (get_pkgdata (), "sources", file_name);
+    }
+
     private string get_pkgcache (string? file_name = null) {
         var dir = Path.build_filename (Environment.get_user_cache_dir (), Config.PACKAGE_TARNAME);
 
+        ensure_directory (dir);
+
+        return Path.build_filename (dir, file_name);
+    }
+
+    private string get_pkgconfig (string? file_name = null) {
+        var dir = Path.build_filename (Environment.get_user_config_dir (), Config.PACKAGE_TARNAME);
+
+        ensure_directory (dir);
+
+        return Path.build_filename (dir, file_name);
+    }
+
+    private bool has_pkgconfig_sources () {
+        return FileUtils.test (Path.build_filename (get_pkgconfig (), "sources"), FileTest.IS_DIR);
+    }
+
+    private string get_pkgconfig_source (string? file_name = null) {
+        var dir = Path.build_filename (get_pkgconfig (), "sources");
+
+        ensure_directory (dir);
+
+        return Path.build_filename (dir, file_name);
+    }
+
+    private void ensure_directory (string dir) {
         try {
             var file = GLib.File.new_for_path (dir);
             file.make_directory_with_parents (null);
         } catch (GLib.Error e) {
-            if (!(e is IOError.EXISTS))
-                warning (e.message);
+            if (e is IOError.EXISTS)
+                return;
+            warning (e.message);
         }
-
-        return Path.build_filename (dir, file_name);
     }
 
     private Clutter.Color gdk_rgba_to_clutter_color (Gdk.RGBA gdk_rgba) {
@@ -115,6 +144,38 @@ namespace Boxes {
         return obj->stringval;
     }
 
+    private bool keyfile_save (KeyFile key_file, string file_name, bool overwrite = false) {
+        if (!overwrite && FileUtils.test (file_name, FileTest.EXISTS))
+            return false;
+
+        var file = FileStream.open (file_name, "w");
+        var data = key_file.to_data (null);
+        file.puts (data);
+
+        return true;
+    }
+
+    public string replace_regex (string str, string old, string replacement) {
+        try {
+            var regex = new GLib.Regex (old);
+            return regex.replace_literal (str, -1, 0, replacement);
+        } catch (GLib.RegexError e) {
+            critical (e.message);
+            return str;
+        }
+    }
+
+    private string make_filename (string name) {
+        var filename = replace_regex (name, "[\\\\/:()<>|?*]", "_");
+
+        var tryname = filename;
+        for (var i = 0; FileUtils.test (tryname, FileTest.EXISTS); i++) {
+            tryname =  "%s-%d".printf (filename, i);
+        }
+
+        return tryname;
+    }
+
     private void actor_add (Clutter.Actor actor, Clutter.Container container) {
         if (actor.get_parent () == (Clutter.Actor) container)
             return;
diff --git a/src/wizard.vala b/src/wizard.vala
index 81bff77..dfe9a6b 100644
--- a/src/wizard.vala
+++ b/src/wizard.vala
@@ -32,6 +32,7 @@ private class Boxes.Source: GLib.Object {
     }
 
     private Gtk.Notebook notebook;
+    public Gtk.Entry url_entry;
 
     public Source () {
         notebook = new Gtk.Notebook ();
@@ -76,8 +77,8 @@ private class Boxes.Source: GLib.Object {
         separator.height_request = 5;
         vbox.pack_start (separator, false, false);
         hbox = add_entry (vbox);
-        var entry = new Gtk.Entry ();
-        hbox.add (entry);
+        url_entry = new Gtk.Entry ();
+        hbox.add (url_entry);
         hbox = add_entry (vbox);
         var image = new Gtk.Image.from_icon_name ("network-workgroup", 0);
         // var image = new Gtk.Image.from_icon_name ("krfb", 0);
@@ -160,6 +161,8 @@ private class Boxes.Wizard: Boxes.UI {
         get { return _page; }
         set {
             if (value == WizardPage.LAST) {
+                if (!create ())
+                    return;
                 app.ui_state = UIState.COLLECTION;
             }
 
@@ -196,6 +199,23 @@ private class Boxes.Wizard: Boxes.UI {
         setup_ui ();
     }
 
+    private bool create () {
+        if (this.source.page == Boxes.SourcePage.URL) {
+            var text = this.source.url_entry.get_text ();
+
+            bool uncertain;
+            var type = ContentType.guess (text, null, out uncertain);
+            if (uncertain) {
+                var uri = Xml.URI.parse (text);
+                if (uri.scheme == "spice" || uri.scheme == "vnc") {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
     private void add_step (Gtk.Widget widget, string label, WizardPage page) {
         notebook.append_page (widget, null);
 



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