From: Philipp Zabel Date: Thu, 19 Nov 2009 12:45:57 +0000 (+0100) Subject: Google plugin: store location list and selected location X-Git-Tag: v0.0.6~9 X-Git-Url: http://git.maemo.org/git/?a=commitdiff_plain;h=928c86630fbc7b98b69e34ee307a5280d651183b;p=cinaest Google plugin: store location list and selected location --- diff --git a/src/plugins/google-plugin.vala b/src/plugins/google-plugin.vala index a617e55..38be5c1 100644 --- a/src/plugins/google-plugin.vala +++ b/src/plugins/google-plugin.vala @@ -21,6 +21,8 @@ using Hildon; class GooglePlugin : Plugin { List sources; + List locations; + string last_location; public override void hello (Gtk.Window window, Osso.Context context) { stdout.printf ("Google Plugin Loaded.\n"); @@ -30,6 +32,26 @@ class GooglePlugin : Plugin { sources = new List (); sources.append (source); + locations = null; + try { + var config_file = Path.build_filename (Environment.get_user_config_dir (), "cinaest", "cinaest.cfg"); + var keyfile = new KeyFile (); + if (keyfile.load_from_file (config_file, KeyFileFlags.NONE) + && keyfile.has_group ("GooglePlugin")) { + if (keyfile.has_key ("GooglePlugin", "KnownLocations")) { + var l = keyfile.get_string_list ("GooglePlugin", "KnownLocations"); + for (int i = 0; i < l.length; i++) + locations.append (l[i]); + } + if (keyfile.has_key ("GooglePlugin", "LastLocation")) { + source.location = last_location = keyfile.get_string ("GooglePlugin", "LastLocation"); + } + } + } catch (Error e) { + if (!(e is KeyFileError.NOT_FOUND)) + stdout.printf ("Error loading configuration: %s\n", e.message); + } + // FIXME - this forces the inclusion of config.h (void) Config.GETTEXT_PACKAGE; } @@ -51,7 +73,9 @@ class GooglePlugin : Plugin { dialog.set_title (_("Google plugin settings")); var selector = new TouchSelectorEntry.text (); - selector.append_text ("Berlin"); + insert_location_sorted (source.location); + foreach (string l in locations) + selector.append_text (l); var button = new PickerButton (SizeType.FINGER_HEIGHT, ButtonArrangement.HORIZONTAL); button.set_title (_("Location")); @@ -67,10 +91,61 @@ class GooglePlugin : Plugin { int res = dialog.run (); if (res == ResponseType.ACCEPT) { source.location = button.get_value (); + if (insert_location_sorted (source.location) || source.location != last_location) { + var config_dir = Path.build_filename (Environment.get_user_config_dir (), "cinaest"); + var config_file = Path.build_filename (config_dir, "cinaest.cfg"); + + // Make sure the directory is available + DirUtils.create_with_parents (config_dir, 0770); + + var keyfile = new KeyFile (); + try { + keyfile.load_from_file (config_file, KeyFileFlags.NONE); + } catch (Error e) { + if (!(e is KeyFileError.NOT_FOUND)) + stdout.printf ("Error loading configuration: %s\n", e.message); + } + var l = new string[locations.length ()]; + for (int i = 0; i < l.length; i++) { + l[i] = locations.nth_data (i); + } + keyfile.set_string_list ("GooglePlugin", "KnownLocations", l); + keyfile.set_string ("GooglePlugin", "LastLocation", source.location); + last_location = source.location; + + try { + var file = File.new_for_path (config_file + ".part"); + var stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null); + var data = keyfile.to_data (); + + stream.write (data, data.length, null); + FileUtils.rename (config_file + ".part", config_file); + } catch (Error e) { + stdout.printf ("Failed to store configuration: %s\n", e.message); + } + } } dialog.destroy (); } + private bool insert_location_sorted (string? location) { + if (location == null) + return false; + if (locations != null) { + for (unowned List l = locations.first (); l != null; l = l.next) { + if (l.data == location) { + return false; + } + if (l.data > location) { + l.insert (location, 0); + return true; + } + } + } + locations.append (location); + return true; + } + public override unowned string get_name () { return "Google"; }