empathy_roster_model_get_groups_for_individual: return a (transfer full) list
[empathy-mirror.git] / libempathy-gtk / empathy-roster-view.c
blobb5f2ccd074fd42840c1c21064534a93dc5562b6b
1 #include "config.h"
3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy/empathy-contact-groups.h>
9 #include <libempathy-gtk/empathy-roster-contact.h>
10 #include <libempathy-gtk/empathy-roster-group.h>
11 #include <libempathy-gtk/empathy-ui-utils.h>
13 #include <libempathy/empathy-utils.h>
15 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
17 /* Flashing delay for icons (milliseconds). */
18 #define FLASH_TIMEOUT 500
20 enum
22 PROP_MODEL = 1,
23 PROP_SHOW_OFFLINE,
24 PROP_SHOW_GROUPS,
25 PROP_EMPTY,
26 N_PROPS
29 enum
31 SIG_INDIVIDUAL_ACTIVATED,
32 SIG_POPUP_INDIVIDUAL_MENU,
33 SIG_EVENT_ACTIVATED,
34 SIG_INDIVIDUAL_TOOLTIP,
35 LAST_SIGNAL
38 static guint signals[LAST_SIGNAL];
40 #define NO_GROUP "X-no-group"
42 struct _EmpathyRosterViewPriv
44 /* FolksIndividual (borrowed) -> GHashTable (
45 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
47 * When not using groups, this hash just have one element mapped
48 * from the special NO_GROUP key. We could use it as a set but
49 * I prefer to stay coherent in the way this hash is managed.
51 GHashTable *roster_contacts;
52 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
53 GHashTable *roster_groups;
54 /* Hash of the EmpathyRosterContact currently displayed */
55 GHashTable *displayed_contacts;
57 guint last_event_id;
58 /* queue of (Event *). The most recent events are in the head of the queue
59 * so we always display the icon of the oldest one. */
60 GQueue *events;
61 guint flash_id;
62 gboolean display_flash_event;
64 gboolean show_offline;
65 gboolean show_groups;
66 gboolean empty;
68 EmpathyLiveSearch *search;
70 EmpathyRosterModel *model;
73 typedef struct
75 guint id;
76 FolksIndividual *individual;
77 gchar *icon;
78 gpointer user_data;
79 } Event;
81 static Event *
82 event_new (guint id,
83 FolksIndividual *individual,
84 const gchar *icon,
85 gpointer user_data)
87 Event *event = g_slice_new (Event);
89 event->id = id;
90 event->individual = g_object_ref (individual);
91 event->icon = g_strdup (icon);
92 event->user_data = user_data;
93 return event;
96 static void
97 event_free (gpointer data)
99 Event *event = data;
100 g_object_unref (event->individual);
101 g_free (event->icon);
103 g_slice_free (Event, event);
106 static void
107 empathy_roster_view_get_property (GObject *object,
108 guint property_id,
109 GValue *value,
110 GParamSpec *pspec)
112 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
114 switch (property_id)
116 case PROP_MODEL:
117 g_value_set_object (value, self->priv->model);
118 break;
119 case PROP_SHOW_OFFLINE:
120 g_value_set_boolean (value, self->priv->show_offline);
121 break;
122 case PROP_SHOW_GROUPS:
123 g_value_set_boolean (value, self->priv->show_groups);
124 break;
125 case PROP_EMPTY:
126 g_value_set_boolean (value, self->priv->empty);
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
130 break;
134 static void
135 empathy_roster_view_set_property (GObject *object,
136 guint property_id,
137 const GValue *value,
138 GParamSpec *pspec)
140 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
142 switch (property_id)
144 case PROP_MODEL:
145 g_assert (self->priv->model == NULL);
146 self->priv->model = g_value_dup_object (value);
147 break;
148 case PROP_SHOW_OFFLINE:
149 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
150 break;
151 case PROP_SHOW_GROUPS:
152 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
153 break;
154 default:
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
156 break;
160 static void
161 roster_contact_changed_cb (GtkWidget *child,
162 GParamSpec *spec,
163 EmpathyRosterView *self)
165 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
168 static GtkWidget *
169 add_roster_contact (EmpathyRosterView *self,
170 FolksIndividual *individual,
171 const gchar *group)
173 GtkWidget *contact;
175 contact = empathy_roster_contact_new (individual, group);
177 /* Need to refilter if online is changed */
178 g_signal_connect (contact, "notify::online",
179 G_CALLBACK (roster_contact_changed_cb), self);
181 /* Need to resort if alias is changed */
182 g_signal_connect (contact, "notify::alias",
183 G_CALLBACK (roster_contact_changed_cb), self);
185 gtk_widget_show (contact);
186 gtk_container_add (GTK_CONTAINER (self), contact);
188 return contact;
191 static void
192 group_expanded_cb (EmpathyRosterGroup *group,
193 GParamSpec *spec,
194 EmpathyRosterView *self)
196 GList *widgets, *l;
198 widgets = empathy_roster_group_get_widgets (group);
199 for (l = widgets; l != NULL; l = g_list_next (l))
201 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
204 g_list_free (widgets);
206 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
207 gtk_expander_get_expanded (GTK_EXPANDER (group)));
210 static EmpathyRosterGroup *
211 lookup_roster_group (EmpathyRosterView *self,
212 const gchar *group)
214 return g_hash_table_lookup (self->priv->roster_groups, group);
217 static EmpathyRosterGroup *
218 ensure_roster_group (EmpathyRosterView *self,
219 const gchar *group)
221 GtkWidget *roster_group;
223 roster_group = (GtkWidget *) lookup_roster_group (self, group);
224 if (roster_group != NULL)
225 return EMPATHY_ROSTER_GROUP (roster_group);
227 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
228 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
229 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
230 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
231 else
232 roster_group = empathy_roster_group_new (group, NULL);
234 gtk_expander_set_expanded (GTK_EXPANDER (roster_group),
235 empathy_contact_group_get_expanded (group));
237 g_signal_connect (roster_group, "notify::expanded",
238 G_CALLBACK (group_expanded_cb), self);
240 gtk_widget_show (roster_group);
241 gtk_container_add (GTK_CONTAINER (self), roster_group);
243 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
244 roster_group);
246 return EMPATHY_ROSTER_GROUP (roster_group);
249 static void
250 update_empty (EmpathyRosterView *self,
251 gboolean empty)
253 if (self->priv->empty == empty)
254 return;
256 self->priv->empty = empty;
257 g_object_notify (G_OBJECT (self), "empty");
260 static gboolean filter_group (EmpathyRosterView *self,
261 EmpathyRosterGroup *group);
263 static gboolean
264 at_least_one_group_displayed (EmpathyRosterView *self)
266 GHashTableIter iter;
267 gpointer v;
269 g_hash_table_iter_init (&iter, self->priv->roster_groups);
270 while (g_hash_table_iter_next (&iter, NULL, &v))
272 EmpathyRosterGroup *group = EMPATHY_ROSTER_GROUP (v);
274 if (filter_group (self, group))
275 return TRUE;
278 return FALSE;
281 static void
282 check_if_empty (EmpathyRosterView *self)
284 /* Roster is considered as empty if there is no contact *and* no group
285 * currently displayed. */
286 if (g_hash_table_size (self->priv->displayed_contacts) != 0 ||
287 at_least_one_group_displayed (self))
289 update_empty (self, FALSE);
290 return;
293 update_empty (self, TRUE);
296 static void
297 update_group_widgets (EmpathyRosterView *self,
298 EmpathyRosterGroup *group,
299 EmpathyRosterContact *contact,
300 gboolean add)
302 guint old_count, count;
304 old_count = empathy_roster_group_get_widgets_count (group);
306 if (add)
307 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
308 else
309 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
311 if (count != old_count)
313 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
315 check_if_empty (self);
319 static void
320 add_to_group (EmpathyRosterView *self,
321 FolksIndividual *individual,
322 const gchar *group)
324 GtkWidget *contact;
325 GHashTable *contacts;
326 EmpathyRosterGroup *roster_group = NULL;
328 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
329 if (contacts == NULL)
330 return;
332 if (g_hash_table_lookup (contacts, group) != NULL)
333 return;
335 if (tp_strdiff (group, NO_GROUP))
336 roster_group = ensure_roster_group (self, group);
338 contact = add_roster_contact (self, individual, group);
339 g_hash_table_insert (contacts, g_strdup (group), contact);
341 if (roster_group != NULL)
343 update_group_widgets (self, roster_group,
344 EMPATHY_ROSTER_CONTACT (contact), TRUE);
348 static void
349 individual_favourite_change_cb (FolksIndividual *individual,
350 GParamSpec *spec,
351 EmpathyRosterView *self)
353 /* We may have to refilter the contact as only favorite contacts are always
354 * displayed regardless of their presence. */
355 GHashTable *contacts;
356 GtkWidget *contact;
358 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
359 if (contacts == NULL)
360 return;
362 if (self->priv->show_groups)
363 contact = g_hash_table_lookup (contacts,
364 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
365 else
366 contact = g_hash_table_lookup (contacts, NO_GROUP);
368 if (contact == NULL)
369 return;
371 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
374 static void
375 individual_added (EmpathyRosterView *self,
376 FolksIndividual *individual)
378 GHashTable *contacts;
380 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
381 if (contacts != NULL)
382 return;
384 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
386 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
388 if (!self->priv->show_groups)
390 add_to_group (self, individual, NO_GROUP);
392 else
394 GList *groups, *l;
396 groups = empathy_roster_model_get_groups_for_individual (self->priv->model,
397 individual);
399 if (g_list_length (groups) > 0)
401 for (l = groups; l != NULL; l = g_list_next (l))
403 add_to_group (self, individual, l->data);
406 else
408 /* No group, adds to Ungrouped */
409 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
412 g_list_free_full (groups, g_free);
415 tp_g_signal_connect_object (individual, "notify::is-favourite",
416 G_CALLBACK (individual_favourite_change_cb), self, 0);
419 static void
420 set_event_icon_on_individual (EmpathyRosterView *self,
421 FolksIndividual *individual,
422 const gchar *icon)
424 GHashTable *contacts;
425 GHashTableIter iter;
426 gpointer v;
428 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
429 if (contacts == NULL)
430 return;
432 g_hash_table_iter_init (&iter, contacts);
433 while (g_hash_table_iter_next (&iter, NULL, &v))
435 EmpathyRosterContact *contact =v;
437 empathy_roster_contact_set_event_icon (contact, icon);
441 static void
442 flash_event (Event *event,
443 EmpathyRosterView *self)
445 set_event_icon_on_individual (self, event->individual, event->icon);
448 static void
449 unflash_event (Event *event,
450 EmpathyRosterView *self)
452 set_event_icon_on_individual (self, event->individual, NULL);
455 static gboolean
456 flash_cb (gpointer data)
458 EmpathyRosterView *self = data;
460 if (self->priv->display_flash_event)
462 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
463 self->priv->display_flash_event = FALSE;
465 else
467 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
468 self->priv->display_flash_event = TRUE;
471 return TRUE;
474 static void
475 start_flashing (EmpathyRosterView *self)
477 if (self->priv->flash_id != 0)
478 return;
480 self->priv->display_flash_event = TRUE;
482 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
483 flash_cb, self);
486 static void
487 stop_flashing (EmpathyRosterView *self)
489 if (self->priv->flash_id == 0)
490 return;
492 g_source_remove (self->priv->flash_id);
493 self->priv->flash_id = 0;
496 static void
497 remove_event (EmpathyRosterView *self,
498 Event *event)
500 unflash_event (event, self);
501 g_queue_remove (self->priv->events, event);
503 if (g_queue_get_length (self->priv->events) == 0)
505 stop_flashing (self);
509 static void
510 remove_all_individual_event (EmpathyRosterView *self,
511 FolksIndividual *individual)
513 GList *l;
515 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
516 l = g_list_next (l))
518 Event *event = l->data;
520 if (event->individual == individual)
522 remove_event (self, event);
523 return;
528 static void
529 individual_removed (EmpathyRosterView *self,
530 FolksIndividual *individual)
532 GHashTable *contacts;
533 GHashTableIter iter;
534 gpointer key, value;
536 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
537 if (contacts == NULL)
538 return;
540 remove_all_individual_event (self, individual);
542 g_hash_table_iter_init (&iter, contacts);
543 while (g_hash_table_iter_next (&iter, &key, &value))
545 const gchar *group_name = key;
546 GtkWidget *contact = value;
547 EmpathyRosterGroup *group;
549 group = lookup_roster_group (self, group_name);
550 if (group != NULL)
552 update_group_widgets (self, group,
553 EMPATHY_ROSTER_CONTACT (contact), FALSE);
556 gtk_container_remove (GTK_CONTAINER (self), contact);
559 g_hash_table_remove (self->priv->roster_contacts, individual);
562 static void
563 individual_added_cb (EmpathyRosterModel *model,
564 FolksIndividual *individual,
565 EmpathyRosterView *self)
567 individual_added (self, individual);
570 static void
571 individual_removed_cb (EmpathyRosterModel *model,
572 FolksIndividual *individual,
573 EmpathyRosterView *self)
575 individual_removed (self, individual);
578 static gboolean
579 contact_in_top (EmpathyRosterView *self,
580 EmpathyRosterContact *contact)
582 if (!self->priv->show_groups)
584 /* Always display top contacts in non-group mode. */
585 GList *groups;
586 FolksIndividual *individual;
587 gboolean result = FALSE;
589 individual = empathy_roster_contact_get_individual (contact);
591 groups = empathy_roster_model_get_groups_for_individual (
592 self->priv->model, individual);
594 if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
595 (GCompareFunc) g_strcmp0) != NULL)
596 result = TRUE;
598 g_list_free_full (groups, g_free);
600 return result;
603 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
604 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
605 /* If we are displaying contacts, we only want to *always* display the
606 * RosterContact which is displayed at the top; not the ones displayed in
607 * the 'normal' group sections */
608 return TRUE;
610 return FALSE;
613 static gint
614 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
615 EmpathyRosterContact *b)
617 FolksIndividual *ind_a, *ind_b;
618 const gchar *alias_a, *alias_b;
620 ind_a = empathy_roster_contact_get_individual (a);
621 ind_b = empathy_roster_contact_get_individual (b);
623 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
624 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
626 return g_ascii_strcasecmp (alias_a, alias_b);
629 static gint
630 compare_roster_contacts_no_group (EmpathyRosterView *self,
631 EmpathyRosterContact *a,
632 EmpathyRosterContact *b)
634 gboolean top_a, top_b;
636 top_a = contact_in_top (self, a);
637 top_b = contact_in_top (self, b);
639 if (top_a == top_b)
640 /* Both contacts are in the top of the roster (or not). Sort them
641 * alphabetically */
642 return compare_roster_contacts_by_alias (a, b);
643 else if (top_a)
644 return -1;
645 else
646 return 1;
649 static gint
650 compare_group_names (const gchar *group_a,
651 const gchar *group_b)
653 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
654 return -1;
656 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
657 return 1;
659 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
660 return 1;
661 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
662 return -1;
664 return g_ascii_strcasecmp (group_a, group_b);
667 static gint
668 compare_roster_contacts_with_groups (EmpathyRosterView *self,
669 EmpathyRosterContact *a,
670 EmpathyRosterContact *b)
672 const gchar *group_a, *group_b;
674 group_a = empathy_roster_contact_get_group (a);
675 group_b = empathy_roster_contact_get_group (b);
677 if (!tp_strdiff (group_a, group_b))
678 /* Same group, compare the contacts */
679 return compare_roster_contacts_by_alias (a, b);
681 /* Sort by group */
682 return compare_group_names (group_a, group_b);
685 static gint
686 compare_roster_contacts (EmpathyRosterView *self,
687 EmpathyRosterContact *a,
688 EmpathyRosterContact *b)
690 if (!self->priv->show_groups)
691 return compare_roster_contacts_no_group (self, a, b);
692 else
693 return compare_roster_contacts_with_groups (self, a, b);
696 static gint
697 compare_roster_groups (EmpathyRosterGroup *a,
698 EmpathyRosterGroup *b)
700 const gchar *name_a, *name_b;
702 name_a = empathy_roster_group_get_name (a);
703 name_b = empathy_roster_group_get_name (b);
705 return compare_group_names (name_a, name_b);
708 static gint
709 compare_contact_group (EmpathyRosterContact *contact,
710 EmpathyRosterGroup *group)
712 const char *contact_group, *group_name;
714 contact_group = empathy_roster_contact_get_group (contact);
715 group_name = empathy_roster_group_get_name (group);
717 if (!tp_strdiff (contact_group, group_name))
718 /* @contact is in @group, @group has to be displayed first */
719 return 1;
721 /* @contact is in a different group, sort by group name */
722 return compare_group_names (contact_group, group_name);
725 static gint
726 roster_view_sort (gconstpointer a,
727 gconstpointer b,
728 gpointer user_data)
730 EmpathyRosterView *self = user_data;
732 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
733 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
734 EMPATHY_ROSTER_CONTACT (b));
735 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
736 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
737 EMPATHY_ROSTER_GROUP (b));
738 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
739 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
740 EMPATHY_ROSTER_GROUP (b));
741 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
742 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
743 EMPATHY_ROSTER_GROUP (a));
745 g_return_val_if_reached (0);
748 static void
749 update_separator (GtkWidget **separator,
750 GtkWidget *child,
751 GtkWidget *before,
752 gpointer user_data)
754 if (before == NULL)
756 /* No separator before the first row */
757 g_clear_object (separator);
758 return;
761 if (*separator != NULL)
762 return;
764 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
765 g_object_ref_sink (*separator);
768 static gboolean
769 is_searching (EmpathyRosterView *self)
771 if (self->priv->search == NULL)
772 return FALSE;
774 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
777 static void
778 add_to_displayed (EmpathyRosterView *self,
779 EmpathyRosterContact *contact)
781 FolksIndividual *individual;
782 GHashTable *contacts;
783 GHashTableIter iter;
784 gpointer k;
786 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
787 return;
789 g_hash_table_add (self->priv->displayed_contacts, contact);
790 update_empty (self, FALSE);
792 /* Groups of this contact may now be displayed if we just displays the first
793 * child in this group. */
795 if (!self->priv->show_groups)
796 return;
798 individual = empathy_roster_contact_get_individual (contact);
799 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
800 if (contacts == NULL)
801 return;
803 g_hash_table_iter_init (&iter, contacts);
804 while (g_hash_table_iter_next (&iter, &k, NULL))
806 const gchar *group_name = k;
807 GtkWidget *group;
809 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
810 if (group == NULL)
811 continue;
813 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
817 static void
818 remove_from_displayed (EmpathyRosterView *self,
819 EmpathyRosterContact *contact)
821 g_hash_table_remove (self->priv->displayed_contacts, contact);
823 check_if_empty (self);
826 static gboolean
827 contact_is_favorite (EmpathyRosterContact *contact)
829 FolksIndividual *individual;
831 individual = empathy_roster_contact_get_individual (contact);
833 return folks_favourite_details_get_is_favourite (
834 FOLKS_FAVOURITE_DETAILS (individual));
838 * check if @contact should be displayed according to @self's current status
839 * and without consideration for the state of @contact's groups.
841 static gboolean
842 contact_should_be_displayed (EmpathyRosterView *self,
843 EmpathyRosterContact *contact)
845 if (is_searching (self))
847 FolksIndividual *individual;
849 individual = empathy_roster_contact_get_individual (contact);
851 return empathy_individual_match_string (individual,
852 empathy_live_search_get_text (self->priv->search),
853 empathy_live_search_get_words (self->priv->search));
856 if (self->priv->show_offline)
857 return TRUE;
859 if (contact_in_top (self, contact) &&
860 contact_is_favorite (contact))
861 /* Favorite top contacts are always displayed */
862 return TRUE;
864 return empathy_roster_contact_is_online (contact);
868 static gboolean
869 filter_contact (EmpathyRosterView *self,
870 EmpathyRosterContact *contact)
872 gboolean displayed;
874 displayed = contact_should_be_displayed (self, contact);
876 if (self->priv->show_groups)
878 const gchar *group_name;
879 EmpathyRosterGroup *group;
881 group_name = empathy_roster_contact_get_group (contact);
882 group = lookup_roster_group (self, group_name);
884 if (group != NULL)
886 /* When searching, always display even if the group is closed */
887 if (!is_searching (self) &&
888 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
889 displayed = FALSE;
893 if (displayed)
895 add_to_displayed (self, contact);
897 else
899 remove_from_displayed (self, contact);
902 return displayed;
905 static gboolean
906 filter_group (EmpathyRosterView *self,
907 EmpathyRosterGroup *group)
909 GList *widgets, *l;
910 gboolean result = FALSE;
912 /* Display the group if it contains at least one displayed contact */
913 widgets = empathy_roster_group_get_widgets (group);
914 for (l = widgets; l != NULL; l = g_list_next (l))
916 EmpathyRosterContact *contact = l->data;
918 if (contact_should_be_displayed (self, contact))
920 result = TRUE;
921 break;
925 g_list_free (widgets);
927 return result;
930 static gboolean
931 filter_list (GtkWidget *child,
932 gpointer user_data)
934 EmpathyRosterView *self = user_data;
936 if (EMPATHY_IS_ROSTER_CONTACT (child))
937 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
939 else if (EMPATHY_IS_ROSTER_GROUP (child))
940 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
942 g_return_val_if_reached (FALSE);
945 static void
946 populate_view (EmpathyRosterView *self)
948 GList *individuals, *l;
950 individuals = empathy_roster_model_get_individuals (self->priv->model);
951 for (l = individuals; l != NULL; l = g_list_next (l))
953 FolksIndividual *individual = l->data;
955 individual_added (self, individual);
958 g_list_free (individuals);
961 static void
962 remove_from_group (EmpathyRosterView *self,
963 FolksIndividual *individual,
964 const gchar *group)
966 GHashTable *contacts;
967 GtkWidget *contact;
968 EmpathyRosterGroup *roster_group;
970 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
971 if (contacts == NULL)
972 return;
974 contact = g_hash_table_lookup (contacts, group);
975 if (contact == NULL)
976 return;
978 g_hash_table_remove (contacts, group);
980 if (g_hash_table_size (contacts) == 0)
982 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
985 roster_group = lookup_roster_group (self, group);
987 if (roster_group != NULL)
989 update_group_widgets (self, roster_group,
990 EMPATHY_ROSTER_CONTACT (contact), FALSE);
993 gtk_container_remove (GTK_CONTAINER (self), contact);
996 static void
997 groups_changed_cb (EmpathyRosterModel *model,
998 FolksIndividual *individual,
999 const gchar *group,
1000 gboolean is_member,
1001 EmpathyRosterView *self)
1003 if (!self->priv->show_groups)
1005 egg_list_box_resort (EGG_LIST_BOX (self));
1006 return;
1009 if (is_member)
1011 add_to_group (self, individual, group);
1013 else
1015 remove_from_group (self, individual, group);
1019 static void
1020 empathy_roster_view_constructed (GObject *object)
1022 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1023 void (*chain_up) (GObject *) =
1024 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1026 if (chain_up != NULL)
1027 chain_up (object);
1029 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1031 /* Get saved group states. */
1032 empathy_contact_groups_get_all ();
1034 populate_view (self);
1036 tp_g_signal_connect_object (self->priv->model, "individual-added",
1037 G_CALLBACK (individual_added_cb), self, 0);
1038 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1039 G_CALLBACK (individual_removed_cb), self, 0);
1040 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1041 G_CALLBACK (groups_changed_cb), self, 0);
1043 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1044 roster_view_sort, self, NULL);
1046 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1047 self, NULL);
1049 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1051 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1054 static void
1055 clear_view (EmpathyRosterView *self)
1057 g_hash_table_remove_all (self->priv->roster_contacts);
1058 g_hash_table_remove_all (self->priv->roster_groups);
1059 g_hash_table_remove_all (self->priv->displayed_contacts);
1061 gtk_container_foreach (GTK_CONTAINER (self),
1062 (GtkCallback) gtk_widget_destroy, NULL);
1065 static void
1066 empathy_roster_view_dispose (GObject *object)
1068 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1069 void (*chain_up) (GObject *) =
1070 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1072 /* Start by clearing the view so our internal hash tables are cleared from
1073 * objects being destroyed. */
1074 clear_view (self);
1076 stop_flashing (self);
1078 empathy_roster_view_set_live_search (self, NULL);
1079 g_clear_object (&self->priv->model);
1081 if (chain_up != NULL)
1082 chain_up (object);
1085 static void
1086 empathy_roster_view_finalize (GObject *object)
1088 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1089 void (*chain_up) (GObject *) =
1090 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1092 g_hash_table_unref (self->priv->roster_contacts);
1093 g_hash_table_unref (self->priv->roster_groups);
1094 g_hash_table_unref (self->priv->displayed_contacts);
1095 g_queue_free_full (self->priv->events, event_free);
1097 if (chain_up != NULL)
1098 chain_up (object);
1101 static void
1102 empathy_roster_view_child_activated (EggListBox *box,
1103 GtkWidget *child)
1105 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1106 EmpathyRosterContact *contact;
1107 FolksIndividual *individual;
1108 GList *l;
1110 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1111 return;
1113 contact = EMPATHY_ROSTER_CONTACT (child);
1114 individual = empathy_roster_contact_get_individual (contact);
1116 /* Activate the oldest event associated with this contact, if any */
1117 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1118 l = g_list_previous (l))
1120 Event *event = l->data;
1122 if (event->individual == individual)
1124 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1125 event->user_data);
1126 return;
1130 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1133 static void
1134 fire_popup_individual_menu (EmpathyRosterView *self,
1135 GtkWidget *child,
1136 guint button,
1137 guint time)
1139 EmpathyRosterContact *contact;
1140 FolksIndividual *individual;
1142 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1143 return;
1145 contact = EMPATHY_ROSTER_CONTACT (child);
1146 individual = empathy_roster_contact_get_individual (contact);
1148 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1149 individual, button, time);
1152 static gboolean
1153 empathy_roster_view_button_press_event (GtkWidget *widget,
1154 GdkEventButton *event)
1156 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1157 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1158 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1160 if (event->button == 3)
1162 GtkWidget *child;
1164 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1166 if (child != NULL)
1168 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1170 fire_popup_individual_menu (self, child, event->button, event->time);
1174 return chain_up (widget, event);
1177 static gboolean
1178 empathy_roster_view_key_press_event (GtkWidget *widget,
1179 GdkEventKey *event)
1181 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1182 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1183 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1185 if (event->keyval == GDK_KEY_Menu)
1187 GtkWidget *child;
1189 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1191 if (child != NULL)
1192 fire_popup_individual_menu (self, child, 0, event->time);
1195 return chain_up (widget, event);
1199 * @out_child: (out) (allow-none)
1201 FolksIndividual *
1202 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1203 gint y,
1204 GtkWidget **out_child)
1206 GtkWidget *child;
1208 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1210 if (out_child != NULL)
1211 *out_child = child;
1213 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1214 return NULL;
1216 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
1220 * @out_child: (out) (allow-none)
1222 const gchar *
1223 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1224 gint y)
1226 GtkWidget *child;
1228 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1230 if (EMPATHY_IS_ROSTER_CONTACT (child))
1231 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child));
1232 else if (EMPATHY_IS_ROSTER_GROUP (child))
1233 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child));
1235 return NULL;
1238 static gboolean
1239 empathy_roster_view_query_tooltip (GtkWidget *widget,
1240 gint x,
1241 gint y,
1242 gboolean keyboard_mode,
1243 GtkTooltip *tooltip)
1245 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1246 FolksIndividual *individual;
1247 gboolean result;
1248 GtkWidget *child;
1250 individual = empathy_roster_view_get_individual_at_y (self, y, &child);
1251 if (individual == NULL)
1252 return FALSE;
1254 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1255 individual, keyboard_mode, tooltip, &result);
1257 if (result)
1259 GtkAllocation allocation;
1261 gtk_widget_get_allocation (child, &allocation);
1262 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1265 return result;
1268 static void
1269 empathy_roster_view_remove (GtkContainer *container,
1270 GtkWidget *widget)
1272 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1273 void (*chain_up) (GtkContainer *, GtkWidget *) =
1274 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1276 chain_up (container, widget);
1278 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1279 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1282 static void
1283 empathy_roster_view_class_init (
1284 EmpathyRosterViewClass *klass)
1286 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1287 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1288 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1289 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1290 GParamSpec *spec;
1292 oclass->get_property = empathy_roster_view_get_property;
1293 oclass->set_property = empathy_roster_view_set_property;
1294 oclass->constructed = empathy_roster_view_constructed;
1295 oclass->dispose = empathy_roster_view_dispose;
1296 oclass->finalize = empathy_roster_view_finalize;
1298 widget_class->button_press_event = empathy_roster_view_button_press_event;
1299 widget_class->key_press_event = empathy_roster_view_key_press_event;
1300 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1302 container_class->remove = empathy_roster_view_remove;
1304 box_class->child_activated = empathy_roster_view_child_activated;
1306 spec = g_param_spec_object ("model", "Model",
1307 "EmpathyRosterModel",
1308 EMPATHY_TYPE_ROSTER_MODEL,
1309 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1310 g_object_class_install_property (oclass, PROP_MODEL, spec);
1312 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1313 "Show offline contacts",
1314 FALSE,
1315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1316 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1318 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1319 "Show groups",
1320 FALSE,
1321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1322 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1324 spec = g_param_spec_boolean ("empty", "Empty",
1325 "Is the view currently empty?",
1326 FALSE,
1327 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1328 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1330 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1331 G_OBJECT_CLASS_TYPE (klass),
1332 G_SIGNAL_RUN_LAST,
1333 0, NULL, NULL, NULL,
1334 G_TYPE_NONE,
1335 1, FOLKS_TYPE_INDIVIDUAL);
1337 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1338 G_OBJECT_CLASS_TYPE (klass),
1339 G_SIGNAL_RUN_LAST,
1340 0, NULL, NULL, NULL,
1341 G_TYPE_NONE,
1342 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1344 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1345 G_OBJECT_CLASS_TYPE (klass),
1346 G_SIGNAL_RUN_LAST,
1347 0, NULL, NULL, NULL,
1348 G_TYPE_NONE,
1349 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1351 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1352 G_OBJECT_CLASS_TYPE (klass),
1353 G_SIGNAL_RUN_LAST,
1354 0, g_signal_accumulator_true_handled, NULL, NULL,
1355 G_TYPE_BOOLEAN,
1356 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1358 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1361 static void
1362 empathy_roster_view_init (EmpathyRosterView *self)
1364 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1365 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1367 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1368 NULL, (GDestroyNotify) g_hash_table_unref);
1369 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1370 g_free, NULL);
1371 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1373 self->priv->events = g_queue_new ();
1375 self->priv->empty = TRUE;
1378 GtkWidget *
1379 empathy_roster_view_new (EmpathyRosterModel *model)
1381 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1383 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1384 "model", model,
1385 NULL);
1388 void
1389 empathy_roster_view_show_offline (EmpathyRosterView *self,
1390 gboolean show)
1392 if (self->priv->show_offline == show)
1393 return;
1395 self->priv->show_offline = show;
1396 egg_list_box_refilter (EGG_LIST_BOX (self));
1398 g_object_notify (G_OBJECT (self), "show-offline");
1401 void
1402 empathy_roster_view_show_groups (EmpathyRosterView *self,
1403 gboolean show)
1405 if (self->priv->show_groups == show)
1406 return;
1408 self->priv->show_groups = show;
1410 /* TODO: block sort/filter? */
1411 clear_view (self);
1412 populate_view (self);
1414 g_object_notify (G_OBJECT (self), "show-groups");
1417 static void
1418 select_first_contact (EmpathyRosterView *self)
1420 GList *children, *l;
1422 children = gtk_container_get_children (GTK_CONTAINER (self));
1423 for (l = children; l != NULL; l = g_list_next (l))
1425 GtkWidget *child = l->data;
1427 if (!gtk_widget_get_child_visible (child))
1428 continue;
1430 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1431 continue;
1433 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1434 break;
1437 g_list_free (children);
1440 static void
1441 search_text_notify_cb (EmpathyLiveSearch *search,
1442 GParamSpec *pspec,
1443 EmpathyRosterView *self)
1445 egg_list_box_refilter (EGG_LIST_BOX (self));
1447 select_first_contact (self);
1450 static void
1451 search_activate_cb (GtkWidget *search,
1452 EmpathyRosterView *self)
1454 EggListBox *box = EGG_LIST_BOX (self);
1455 GtkWidget *child;
1457 child = egg_list_box_get_selected_child (box);
1458 if (child == NULL)
1459 return;
1461 empathy_roster_view_child_activated (box, child);
1464 void
1465 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1466 EmpathyLiveSearch *search)
1468 if (self->priv->search != NULL)
1470 g_signal_handlers_disconnect_by_func (self->priv->search,
1471 search_text_notify_cb, self);
1472 g_signal_handlers_disconnect_by_func (self->priv->search,
1473 search_activate_cb, self);
1475 g_clear_object (&self->priv->search);
1478 if (search == NULL)
1479 return;
1481 self->priv->search = g_object_ref (search);
1483 g_signal_connect (self->priv->search, "notify::text",
1484 G_CALLBACK (search_text_notify_cb), self);
1485 g_signal_connect (self->priv->search, "activate",
1486 G_CALLBACK (search_activate_cb), self);
1489 gboolean
1490 empathy_roster_view_is_empty (EmpathyRosterView *self)
1492 return self->priv->empty;
1495 gboolean
1496 empathy_roster_view_is_searching (EmpathyRosterView *self)
1498 return (self->priv->search != NULL &&
1499 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1502 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1503 * Empathy's internals. */
1504 guint
1505 empathy_roster_view_add_event (EmpathyRosterView *self,
1506 FolksIndividual *individual,
1507 const gchar *icon,
1508 gpointer user_data)
1510 GHashTable *contacts;
1512 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1513 if (contacts == NULL)
1514 return 0;
1516 self->priv->last_event_id++;
1518 g_queue_push_head (self->priv->events,
1519 event_new (self->priv->last_event_id, individual, icon, user_data));
1521 start_flashing (self);
1523 return self->priv->last_event_id;
1526 void
1527 empathy_roster_view_remove_event (EmpathyRosterView *self,
1528 guint event_id)
1530 GList *l;
1532 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1533 l = g_list_next (l))
1535 Event *event = l->data;
1537 if (event->id == event_id)
1539 remove_event (self, event);
1540 return;
1545 FolksIndividual *
1546 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1548 GtkWidget *child;
1550 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1552 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1553 return NULL;
1555 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));