Move bridge relay configuration dialog into separate file
authorPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 22 Apr 2010 12:46:32 +0000 (14:46 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 23 Apr 2010 12:22:53 +0000 (14:22 +0200)
Makefile.am
po/POTFILES.in
src/bridge-dialog.vala [new file with mode: 0644]
src/status-area-applet-tor.vala

index 8887f6f..2a86f5d 100644 (file)
@@ -23,11 +23,13 @@ icon18_DATA = \
 
 status_area_applet_tor_la_SOURCES = \
        src/status-area-applet-tor.c \
+       src/bridge-dialog.c \
        src/torcontrol.c \
        src/torcontrol-socket.c
 
 status_area_applet_tor_la_VALASOURCES = \
        src/status-area-applet-tor.vala \
+       src/bridge-dialog.vala \
        src/torcontrol.vala
 
 src/status-area-applet-tor.c: ${status_area_applet_tor_la_VALASOURCES}
@@ -45,4 +47,5 @@ ACLOCAL_AMFLAGS = -Im4
 
 CLEANFILES = \
         src/status-area-applet-tor.c \
+       src/bridge-dialog.c \
         src/torcontrol.c
index cbb43d3..2f789fa 100644 (file)
@@ -1 +1,2 @@
 src/status-area-applet-tor.vala
+src/bridge-dialog.vala
diff --git a/src/bridge-dialog.vala b/src/bridge-dialog.vala
new file mode 100644 (file)
index 0000000..b84ac97
--- /dev/null
@@ -0,0 +1,209 @@
+class BridgeDialog : Gtk.Dialog {
+       private const string GCONF_DIR_TOR     = "/apps/maemo/tor";
+       private const string GCONF_KEY_BRIDGES = GCONF_DIR_TOR + "/bridges";
+
+       GConf.Client gconf;
+       Gtk.ListStore list_store;
+
+       /**
+        * Show the bridge relay configuration dialog
+        */
+       private const int RESPONSE_NEW = 1;
+       public BridgeDialog () {
+               var content = (Gtk.VBox) get_content_area ();
+                content.set_size_request (-1, 5*70);
+
+               set_title (_("Bridge relays"));
+
+               gconf = GConf.Client.get_default ();
+               var bridges = new SList<string> ();
+               try {
+                       bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
+               } catch (Error e) {
+                       Hildon.Banner.show_information (null, null, "Error loading bridges: %s".printf (e.message));
+               }
+
+               list_store = new Gtk.ListStore (1, typeof (string));
+               Gtk.TreeIter iter;
+               foreach (string bridge in bridges) {
+                       list_store.append (out iter);
+                       list_store.@set (iter, 0, bridge);
+               }
+
+               var pannable_area = new Hildon.PannableArea ();
+               var tree_view = new Gtk.TreeView.with_model (list_store);
+               var renderer = new Gtk.CellRendererText ();
+               var column = new Gtk.TreeViewColumn.with_attributes ("IP", renderer, "text", 0);
+               tree_view.append_column (column);
+               pannable_area.add (tree_view);
+               content.pack_start (pannable_area, true, true, 0);
+
+               tree_view.row_activated.connect ((path, column) => {
+                       bridge_edit_dialog (list_store, path);
+               });
+
+               add_button (_("New"), RESPONSE_NEW);
+               response.connect ((response_id) => {
+                       if (response_id == RESPONSE_NEW) {
+                               bridge_edit_dialog (list_store, null);
+                       }
+               });
+
+               content.show_all ();
+       }
+
+       /**
+        * Show the bridge relay edit dialog
+        */
+       private const int RESPONSE_DELETE = 1;
+       private void bridge_edit_dialog (Gtk.ListStore store, Gtk.TreePath? path) {
+               var dialog = new Gtk.Dialog ();
+               var content = (Gtk.VBox) dialog.get_content_area ();
+
+               if (path == null)
+                       dialog.set_title (_("New bridge relay"));
+               else
+                       dialog.set_title (_("Edit bridge relay"));
+
+               var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
+
+               var hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
+               var label = new Gtk.Label (_("IP address"));
+               label.set_alignment (0, 0.5f);
+               size_group.add_widget (label);
+               hbox.pack_start (label, false, false, 0);
+               var ip_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
+               ip_entry.set ("hildon-input-mode", Hildon.GtkInputMode.NUMERIC |
+                                                  Hildon.GtkInputMode.SPECIAL);
+               hbox.pack_start (ip_entry, true, true, 0);
+               content.pack_start (hbox, false, false, 0);
+
+               hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
+               label = new Gtk.Label (_("Port"));
+               label.set_alignment (0, 0.5f);
+               size_group.add_widget (label);
+               hbox.pack_start (label, false, false, 0);
+               var port_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
+               port_entry.set ("hildon-input-mode", Hildon.GtkInputMode.NUMERIC);
+               hbox.pack_start (port_entry, true, true, 0);
+               content.pack_start (hbox, true, true, 0);
+
+               hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
+               label = new Gtk.Label (_("Fingerprint"));
+               label.set_alignment (0, 0.5f);
+               size_group.add_widget (label);
+               hbox.pack_start (label, false, false, 0);
+               var fingerprint_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
+               fingerprint_entry.set ("hildon-input-mode", Hildon.GtkInputMode.HEXA);
+               hbox.pack_start (fingerprint_entry, true, true, 0);
+               content.pack_start (hbox, true, true, 0);
+
+               var iter = Gtk.TreeIter ();
+               if (path == null) {
+                       port_entry.set_text ("443");
+               } else if (store.get_iter (out iter, path)) {
+                       string tmp;
+                       store.@get (iter, 0, out tmp);
+                       string[] ip_port = tmp.split (":");
+                       if (ip_port.length == 2) {
+                               ip_entry.set_text (ip_port[0]);
+                               port_entry.set_text (ip_port[1]);
+                       }
+
+                       dialog.add_button (_("Delete"), RESPONSE_DELETE);
+               }
+               dialog.add_button (_("Save"), Gtk.ResponseType.OK);
+               dialog.response.connect ((response_id) => {
+                       var bridges = new SList<string> ();
+
+                       if (response_id == RESPONSE_DELETE) {
+                               if (path != null) {
+                                       Gtk.TreeIter iter2;
+                                       store.get_iter (out iter2, path);
+                                       store.remove (iter2);
+                                       string bridge;
+                                       if (store.get_iter_first (out iter2)) do {
+                                               store.@get (iter2, 0, out bridge);
+                                               bridges.append (bridge);
+                                       } while (store.iter_next (ref iter2));
+                                       try {
+                                               gconf.set_list (GCONF_KEY_BRIDGES,
+                                                               GConf.ValueType.STRING,
+                                                               bridges);
+                                       } catch (Error e) {
+                                               Hildon.Banner.show_information (dialog, null,
+                                                                               "Failed to save bridge relay list: %s".printf (e.message));
+                                       }
+                               }
+                               dialog.destroy ();
+                       }
+                       if (response_id == Gtk.ResponseType.OK) {
+                               if (!is_valid_ip_address (ip_entry.get_text ())) {
+                                       Hildon.Banner.show_information (dialog, null,
+                                                                       _("Invalid IP address"));
+                                       return;
+                               }
+                               int port = port_entry.get_text ().to_int ();
+                               if (port < 0 || port > 65565) {
+                                       Hildon.Banner.show_information (dialog, null,
+                                                                       _("Invalid port number"));
+                                       return;
+                               }
+                               Gtk.TreeIter iter2;
+                               if (path == null) {
+                                       store.append (out iter2);
+                               } else {
+                                       store.get_iter (out iter2, path);
+                               }
+                               store.@set (iter2, 0, "%s:%d".printf (ip_entry.get_text (), port));
+                               try {
+                                       bridges = gconf.get_list (GCONF_KEY_BRIDGES,
+                                                                 GConf.ValueType.STRING);
+                               } catch (Error e) {
+                                       Hildon.Banner.show_information (null, null,
+                                                                       "Error loading bridges: %s".printf (e.message));
+                               }
+                               if (path == null) {
+                                       bridges.append ("%s:%d".printf (ip_entry.get_text (), port));
+                               } else {
+                                       bridges = null;
+                                       string bridge;
+                                       if (store.get_iter_first (out iter2)) do {
+                                               store.@get (iter2, 0, out bridge);
+                                               bridges.append (bridge);
+                                       } while (store.iter_next (ref iter2));
+                               }
+                               try {
+                                       gconf.set_list (GCONF_KEY_BRIDGES,
+                                                       GConf.ValueType.STRING,
+                                                       bridges);
+                               } catch (Error e) {
+                                               Hildon.Banner.show_information (dialog, null,
+                                                                               "Failed to save bridge relay list: %s".printf (e.message));
+                               }
+
+                               dialog.destroy ();
+                       }
+               });
+
+               dialog.show_all ();
+       }
+
+       /**
+        * Check whether the IP address consists of four numbers in the 0..255 range
+        */
+       bool is_valid_ip_address (string address) {
+               string[] ip = address.split (".");
+
+               if (ip.length != 4)
+                       return false;
+
+               for (int i = 0; i < ip.length; i++) {
+                       int n = ip[i].to_int ();
+                       if (n < 0 || n > 255)
+                               return false;
+               }
+
+               return true;
+       }
+}
index e2ff9d5..083d465 100644 (file)
@@ -174,25 +174,22 @@ class TorStatusMenuItem : HD.StatusMenuItem {
        private async void tor_control_auth () throws Error {
                yield tor_control.authenticate_async (password);
 
-               var bridges = new SList<string> ();
                try {
-                       bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
-               } catch (Error e) {
-                       error ("Error loading bridges: %s", e.message);
-                       return;
-               }
-
-               if (bridges.length () <= 0)
-                       return;
+                       var bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
 
-               // Enable bridge relays
-               tor_control.set_conf_list ("Bridge", bridges);
-               tor_control.set_conf_bool ("UseBridges", true);
+                       if (bridges.length () > 0) {
+                               // Enable bridge relays
+                               tor_control.set_conf_list ("Bridge", bridges);
+                               tor_control.set_conf_bool ("UseBridges", true);
 
-               bool use = yield tor_control.get_conf_bool_async ("UseBridges");
-               if (!use) {
-                       Hildon.Banner.show_information (null, null,
-                                                       "Failed to set up bridge relays");
+                               bool use = yield tor_control.get_conf_bool_async ("UseBridges");
+                               if (!use) {
+                                       Hildon.Banner.show_information (null, null,
+                                                                       "Failed to set up bridge relays");
+                               }
+                       }
+               } catch (Error e) {
+                       error ("Error loading bridges: %s", e.message);
                }
        }
 
@@ -355,185 +352,9 @@ class TorStatusMenuItem : HD.StatusMenuItem {
        /**
         * Show the bridge relay configuration dialog
         */
-       private const int RESPONSE_NEW = 1;
        private void bridges_clicked_cb () {
-               var dialog = new Gtk.Dialog ();
-               var content = (Gtk.VBox) dialog.get_content_area ();
-                content.set_size_request (-1, 5*70);
-
-               dialog.set_title (_("Bridge relays"));
-
-               var bridges = new SList<string> ();
-               try {
-                       bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
-               } catch (Error e) {
-                       Hildon.Banner.show_information (null, null, "Error loading bridges: %s".printf (e.message));
-               }
-
-               var list_store = new Gtk.ListStore (1, typeof (string));
-               Gtk.TreeIter iter;
-               foreach (string bridge in bridges) {
-                       list_store.append (out iter);
-                       list_store.@set (iter, 0, bridge);
-               }
-
-               var pannable_area = new Hildon.PannableArea ();
-               var tree_view = new Gtk.TreeView.with_model (list_store);
-               var renderer = new Gtk.CellRendererText ();
-               var column = new Gtk.TreeViewColumn.with_attributes ("IP", renderer, "text", 0);
-               tree_view.append_column (column);
-               pannable_area.add (tree_view);
-               content.pack_start (pannable_area, true, true, 0);
-
-               tree_view.row_activated.connect ((path, column) => {
-                       bridge_edit_dialog (list_store, path);
-               });
-
-               dialog.add_button (_("New"), RESPONSE_NEW);
-               dialog.response.connect ((response_id) => {
-                       if (response_id == RESPONSE_NEW) {
-                               bridge_edit_dialog (list_store, null);
-                       }
-               });
-
-               dialog.show_all ();
-       }
-
-       /**
-        * Show the bridge relay edit dialog
-        */
-       private const int RESPONSE_DELETE = 1;
-       private void bridge_edit_dialog (Gtk.ListStore store, Gtk.TreePath? path) {
-               var dialog = new Gtk.Dialog ();
-               var content = (Gtk.VBox) dialog.get_content_area ();
-
-               if (path == null)
-                       dialog.set_title (_("New bridge relay"));
-               else
-                       dialog.set_title (_("Edit bridge relay"));
-
-               var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
-
-               var hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
-               var label = new Gtk.Label (_("IP address"));
-               label.set_alignment (0, 0.5f);
-               size_group.add_widget (label);
-               hbox.pack_start (label, false, false, 0);
-               var ip_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
-               ip_entry.set ("hildon-input-mode", Hildon.GtkInputMode.NUMERIC |
-                                                  Hildon.GtkInputMode.SPECIAL);
-               hbox.pack_start (ip_entry, true, true, 0);
-               content.pack_start (hbox, false, false, 0);
-
-               hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
-               label = new Gtk.Label (_("Port"));
-               label.set_alignment (0, 0.5f);
-               size_group.add_widget (label);
-               hbox.pack_start (label, false, false, 0);
-               var port_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
-               port_entry.set ("hildon-input-mode", Hildon.GtkInputMode.NUMERIC);
-               hbox.pack_start (port_entry, true, true, 0);
-               content.pack_start (hbox, true, true, 0);
-
-               hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
-               label = new Gtk.Label (_("Fingerprint"));
-               label.set_alignment (0, 0.5f);
-               size_group.add_widget (label);
-               hbox.pack_start (label, false, false, 0);
-               var fingerprint_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
-               fingerprint_entry.set ("hildon-input-mode", Hildon.GtkInputMode.HEXA);
-               hbox.pack_start (fingerprint_entry, true, true, 0);
-               content.pack_start (hbox, true, true, 0);
-
-               var iter = Gtk.TreeIter ();
-               if (path == null) {
-                       port_entry.set_text ("443");
-               } else if (store.get_iter (out iter, path)) {
-                       string tmp;
-                       store.@get (iter, 0, out tmp);
-                       string[] ip_port = tmp.split (":");
-                       if (ip_port.length == 2) {
-                               ip_entry.set_text (ip_port[0]);
-                               port_entry.set_text (ip_port[1]);
-                       }
-
-                       dialog.add_button (_("Delete"), RESPONSE_DELETE);
-               }
-               dialog.add_button (_("Save"), Gtk.ResponseType.OK);
-               dialog.response.connect ((response_id) => {
-                       var bridges = new SList<string> ();
-
-                       if (response_id == RESPONSE_DELETE) {
-                               if (path != null) {
-                                       Gtk.TreeIter iter2;
-                                       store.get_iter (out iter2, path);
-                                       store.remove (iter2);
-                                       string bridge;
-                                       if (store.get_iter_first (out iter2)) do {
-                                               store.@get (iter2, 0, out bridge);
-                                               bridges.append (bridge);
-                                       } while (store.iter_next (ref iter2));
-                                       try {
-                                               gconf.set_list (GCONF_KEY_BRIDGES,
-                                                               GConf.ValueType.STRING,
-                                                               bridges);
-                                       } catch (Error e) {
-                                               Hildon.Banner.show_information (dialog, null,
-                                                                               "Failed to save bridge relay list: %s".printf (e.message));
-                                       }
-                               }
-                               dialog.destroy ();
-                       }
-                       if (response_id == Gtk.ResponseType.OK) {
-                               if (!is_valid_ip_address (ip_entry.get_text ())) {
-                                       Hildon.Banner.show_information (dialog, null,
-                                                                       _("Invalid IP address"));
-                                       return;
-                               }
-                               int port = port_entry.get_text ().to_int ();
-                               if (port < 0 || port > 65565) {
-                                       Hildon.Banner.show_information (dialog, null,
-                                                                       _("Invalid port number"));
-                                       return;
-                               }
-                               Gtk.TreeIter iter2;
-                               if (path == null) {
-                                       store.append (out iter2);
-                               } else {
-                                       store.get_iter (out iter2, path);
-                               }
-                               store.@set (iter2, 0, "%s:%d".printf (ip_entry.get_text (), port));
-                               try {
-                                       bridges = gconf.get_list (GCONF_KEY_BRIDGES,
-                                                                 GConf.ValueType.STRING);
-                               } catch (Error e) {
-                                       Hildon.Banner.show_information (null, null,
-                                                                       "Error loading bridges: %s".printf (e.message));
-                               }
-                               if (path == null) {
-                                       bridges.append ("%s:%d".printf (ip_entry.get_text (), port));
-                               } else {
-                                       bridges = null;
-                                       string bridge;
-                                       if (store.get_iter_first (out iter2)) do {
-                                               store.@get (iter2, 0, out bridge);
-                                               bridges.append (bridge);
-                                       } while (store.iter_next (ref iter2));
-                               }
-                               try {
-                                       gconf.set_list (GCONF_KEY_BRIDGES,
-                                                       GConf.ValueType.STRING,
-                                                       bridges);
-                               } catch (Error e) {
-                                               Hildon.Banner.show_information (dialog, null,
-                                                                               "Failed to save bridge relay list: %s".printf (e.message));
-                               }
-
-                               dialog.destroy ();
-                       }
-               });
-
-               dialog.show_all ();
+               var dialog = new BridgeDialog ();
+               dialog.show ();
        }
 
        /**