From df1e54134acb21acbe4b43e8a34e5959f4ffabb0 Mon Sep 17 00:00:00 2001 From: Philipp Zabel Date: Fri, 6 May 2011 14:14:15 +0200 Subject: [PATCH] Listen to changes of the /apps/maemo/tor/enabled GConf key Start/stop Tor when the GConf key changes. This allows to configure the desired status externally. --- src/status-area-applet-tor.vala | 50 +++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/src/status-area-applet-tor.vala b/src/status-area-applet-tor.vala index 8bb41fa..7e5e1c0 100644 --- a/src/status-area-applet-tor.vala +++ b/src/status-area-applet-tor.vala @@ -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); } -- 1.7.9.5