Back to index

indicator-power  12.10.0
Classes | Defines | Typedefs | Functions
device.h File Reference
#include <glib-object.h>
#include <libupower-glib/upower.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _IndicatorPowerDeviceClass
 IndicatorPowerDeviceClass: : #GObjectClass. More...
struct  _IndicatorPowerDevice
 IndicatorPowerDevice: : #GObject : A cached reference to the private data for the instance. More...

Defines

#define INDICATOR_POWER_DEVICE_TYPE   (indicator_power_device_get_type ())
#define INDICATOR_POWER_DEVICE(obj)   (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDevice))
#define INDICATOR_POWER_DEVICE_CLASS(klass)   (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDeviceClass))
#define INDICATOR_IS_POWER_DEVICE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_DEVICE_TYPE))
#define INDICATOR_IS_POWER_DEVICE_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_DEVICE_TYPE))
#define INDICATOR_POWER_DEVICE_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDeviceClass))
#define INDICATOR_POWER_DEVICE_KIND   "kind"
#define INDICATOR_POWER_DEVICE_STATE   "state"
#define INDICATOR_POWER_DEVICE_OBJECT_PATH   "object-path"
#define INDICATOR_POWER_DEVICE_PERCENTAGE   "percentage"
#define INDICATOR_POWER_DEVICE_TIME   "time"

Typedefs

typedef struct _IndicatorPowerDevice
typedef struct _IndicatorPowerDeviceClass
typedef struct _IndicatorPowerDevicePrivate

Functions

GType indicator_power_device_get_type (void)
IndicatorPowerDevice * indicator_power_device_new (const gchar *object_path, UpDeviceKind kind, gdouble percentage, UpDeviceState state, time_t time)
IndicatorPowerDevice * indicator_power_device_new_from_variant (GVariant *variant)
 Convenience wrapper around indicator_power_device_new() holds the same args as indicator_power_device_new() in "(susdut)".
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)
 indicator_power_device_get_icon_names: : #IndicatorPowerDevice from which to generate the icon names
GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice *device)
 indicator_power_device_get_gicon: : #IndicatorPowerDevice to generate the icon names from
void indicator_power_device_get_time_details (const IndicatorPowerDevice *device, gchar **short_details, gchar **details, gchar **accessible_name)

Class Documentation

struct _IndicatorPowerDeviceClass

IndicatorPowerDeviceClass: : #GObjectClass.

Definition at line 53 of file device.h.

Class Members
GObjectClass parent_class
struct _IndicatorPowerDevice

IndicatorPowerDevice: : #GObject : A cached reference to the private data for the instance.

Definition at line 63 of file device.h.

Class Members
GObject parent
IndicatorPowerDevicePrivate * priv

Define Documentation

#define INDICATOR_IS_POWER_DEVICE (   obj)    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_DEVICE_TYPE))

Definition at line 35 of file device.h.

#define INDICATOR_IS_POWER_DEVICE_CLASS (   klass)    (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_POWER_DEVICE_TYPE))

Definition at line 36 of file device.h.

#define INDICATOR_POWER_DEVICE (   obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDevice))

Definition at line 33 of file device.h.

#define INDICATOR_POWER_DEVICE_CLASS (   klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDeviceClass))

Definition at line 34 of file device.h.

#define INDICATOR_POWER_DEVICE_GET_CLASS (   obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_POWER_DEVICE_TYPE, IndicatorPowerDeviceClass))

Definition at line 37 of file device.h.

#define INDICATOR_POWER_DEVICE_KIND   "kind"

Definition at line 43 of file device.h.

#define INDICATOR_POWER_DEVICE_OBJECT_PATH   "object-path"

Definition at line 45 of file device.h.

#define INDICATOR_POWER_DEVICE_PERCENTAGE   "percentage"

Definition at line 46 of file device.h.

#define INDICATOR_POWER_DEVICE_STATE   "state"

Definition at line 44 of file device.h.

#define INDICATOR_POWER_DEVICE_TIME   "time"

Definition at line 47 of file device.h.

Definition at line 32 of file device.h.


Typedef Documentation

typedef struct _IndicatorPowerDevice

Definition at line 39 of file device.h.

Definition at line 40 of file device.h.

Definition at line 41 of file device.h.


Function Documentation

GIcon* indicator_power_device_get_gicon ( const IndicatorPowerDevice *  device)

