[vinagre/vala-rewrite: 2/3] Rewrite cache settings handling in Vala



commit df7879ff1b610deabf7dd42922117dbb1a60e4b1
Author: David King <amigadave amigadave com>
Date:   Thu Jun 16 18:53:28 2011 +0200

    Rewrite cache settings handling in Vala

 Makefile.am                         |    3 +-
 vinagre/vinagre-cache-prefs.c       |  156 -----------------------------------
 vinagre/vinagre-cache-prefs.h       |   42 ---------
 vinagre/vinagre-cache-settings.vala |  119 ++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 200 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 1d197a4..c2d4549 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -60,7 +60,6 @@ noinst_vinagreh_headers = \
 	vinagre/vinagre-bookmarks-migration.h \
 	vinagre/vinagre-bookmarks-tree.h \
 	vinagre/vinagre-bookmarks-ui.h \
-	vinagre/vinagre-cache-prefs.h \
 	vinagre/vinagre-commands.h \
 	vinagre/vinagre-connect.h \
 	vinagre/vinagre-connection.h \
@@ -91,6 +90,7 @@ handwritten_sources = \
 	vinagre/vinagre-bookmarks-migration.c \
 	vinagre/vinagre-bookmarks-tree.c \
 	vinagre/vinagre-bookmarks-ui.c \
+	vinagre/vinagre-cache-settings.vala \
 	vinagre/vinagre-commands.c \
 	vinagre/vinagre-connect.c \
 	vinagre/vinagre-connection.c \
@@ -106,7 +106,6 @@ handwritten_sources = \
 	vinagre/vinagre-window.c \
 	vinagre/pty_open.c \
 	vinagre/vinagre-ssh.c \
-	vinagre/vinagre-cache-prefs.c \
 	vinagre/vinagre-protocol.c \
 	vinagre/vinagre-plugins-engine.c \
 	$(ifaddrs_sources)
diff --git a/vinagre/vinagre-cache-settings.vala b/vinagre/vinagre-cache-settings.vala
new file mode 100644
index 0000000..42f2a09
--- /dev/null
+++ b/vinagre/vinagre-cache-settings.vala
@@ -0,0 +1,119 @@
+/*  Vinagre - GNOME Remote Desktop viewer
+ *
+ *  Copyright (C) 2011  David King <amigadave amigadave com>
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Store some settings that are safe to destroy, in the cache directory.
+class Vinagre.CacheSettings : Object {
+    private KeyFile keyfile;
+    private string cachefile_path;
+
+    public CacheSettings () {
+        keyfile = new KeyFile ();
+	// The filename used to end in .ini, but a KeyFile is not identical.
+        cachefile_path = FileUtils.build_filename (Dirs.get_user_cache_dir (),
+            "vinagre-prefs-cache.keyfile");
+
+        // TODO: Better error handling.
+        try {
+            keyfile.load_from_file (path, KeyFileFlags.NONE);
+        } catch (Error error) {
+            warning ("Error loading cache preferences: %s", error.message);
+        }
+    }
+
+    // Save the cache preferences.
+    public ~CacheSettings () {
+        // Read-write permissions.
+        var result = DirUtils.create_with_parents
+            (Path.get_dirname (cachefile_path), 600);
+
+        // FIXME: How does one use errno from Vala?
+        if (result != 0) {
+            warning ("Error creating cache preferences directory. Unable to save cache preferences.");
+            return;
+        }
+
+        // KeyFile.to_data () never returns an error.
+        // TODO: Use length return value?
+        var keyfile_data = keyfile.to_data (null, null);
+        try {
+            FileUtils.set_contents (cachefile_path, keyfile_data);
+        } catch (Error error) {
+            warning ("Error while saving cache preferences: %s", error.message);
+        }
+    }
+
+    public bool get_boolean (const string group, const string key,
+        bool default_value) {
+        return_val_if_fail (keyfile != null, false);
+
+        try {
+            var result = keyfile.get_boolean (group, key);
+        } catch (Error error) {
+            return default_value;
+        }
+
+        return result;
+    }
+
+    public void set_boolean (const string group, const string key,
+        bool @value) {
+        return_val_if_fail (keyfile != null, false);
+
+        keyfile.set_boolean (group, key, @value);
+    }
+
+    public string get_string (const string group, const string key,
+        const string default_value) {
+        return_if_fail (keyfile != null);
+
+        try {
+            var result = keyfile.get_string (group, key);
+        } catch (Error error) {
+            return default_value;
+        }
+
+        return result;
+    }
+
+    public void set_string (const string group, const string key,
+        const string @value) {
+        return_if_fail (keyfile != null);
+
+        keyfile.set_string (group, key, @value);
+    }
+
+    public int get_int (const string group, const string key,
+        const int default_value) {
+        return_val_if_fail (keyfile != null, 0);
+
+        try {
+            var result = keyfile.get_integer (group, key);
+        } catch (Error error) {
+            return default_value;
+        }
+
+        return result;
+    }
+
+    public void set_int (const string group, const string key,
+        const int @value) {
+        return_if_fail (keyfile != null);
+
+        keyfile.set_integer (group, key, @value);
+    }
+}



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