-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.js
191 lines (164 loc) · 5.99 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const St = imports.gi.St;
const Main = imports.ui.main;
const Clutter = imports.gi.Clutter;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Mainloop = imports.mainloop;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Config = imports.misc.config;
const Util = imports.misc.util;
const Gettext = imports.gettext.domain('gnome-shell-extension-pingindicator');
const _ = Gettext.gettext;
const SHELL_MINOR = parseInt(Config.PACKAGE_VERSION.split('.')[1]);
const PING_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.pingindicator';
const PING_DESTINATION = 'ping-destination';
const REFRESH_INTERVAL = 'refresh-interval';
const BEEP_WHEN_TIMEOUT = 'beep-when-timeout';
const SOUND_FILE_PATH = '/usr/share/sounds/freedesktop/stereo/bell.oga';
const PingMenuButton = new Lang.Class({
Name: 'PingMenuButton',
Extends: PanelMenu.Button,
_init: function() {
this.parent(0.0, 'Ping Indicator', false);
this._loadConfig();
this.buttonText = new St.Label({
text: _("..."),
y_align: Clutter.ActorAlign.CENTER
});
// Compatibility with gnome-shell >= 3.32
if (SHELL_MINOR > 30) {
this.add_actor(this.buttonText);
}
else {
this.actor.add_actor(this.buttonText);
}
let item = new PopupMenu.PopupMenuItem(_("Settings"));
item.connect('activate', Lang.bind(this, this._onPreferencesActivate));
this.menu.addMenuItem(item);
this._refresh();
},
_loadConfig: function() {
this._settings = Convenience.getSettings(PING_SETTINGS_SCHEMA);
this._settingsC = this._settings.connect("changed", Lang.bind(this, function() {
this._refresh();
}));
},
_onPreferencesActivate: function() {
Util.spawn(["gnome-shell-extension-prefs", "[email protected]"]);
return 0;
},
_loadData: function() {
let success;
this.command = ["ping", "-c 1", this._pingDestination];
[success, this.child_pid, this.std_in, this.std_out, this.std_err] =
GLib.spawn_async_with_pipes(
null,
this.command,
null,
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null);
if (!success) {
return;
}
this.IOchannelIN = GLib.IOChannel.unix_new(this.std_in);
this.IOchannelOUT = GLib.IOChannel.unix_new(this.std_out);
this.IOchannelERR = GLib.IOChannel.unix_new(this.std_err);
this.IOchannelIN.shutdown(false);
this.tagWatchChild = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.child_pid,
Lang.bind(this, function(pid, status, data) {
GLib.source_remove(this.tagWatchChild);
GLib.spawn_close_pid(pid);
this.child_pid = undefined;
})
);
this.tagWatchOUT = GLib.io_add_watch(this.IOchannelOUT, GLib.PRIORITY_DEFAULT,
GLib.IOCondition.IN | GLib.IOCondition.HUP,
Lang.bind(this, this._loadPipeOUT)
);
this.tagWatchERR = GLib.io_add_watch(this.IOchannelERR, GLib.PRIORITY_DEFAULT,
GLib.IOCondition.IN | GLib.IOCondition.HUP,
Lang.bind(this, this._loadPipeERR)
);
},
_loadPipeOUT: function(channel, condition, data) {
if (condition != GLib.IOCondition.HUP) {
let [size, out] = channel.read_to_end();
let result = String.fromCharCode.apply(null, out).match(/(?<=\w=)\d+(?=(.\d+)?\s\w+$)/m);
if(result != null) {
let str = result[0];
str = str.concat(_(" ms"));
this.buttonText.set_text(str);
}
}
GLib.source_remove(this.tagWatchOUT);
channel.shutdown(true);
},
_loadPipeERR: function(channel, condition, data) {
if (condition != GLib.IOCondition.HUP) {
this.buttonText.set_text(_("Error"));
}
GLib.source_remove(this.tagWatchERR);
channel.shutdown(false);
},
get _pingDestination() {
if (!this._settings)
this._loadConfig();
return this._settings.get_string(PING_DESTINATION);
},
get _refreshInterval() {
if (!this._settings)
this._loadConfig();
return this._settings.get_int(REFRESH_INTERVAL);
},
get _playBeep() {
if (!this._settings)
this._loadConfig();
return this._settings.get_boolean(BEEP_WHEN_TIMEOUT);
},
_refresh: function() {
this._removeTimeout();
if (this.child_pid === undefined) {
this._loadData();
} else {
this.buttonText.set_text(_('Waiting'));
if (this._playBeep) {
Util.trySpawnCommandLine('canberra-gtk-play -f ' + SOUND_FILE_PATH);
}
}
this._timeout = Mainloop.timeout_add_seconds(this._refreshInterval,
Lang.bind(this, this._refresh));
return true;
},
_removeTimeout: function() {
if (this._timeout !== undefined) {
Mainloop.source_remove(this._timeout);
this._timeout = undefined;
}
},
stop: function() {
this._removeTimeout();
if (this._settingsC) {
this._settings.disconnect(this._settingsC);
this._settingsC = undefined;
}
this.menu.removeAll();
}
})
let pingMenu;
function init() {
Convenience.initTranslations('gnome-shell-extension-pingindicator');
}
function enable() {
log(`enabling ${Me.metadata.name} version ${Me.metadata.version}`);
pingMenu = new PingMenuButton;
Main.panel.addToStatusArea('ping-indicator', pingMenu);
}
function disable() {
log(`disabling ${Me.metadata.name} version ${Me.metadata.version}`);
pingMenu.stop();
pingMenu.destroy();
}