Net = function () {
this._init.apply(this, arguments);
}
Net.prototype = {
__proto__: ElementBase.prototype,
elt: 'net',
color_name: ['down', 'downerrors', 'up', 'uperrors', 'collisions'],
text_items: [new St.Icon({ icon_type: St.IconType.SYMBOLIC,
icon_size: 2 * IconSize / 3,
icon_name:'go-down'}),
new St.Label({ style_class: "sm-status-value"}),
new St.Label({ text: 'kB/s', style_class: "sm-unit-label"}),
new St.Icon({ icon_type: St.IconType.SYMBOLIC,
icon_size: 2 * IconSize / 3,
icon_name:'go-up'}),
new St.Label({ style_class: "sm-status-value"}),
new St.Label({ text: 'kB/s', style_class: "sm-unit-label"})],
menu_items: [new St.Label({ style_class: "sm-value"}),
new St.Label({ text:'k', style_class: "sm-label"}),
new St.Icon({ icon_type: St.IconType.SYMBOLIC,
icon_size: 16, icon_name:'go-down'}),
new St.Label({ style_class: "sm-value"}),
new St.Label({ text:'k', style_class: "sm-label"}),
new St.Icon({ icon_type: St.IconType.SYMBOLIC,
icon_size: 16, icon_name:'go-up'})],
speed_in_bits: false,
_init: function() {
this.ifs = [];
// Can't get netlist:
// GTop.glibtop_get_netlist
// Error: No symbol 'glibtop_get_netlist' in namespace 'GTop'
let net_lines = Shell.get_file_contents_utf8_sync('/proc/net/dev').split("\n");
for(let i = 3; i < net_lines.length - 1 ; i++) {
let ifc = net_lines.replace(/^\s+/g, '').split(":")[0];
if(ifc.indexOf("br") < 0 && ifc.indexOf("lo") < 0) {
this.ifs.push(ifc);
}
}
this.gtop = new GTop.glibtop_netload();
this.last = [0, 0, 0, 0, 0];
this.usage = [0, 0, 0, 0, 0];
this.last_time = 0;
this.menu_item = new PopupMenu.PopupMenuItem(_("Net"), {reactive: false});
ElementBase.prototype._init.call(this);
this.tip_format(['kB/s', '/s', 'kB/s', '/s', '/s']);
this.update_units();
Schema.connect('changed::' + this.elt + '-speed-in-bits', Lang.bind(this, this.update_units));
this.update();
},
update_units: function() {
let previous_setting = this.speed_in_bits;
this.speed_in_bits = Schema.get_boolean(this.elt + '-speed-in-bits');
if (this.speed_in_bits) {
this.set_tip_unit(['kbps', '/s', 'kbps', '/s', '/s']);
this.text_items[2].text = 'kbps';
this.text_items[5].text = 'kbps';
if (!previous_setting) {
this.last[0] *= 8;
this.last[1] *= 8;
this.usage[0] *= 8;
this.usage[1] *= 8;
}
} else {
this.set_tip_unit(['kB/s', '/s', 'kB/s', '/s', '/s']);
this.text_items[2].text = 'kB/s';
this.text_items[5].text = 'kB/s';
if (previous_setting) {
this.last[0] /= 8;
this.last[1] /= 8;
this.usage[0] /= 8;
this.usage[1] /= 8;
}
}
},
refresh: function() {
let accum = [0, 0, 0, 0, 0];
for (let ifn in this.ifs) {
GTop.glibtop_get_netload(this.gtop, this.ifs[ifn]);
accum[0] += this.gtop.bytes_in * (this.speed_in_bits ? 8 : 1);
accum[1] += this.gtop.errors_in;
accum[2] += this.gtop.bytes_out * (this.speed_in_bits ? 8 : 1);
accum[3] += this.gtop.errors_out;
accum[4] += this.gtop.collisions;
}
let time = GLib.get_monotonic_time() / 1000;
let delta = time - this.last_time;
if (delta > 0)
for (let i = 0;i < 5;i++) {
this.usage = Math.round((accum - this.last) / delta);
this.last = accum;
}
this.last_time = time;
},
_apply: function() {
this.tip_vals = this.vals = this.usage;
this.menu_items[0].text = this.text_items[1].text = this.tip_vals[0].toString();
this.menu_items[3].text = this.text_items[4].text = this.tip_vals[2].toString();
}
};