roster-view: add_to_displayed: get the proper EmpathyRosterGroup object
[empathy-mirror.git] / libempathy-gtk / empathy-roster-view.c
blob1e98efcfe253feac8a21a72d5bcc77d71df95321
1 #include "config.h"
3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy-gtk/empathy-roster-contact.h>
8 #include <libempathy-gtk/empathy-roster-group.h>
9 #include <libempathy-gtk/empathy-ui-utils.h>
11 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, EGG_TYPE_LIST_BOX)
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
16 enum
18 PROP_MANAGER = 1,
19 PROP_SHOW_OFFLINE,
20 PROP_SHOW_GROUPS,
21 PROP_EMPTY,
22 N_PROPS
25 enum
27 SIG_INDIVIDUAL_ACTIVATED,
28 SIG_POPUP_INDIVIDUAL_MENU,
29 SIG_EVENT_ACTIVATED,
30 SIG_INDIVIDUAL_TOOLTIP,
31 LAST_SIGNAL
34 static guint signals[LAST_SIGNAL];
36 #define NO_GROUP "X-no-group"
37 #define UNGROUPED _("Ungrouped")
38 #define TOP_GROUP _("Top Contacts")
40 struct _EmpathyRosterViewPriv
42 EmpathyIndividualManager *manager;
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;
71 typedef struct
73 guint id;
74 FolksIndividual *individual;
75 gchar *icon;
76 gpointer user_data;
77 } Event;
79 static Event *
80 event_new (guint id,
81 FolksIndividual *individual,
82 const gchar *icon,
83 gpointer user_data)
85 Event *event = g_slice_new (Event);
87 event->id = id;
88 event->individual = g_object_ref (individual);
89 event->icon = g_strdup (icon);
90 event->user_data = user_data;
91 return event;
94 static void
95 event_free (gpointer data)
97 Event *event = data;
98 g_object_unref (event->individual);
99 g_free (event->icon);
101 g_slice_free (Event, event);
104 static void
105 empathy_roster_view_get_property (GObject *object,
106 guint property_id,
107 GValue *value,
108 GParamSpec *pspec)
110 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
112 switch (property_id)
114 case PROP_MANAGER:
115 g_value_set_object (value, self->priv->manager);
116 break;
117 case PROP_SHOW_OFFLINE:
118 g_value_set_boolean (value, self->priv->show_offline);
119 break;
120 case PROP_SHOW_GROUPS:
121 g_value_set_boolean (value, self->priv->show_groups);
122 break;
123 case PROP_EMPTY:
124 g_value_set_boolean (value, self->priv->empty);
125 break;
126 default:
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
128 break;
132 static void
133 empathy_roster_view_set_property (GObject *object,
134 guint property_id,
135 const GValue *value,
136 GParamSpec *pspec)
138 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
140 switch (property_id)
142 case PROP_MANAGER:
143 g_assert (self->priv->manager == NULL); /* construct only */
144 self->priv->manager = g_value_dup_object (value);
145 break;
146 case PROP_SHOW_OFFLINE:
147 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
148 break;
149 case PROP_SHOW_GROUPS:
150 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
151 break;
152 default:
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
154 break;
158 static void
159 roster_contact_changed_cb (GtkWidget *child,
160 GParamSpec *spec,
161 EmpathyRosterView *self)
163 egg_list_box_child_changed (EGG_LIST_BOX (self), child);
166 static GtkWidget *
167 add_roster_contact (EmpathyRosterView *self,
168 FolksIndividual *individual,
169 const gchar *group)
171 GtkWidget *contact;
173 contact = empathy_roster_contact_new (individual, group);
175 /* Need to refilter if online is changed */
176 g_signal_connect (contact, "notify::online",
177 G_CALLBACK (roster_contact_changed_cb), self);
179 /* Need to resort if alias is changed */
180 g_signal_connect (contact, "notify::alias",
181 G_CALLBACK (roster_contact_changed_cb), self);
183 gtk_widget_show (contact);
184 gtk_container_add (GTK_CONTAINER (self), contact);
186 return contact;
189 static void
190 group_expanded_cb (EmpathyRosterGroup *group,
191 GParamSpec *spec,
192 EmpathyRosterView *self)
194 GList *widgets, *l;
196 widgets = empathy_roster_group_get_widgets (group);
197 for (l = widgets; l != NULL; l = g_list_next (l))
199 egg_list_box_child_changed (EGG_LIST_BOX (self), l->data);
202 g_list_free (widgets);
205 static EmpathyRosterGroup *
206 lookup_roster_group (EmpathyRosterView *self,
207 const gchar *group)
209 return g_hash_table_lookup (self->priv->roster_groups, group);
212 static EmpathyRosterGroup *
213 ensure_roster_group (EmpathyRosterView *self,
214 const gchar *group)
216 GtkWidget *roster_group;
218 roster_group = (GtkWidget *) lookup_roster_group (self, group);
219 if (roster_group != NULL)
220 return EMPATHY_ROSTER_GROUP (roster_group);
222 if (!tp_strdiff (group, TOP_GROUP))
223 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
224 else
225 roster_group = empathy_roster_group_new (group, NULL);
227 g_signal_connect (roster_group, "notify::expanded",
228 G_CALLBACK (group_expanded_cb), self);
230 gtk_widget_show (roster_group);
231 gtk_container_add (GTK_CONTAINER (self), roster_group);
233 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
234 roster_group);
236 return EMPATHY_ROSTER_GROUP (roster_group);
239 static void
240 update_group_widgets (EmpathyRosterView *self,
241 EmpathyRosterGroup *group,
242 EmpathyRosterContact *contact,
243 gboolean add)
245 guint old_count, count;
247 old_count = empathy_roster_group_get_widgets_count (group);
249 if (add)
250 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
251 else
252 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
254 if (count != old_count)
255 egg_list_box_child_changed (EGG_LIST_BOX (self), GTK_WIDGET (group));
258 static void
259 add_to_group (EmpathyRosterView *self,
260 FolksIndividual *individual,
261 const gchar *group)
263 GtkWidget *contact;
264 GHashTable *contacts;
265 EmpathyRosterGroup *roster_group = NULL;
267 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
268 if (contacts == NULL)
269 return;
271 if (tp_strdiff (group, NO_GROUP))
272 roster_group = ensure_roster_group (self, group);
274 contact = add_roster_contact (self, individual, group);
275 g_hash_table_insert (contacts, g_strdup (group), contact);
277 if (roster_group != NULL)
279 update_group_widgets (self, roster_group,
280 EMPATHY_ROSTER_CONTACT (contact), TRUE);
284 static void
285 individual_added (EmpathyRosterView *self,
286 FolksIndividual *individual)
288 GHashTable *contacts;
290 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
291 if (contacts != NULL)
292 return;
294 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
296 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
298 if (!self->priv->show_groups)
300 add_to_group (self, individual, NO_GROUP);
302 else
304 GeeSet *groups;
305 GList *tops;
307 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
309 if (folks_favourite_details_get_is_favourite (
310 FOLKS_FAVOURITE_DETAILS (individual)) ||
311 g_list_index (tops, individual) != -1)
313 add_to_group (self, individual, TOP_GROUP);
316 groups = folks_group_details_get_groups (
317 FOLKS_GROUP_DETAILS (individual));
319 if (gee_collection_get_size (GEE_COLLECTION (groups)) > 0)
321 GeeIterator *iter = gee_iterable_iterator (GEE_ITERABLE (groups));
323 while (iter != NULL && gee_iterator_next (iter))
325 gchar *group = gee_iterator_get (iter);
327 add_to_group (self, individual, group);
329 g_free (group);
332 g_clear_object (&iter);
334 else
336 /* No group, adds to Ungrouped */
337 add_to_group (self, individual, UNGROUPED);
342 static void
343 set_event_icon_on_individual (EmpathyRosterView *self,
344 FolksIndividual *individual,
345 const gchar *icon)
347 GHashTable *contacts;
348 GHashTableIter iter;
349 gpointer v;
351 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
352 if (contacts == NULL)
353 return;
355 g_hash_table_iter_init (&iter, contacts);
356 while (g_hash_table_iter_next (&iter, NULL, &v))
358 EmpathyRosterContact *contact =v;
360 empathy_roster_contact_set_event_icon (contact, icon);
364 static void
365 flash_event (Event *event,
366 EmpathyRosterView *self)
368 set_event_icon_on_individual (self, event->individual, event->icon);
371 static void
372 unflash_event (Event *event,
373 EmpathyRosterView *self)
375 set_event_icon_on_individual (self, event->individual, NULL);
378 static gboolean
379 flash_cb (gpointer data)
381 EmpathyRosterView *self = data;
383 if (self->priv->display_flash_event)
385 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
386 self->priv->display_flash_event = FALSE;
388 else
390 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
391 self->priv->display_flash_event = TRUE;
394 return TRUE;
397 static void
398 start_flashing (EmpathyRosterView *self)
400 if (self->priv->flash_id != 0)
401 return;
403 self->priv->display_flash_event = TRUE;
405 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
406 flash_cb, self);
409 static void
410 stop_flashing (EmpathyRosterView *self)
412 if (self->priv->flash_id == 0)
413 return;
415 g_source_remove (self->priv->flash_id);
416 self->priv->flash_id = 0;
419 static void
420 remove_event (EmpathyRosterView *self,
421 Event *event)
423 unflash_event (event, self);
424 g_queue_remove (self->priv->events, event);
426 if (g_queue_get_length (self->priv->events) == 0)
428 stop_flashing (self);
432 static void
433 remove_all_individual_event (EmpathyRosterView *self,
434 FolksIndividual *individual)
436 GList *l;
438 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
439 l = g_list_next (l))
441 Event *event = l->data;
443 if (event->individual == individual)
445 remove_event (self, event);
446 return;
451 static void
452 individual_removed (EmpathyRosterView *self,
453 FolksIndividual *individual)
455 GHashTable *contacts;
456 GHashTableIter iter;
457 gpointer key, value;
459 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
460 if (contacts == NULL)
461 return;
463 remove_all_individual_event (self, individual);
465 g_hash_table_iter_init (&iter, contacts);
466 while (g_hash_table_iter_next (&iter, &key, &value))
468 const gchar *group_name = key;
469 GtkWidget *contact = value;
470 EmpathyRosterGroup *group;
472 group = lookup_roster_group (self, group_name);
473 if (group != NULL)
475 update_group_widgets (self, group,
476 EMPATHY_ROSTER_CONTACT (contact), FALSE);
479 gtk_container_remove (GTK_CONTAINER (self), contact);
482 g_hash_table_remove (self->priv->roster_contacts, individual);
485 static void
486 members_changed_cb (EmpathyIndividualManager *manager,
487 const gchar *message,
488 GList *added,
489 GList *removed,
490 TpChannelGroupChangeReason reason,
491 EmpathyRosterView *self)
493 GList *l;
495 for (l = added; l != NULL; l = g_list_next (l))
497 FolksIndividual *individual = l->data;
499 individual_added (self, individual);
502 for (l = removed; l != NULL; l = g_list_next (l))
504 FolksIndividual *individual = l->data;
506 individual_removed (self, individual);
510 static gint
511 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
512 EmpathyRosterContact *b)
514 FolksIndividual *ind_a, *ind_b;
515 const gchar *alias_a, *alias_b;
517 ind_a = empathy_roster_contact_get_individual (a);
518 ind_b = empathy_roster_contact_get_individual (b);
520 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
521 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
523 return g_ascii_strcasecmp (alias_a, alias_b);
526 static gboolean
527 contact_is_favourite (EmpathyRosterContact *contact)
529 FolksIndividual *individual;
531 individual = empathy_roster_contact_get_individual (contact);
533 return folks_favourite_details_get_is_favourite (
534 FOLKS_FAVOURITE_DETAILS (individual));
537 static gboolean
538 contact_in_top (EmpathyRosterView *self,
539 EmpathyRosterContact *contact)
541 FolksIndividual *individual;
542 GList *tops;
544 if (contact_is_favourite (contact))
545 return TRUE;
547 individual = empathy_roster_contact_get_individual (contact);
549 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
551 if (g_list_index (tops, individual) != -1)
552 return TRUE;
554 return FALSE;
557 static gint
558 compare_roster_contacts_no_group (EmpathyRosterView *self,
559 EmpathyRosterContact *a,
560 EmpathyRosterContact *b)
562 gboolean top_a, top_b;
564 top_a = contact_in_top (self, a);
565 top_b = contact_in_top (self, b);
567 if (top_a == top_b)
568 /* Both contacts are in the top of the roster (or not). Sort them
569 * alphabetically */
570 return compare_roster_contacts_by_alias (a, b);
571 else if (top_a)
572 return -1;
573 else
574 return 1;
577 static gint
578 compare_group_names (const gchar *group_a,
579 const gchar *group_b)
581 if (!tp_strdiff (group_a, TOP_GROUP))
582 return -1;
584 if (!tp_strdiff (group_b, TOP_GROUP))
585 return 1;
587 return g_ascii_strcasecmp (group_a, group_b);
590 static gint
591 compare_roster_contacts_with_groups (EmpathyRosterView *self,
592 EmpathyRosterContact *a,
593 EmpathyRosterContact *b)
595 const gchar *group_a, *group_b;
597 group_a = empathy_roster_contact_get_group (a);
598 group_b = empathy_roster_contact_get_group (b);
600 if (!tp_strdiff (group_a, group_b))
601 /* Same group, compare the contacts */
602 return compare_roster_contacts_by_alias (a, b);
604 /* Sort by group */
605 return compare_group_names (group_a, group_b);
608 static gint
609 compare_roster_contacts (EmpathyRosterView *self,
610 EmpathyRosterContact *a,
611 EmpathyRosterContact *b)
613 if (!self->priv->show_groups)
614 return compare_roster_contacts_no_group (self, a, b);
615 else
616 return compare_roster_contacts_with_groups (self, a, b);
619 static gint
620 compare_roster_groups (EmpathyRosterGroup *a,
621 EmpathyRosterGroup *b)
623 const gchar *name_a, *name_b;
625 name_a = empathy_roster_group_get_name (a);
626 name_b = empathy_roster_group_get_name (b);
628 return compare_group_names (name_a, name_b);
631 static gint
632 compare_contact_group (EmpathyRosterContact *contact,
633 EmpathyRosterGroup *group)
635 const char *contact_group, *group_name;
637 contact_group = empathy_roster_contact_get_group (contact);
638 group_name = empathy_roster_group_get_name (group);
640 if (!tp_strdiff (contact_group, group_name))
641 /* @contact is in @group, @group has to be displayed first */
642 return 1;
644 /* @contact is in a different group, sort by group name */
645 return compare_group_names (contact_group, group_name);
648 static gint
649 roster_view_sort (gconstpointer a,
650 gconstpointer b,
651 gpointer user_data)
653 EmpathyRosterView *self = user_data;
655 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
656 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
657 EMPATHY_ROSTER_CONTACT (b));
658 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
659 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
660 EMPATHY_ROSTER_GROUP (b));
661 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
662 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
663 EMPATHY_ROSTER_GROUP (b));
664 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
665 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
666 EMPATHY_ROSTER_GROUP (a));
668 g_return_val_if_reached (0);
671 static void
672 update_separator (GtkWidget **separator,
673 GtkWidget *child,
674 GtkWidget *before,
675 gpointer user_data)
677 if (before == NULL)
679 /* No separator before the first row */
680 g_clear_object (separator);
681 return;
684 if (*separator != NULL)
685 return;
687 *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
688 g_object_ref_sink (*separator);
691 static gboolean
692 is_searching (EmpathyRosterView *self)
694 if (self->priv->search == NULL)
695 return FALSE;
697 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
700 static void
701 update_empty (EmpathyRosterView *self,
702 gboolean empty)
704 if (self->priv->empty == empty)
705 return;
707 self->priv->empty = empty;
708 g_object_notify (G_OBJECT (self), "empty");
711 static void
712 add_to_displayed (EmpathyRosterView *self,
713 EmpathyRosterContact *contact)
715 FolksIndividual *individual;
716 GHashTable *contacts;
717 GHashTableIter iter;
718 gpointer k;
720 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
721 return;
723 g_hash_table_add (self->priv->displayed_contacts, contact);
724 update_empty (self, FALSE);
726 /* Groups of this contact may now be displayed if we just displays the first
727 * child in this group. */
728 individual = empathy_roster_contact_get_individual (contact);
729 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
730 if (contacts == NULL)
731 return;
733 g_hash_table_iter_init (&iter, contacts);
734 while (g_hash_table_iter_next (&iter, &k, NULL))
736 const gchar *group_name = k;
737 GtkWidget *group;
739 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
740 if (group == NULL)
741 continue;
743 egg_list_box_child_changed (EGG_LIST_BOX (self), group);
747 static void
748 remove_from_displayed (EmpathyRosterView *self,
749 EmpathyRosterContact *contact)
751 g_hash_table_remove (self->priv->displayed_contacts, contact);
753 if (g_hash_table_size (self->priv->displayed_contacts) == 0)
754 update_empty (self, TRUE);
758 * check if @contact should be displayed according to @self's current status
759 * and without consideration for the state of @contact's groups.
761 static gboolean
762 contact_should_be_displayed (EmpathyRosterView *self,
763 EmpathyRosterContact *contact)
765 if (is_searching (self))
767 FolksIndividual *individual;
769 individual = empathy_roster_contact_get_individual (contact);
771 return empathy_individual_match_string (individual,
772 empathy_live_search_get_text (self->priv->search),
773 empathy_live_search_get_words (self->priv->search));
775 else
777 if (self->priv->show_offline)
779 return TRUE;
781 else if (!self->priv->show_groups &&
782 contact_is_favourite (contact))
784 /* Always display favourite contacts in non-group mode. In the group
785 * mode we'll display only the one in the 'top' section. */
786 return TRUE;
788 else
790 return empathy_roster_contact_is_online (contact);
795 static gboolean
796 filter_contact (EmpathyRosterView *self,
797 EmpathyRosterContact *contact)
799 gboolean displayed;
801 displayed = contact_should_be_displayed (self, contact);
803 if (self->priv->show_groups)
805 const gchar *group_name;
806 EmpathyRosterGroup *group;
808 group_name = empathy_roster_contact_get_group (contact);
809 group = lookup_roster_group (self, group_name);
811 if (!tp_strdiff (group_name, TOP_GROUP) &&
812 contact_is_favourite (contact))
813 displayed = TRUE;
815 if (group != NULL)
817 /* When searching, always display even if the group is closed */
818 if (!is_searching (self) &&
819 !gtk_expander_get_expanded (GTK_EXPANDER (group)))
820 displayed = FALSE;
824 if (displayed)
826 add_to_displayed (self, contact);
828 else
830 remove_from_displayed (self, contact);
833 return displayed;
836 static gboolean
837 filter_group (EmpathyRosterView *self,
838 EmpathyRosterGroup *group)
840 GList *widgets, *l;
842 /* Display the group if it contains at least one displayed contact */
843 widgets = empathy_roster_group_get_widgets (group);
844 for (l = widgets; l != NULL; l = g_list_next (l))
846 EmpathyRosterContact *contact = l->data;
848 if (contact_should_be_displayed (self, contact))
849 return TRUE;
852 return FALSE;
855 static gboolean
856 filter_list (GtkWidget *child,
857 gpointer user_data)
859 EmpathyRosterView *self = user_data;
861 if (EMPATHY_IS_ROSTER_CONTACT (child))
862 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
864 else if (EMPATHY_IS_ROSTER_GROUP (child))
865 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
867 g_return_val_if_reached (FALSE);
870 /* @list: GList of EmpathyRosterContact
872 * Returns: %TRUE if @list contains an EmpathyRosterContact associated with
873 * @individual */
874 static gboolean
875 individual_in_list (FolksIndividual *individual,
876 GList *list)
878 GList *l;
880 for (l = list; l != NULL; l = g_list_next (l))
882 EmpathyRosterContact *contact = l->data;
884 if (empathy_roster_contact_get_individual (contact) == individual)
885 return TRUE;
888 return FALSE;
891 static void
892 populate_view (EmpathyRosterView *self)
894 GList *individuals, *l;
896 individuals = empathy_individual_manager_get_members (self->priv->manager);
897 for (l = individuals; l != NULL; l = g_list_next (l))
899 FolksIndividual *individual = l->data;
901 individual_added (self, individual);
904 g_list_free (individuals);
907 static void
908 remove_from_group (EmpathyRosterView *self,
909 FolksIndividual *individual,
910 const gchar *group)
912 GHashTable *contacts;
913 GtkWidget *contact;
914 EmpathyRosterGroup *roster_group;
916 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
917 if (contacts == NULL)
918 return;
920 contact = g_hash_table_lookup (contacts, group);
921 if (contact == NULL)
922 return;
924 g_hash_table_remove (contacts, group);
926 if (g_hash_table_size (contacts) == 0)
928 add_to_group (self, individual, UNGROUPED);
931 roster_group = lookup_roster_group (self, group);
933 if (roster_group != NULL)
935 update_group_widgets (self, roster_group,
936 EMPATHY_ROSTER_CONTACT (contact), FALSE);
939 gtk_container_remove (GTK_CONTAINER (self), contact);
942 static void
943 update_top_contacts (EmpathyRosterView *self)
945 GList *tops, *l;
946 GList *to_add = NULL, *to_remove = NULL;
947 EmpathyRosterGroup *group;
949 if (!self->priv->show_groups)
951 egg_list_box_resort (EGG_LIST_BOX (self));
952 return;
955 tops = empathy_individual_manager_get_top_individuals (self->priv->manager);
957 group = g_hash_table_lookup (self->priv->roster_groups, TOP_GROUP);
958 if (group == NULL)
960 to_add = g_list_copy (tops);
962 else
964 GList *contacts;
966 contacts = empathy_roster_group_get_widgets (group);
968 /* Check which EmpathyRosterContact have to be removed */
969 for (l = contacts; l != NULL; l = g_list_next (l))
971 EmpathyRosterContact *contact = l->data;
972 FolksIndividual *individual;
974 if (contact_is_favourite (contact))
975 continue;
977 individual = empathy_roster_contact_get_individual (contact);
979 if (g_list_find (tops, individual) == NULL)
980 to_remove = g_list_prepend (to_remove, individual);
983 /* Check which EmpathyRosterContact have to be added */
984 for (l = tops; l != NULL; l = g_list_next (l))
986 FolksIndividual *individual = l->data;
988 if (!individual_in_list (individual, contacts))
989 to_add = g_list_prepend (to_add, individual);
993 for (l = to_add; l != NULL; l = g_list_next (l))
994 add_to_group (self, l->data, TOP_GROUP);
996 for (l = to_remove; l != NULL; l = g_list_next (l))
997 remove_from_group (self, l->data, TOP_GROUP);
999 g_list_free (to_add);
1000 g_list_free (to_remove);
1003 static void
1004 groups_changed_cb (EmpathyIndividualManager *manager,
1005 FolksIndividual *individual,
1006 gchar *group,
1007 gboolean is_member,
1008 EmpathyRosterView *self)
1010 if (!self->priv->show_groups)
1011 return;
1013 if (is_member)
1015 add_to_group (self, individual, group);
1017 else
1019 remove_from_group (self, individual, group);
1023 static void
1024 top_individuals_changed_cb (EmpathyIndividualManager *manager,
1025 GParamSpec *spec,
1026 EmpathyRosterView *self)
1028 update_top_contacts (self);
1031 static void
1032 favourites_changed_cb (EmpathyIndividualManager *manager,
1033 FolksIndividual *individual,
1034 gboolean favourite,
1035 EmpathyRosterView *self)
1037 GHashTable *contacts;
1039 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1040 if (contacts == NULL)
1041 return;
1043 if (self->priv->show_groups)
1045 if (favourite)
1046 add_to_group (self, individual, TOP_GROUP);
1047 else
1048 remove_from_group (self, individual, TOP_GROUP);
1050 else
1052 GtkWidget *contact;
1054 contact = g_hash_table_lookup (contacts, NO_GROUP);
1055 if (contact == NULL)
1056 return;
1058 egg_list_box_child_changed (EGG_LIST_BOX (self), contact);
1062 static void
1063 empathy_roster_view_constructed (GObject *object)
1065 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1066 void (*chain_up) (GObject *) =
1067 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1069 if (chain_up != NULL)
1070 chain_up (object);
1072 g_assert (EMPATHY_IS_INDIVIDUAL_MANAGER (self->priv->manager));
1074 populate_view (self);
1076 tp_g_signal_connect_object (self->priv->manager, "members-changed",
1077 G_CALLBACK (members_changed_cb), self, 0);
1078 tp_g_signal_connect_object (self->priv->manager, "groups-changed",
1079 G_CALLBACK (groups_changed_cb), self, 0);
1080 tp_g_signal_connect_object (self->priv->manager, "notify::top-individuals",
1081 G_CALLBACK (top_individuals_changed_cb), self, 0);
1082 tp_g_signal_connect_object (self->priv->manager, "notify::favourites-changed",
1083 G_CALLBACK (favourites_changed_cb), self, 0);
1085 egg_list_box_set_sort_func (EGG_LIST_BOX (self),
1086 roster_view_sort, self, NULL);
1088 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self), update_separator,
1089 self, NULL);
1091 egg_list_box_set_filter_func (EGG_LIST_BOX (self), filter_list, self, NULL);
1093 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self), FALSE);
1096 static void
1097 empathy_roster_view_dispose (GObject *object)
1099 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1100 void (*chain_up) (GObject *) =
1101 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1103 stop_flashing (self);
1105 empathy_roster_view_set_live_search (self, NULL);
1106 g_clear_object (&self->priv->manager);
1108 if (chain_up != NULL)
1109 chain_up (object);
1112 static void
1113 empathy_roster_view_finalize (GObject *object)
1115 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1116 void (*chain_up) (GObject *) =
1117 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1119 g_hash_table_unref (self->priv->roster_contacts);
1120 g_hash_table_unref (self->priv->roster_groups);
1121 g_hash_table_unref (self->priv->displayed_contacts);
1122 g_queue_free_full (self->priv->events, event_free);
1124 if (chain_up != NULL)
1125 chain_up (object);
1128 static void
1129 empathy_roster_view_child_activated (EggListBox *box,
1130 GtkWidget *child)
1132 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1133 EmpathyRosterContact *contact;
1134 FolksIndividual *individual;
1135 GList *l;
1137 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1138 return;
1140 contact = EMPATHY_ROSTER_CONTACT (child);
1141 individual = empathy_roster_contact_get_individual (contact);
1143 /* Activate the oldest event associated with this contact, if any */
1144 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1145 l = g_list_previous (l))
1147 Event *event = l->data;
1149 if (event->individual == individual)
1151 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1152 event->user_data);
1153 return;
1157 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1160 static void
1161 fire_popup_individual_menu (EmpathyRosterView *self,
1162 GtkWidget *child,
1163 guint button,
1164 guint time)
1166 EmpathyRosterContact *contact;
1167 FolksIndividual *individual;
1169 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1170 return;
1172 contact = EMPATHY_ROSTER_CONTACT (child);
1173 individual = empathy_roster_contact_get_individual (contact);
1175 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1176 individual, button, time);
1179 static gboolean
1180 empathy_roster_view_button_press_event (GtkWidget *widget,
1181 GdkEventButton *event)
1183 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1184 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1185 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1187 if (event->button == 3)
1189 GtkWidget *child;
1191 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), event->y);
1193 if (child != NULL)
1195 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1197 fire_popup_individual_menu (self, child, event->button, event->time);
1201 return chain_up (widget, event);
1204 static gboolean
1205 empathy_roster_view_key_press_event (GtkWidget *widget,
1206 GdkEventKey *event)
1208 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1209 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1210 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1212 if (event->keyval == GDK_KEY_Menu)
1214 GtkWidget *child;
1216 child = egg_list_box_get_selected_child (EGG_LIST_BOX (self));
1218 if (child != NULL)
1219 fire_popup_individual_menu (self, child, 0, event->time);
1222 return chain_up (widget, event);
1225 static gboolean
1226 empathy_roster_view_query_tooltip (GtkWidget *widget,
1227 gint x,
1228 gint y,
1229 gboolean keyboard_mode,
1230 GtkTooltip *tooltip)
1232 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1233 GtkWidget *child;
1234 EmpathyRosterContact *contact;
1235 FolksIndividual *individual;
1236 gboolean result;
1238 child = egg_list_box_get_child_at_y (EGG_LIST_BOX (self), y);
1239 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1240 return FALSE;
1242 contact = EMPATHY_ROSTER_CONTACT (child);
1243 individual = empathy_roster_contact_get_individual (contact);
1245 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1246 individual, keyboard_mode, tooltip, &result);
1248 if (result)
1250 GtkAllocation allocation;
1252 gtk_widget_get_allocation (child, &allocation);
1253 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1256 return result;
1259 static void
1260 empathy_roster_view_remove (GtkContainer *container,
1261 GtkWidget *widget)
1263 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1264 void (*chain_up) (GtkContainer *, GtkWidget *) =
1265 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1267 chain_up (container, widget);
1269 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1270 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1273 static void
1274 empathy_roster_view_class_init (
1275 EmpathyRosterViewClass *klass)
1277 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1278 EggListBoxClass *box_class = EGG_LIST_BOX_CLASS (klass);
1279 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1280 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1281 GParamSpec *spec;
1283 oclass->get_property = empathy_roster_view_get_property;
1284 oclass->set_property = empathy_roster_view_set_property;
1285 oclass->constructed = empathy_roster_view_constructed;
1286 oclass->dispose = empathy_roster_view_dispose;
1287 oclass->finalize = empathy_roster_view_finalize;
1289 widget_class->button_press_event = empathy_roster_view_button_press_event;
1290 widget_class->key_press_event = empathy_roster_view_key_press_event;
1291 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1293 container_class->remove = empathy_roster_view_remove;
1295 box_class->child_activated = empathy_roster_view_child_activated;
1297 spec = g_param_spec_object ("manager", "Manager",
1298 "EmpathyIndividualManager",
1299 EMPATHY_TYPE_INDIVIDUAL_MANAGER,
1300 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1301 g_object_class_install_property (oclass, PROP_MANAGER, spec);
1303 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1304 "Show offline contacts",
1305 FALSE,
1306 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1307 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1309 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1310 "Show groups",
1311 FALSE,
1312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1313 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1315 spec = g_param_spec_boolean ("empty", "Empty",
1316 "Is the view currently empty?",
1317 FALSE,
1318 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1319 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1321 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1322 G_OBJECT_CLASS_TYPE (klass),
1323 G_SIGNAL_RUN_LAST,
1324 0, NULL, NULL, NULL,
1325 G_TYPE_NONE,
1326 1, FOLKS_TYPE_INDIVIDUAL);
1328 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1329 G_OBJECT_CLASS_TYPE (klass),
1330 G_SIGNAL_RUN_LAST,
1331 0, NULL, NULL, NULL,
1332 G_TYPE_NONE,
1333 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT, G_TYPE_UINT);
1335 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1336 G_OBJECT_CLASS_TYPE (klass),
1337 G_SIGNAL_RUN_LAST,
1338 0, NULL, NULL, NULL,
1339 G_TYPE_NONE,
1340 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1342 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1343 G_OBJECT_CLASS_TYPE (klass),
1344 G_SIGNAL_RUN_LAST,
1345 0, g_signal_accumulator_true_handled, NULL, NULL,
1346 G_TYPE_BOOLEAN,
1347 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1349 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1352 static void
1353 empathy_roster_view_init (EmpathyRosterView *self)
1355 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1356 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1358 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1359 NULL, (GDestroyNotify) g_hash_table_unref);
1360 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1361 g_free, NULL);
1362 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1364 self->priv->events = g_queue_new ();
1366 self->priv->empty = TRUE;
1369 GtkWidget *
1370 empathy_roster_view_new (EmpathyIndividualManager *manager)
1372 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (manager), NULL);
1374 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1375 "manager", manager,
1376 NULL);
1379 EmpathyIndividualManager *
1380 empathy_roster_view_get_manager (EmpathyRosterView *self)
1382 return self->priv->manager;
1385 void
1386 empathy_roster_view_show_offline (EmpathyRosterView *self,
1387 gboolean show)
1389 if (self->priv->show_offline == show)
1390 return;
1392 self->priv->show_offline = show;
1393 egg_list_box_refilter (EGG_LIST_BOX (self));
1395 g_object_notify (G_OBJECT (self), "show-offline");
1398 static void
1399 clear_view (EmpathyRosterView *self)
1401 gtk_container_foreach (GTK_CONTAINER (self),
1402 (GtkCallback) gtk_widget_destroy, NULL);
1404 g_hash_table_remove_all (self->priv->roster_contacts);
1405 g_hash_table_remove_all (self->priv->roster_groups);
1406 g_hash_table_remove_all (self->priv->displayed_contacts);
1409 void
1410 empathy_roster_view_show_groups (EmpathyRosterView *self,
1411 gboolean show)
1413 if (self->priv->show_groups == show)
1414 return;
1416 self->priv->show_groups = show;
1418 /* TODO: block sort/filter? */
1419 clear_view (self);
1420 populate_view (self);
1422 g_object_notify (G_OBJECT (self), "show-groups");
1425 static void
1426 select_first_contact (EmpathyRosterView *self)
1428 GList *children, *l;
1430 children = gtk_container_get_children (GTK_CONTAINER (self));
1431 for (l = children; l != NULL; l = g_list_next (l))
1433 GtkWidget *child = l->data;
1435 if (!gtk_widget_get_child_visible (child))
1436 continue;
1438 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1439 continue;
1441 egg_list_box_select_child (EGG_LIST_BOX (self), child);
1442 break;
1445 g_list_free (children);
1448 static void
1449 search_text_notify_cb (EmpathyLiveSearch *search,
1450 GParamSpec *pspec,
1451 EmpathyRosterView *self)
1453 egg_list_box_refilter (EGG_LIST_BOX (self));
1455 select_first_contact (self);
1458 static void
1459 search_activate_cb (GtkWidget *search,
1460 EmpathyRosterView *self)
1462 EggListBox *box = EGG_LIST_BOX (self);
1463 GtkWidget *child;
1465 child = egg_list_box_get_selected_child (box);
1466 if (child == NULL)
1467 return;
1469 empathy_roster_view_child_activated (box, child);
1472 void
1473 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1474 EmpathyLiveSearch *search)
1476 if (self->priv->search != NULL)
1478 g_signal_handlers_disconnect_by_func (self->priv->search,
1479 search_text_notify_cb, self);
1480 g_signal_handlers_disconnect_by_func (self->priv->search,
1481 search_activate_cb, self);
1483 g_clear_object (&self->priv->search);
1486 if (search == NULL)
1487 return;
1489 self->priv->search = g_object_ref (search);
1491 g_signal_connect (self->priv->search, "notify::text",
1492 G_CALLBACK (search_text_notify_cb), self);
1493 g_signal_connect (self->priv->search, "activate",
1494 G_CALLBACK (search_activate_cb), self);
1497 gboolean
1498 empathy_roster_view_is_empty (EmpathyRosterView *self)
1500 return self->priv->empty;
1503 gboolean
1504 empathy_roster_view_is_searching (EmpathyRosterView *self)
1506 return (self->priv->search != NULL &&
1507 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1510 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1511 * Empathy's internals. */
1512 guint
1513 empathy_roster_view_add_event (EmpathyRosterView *self,
1514 FolksIndividual *individual,
1515 const gchar *icon,
1516 gpointer user_data)
1518 GHashTable *contacts;
1520 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1521 if (contacts == NULL)
1522 return 0;
1524 self->priv->last_event_id++;
1526 g_queue_push_head (self->priv->events,
1527 event_new (self->priv->last_event_id, individual, icon, user_data));
1529 start_flashing (self);
1531 return self->priv->last_event_id;
1534 void
1535 empathy_roster_view_remove_event (EmpathyRosterView *self,
1536 guint event_id)
1538 GList *l;
1540 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1541 l = g_list_next (l))
1543 Event *event = l->data;
1545 if (event->id == event_id)
1547 remove_event (self, event);
1548 return;