Do not re-create menu if it already exists

master
Frank Ploss 9 years ago
parent 3cf6383cc8
commit 0fa48c8b78

@ -68,8 +68,11 @@ const TorControlClient = new Lang.Class({
closeConnection: function(reason) { closeConnection: function(reason) {
if (this._connection && this._connection.is_connected()) { if (this._connection && this._connection.is_connected()) {
this._connection.close(null); this._connection.close(null);
this.emit('changed-connection-state', 'closed', reason);
} }
this._connection = null;
this.emit('changed-connection-state', 'closed', reason);
}, },
switchIdentity: function() { switchIdentity: function() {
@ -167,8 +170,11 @@ const TorControlClient = new Lang.Class({
do { do {
let line = this._readLine(); let line = this._readLine();
if (line === null) if (line === null) {
return {replyLines: ['Lost connection to Tor server']}; let reason = 'Lost connection to Tor server';
this.closeConnection(reason);
return {replyLines: [reason]};
}
var reply = this._parseLine(line); var reply = this._parseLine(line);
statusCode = reply.statusCode; statusCode = reply.statusCode;

@ -55,7 +55,7 @@ const TorButton = new Lang.Class({
this.actor.add_child(this._icon); this.actor.add_child(this._icon);
this._showDisconnectedMenu(); this._showDisconnectedMenu('Havent tried to connect yet');
}, },
_bindEvents: function() { _bindEvents: function() {
@ -76,6 +76,10 @@ const TorButton = new Lang.Class({
}, },
_showConnectedMenu: function() { _showConnectedMenu: function() {
if (this._menu instanceof TorConnectedIcon) {
return;
}
this._icon.icon_name = TorConnectedIcon; this._icon.icon_name = TorConnectedIcon;
this._menu = new TorPopupMenu(this.actor, this._torControlClient); this._menu = new TorPopupMenu(this.actor, this._torControlClient);
this.setMenu(this._menu); this.setMenu(this._menu);
@ -83,6 +87,11 @@ const TorButton = new Lang.Class({
}, },
_showDisconnectedMenu: function(reason) { _showDisconnectedMenu: function(reason) {
if (this._menu instanceof TorDisconnectedMenu) {
this._menu.setReason(reason);
return;
}
this._icon.icon_name = TorDisconnectedIcon; this._icon.icon_name = TorDisconnectedIcon;
this._menu = new TorDisconnectedMenu(this.actor, this._torControlClient, reason); this._menu = new TorDisconnectedMenu(this.actor, this._torControlClient, reason);
this.setMenu(this._menu); this.setMenu(this._menu);
@ -94,7 +103,7 @@ const TorButton = new Lang.Class({
}, },
_onProtocolError: function(source, message, statusCode) { _onProtocolError: function(source, message, statusCode) {
Main.notifyError('Tor: ' + message); var msg = 'Tor control protocol error: ' + message + ' (status code' + statusCode + ')';
log('Tor control procotol error (status code ' + statusCode + '): ' + reason) Main.notifyError(msg);
} }
}); });

@ -29,29 +29,31 @@ const TorDisconnectedMenu = new Lang.Class({
_init: function(actor, torControlClient, reason) { _init: function(actor, torControlClient, reason) {
this._torControlClient = torControlClient; this._torControlClient = torControlClient;
this._reason = reason;
this.parent(actor, 0.25, St.Side.TOP); this.parent(actor, 0.25, St.Side.TOP);
this._addActions(); this._addActions();
this.setReason(reason);
}, },
destroy: function() { destroy: function() {
this.parent(arguments); this.parent(arguments);
}, },
setReason: function(reason) {
this._msgLabel.set_text('No connection. ' + reason);
},
_addActions: function() { _addActions: function() {
var errorMessageMenuItem = new PopupMenu.PopupBaseMenuItem({reactive: false}); var errorMessageMenuItem = new PopupMenu.PopupBaseMenuItem({reactive: false});
errorMessageMenuItem.setSensitive(false); errorMessageMenuItem.setSensitive(false);
errorMessageMenuItem.actor.add_actor(new St.Label({ this._msgLabel = new St.Label();
text: 'No connection. Reason: ' + this._reason errorMessageMenuItem.actor.add_actor(this._msgLabel);
}));
this.addMenuItem(errorMessageMenuItem); this.addMenuItem(errorMessageMenuItem);
this.addAction('Reconnect', Lang.bind(this, this._reconnect)); this.addAction('Reconnect', Lang.bind(this, this._reconnect));
}, },
_reconnect: function() { _reconnect: function() {
this._torControlClient.connect('changed-connection-state', function() {})
this._torControlClient.openConnection(); this._torControlClient.openConnection();
} }
}); });

Loading…
Cancel
Save