Merge lp:~alucryd/switchboard-plug-networking/no-ifconfig into lp:~elementary-pantheon/switchboard-plug-networking/trunk

Proposed by Maxime Gauduin
Status: Merged
Approved by: Adam Bieńkowski
Approved revision: 99
Merged at revision: 99
Proposed branch: lp:~alucryd/switchboard-plug-networking/no-ifconfig
Merge into: lp:~elementary-pantheon/switchboard-plug-networking/trunk
Diff against target: 113 lines (+25/-40)
1 file modified
src/Widgets/Device/DevicePage.vala (+25/-40)
To merge this branch: bzr merge lp:~alucryd/switchboard-plug-networking/no-ifconfig
Reviewer Review Type Date Requested Status
Adam Bieńkowski (community) Approve
Viko Adi Rahmawan Pending
Review via email: mp+263825@code.launchpad.net

This proposal supersedes a proposal from 2015-06-18.

Commit message

Read received and transferred bytes values from /sys instead of deprecated ifconfig (lp:1471375)

Description of the change

ifconfig has been deprecated for some time by most distros. This change makes the plug read the received and transferred bytes values from /sys and process them to make them look pretty.
I also fixed all whitespace issues while I was at it.

I'm open to criticism as I've never written any vala or C code before so I may have done some stupid things.

To post a comment you must log in.
Revision history for this message
Viko Adi Rahmawan (vikoadi) wrote : Posted in a previous version of this proposal

You can use format_size (http://valadoc.org/#!api=glib-2.0/GLib.format_size) instead of dividing by 1024

review: Needs Fixing
Revision history for this message
Corentin Noël (tintou) wrote : Posted in a previous version of this proposal

The format_size fonction is definitely the way to go, it also returns a translated string (Byte is Octet in french so kb becomes ko)

Revision history for this message
Maxime Gauduin (alucryd) wrote : Posted in a previous version of this proposal

Thx for the suggestion, works like a charm!

Revision history for this message
Adam Bieńkowski (donadigo) wrote : Posted in a previous version of this proposal

It works well, but the code has some issues:
* Branch the current revision to apply the latest changes;
* Diff lines from 101 to 107 don't have proper ident;
* The code throws 2 additional warnings on the compile: unhandled error `GLib.FileError':
I think you should change the SpawnError to FileError.

* Lastly I would change diff line 109 to: error ("%s\n", e.message);

Other than that it is okay ;)

review: Needs Fixing
99. By Maxime Gauduin

Ditch deprecated ifconfig

Revision history for this message
Maxime Gauduin (alucryd) wrote :

Thx for the review, everything should be in order now.

Revision history for this message
Adam Bieńkowski (donadigo) wrote :

Everything now works & looks as expected...
Thanks ;)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Widgets/Device/DevicePage.vala'
2--- src/Widgets/Device/DevicePage.vala 2015-06-23 21:22:11 +0000
3+++ src/Widgets/Device/DevicePage.vala 2015-07-04 08:57:36 +0000
4@@ -30,10 +30,10 @@
5 private Gtk.Label sent;
6 private Gtk.Label received;
7
8- private string sent_l = (_("Sent:") + SUFFIX);
9- private string received_l = (_("Received:") + SUFFIX);
10+ private string sent_l = (_("Sent:") + SUFFIX);
11+ private string received_l = (_("Received:") + SUFFIX);
12
13- public DevicePage.from_owner (DeviceItem? _owner) {
14+ public DevicePage.from_owner (DeviceItem? _owner) {
15 owner = _owner;
16 device = owner.get_item_device ();
17
18@@ -50,16 +50,16 @@
19
20 var device_img = new Gtk.Image.from_icon_name (owner.get_item_icon_name (), Gtk.IconSize.DIALOG);
21 device_img.margin_end = 15;
22-
23+
24 var device_label = new Gtk.Label (Utils.type_to_string (device.get_device_type ()));
25 device_label.get_style_context ().add_class ("h2");
26-
27+
28 control_switch = new Gtk.Switch ();
29
30 var control_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 10);
31 control_box.pack_start (device_img, false, false, 0);
32 control_box.pack_start (device_label, false, false, 0);
33- control_box.pack_end (control_switch, false, false, 0);
34+ control_box.pack_end (control_switch, false, false, 0);
35
36 var activitybox = new Gtk.Box (Gtk.Orientation.VERTICAL, 1);
37 activitybox.hexpand = true;
38@@ -79,7 +79,7 @@
39 activitybox.add (new Gtk.Label ("\n"));
40 activitybox.add (sent);
41 activitybox.add (received);
42-
43+
44 update_activity ();
45 update_switch_state ();
46
47@@ -89,11 +89,11 @@
48 this.show_all ();
49 }
50
51- private void update_activity () {
52+ private void update_activity () {
53 string sent_bytes, received_bytes;
54 get_activity_information (device.get_iface (), out sent_bytes, out received_bytes);
55 sent.label = sent_l + sent_bytes ?? UNKNOWN;
56- received.label = received_l + received_bytes ?? UNKNOWN;
57+ received.label = received_l + received_bytes ?? UNKNOWN;
58 }
59
60 private void update_switch_state () {
61@@ -101,36 +101,21 @@
62 }
63
64 /* Main method to get all information about the interface */
65- private void get_activity_information (string iface, out string received_bytes, out string transfered_bytes) {
66+ private void get_activity_information (string iface, out string sent_bytes, out string received_bytes) {
67+ sent_bytes = UNKNOWN;
68 received_bytes = UNKNOWN;
69- transfered_bytes = UNKNOWN;
70-
71- try {
72- string[] spawn_args = { "ifconfig", iface };
73- string[] spawn_env = Environ.get ();
74- string output;
75-
76- Process.spawn_sync ("/",
77- spawn_args,
78- spawn_env,
79- SpawnFlags.SEARCH_PATH,
80- null,
81- out output,
82- null,
83- null);
84-
85- string[] data = output.split ("\n");
86- foreach (string line in data) {
87- if (line.contains ("RX bytes:")) {
88- string[] inf3 = line.split (":");
89- received_bytes = inf3[1].split (" ")[0].split (" ", 2)[1].replace ("(", "").replace (")", "");
90- transfered_bytes = inf3[2].split (" ", 2)[1].replace ("(", "").replace (")", "");
91- }
92- }
93-
94- } catch (SpawnError e) {
95- error (e.message);
96- }
97- }
98- }
99+
100+ try {
101+ string tx_bytes, rx_bytes;
102+
103+ FileUtils.get_contents ("/sys/class/net/" + iface + "/statistics/tx_bytes", out tx_bytes);
104+ FileUtils.get_contents ("/sys/class/net/" + iface + "/statistics/rx_bytes", out rx_bytes);
105+
106+ sent_bytes = format_size (uint64.parse (tx_bytes));
107+ received_bytes = format_size (uint64.parse (rx_bytes));
108+ } catch (FileError e) {
109+ error ("%s\n", e.message);
110+ }
111+ }
112+ }
113 }

Subscribers

People subscribed via source and target branches