Listen to changes of the /apps/maemo/tor/enabled GConf key
authorPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 6 May 2011 12:14:15 +0000 (14:14 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 6 May 2011 12:14:15 +0000 (14:14 +0200)
Start/stop Tor when the GConf key changes. This allows to configure the
desired status externally.

src/status-area-applet-tor.vala

index 8bb41fa..7e5e1c0 100644 (file)
@@ -1,6 +1,6 @@
 /* This file is part of status-area-applet-tor.
  *
- * Copyright (C) 2010 Philipp Zabel
+ * Copyright (C) 2010-2011 Philipp Zabel
  *
  * status-area-applet-tor is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
@@ -463,20 +463,18 @@ class TorStatusMenuItem : HD.StatusMenuItem {
                                return;
                        }
                        if (response_id == Gtk.ResponseType.ACCEPT) {
-                               if (!tor_enabled && check.get_active ()) {
-                                       tor_enabled = true;
+                               if (!tor_enabled && check.get_active ()) try {
+                                       gconf.set_bool (GCONF_KEY_TOR_ENABLED, true);
 
-                                       if (conic_connected) {
-                                               start_tor ();
-                                       } else {
+                                       // Enabled by user interaction, so connect if needed
+                                       if (!conic_connected)
                                                conic.connect (ConIc.ConnectFlags.NONE);
-                                       }
-                               } else if (tor_enabled && !check.get_active ()) {
-                                       tor_enabled = false;
-
-                                       stop_tor ();
-                                       if (conic_connected)
-                                               conic.disconnect ();
+                               } catch (Error e) {
+                                       Hildon.Banner.show_information (null, null, "Failed to enable GConf key");
+                               } else if (tor_enabled && !check.get_active ()) try {
+                                       gconf.set_bool (GCONF_KEY_TOR_ENABLED, false);
+                               } catch (Error e) {
+                                       Hildon.Banner.show_information (null, null, "Failed to disable GConf key");
                                }
                        }
                        dialog.destroy ();
@@ -526,6 +524,28 @@ class TorStatusMenuItem : HD.StatusMenuItem {
        }
 
        /**
+        * Callback for GConf change notification on the tor_enabled key
+        */
+       private void tor_enabled_changed_cb (GConf.Client gc, uint cxnid, GConf.Entry entry) {
+               if (entry.key == GCONF_KEY_TOR_ENABLED) {
+                       bool old_tor_enabled = tor_enabled;
+                       tor_enabled = entry.get_value ().get_bool ();
+                       if (old_tor_enabled == tor_enabled)
+                               return;
+
+                       if (tor_enabled) {
+                               // Start Tor immediately if a connection is already available
+                               if (conic_connected)
+                                       start_tor ();
+                       } else {
+                               stop_tor ();
+                               if (conic_connected)
+                                       conic.disconnect ();
+                       }
+               }
+       }
+
+       /**
         * Callback for the ConIc connection-event signal
         */
        private void conic_connection_event_cb (ConIc.Connection conic, ConIc.ConnectionEvent event) {
@@ -594,6 +614,10 @@ class TorStatusMenuItem : HD.StatusMenuItem {
                gconf = GConf.Client.get_default ();
                try {
                        tor_enabled = gconf.get_bool (GCONF_KEY_TOR_ENABLED);
+
+                       // Request change notifications for the tor_enabled key
+                       gconf.add_dir (GCONF_DIR_TOR, GConf.ClientPreloadType.ONELEVEL);
+                       gconf.notify_add (GCONF_KEY_TOR_ENABLED, tor_enabled_changed_cb);
                } catch (Error e) {
                        critical ("Failed to get GConf setting: %s", e.message);
                }