Merge lp:~jonathanlambrechts/do-plugins/MultipleBattery into lp:do-plugins/docklets

Proposed by Jonathan Lambrechts
Status: Rejected
Rejected by: Robert Dyer
Proposed branch: lp:~jonathanlambrechts/do-plugins/MultipleBattery
Merge into: lp:do-plugins/docklets
Diff against target: None lines
To merge this branch: bzr merge lp:~jonathanlambrechts/do-plugins/MultipleBattery
Reviewer Review Type Date Requested Status
Robert Dyer (community) Disapprove
Review via email: mp+9415@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jonathan Lambrechts (jonathanlambrechts) wrote :

Hi,
this is a proposition to use the sysfs instead of the proc interface and handle multiple batteries in the Battery applet.
This branch applies and cleans up the patch of the bug #390996.
It's a simplification of the UpdateBattStat method.

Jonathan

Revision history for this message
Robert Dyer (psybers) wrote :

This looks promising. I will probably do something along these lines at some point.

Revision history for this message
Robert Dyer (psybers) wrote :

Thanks for the submission. I am rejecting because a) we split Docky out into its own app and b) for Docky 2 I went ahead and implemented similar functionality (supporting multiple batteries) for that docklet.

review: Disapprove

Unmerged revisions

66. By Jonathan Lambrechts <email address hidden>

Use sysfs and handle multiple battery in battery monitor applet

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'BatteryMonitor/src/BatteryMonitor.cs'
2--- BatteryMonitor/src/BatteryMonitor.cs 2009-06-03 15:40:26 +0000
3+++ BatteryMonitor/src/BatteryMonitor.cs 2009-07-29 07:01:14 +0000
4@@ -37,8 +37,7 @@
5
6 public class BatteryMonitor : AbstractDockletItem
7 {
8- const string BattInfoPath = "/proc/acpi/battery/BAT0/info";
9- const string BattStatePath = "/proc/acpi/battery/BAT0/state";
10+ const string PowerSupplyPath ="/sys/class/power_supply";
11
12 const string BottomSvg = "battery_bottom.svg";
13 const string InsideSvg = "battery_inside_{0}.svg";
14@@ -49,8 +48,6 @@
15 int current_capacity;
16 uint timer;
17
18- Regex number_regex;
19-
20 double Capacity {
21 get {
22 return (double) current_capacity / max_capacity;
23@@ -71,10 +68,6 @@
24
25 public BatteryMonitor ()
26 {
27- number_regex = new Regex ("[0-9]+");
28-
29- max_capacity = GetBatteryCapacity ();
30-
31 UpdateBattStat ();
32
33 timer = GLib.Timeout.Add (20 * 1000, UpdateBattStat);
34@@ -90,66 +83,42 @@
35 RedrawIcon ();
36 }
37
38- int GetBatteryCapacity ()
39+ string ReadFile (string path)
40 {
41- int capacity = 1;
42-
43- if (File.Exists (BattInfoPath)) {
44- using (StreamReader reader = new StreamReader (BattInfoPath)) {
45- string line;
46- while (!reader.EndOfStream) {
47- line = reader.ReadLine ();
48- if (!line.StartsWith ("last full capacity"))
49- continue;
50-
51- try {
52- capacity = Convert.ToInt32 (number_regex.Matches (line) [0].Value);
53- } catch { }
54- break;
55- }
56- }
57- }
58-
59- return capacity;
60+ StreamReader file = new StreamReader (path);
61+ string r = file.ReadLine ();
62+ file.Close();
63+ return r;
64 }
65
66 bool UpdateBattStat ()
67 {
68- string capacity = null;
69- string chargeState = null;
70-
71- if (!File.Exists (BattStatePath)) {
72- SetText ("No Battery Found");
73- current_capacity = 0;
74- max_capacity = 1;
75-
76- RedrawIcon ();
77- return true;
78- }
79-
80- using (StreamReader reader = new StreamReader (BattStatePath)) {
81- string line;
82- while (!reader.EndOfStream) {
83- if (!string.IsNullOrEmpty (capacity) && !string.IsNullOrEmpty (chargeState))
84- break;
85-
86- line = reader.ReadLine ();
87- if (line.StartsWith ("remaining capacity")) {
88- capacity = line;
89+ current_capacity = 0;
90+ max_capacity = 0;
91+ string full_text = "";
92+ int n_devices = 0;
93+ try {
94+ foreach (string file in Directory.GetDirectories (PowerSupplyPath) ) {
95+ if (ReadFile (file + "/type") != "Battery")
96 continue;
97- }
98-
99- if (line.StartsWith ("charging state"))
100- chargeState = line;
101+ n_devices ++;
102+ if (n_devices != 1)
103+ full_text += " - ";
104+ int d_current = Convert.ToInt32 (ReadFile (file + "/charge_now"));
105+ int d_max = Convert.ToInt32 (ReadFile (file + "/charge_full"));
106+ string name = ReadFile (file + "/model_name");
107+ full_text += name + string.Format (" {0:0.0}%", 100 * d_current / d_max);
108+ current_capacity += d_current;
109+ max_capacity += d_max;
110 }
111- }
112-
113- try {
114- current_capacity = Convert.ToInt32 (number_regex.Matches (capacity) [0].Value);
115 } catch { }
116-
117+ if(n_devices == 0){
118+ max_capacity = 1;
119+ SetText ("No Battery Found");
120+ } else {
121+ SetText (full_text);
122+ }
123 RedrawIcon ();
124- SetText (string.Format ("{0:0.0}%", Capacity * 100));
125 return true;
126 }
127

Subscribers

People subscribed via source and target branches