diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2012-06-13 14:32:17 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2012-06-13 14:32:17 -0500 |
commit | 961c326fd07a11818c10cc827cd79caa83d2402c (patch) | |
tree | 6639f4afb506b739dadf179fc037af0eeca6a2ca /src | |
parent | 192be127691af87a88f4ac15d4fe3dd9296499b0 (diff) | |
download | ayatana-indicator-session-961c326fd07a11818c10cc827cd79caa83d2402c.tar.gz ayatana-indicator-session-961c326fd07a11818c10cc827cd79caa83d2402c.tar.bz2 ayatana-indicator-session-961c326fd07a11818c10cc827cd79caa83d2402c.zip |
Have the user widget respond to changes in the LOGGED_IN and ICON properties
Diffstat (limited to 'src')
-rw-r--r-- | src/user-widget.c | 161 | ||||
-rw-r--r-- | src/user-widget.h | 15 |
2 files changed, 100 insertions, 76 deletions
diff --git a/src/user-widget.c b/src/user-widget.c index f886660..9fac172 100644 --- a/src/user-widget.c +++ b/src/user-widget.c @@ -98,7 +98,9 @@ user_widget_class_init (UserWidgetClass *klass) static void user_widget_init (UserWidget *self) { - UserWidgetPrivate * priv = USER_WIDGET_GET_PRIVATE(self); + self->priv = USER_WIDGET_GET_PRIVATE(self); + + UserWidgetPrivate * priv = self->priv; gint padding = 0; gtk_widget_style_get (GTK_WIDGET(self), @@ -220,10 +222,11 @@ user_widget_draw_usericon_gtk_3 (GtkWidget *widget, UserWidget* meta = USER_WIDGET(user_data); UserWidgetPrivate * priv = USER_WIDGET_GET_PRIVATE(meta); - if (priv->using_personal_icon == FALSE) - return FALSE; + if (priv->using_personal_icon) + { + draw_album_border (widget, FALSE); + } - draw_album_border (widget, FALSE); return FALSE; } @@ -529,19 +532,91 @@ user_widget_button_release_event (GtkWidget *menuitem, return FALSE; } +/*** +**** +***/ + +static void +update_icon (UserWidget * self, DbusmenuMenuitem * mi) +{ + GdkPixbuf * pixbuf = NULL; + + /* first, try the menuitem's icon property */ + const gchar * icon_name = dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_ICON); + if (icon_name) + { + GError* error = NULL; + pixbuf = gdk_pixbuf_new_from_file_at_size (icon_name, 32, 32, &error); + if (error != NULL) + { + g_warning ("Couldn't load the image \"%s\": %s", icon_name, error->message); + g_clear_error (&error); + } + } + + /* as a fallback, try to use the default user icon */ + if (pixbuf != NULL) + { + self->priv->using_personal_icon = TRUE; + } + else + { + self->priv->using_personal_icon = FALSE; + + pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), + USER_ITEM_ICON_DEFAULT, + 32, + GTK_ICON_LOOKUP_FORCE_SIZE, + NULL); + } + + if (pixbuf != NULL) + { + gtk_image_set_from_pixbuf (GTK_IMAGE(self->priv->user_image), pixbuf); + g_object_unref (pixbuf); + } +} + +static void +update_logged_in (UserWidget * self, DbusmenuMenuitem * mi) +{ + const gboolean b = dbusmenu_menuitem_property_get_bool (mi, USER_ITEM_PROP_LOGGED_IN); + + g_debug ("User \"%s\" %s active sessions", + dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME), + b ? "has" : "doesn't have"); + + gtk_widget_set_visible (self->priv->tick_icon, b); +} + +static void +update_name (UserWidget * self, DbusmenuMenuitem * mi) +{ + gtk_label_set_label (GTK_LABEL(self->priv->user_name), + dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME)); +} + static void -user_widget_property_update (DbusmenuMenuitem * item, +user_widget_property_update (DbusmenuMenuitem * mi, const gchar * property, GVariant * value, UserWidget * self) { g_return_if_fail (IS_USER_WIDGET (self)); - UserWidgetPrivate* priv = USER_WIDGET_GET_PRIVATE(self); +g_message ("user_widget_property_update: %s", property); if (!g_strcmp0 (property, USER_ITEM_PROP_LOGGED_IN)) { - gtk_widget_set_visible (priv->tick_icon, g_variant_get_boolean(value)); + update_logged_in (self, mi); + } + else if (!g_strcmp0 (property, USER_ITEM_PROP_ICON)) + { + update_icon (self, mi); + } + else if (!g_strcmp0 (property, USER_ITEM_PROP_NAME)) + { + update_name (self, mi); } else { @@ -550,72 +625,16 @@ user_widget_property_update (DbusmenuMenuitem * item, } static void -user_widget_set_twin_item (UserWidget* self, - DbusmenuMenuitem* twin_item) +user_widget_set_twin_item (UserWidget * self, DbusmenuMenuitem * mi) { - UserWidgetPrivate* priv = USER_WIDGET_GET_PRIVATE(self); - priv->twin_item = twin_item; - g_signal_connect( G_OBJECT(priv->twin_item), "property-changed", + self->priv->twin_item = mi; + + update_icon (self, mi); + update_name (self, mi); + update_logged_in (self, mi); + + g_signal_connect (G_OBJECT(mi), "property-changed", G_CALLBACK(user_widget_property_update), self); - - const gchar * icon_name = dbusmenu_menuitem_property_get (twin_item, - USER_ITEM_PROP_ICON); - gtk_label_set_label (GTK_LABEL (priv->user_name), - dbusmenu_menuitem_property_get (twin_item, USER_ITEM_PROP_NAME)); - - if (dbusmenu_menuitem_property_get_bool (twin_item, USER_ITEM_PROP_LOGGED_IN)) { - g_debug ("%s USER HAS ACTIVE SESSIONS", - dbusmenu_menuitem_property_get (twin_item, USER_ITEM_PROP_NAME)); - gtk_widget_show(priv->tick_icon); - } - else { - g_debug ("%s USER DOESN'T HAVE ACTIVE SESSIONS", - dbusmenu_menuitem_property_get (twin_item, USER_ITEM_PROP_NAME)); - gtk_widget_hide(priv->tick_icon); - } - - GdkPixbuf* pixbuf = NULL; - GError* error = NULL; - pixbuf = gdk_pixbuf_new_from_file_at_size(icon_name, 32, 32, NULL); - - if (pixbuf == NULL || error != NULL) { - g_warning ("Could not load the user image (%s) for some reason", - icon_name); - if (pixbuf != NULL){ - g_object_unref (pixbuf); - pixbuf = NULL; - } - if (error != NULL){ - g_error_free (error); - error = NULL; - } - - priv->using_personal_icon = FALSE; - - pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), - USER_ITEM_ICON_DEFAULT, - 32, - GTK_ICON_LOOKUP_FORCE_SIZE, - &error); - } - else{ - priv->using_personal_icon = TRUE; - } - - if (pixbuf == NULL || error != NULL) { - g_warning ("Could not load the user image"); - if (error != NULL){ - g_error_free (error); - error = NULL; - } - } - else{ - gtk_image_set_from_pixbuf (GTK_IMAGE(priv->user_image), pixbuf); - } - if (pixbuf != NULL){ - g_object_unref (pixbuf); - pixbuf = NULL; - } } /** diff --git a/src/user-widget.h b/src/user-widget.h index 52a5e34..0953e6c 100644 --- a/src/user-widget.h +++ b/src/user-widget.h @@ -31,18 +31,23 @@ G_BEGIN_DECLS #define IS_USER_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), USER_WIDGET_TYPE)) #define USER_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), USER_WIDGET_TYPE, UserWidgetClass)) -typedef struct _UserWidget UserWidget; -typedef struct _UserWidgetClass UserWidgetClass; +typedef struct _UserWidget UserWidget; +typedef struct _UserWidgetClass UserWidgetClass; +typedef struct _UserWidgetPrivate UserWidgetPrivate; -struct _UserWidgetClass { +struct _UserWidgetClass +{ GtkMenuItemClass parent_class; }; -struct _UserWidget { +struct _UserWidget +{ + /*< private >*/ GtkMenuItem parent; + UserWidgetPrivate * priv; }; -GType user_widget_get_type (void); +GType user_widget_get_type (void) G_GNUC_CONST; GtkWidget* user_widget_new(DbusmenuMenuitem *twin_item); G_END_DECLS |