Merge lp:~charlesk/indicator-power/icon-policy into lp:indicator-power/1.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
Reviewer Review Type Date Requested Status
Lars Karlitski (community) Approve
Review via email: mp+92575@code.launchpad.net
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().

Revision history for this message
Lars Karlitski (larsu) wrote :

I've reviewed this exact code in another merge:

  https://code.launchpad.net/~charlesk/indicator-power/icon-policy/+merge/91483

Approving it here, too.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'data/com.canonical.indicator.power.gschema.xml.in'
--- data/com.canonical.indicator.power.gschema.xml.in 2011-08-25 17:46:15 +0000
+++ data/com.canonical.indicator.power.gschema.xml.in 2012-02-13 20:29:18 +0000
@@ -1,9 +1,19 @@
1<schemalist>1<schemalist>
2 <enum id="icon-policy-enum">
3 <value nick="present" value="0" />
4 <value nick="charge" value="1" />
5 <value nick="never" value="2" />
6 </enum>
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/">
3 <key name="show-time" type="b">8 <key name="show-time" type="b">
4 <default>false</default>9 <default>false</default>
5 <_summary>Show time in Menu Bar</_summary>10 <_summary>Show time in Menu Bar</_summary>
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>
12 </key>
13 <key enum="icon-policy-enum" name="icon-policy">
14 <default>"present"</default>
15 <_summary>When to show the battery status in the menu bar.</_summary>
16 <_description>Options for when to show battery status. Valid options are "present", "charge", and "never".</_description>
7 </key>17 </key>
8 </schema>18 </schema>
9</schemalist>19</schemalist>
1020
=== modified file 'src/indicator-power.c'
--- src/indicator-power.c 2011-11-08 21:38:58 +0000
+++ src/indicator-power.c 2012-02-13 20:29:18 +0000
@@ -24,7 +24,6 @@
24#endif24#endif
2525
26/* GStuff */26/* GStuff */
27#include <glib.h>
28#include <glib-object.h>27#include <glib-object.h>
29#include <glib/gi18n-lib.h>28#include <glib/gi18n-lib.h>
30#include <gio/gio.h>29#include <gio/gio.h>
@@ -36,6 +35,8 @@
36#include <libindicator/indicator.h>35#include <libindicator/indicator.h>
37#include <libindicator/indicator-object.h>36#include <libindicator/indicator-object.h>
3837
38#define ICON_POLICY_KEY "icon-policy"
39
39#define DEFAULT_ICON "gpm-battery-missing"40#define DEFAULT_ICON "gpm-battery-missing"
4041
41#define DBUS_SERVICE "org.gnome.SettingsDaemon"42#define DBUS_SERVICE "org.gnome.SettingsDaemon"
@@ -50,27 +51,25 @@
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))
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))
5253
53typedef struct _IndicatorPower IndicatorPower;54enum {
54typedef struct _IndicatorPowerClass IndicatorPowerClass;55 POWER_INDICATOR_ICON_POLICY_PRESENT,
55typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate;56 POWER_INDICATOR_ICON_POLICY_CHARGE,
5657 POWER_INDICATOR_ICON_POLICY_NEVER
57struct _IndicatorPower58};
58{59
60GType indicator_power_get_type (void);
61
62INDICATOR_SET_VERSION
63INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE)
64
65typedef struct {
66 IndicatorObjectClass parent_class;
67}
68IndicatorPowerClass;
69
70typedef struct {
59 IndicatorObject parent_instance;71 IndicatorObject parent_instance;
6072
61 IndicatorPowerPrivate *priv;
62};
63
64struct _IndicatorPowerClass
65{
66 IndicatorObjectClass parent_class;
67};
68
69GType indicator_power_get_type (void) G_GNUC_CONST;
70
71
72struct _IndicatorPowerPrivate
73{
74 GtkMenu *menu;73 GtkMenu *menu;
7574
76 GtkLabel *label;75 GtkLabel *label;
@@ -85,11 +84,10 @@
85 GVariant *device;84 GVariant *device;
8685
87 GSettings *settings;86 GSettings *settings;
88};87}
88IndicatorPower;
8989
90/* Prototypes */90/* Prototypes */
91static void indicator_power_class_init (IndicatorPowerClass *klass);
92static void indicator_power_init (IndicatorPower *self);
93static void indicator_power_dispose (GObject *object);91static void indicator_power_dispose (GObject *object);
94static void indicator_power_finalize (GObject *object);92static void indicator_power_finalize (GObject *object);
9593
@@ -99,15 +97,13 @@
99static const gchar* get_accessible_desc (IndicatorObject * io);97static const gchar* get_accessible_desc (IndicatorObject * io);
100static const gchar* get_name_hint (IndicatorObject * io);98static const gchar* get_name_hint (IndicatorObject * io);
10199
100static void update_visibility (IndicatorPower * self);
101static gboolean should_be_visible (IndicatorPower * self);
102
103static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data);
102104
103G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE);105G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE);
104106
105
106/* Indicator stuff */
107INDICATOR_SET_VERSION
108INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE)
109
110
111static void107static void
112indicator_power_class_init (IndicatorPowerClass *klass)108indicator_power_class_init (IndicatorPowerClass *klass)
113{109{
@@ -122,8 +118,75 @@
122 io_class->get_menu = get_menu;118 io_class->get_menu = get_menu;
123 io_class->get_accessible_desc = get_accessible_desc;119 io_class->get_accessible_desc = get_accessible_desc;
124 io_class->get_name_hint = get_name_hint;120 io_class->get_name_hint = get_name_hint;
125121}
126 g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate));122
123static void
124indicator_power_init (IndicatorPower *self)
125{
126 self->menu = GTK_MENU(gtk_menu_new());
127
128 self->accessible_desc = NULL;
129
130 self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
131 DBUS_SERVICE,
132 G_BUS_NAME_WATCHER_FLAGS_NONE,
133 gsd_appeared_callback,
134 NULL,
135 self,
136 NULL);
137
138 self->settings = g_settings_new ("com.canonical.indicator.power");
139 g_signal_connect_swapped (self->settings, "changed::" ICON_POLICY_KEY,
140 G_CALLBACK(update_visibility), self);
141 g_object_set (G_OBJECT(self),
142 INDICATOR_OBJECT_DEFAULT_VISIBILITY, FALSE,
143 NULL);
144}
145
146static void
147indicator_power_dispose (GObject *object)
148{
149 IndicatorPower *self = INDICATOR_POWER(object);
150
151 if (self->devices != NULL) {
152 g_variant_unref (self->devices);
153 self->devices = NULL;
154 }
155
156 if (self->device != NULL) {
157 g_variant_unref (self->device);
158 self->device = NULL;
159 }
160
161 g_clear_object (&self->proxy);
162 g_clear_object (&self->proxy_cancel);
163
164 g_clear_object (&self->settings);
165
166 G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object);
167}
168
169static void
170indicator_power_finalize (GObject *object)
171{
172 IndicatorPower *self = INDICATOR_POWER(object);
173
174 g_free (self->accessible_desc);
175
176 G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object);
177}
178
179/***
180****
181***/
182
183static void
184spawn_command_line_async (const char * command)
185{
186 GError * err = NULL;
187 if (!g_spawn_command_line_async (command, &err))
188 g_warning ("Couldn't execute command \"%s\": %s", command, err->message);
189 g_clear_error (&err);
127}190}
128191
129static void192static void
@@ -131,37 +194,21 @@
131 gpointer data)194 gpointer data)
132{195{
133 /*TODO: show the statistics of the specific device*/196 /*TODO: show the statistics of the specific device*/
134 const gchar *command = "gnome-power-statistics";197 spawn_command_line_async ("gnome-power-statistics");
135
136 if (g_spawn_command_line_async (command, NULL) == FALSE)
137 g_warning ("Couldn't execute command: %s", command);
138}198}
139199
140static void200static void
141option_toggled_cb (GtkCheckMenuItem *item,201option_toggled_cb (GtkCheckMenuItem *item, IndicatorPower * self)
142 gpointer user_data)
143{202{
144 IndicatorPower *self = INDICATOR_POWER (user_data);203 gtk_widget_set_visible (GTK_WIDGET (self->label),
145 IndicatorPowerPrivate *priv = self->priv;204 gtk_check_menu_item_get_active(item));
146 gboolean visible;
147
148 visible = gtk_check_menu_item_get_active (item);
149
150 gtk_widget_set_visible (GTK_WIDGET (priv->label),
151 visible);
152
153 g_settings_set_boolean (priv->settings, "show-time",
154 visible);
155}205}
156206
157static void207static void
158show_preferences_cb (GtkMenuItem *item,208show_preferences_cb (GtkMenuItem *item,
159 gpointer data)209 gpointer data)
160{210{
161 const gchar *command = "gnome-control-center power";211 spawn_command_line_async ("gnome-control-center power");
162
163 if (g_spawn_command_line_async (command, NULL) == FALSE)
164 g_warning ("Couldn't execute command: %s", command);
165}212}
166213
167static void214static void
@@ -352,14 +399,12 @@
352set_accessible_desc (IndicatorPower *self,399set_accessible_desc (IndicatorPower *self,
353 const gchar *desc)400 const gchar *desc)
354{401{
355 IndicatorPowerPrivate *priv = self->priv;402 if (desc == NULL || desc[0] == '\0')
356
357 if (desc == NULL || strlen(desc) == 0)
358 return;403 return;
359404
360 g_free (priv->accessible_desc);405 g_free (self->accessible_desc);
361406
362 priv->accessible_desc = g_strdup (desc);407 self->accessible_desc = g_strdup (desc);
363}408}
364409
365static const gchar *410static const gchar *
@@ -559,38 +604,32 @@
559static void604static void
560build_menu (IndicatorPower *self)605build_menu (IndicatorPower *self)
561{606{
562 IndicatorPowerPrivate *priv = self->priv;
563 GtkWidget *item;607 GtkWidget *item;
564 GtkWidget *image;608 GtkWidget *image;
565 GList *children;609 GList *children;
566 gsize n_devices = 0;610 gsize n_devices = 0;
567 gboolean visible;611
568612 /* remove the existing menuitems */
569 if (priv->menu == NULL)613 children = gtk_container_get_children (GTK_CONTAINER (self->menu));
570 priv->menu = GTK_MENU (gtk_menu_new ());
571
572 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
573 g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL);614 g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL);
574 g_list_free (children);615 g_list_free (children);
575616
576 /* devices */617 /* devices */
577 n_devices = menu_add_devices (priv->menu, priv->devices);618 n_devices = menu_add_devices (self->menu, self->devices);
578619
579 if (!get_greeter_mode ()) {620 if (!get_greeter_mode ()) {
580 /* only do the separator if we have at least one device */621 /* only do the separator if we have at least one device */
581 if (n_devices != 0)622 if (n_devices != 0)
582 {623 {
583 item = gtk_separator_menu_item_new ();624 item = gtk_separator_menu_item_new ();
584 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);625 gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item);
585 }626 }
586627
587 /* options */628 /* options */
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"));
589 g_signal_connect (G_OBJECT (item), "toggled",630 g_signal_connect (item, "toggled", G_CALLBACK(option_toggled_cb), self);
590 G_CALLBACK (option_toggled_cb), self);631 g_settings_bind (self->settings, "show-time", item, "active", G_SETTINGS_BIND_DEFAULT);
591 visible = g_settings_get_boolean (priv->settings, "show-time");632 gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item);
592 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), visible);
593 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);
594633
595 /* preferences */634 /* preferences */
596 item = gtk_image_menu_item_new_with_label (_("Power Settings..."));635 item = gtk_image_menu_item_new_with_label (_("Power Settings..."));
@@ -598,11 +637,11 @@
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);
599 g_signal_connect (G_OBJECT (item), "activate",638 g_signal_connect (G_OBJECT (item), "activate",
600 G_CALLBACK (show_preferences_cb), NULL);639 G_CALLBACK (show_preferences_cb), NULL);
601 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item);640 gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item);
602 }641 }
603642
604 /* show the menu */643 /* show the menu */
605 gtk_widget_show_all (GTK_WIDGET (priv->menu));644 gtk_widget_show_all (GTK_WIDGET (self->menu));
606}645}
607646
608static GVariant *647static GVariant *
@@ -625,7 +664,7 @@
625 gsize n_devices;664 gsize n_devices;
626 guint i;665 guint i;
627666
628 n_devices = g_variant_n_children (devices);667 n_devices = devices ? g_variant_n_children (devices) : 0;
629 g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices);668 g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices);
630669
631 for (i = 0; i < n_devices; i++)670 for (i = 0; i < n_devices; i++)
@@ -699,7 +738,6 @@
699put_primary_device (IndicatorPower *self,738put_primary_device (IndicatorPower *self,
700 GVariant *device)739 GVariant *device)
701{740{
702 IndicatorPowerPrivate *priv = self->priv;
703 UpDeviceKind kind;741 UpDeviceKind kind;
704 UpDeviceState state;742 UpDeviceState state;
705 GIcon *device_gicons;743 GIcon *device_gicons;
@@ -726,10 +764,10 @@
726764
727 /* set icon */765 /* set icon */
728 device_gicons = get_device_icon (kind, state, time, device_icon);766 device_gicons = get_device_icon (kind, state, time, device_icon);
729 gtk_image_set_from_gicon (priv->status_image,767 gtk_image_set_from_gicon (self->status_image,
730 device_gicons,768 device_gicons,
731 GTK_ICON_SIZE_LARGE_TOOLBAR);769 GTK_ICON_SIZE_LARGE_TOOLBAR);
732 gtk_widget_show (GTK_WIDGET (priv->status_image));770 gtk_widget_show (GTK_WIDGET (self->status_image));
733771
734772
735 /* get the device name */773 /* get the device name */
@@ -738,7 +776,7 @@
738 /* get the description */776 /* get the description */
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);
740778
741 gtk_label_set_label (GTK_LABEL (priv->label),779 gtk_label_set_label (GTK_LABEL (self->label),
742 short_details);780 short_details);
743 set_accessible_desc (self, accesible_name);781 set_accessible_desc (self, accesible_name);
744782
@@ -755,32 +793,47 @@
755 gpointer user_data)793 gpointer user_data)
756{794{
757 IndicatorPower *self = INDICATOR_POWER (user_data);795 IndicatorPower *self = INDICATOR_POWER (user_data);
758 IndicatorPowerPrivate *priv = self->priv;
759 GVariant *devices_container;796 GVariant *devices_container;
760 GError *error = NULL;797 GError *error = NULL;
761798
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);
763 if (devices_container == NULL)800 if (devices_container == NULL)
764 {801 {
765 g_printerr ("Error getting devices: %s\n", error->message);802 g_message ("Couldn't get devices: %s\n", error->message);
766 g_error_free (error);803 g_error_free (error);
767
768 return;
769 }804 }
770 priv->devices = g_variant_get_child_value (devices_container, 0);805 else /* update 'devices' */
771 g_variant_unref (devices_container);
772
773 priv->device = get_primary_device (priv->devices);
774 if (priv->device == NULL)
775 {806 {
776 g_printerr ("Error getting primary device");807 if (self->devices != NULL)
777808 g_variant_unref (self->devices);
778 return;809 self->devices = g_variant_get_child_value (devices_container, 0);
810
811 g_variant_unref (devices_container);
812
813 if (self->device != NULL)
814 g_variant_unref (self->device);
815 self->device = get_primary_device (self->devices);
816
817 if (self->device == NULL)
818 {
819 g_message ("Couldn't find primary device");
820 }
821 else
822 {
823 put_primary_device (self, self->device);
824 }
779 }825 }
780826
781 put_primary_device (self, priv->device);
782
783 build_menu (self);827 build_menu (self);
828
829 update_visibility (self);
830}
831
832static void
833update_visibility (IndicatorPower * self)
834{
835 indicator_object_set_visible (INDICATOR_OBJECT (self),
836 should_be_visible (self));
784}837}
785838
786static void839static void
@@ -791,17 +844,16 @@
791 gpointer user_data)844 gpointer user_data)
792{845{
793 IndicatorPower *self = INDICATOR_POWER (user_data);846 IndicatorPower *self = INDICATOR_POWER (user_data);
794 IndicatorPowerPrivate *priv = self->priv;
795847
796 if (g_strcmp0 (signal_name, "Changed") == 0)848 if (g_strcmp0 (signal_name, "Changed") == 0)
797 {849 {
798 /* get the new state */850 /* get the new state */
799 g_dbus_proxy_call (priv->proxy,851 g_dbus_proxy_call (self->proxy,
800 "GetDevices",852 "GetDevices",
801 NULL,853 NULL,
802 G_DBUS_CALL_FLAGS_NONE,854 G_DBUS_CALL_FLAGS_NONE,
803 -1,855 -1,
804 priv->proxy_cancel,856 self->proxy_cancel,
805 get_devices_cb,857 get_devices_cb,
806 user_data);858 user_data);
807 }859 }
@@ -813,16 +865,11 @@
813 gpointer user_data)865 gpointer user_data)
814{866{
815 IndicatorPower *self = INDICATOR_POWER (user_data);867 IndicatorPower *self = INDICATOR_POWER (user_data);
816 IndicatorPowerPrivate *priv = self->priv;
817 GError *error = NULL;868 GError *error = NULL;
818869
819 priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);870 self->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
820871
821 if (priv->proxy_cancel != NULL)872 g_clear_object (&self->proxy_cancel);
822 {
823 g_object_unref (priv->proxy_cancel);
824 priv->proxy_cancel = NULL;
825 }
826873
827 if (error != NULL)874 if (error != NULL)
828 {875 {
@@ -833,18 +880,18 @@
833 }880 }
834881
835 /* we want to change the primary device changes */882 /* we want to change the primary device changes */
836 g_signal_connect (priv->proxy,883 g_signal_connect (self->proxy,
837 "g-signal",884 "g-signal",
838 G_CALLBACK (receive_signal),885 G_CALLBACK (receive_signal),
839 user_data);886 user_data);
840887
841 /* get the initial state */888 /* get the initial state */
842 g_dbus_proxy_call (priv->proxy,889 g_dbus_proxy_call (self->proxy,
843 "GetDevices",890 "GetDevices",
844 NULL,891 NULL,
845 G_DBUS_CALL_FLAGS_NONE,892 G_DBUS_CALL_FLAGS_NONE,
846 -1,893 -1,
847 priv->proxy_cancel,894 self->proxy_cancel,
848 get_devices_cb,895 get_devices_cb,
849 user_data);896 user_data);
850}897}
@@ -856,9 +903,8 @@
856 gpointer user_data)903 gpointer user_data)
857{904{
858 IndicatorPower *self = INDICATOR_POWER (user_data);905 IndicatorPower *self = INDICATOR_POWER (user_data);
859 IndicatorPowerPrivate *priv = self->priv;
860906
861 priv->proxy_cancel = g_cancellable_new ();907 self->proxy_cancel = g_cancellable_new ();
862908
863 g_dbus_proxy_new (connection,909 g_dbus_proxy_new (connection,
864 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,910 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
@@ -866,50 +912,11 @@
866 name,912 name,
867 POWER_DBUS_PATH,913 POWER_DBUS_PATH,
868 POWER_DBUS_INTERFACE,914 POWER_DBUS_INTERFACE,
869 priv->proxy_cancel,915 self->proxy_cancel,
870 service_proxy_cb,916 service_proxy_cb,
871 self);917 self);
872}918}
873919
874static void
875indicator_power_init (IndicatorPower *self)
876{
877 IndicatorPowerPrivate *priv;
878
879 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
880 INDICATOR_POWER_TYPE,
881 IndicatorPowerPrivate);
882 priv = self->priv;
883
884 /* Init variables */
885 priv->menu = NULL;
886 priv->accessible_desc = NULL;
887
888
889 priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
890 DBUS_SERVICE,
891 G_BUS_NAME_WATCHER_FLAGS_NONE,
892 gsd_appeared_callback,
893 NULL,
894 self,
895 NULL);
896
897 /* GSettings */
898 priv->settings = g_settings_new ("com.canonical.indicator.power");
899}
900
901static void
902indicator_power_dispose (GObject *object)
903{
904 G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object);
905}
906
907static void
908indicator_power_finalize (GObject *object)
909{
910 G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object);
911}
912
913920
914921
915922
@@ -919,54 +926,50 @@
919get_label (IndicatorObject *io)926get_label (IndicatorObject *io)
920{927{
921 IndicatorPower *self = INDICATOR_POWER (io);928 IndicatorPower *self = INDICATOR_POWER (io);
922 IndicatorPowerPrivate *priv = self->priv;
923929
924 if (priv->label == NULL)930 if (self->label == NULL)
925 {931 {
926 /* Create the label if it doesn't exist already */932 /* Create the label if it doesn't exist already */
927 priv->label = GTK_LABEL (gtk_label_new (""));933 self->label = GTK_LABEL (gtk_label_new (""));
928 gtk_widget_set_visible (GTK_WIDGET (priv->label), FALSE);934 gtk_widget_set_visible (GTK_WIDGET (self->label), FALSE);
929 }935 }
930936
931 return priv->label;937 return self->label;
932}938}
933939
934static GtkImage *940static GtkImage *
935get_image (IndicatorObject *io)941get_image (IndicatorObject *io)
936{942{
937 IndicatorPower *self = INDICATOR_POWER (io);943 IndicatorPower *self = INDICATOR_POWER (io);
938 IndicatorPowerPrivate *priv = self->priv;
939 GIcon *gicon;944 GIcon *gicon;
940945
941 if (priv->status_image == NULL)946 if (self->status_image == NULL)
942 {947 {
943 /* Will create the status icon if it doesn't exist already */948 /* Will create the status icon if it doesn't exist already */
944 gicon = g_themed_icon_new (DEFAULT_ICON);949 gicon = g_themed_icon_new (DEFAULT_ICON);
945 priv->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon,950 self->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon,
946 GTK_ICON_SIZE_LARGE_TOOLBAR));951 GTK_ICON_SIZE_LARGE_TOOLBAR));
947 }952 }
948953
949 return priv->status_image;954 return self->status_image;
950}955}
951956
952static GtkMenu *957static GtkMenu *
953get_menu (IndicatorObject *io)958get_menu (IndicatorObject *io)
954{959{
955 IndicatorPower *self = INDICATOR_POWER (io);960 IndicatorPower *self = INDICATOR_POWER (io);
956 IndicatorPowerPrivate *priv = self->priv;
957961
958 build_menu (self);962 build_menu (self);
959963
960 return GTK_MENU (priv->menu);964 return GTK_MENU (self->menu);
961}965}
962966
963static const gchar *967static const gchar *
964get_accessible_desc (IndicatorObject *io)968get_accessible_desc (IndicatorObject *io)
965{969{
966 IndicatorPower *self = INDICATOR_POWER (io);970 IndicatorPower *self = INDICATOR_POWER (io);
967 IndicatorPowerPrivate *priv = self->priv;
968971
969 return priv->accessible_desc;972 return self->accessible_desc;
970}973}
971974
972static const gchar *975static const gchar *
@@ -974,3 +977,65 @@
974{977{
975 return PACKAGE_NAME;978 return PACKAGE_NAME;
976}979}
980
981/***
982****
983***/
984
985static void
986count_batteries(GVariant *devices, int *total, int *inuse)
987{
988 const int n_devices = devices ? g_variant_n_children (devices) : 0;
989
990 int i;
991 for (i=0; i<n_devices; i++)
992 {
993 GVariant * device = g_variant_get_child_value (devices, i);
994
995 UpDeviceKind kind;
996 g_variant_get_child (device, 1, "u", &kind);
997 if (kind == UP_DEVICE_KIND_BATTERY)
998 {
999 ++*total;
1000
1001 UpDeviceState state;
1002 g_variant_get_child (device, 4, "u", &state);
1003 if ((state == UP_DEVICE_STATE_CHARGING) || (state == UP_DEVICE_STATE_DISCHARGING))
1004 ++*inuse;
1005 }
1006 }
1007
1008 g_debug("count_batteries found %d batteries (%d are charging/discharging)", *total, *inuse);
1009}
1010
1011static gboolean
1012should_be_visible (IndicatorPower * self)
1013{
1014 gboolean visible = TRUE;
1015
1016 const int icon_policy = g_settings_get_enum (self->settings, ICON_POLICY_KEY);
1017
1018 g_debug ("icon_policy is: %d (present==0, charge==1, never==2)", icon_policy);
1019
1020 if (icon_policy == POWER_INDICATOR_ICON_POLICY_NEVER)
1021 {
1022 visible = FALSE;
1023 }
1024 else
1025 {
1026 int batteries=0, inuse=0;
1027 count_batteries (self->devices, &batteries, &inuse);
1028
1029 if (icon_policy == POWER_INDICATOR_ICON_POLICY_PRESENT)
1030 {
1031 visible = batteries > 0;
1032 }
1033 else if (icon_policy == POWER_INDICATOR_ICON_POLICY_CHARGE)
1034 {
1035 visible = inuse > 0;
1036 }
1037 }
1038
1039 g_debug ("should_be_visible: %s", visible?"yes":"no");
1040 return visible;
1041}

Subscribers

People subscribed via source and target branches