aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/device.c173
-rw-r--r--src/device.h31
-rw-r--r--src/service.c145
3 files changed, 201 insertions, 148 deletions
diff --git a/src/device.c b/src/device.c
index 02c9e65..69336eb 100644
--- a/src/device.c
+++ b/src/device.c
@@ -451,8 +451,8 @@ indicator_power_device_get_gicon (const IndicatorPowerDevice * device)
static void
get_timestring (guint64 time_secs,
- gchar **short_timestring,
- gchar **detailed_timestring)
+ gchar **readable_timestring,
+ gchar **accessible_timestring)
{
gint hours;
gint minutes;
@@ -462,16 +462,16 @@ get_timestring (guint64 time_secs,
if (minutes == 0)
{
- *short_timestring = g_strdup (_("Unknown time"));
- *detailed_timestring = g_strdup (_("Unknown time"));
+ *readable_timestring = g_strdup (_("Unknown time"));
+ *accessible_timestring = g_strdup (_("Unknown time"));
return;
}
if (minutes < 60)
{
- *short_timestring = g_strdup_printf ("0:%.2i", minutes);
- *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute",
+ *readable_timestring = g_strdup_printf ("0:%.2i", minutes);
+ *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, "%i minute",
"%i minutes",
minutes), minutes);
return;
@@ -480,11 +480,11 @@ get_timestring (guint64 time_secs,
hours = minutes / 60;
minutes = minutes % 60;
- *short_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
+ *readable_timestring = g_strdup_printf ("%i:%.2i", hours, minutes);
if (minutes == 0)
{
- *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
+ *accessible_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
"%i hour",
"%i hours",
hours), hours);
@@ -493,7 +493,7 @@ get_timestring (guint64 time_secs,
{
/* TRANSLATOR: "%i %s %i %s" are "%i hours %i minutes"
* Swap order with "%2$s %2$i %1$s %1$i if needed */
- *detailed_timestring = g_strdup_printf (_("%i %s %i %s"),
+ *accessible_timestring = g_strdup_printf (_("%i %s %i %s"),
hours, g_dngettext (GETTEXT_PACKAGE, "hour", "hours", hours),
minutes, g_dngettext (GETTEXT_PACKAGE, "minute", "minutes", minutes));
}
@@ -561,86 +561,145 @@ device_kind_to_localised_string (UpDeviceKind kind)
return text;
}
-void
+static char *
+join_strings (const char * name, const char * time, const char * percent)
+{
+ char * str;
+ const gboolean have_name = name && *name;
+ const gboolean have_time = time && *time;
+ const gboolean have_percent = percent && *percent;
+
+ if (have_name && have_time && have_percent)
+ str = g_strdup_printf (_("%s (%s, %s)"), name, time, percent);
+ else if (have_name && have_time)
+ str = g_strdup_printf (_("%s (%s)"), name, time);
+ else if (have_name && have_percent)
+ str = g_strdup_printf (_("%s (%s)"), name, percent);
+ else if (have_name)
+ str = g_strdup (name);
+ else if (have_time && have_percent)
+ str = g_strdup_printf (_("(%s, %s)"), time, percent);
+ else if (have_time)
+ str = g_strdup_printf (_("(%s)"), time);
+ else if (have_percent)
+ str = g_strdup_printf (_("(%s)"), percent);
+ else
+ str = g_strdup ("");
+
+ return str;
+}
+
+static void
indicator_power_device_get_time_details (const IndicatorPowerDevice * device,
- gchar ** short_details,
- gchar ** details,
- gchar ** accessible_name)
+ gboolean show_time_in_header,
+ gboolean show_percentage_in_header,
+ gchar ** header,
+ gchar ** label,
+ gchar ** a11y)
{
if (!INDICATOR_IS_POWER_DEVICE(device))
{
- *short_details = NULL;
- *details = NULL;
- *accessible_name = NULL;
+ if (a11y != NULL) *a11y = NULL;
+ if (label != NULL) *label = NULL;
+ if (header != NULL) *header = NULL;
g_warning ("%s: %p is not an IndicatorPowerDevice", G_STRFUNC, device);
return;
}
const time_t time = indicator_power_device_get_time (device);
const UpDeviceState state = indicator_power_device_get_state (device);
- const gdouble percentage = indicator_power_device_get_percentage (device);
const UpDeviceKind kind = indicator_power_device_get_kind (device);
const gchar * device_name = device_kind_to_localised_string (kind);
+ const gdouble percentage = indicator_power_device_get_percentage (device);
+ char pctstr[32] = { '\0' };
+ g_snprintf (pctstr, sizeof(pctstr), "%.0lf%%", percentage);
+
+ GString * terse_time = g_string_new (NULL);
+ GString * verbose_time = g_string_new (NULL);
+ GString * accessible_time = g_string_new (NULL);
if (time > 0)
{
- gchar *short_timestring = NULL;
- gchar *detailed_timestring = NULL;
-
- get_timestring (time,
- &short_timestring,
- &detailed_timestring);
+ char * readable_timestr = NULL;
+ char * accessible_timestr = NULL;
+ get_timestring (time, &readable_timestr, &accessible_timestr);
if (state == UP_DEVICE_STATE_CHARGING)
{
- /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
- *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), device_name, detailed_timestring, percentage);
- *details = g_strdup_printf (_("%s (%s to charge)"), device_name, short_timestring);
- *short_details = g_strdup_printf ("(%s)", short_timestring);
+ g_string_assign (terse_time, readable_timestr);
+ g_string_printf (verbose_time, _("%s to charge"), readable_timestr);
+ g_string_printf (accessible_time, _("%s to charge"), accessible_timestr);
}
- else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time > (60*60*12)))
+ else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time < (60*60*12)))
{
- *accessible_name = g_strdup_printf (_("%s"), device_name);
- *details = g_strdup_printf (_("%s"), device_name);
- *short_details = g_strdup (short_timestring);
- }
- else
- {
- /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
- *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, detailed_timestring, percentage);
- *details = g_strdup_printf (_("%s (%s left)"), device_name, short_timestring);
- *short_details = g_strdup (short_timestring);
+ g_string_assign (terse_time, readable_timestr);
+ g_string_printf (verbose_time, _("%s left"), readable_timestr);
+ g_string_printf (accessible_time, _("%s left"), accessible_timestr);
}
- g_free (short_timestring);
- g_free (detailed_timestring);
+ g_free (readable_timestr);
+ g_free (accessible_timestr);
}
else if (state == UP_DEVICE_STATE_FULLY_CHARGED)
{
- *details = g_strdup_printf (_("%s (charged)"), device_name);
- *accessible_name = g_strdup (*details);
- *short_details = g_strdup ("");
+ g_string_assign (verbose_time, _("charged"));
+ g_string_assign (accessible_time, _("charged"));
}
else if (percentage > 0)
{
- /* TRANSLATORS: %2 is a percentage value. Note: this string is only
- * used when we don't have a time value */
- *details = g_strdup_printf (_("%s (%.0lf%%)"), device_name, percentage);
- *accessible_name = g_strdup (*details);
- *short_details = g_strdup_printf (_("(%.0lf%%)"), percentage);
- }
- else if (kind == UP_DEVICE_KIND_LINE_POWER)
- {
- *details = g_strdup (device_name);
- *accessible_name = g_strdup (device_name);
- *short_details = g_strdup ("");
+ g_string_assign (terse_time, _("estimating…"));
+ g_string_assign (verbose_time, _("estimating…"));
+ g_string_assign (accessible_time, _("estimating…"));
}
else
{
- *details = g_strdup_printf (_("%s (not present)"), device_name);
- *accessible_name = g_strdup (*details);
- *short_details = g_strdup (_("(not present)"));
+ *pctstr = '\0';
+
+ if (kind != UP_DEVICE_KIND_LINE_POWER)
+ {
+ g_string_assign (verbose_time, _("not present"));
+ g_string_assign (accessible_time, _("not present"));
+ }
}
+
+ if (header != NULL)
+ *header = join_strings (NULL,
+ show_time_in_header ? terse_time->str : "",
+ show_percentage_in_header ? pctstr : "");
+
+ if (label != NULL)
+ *label = join_strings (device_name,
+ verbose_time->str,
+ NULL);
+
+ if (a11y != NULL)
+ *a11y = join_strings (device_name,
+ accessible_time->str,
+ pctstr);
+
+ g_string_free (terse_time, TRUE);
+ g_string_free (verbose_time, TRUE);
+ g_string_free (accessible_time, TRUE);
+}
+
+gchar *
+indicator_power_device_get_label (const IndicatorPowerDevice * device)
+{
+ gchar * label = NULL;
+ indicator_power_device_get_time_details (device, FALSE, FALSE,
+ NULL, &label, NULL);
+ return label;
+}
+
+void
+indicator_power_device_get_header (const IndicatorPowerDevice * device,
+ gboolean show_time,
+ gboolean show_percentage,
+ gchar ** header,
+ gchar ** a11y)
+{
+ indicator_power_device_get_time_details (device, show_time, show_percentage,
+ header, NULL, a11y);
}
/***
diff --git a/src/device.h b/src/device.h
index ffbb5da..1f395a1 100644
--- a/src/device.h
+++ b/src/device.h
@@ -116,21 +116,22 @@ IndicatorPowerDevice* indicator_power_device_new (const gchar * object_path,
IndicatorPowerDevice* indicator_power_device_new_from_variant (GVariant * variant);
-UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device);
-UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device);
-const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device);
-gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device);
-time_t indicator_power_device_get_time (const IndicatorPowerDevice * device);
-
-GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device);
-GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device);
-
-void indicator_power_device_get_time_details (const IndicatorPowerDevice * device,
- gchar ** short_details,
- gchar ** details,
- gchar ** accessible_name);
-
-
+UpDeviceKind indicator_power_device_get_kind (const IndicatorPowerDevice * device);
+UpDeviceState indicator_power_device_get_state (const IndicatorPowerDevice * device);
+const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device);
+gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device);
+time_t indicator_power_device_get_time (const IndicatorPowerDevice * device);
+
+GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device);
+GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device);
+
+gchar * indicator_power_device_get_label (const IndicatorPowerDevice * device);
+
+void indicator_power_device_get_header (const IndicatorPowerDevice * device,
+ gboolean show_time,
+ gboolean show_percentage,
+ gchar ** header,
+ gchar ** a11y);
G_END_DECLS
diff --git a/src/service.c b/src/service.c
index 4aaca3f..81a70d5 100644
--- a/src/service.c
+++ b/src/service.c
@@ -113,6 +113,7 @@ struct _IndicatorPowerServicePrivate
GSimpleActionGroup * actions;
GSimpleAction * header_action;
GSimpleAction * show_time_action;
+ GSimpleAction * show_percentage_action;
GSimpleAction * battery_level_action;
GSimpleAction * brightness_action;
@@ -319,16 +320,13 @@ create_header_state (IndicatorPowerService * self)
if (p->primary_device != NULL)
{
- gchar * details;
-
- indicator_power_device_get_time_details (p->primary_device,
- &label,
- &details,
- &a11y);
+ indicator_power_device_get_header (p->primary_device,
+ g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S),
+ g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S),
+ &label,
+ &a11y);
icon = indicator_power_device_get_gicon (p->primary_device);
-
- g_free (details);
}
g_variant_builder_init (&b, G_VARIANT_TYPE("a{sv}"));
@@ -338,9 +336,7 @@ create_header_state (IndicatorPowerService * self)
if (label != NULL)
{
- if (g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S))
- g_variant_builder_add (&b, "{sv}", "label",
- g_variant_new_string (label));
+ g_variant_builder_add (&b, "{sv}", "label", g_variant_new_string (label));
g_free (label);
}
@@ -377,33 +373,24 @@ append_device_to_menu (GMenu * menu, const IndicatorPowerDevice * device)
if (kind != UP_DEVICE_KIND_LINE_POWER)
{
- char * brief;
+ GIcon * icon;
char * label;
- char * a11y;
GMenuItem * menu_item;
- GIcon * icon = indicator_power_device_get_gicon (device);
-
- indicator_power_device_get_time_details (device,
- &brief,
- &label,
- &a11y);
+ icon = indicator_power_device_get_gicon (device);
+ label = indicator_power_device_get_label (device);
menu_item = g_menu_item_new (label, "indicator.activate-statistics");
if (icon != NULL)
- {
- g_menu_item_set_attribute_value (menu_item,
- G_MENU_ATTRIBUTE_ICON,
- g_icon_serialize (icon));
- }
+ g_menu_item_set_attribute_value (menu_item,
+ G_MENU_ATTRIBUTE_ICON,
+ g_icon_serialize (icon));
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
g_clear_object (&icon);
- g_free (brief);
g_free (label);
- g_free (a11y);
}
}
@@ -522,6 +509,10 @@ create_desktop_settings_section (IndicatorPowerService * self G_GNUC_UNUSED)
"indicator.show-time");
g_menu_append (menu,
+ _("Show Percentage in Menu Bar"),
+ "indicator.show-percentage");
+
+ g_menu_append (menu,
_("Power Settings…"),
"indicator.activate-settings");
@@ -707,61 +698,43 @@ on_statistics_activated (GSimpleAction * a G_GNUC_UNUSED,
execute_command ("gnome-power-statistics");
}
-/* FIXME: use a GBinding to tie the gaction's state and the GSetting together? */
+/***
+****
+***/
+/* toggles the state */
static void
-set_show_time_flag (IndicatorPowerService * self, gboolean b)
+on_toggle_action_activated (GSimpleAction * simple,
+ GVariant * parameter G_GNUC_UNUSED,
+ gpointer unused G_GNUC_UNUSED)
{
- GVariant * v;
- priv_t * p = self->priv;
-
- /* update the settings */
- if (b != g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S))
- g_settings_set_boolean (p->settings, SETTINGS_SHOW_TIME_S, b);
-
- /* update the action state */
- v = g_action_get_state (G_ACTION(p->show_time_action));
- if (b != g_variant_get_boolean (v))
- g_simple_action_set_state (p->show_time_action, g_variant_new_boolean (b));
+ GVariant * v = g_action_get_state (G_ACTION (simple));
+ gboolean flag = g_variant_get_boolean (v);
+ g_simple_action_set_state (simple, g_variant_new_boolean (!flag));
g_variant_unref (v);
-
- rebuild_header_now (self);
-}
-static void
-on_show_time_setting_changed (GSettings * settings, gchar * key, gpointer gself)
-{
- set_show_time_flag (INDICATOR_POWER_SERVICE(gself),
- g_settings_get_boolean (settings, key));
}
-static void
-on_show_time_action_state_changed (GAction * action,
- GParamSpec * pspec G_GNUC_UNUSED,
- gpointer gself)
+static gboolean
+settings_to_action_state (GValue * value,
+ GVariant * variant,
+ gpointer user_data G_GNUC_UNUSED)
{
- GVariant * v = g_action_get_state (action);
- set_show_time_flag (INDICATOR_POWER_SERVICE(gself),
- g_variant_get_boolean (v));
- g_variant_unref (v);
+ g_value_set_variant (value, variant);
+ return TRUE;
}
-/* toggles the state */
-static void
-on_show_time_action_activated (GSimpleAction * simple,
- GVariant * parameter G_GNUC_UNUSED,
- gpointer unused G_GNUC_UNUSED)
+static GVariant *
+action_state_to_settings (const GValue * value,
+ const GVariantType * expected_type G_GNUC_UNUSED,
+ gpointer user_data G_GNUC_UNUSED)
{
- GVariant * v = g_action_get_state (G_ACTION (simple));
- gboolean flag = g_variant_get_boolean (v);
- g_simple_action_set_state (simple, g_variant_new_boolean (!flag));
- g_variant_unref (v);
+ return g_value_dup_variant (value);
}
static void
init_gactions (IndicatorPowerService * self)
{
GSimpleAction * a;
- gboolean show_time;
priv_t * p = self->priv;
GActionEntry entries[] = {
@@ -794,17 +767,31 @@ init_gactions (IndicatorPowerService * self)
p->brightness_action = a;
/* add the show-time action */
- show_time = g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S);
- a = g_simple_action_new_stateful ("show-time",
- NULL,
- g_variant_new_boolean(show_time));
- g_signal_connect (a, "activate",
- G_CALLBACK(on_show_time_action_activated), self);
- g_signal_connect (a, "notify",
- G_CALLBACK(on_show_time_action_state_changed), self);
+ a = g_simple_action_new ("show-time", NULL);
+ g_settings_bind_with_mapping (p->settings, SETTINGS_SHOW_TIME_S,
+ a, "state",
+ G_SETTINGS_BIND_DEFAULT,
+ settings_to_action_state,
+ action_state_to_settings,
+ NULL, NULL);
+ g_signal_connect (a, "activate", G_CALLBACK(on_toggle_action_activated), self);
+ g_signal_connect_swapped (a, "notify", G_CALLBACK(rebuild_header_now), self);
g_simple_action_group_insert (p->actions, G_ACTION(a));
p->show_time_action = a;
+ /* add the show-percentage action */
+ a = g_simple_action_new ("show-percentage", NULL);
+ g_settings_bind_with_mapping (p->settings, SETTINGS_SHOW_PERCENTAGE_S,
+ a, "state",
+ G_SETTINGS_BIND_DEFAULT,
+ settings_to_action_state,
+ action_state_to_settings,
+ NULL, NULL);
+ g_signal_connect (a, "activate", G_CALLBACK(on_toggle_action_activated), self);
+ g_signal_connect_swapped (a, "notify", G_CALLBACK(rebuild_header_now), self);
+ g_simple_action_group_insert (p->actions, G_ACTION(a));
+ p->show_percentage_action = a;
+
rebuild_header_now (self);
}
@@ -1015,10 +1002,16 @@ my_dispose (GObject * o)
g_clear_object (&p->show_time_action);
}
+ if (p->show_percentage_action != NULL)
+ {
+ g_signal_handlers_disconnect_by_data (p->show_percentage_action, self);
+
+ g_clear_object (&p->show_percentage_action);
+ }
+
g_clear_object (&p->brightness_action);
g_clear_object (&p->battery_level_action);
g_clear_object (&p->header_action);
- g_clear_object (&p->show_time_action);
g_clear_object (&p->actions);
g_clear_object (&p->conn);
@@ -1052,8 +1045,8 @@ indicator_power_service_init (IndicatorPowerService * self)
g_signal_connect_swapped (p->settings, "changed::" SETTINGS_ICON_POLICY_S,
G_CALLBACK(rebuild_header_now), self);
- g_signal_connect (p->settings, "changed::" SETTINGS_SHOW_TIME_S,
- G_CALLBACK(on_show_time_setting_changed), self);
+ //g_signal_connect (p->settings, "changed::" SETTINGS_SHOW_TIME_S,
+ // G_CALLBACK(on_show_time_setting_changed), self);
p->own_id = g_bus_own_name (G_BUS_TYPE_SESSION,
BUS_NAME,