Merge lp:~charlesk/indicator-power/lp-1416096-publish-primary-device-state-on-bus-15.04 into lp:indicator-power/15.04

Proposed by Charles Kerr
Status: Merged
Approved by: Ken VanDine
Approved revision: 277
Merged at revision: 276
Proposed branch: lp:~charlesk/indicator-power/lp-1416096-publish-primary-device-state-on-bus-15.04
Merge into: lp:indicator-power/15.04
Diff against target: 129 lines (+80/-7)
1 file modified
src/service.c (+80/-7)
To merge this branch: bzr merge lp:~charlesk/indicator-power/lp-1416096-publish-primary-device-state-on-bus-15.04
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Ken VanDine Approve
Ted Gould (community) Approve
Review via email: mp+248145@code.launchpad.net

Commit message

Publish the primary device's state for on the bus.

Description of the change

Publish the primary device's state on the bus for the benefit of u-s-s.

Published as a string, eg "charging", "discharging", "empty", "full", "unknown". In the simple case of a device with a single battery, that battery's state is used. In more complex cases, the state of a virtual aggregated battery will be used as described in <https://wiki.ubuntu.com/Power#Handling_multiple_batteries>.

To post a comment you must log in.
277. By Charles Kerr

in the new device-state action state, use the string 'unknown' if no primary device is found.

Revision history for this message
Ted Gould (ted) wrote :

Cool, thanks for the "unknown" fix. Looks good.

review: Approve
Revision history for this message
Ken VanDine (ken-vandine) wrote :

looks good and works great. Perfectly solves my use case!

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/service.c'
2--- src/service.c 2014-10-14 19:07:50 +0000
3+++ src/service.c 2015-01-30 17:59:25 +0000
4@@ -115,6 +115,7 @@
5 GSimpleActionGroup * actions;
6 GSimpleAction * header_action;
7 GSimpleAction * battery_level_action;
8+ GSimpleAction * device_state_action;
9 GSimpleAction * brightness_action;
10
11 IndicatorPowerDevice * primary_device;
12@@ -246,6 +247,74 @@
13 return ret;
14 }
15
16+static const char*
17+device_state_to_string(UpDeviceState device_state)
18+{
19+ const char * str;
20+
21+ switch (device_state)
22+ {
23+ case UP_DEVICE_STATE_CHARGING:
24+ str = "charging";
25+ break;
26+
27+ case UP_DEVICE_STATE_DISCHARGING:
28+ str = "discharging";
29+ break;
30+
31+ case UP_DEVICE_STATE_EMPTY:
32+ str = "empty";
33+ break;
34+
35+ case UP_DEVICE_STATE_FULLY_CHARGED:
36+ str = "fully-charged";
37+ break;
38+
39+ case UP_DEVICE_STATE_PENDING_CHARGE:
40+ str = "pending-charge";
41+ break;
42+
43+ case UP_DEVICE_STATE_PENDING_DISCHARGE:
44+ str = "pending-discharge";
45+ break;
46+
47+ default:
48+ str = "unknown";
49+ break;
50+ }
51+
52+ return str;
53+}
54+
55+static GVariant *
56+calculate_device_state_action_state (IndicatorPowerService * self)
57+{
58+ const priv_t * const p = self->priv;
59+ UpDeviceState device_state;
60+
61+ if (p->primary_device != NULL)
62+ device_state = indicator_power_device_get_state(p->primary_device);
63+ else
64+ device_state = UP_DEVICE_STATE_UNKNOWN;
65+
66+ return g_variant_new_string(device_state_to_string(device_state));
67+}
68+
69+static GVariant*
70+calculate_battery_level_action_state (IndicatorPowerService * self)
71+{
72+ const priv_t * const p = self->priv;
73+ guint32 battery_level;
74+
75+ if (p->primary_device == NULL)
76+ battery_level = 0;
77+ else
78+ battery_level = (guint32)(indicator_power_device_get_percentage (p->primary_device) + 0.5);
79+
80+ return g_variant_new_uint32 (battery_level);
81+}
82+
83+
84 /***
85 ****
86 **** HEADER SECTION
87@@ -786,11 +855,17 @@
88 p->header_action = a;
89
90 /* add the power-level action */
91- a = g_simple_action_new_stateful ("battery-level", NULL, g_variant_new_uint32(0));
92+ a = g_simple_action_new_stateful ("battery-level", NULL, calculate_battery_level_action_state(self));
93 g_simple_action_set_enabled (a, FALSE);
94 g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
95 p->battery_level_action = a;
96
97+ /* add the charge state action */
98+ a = g_simple_action_new_stateful ("device-state", NULL, calculate_device_state_action_state(self));
99+ g_simple_action_set_enabled (a, FALSE);
100+ g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
101+ p->device_state_action = a;
102+
103 /* add the auto-brightness action */
104 a = g_simple_action_new_stateful("auto-brightness", NULL, g_variant_new_boolean(FALSE));
105 g_object_bind_property_full(p->brightness, "auto-brightness",
106@@ -931,7 +1006,6 @@
107 on_devices_changed (IndicatorPowerService * self)
108 {
109 priv_t * p = self->priv;
110- guint32 battery_level;
111
112 /* update the device list */
113 g_list_free_full (p->devices, (GDestroyNotify)g_object_unref);
114@@ -948,11 +1022,10 @@
115 indicator_power_notifier_set_battery (p->notifier, NULL);
116
117 /* update the battery-level action's state */
118- if (p->primary_device == NULL)
119- battery_level = 0;
120- else
121- battery_level = (guint32)(indicator_power_device_get_percentage (p->primary_device) + 0.5);
122- g_simple_action_set_state (p->battery_level_action, g_variant_new_uint32 (battery_level));
123+ g_simple_action_set_state (p->battery_level_action, calculate_battery_level_action_state(self));
124+
125+ /* update the device-state action's state */
126+ g_simple_action_set_state (p->device_state_action, calculate_device_state_action_state(self));
127
128 rebuild_now (self, SECTION_HEADER | SECTION_DEVICES);
129 }

Subscribers

People subscribed via source and target branches