+ alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
+ alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
+
+ return g_utf8_collate (alias_a, alias_b);
+}
+
+static gint
+compare_roster_contacts_no_group (EmpathyRosterView *self,
+ EmpathyRosterContact *a,
+ EmpathyRosterContact *b)
+{
+ gboolean top_a, top_b;
+
+ top_a = contact_in_top (self, a);
+ top_b = contact_in_top (self, b);
+
+ if (top_a == top_b)
+ /* Both contacts are in the top of the roster (or not). Sort them
+ * alphabetically */
+ return compare_roster_contacts_by_conversation_time (a, b);
+ else if (top_a)
+ return -1;
+ else
+ return 1;
+}
+
+static gint
+compare_group_names (const gchar *group_a,
+ const gchar *group_b)
+{
+ if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
+ return -1;
+
+ if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
+ return 1;
+
+ if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
+ return 1;
+ else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
+ return -1;
+
+ return g_utf8_collate (group_a, group_b);
+}
+
+static gint
+compare_roster_contacts_with_groups (EmpathyRosterView *self,
+ EmpathyRosterContact *a,
+ EmpathyRosterContact *b)
+{
+ const gchar *group_a, *group_b;
+
+ group_a = empathy_roster_contact_get_group (a);
+ group_b = empathy_roster_contact_get_group (b);
+
+ if (!tp_strdiff (group_a, group_b))
+ /* Same group, compare the contacts */
+ return compare_roster_contacts_by_conversation_time (a, b);
+
+ /* Sort by group */
+ return compare_group_names (group_a, group_b);
+}
+
+static gint
+compare_roster_contacts (EmpathyRosterView *self,
+ EmpathyRosterContact *a,
+ EmpathyRosterContact *b)
+{
+ if (!self->priv->show_groups)
+ return compare_roster_contacts_no_group (self, a, b);
+ else
+ return compare_roster_contacts_with_groups (self, a, b);
+}
+
+static gint
+compare_roster_groups (EmpathyRosterGroup *a,
+ EmpathyRosterGroup *b)
+{
+ const gchar *name_a, *name_b;
+
+ name_a = empathy_roster_group_get_name (a);
+ name_b = empathy_roster_group_get_name (b);
+
+ return compare_group_names (name_a, name_b);
+}
+
+static gint
+compare_contact_group (EmpathyRosterContact *contact,
+ EmpathyRosterGroup *group)
+{
+ const char *contact_group, *group_name;
+
+ contact_group = empathy_roster_contact_get_group (contact);
+ group_name = empathy_roster_group_get_name (group);
+
+ if (!tp_strdiff (contact_group, group_name))
+ /* @contact is in @group, @group has to be displayed first */
+ return 1;
+
+ /* @contact is in a different group, sort by group name */
+ return compare_group_names (contact_group, group_name);
+}
+
+static gint
+roster_view_sort (GtkListBoxRow *a,
+ GtkListBoxRow *b,
+ gpointer user_data)
+{
+ EmpathyRosterView *self = user_data;
+
+ if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
+ return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
+ EMPATHY_ROSTER_CONTACT (b));
+ else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
+ return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
+ EMPATHY_ROSTER_GROUP (b));
+ else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
+ return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
+ EMPATHY_ROSTER_GROUP (b));
+ else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
+ return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
+ EMPATHY_ROSTER_GROUP (a));
+
+ g_return_val_if_reached (0);
+}
+
+static void
+update_header (GtkListBoxRow *row,
+ GtkListBoxRow *before,
+ gpointer user_data)
+{
+ if (before == NULL)
+ {
+ /* No separator before the first row */
+ gtk_list_box_row_set_header (row, NULL);
+ return;
+ }
+
+ if (gtk_list_box_row_get_header (row) != NULL)
+ return;
+
+ gtk_list_box_row_set_header (row,
+ gtk_separator_new (GTK_ORIENTATION_HORIZONTAL));
+}
+
+static gboolean
+is_searching (EmpathyRosterView *self)
+{
+ if (self->priv->search == NULL)
+ return FALSE;
+
+ return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
+}
+
+static void
+add_to_displayed (EmpathyRosterView *self,
+ EmpathyRosterContact *contact)
+{
+ FolksIndividual *individual;
+ GHashTable *contacts;
+ GHashTableIter iter;
+ gpointer k;
+
+ if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
+ return;
+
+ g_hash_table_add (self->priv->displayed_contacts, contact);
+ update_empty (self, FALSE);
+
+ /* Groups of this contact may now be displayed if we just displays the first
+ * child in this group. */
+
+ if (!self->priv->show_groups)
+ return;
+
+ individual = empathy_roster_contact_get_individual (contact);
+ contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
+ if (contacts == NULL)
+ return;
+
+ g_hash_table_iter_init (&iter, contacts);
+ while (g_hash_table_iter_next (&iter, &k, NULL))
+ {
+ const gchar *group_name = k;
+ GtkListBoxRow *group;
+
+ group = g_hash_table_lookup (self->priv->roster_groups, group_name);
+ if (group == NULL)
+ continue;
+
+ gtk_list_box_row_changed (group);
+ }
+}
+
+static void
+remove_from_displayed (EmpathyRosterView *self,
+ EmpathyRosterContact *contact)
+{
+ g_hash_table_remove (self->priv->displayed_contacts, contact);
+
+ check_if_empty (self);
+}
+
+static gboolean
+contact_is_favorite (EmpathyRosterContact *contact)
+{
+ FolksIndividual *individual;
+
+ individual = empathy_roster_contact_get_individual (contact);
+
+ return folks_favourite_details_get_is_favourite (
+ FOLKS_FAVOURITE_DETAILS (individual));
+}
+
+/**
+ * check if @contact should be displayed according to @self's current status
+ * and without consideration for the state of @contact's groups.
+ */
+static gboolean
+contact_should_be_displayed (EmpathyRosterView *self,
+ EmpathyRosterContact *contact)
+{
+ if (is_searching (self))
+ {
+ FolksIndividual *individual;
+
+ individual = empathy_roster_contact_get_individual (contact);
+
+ return empathy_individual_match_string (individual,
+ tpaw_live_search_get_text (self->priv->search),
+ tpaw_live_search_get_words (self->priv->search));
+ }
+
+ if (self->priv->show_offline)
+ return TRUE;
+
+ if (contact_in_top (self, contact) &&
+ contact_is_favorite (contact))
+ /* Favorite top contacts are always displayed */
+ return TRUE;
+
+ return empathy_roster_contact_is_online (contact);
+}
+
+
+static gboolean
+filter_contact (EmpathyRosterView *self,
+ EmpathyRosterContact *contact)
+{
+ gboolean displayed;
+
+ displayed = contact_should_be_displayed (self, contact);
+
+ if (self->priv->show_groups)
+ {
+ const gchar *group_name;
+ EmpathyRosterGroup *group;
+
+ group_name = empathy_roster_contact_get_group (contact);
+ group = lookup_roster_group (self, group_name);
+
+ if (group != NULL)
+ {
+ /* When searching, always display even if the group is closed */
+ if (!is_searching (self) &&
+ !gtk_expander_get_expanded (group->expander))
+ displayed = FALSE;
+ }
+ }
+
+ if (displayed)
+ {
+ add_to_displayed (self, contact);
+ }
+ else
+ {
+ remove_from_displayed (self, contact);
+ }
+
+ return displayed;
+}
+
+static gboolean
+filter_group (EmpathyRosterView *self,
+ EmpathyRosterGroup *group)
+{
+ GList *widgets, *l;
+ gboolean result = FALSE;
+
+ /* Display the group if it contains at least one displayed contact */
+ widgets = empathy_roster_group_get_widgets (group);
+ for (l = widgets; l != NULL; l = g_list_next (l))
+ {
+ EmpathyRosterContact *contact = l->data;
+
+ if (contact_should_be_displayed (self, contact))
+ {
+ result = TRUE;
+ break;
+ }
+ }
+
+ g_list_free (widgets);
+
+ return result;
+}
+
+static gboolean
+filter_list (GtkListBoxRow *child,
+ gpointer user_data)
+{
+ EmpathyRosterView *self = user_data;
+
+ if (EMPATHY_IS_ROSTER_CONTACT (child))
+ return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
+
+ else if (EMPATHY_IS_ROSTER_GROUP (child))
+ return filter_group (self, EMPATHY_ROSTER_GROUP (child));
+
+ g_return_val_if_reached (FALSE);
+}
+
+static void
+populate_view (EmpathyRosterView *self)
+{
+ GList *individuals, *l;
+
+ individuals = empathy_roster_model_get_individuals (self->priv->model);
+ for (l = individuals; l != NULL; l = g_list_next (l))
+ {
+ FolksIndividual *individual = l->data;
+
+ individual_added (self, individual);
+ }
+
+ g_list_free (individuals);
+}
+
+static void
+remove_from_group (EmpathyRosterView *self,
+ FolksIndividual *individual,
+ const gchar *group)
+{
+ GHashTable *contacts;
+ GtkWidget *contact;
+ EmpathyRosterGroup *roster_group;
+
+ contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
+ if (contacts == NULL)
+ return;
+
+ contact = g_hash_table_lookup (contacts, group);
+ if (contact == NULL)
+ return;
+
+ g_hash_table_remove (contacts, group);
+
+ if (g_hash_table_size (contacts) == 0)
+ {
+ add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
+ }
+
+ roster_group = lookup_roster_group (self, group);
+
+ if (roster_group != NULL)
+ {
+ update_group_widgets (self, roster_group,
+ EMPATHY_ROSTER_CONTACT (contact), FALSE);
+ }
+
+ gtk_container_remove (GTK_CONTAINER (self), contact);
+}
+
+static void
+groups_changed_cb (EmpathyRosterModel *model,
+ FolksIndividual *individual,
+ const gchar *group,
+ gboolean is_member,
+ EmpathyRosterView *self)
+{
+ if (!self->priv->show_groups)
+ {
+ gtk_list_box_invalidate_sort (GTK_LIST_BOX (self));
+ return;
+ }
+
+ if (is_member)
+ {
+ add_to_group (self, individual, group);
+ }
+ else
+ {
+ remove_from_group (self, individual, group);
+ }
+}
+
+static void
+empathy_roster_view_constructed (GObject *object)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
+ void (*chain_up) (GObject *) =
+ ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
+
+ if (chain_up != NULL)
+ chain_up (object);
+
+ g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
+
+ /* Get saved group states. */
+ empathy_contact_groups_get_all ();
+
+ populate_view (self);
+
+ tp_g_signal_connect_object (self->priv->model, "individual-added",
+ G_CALLBACK (individual_added_cb), self, 0);
+ tp_g_signal_connect_object (self->priv->model, "individual-removed",
+ G_CALLBACK (individual_removed_cb), self, 0);
+ tp_g_signal_connect_object (self->priv->model, "groups-changed",
+ G_CALLBACK (groups_changed_cb), self, 0);
+
+ gtk_list_box_set_sort_func (GTK_LIST_BOX (self),
+ roster_view_sort, self, NULL);
+
+ gtk_list_box_set_header_func (GTK_LIST_BOX (self), update_header, self, NULL);
+
+ gtk_list_box_set_filter_func (GTK_LIST_BOX (self), filter_list, self, NULL);
+
+ gtk_list_box_set_activate_on_single_click (GTK_LIST_BOX (self), FALSE);
+}
+
+static void
+clear_view (EmpathyRosterView *self)
+{
+ g_hash_table_remove_all (self->priv->roster_contacts);
+ g_hash_table_remove_all (self->priv->roster_groups);
+ g_hash_table_remove_all (self->priv->displayed_contacts);
+
+ gtk_container_foreach (GTK_CONTAINER (self),
+ (GtkCallback) gtk_widget_destroy, NULL);
+}
+
+static void
+empathy_roster_view_dispose (GObject *object)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
+ void (*chain_up) (GObject *) =
+ ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
+
+ /* Start by clearing the view so our internal hash tables are cleared from
+ * objects being destroyed. */
+ clear_view (self);
+
+ stop_flashing (self);
+
+ empathy_roster_view_set_live_search (self, NULL);
+ g_clear_object (&self->priv->model);
+
+ if (self->priv->search_id != 0)
+ {
+ g_source_remove (self->priv->search_id);
+ self->priv->search_id = 0;
+ }
+
+ if (chain_up != NULL)
+ chain_up (object);
+}
+
+static void
+empathy_roster_view_finalize (GObject *object)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
+ void (*chain_up) (GObject *) =
+ ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
+
+ g_hash_table_unref (self->priv->roster_contacts);
+ g_hash_table_unref (self->priv->roster_groups);
+ g_hash_table_unref (self->priv->displayed_contacts);
+ g_queue_free_full (self->priv->events, event_free);
+
+ if (chain_up != NULL)
+ chain_up (object);
+}
+
+static void
+empathy_roster_view_row_activated (GtkListBox *box,
+ GtkListBoxRow *row)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
+ EmpathyRosterContact *contact;
+ FolksIndividual *individual;
+ GList *l;
+
+ if (!EMPATHY_IS_ROSTER_CONTACT (row))
+ return;
+
+ contact = EMPATHY_ROSTER_CONTACT (row);
+ individual = empathy_roster_contact_get_individual (contact);
+
+ /* Activate the oldest event associated with this contact, if any */
+ for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
+ l = g_list_previous (l))
+ {
+ Event *event = l->data;
+
+ if (event->individual == individual)
+ {
+ g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
+ event->user_data);
+ return;
+ }
+ }
+
+ g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
+}
+
+static void
+fire_popup_individual_menu (EmpathyRosterView *self,
+ GtkListBoxRow *row,
+ guint button,
+ guint time)
+{
+ EmpathyRosterContact *contact;
+ FolksIndividual *individual;
+ const gchar *active_group;
+
+ if (!EMPATHY_IS_ROSTER_CONTACT (row))
+ return;
+
+ contact = EMPATHY_ROSTER_CONTACT (row);
+ individual = empathy_roster_contact_get_individual (contact);
+
+ active_group = empathy_roster_contact_get_group (contact);
+ g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
+ active_group, individual, button, time);
+}
+
+static gboolean
+empathy_roster_view_button_press_event (GtkWidget *widget,
+ GdkEventButton *event)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
+ gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
+ ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
+
+ if (event->button == 3)
+ {
+ GtkListBoxRow *row;
+
+ row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), event->y);
+
+ if (row != NULL)
+ {
+ gtk_list_box_select_row (GTK_LIST_BOX (self), row);
+
+ fire_popup_individual_menu (self, row, event->button, event->time);
+ }
+ }
+
+ return chain_up (widget, event);
+}
+
+static gboolean
+empathy_roster_view_key_press_event (GtkWidget *widget,
+ GdkEventKey *event)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
+ gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
+ ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
+
+ if (event->keyval == GDK_KEY_Menu)
+ {
+ GtkListBoxRow *row;
+
+ row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
+
+ if (row != NULL)
+ fire_popup_individual_menu (self, row, 0, event->time);
+ }
+
+ return chain_up (widget, event);
+}
+
+/**
+ * @out_row: (out) (allow-none)
+ */
+FolksIndividual *
+empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
+ gint y,
+ GtkListBoxRow **out_row)
+{
+ GtkListBoxRow *row;
+
+ row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
+
+ if (out_row != NULL)
+ *out_row = row;
+
+ if (!EMPATHY_IS_ROSTER_CONTACT (row))
+ return NULL;
+
+ return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));
+}
+
+const gchar *
+empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
+ gint y)
+{
+ GtkListBoxRow *row;
+
+ row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
+
+ if (EMPATHY_IS_ROSTER_CONTACT (row))
+ return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (row));
+ else if (EMPATHY_IS_ROSTER_GROUP (row))
+ return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (row));
+
+ return NULL;
+}
+
+static gboolean
+empathy_roster_view_query_tooltip (GtkWidget *widget,
+ gint x,
+ gint y,
+ gboolean keyboard_mode,
+ GtkTooltip *tooltip)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
+ FolksIndividual *individual;
+ gboolean result;
+ GtkListBoxRow *row;
+
+ individual = empathy_roster_view_get_individual_at_y (self, y, &row);
+ if (individual == NULL)
+ return FALSE;
+
+ g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
+ individual, keyboard_mode, tooltip, &result);
+
+ if (result)
+ {
+ GtkAllocation allocation;
+
+ gtk_widget_get_allocation (GTK_WIDGET (row), &allocation);
+ gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
+ }
+
+ return result;
+}
+
+static void
+empathy_roster_view_remove (GtkContainer *container,
+ GtkWidget *widget)
+{
+ EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
+ void (*chain_up) (GtkContainer *, GtkWidget *) =
+ ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
+
+ chain_up (container, widget);
+
+ if (EMPATHY_IS_ROSTER_CONTACT (widget))
+ remove_from_displayed (self, (EmpathyRosterContact *) widget);