indicator_power_device_get_gicon: : #IndicatorPowerDevice to generate the icon names from

A convenience function to call g_themed_icon_new_from_names() with the names returned by indicator_power_device_get_icon_names()

Return value: (transfer full): A themed GIcon

Definition at line 418 of file device.c.

{
  GStrv names = indicator_power_device_get_icon_names (device);
  GIcon * icon = g_themed_icon_new_from_names (names, -1);
  g_strfreev (names);
  return icon;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GStrv indicator_power_device_get_icon_names ( const IndicatorPowerDevice *  device)

indicator_power_device_get_icon_names: : #IndicatorPowerDevice from which to generate the icon names

This function's logic differs from GSD's power plugin in some ways:

  1. All charging batteries use the same icon regardless of progress. https://bugs.launchpad.net/indicator-power/+bug/824629/comments/7
  1. For discharging batteries, we decide whether or not to use the 'caution' icon based on whether or not we have <= 30 minutes remaining, rather than looking at the battery's percentage left. https://bugs.launchpad.net/indicator-power/+bug/743823

See also indicator_power_device_get_gicon().

Return value: (array zero-terminated=1) (transfer full): A GStrv of icon names suitable for passing to g_themed_icon_new_from_names(). Free with g_strfreev() when done.

Definition at line 325 of file device.c.

{
  const gchar *suffix_str;
  const gchar *index_str;

  /* LCOV_EXCL_START */
  g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
  /* LCOV_EXCL_STOP */

  gdouble percentage = indicator_power_device_get_percentage (device);
  const UpDeviceKind kind = indicator_power_device_get_kind (device);
  const UpDeviceState state = indicator_power_device_get_state (device);
  const gchar * kind_str = kind_str = up_device_kind_to_string (kind);

  GPtrArray * names = g_ptr_array_new ();

  if (kind == UP_DEVICE_KIND_LINE_POWER)
    {
      g_ptr_array_add (names, g_strdup("ac-adapter-symbolic"));
      g_ptr_array_add (names, g_strdup("ac-adapter"));
    }
  else if (kind == UP_DEVICE_KIND_MONITOR)
    {
      g_ptr_array_add (names, g_strdup("gpm-monitor-symbolic"));
      g_ptr_array_add (names, g_strdup("gpm-monitor"));
    }
  else switch (state)
    {
      case UP_DEVICE_STATE_EMPTY:
        g_ptr_array_add (names, g_strdup("battery-empty-symbolic"));
        g_ptr_array_add (names, g_strdup_printf("gpm-%s-empty", kind_str));
        g_ptr_array_add (names, g_strdup_printf("gpm-%s-000", kind_str));
        g_ptr_array_add (names, g_strdup("battery-empty"));
        break;

      case UP_DEVICE_STATE_FULLY_CHARGED:
        g_ptr_array_add (names, g_strdup("battery-full-charged-symbolic"));
        g_ptr_array_add (names, g_strdup("battery-full-charging-symbolic"));
        g_ptr_array_add (names, g_strdup_printf("gpm-%s-full", kind_str));
        g_ptr_array_add (names, g_strdup_printf("gpm-%s-100", kind_str));
        g_ptr_array_add (names, g_strdup("battery-full-charged"));
        g_ptr_array_add (names, g_strdup("battery-full-charging"));
        break;

      case UP_DEVICE_STATE_CHARGING:
      case UP_DEVICE_STATE_PENDING_CHARGE:
        /* When charging, always use the same icon regardless of percentage.
           <http://bugs.launchpad.net/indicator-power/+bug/824629> */
        percentage = 0;

        suffix_str = get_device_icon_suffix (percentage);
        index_str = get_device_icon_index (percentage);
        g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging-symbolic", suffix_str));
        g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s-charging", kind_str, index_str));
        g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging", suffix_str));
        break;

      case UP_DEVICE_STATE_DISCHARGING:
      case UP_DEVICE_STATE_PENDING_DISCHARGE:
        /* Don't show the caution/red icons unless we have <=30 min left.
           <https://bugs.launchpad.net/indicator-power/+bug/743823>
           Themes use the caution color when the percentage is 0% or 20%,
           so if we have >30 min left, use 30% as the icon's percentage floor */
        if (indicator_power_device_get_time (device) > (30*60))
          percentage = MAX(percentage, 30);

        suffix_str = get_device_icon_suffix (percentage);
        index_str = get_device_icon_index (percentage);
        g_ptr_array_add (names, g_strdup_printf ("battery-%s-symbolic", suffix_str));
        g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s", kind_str, index_str));
        g_ptr_array_add (names, g_strdup_printf ("battery-%s", suffix_str));
        break;

      default:
        g_ptr_array_add (names, g_strdup("battery-missing-symbolic"));
        g_ptr_array_add (names, g_strdup("gpm-battery-missing"));
        g_ptr_array_add (names, g_strdup("battery-missing"));
    }

    g_ptr_array_add (names, NULL); /* terminates the strv */
    return (GStrv) g_ptr_array_free (names, FALSE);
}

Here is the call graph for this function:

Here is the caller graph for this function:

UpDeviceKind indicator_power_device_get_kind ( const IndicatorPowerDevice *  device)

Definition at line 230 of file device.c.

{
  /* LCOV_EXCL_START */
  g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN);
  /* LCOV_EXCL_STOP */

  return device->priv->kind;
}

