[gnome-flashback] power-applet: add status icon
- From: Alberts Muktupāvels <muktupavels src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-flashback] power-applet: add status icon
- Date: Wed, 9 Sep 2015 21:48:38 +0000 (UTC)
commit f692640283cd8a8ea86161535f076fb5ceb5d73d
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date: Thu Sep 10 00:34:55 2015 +0300
power-applet: add status icon
Thanks György Balló for initial version in javascript.
https://bugzilla.gnome.org/show_bug.cgi?id=753425
configure.ac | 2 +-
gnome-flashback/libpower-applet/gf-power-applet.c | 306 ++++++++++++++++++++-
po/POTFILES.in | 1 +
3 files changed, 307 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 1c19e0e..92cff57 100644
--- a/configure.ac
+++ b/configure.ac
@@ -71,7 +71,7 @@ PKG_CHECK_MODULES(POLKIT, gtk+-3.0 >= $GTK_REQUIRED glib-2.0 >= $GLIB_REQUIRED p
AC_SUBST(POLKIT_CFLAGS)
AC_SUBST(POLKIT_LIBS)
-PKG_CHECK_MODULES(POWER_APPLET, gtk+-3.0 >= $GTK_REQUIRED)
+PKG_CHECK_MODULES(POWER_APPLET, gtk+-3.0 >= $GTK_REQUIRED upower-glib)
AC_SUBST(POWER_APPLET_CFLAGS)
AC_SUBST(POWER_APPLET_LIBS)
diff --git a/gnome-flashback/libpower-applet/gf-power-applet.c
b/gnome-flashback/libpower-applet/gf-power-applet.c
index 3d05485..afdd283 100644
--- a/gnome-flashback/libpower-applet/gf-power-applet.c
+++ b/gnome-flashback/libpower-applet/gf-power-applet.c
@@ -17,23 +17,327 @@
#include "config.h"
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <libupower-glib/upower.h>
+#include <math.h>
+
#include "gf-power-applet.h"
+#include "gf-upower-device.h"
+
+#define UPOWER_DBUS_NAME "org.freedesktop.UPower"
+#define UPOWER_DEVICE_DBUS_PATH "/org/freedesktop/UPower/devices/DisplayDevice"
struct _GfPowerApplet
{
- GObject parent;
+ GObject parent;
+
+ GtkStatusIcon *status_icon;
+ GfUPowerDevice *device;
};
G_DEFINE_TYPE (GfPowerApplet, gf_power_applet, G_TYPE_OBJECT)
static void
+statistics_cb (GtkMenuItem *item,
+ gpointer user_data)
+{
+ GAppInfo *app_info;
+ GError *error;
+
+ error = NULL;
+ app_info = g_app_info_create_from_commandline ("gnome-power-statistics",
+ "Power Statistics",
+ G_APP_INFO_CREATE_NONE,
+ &error);
+
+ if (error != NULL)
+ {
+ g_warning ("Failed to crete GAppInfo from commandline - %s",
+ error->message);
+ g_error_free (error);
+ }
+ else
+ {
+ g_app_info_launch (app_info, NULL, NULL, &error);
+
+ if (error != NULL)
+ {
+ g_warning ("Failed to start Power Statistics - %s", error->message);
+ g_error_free (error);
+ }
+ }
+
+ g_clear_object (&app_info);
+}
+
+static void
+settings_cb (GtkMenuItem *item,
+ gpointer user_data)
+{
+ GAppInfo *app_info;
+ GError *error;
+
+ error = NULL;
+ app_info = g_app_info_create_from_commandline ("gnome-control-center power",
+ "Power Settings",
+ G_APP_INFO_CREATE_NONE,
+ &error);
+
+ if (error != NULL)
+ {
+ g_warning ("Failed to crete GAppInfo from commandline - %s",
+ error->message);
+ g_error_free (error);
+ }
+ else
+ {
+ g_app_info_launch (app_info, NULL, NULL, &error);
+
+ if (error != NULL)
+ {
+ g_warning ("Failed to start Power Settings - %s", error->message);
+ g_error_free (error);
+ }
+ }
+
+ g_clear_object (&app_info);
+}
+
+static void
+popup_menu_cb (GtkStatusIcon *status_icon,
+ guint button,
+ guint activate_time,
+ gpointer user_data)
+{
+ const gchar *title;
+ gchar *tooltip_text;
+ gchar *label;
+ GtkWidget *menu;
+ GtkWidget *item;
+
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+
+ title = gtk_status_icon_get_title (status_icon);
+ tooltip_text = gtk_status_icon_get_tooltip_text (status_icon);
+ label = g_strdup_printf ("%s: %s", title, tooltip_text);
+ g_free (tooltip_text);
+
+ menu = gtk_menu_new ();
+
+ item = gtk_menu_item_new_with_label (label);
+ g_free (label);
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+ g_signal_connect (item, "activate", G_CALLBACK (statistics_cb), NULL);
+
+ item = gtk_menu_item_new_with_label (_("Power Settings"));
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+ g_signal_connect (item, "activate", G_CALLBACK (settings_cb), NULL);
+
+ gtk_widget_show_all (menu);
+ gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
+ gtk_status_icon_position_menu, status_icon,
+ button, activate_time);
+
+ G_GNUC_END_IGNORE_DEPRECATIONS
+}
+
+static gchar *
+get_icon_name (GfPowerApplet *applet)
+{
+ gchar *icon_name;
+ gchar *symbolic;
+
+ icon_name = g_strdup (gf_upower_device_get_icon_name (applet->device));
+
+ if (icon_name == NULL || icon_name[0] == '\0')
+ {
+ g_free (icon_name);
+ return g_strdup ("battery");
+ }
+
+ symbolic = g_strrstr (icon_name, "-symbolic");
+
+ if (symbolic != NULL)
+ g_strlcpy (symbolic, "", sizeof (symbolic));
+
+ return icon_name;
+}
+
+static gchar *
+get_tooltip_text (GfPowerApplet *applet)
+{
+ UpDeviceState state;
+ gint64 seconds;
+ gdouble time;
+ gdouble minutes;
+ gdouble hours;
+ gdouble percentage;
+
+ state = gf_upower_device_get_state (applet->device);
+
+ if (state == UP_DEVICE_STATE_FULLY_CHARGED)
+ return g_strdup (_("Fully Charged"));
+ else if (state == UP_DEVICE_STATE_EMPTY)
+ return g_strdup (_("Empty"));
+ else if (state == UP_DEVICE_STATE_CHARGING)
+ seconds = gf_upower_device_get_time_to_full (applet->device);
+ else if (state == UP_DEVICE_STATE_DISCHARGING)
+ seconds = gf_upower_device_get_time_to_empty (applet->device);
+ else
+ return g_strdup (_("Estimating..."));
+
+ time = round (seconds / 60);
+
+ if (time == 0)
+ return g_strdup (_("Estimating..."));
+
+ minutes = fmod (time, 60);
+ hours = floor (time / 60);
+ percentage = gf_upower_device_get_percentage (applet->device);
+
+ if (state == UP_DEVICE_STATE_DISCHARGING)
+ {
+ /* Translators: this is <hours>:<minutes> Remaining (<percentage>) */
+ return g_strdup_printf (_("%.0f:%02.0f Remaining (%.0f%%)"),
+ hours, minutes, percentage);
+ }
+
+ if (state == UP_DEVICE_STATE_CHARGING)
+ {
+ /* Translators: this is <hours>:<minutes> Until Full (<percentage>) */
+ return g_strdup_printf (_("%.0f:%02.0f Remaining (%.0f%%)"),
+ hours, minutes, percentage);
+ }
+
+ return NULL;
+}
+
+static const gchar *
+get_title (GfPowerApplet *applet)
+{
+ UpDeviceKind type;
+
+ type = gf_upower_device_get_type_ (applet->device);
+
+ if (type == UP_DEVICE_KIND_UPS)
+ return _("UPS");
+
+ return _("Battery");
+}
+
+static void
+gf_power_applet_sync (GfPowerApplet *applet)
+{
+ gchar *icon_name;
+ gchar *tooltip_text;
+ const gchar *title;
+ gboolean is_present;
+
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+
+ icon_name = get_icon_name (applet);
+ gtk_status_icon_set_from_icon_name (applet->status_icon, icon_name);
+ g_free (icon_name);
+
+ tooltip_text = get_tooltip_text (applet);
+ gtk_status_icon_set_tooltip_text (applet->status_icon, tooltip_text);
+ g_free (tooltip_text);
+
+ title = get_title (applet);
+ gtk_status_icon_set_title (applet->status_icon, title);
+
+ is_present = gf_upower_device_get_is_present (applet->device);
+ gtk_status_icon_set_visible (applet->status_icon, is_present);
+
+ G_GNUC_END_IGNORE_DEPRECATIONS
+}
+
+static void
+properties_changed_cb (GDBusProxy *proxy,
+ GVariant *changed_properties,
+ GStrv invalidated_properties,
+ gpointer user_data)
+{
+ GfPowerApplet *applet;
+
+ applet = GF_POWER_APPLET (user_data);
+
+ gf_power_applet_sync (applet);
+}
+
+static void
+device_proxy_ready_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GfPowerApplet *applet;
+ GError *error;
+
+ applet = GF_POWER_APPLET (user_data);
+
+ error = NULL;
+ applet->device = gf_upower_device_proxy_new_for_bus_finish (res, &error);
+
+ if (error != NULL)
+ {
+ g_warning ("Failed to get UPower device proxy - %s", error->message);
+ g_error_free (error);
+
+ return;
+ }
+
+ g_signal_connect (applet->device, "g-properties-changed",
+ G_CALLBACK (properties_changed_cb), applet);
+
+ gf_power_applet_sync (applet);
+}
+
+static void
+gf_power_applet_dispose (GObject *object)
+{
+ GfPowerApplet *applet;
+
+ applet = GF_POWER_APPLET (object);
+
+ g_clear_object (&applet->status_icon);
+ g_clear_object (&applet->device);
+
+ G_OBJECT_CLASS (gf_power_applet_parent_class)->dispose (object);
+}
+
+static void
gf_power_applet_class_init (GfPowerAppletClass *applet_class)
{
+ GObjectClass *object_class;
+
+ object_class = G_OBJECT_CLASS (applet_class);
+
+ object_class->dispose = gf_power_applet_dispose;
}
static void
gf_power_applet_init (GfPowerApplet *applet)
{
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+
+ applet->status_icon = gtk_status_icon_new ();
+
+ gtk_status_icon_set_title (applet->status_icon, _("Power status"));
+ gtk_status_icon_set_tooltip_text (applet->status_icon, _("Power"));
+
+ G_GNUC_END_IGNORE_DEPRECATIONS
+
+ g_signal_connect (applet->status_icon, "popup-menu",
+ G_CALLBACK (popup_menu_cb), applet);
+
+ gf_upower_device_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
+ G_DBUS_PROXY_FLAGS_NONE,
+ UPOWER_DBUS_NAME,
+ UPOWER_DEVICE_DBUS_PATH,
+ NULL,
+ device_proxy_ready_cb,
+ applet);
}
GfPowerApplet *
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 43a93f0..0be07fd 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -21,6 +21,7 @@ gnome-flashback/libpolkit/flashback-authenticator.c
gnome-flashback/libpolkit/flashback-listener.c
gnome-flashback/libpolkit/flashback-polkit-dialog.c
[type: gettext/glade]gnome-flashback/libpolkit/flashback-polkit-dialog.ui
+gnome-flashback/libpower-applet/gf-power-applet.c
gnome-flashback/libsound-applet/gf-sound-applet.c
gnome-flashback/libsound-applet/gvc-channel-bar.c
gnome-flashback/libsound-applet/gvc/gvc-mixer-control.c
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]