Handle errors a little more gracefully

master
Frank Ploss 9 years ago
parent be8c9aa464
commit 94c0c27c25

@ -4,6 +4,7 @@
const Lang = imports.lang; const Lang = imports.lang;
const St = imports.gi.St; const St = imports.gi.St;
const Main = imports.ui.main; const Main = imports.ui.main;
const Notify = imports.gi.Notify;
const PanelMenu = imports.ui.panelMenu; const PanelMenu = imports.ui.panelMenu;
const Me = imports.misc.extensionUtils.getCurrentExtension(); const Me = imports.misc.extensionUtils.getCurrentExtension();
@ -11,8 +12,8 @@ const TorControlClient = Me.imports.tor_control_client.TorControlClient;
const TorIcon = 'tor'; const TorIcon = 'tor';
let torButton; let torButton = null;
let torControlClient; let torControlClient = null;
const TorButton = new Lang.Class({ const TorButton = new Lang.Class({
Name: 'TorButton', Name: 'TorButton',
@ -43,14 +44,23 @@ function init(extensionMeta) {
} }
function enable() { function enable() {
torControlClient = new TorControlClient(); try {
torButton = new TorButton(); torControlClient = new TorControlClient();
Main.panel.addToStatusArea(torButton.Name, torButton); torButton = new TorButton();
Main.panel.addToStatusArea(torButton.Name, torButton);
} catch (e) {
Main.notifyError('Error starting extension: ' + e);
disable();
}
} }
function disable() { function disable() {
torButton.destroy(); if (torButton !== null) {
torButton = null; torButton.destroy();
torControlClient.close(); torButton = null;
torControlClient = null; }
if (torControlClient !== null) {
torControlClient.close();
torControlClient = null;
}
} }

@ -14,15 +14,17 @@ const TorControlClient = new Lang.Class({
}, },
close: function() { close: function() {
this._outputStream.close(null); if (this._connection.is_connected()) {
this._inputStream.close(null); this._outputStream.close(null);
this._inputStream.close(null);
}
}, },
_connect: function() { _connect: function() {
var socketClient = new Gio.SocketClient(); var socketClient = new Gio.SocketClient();
var connection = socketClient.connect_to_host('127.0.0.1:9051', null, null); this._connection = socketClient.connect_to_host('127.0.0.1:9051', null, null);
this._inputStream = new Gio.DataInputStream({base_stream: connection.get_input_stream()}); this._inputStream = new Gio.DataInputStream({base_stream: this._connection.get_input_stream()});
this._outputStream = new Gio.DataOutputStream({base_stream: connection.get_output_stream()}); this._outputStream = new Gio.DataOutputStream({base_stream: this._connection.get_output_stream()});
}, },
_updateProtocolInfo: function() { _updateProtocolInfo: function() {

Loading…
Cancel
Save