From aa089d4d002ae4c93a1d1d3757f408ba2cd7c28c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 18:46:00 -0600 Subject: remove redundant prototypes --- src/indicator-power.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 8dc2788..9287f0b 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -88,8 +88,6 @@ struct _IndicatorPowerPrivate }; /* Prototypes */ -static void indicator_power_class_init (IndicatorPowerClass *klass); -static void indicator_power_init (IndicatorPower *self); static void indicator_power_dispose (GObject *object); static void indicator_power_finalize (GObject *object); -- cgit v1.2.3 From c63e1db05b4063b6d7b1d78e7e52e5d8176cfd67 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 18:50:19 -0600 Subject: don't leak priv->accessible_desc --- src/indicator-power.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 9287f0b..dcd5db0 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -352,7 +352,7 @@ set_accessible_desc (IndicatorPower *self, { IndicatorPowerPrivate *priv = self->priv; - if (desc == NULL || strlen(desc) == 0) + if (desc == NULL || desc[0] == '\0') return; g_free (priv->accessible_desc); @@ -905,6 +905,10 @@ indicator_power_dispose (GObject *object) static void indicator_power_finalize (GObject *object) { + IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + + g_free (priv->accessible_desc); + G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); } -- cgit v1.2.3 From 17626e17645023a17e843bedac589d6218ac38bf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 18:51:17 -0600 Subject: don't leak priv->settings --- src/indicator-power.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/indicator-power.c b/src/indicator-power.c index dcd5db0..6b95b4c 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -899,6 +899,10 @@ indicator_power_init (IndicatorPower *self) static void indicator_power_dispose (GObject *object) { + IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + + g_clear_object (&priv->settings); + G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); } -- cgit v1.2.3 From 85a04b44e3e4f77db07c52ab8667195c6c98cedf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 18:55:19 -0600 Subject: unref+clear Priv's variant fields in _dispose() --- src/indicator-power.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 6b95b4c..4eebf3a 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -50,6 +50,11 @@ with this program. If not, see . #define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) #define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) +GType indicator_power_get_type (void); + +INDICATOR_SET_VERSION +INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) + typedef struct _IndicatorPower IndicatorPower; typedef struct _IndicatorPowerClass IndicatorPowerClass; typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate; @@ -66,8 +71,6 @@ struct _IndicatorPowerClass IndicatorObjectClass parent_class; }; -GType indicator_power_get_type (void) G_GNUC_CONST; - struct _IndicatorPowerPrivate { @@ -101,10 +104,6 @@ static const gchar* get_name_hint (IndicatorObject * io); G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); -/* Indicator stuff */ -INDICATOR_SET_VERSION -INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) - static void indicator_power_class_init (IndicatorPowerClass *klass) @@ -901,6 +900,16 @@ indicator_power_dispose (GObject *object) { IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + if (priv->devices != NULL) { + g_variant_unref (priv->devices); + priv->devices = NULL; + } + + if (priv->device != NULL) { + g_variant_unref (priv->device); + priv->device = NULL; + } + g_clear_object (&priv->settings); G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); -- cgit v1.2.3 From 7c8371d127b16cd2fe2893605b6cdf61a94b2451 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 18:58:48 -0600 Subject: group the indicator_power lifecycle funcs together --- src/indicator-power.c | 120 +++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 4eebf3a..27202cb 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -100,11 +100,10 @@ static GtkMenu* get_menu (IndicatorObject * io); static const gchar* get_accessible_desc (IndicatorObject * io); static const gchar* get_name_hint (IndicatorObject * io); +static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); - - static void indicator_power_class_init (IndicatorPowerClass *klass) { @@ -123,6 +122,66 @@ indicator_power_class_init (IndicatorPowerClass *klass) g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate)); } +static void +indicator_power_init (IndicatorPower *self) +{ + IndicatorPowerPrivate *priv; + + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_POWER_TYPE, + IndicatorPowerPrivate); + priv = self->priv; + + /* Init variables */ + priv->menu = NULL; + priv->accessible_desc = NULL; + + + priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + DBUS_SERVICE, + G_BUS_NAME_WATCHER_FLAGS_NONE, + gsd_appeared_callback, + NULL, + self, + NULL); + + priv->settings = g_settings_new ("com.canonical.indicator.power"); +} + +static void +indicator_power_dispose (GObject *object) +{ + IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + + if (priv->devices != NULL) { + g_variant_unref (priv->devices); + priv->devices = NULL; + } + + if (priv->device != NULL) { + g_variant_unref (priv->device); + priv->device = NULL; + } + + g_clear_object (&priv->settings); + + G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); +} + +static void +indicator_power_finalize (GObject *object) +{ + IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + + g_free (priv->accessible_desc); + + G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); +} + +/*** +**** +***/ + static void show_info_cb (GtkMenuItem *item, gpointer data) @@ -868,63 +927,6 @@ gsd_appeared_callback (GDBusConnection *connection, self); } -static void -indicator_power_init (IndicatorPower *self) -{ - IndicatorPowerPrivate *priv; - - self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_TYPE, - IndicatorPowerPrivate); - priv = self->priv; - - /* Init variables */ - priv->menu = NULL; - priv->accessible_desc = NULL; - - - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, - DBUS_SERVICE, - G_BUS_NAME_WATCHER_FLAGS_NONE, - gsd_appeared_callback, - NULL, - self, - NULL); - - /* GSettings */ - priv->settings = g_settings_new ("com.canonical.indicator.power"); -} - -static void -indicator_power_dispose (GObject *object) -{ - IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; - - if (priv->devices != NULL) { - g_variant_unref (priv->devices); - priv->devices = NULL; - } - - if (priv->device != NULL) { - g_variant_unref (priv->device); - priv->device = NULL; - } - - g_clear_object (&priv->settings); - - G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); -} - -static void -indicator_power_finalize (GObject *object) -{ - IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; - - g_free (priv->accessible_desc); - - G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); -} - -- cgit v1.2.3 From 7cc07d1db1a5bf36479a3db7594e39d6b9d80c30 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 19:05:08 -0600 Subject: remove unused struct names --- src/indicator-power.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 27202cb..5858b71 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -55,25 +55,12 @@ GType indicator_power_get_type (void); INDICATOR_SET_VERSION INDICATOR_SET_TYPE (INDICATOR_POWER_TYPE) -typedef struct _IndicatorPower IndicatorPower; -typedef struct _IndicatorPowerClass IndicatorPowerClass; -typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate; - -struct _IndicatorPower -{ - IndicatorObject parent_instance; - - IndicatorPowerPrivate *priv; -}; - -struct _IndicatorPowerClass -{ +typedef struct { IndicatorObjectClass parent_class; -}; - +} +IndicatorPowerClass; -struct _IndicatorPowerPrivate -{ +typedef struct { GtkMenu *menu; GtkLabel *label; @@ -88,7 +75,15 @@ struct _IndicatorPowerPrivate GVariant *device; GSettings *settings; -}; +} +IndicatorPowerPrivate; + +typedef struct { + IndicatorObject parent_instance; + + IndicatorPowerPrivate *priv; +} +IndicatorPower; /* Prototypes */ static void indicator_power_dispose (GObject *object); @@ -136,7 +131,6 @@ indicator_power_init (IndicatorPower *self) priv->menu = NULL; priv->accessible_desc = NULL; - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, DBUS_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, -- cgit v1.2.3 From 4a6c62934fa66ade8842ffc9e0075291c11952dd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 19:24:34 -0600 Subject: remove unnecessary Priv struct -- the =entire class= is private --- src/indicator-power.c | 140 ++++++++++++++++++++------------------------------ 1 file changed, 56 insertions(+), 84 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 5858b71..c833673 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -61,6 +61,8 @@ typedef struct { IndicatorPowerClass; typedef struct { + IndicatorObject parent_instance; + GtkMenu *menu; GtkLabel *label; @@ -76,13 +78,6 @@ typedef struct { GSettings *settings; } -IndicatorPowerPrivate; - -typedef struct { - IndicatorObject parent_instance; - - IndicatorPowerPrivate *priv; -} IndicatorPower; /* Prototypes */ @@ -113,25 +108,15 @@ indicator_power_class_init (IndicatorPowerClass *klass) io_class->get_menu = get_menu; io_class->get_accessible_desc = get_accessible_desc; io_class->get_name_hint = get_name_hint; - - g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate)); } static void indicator_power_init (IndicatorPower *self) { - IndicatorPowerPrivate *priv; - - self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_POWER_TYPE, - IndicatorPowerPrivate); - priv = self->priv; + self->menu = NULL; + self->accessible_desc = NULL; - /* Init variables */ - priv->menu = NULL; - priv->accessible_desc = NULL; - - priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, + self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, DBUS_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, gsd_appeared_callback, @@ -139,25 +124,25 @@ indicator_power_init (IndicatorPower *self) self, NULL); - priv->settings = g_settings_new ("com.canonical.indicator.power"); + self->settings = g_settings_new ("com.canonical.indicator.power"); } static void indicator_power_dispose (GObject *object) { - IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + IndicatorPower *self = INDICATOR_POWER(object); - if (priv->devices != NULL) { - g_variant_unref (priv->devices); - priv->devices = NULL; + if (self->devices != NULL) { + g_variant_unref (self->devices); + self->devices = NULL; } - if (priv->device != NULL) { - g_variant_unref (priv->device); - priv->device = NULL; + if (self->device != NULL) { + g_variant_unref (self->device); + self->device = NULL; } - g_clear_object (&priv->settings); + g_clear_object (&self->settings); G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); } @@ -165,9 +150,9 @@ indicator_power_dispose (GObject *object) static void indicator_power_finalize (GObject *object) { - IndicatorPowerPrivate *priv = INDICATOR_POWER(object)->priv; + IndicatorPower *self = INDICATOR_POWER(object); - g_free (priv->accessible_desc); + g_free (self->accessible_desc); G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); } @@ -192,15 +177,14 @@ option_toggled_cb (GtkCheckMenuItem *item, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate *priv = self->priv; gboolean visible; visible = gtk_check_menu_item_get_active (item); - gtk_widget_set_visible (GTK_WIDGET (priv->label), + gtk_widget_set_visible (GTK_WIDGET (self->label), visible); - g_settings_set_boolean (priv->settings, "show-time", + g_settings_set_boolean (self->settings, "show-time", visible); } @@ -402,14 +386,12 @@ static void set_accessible_desc (IndicatorPower *self, const gchar *desc) { - IndicatorPowerPrivate *priv = self->priv; - if (desc == NULL || desc[0] == '\0') return; - g_free (priv->accessible_desc); + g_free (self->accessible_desc); - priv->accessible_desc = g_strdup (desc); + self->accessible_desc = g_strdup (desc); } static const gchar * @@ -609,38 +591,37 @@ get_greeter_mode (void) static void build_menu (IndicatorPower *self) { - IndicatorPowerPrivate *priv = self->priv; GtkWidget *item; GtkWidget *image; GList *children; gsize n_devices = 0; gboolean visible; - if (priv->menu == NULL) - priv->menu = GTK_MENU (gtk_menu_new ()); + if (self->menu == NULL) + self->menu = GTK_MENU (gtk_menu_new ()); - children = gtk_container_get_children (GTK_CONTAINER (priv->menu)); + children = gtk_container_get_children (GTK_CONTAINER (self->menu)); g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL); g_list_free (children); /* devices */ - n_devices = menu_add_devices (priv->menu, priv->devices); + n_devices = menu_add_devices (self->menu, self->devices); if (!get_greeter_mode ()) { /* only do the separator if we have at least one device */ if (n_devices != 0) { item = gtk_separator_menu_item_new (); - gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); + gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); } /* options */ item = gtk_check_menu_item_new_with_label (_("Show Time in Menu Bar")); g_signal_connect (G_OBJECT (item), "toggled", G_CALLBACK (option_toggled_cb), self); - visible = g_settings_get_boolean (priv->settings, "show-time"); + visible = g_settings_get_boolean (self->settings, "show-time"); gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), visible); - gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); + gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); /* preferences */ item = gtk_image_menu_item_new_with_label (_("Power Settings...")); @@ -648,11 +629,11 @@ build_menu (IndicatorPower *self) gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image); g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (show_preferences_cb), NULL); - gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), item); + gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); } /* show the menu */ - gtk_widget_show_all (GTK_WIDGET (priv->menu)); + gtk_widget_show_all (GTK_WIDGET (self->menu)); } static GVariant * @@ -749,7 +730,6 @@ static void put_primary_device (IndicatorPower *self, GVariant *device) { - IndicatorPowerPrivate *priv = self->priv; UpDeviceKind kind; UpDeviceState state; GIcon *device_gicons; @@ -776,10 +756,10 @@ put_primary_device (IndicatorPower *self, /* set icon */ device_gicons = get_device_icon (kind, state, time, device_icon); - gtk_image_set_from_gicon (priv->status_image, + gtk_image_set_from_gicon (self->status_image, device_gicons, GTK_ICON_SIZE_LARGE_TOOLBAR); - gtk_widget_show (GTK_WIDGET (priv->status_image)); + gtk_widget_show (GTK_WIDGET (self->status_image)); /* get the device name */ @@ -788,7 +768,7 @@ put_primary_device (IndicatorPower *self, /* get the description */ build_device_time_details (device_name, time, state, percentage, &short_details, &details, &accesible_name); - gtk_label_set_label (GTK_LABEL (priv->label), + gtk_label_set_label (GTK_LABEL (self->label), short_details); set_accessible_desc (self, accesible_name); @@ -805,7 +785,6 @@ get_devices_cb (GObject *source_object, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate *priv = self->priv; GVariant *devices_container; GError *error = NULL; @@ -817,18 +796,18 @@ get_devices_cb (GObject *source_object, return; } - priv->devices = g_variant_get_child_value (devices_container, 0); + self->devices = g_variant_get_child_value (devices_container, 0); g_variant_unref (devices_container); - priv->device = get_primary_device (priv->devices); - if (priv->device == NULL) + self->device = get_primary_device (self->devices); + if (self->device == NULL) { g_printerr ("Error getting primary device"); return; } - put_primary_device (self, priv->device); + put_primary_device (self, self->device); build_menu (self); } @@ -841,17 +820,16 @@ receive_signal (GDBusProxy *proxy, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate *priv = self->priv; if (g_strcmp0 (signal_name, "Changed") == 0) { /* get the new state */ - g_dbus_proxy_call (priv->proxy, + g_dbus_proxy_call (self->proxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, - priv->proxy_cancel, + self->proxy_cancel, get_devices_cb, user_data); } @@ -863,15 +841,14 @@ service_proxy_cb (GObject *object, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate *priv = self->priv; GError *error = NULL; - priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); + self->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - if (priv->proxy_cancel != NULL) + if (self->proxy_cancel != NULL) { - g_object_unref (priv->proxy_cancel); - priv->proxy_cancel = NULL; + g_object_unref (self->proxy_cancel); + self->proxy_cancel = NULL; } if (error != NULL) @@ -883,18 +860,18 @@ service_proxy_cb (GObject *object, } /* we want to change the primary device changes */ - g_signal_connect (priv->proxy, + g_signal_connect (self->proxy, "g-signal", G_CALLBACK (receive_signal), user_data); /* get the initial state */ - g_dbus_proxy_call (priv->proxy, + g_dbus_proxy_call (self->proxy, "GetDevices", NULL, G_DBUS_CALL_FLAGS_NONE, -1, - priv->proxy_cancel, + self->proxy_cancel, get_devices_cb, user_data); } @@ -906,9 +883,8 @@ gsd_appeared_callback (GDBusConnection *connection, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); - IndicatorPowerPrivate *priv = self->priv; - priv->proxy_cancel = g_cancellable_new (); + self->proxy_cancel = g_cancellable_new (); g_dbus_proxy_new (connection, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, @@ -916,7 +892,7 @@ gsd_appeared_callback (GDBusConnection *connection, name, POWER_DBUS_PATH, POWER_DBUS_INTERFACE, - priv->proxy_cancel, + self->proxy_cancel, service_proxy_cb, self); } @@ -930,54 +906,50 @@ static GtkLabel * get_label (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); - IndicatorPowerPrivate *priv = self->priv; - if (priv->label == NULL) + if (self->label == NULL) { /* Create the label if it doesn't exist already */ - priv->label = GTK_LABEL (gtk_label_new ("")); - gtk_widget_set_visible (GTK_WIDGET (priv->label), FALSE); + self->label = GTK_LABEL (gtk_label_new ("")); + gtk_widget_set_visible (GTK_WIDGET (self->label), FALSE); } - return priv->label; + return self->label; } static GtkImage * get_image (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); - IndicatorPowerPrivate *priv = self->priv; GIcon *gicon; - if (priv->status_image == NULL) + if (self->status_image == NULL) { /* Will create the status icon if it doesn't exist already */ gicon = g_themed_icon_new (DEFAULT_ICON); - priv->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon, + self->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon, GTK_ICON_SIZE_LARGE_TOOLBAR)); } - return priv->status_image; + return self->status_image; } static GtkMenu * get_menu (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); - IndicatorPowerPrivate *priv = self->priv; build_menu (self); - return GTK_MENU (priv->menu); + return GTK_MENU (self->menu); } static const gchar * get_accessible_desc (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); - IndicatorPowerPrivate *priv = self->priv; - return priv->accessible_desc; + return self->accessible_desc; } static const gchar * -- cgit v1.2.3 From aacad61c29de5f9d702f84fc425a60561425ebac Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 19:26:30 -0600 Subject: use g_clear_object() on the proxy_cancel field --- src/indicator-power.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index c833673..6280d4c 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -845,11 +845,7 @@ service_proxy_cb (GObject *object, self->proxy = g_dbus_proxy_new_for_bus_finish (res, &error); - if (self->proxy_cancel != NULL) - { - g_object_unref (self->proxy_cancel); - self->proxy_cancel = NULL; - } + g_clear_object (&self->proxy_cancel); if (error != NULL) { -- cgit v1.2.3 From 98a2abe1e732fb0110dbf7785569f3a5c0455dca Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 19:28:39 -0600 Subject: remove redundant #include of glib.h --- src/indicator-power.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 6280d4c..4bf9a6f 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -24,7 +24,6 @@ with this program. If not, see . #endif /* GStuff */ -#include #include #include #include -- cgit v1.2.3 From da453ec039c78d60ebad64226367780037621197 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 19:33:47 -0600 Subject: better error reporting if g_spawn_command_line_async() fails --- src/indicator-power.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 4bf9a6f..dc1007d 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -160,15 +160,21 @@ indicator_power_finalize (GObject *object) **** ***/ +static void +spawn_command_line_async (const char * command) +{ + GError * err = NULL; + if (!g_spawn_command_line_async (command, &err)) + g_warning ("Couldn't execute command \"%s\": %s", command, err->message); + g_clear_error (&err); +} + static void show_info_cb (GtkMenuItem *item, gpointer data) { /*TODO: show the statistics of the specific device*/ - const gchar *command = "gnome-power-statistics"; - - if (g_spawn_command_line_async (command, NULL) == FALSE) - g_warning ("Couldn't execute command: %s", command); + spawn_command_line_async ("gnome-power-statistics"); } static void @@ -191,10 +197,7 @@ static void show_preferences_cb (GtkMenuItem *item, gpointer data) { - const gchar *command = "gnome-control-center power"; - - if (g_spawn_command_line_async (command, NULL) == FALSE) - g_warning ("Couldn't execute command: %s", command); + spawn_command_line_async ("gnome-control-center power"); } static void -- cgit v1.2.3 From 33beea08d1a833a3517b1a7a858d14ecc438017c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 22:51:26 -0600 Subject: use g_settings_bind() for the show-time checkbox --- src/indicator-power.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index dc1007d..f91119d 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -178,19 +178,10 @@ show_info_cb (GtkMenuItem *item, } static void -option_toggled_cb (GtkCheckMenuItem *item, - gpointer user_data) +option_toggled_cb (GtkCheckMenuItem *item, IndicatorPower * self) { - IndicatorPower *self = INDICATOR_POWER (user_data); - gboolean visible; - - visible = gtk_check_menu_item_get_active (item); - gtk_widget_set_visible (GTK_WIDGET (self->label), - visible); - - g_settings_set_boolean (self->settings, "show-time", - visible); + gtk_check_menu_item_get_active(item)); } static void @@ -597,7 +588,6 @@ build_menu (IndicatorPower *self) GtkWidget *image; GList *children; gsize n_devices = 0; - gboolean visible; if (self->menu == NULL) self->menu = GTK_MENU (gtk_menu_new ()); @@ -619,10 +609,8 @@ build_menu (IndicatorPower *self) /* options */ item = gtk_check_menu_item_new_with_label (_("Show Time in Menu Bar")); - g_signal_connect (G_OBJECT (item), "toggled", - G_CALLBACK (option_toggled_cb), self); - visible = g_settings_get_boolean (self->settings, "show-time"); - gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), visible); + g_signal_connect (item, "toggled", G_CALLBACK(option_toggled_cb), self); + g_settings_bind (self->settings, "show-time", item, "active", G_SETTINGS_BIND_DEFAULT); gtk_menu_shell_append (GTK_MENU_SHELL (self->menu), item); /* preferences */ -- cgit v1.2.3 From f9dc8cc28aa8dcffbec8f010d71234119d6d889d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 22:53:16 -0600 Subject: create the menu at init time s.t. we don't have to keep checking to see if it exists --- src/indicator-power.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index f91119d..e359a03 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -112,7 +112,8 @@ indicator_power_class_init (IndicatorPowerClass *klass) static void indicator_power_init (IndicatorPower *self) { - self->menu = NULL; + self->menu = GTK_MENU(gtk_menu_new()); + self->accessible_desc = NULL; self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, @@ -589,9 +590,7 @@ build_menu (IndicatorPower *self) GList *children; gsize n_devices = 0; - if (self->menu == NULL) - self->menu = GTK_MENU (gtk_menu_new ()); - + /* remove the existing menuitems */ children = gtk_container_get_children (GTK_CONTAINER (self->menu)); g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL); g_list_free (children); -- cgit v1.2.3 From 4b5ec3d0baaf4bb377ad1cb05a88e62408e14a28 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 1 Feb 2012 23:00:03 -0600 Subject: add support for icon-policy setting --- data/com.canonical.indicator.power.gschema.xml.in | 12 +- src/indicator-power.c | 129 +++++++++++++++++++--- 2 files changed, 127 insertions(+), 14 deletions(-) diff --git a/data/com.canonical.indicator.power.gschema.xml.in b/data/com.canonical.indicator.power.gschema.xml.in index bf4368f..3fb065f 100644 --- a/data/com.canonical.indicator.power.gschema.xml.in +++ b/data/com.canonical.indicator.power.gschema.xml.in @@ -1,9 +1,19 @@ + + + + + false <_summary>Show time in Menu Bar - <_description>Whether show the time in the menu bar. + <_description>Whether or not to show the time in the menu bar. + + + "present" + <_summary>When to show the battery status in the menu bar. + <_description>Options for when to show battery status. Valid options are "present", "charge", and "never". diff --git a/src/indicator-power.c b/src/indicator-power.c index e359a03..4b84b26 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -35,6 +35,8 @@ with this program. If not, see . #include #include +#define ICON_POLICY_KEY "icon-policy" + #define DEFAULT_ICON "gpm-battery-missing" #define DBUS_SERVICE "org.gnome.SettingsDaemon" @@ -76,6 +78,8 @@ typedef struct { GVariant *device; GSettings *settings; + + gboolean visible; } IndicatorPower; @@ -89,6 +93,9 @@ static GtkMenu* get_menu (IndicatorObject * io); static const gchar* get_accessible_desc (IndicatorObject * io); static const gchar* get_name_hint (IndicatorObject * io); +static void update_visibility (IndicatorPower * self); +static gboolean should_be_visible (IndicatorPower * self); + static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); G_DEFINE_TYPE (IndicatorPower, indicator_power, INDICATOR_OBJECT_TYPE); @@ -116,6 +123,8 @@ indicator_power_init (IndicatorPower *self) self->accessible_desc = NULL; + self->visible = FALSE; + self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, DBUS_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, @@ -125,6 +134,11 @@ indicator_power_init (IndicatorPower *self) NULL); self->settings = g_settings_new ("com.canonical.indicator.power"); + g_signal_connect_swapped (G_OBJECT(self->settings), "changed::" ICON_POLICY_KEY, + G_CALLBACK(update_visibility), self); + g_object_set (G_OBJECT(self), + INDICATOR_OBJECT_DEFAULT_VISIBILITY, self->visible, + NULL); } static void @@ -645,7 +659,7 @@ get_primary_device (GVariant *devices) gsize n_devices; guint i; - n_devices = g_variant_n_children (devices); + n_devices = devices ? g_variant_n_children (devices) : 0; g_debug ("Num devices: '%" G_GSIZE_FORMAT "'\n", n_devices); for (i = 0; i < n_devices; i++) @@ -780,25 +794,46 @@ get_devices_cb (GObject *source_object, devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error); if (devices_container == NULL) { - g_printerr ("Error getting devices: %s\n", error->message); + g_message ("Couldn't get devices: %s\n", error->message); g_error_free (error); - - return; } - self->devices = g_variant_get_child_value (devices_container, 0); - g_variant_unref (devices_container); - - self->device = get_primary_device (self->devices); - if (self->device == NULL) + else /* update 'devices' */ { - g_printerr ("Error getting primary device"); + if (self->devices != NULL) + g_variant_unref (self->devices); + self->devices = g_variant_get_child_value (devices_container, 0); - return; - } + g_variant_unref (devices_container); - put_primary_device (self, self->device); + if (self->device != NULL) + g_variant_unref (self->device); + self->device = get_primary_device (self->devices); + + if (self->device == NULL) + { + g_message ("Couldn't find primary device"); + } + else + { + put_primary_device (self, self->device); + } + } build_menu (self); + + update_visibility (self); +} + +static void +update_visibility (IndicatorPower * self) +{ + const gboolean visible = should_be_visible (self); + + if (self->visible != visible) + { + self->visible = visible; + indicator_object_set_visible (INDICATOR_OBJECT (self), visible); + } } static void @@ -942,3 +977,71 @@ get_name_hint (IndicatorObject *io) { return PACKAGE_NAME; } + +/*** +**** +***/ + +static void +count_batteries(GVariant *devices, int *total, int *inuse) +{ + const int n_devices = devices ? g_variant_n_children (devices) : 0; + + int i; + for (i=0; isettings, "icon-policy"); + + g_debug ("icon_policy is: %d (present==0, charge==1, never==2)", icon_policy); + + if (icon_policy == POWER_INDICATOR_ICON_POLICY_NEVER) + { + visible = FALSE; + } + else + { + int batteries=0, inuse=0; + count_batteries (self->devices, &batteries, &inuse); + + if (icon_policy == POWER_INDICATOR_ICON_POLICY_PRESENT) + { + visible = batteries > 0; + } + else if (icon_policy == POWER_INDICATOR_ICON_POLICY_CHARGE) + { + visible = inuse > 0; + } + } + + g_debug ("should_be_visible: %s", visible?"yes":"no"); + return visible; +} -- cgit v1.2.3 From 695ba1e2dba25a8974e618b58c4d200cd1b6e8f3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 13 Feb 2012 14:10:22 -0600 Subject: make prototypes for update_visibility() and should_be_visible() align with the neighboring forward declarations --- src/indicator-power.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 4b84b26..bdec98e 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -93,8 +93,8 @@ static GtkMenu* get_menu (IndicatorObject * io); static const gchar* get_accessible_desc (IndicatorObject * io); static const gchar* get_name_hint (IndicatorObject * io); -static void update_visibility (IndicatorPower * self); -static gboolean should_be_visible (IndicatorPower * self); +static void update_visibility (IndicatorPower * self); +static gboolean should_be_visible (IndicatorPower * self); static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data); -- cgit v1.2.3 From f7d828a76741af2dd2652bff7d17e5b9d6764ced Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 13 Feb 2012 14:14:09 -0600 Subject: remove unnecessary G_OBJECT() cast --- src/indicator-power.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index bdec98e..d000365 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -134,7 +134,7 @@ indicator_power_init (IndicatorPower *self) NULL); self->settings = g_settings_new ("com.canonical.indicator.power"); - g_signal_connect_swapped (G_OBJECT(self->settings), "changed::" ICON_POLICY_KEY, + g_signal_connect_swapped (self->settings, "changed::" ICON_POLICY_KEY, G_CALLBACK(update_visibility), self); g_object_set (G_OBJECT(self), INDICATOR_OBJECT_DEFAULT_VISIBILITY, self->visible, -- cgit v1.2.3 From cb4bd9d9338e07504365491c1b45bf7918fdc8bc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 13 Feb 2012 14:15:28 -0600 Subject: consistent use of ICON_POLICY_KEY --- src/indicator-power.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index d000365..90db2f9 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -1019,7 +1019,7 @@ should_be_visible (IndicatorPower * self) { gboolean visible = TRUE; - const int icon_policy = g_settings_get_enum (self->settings, "icon-policy"); + const int icon_policy = g_settings_get_enum (self->settings, ICON_POLICY_KEY); g_debug ("icon_policy is: %d (present==0, charge==1, never==2)", icon_policy); -- cgit v1.2.3 From 0c1e774b428c6ccb76e6ad9e0de9e809b56790fa Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 13 Feb 2012 14:18:56 -0600 Subject: remove unncessary private field 'visible' --- src/indicator-power.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index 90db2f9..bbd00d8 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -78,8 +78,6 @@ typedef struct { GVariant *device; GSettings *settings; - - gboolean visible; } IndicatorPower; @@ -123,8 +121,6 @@ indicator_power_init (IndicatorPower *self) self->accessible_desc = NULL; - self->visible = FALSE; - self->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION, DBUS_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE, @@ -137,7 +133,7 @@ indicator_power_init (IndicatorPower *self) g_signal_connect_swapped (self->settings, "changed::" ICON_POLICY_KEY, G_CALLBACK(update_visibility), self); g_object_set (G_OBJECT(self), - INDICATOR_OBJECT_DEFAULT_VISIBILITY, self->visible, + INDICATOR_OBJECT_DEFAULT_VISIBILITY, FALSE, NULL); } @@ -827,13 +823,8 @@ get_devices_cb (GObject *source_object, static void update_visibility (IndicatorPower * self) { - const gboolean visible = should_be_visible (self); - - if (self->visible != visible) - { - self->visible = visible; - indicator_object_set_visible (INDICATOR_OBJECT (self), visible); - } + indicator_object_set_visible (INDICATOR_OBJECT (self), + should_be_visible (self)); } static void -- cgit v1.2.3 From 8be6c2d51d4927d524370b4f5bab39b6e72bbc0d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 13 Feb 2012 14:20:34 -0600 Subject: move POWER_INDICATOR_ICON_POLICY_* enum to the top of the file --- src/indicator-power.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/indicator-power.c b/src/indicator-power.c index bbd00d8..ee20362 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -51,6 +51,12 @@ with this program. If not, see . #define IS_INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_TYPE)) #define INDICATOR_POWER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_TYPE, IndicatorPowerClass)) +enum { + POWER_INDICATOR_ICON_POLICY_PRESENT, + POWER_INDICATOR_ICON_POLICY_CHARGE, + POWER_INDICATOR_ICON_POLICY_NEVER +}; + GType indicator_power_get_type (void); INDICATOR_SET_VERSION @@ -999,12 +1005,6 @@ count_batteries(GVariant *devices, int *total, int *inuse) g_debug("count_batteries found %d batteries (%d are charging/discharging)", *total, *inuse); } -enum { - POWER_INDICATOR_ICON_POLICY_PRESENT, - POWER_INDICATOR_ICON_POLICY_CHARGE, - POWER_INDICATOR_ICON_POLICY_NEVER -}; - static gboolean should_be_visible (IndicatorPower * self) { -- cgit v1.2.3 From aee1300b7e026c7724859c019599ea3739721c78 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 13 Feb 2012 14:26:23 -0600 Subject: ensure that we don't have a reference to the proxy or proxy_cancel fields in indicator_power_dispose(). --- src/indicator-power.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/indicator-power.c b/src/indicator-power.c index ee20362..f7de038 100644 --- a/src/indicator-power.c +++ b/src/indicator-power.c @@ -158,6 +158,9 @@ indicator_power_dispose (GObject *object) self->device = NULL; } + g_clear_object (&self->proxy); + g_clear_object (&self->proxy_cancel); + g_clear_object (&self->settings); G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); -- cgit v1.2.3