aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/indicator-messages.c99
-rw-r--r--src/messages-service.c39
2 files changed, 74 insertions, 64 deletions
diff --git a/src/indicator-messages.c b/src/indicator-messages.c
index 942e46f..b802cfe 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,
@@ -212,8 +210,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)
+ menu_items_changed (self->menu, 0, 0, 1, self);
+ else
+ indicator_object_set_visible (INDICATOR_OBJECT (self), FALSE);
g_object_unref (bus);
}
@@ -261,24 +261,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 +281,7 @@ 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);
-}
-
-static void
-update_menu (IndicatorMessages *self)
-{
- GMenuModel *popup;
- GMenuItem *item;
-
- if (self->menu == NULL || g_menu_model_get_n_items (self->menu) == 0)
- return;
-
- popup = g_menu_model_get_item_link (self->menu, 0, G_MENU_LINK_SUBMENU);
- if (popup == NULL)
- return;
-
- if (g_menu_model_get_n_items (G_MENU_MODEL (self->menu_wrapper)) == 1)
- g_menu_remove (self->menu_wrapper, 0);
-
- 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);
+ return icon;
}
static void
@@ -328,9 +293,43 @@ 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) {
+ GIcon *icon;
+ GMenuModel *popup;
+
+ indicator_object_set_visible (INDICATOR_OBJECT (self), TRUE);
+
+ icon = g_menu_model_get_item_attribute_icon (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);
+ }
+
+ 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);
+ }
+
+ popup = g_menu_model_get_item_link (menu, 0, G_MENU_LINK_SUBMENU);
+ if (popup) {
+ GMenuItem *item;
+
+ 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);
+ }
+ }
+ 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 15c5123..d016ff1 100644
--- a/src/messages-service.c
+++ b/src/messages-service.c
@@ -175,6 +175,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;
@@ -211,6 +230,12 @@ remove_application (const char *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);
}
@@ -484,9 +509,6 @@ main (int argc, char ** argv)
{
GMainLoop * mainloop = NULL;
IndicatorService * service = NULL;
- GMenuItem *header;
- GIcon *icon;
- gchar *iconstr;
/* Glib init */
g_type_init();
@@ -524,16 +546,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");
@@ -544,8 +557,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);