Google plugin: store location list and selected location
authorPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 19 Nov 2009 12:45:57 +0000 (13:45 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 19 Nov 2009 15:37:16 +0000 (16:37 +0100)
src/plugins/google-plugin.vala

index a617e55..38be5c1 100644 (file)
@@ -21,6 +21,8 @@ using Hildon;
 
 class GooglePlugin : Plugin {
        List<MovieSource> sources;
+       List<string> 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<MovieSource> ();
                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<string> 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";
        }