diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/indicator-messages.c | 86 | ||||
-rw-r--r-- | src/messages-service.c | 40 |
2 files changed, 72 insertions, 54 deletions
diff --git a/src/indicator-messages.c b/src/indicator-messages.c index 942e46f..29979ed 100644 --- a/src/indicator-messages.c +++ b/src/indicator-messages.c @@ -83,8 +83,6 @@ static GtkImage * get_image (IndicatorObject * io); static GtkMenu * get_menu (IndicatorObject * io); static const gchar * get_accessible_desc (IndicatorObject * io); static const gchar * get_name_hint (IndicatorObject * io); -static void update_root_item (IndicatorMessages * self); -static void update_menu (IndicatorMessages *self); static void menu_items_changed (GMenuModel *menu, gint position, gint removed, @@ -94,6 +92,7 @@ static void messages_state_changed (GActionGroup *action_group, gchar *action_name, GVariant *value, gpointer user_data); +static void indicator_messages_add_toplevel_menu (IndicatorMessages *self); G_DEFINE_TYPE (IndicatorMessages, indicator_messages, INDICATOR_OBJECT_TYPE); @@ -212,8 +211,10 @@ static void service_connection_changed (IndicatorServiceManager *sm, INDICATOR_MESSAGES_DBUS_OBJECT)); g_signal_connect (self->menu, "items-changed", G_CALLBACK (menu_items_changed), self); - update_root_item (self); - update_menu (self); + if (g_menu_model_get_n_items (self->menu) == 1) + indicator_messages_add_toplevel_menu (self); + else + indicator_object_set_visible (INDICATOR_OBJECT (self), FALSE); g_object_unref (bus); } @@ -261,24 +262,19 @@ indicator_messages_accessible_desc_updated (IndicatorMessages *self) g_list_free (entries); } -static void -update_root_item (IndicatorMessages * self) +static GIcon * +g_menu_model_get_item_attribute_icon (GMenuModel *menu, + gint index, + const gchar *attribute) { gchar *iconstr; + GIcon *icon = NULL; - if (g_menu_model_get_n_items (self->menu) == 0) - return; - - if (g_menu_model_get_item_attribute (self->menu, 0, "x-canonical-icon", "s", &iconstr)) { - GIcon *icon; + if (g_menu_model_get_item_attribute (menu, index, attribute, "s", &iconstr)) { GError *error; icon = g_icon_new_for_string (iconstr, &error); - if (icon) { - gtk_image_set_from_gicon (GTK_IMAGE (self->image), icon, GTK_ICON_SIZE_MENU); - g_object_unref (icon); - } - else { + if (icon == NULL) { g_warning ("unable to load icon: %s", error->message); g_error_free (error); } @@ -286,37 +282,42 @@ update_root_item (IndicatorMessages * self) g_free (iconstr); } - g_free (self->accessible_desc); - self->accessible_desc = NULL; - - g_menu_model_get_item_attribute (self->menu, 0, "x-canonical-accessible-description", - "s", &self->accessible_desc); - indicator_messages_accessible_desc_updated (self); + return icon; } static void -update_menu (IndicatorMessages *self) +indicator_messages_add_toplevel_menu (IndicatorMessages *self) { + GIcon *icon; GMenuModel *popup; - GMenuItem *item; - if (self->menu == NULL || g_menu_model_get_n_items (self->menu) == 0) - return; + indicator_object_set_visible (INDICATOR_OBJECT (self), TRUE); - popup = g_menu_model_get_item_link (self->menu, 0, G_MENU_LINK_SUBMENU); - if (popup == NULL) - return; + icon = g_menu_model_get_item_attribute_icon (self->menu, 0, "x-canonical-icon"); + if (icon) { + gtk_image_set_from_gicon (GTK_IMAGE (self->image), icon, GTK_ICON_SIZE_MENU); + g_object_unref (icon); + } - if (g_menu_model_get_n_items (G_MENU_MODEL (self->menu_wrapper)) == 1) - g_menu_remove (self->menu_wrapper, 0); + g_free (self->accessible_desc); + self->accessible_desc = NULL; + if (g_menu_model_get_item_attribute (self->menu, 0, "x-canonical-accessible-description", + "s", &self->accessible_desc)) { + indicator_messages_accessible_desc_updated (self); + } - item = g_menu_item_new_section (NULL, popup); - g_menu_item_set_attribute (item, "action-namespace", - "s", get_name_hint (INDICATOR_OBJECT (self))); - g_menu_append_item (self->menu_wrapper, item); + popup = g_menu_model_get_item_link (self->menu, 0, G_MENU_LINK_SUBMENU); + if (popup) { + GMenuItem *item; - g_object_unref (item); - g_object_unref (popup); + item = g_menu_item_new_section (NULL, popup); + g_menu_item_set_attribute (item, "action-namespace", + "s", get_name_hint (INDICATOR_OBJECT (self))); + g_menu_append_item (self->menu_wrapper, item); + + g_object_unref (item); + g_object_unref (popup); + } } static void @@ -328,9 +329,14 @@ menu_items_changed (GMenuModel *menu, { IndicatorMessages *self = user_data; - if (position == 0) { - update_root_item (self); - update_menu (self); + g_return_if_fail (position == 0); + + if (added == 1) { + indicator_messages_add_toplevel_menu (self); + } + else if (removed == 1) { + g_menu_remove (self->menu_wrapper, 0); + indicator_object_set_visible (INDICATOR_OBJECT (self), FALSE); } } diff --git a/src/messages-service.c b/src/messages-service.c index acf4995..437f046 100644 --- a/src/messages-service.c +++ b/src/messages-service.c @@ -196,6 +196,25 @@ add_application (const gchar *desktop_id) g_object_unref (menuitem); } + if (g_menu_model_get_n_items (G_MENU_MODEL (toplevel_menu)) == 0) { + GMenuItem *header; + GIcon *icon; + gchar *iconstr; + + icon = g_themed_icon_new ("indicator-messages"); + iconstr = g_icon_to_string (icon); + + header = g_menu_item_new (NULL, "messages"); + g_menu_item_set_submenu (header, G_MENU_MODEL (menu)); + g_menu_item_set_attribute (header, "x-canonical-icon", "s", iconstr); + g_menu_item_set_attribute (header, "x-canonical-accessible-description", "s", _("Messages")); + g_menu_append_item (toplevel_menu, header); + + g_object_unref (header); + g_free (iconstr); + g_object_unref (icon); + } + g_free (id); g_object_unref (appinfo); return section; @@ -224,6 +243,13 @@ remove_application (const char *desktop_id) g_warning ("could not remove '%s', it's not registered", desktop_id); } + g_hash_table_remove (applications, id); + + if (g_hash_table_size (applications) == 0 && + g_menu_model_get_n_items (G_MENU_MODEL (toplevel_menu)) == 1) { + g_menu_remove (toplevel_menu, 0); + } + g_free (id); g_object_unref (appinfo); } @@ -497,9 +523,6 @@ main (int argc, char ** argv) { GMainLoop * mainloop = NULL; IndicatorService * service = NULL; - GMenuItem *header; - GIcon *icon; - gchar *iconstr; /* Glib init */ g_type_init(); @@ -537,16 +560,7 @@ main (int argc, char ** argv) chat_section = create_status_section (); g_menu_append (menu, _("Clear"), "clear"); - icon = g_themed_icon_new ("indicator-messages"); - iconstr = g_icon_to_string (icon); - toplevel_menu = g_menu_new (); - header = g_menu_item_new (NULL, "messages"); - g_menu_item_set_submenu (header, G_MENU_MODEL (menu)); - g_menu_item_set_attribute (header, "x-canonical-icon", "s", iconstr); - g_menu_item_set_attribute (header, "x-canonical-accessible-description", "s", _("Messages")); - g_menu_append_item (toplevel_menu, header); - g_object_unref (header); settings = g_settings_new ("com.canonical.indicator.messages"); @@ -557,8 +571,6 @@ main (int argc, char ** argv) g_main_loop_run(mainloop); /* Clean up */ - g_free (iconstr); - g_object_unref (icon); g_object_unref (messages_service); g_object_unref (chat_section); g_object_unref (settings); |