/* An indicator to power related information in the menubar. Copyright 2011 Canonical Ltd. Authors: Javier Jardon This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifdef HAVE_CONFIG_H #include "config.h" #endif /* GStuff */ #include #include #include #include /* Indicator Stuff */ #include #include #include #include #include "dbus-shared-names.h" #define DEFAULT_ICON "battery" #define INDICATOR_POWER_TYPE (indicator_power_get_type ()) #define INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_POWER_TYPE, IndicatorPower)) #define INDICATOR_POWER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_POWER_TYPE, IndicatorPowerClass)) #define IS_INDICATOR_POWER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_POWER_TYPE)) #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)) typedef struct _IndicatorPower IndicatorPower; typedef struct _IndicatorPowerClass IndicatorPowerClass; typedef struct _IndicatorPowerPrivate IndicatorPowerPrivate; struct _IndicatorPower { IndicatorObject parent_instance; IndicatorPowerPrivate *priv; }; struct _IndicatorPowerClass { IndicatorObjectClass parent_class; }; GType indicator_power_get_type (void) G_GNUC_CONST; struct _IndicatorPowerPrivate { IndicatorServiceManager *service; GtkMenu *menu; GtkLabel *label; GtkImage *status_image; GCancellable *service_proxy_cancel; GDBusProxy *service_proxy; }; /* 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); static GtkLabel* get_label (IndicatorObject * io); static GtkImage* get_image (IndicatorObject * io); static GtkMenu* get_menu (IndicatorObject * io); static const gchar* get_accessible_desc (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) { GObjectClass *object_class = G_OBJECT_CLASS (klass); IndicatorObjectClass *io_class = INDICATOR_OBJECT_CLASS (klass); object_class->dispose = indicator_power_dispose; object_class->finalize = indicator_power_finalize; io_class->get_label = get_label; io_class->get_image = get_image; io_class->get_menu = get_menu; io_class->get_accessible_desc = get_accessible_desc; g_type_class_add_private (klass, sizeof (IndicatorPowerPrivate)); } /* Update the power right now. Usually the result of a timezone switch. */ static void update_power (IndicatorPower *self) { /*TODO*/ } /* Receives all signals from the service, routed to the appropriate functions */ static void receive_signal (GDBusProxy *proxy, gchar *sender_name, gchar *signal_name, GVariant *parameters, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); if (g_strcmp0(signal_name, "UpdatePower") == 0) { update_power (self); } } /* Callback from trying to create the proxy for the service, this could include starting the service. Sometimes it'll fail and we'll try to start that dang service again! */ static void service_proxy_cb (GObject *object, GAsyncResult *res, gpointer user_data) { IndicatorPower *self = INDICATOR_POWER (user_data); IndicatorPowerPrivate *priv = self->priv; GDBusProxy *proxy; GError *error = NULL; proxy = g_dbus_proxy_new_for_bus_finish(res, &error); if (priv->service_proxy_cancel != NULL) { g_object_unref (priv->service_proxy_cancel); priv->service_proxy_cancel = NULL; } if (error != NULL) { g_error ("Could not grab DBus proxy for %s: %s", INDICATOR_POWER_DBUS_NAME, error->message); g_error_free (error); return; } /* Okay, we're good to grab the proxy at this point, we're sure that it's ours. */ priv->service_proxy = proxy; g_signal_connect (priv->service_proxy, "g-signal", G_CALLBACK (receive_signal), 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->service = NULL; priv->menu = NULL; /* Do stuff with them */ priv->service = indicator_service_manager_new_version (INDICATOR_POWER_DBUS_NAME, INDICATOR_POWER_DBUS_VERSION); /* g_signal_connect (G_OBJECT (priv->service), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, G_CALLBACK (connection_changed), self); */ priv->menu = GTK_MENU (gtk_menu_new ()); gtk_menu_set_title (priv->menu, _("Power")); gtk_menu_popup (priv->menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time ()); priv->service_proxy_cancel = g_cancellable_new(); g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL, INDICATOR_POWER_DBUS_NAME, INDICATOR_POWER_DBUS_OBJECT, INDICATOR_POWER_SERVICE_DBUS_INTERFACE, priv->service_proxy_cancel, service_proxy_cb, self); } static void indicator_power_dispose (GObject *object) { IndicatorPower *self = INDICATOR_POWER (object); IndicatorPowerPrivate *priv = self->priv; if (priv->service != NULL) { g_object_unref (G_OBJECT (priv->service)); priv->service = NULL; } G_OBJECT_CLASS (indicator_power_parent_class)->dispose (object); } static void indicator_power_finalize (GObject *object) { G_OBJECT_CLASS (indicator_power_parent_class)->finalize (object); } /* Grabs the label. Creates it if it doesn't exist already */ static GtkLabel * get_label (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); IndicatorPowerPrivate *priv = self->priv; if (priv->label == NULL) { /* Create the label if it doesn't exist already */ priv->label = GTK_LABEL (gtk_label_new ("Power")); } return priv->label; } static GtkImage * get_image (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); IndicatorPowerPrivate *priv = self->priv; if (priv->status_image == NULL) { /* Will create the status icon if it doesn't exist already */ priv->status_image = indicator_image_helper (DEFAULT_ICON "-panel"); gtk_widget_show (GTK_WIDGET (priv->status_image)); } return priv->status_image; } static GtkMenu * get_menu (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); IndicatorPowerPrivate *priv = self->priv; return GTK_MENU (priv->menu); } static const gchar * get_accessible_desc (IndicatorObject *io) { IndicatorPower *self = INDICATOR_POWER (io); IndicatorPowerPrivate *priv = self->priv; const gchar *name; if (priv->label == NULL) { name = gtk_label_get_text (priv->label); return name; } return NULL; }