Here is the caller graph for this function:

const gchar* indicator_power_device_get_object_path ( const IndicatorPowerDevice *  device)

Definition at line 250 of file device.c.

{
  /* LCOV_EXCL_START */
  g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), NULL);
  /* LCOV_EXCL_STOP */

  return device->priv->object_path;
}

Here is the caller graph for this function:

gdouble indicator_power_device_get_percentage ( const IndicatorPowerDevice *  device)

Definition at line 260 of file device.c.

{
  /* LCOV_EXCL_START */
  g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), 0.0);
  /* LCOV_EXCL_STOP */

  return device->priv->percentage;
}

Here is the caller graph for this function:

UpDeviceState indicator_power_device_get_state ( const IndicatorPowerDevice *  device)

Definition at line 240 of file device.c.

{
  /* LCOV_EXCL_START */
  g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN);
  /* LCOV_EXCL_STOP */

  return device->priv->state;
}

Here is the caller graph for this function:

time_t indicator_power_device_get_time ( const IndicatorPowerDevice *  device)

Definition at line 270 of file device.c.

{
  /* LCOV_EXCL_START */
  g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), (time_t)0);
  /* LCOV_EXCL_STOP */

  return device->priv->time;
}

Here is the caller graph for this function:

void indicator_power_device_get_time_details ( const IndicatorPowerDevice *  device,
gchar **  short_details,
gchar **  details,
gchar **  accessible_name 
)

Definition at line 539 of file device.c.

{
  if (!INDICATOR_IS_POWER_DEVICE(device))
    {
      *short_details = NULL;
      *details = NULL;
      *accessible_name = 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);

  if (time > 0)
    {
      gchar *short_timestring = NULL;
      gchar *detailed_timestring = NULL;

      get_timestring (time,
                      &short_timestring,
                      &detailed_timestring);

      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);
        }
      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_free (short_timestring);
      g_free (detailed_timestring);
    }
  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 ("");
    }
  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 (device_name);
    }
  else
    {
      *details = g_strdup_printf (_("%s (not present)"), device_name);
      *accessible_name = g_strdup (*details);
      *short_details = g_strdup (_("(not present)"));
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

IndicatorPowerDevice* indicator_power_device_new ( const gchar *  object_path,
UpDeviceKind  kind,
gdouble  percentage,
UpDeviceState  state,
time_t  time 
)

Definition at line 625 of file device.c.

Here is the caller graph for this function:

IndicatorPowerDevice* indicator_power_device_new_from_variant ( GVariant *  variant)

Convenience wrapper around indicator_power_device_new() holds the same args as indicator_power_device_new() in "(susdut)".

Definition at line 642 of file device.c.

{
  g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdut)")), NULL);

  UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN;
  UpDeviceState state = UP_DEVICE_STATE_UNKNOWN;
  const gchar * icon = NULL;
  const gchar * object_path = NULL;
  gdouble percentage = 0;
  guint64 time = 0;

  g_variant_get (v, "(&su&sdut)",
                 &object_path,
                 &kind,
                 &icon,
                 &percentage,
                 &state,
                 &time);

  return indicator_power_device_new (object_path,
                                     kind,
                                     percentage,
                                     state,
                                     time);
}

Here is the call graph for this function:

Here is the caller graph for this function: