Merge lp:~charlesk/indicator-power/icon-policy into lp:indicator-power/1.0
- icon-policy
- Merge into trunk.2.0
Proposed by
Ted Gould
Status: | Merged |
---|---|
Merged at revision: | 125 |
Proposed branch: | lp:~charlesk/indicator-power/icon-policy |
Merge into: | lp:indicator-power/1.0 |
Diff against target: |
679 lines (+236/-161) 2 files modified
data/com.canonical.indicator.power.gschema.xml.in (+11/-1) src/indicator-power.c (+225/-160) |
To merge this branch: | bzr merge lp:~charlesk/indicator-power/icon-policy |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Lars Karlitski (community) | Approve | ||
Review via email: mp+92575@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
- 137. By Charles Kerr
-
make prototypes for update_visibility() and should_be_visible() align with the neighboring forward declarations
- 138. By Charles Kerr
-
remove unnecessary G_OBJECT() cast
- 139. By Charles Kerr
-
consistent use of ICON_POLICY_KEY
- 140. By Charles Kerr
-
remove unncessary private field 'visible'
- 141. By Charles Kerr
-
move POWER_INDICATOR
_ICON_POLICY_ * enum to the top of the file - 142. By Charles Kerr
-
ensure that we don't have a reference to the proxy or proxy_cancel fields in indicator_
power_dispose( ).
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'data/com.canonical.indicator.power.gschema.xml.in' | |||
2 | --- data/com.canonical.indicator.power.gschema.xml.in 2011-08-25 17:46:15 +0000 | |||
3 | +++ data/com.canonical.indicator.power.gschema.xml.in 2012-02-13 20:29:18 +0000 | |||
4 | @@ -1,9 +1,19 @@ | |||
5 | 1 | <schemalist> | 1 | <schemalist> |
6 | 2 | <enum id="icon-policy-enum"> | ||
7 | 3 | <value nick="present" value="0" /> | ||
8 | 4 | <value nick="charge" value="1" /> | ||
9 | 5 | <value nick="never" value="2" /> | ||
10 | 6 | </enum> | ||
11 | 2 | <schema gettext-domain="@GETTEXT_PACKAGE@" id="com.canonical.indicator.power" path="/com/canonical/indicator/power/"> | 7 | <schema gettext-domain="@GETTEXT_PACKAGE@" id="com.canonical.indicator.power" path="/com/canonical/indicator/power/"> |
12 | 3 | <key name="show-time" type="b"> | 8 | <key name="show-time" type="b"> |
13 | 4 | <default>false</default> | 9 | <default>false</default> |
14 | 5 | <_summary>Show time in Menu Bar</_summary> | 10 | <_summary>Show time in Menu Bar</_summary> |
16 | 6 | <_description>Whether show the time in the menu bar.</_description> | 11 | <_description>Whether or not to show the time in the menu bar.</_description> |
17 | 12 | </key> | ||
18 | 13 | <key enum="icon-policy-enum" name="icon-policy"> | ||
19 | 14 | <default>"present"</default> | ||
20 | 15 | <_summary>When to show the battery status in the menu bar.</_summary> | ||
21 | 16 | <_description>Options for when to show battery status. Valid options are "present", "charge", and "never".</_description> | ||
22 | 7 | </key> | 17 | </key> |
23 | 8 | </schema> | 18 | </schema> |
24 | 9 | </schemalist> | 19 | </schemalist> |
25 | 10 | 20 | ||
26 | === modified file 'src/indicator-power.c' | |||
27 | --- src/indicator-power.c 2011-11-08 21:38:58 +0000 | |||
28 | +++ src/indicator-power.c 2012-02-13 20:29:18 +0000 | |||
29 | @@ -24,7 +24,6 @@ | |||
30 | 24 | #endif | 24 | #endif |
31 | 25 | 25 | ||
32 | 26 | /* GStuff */ | 26 | /* GStuff */ |
33 | 27 | #include <glib.h> | ||
34 | 28 | #include <glib-object.h> | 27 | #include <glib-object.h> |
35 | 29 | #include <glib/gi18n-lib.h> | 28 | #include <glib/gi18n-lib.h> |
36 | 30 | #include <gio/gio.h> | 29 | #include <gio/gio.h> |
37 | @@ -36,6 +35,8 @@ | |||
38 | 36 | #include <libindicator/indicator.h> | 35 | #include <libindicator/indicator.h> |
39 | 37 | #include <libindicator/indicator-object.h> | 36 | #include <libindicator/indicator-object.h> |
40 | 38 | 37 | ||
41 | 38 | #define ICON_POLICY_KEY "icon-policy" | ||
42 | 39 | |||
43 | 39 | #define DEFAULT_ICON "gpm-battery-missing" | 40 | #define DEFAULT_ICON "gpm-battery-missing" |
44 | 40 | 41 | ||
45 | 41 | #define DBUS_SERVICE "org.gnome.SettingsDaemon" | 42 | #define DBUS_SERVICE "org.gnome.SettingsDaemon" |
46 | @@ -50,27 +51,25 @@ | |||
47 | 50 | #define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) | 51 | #define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) |
48 | 51 | #define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) | 52 | #define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) |
49 | 52 | 53 | ||
56 | 53 | typedef struct _IndicatorPower IndicatorPower; | 54 | enum { |
57 | 54 | typedef struct _IndicatorPowerClass IndicatorPowerClass; | 55 | POWER_INDICATOR_ICON_POLICY_PRESENT, |
58 | 55 | typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate; | 56 | POWER_INDICATOR_ICON_POLICY_CHARGE, |
59 | 56 | 57 | POWER_INDICATOR_ICON_POLICY_NEVER | |
60 | 57 | struct _IndicatorPower | 58 | }; |
61 | 58 | { | 59 | |
62 | 60 | GType indicator_power_get_type (void); | ||
63 | 61 | |||
64 | 62 | INDICATOR_SET_VERSION | ||
65 | 63 | INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) | ||
66 | 64 | |||
67 | 65 | typedef struct { | ||
68 | 66 | IndicatorObjectClass parent_class; | ||
69 | 67 | } | ||
70 | 68 | IndicatorPowerClass; | ||
71 | 69 | |||
72 | 70 | typedef struct { | ||
73 | 59 | IndicatorObject parent_instance; | 71 | IndicatorObject parent_instance; |
74 | 60 | 72 | ||
75 | 61 | IndicatorPowerPrivate *priv; | ||
76 | 62 | }; | ||
77 | 63 | |||
78 | 64 | struct _IndicatorPowerClass | ||
79 | 65 | { | ||
80 | 66 | IndicatorObjectClass parent_class; | ||
81 | 67 | }; | ||
82 | 68 | |||
83 | 69 | GType indicator_power_get_type (void) G_GNUC_CONST; | ||
84 | 70 | |||
85 | 71 | |||
86 | 72 | struct _IndicatorPowerPrivate | ||
87 | 73 | { | ||
88 | 74 | GtkMenu *menu; | 73 | GtkMenu *menu; |
89 | 75 | 74 | ||
90 | 76 | GtkLabel *label; | 75 | GtkLabel *label; |
91 | @@ -85,11 +84,10 @@ | |||
92 | 85 | GVariant *device; | 84 | GVariant *device; |
93 | 86 | 85 | ||
94 | 87 | GSettings *settings; | 86 | GSettings *settings; |
96 | 88 | }; | 87 | } |
97 | 88 | IndicatorPower; | ||
98 | 89 | 89 | ||
99 | 90 | /* Prototypes */ | 90 | /* Prototypes */ |
100 | 91 | static void indicator_power_class_init (IndicatorPowerClass *klass); | ||
101 | 92 | static void indicator_power_init (IndicatorPower *self); | ||
102 | 93 | static void indicator_power_dispose (GObject *object); | 91 | static void indicator_power_dispose (GObject *object); |
103 | 94 | static void indicator_power_finalize (GObject *object); | 92 | static void indicator_power_finalize (GObject *object); |
104 | 95 | 93 | ||
105 | @@ -99,15 +97,13 @@ | |||
106 | 99 | static const gchar* get_accessible_desc (IndicatorObject * io); | 97 | static const gchar* get_accessible_desc (IndicatorObject * io); |
107 | 100 | static const gchar* get_name_hint (IndicatorObject * io); | 98 | static const gchar* get_name_hint (IndicatorObject * io); |
108 | 101 | 99 | ||
109 | 100 | static void update_visibility (IndicatorPower * self); | ||
110 | 101 | static gboolean should_be_visible (IndicatorPower * self); | ||
111 | 102 | |||
112 | 103 | static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); | ||
113 | 102 | 104 | ||
114 | 103 | G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); | 105 | G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); |
115 | 104 | 106 | ||
116 | 105 | |||
117 | 106 | /* Indicator stuff */ | ||
118 | 107 | INDICATOR_SET_VERSION | ||
119 | 108 | INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) | ||
120 | 109 | |||
121 | 110 | |||
122 | 111 | static void | 107 | static void |
123 | 112 | indicator_power_class_init (IndicatorPowerClass *klass) | 108 | indicator_power_class_init (IndicatorPowerClass *klass) |
124 | 113 | { | 109 | { |
125 | @@ -122,8 +118,75 @@ | |||
126 | 122 | io_class->get_menu = get_menu; | 118 | io_class->get_menu = get_menu; |
127 | 123 | io_class->get_accessible_desc = get_accessible_desc; | 119 | io_class->get_accessible_desc = get_accessible_desc; |
128 | 124 | io_class->get_name_hint = get_name_hint; | 120 | io_class->get_name_hint = get_name_hint; |
131 | 125 | 121 | } | |
132 | 126 | g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate)); | 122 | |
133 | 123 | static void | ||
134 | 124 | indicator_power_init (IndicatorPower *self) | ||
135 | 125 | { | ||
136 | 126 | self->menu = GTK_MENU(gtk_menu_new()); | ||
137 | 127 | |||
138 | 128 | self->accessible_desc = NULL; | ||
139 | 129 | |||
140 | 130 | self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, | ||
141 | 131 | DBUS_SERVICE, | ||
142 | 132 | G_BUS_NAME_WATCHER_FLAGS_NONE, | ||
143 | 133 | gsd_appeared_callback, | ||
144 | 134 | NULL, | ||
145 | 135 | self, | ||
146 | 136 | NULL); | ||
147 | 137 | |||
148 | 138 | self->settings = g_settings_new ("com.canonical.indicator.power"); | ||
149 | 139 | g_signal_connect_swapped (self->settings, "changed::" ICON_POLICY_KEY, | ||
150 | 140 | G_CALLBACK(update_visibility), self); | ||
151 | 141 | g_object_set (G_OBJECT(self), | ||
152 | 142 | INDICATOR_OBJECT_DEFAULT_VISIBILITY, FALSE, | ||
153 | 143 | NULL); | ||
154 | 144 | } | ||
155 | 145 | |||
156 | 146 | static void | ||
157 | 147 | indicator_power_dispose (GObject *object) | ||
158 | 148 | { | ||
159 | 149 | IndicatorPower *self = INDICATOR_POWER(object); | ||
160 | 150 | |||
161 | 151 | if (self->devices != NULL) { | ||
162 | 152 | g_variant_unref (self->devices); | ||
163 | 153 | self->devices = NULL; | ||
164 | 154 | } | ||
165 | 155 | |||
166 | 156 | if (self->device != NULL) { | ||
167 | 157 | g_variant_unref (self->device); | ||
168 | 158 | self->device = NULL; | ||
169 | 159 | } | ||
170 | 160 | |||
171 | 161 | g_clear_object (&self->proxy); | ||
172 | 162 | g_clear_object (&self->proxy_cancel); | ||
173 | 163 | |||
174 | 164 | g_clear_object (&self->settings); | ||
175 | 165 | |||
176 | 166 | G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); | ||
177 | 167 | } | ||
178 | 168 | |||
179 | 169 | static void | ||
180 | 170 | indicator_power_finalize (GObject *object) | ||
181 | 171 | { | ||
182 | 172 | IndicatorPower *self = INDICATOR_POWER(object); | ||
183 | 173 | |||
184 | 174 | g_free (self->accessible_desc); | ||
185 | 175 | |||
186 | 176 | G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); | ||
187 | 177 | } | ||
188 | 178 | |||
189 | 179 | /*** | ||
190 | 180 | **** | ||
191 | 181 | ***/ | ||
192 | 182 | |||
193 | 183 | static void | ||
194 | 184 | spawn_command_line_async (const char * command) | ||
195 | 185 | { | ||
196 | 186 | GError * err = NULL; | ||
197 | 187 | if (!g_spawn_command_line_async (command, &err)) | ||
198 | 188 | g_warning ("Couldn't execute command \"%s\": %s", command, err->message); | ||
199 | 189 | g_clear_error (&err); | ||
200 | 127 | } | 190 | } |
201 | 128 | 191 | ||
202 | 129 | static void | 192 | static void |
203 | @@ -131,37 +194,21 @@ | |||
204 | 131 | gpointer data) | 194 | gpointer data) |
205 | 132 | { | 195 | { |
206 | 133 | /*TODO: show the statistics of the specific device*/ | 196 | /*TODO: show the statistics of the specific device*/ |
211 | 134 | const gchar *command = "gnome-power-statistics"; | 197 | spawn_command_line_async ("gnome-power-statistics"); |
208 | 135 | |||
209 | 136 | if (g_spawn_command_line_async (command, NULL) == FALSE) | ||
210 | 137 | g_warning ("Couldn't execute command: %s", command); | ||
212 | 138 | } | 198 | } |
213 | 139 | 199 | ||
214 | 140 | static void | 200 | static void |
217 | 141 | option_toggled_cb (GtkCheckMenuItem *item, | 201 | option_toggled_cb (GtkCheckMenuItem *item, IndicatorPower * self) |
216 | 142 | gpointer user_data) | ||
218 | 143 | { | 202 | { |
230 | 144 | IndicatorPower *self = INDICATOR_POWER (user_data); | 203 | gtk_widget_set_visible (GTK_WIDGET (self->label), |
231 | 145 | IndicatorPowerPrivate *priv = self->priv; | 204 | gtk_check_menu_item_get_active(item)); |
221 | 146 | gboolean visible; | ||
222 | 147 | |||
223 | 148 | visible = gtk_check_menu_item_get_active (item); | ||
224 | 149 | |||
225 | 150 | gtk_widget_set_visible (GTK_WIDGET (priv->label), | ||
226 | 151 | visible); | ||
227 | 152 | |||
228 | 153 | g_settings_set_boolean (priv->settings, "show-time", | ||
229 | 154 | visible); | ||
232 | 155 | } | 205 | } |
233 | 156 | 206 | ||
234 | 157 | static void | 207 | static void |
235 | 158 | show_preferences_cb (GtkMenuItem *item, | 208 | show_preferences_cb (GtkMenuItem *item, |
236 | 159 | gpointer data) | 209 | gpointer data) |
237 | 160 | { | 210 | { |
242 | 161 | const gchar *command = "gnome-control-center power"; | 211 | spawn_command_line_async ("gnome-control-center power"); |
239 | 162 | |||
240 | 163 | if (g_spawn_command_line_async (command, NULL) == FALSE) | ||
241 | 164 | g_warning ("Couldn't execute command: %s", command); | ||
243 | 165 | } | 212 | } |
244 | 166 | 213 | ||
245 | 167 | static void | 214 | static void |
246 | @@ -352,14 +399,12 @@ | |||
247 | 352 | set_accessible_desc (IndicatorPower *self, | 399 | set_accessible_desc (IndicatorPower *self, |
248 | 353 | const gchar *desc) | 400 | const gchar *desc) |
249 | 354 | { | 401 | { |
253 | 355 | IndicatorPowerPrivate *priv = self->priv; | 402 | if (desc == NULL || desc[0] == '\0') |
251 | 356 | |||
252 | 357 | if (desc == NULL || strlen(desc) == 0) | ||
254 | 358 | return; | 403 | return; |
255 | 359 | 404 | ||
257 | 360 | g_free (priv->accessible_desc); | 405 | g_free (self->accessible_desc); |
258 | 361 | 406 | ||
260 | 362 | priv->accessible_desc = g_strdup (desc); | 407 | self->accessible_desc = g_strdup (desc); |
261 | 363 | } | 408 | } |
262 | 364 | 409 | ||
263 | 365 | static const gchar * | 410 | static const gchar * |
264 | @@ -559,38 +604,32 @@ | |||
265 | 559 | static void | 604 | static void |
266 | 560 | build_menu (IndicatorPower *self) | 605 | build_menu (IndicatorPower *self) |
267 | 561 | { | 606 | { |
268 | 562 | IndicatorPowerPrivate *priv = self->priv; | ||
269 | 563 | GtkWidget *item; | 607 | GtkWidget *item; |
270 | 564 | GtkWidget *image; | 608 | GtkWidget *image; |
271 | 565 | GList *children; | 609 | GList *children; |
272 | 566 | gsize n_devices = 0; | 610 | gsize n_devices = 0; |
279 | 567 | gboolean visible; | 611 | |
280 | 568 | 612 | /* remove the existing menuitems */ | |
281 | 569 | if (priv->menu == NULL) | 613 | children = gtk_container_get_children (GTK_CONTAINER (self->menu)); |
276 | 570 | priv->menu = GTK_MENU (gtk_menu_new ()); | ||
277 | 571 | |||
278 | 572 | children = gtk_container_get_children (GTK_CONTAINER (priv->menu)); | ||
282 | 573 | g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL); | 614 | g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL); |
283 | 574 | g_list_free (children); | 615 | g_list_free (children); |
284 | 575 | 616 | ||
285 | 576 | /* devices */ | 617 | /* devices */ |
287 | 577 | n_devices = menu_add_devices (priv->menu, priv->devices); | 618 | n_devices = menu_add_devices (self->menu, self->devices); |
288 | 578 | 619 | ||
289 | 579 | if (!get_greeter_mode ()) { | 620 | if (!get_greeter_mode ()) { |
290 | 580 | /* only do the separator if we have at least one device */ | 621 | /* only do the separator if we have at least one device */ |
291 | 581 | if (n_devices != 0) | 622 | if (n_devices != 0) |
292 | 582 | { | 623 | { |
293 | 583 | item = gtk_separator_menu_item_new (); | 624 | item = gtk_separator_menu_item_new (); |
295 | 584 | gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); | 625 | gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); |
296 | 585 | } | 626 | } |
297 | 586 | 627 | ||
298 | 587 | /* options */ | 628 | /* options */ |
299 | 588 | item = gtk_check_menu_item_new_with_label (_("Show Time in Menu Bar")); | 629 | item = gtk_check_menu_item_new_with_label (_("Show Time in Menu Bar")); |
305 | 589 | g_signal_connect (G_OBJECT (item), "toggled", | 630 | g_signal_connect (item, "toggled", G_CALLBACK(option_toggled_cb), self); |
306 | 590 | G_CALLBACK (option_toggled_cb), self); | 631 | g_settings_bind (self->settings, "show-time", item, "active", G_SETTINGS_BIND_DEFAULT); |
307 | 591 | visible = g_settings_get_boolean (priv->settings, "show-time"); | 632 | gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); |
303 | 592 | gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), visible); | ||
304 | 593 | gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); | ||
308 | 594 | 633 | ||
309 | 595 | /* preferences */ | 634 | /* preferences */ |
310 | 596 | item = gtk_image_menu_item_new_with_label (_("Power Settings...")); | 635 | item = gtk_image_menu_item_new_with_label (_("Power Settings...")); |
311 | @@ -598,11 +637,11 @@ | |||
312 | 598 | gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image); | 637 | gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image); |
313 | 599 | g_signal_connect (G_OBJECT (item), "activate", | 638 | g_signal_connect (G_OBJECT (item), "activate", |
314 | 600 | G_CALLBACK (show_preferences_cb), NULL); | 639 | G_CALLBACK (show_preferences_cb), NULL); |
316 | 601 | gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); | 640 | gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); |
317 | 602 | } | 641 | } |
318 | 603 | 642 | ||
319 | 604 | /* show the menu */ | 643 | /* show the menu */ |
321 | 605 | gtk_widget_show_all (GTK_WIDGET (priv->menu)); | 644 | gtk_widget_show_all (GTK_WIDGET (self->menu)); |
322 | 606 | } | 645 | } |
323 | 607 | 646 | ||
324 | 608 | static GVariant * | 647 | static GVariant * |
325 | @@ -625,7 +664,7 @@ | |||
326 | 625 | gsize n_devices; | 664 | gsize n_devices; |
327 | 626 | guint i; | 665 | guint i; |
328 | 627 | 666 | ||
330 | 628 | n_devices = g_variant_n_children (devices); | 667 | n_devices = devices ? g_variant_n_children (devices) : 0; |
331 | 629 | g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); | 668 | g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); |
332 | 630 | 669 | ||
333 | 631 | for (i = 0; i < n_devices; i++) | 670 | for (i = 0; i < n_devices; i++) |
334 | @@ -699,7 +738,6 @@ | |||
335 | 699 | put_primary_device (IndicatorPower *self, | 738 | put_primary_device (IndicatorPower *self, |
336 | 700 | GVariant *device) | 739 | GVariant *device) |
337 | 701 | { | 740 | { |
338 | 702 | IndicatorPowerPrivate *priv = self->priv; | ||
339 | 703 | UpDeviceKind kind; | 741 | UpDeviceKind kind; |
340 | 704 | UpDeviceState state; | 742 | UpDeviceState state; |
341 | 705 | GIcon *device_gicons; | 743 | GIcon *device_gicons; |
342 | @@ -726,10 +764,10 @@ | |||
343 | 726 | 764 | ||
344 | 727 | /* set icon */ | 765 | /* set icon */ |
345 | 728 | device_gicons = get_device_icon (kind, state, time, device_icon); | 766 | device_gicons = get_device_icon (kind, state, time, device_icon); |
347 | 729 | gtk_image_set_from_gicon (priv->status_image, | 767 | gtk_image_set_from_gicon (self->status_image, |
348 | 730 | device_gicons, | 768 | device_gicons, |
349 | 731 | GTK_ICON_SIZE_LARGE_TOOLBAR); | 769 | GTK_ICON_SIZE_LARGE_TOOLBAR); |
351 | 732 | gtk_widget_show (GTK_WIDGET (priv->status_image)); | 770 | gtk_widget_show (GTK_WIDGET (self->status_image)); |
352 | 733 | 771 | ||
353 | 734 | 772 | ||
354 | 735 | /* get the device name */ | 773 | /* get the device name */ |
355 | @@ -738,7 +776,7 @@ | |||
356 | 738 | /* get the description */ | 776 | /* get the description */ |
357 | 739 | build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accesible_name); | 777 | build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accesible_name); |
358 | 740 | 778 | ||
360 | 741 | gtk_label_set_label (GTK_LABEL (priv->label), | 779 | gtk_label_set_label (GTK_LABEL (self->label), |
361 | 742 | short_details); | 780 | short_details); |
362 | 743 | set_accessible_desc (self, accesible_name); | 781 | set_accessible_desc (self, accesible_name); |
363 | 744 | 782 | ||
364 | @@ -755,32 +793,47 @@ | |||
365 | 755 | gpointer user_data) | 793 | gpointer user_data) |
366 | 756 | { | 794 | { |
367 | 757 | IndicatorPower *self = INDICATOR_POWER (user_data); | 795 | IndicatorPower *self = INDICATOR_POWER (user_data); |
368 | 758 | IndicatorPowerPrivate *priv = self->priv; | ||
369 | 759 | GVariant *devices_container; | 796 | GVariant *devices_container; |
370 | 760 | GError *error = NULL; | 797 | GError *error = NULL; |
371 | 761 | 798 | ||
372 | 762 | devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); | 799 | devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); |
373 | 763 | if (devices_container == NULL) | 800 | if (devices_container == NULL) |
374 | 764 | { | 801 | { |
376 | 765 | g_printerr ("Error getting devices: %s\n", error->message); | 802 | g_message ("Couldn't get devices: %s\n", error->message); |
377 | 766 | g_error_free (error); | 803 | g_error_free (error); |
378 | 767 | |||
379 | 768 | return; | ||
380 | 769 | } | 804 | } |
386 | 770 | priv->devices = g_variant_get_child_value (devices_container, 0); | 805 | else /* update 'devices' */ |
382 | 771 | g_variant_unref (devices_container); | ||
383 | 772 | |||
384 | 773 | priv->device = get_primary_device (priv->devices); | ||
385 | 774 | if (priv->device == NULL) | ||
387 | 775 | { | 806 | { |
391 | 776 | g_printerr ("Error getting primary device"); | 807 | if (self->devices != NULL) |
392 | 777 | 808 | g_variant_unref (self->devices); | |
393 | 778 | return; | 809 | self->devices = g_variant_get_child_value (devices_container, 0); |
394 | 810 | |||
395 | 811 | g_variant_unref (devices_container); | ||
396 | 812 | |||
397 | 813 | if (self->device != NULL) | ||
398 | 814 | g_variant_unref (self->device); | ||
399 | 815 | self->device = get_primary_device (self->devices); | ||
400 | 816 | |||
401 | 817 | if (self->device == NULL) | ||
402 | 818 | { | ||
403 | 819 | g_message ("Couldn't find primary device"); | ||
404 | 820 | } | ||
405 | 821 | else | ||
406 | 822 | { | ||
407 | 823 | put_primary_device (self, self->device); | ||
408 | 824 | } | ||
409 | 779 | } | 825 | } |
410 | 780 | 826 | ||
411 | 781 | put_primary_device (self, priv->device); | ||
412 | 782 | |||
413 | 783 | build_menu (self); | 827 | build_menu (self); |
414 | 828 | |||
415 | 829 | update_visibility (self); | ||
416 | 830 | } | ||
417 | 831 | |||
418 | 832 | static void | ||
419 | 833 | update_visibility (IndicatorPower * self) | ||
420 | 834 | { | ||
421 | 835 | indicator_object_set_visible (INDICATOR_OBJECT (self), | ||
422 | 836 | should_be_visible (self)); | ||
423 | 784 | } | 837 | } |
424 | 785 | 838 | ||
425 | 786 | static void | 839 | static void |
426 | @@ -791,17 +844,16 @@ | |||
427 | 791 | gpointer user_data) | 844 | gpointer user_data) |
428 | 792 | { | 845 | { |
429 | 793 | IndicatorPower *self = INDICATOR_POWER (user_data); | 846 | IndicatorPower *self = INDICATOR_POWER (user_data); |
430 | 794 | IndicatorPowerPrivate *priv = self->priv; | ||
431 | 795 | 847 | ||
432 | 796 | if (g_strcmp0 (signal_name, "Changed") == 0) | 848 | if (g_strcmp0 (signal_name, "Changed") == 0) |
433 | 797 | { | 849 | { |
434 | 798 | /* get the new state */ | 850 | /* get the new state */ |
436 | 799 | g_dbus_proxy_call (priv->proxy, | 851 | g_dbus_proxy_call (self->proxy, |
437 | 800 | "GetDevices", | 852 | "GetDevices", |
438 | 801 | NULL, | 853 | NULL, |
439 | 802 | G_DBUS_CALL_FLAGS_NONE, | 854 | G_DBUS_CALL_FLAGS_NONE, |
440 | 803 | -1, | 855 | -1, |
442 | 804 | priv->proxy_cancel, | 856 | self->proxy_cancel, |
443 | 805 | get_devices_cb, | 857 | get_devices_cb, |
444 | 806 | user_data); | 858 | user_data); |
445 | 807 | } | 859 | } |
446 | @@ -813,16 +865,11 @@ | |||
447 | 813 | gpointer user_data) | 865 | gpointer user_data) |
448 | 814 | { | 866 | { |
449 | 815 | IndicatorPower *self = INDICATOR_POWER (user_data); | 867 | IndicatorPower *self = INDICATOR_POWER (user_data); |
450 | 816 | IndicatorPowerPrivate *priv = self->priv; | ||
451 | 817 | GError *error = NULL; | 868 | GError *error = NULL; |
452 | 818 | 869 | ||
454 | 819 | priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); | 870 | self->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); |
455 | 820 | 871 | ||
461 | 821 | if (priv->proxy_cancel != NULL) | 872 | g_clear_object (&self->proxy_cancel); |
457 | 822 | { | ||
458 | 823 | g_object_unref (priv->proxy_cancel); | ||
459 | 824 | priv->proxy_cancel = NULL; | ||
460 | 825 | } | ||
462 | 826 | 873 | ||
463 | 827 | if (error != NULL) | 874 | if (error != NULL) |
464 | 828 | { | 875 | { |
465 | @@ -833,18 +880,18 @@ | |||
466 | 833 | } | 880 | } |
467 | 834 | 881 | ||
468 | 835 | /* we want to change the primary device changes */ | 882 | /* we want to change the primary device changes */ |
470 | 836 | g_signal_connect (priv->proxy, | 883 | g_signal_connect (self->proxy, |
471 | 837 | "g-signal", | 884 | "g-signal", |
472 | 838 | G_CALLBACK (receive_signal), | 885 | G_CALLBACK (receive_signal), |
473 | 839 | user_data); | 886 | user_data); |
474 | 840 | 887 | ||
475 | 841 | /* get the initial state */ | 888 | /* get the initial state */ |
477 | 842 | g_dbus_proxy_call (priv->proxy, | 889 | g_dbus_proxy_call (self->proxy, |
478 | 843 | "GetDevices", | 890 | "GetDevices", |
479 | 844 | NULL, | 891 | NULL, |
480 | 845 | G_DBUS_CALL_FLAGS_NONE, | 892 | G_DBUS_CALL_FLAGS_NONE, |
481 | 846 | -1, | 893 | -1, |
483 | 847 | priv->proxy_cancel, | 894 | self->proxy_cancel, |
484 | 848 | get_devices_cb, | 895 | get_devices_cb, |
485 | 849 | user_data); | 896 | user_data); |
486 | 850 | } | 897 | } |
487 | @@ -856,9 +903,8 @@ | |||
488 | 856 | gpointer user_data) | 903 | gpointer user_data) |
489 | 857 | { | 904 | { |
490 | 858 | IndicatorPower *self = INDICATOR_POWER (user_data); | 905 | IndicatorPower *self = INDICATOR_POWER (user_data); |
491 | 859 | IndicatorPowerPrivate *priv = self->priv; | ||
492 | 860 | 906 | ||
494 | 861 | priv->proxy_cancel = g_cancellable_new (); | 907 | self->proxy_cancel = g_cancellable_new (); |
495 | 862 | 908 | ||
496 | 863 | g_dbus_proxy_new (connection, | 909 | g_dbus_proxy_new (connection, |
497 | 864 | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, | 910 | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, |
498 | @@ -866,50 +912,11 @@ | |||
499 | 866 | name, | 912 | name, |
500 | 867 | POWER_DBUS_PATH, | 913 | POWER_DBUS_PATH, |
501 | 868 | POWER_DBUS_INTERFACE, | 914 | POWER_DBUS_INTERFACE, |
503 | 869 | priv->proxy_cancel, | 915 | self->proxy_cancel, |
504 | 870 | service_proxy_cb, | 916 | service_proxy_cb, |
505 | 871 | self); | 917 | self); |
506 | 872 | } | 918 | } |
507 | 873 | 919 | ||
508 | 874 | static void | ||
509 | 875 | indicator_power_init (IndicatorPower *self) | ||
510 | 876 | { | ||
511 | 877 | IndicatorPowerPrivate *priv; | ||
512 | 878 | |||
513 | 879 | self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, | ||
514 | 880 | INDICATOR_POWER_TYPE, | ||
515 | 881 | IndicatorPowerPrivate); | ||
516 | 882 | priv = self->priv; | ||
517 | 883 | |||
518 | 884 | /* Init variables */ | ||
519 | 885 | priv->menu = NULL; | ||
520 | 886 | priv->accessible_desc = NULL; | ||
521 | 887 | |||
522 | 888 | |||
523 | 889 | priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, | ||
524 | 890 | DBUS_SERVICE, | ||
525 | 891 | G_BUS_NAME_WATCHER_FLAGS_NONE, | ||
526 | 892 | gsd_appeared_callback, | ||
527 | 893 | NULL, | ||
528 | 894 | self, | ||
529 | 895 | NULL); | ||
530 | 896 | |||
531 | 897 | /* GSettings */ | ||
532 | 898 | priv->settings = g_settings_new ("com.canonical.indicator.power"); | ||
533 | 899 | } | ||
534 | 900 | |||
535 | 901 | static void | ||
536 | 902 | indicator_power_dispose (GObject *object) | ||
537 | 903 | { | ||
538 | 904 | G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); | ||
539 | 905 | } | ||
540 | 906 | |||
541 | 907 | static void | ||
542 | 908 | indicator_power_finalize (GObject *object) | ||
543 | 909 | { | ||
544 | 910 | G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); | ||
545 | 911 | } | ||
546 | 912 | |||
547 | 913 | 920 | ||
548 | 914 | 921 | ||
549 | 915 | 922 | ||
550 | @@ -919,54 +926,50 @@ | |||
551 | 919 | get_label (IndicatorObject *io) | 926 | get_label (IndicatorObject *io) |
552 | 920 | { | 927 | { |
553 | 921 | IndicatorPower *self = INDICATOR_POWER (io); | 928 | IndicatorPower *self = INDICATOR_POWER (io); |
554 | 922 | IndicatorPowerPrivate *priv = self->priv; | ||
555 | 923 | 929 | ||
557 | 924 | if (priv->label == NULL) | 930 | if (self->label == NULL) |
558 | 925 | { | 931 | { |
559 | 926 | /* Create the label if it doesn't exist already */ | 932 | /* Create the label if it doesn't exist already */ |
562 | 927 | priv->label = GTK_LABEL (gtk_label_new ("")); | 933 | self->label = GTK_LABEL (gtk_label_new ("")); |
563 | 928 | gtk_widget_set_visible (GTK_WIDGET (priv->label), FALSE); | 934 | gtk_widget_set_visible (GTK_WIDGET (self->label), FALSE); |
564 | 929 | } | 935 | } |
565 | 930 | 936 | ||
567 | 931 | return priv->label; | 937 | return self->label; |
568 | 932 | } | 938 | } |
569 | 933 | 939 | ||
570 | 934 | static GtkImage * | 940 | static GtkImage * |
571 | 935 | get_image (IndicatorObject *io) | 941 | get_image (IndicatorObject *io) |
572 | 936 | { | 942 | { |
573 | 937 | IndicatorPower *self = INDICATOR_POWER (io); | 943 | IndicatorPower *self = INDICATOR_POWER (io); |
574 | 938 | IndicatorPowerPrivate *priv = self->priv; | ||
575 | 939 | GIcon *gicon; | 944 | GIcon *gicon; |
576 | 940 | 945 | ||
578 | 941 | if (priv->status_image == NULL) | 946 | if (self->status_image == NULL) |
579 | 942 | { | 947 | { |
580 | 943 | /* Will create the status icon if it doesn't exist already */ | 948 | /* Will create the status icon if it doesn't exist already */ |
581 | 944 | gicon = g_themed_icon_new (DEFAULT_ICON); | 949 | gicon = g_themed_icon_new (DEFAULT_ICON); |
583 | 945 | priv->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon, | 950 | self->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon, |
584 | 946 | GTK_ICON_SIZE_LARGE_TOOLBAR)); | 951 | GTK_ICON_SIZE_LARGE_TOOLBAR)); |
585 | 947 | } | 952 | } |
586 | 948 | 953 | ||
588 | 949 | return priv->status_image; | 954 | return self->status_image; |
589 | 950 | } | 955 | } |
590 | 951 | 956 | ||
591 | 952 | static GtkMenu * | 957 | static GtkMenu * |
592 | 953 | get_menu (IndicatorObject *io) | 958 | get_menu (IndicatorObject *io) |
593 | 954 | { | 959 | { |
594 | 955 | IndicatorPower *self = INDICATOR_POWER (io); | 960 | IndicatorPower *self = INDICATOR_POWER (io); |
595 | 956 | IndicatorPowerPrivate *priv = self->priv; | ||
596 | 957 | 961 | ||
597 | 958 | build_menu (self); | 962 | build_menu (self); |
598 | 959 | 963 | ||
600 | 960 | return GTK_MENU (priv->menu); | 964 | return GTK_MENU (self->menu); |
601 | 961 | } | 965 | } |
602 | 962 | 966 | ||
603 | 963 | static const gchar * | 967 | static const gchar * |
604 | 964 | get_accessible_desc (IndicatorObject *io) | 968 | get_accessible_desc (IndicatorObject *io) |
605 | 965 | { | 969 | { |
606 | 966 | IndicatorPower *self = INDICATOR_POWER (io); | 970 | IndicatorPower *self = INDICATOR_POWER (io); |
607 | 967 | IndicatorPowerPrivate *priv = self->priv; | ||
608 | 968 | 971 | ||
610 | 969 | return priv->accessible_desc; | 972 | return self->accessible_desc; |
611 | 970 | } | 973 | } |
612 | 971 | 974 | ||
613 | 972 | static const gchar * | 975 | static const gchar * |
614 | @@ -974,3 +977,65 @@ | |||
615 | 974 | { | 977 | { |
616 | 975 | return PACKAGE_NAME; | 978 | return PACKAGE_NAME; |
617 | 976 | } | 979 | } |
618 | 980 | |||
619 | 981 | /*** | ||
620 | 982 | **** | ||
621 | 983 | ***/ | ||
622 | 984 | |||
623 | 985 | static void | ||
624 | 986 | count_batteries(GVariant *devices, int *total, int *inuse) | ||
625 | 987 | { | ||
626 | 988 | const int n_devices = devices ? g_variant_n_children (devices) : 0; | ||
627 | 989 | |||
628 | 990 | int i; | ||
629 | 991 | for (i=0; i<n_devices; i++) | ||
630 | 992 | { | ||
631 | 993 | GVariant * device = g_variant_get_child_value (devices, i); | ||
632 | 994 | |||
633 | 995 | UpDeviceKind kind; | ||
634 | 996 | g_variant_get_child (device, 1, "u", &kind); | ||
635 | 997 | if (kind == UP_DEVICE_KIND_BATTERY) | ||
636 | 998 | { | ||
637 | 999 | ++*total; | ||
638 | 1000 | |||
639 | 1001 | UpDeviceState state; | ||
640 | 1002 | g_variant_get_child (device, 4, "u", &state); | ||
641 | 1003 | if ((state == UP_DEVICE_STATE_CHARGING) || (state == UP_DEVICE_STATE_DISCHARGING)) | ||
642 | 1004 | ++*inuse; | ||
643 | 1005 | } | ||
644 | 1006 | } | ||
645 | 1007 | |||
646 | 1008 | g_debug("count_batteries found %d batteries (%d are charging/discharging)", *total, *inuse); | ||
647 | 1009 | } | ||
648 | 1010 | |||
649 | 1011 | static gboolean | ||
650 | 1012 | should_be_visible (IndicatorPower * self) | ||
651 | 1013 | { | ||
652 | 1014 | gboolean visible = TRUE; | ||
653 | 1015 | |||
654 | 1016 | const int icon_policy = g_settings_get_enum (self->settings, ICON_POLICY_KEY); | ||
655 | 1017 | |||
656 | 1018 | g_debug ("icon_policy is: %d (present==0, charge==1, never==2)", icon_policy); | ||
657 | 1019 | |||
658 | 1020 | if (icon_policy == POWER_INDICATOR_ICON_POLICY_NEVER) | ||
659 | 1021 | { | ||
660 | 1022 | visible = FALSE; | ||
661 | 1023 | } | ||
662 | 1024 | else | ||
663 | 1025 | { | ||
664 | 1026 | int batteries=0, inuse=0; | ||
665 | 1027 | count_batteries (self->devices, &batteries, &inuse); | ||
666 | 1028 | |||
667 | 1029 | if (icon_policy == POWER_INDICATOR_ICON_POLICY_PRESENT) | ||
668 | 1030 | { | ||
669 | 1031 | visible = batteries > 0; | ||
670 | 1032 | } | ||
671 | 1033 | else if (icon_policy == POWER_INDICATOR_ICON_POLICY_CHARGE) | ||
672 | 1034 | { | ||
673 | 1035 | visible = inuse > 0; | ||
674 | 1036 | } | ||
675 | 1037 | } | ||
676 | 1038 | |||
677 | 1039 | g_debug ("should_be_visible: %s", visible?"yes":"no"); | ||
678 | 1040 | return visible; | ||
679 | 1041 | } |
I've reviewed this exact code in another merge:
https:/ /code.launchpad .net/~charlesk/ indicator- power/icon- policy/ +merge/ 91483
Approving it here, too.