From 0fa48c8b7863d870c71304e0f60274b579d0326f Mon Sep 17 00:00:00 2001 From: Frank Ploss Date: Mon, 28 Dec 2015 16:39:19 +0100 Subject: [PATCH] Do not re-create menu if it already exists --- tor_control_client.js | 12 +++++++++--- ui/tor_button.js | 15 ++++++++++++--- ui/tor_disconnected_menu.js | 12 +++++++----- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/tor_control_client.js b/tor_control_client.js index 760a214..4ef5abf 100644 --- a/tor_control_client.js +++ b/tor_control_client.js @@ -68,8 +68,11 @@ const TorControlClient = new Lang.Class({ closeConnection: function(reason) { if (this._connection && this._connection.is_connected()) { this._connection.close(null); - this.emit('changed-connection-state', 'closed', reason); } + + this._connection = null; + + this.emit('changed-connection-state', 'closed', reason); }, switchIdentity: function() { @@ -167,8 +170,11 @@ const TorControlClient = new Lang.Class({ do { let line = this._readLine(); - if (line === null) - return {replyLines: ['Lost connection to Tor server']}; + if (line === null) { + let reason = 'Lost connection to Tor server'; + this.closeConnection(reason); + return {replyLines: [reason]}; + } var reply = this._parseLine(line); statusCode = reply.statusCode; diff --git a/ui/tor_button.js b/ui/tor_button.js index 4ddca96..1ea1cbe 100644 --- a/ui/tor_button.js +++ b/ui/tor_button.js @@ -55,7 +55,7 @@ const TorButton = new Lang.Class({ this.actor.add_child(this._icon); - this._showDisconnectedMenu(); + this._showDisconnectedMenu('Haven’t tried to connect yet'); }, _bindEvents: function() { @@ -76,6 +76,10 @@ const TorButton = new Lang.Class({ }, _showConnectedMenu: function() { + if (this._menu instanceof TorConnectedIcon) { + return; + } + this._icon.icon_name = TorConnectedIcon; this._menu = new TorPopupMenu(this.actor, this._torControlClient); this.setMenu(this._menu); @@ -83,6 +87,11 @@ const TorButton = new Lang.Class({ }, _showDisconnectedMenu: function(reason) { + if (this._menu instanceof TorDisconnectedMenu) { + this._menu.setReason(reason); + return; + } + this._icon.icon_name = TorDisconnectedIcon; this._menu = new TorDisconnectedMenu(this.actor, this._torControlClient, reason); this.setMenu(this._menu); @@ -94,7 +103,7 @@ const TorButton = new Lang.Class({ }, _onProtocolError: function(source, message, statusCode) { - Main.notifyError('Tor: ' + message); - log('Tor control procotol error (status code ' + statusCode + '): ' + reason) + var msg = 'Tor control protocol error: ' + message + ' (status code' + statusCode + ')'; + Main.notifyError(msg); } }); diff --git a/ui/tor_disconnected_menu.js b/ui/tor_disconnected_menu.js index 455af7e..8b7ee7e 100644 --- a/ui/tor_disconnected_menu.js +++ b/ui/tor_disconnected_menu.js @@ -29,29 +29,31 @@ const TorDisconnectedMenu = new Lang.Class({ _init: function(actor, torControlClient, reason) { this._torControlClient = torControlClient; - this._reason = reason; this.parent(actor, 0.25, St.Side.TOP); this._addActions(); + this.setReason(reason); }, destroy: function() { this.parent(arguments); }, + setReason: function(reason) { + this._msgLabel.set_text('No connection. ' + reason); + }, + _addActions: function() { var errorMessageMenuItem = new PopupMenu.PopupBaseMenuItem({reactive: false}); errorMessageMenuItem.setSensitive(false); - errorMessageMenuItem.actor.add_actor(new St.Label({ - text: 'No connection. Reason: ' + this._reason - })); + this._msgLabel = new St.Label(); + errorMessageMenuItem.actor.add_actor(this._msgLabel); this.addMenuItem(errorMessageMenuItem); this.addAction('Reconnect', Lang.bind(this, this._reconnect)); }, _reconnect: function() { - this._torControlClient.connect('changed-connection-state', function() {}) this._torControlClient.openConnection(); } });