[tracker/needle: 43/43] tracker-needle: Added Config class to remember search history between instances



commit 11dd99ce3eb58e46a477158b5fb2a96bd1828adc
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Sep 28 21:23:26 2010 +0100

    tracker-needle: Added Config class to remember search history between instances

 src/tracker-needle/Makefile.am         |    1 +
 src/tracker-needle/tracker-config.vala |   90 ++++++++++++++++++++++++++++++++
 src/tracker-needle/tracker-needle.vala |   23 +++++++--
 3 files changed, 110 insertions(+), 4 deletions(-)
---
diff --git a/src/tracker-needle/Makefile.am b/src/tracker-needle/Makefile.am
index bcd03ac..ef3e4d7 100644
--- a/src/tracker-needle/Makefile.am
+++ b/src/tracker-needle/Makefile.am
@@ -4,6 +4,7 @@ bin_PROGRAMS = tracker-needle
 
 tracker_needle_VALASOURCES = 					\
 	tracker-cell-renderer-text.vala				\
+	tracker-config.vala \
 	tracker-needle.vala \
 	tracker-query.vala \
 	tracker-stats.vala \
diff --git a/src/tracker-needle/tracker-config.vala b/src/tracker-needle/tracker-config.vala
new file mode 100644
index 0000000..93ad5ae
--- /dev/null
+++ b/src/tracker-needle/tracker-config.vala
@@ -0,0 +1,90 @@
+//
+// Copyright 2010, Martyn Russell <martyn lanedo 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 2
+// 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, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301, USA.
+//
+
+public class Tracker.Config {
+	private KeyFile data;
+	private string filename;
+	private string[] history;
+
+	public Config () {
+		debug ("Loading config");
+
+		data = new KeyFile ();
+		filename = Path.build_filename (Environment.get_home_dir (), ".config", "tracker", "tracker-needle.cfg", null);
+
+		try {
+			data.load_from_file (filename, KeyFileFlags.KEEP_COMMENTS | KeyFileFlags.KEEP_TRANSLATIONS);
+		} catch (KeyFileError e1) {
+			warning ("Could not load config from file:'%s': %s", filename, e1.message);
+			return;
+		} catch (FileError e2) {
+			warning ("Could not load config from file:'%s': %s", filename, e2.message);
+			return;
+		}
+
+		if (data.has_group ("History") == false) {
+			debug ("  No history found");
+			return;
+		}
+
+		try {
+			history = data.get_string_list ("History", "criteria");
+		} catch (KeyFileError e1) {
+			warning ("Could not load config from file:'%s': %s", filename, e1.message);
+			return;
+		}
+
+		debug ("  Found %d previous search histories", history.length);
+
+		debug ("  Done");
+	}
+
+	~Config () {
+		debug ("Saving config");
+
+		data.set_string_list ("History", "criteria", history);
+
+		try {
+			string output = data.to_data ();
+
+			FileUtils.set_contents (filename, output, -1);
+		} catch (GLib.FileError e1) {
+			warning ("Could not save config to file:'%s': %s", filename, e1.message);
+		}
+
+		debug ("  Done");
+	}
+
+	public void add_history (string criteria)
+	requires (criteria != null && criteria.length > 0) {
+		// Don't add the same item more than once
+		foreach (string check in history) {
+			if (check == criteria) {
+				return;
+			}
+		}
+
+		history += criteria;
+	}
+
+	public string[] get_history () {
+		return history;
+	}
+
+}
diff --git a/src/tracker-needle/tracker-needle.vala b/src/tracker-needle/tracker-needle.vala
index c68cdda..bf3bc15 100644
--- a/src/tracker-needle/tracker-needle.vala
+++ b/src/tracker-needle/tracker-needle.vala
@@ -20,6 +20,7 @@
 using Gtk;
 using Tracker.Sparql;
 using Tracker.View;
+using Tracker.Config;
 
 [CCode (cname = "TRACKER_UI_DIR")]
 extern static const string UIDIR;
@@ -27,8 +28,9 @@ extern static const string UIDIR;
 [CCode (cname = "SRCDIR")]
 extern static const string SRCDIR;
 
-public class TrackerNeedle {
+public class Tracker.Needle {
 	private const string UI_FILE = "tracker-needle.ui";
+	private Tracker.Config config;
 	private Window window;
 	private ToggleToolButton view_list;
 	private ToggleToolButton view_icons;
@@ -55,6 +57,10 @@ public class TrackerNeedle {
 	static bool current_view = true;
 	static bool current_find_in = true;
 
+	public Needle () {
+		config = new Tracker.Config ();
+	}
+
 	public void show () {
 		setup_ui ();
 
@@ -110,6 +116,7 @@ public class TrackerNeedle {
 		search_list = builder.get_object ("comboboxentry_search") as ComboBoxEntry;
 		search = search_list.get_child () as Entry;
 		search.changed.connect (search_changed);
+		search_history_insert (config.get_history ());
 
 		spinner = new Spinner ();
 		spinner_shell = builder.get_object ("toolcustom_spinner") as ToolItem;
@@ -468,7 +475,7 @@ public class TrackerNeedle {
 		spinner_shell.hide ();
 	}
 
-	private TreeIter? search_ran_before (string criteria, bool? add_to_model = false) {
+	private TreeIter? search_history_find_or_insert (string criteria, bool? add_to_model = false) {
 		if (criteria.length < 1) {
 			return null;
 		}
@@ -500,11 +507,19 @@ public class TrackerNeedle {
 			ListStore store = (ListStore) model;
 			store.prepend (out new_iter);
 			store.set (new_iter, 0, criteria, -1);
+
+			config.add_history (criteria);
 		}
 
 		return null;
 	}
 
+	private void search_history_insert (string[] history) {
+		foreach (string criteria in history) {
+			search_history_find_or_insert (criteria, true);
+		}
+	}
+
 	private bool search_run () {
 		last_search_id = 0;
 
@@ -522,7 +537,7 @@ public class TrackerNeedle {
 			return false;
 		}
 
-		search_ran_before (criteria, true);
+		search_history_find_or_insert (criteria, true);
 
 		// Show correct window
 		bool rows = view_list.active || view_details.active;
@@ -646,7 +661,7 @@ static int main (string[] args) {
 	Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
 	Intl.textdomain (Config.GETTEXT_PACKAGE);
 
-	TrackerNeedle n = new TrackerNeedle ();
+	Tracker.Needle n = new Tracker.Needle ();
 	n.show();
 	Gtk.main ();
 



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