/usr/share/gnome-shell/extensions/show-ip@sgaraud.github.com/extension.js is in gnome-shell-extension-show-ip 8-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | /*
* Show IP menu GNOME extension
* https://github.com/sgaraud/gnome-extension-show-ip
*
* Copyright (C) 2015 Sylvain Garaud
*
* This file is part of Show-IP GNOME extension.
* Show IP GNOME extension is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Show IP GNOME extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Show IP GNOME extension. If not, see <https://www.gnu.org/licenses/>.
*
*/
/* Ugly. This is here so that we don't crash old libnm-glib based shells unnecessarily
* by loading the new libnm.so. Should go away eventually */
const libnm_glib = imports.gi.GIRepository.Repository.get_default().is_registered("NMClient", "1.0");
const Clutter = imports.gi.Clutter;
const Lang = imports.lang;
const St = imports.gi.St;
const GObject = imports.gi.GObject;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const NetworkPanel = Main.panel.statusArea.aggregateMenu._network;
const NMC = libnm_glib ? imports.gi.NMClient : imports.gi.NM;
const NetworkManager = libnm_glib ? imports.gi.NetworkManager : NMC;
const Gettext = imports.gettext.domain(Me.metadata['gettext-domain']);
const _ = Gettext.gettext;
/* Used to request via HTTP the public address to a server. */
const Soup = imports.gi.Soup;
const _httpSession = new Soup.SessionAsync();
/* This makes the session work under a proxy. The funky syntax here
* is required because of another libsoup quirk, where there's a GObject
* property called 'add-feature', designed as a construct property for
* C convenience.
*/
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
/* Setup to make the Preferences button in the PopupMenu open directly
* the prefs GUI.
*/
const Shell = imports.gi.Shell;
let _appSys = Shell.AppSystem.get_default();
let _gsmPrefs = _appSys.lookup_app('gnome-shell-extension-prefs.desktop');
let indicator = null;
let metadata = Me.metadata;
let Schema = null;
let settingsChangedMenu = null;
let settingsChangedPublic = null;
let settingsChangedIpv6 = null;
const NOT_CONNECTED = _('not connected');
const NM_NOT_RUNNING = _('NM not running');
const PUBLIC_IP = _('Public IP');
function init() {
Schema = Convenience.getSettings();
Convenience.initTranslations();
}
const IpDevice = new Lang.Class({
Name: 'IpDevice.IpDevice',
_init: function (obj) {
this.device = obj;
this.ifc = obj.get_iface();
this.ipconf = obj.get_ip4_config();
this.ip = NOT_CONNECTED;
this._stateChangedId = null;
this._ipConfId = null;
},
});
const IpMenuBase = new Lang.Class({
Name: 'IpMenuBase.IpMenuBase',
_init: function (menu, label) {
this.menu = menu;
this.nmStarted = true;
this.selectedDevice = Schema.get_string('last-device');
this.label = label;
this.client = libnm_glib ? NMC.Client.new() : NMC.Client.new(null);
if ((libnm_glib ? this.client.get_manager_running() : this.client.get_nm_running()) == false) {
this.label.set_text(NM_NOT_RUNNING);
this.nmStarted = false;
return;
}
this._clientAddedId = this.client.connect('device-added', Lang.bind(this, this._deviceAdded));
this._clientRemovedId = this.client.connect('device-removed', Lang.bind(this, this._deviceRemoved));
this._getNetworkDevices(this.client);
},
_deviceAdded: function (client, device) {
let _device;
_device = new IpDevice(device);
_device._stateChangedId = device.connect('state-changed', Lang.bind(this, this._updateIp));
this._devices.push(_device);
this._updateIp(device);
},
_deviceRemoved: function (client, device) {
for (let dev of this._devices) {
if (dev.device == device) {
this._resetDevice(dev);
if (this.selectedDevice == dev.ifc) {
this.selectedDevice = '';
}
let index = this._devices.indexOf(dev);
if (index > -1) {
this._devices.splice(index, 1);
}
break;
}
}
},
_createPopupMenu: function () {
this.menu.removeAll();
for (let device of this._devices) {
this._addToPopupMenu(device.ifc);
}
if (Schema.get_boolean('public')) {
this._addToPopupMenu(PUBLIC_IP);
if (PUBLIC_IP == this.selectedDevice) {
if (this.client.get_state() == NetworkManager.State.CONNECTED_GLOBAL) {
this._setPublic();
}
else {
this.label.set_text(NOT_CONNECTED);
}
}
}
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this.itemClip = new PopupMenu.PopupMenuItem(_("Copy to clipboard"));
this.menu.addMenuItem(this.itemClip);
let lastIp = this.label;
this._itemClipId = this.itemClip.connect('activate', function () {
St.Clipboard.get_default().set_text(St.ClipboardType.PRIMARY, lastIp.get_text());
St.Clipboard.get_default().set_text(St.ClipboardType.CLIPBOARD, lastIp.get_text());
});
this.itemPref = new PopupMenu.PopupMenuItem(_("Preferences"));
this.menu.addMenuItem(this.itemPref);
this._itemPrefId = this.itemPref.connect('activate', function () {
if (_gsmPrefs.get_state() == _gsmPrefs.SHELL_APP_STATE_RUNNING) {
_gsmPrefs.activate();
} else {
let info = _gsmPrefs.get_app_info();
let timestamp = global.display.get_current_time_roundtrip();
info.launch_uris([metadata.uuid], global.create_app_launch_context(timestamp, -1));
}
});
for (let device of this._devices) {
if (device.ifc == this.selectedDevice) {
if (Schema.get_boolean("menu")) {
this.label.set_text(_("IP: %s").format(device.ip));
} else {
this.label.set_text(device.ip);
}
break;
}
}
if ('' == this.selectedDevice) {
this.label.set_text(NOT_CONNECTED);
}
},
_addToPopupMenu: function (dev) {
this.item = new PopupMenu.PopupMenuItem(dev);
this.menu.addMenuItem(this.item);
this._manualUpdateId = this.item.connect('activate', Lang.bind(this, this._manualUpdate));
},
_manualUpdate: function (it) {
for (let device of this._devices) {
if (device.ifc == it.label.get_text()) {
this.selectedDevice = device.ifc;
Schema.set_string('last-device', device.ifc);
break;
}
}
if (PUBLIC_IP == it.label.get_text()) {
this.selectedDevice = PUBLIC_IP;
Schema.set_string('last-device', PUBLIC_IP);
}
this._createPopupMenu();
},
_getNetworkDevices: function (nmc) {
let _device;
this.devices = nmc.get_devices();
this._devices = [];
let i = 0;
for (let device of this.devices) {
_device = new IpDevice(device);
_device._stateChangedId = device.connect('state-changed', Lang.bind(this, this._updateIp));
this._devices[i++] = _device;
this._updateIp(device);
}
},
_updateIp: function (dev) {
let ipconf = dev.get_ip4_config();
if (Schema.get_boolean('ipv6')) {
ipconf = dev.get_ip6_config();
}
let ifc = dev.get_iface();
if (ipconf != null) {
this._addInterface(ipconf, ifc);
}
else {
this._deleteInterface(ifc);
}
},
_addInterface: function (ipconf, ifc) {
for (let device of this._devices) {
if (device.ifc == ifc) {
if (device._ipConfId != null) {
device.ipconf.disconnect(device._ipConfId);
device._ipConfId = null;
}
device.ipconf = ipconf;
if (typeof(device.ipconf.get_addresses()[0]) == 'undefined') {
device._ipConfId = ipconf.connect('notify::addresses', Lang.bind(this, function () {
ipconf.disconnect(device._ipConfId);
device._ipConfId = null;
if (typeof(device.ipconf.get_addresses()[0]) != 'undefined') {
this._getIps(ipconf.get_addresses()[0].get_address(), ifc);
}
// tweak to catch possible buffered notification
else {
this._getIps(0, ifc);
}
}));
}
else {
this._getIps(ipconf.get_addresses()[0].get_address(), ifc);
}
break;
}
}
},
_deleteInterface: function (ifc) {
for (let device of this._devices) {
if (device.ifc == ifc) {
if (this.selectedDevice == device.ifc) {
this.selectedDevice = '';
}
device.ip = NOT_CONNECTED;
break;
}
}
this._createPopupMenu();
},
_getIps: function (ipadd, ifc) {
/* iterate until current device found in list */
for (let device of this._devices) {
if (device.ifc == ifc) {
/* populate the device 'ip' field */
if (Schema.get_boolean('ipv6')) {
device.ip = this._decodeIp6(ipadd);
} else {
device.ip = this._decodeIp4(ipadd);
}
if ('' == this.selectedDevice) {
this.selectedDevice = device.ifc;
}
break;
}
}
this._createPopupMenu();
},
_decodeIp4: function (num) {
if (!libnm_glib)
return num;
num = num >>> 0;
let array = new Uint8Array(4);
array[0] = num;
array[1] = num >> 8;
array[2] = num >> 16;
array[3] = num >> 24;
return array[0] + '.' + array[1] + '.' + array[2] + '.' + array[3];
},
/* inspired by http://locutus.io/php/network/inet_ntop/ */
_decodeIp6: function (num) {
if (!libnm_glib)
return num;
let c = [];
let m = '';
let i = 0;
for (i = 0; i < 16; i = i + 2) {
c.push(num[i].toString(16) + num[i + 1].toString(16));
}
return c.join(':')
.replace(/((^|:)0*(?=:|$))+:?/g, function (t) {
m = (t.length > m.length) ? t : m;
return t;
})
.replace(m || ' ', '::');
},
_getPublic: function (callback) {
let request = Soup.Message.new('GET', Schema.get_string('ip-lookup-service'));
_httpSession.queue_message(request, function (_httpSession, message) {
if (message.status_code !== 200) {
callback(message.status_code, null);
return;
}
let ip = request.response_body.data;
callback(null, ip);
});
},
_setPublic: function () {
let that = this.label;
this._getPublic(function (err, res) {
if (res != null) {
if (Schema.get_boolean("menu")) {
that.set_text(_("IP: %s").format(res.trim()));
} else {
that.set_text(res.trim());
}
}
else {
that.set_text(NOT_CONNECTED);
}
});
},
_resetDevice: function (device) {
GObject.Object.prototype.disconnect.call(device.device, device._stateChangedId);
if (device._ipConfId != null) {
GObject.Object.prototype.disconnect.call(device.ipconf, device._ipConfId);
}
},
destroy: function () {
if (this.nmStarted == true) {
for (let device of this._devices) {
this._resetDevice(device);
}
this._devices = [];
if (this._itemClipId) {
this.itemClip.disconnect(this._itemClipId);
}
if (this._itemPrefId) {
this.itemPref.disconnect(this._itemPrefId);
}
this.client.disconnect(this._clientAddedId);
this.client.disconnect(this._clientRemovedId);
this.item.disconnect(this._manualUpdateId);
}
},
});
const IpMenuPanel = new Lang.Class({
Name: 'IpMenuPanel.IpMenuPanel',
Extends: PanelMenu.Button,
_init: function () {
this.parent(0.0, _("Show IP"));
this.label = new St.Label({
text: '',
y_expand: true,
y_align: Clutter.ActorAlign.CENTER
});
this.label.set_text(NOT_CONNECTED);
this.base = new IpMenuBase(this.menu, this.label);
let nbox = new St.BoxLayout({style_class: 'panel-status-menu-box'});
nbox.add_child(this.base.label);
this.actor.add_child(nbox);
this.actor.show();
},
destroy: function () {
this.base.destroy();
this.parent();
},
});
const IpMenuPopup = new Lang.Class({
Name: 'IpMenuPopup.IpMenuPopup',
Extends: PopupMenu.PopupSubMenuMenuItem,
_init: function () {
this.parent(_("IP:"), false);
this.base = new IpMenuBase(this.menu, this.label);
this.actor.show();
},
destroy: function () {
this.base.destroy();
this.parent();
},
});
function refresh() {
indicator.destroy();
if (Schema.get_boolean("menu")) {
indicator = new IpMenuPopup;
NetworkPanel.menu.addMenuItem(indicator, 0);
} else {
indicator = new IpMenuPanel;
Main.panel.addToStatusArea('Ip-menu', indicator);
}
}
function enable() {
if (Schema.get_boolean("menu")) {
indicator = new IpMenuPopup;
NetworkPanel.menu.addMenuItem(indicator, 0);
} else {
indicator = new IpMenuPanel;
Main.panel.addToStatusArea('Ip-menu', indicator);
}
/* Monitor settings changes */
settingsChangedMenu = Schema.connect('changed::menu', refresh);
settingsChangedPublic = Schema.connect('changed::public', refresh);
settingsChangedIpv6 = Schema.connect('changed::ipv6', refresh);
}
function disable() {
if (indicator) {
indicator.destroy();
}
/* disconnect settings changes */
if (settingsChangedMenu) {
Schema.disconnect(settingsChangedMenu);
}
if (settingsChangedPublic) {
Schema.disconnect(settingsChangedPublic);
}
if (settingsChangedIpv6) {
Schema.disconnect(settingsChangedIpv6);
}
}
|