Updated Friulian translation
[empathy-mirror.git] / libempathy-gtk / empathy-roster-view.c
blob0dab3ba66d833a2c5f8cd3e4779da1942a44bdff
1 #include "config.h"
2 #include "empathy-roster-view.h"
4 #include <glib/gi18n-lib.h>
6 #include "empathy-contact-groups.h"
7 #include "empathy-roster-contact.h"
8 #include "empathy-roster-group.h"
9 #include "empathy-ui-utils.h"
11 G_DEFINE_TYPE (EmpathyRosterView, empathy_roster_view, GTK_TYPE_LIST_BOX)
13 /* Flashing delay for icons (milliseconds). */
14 #define FLASH_TIMEOUT 500
16 /* Delay in milliseconds between the last stroke on the keyboard and the start
17 * of the live search. */
18 #define SEARCH_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 guint search_id;
66 gboolean show_offline;
67 gboolean show_groups;
68 gboolean empty;
70 TpawLiveSearch *search;
72 EmpathyRosterModel *model;
75 /* Prototypes to break cycles */
76 static void remove_from_group (EmpathyRosterView *self,
77 FolksIndividual *individual,
78 const gchar *group);
80 typedef struct
82 guint id;
83 FolksIndividual *individual;
84 gchar *icon;
85 gpointer user_data;
86 } Event;
88 static Event *
89 event_new (guint id,
90 FolksIndividual *individual,
91 const gchar *icon,
92 gpointer user_data)
94 Event *event = g_slice_new (Event);
96 event->id = id;
97 event->individual = g_object_ref (individual);
98 event->icon = g_strdup (icon);
99 event->user_data = user_data;
100 return event;
103 static void
104 event_free (gpointer data)
106 Event *event = data;
107 g_object_unref (event->individual);
108 g_free (event->icon);
110 g_slice_free (Event, event);
113 static void
114 empathy_roster_view_get_property (GObject *object,
115 guint property_id,
116 GValue *value,
117 GParamSpec *pspec)
119 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
121 switch (property_id)
123 case PROP_MODEL:
124 g_value_set_object (value, self->priv->model);
125 break;
126 case PROP_SHOW_OFFLINE:
127 g_value_set_boolean (value, self->priv->show_offline);
128 break;
129 case PROP_SHOW_GROUPS:
130 g_value_set_boolean (value, self->priv->show_groups);
131 break;
132 case PROP_EMPTY:
133 g_value_set_boolean (value, self->priv->empty);
134 break;
135 default:
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
137 break;
141 static void
142 empathy_roster_view_set_property (GObject *object,
143 guint property_id,
144 const GValue *value,
145 GParamSpec *pspec)
147 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
149 switch (property_id)
151 case PROP_MODEL:
152 g_assert (self->priv->model == NULL);
153 self->priv->model = g_value_dup_object (value);
154 break;
155 case PROP_SHOW_OFFLINE:
156 empathy_roster_view_show_offline (self, g_value_get_boolean (value));
157 break;
158 case PROP_SHOW_GROUPS:
159 empathy_roster_view_show_groups (self, g_value_get_boolean (value));
160 break;
161 default:
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 break;
167 static void
168 roster_contact_changed_cb (GtkListBoxRow *child,
169 GParamSpec *spec,
170 EmpathyRosterView *self)
172 gtk_list_box_row_changed (child);
175 static GtkWidget *
176 add_roster_contact (EmpathyRosterView *self,
177 FolksIndividual *individual,
178 const gchar *group)
180 GtkWidget *contact;
182 contact = empathy_roster_contact_new (individual, group);
184 /* Need to refilter if online is changed */
185 g_signal_connect (contact, "notify::online",
186 G_CALLBACK (roster_contact_changed_cb), self);
188 /* Need to resort if alias is changed */
189 g_signal_connect (contact, "notify::alias",
190 G_CALLBACK (roster_contact_changed_cb), self);
192 gtk_widget_show (contact);
193 gtk_container_add (GTK_CONTAINER (self), contact);
195 return contact;
198 static void
199 group_expanded_cb (GtkWidget *expander,
200 GParamSpec *spec,
201 EmpathyRosterGroup *group)
203 GList *widgets, *l;
205 widgets = empathy_roster_group_get_widgets (group);
206 for (l = widgets; l != NULL; l = g_list_next (l))
208 gtk_list_box_row_changed (l->data);
211 g_list_free (widgets);
213 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group),
214 gtk_expander_get_expanded (group->expander));
217 static EmpathyRosterGroup *
218 lookup_roster_group (EmpathyRosterView *self,
219 const gchar *group)
221 return g_hash_table_lookup (self->priv->roster_groups, group);
224 static EmpathyRosterGroup *
225 ensure_roster_group (EmpathyRosterView *self,
226 const gchar *group)
228 GtkWidget *roster_group;
230 roster_group = (GtkWidget *) lookup_roster_group (self, group);
231 if (roster_group != NULL)
232 return EMPATHY_ROSTER_GROUP (roster_group);
234 if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
235 roster_group = empathy_roster_group_new (group, "emblem-favorite-symbolic");
236 else if (!tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY))
237 roster_group = empathy_roster_group_new (group, "im-local-xmpp");
238 else
239 roster_group = empathy_roster_group_new (group, NULL);
241 gtk_expander_set_expanded (EMPATHY_ROSTER_GROUP (roster_group)->expander,
242 empathy_contact_group_get_expanded (group));
244 g_signal_connect (EMPATHY_ROSTER_GROUP (roster_group)->expander,
245 "notify::expanded", G_CALLBACK (group_expanded_cb), roster_group);
247 gtk_widget_show (roster_group);
248 gtk_container_add (GTK_CONTAINER (self), roster_group);
250 g_hash_table_insert (self->priv->roster_groups, g_strdup (group),
251 roster_group);
253 return EMPATHY_ROSTER_GROUP (roster_group);
256 static void
257 update_empty (EmpathyRosterView *self,
258 gboolean empty)
260 if (self->priv->empty == empty)
261 return;
263 self->priv->empty = empty;
264 g_object_notify (G_OBJECT (self), "empty");
267 static gboolean filter_group (EmpathyRosterView *self,
268 EmpathyRosterGroup *group);
270 static gboolean
271 at_least_one_group_displayed (EmpathyRosterView *self)
273 GHashTableIter iter;
274 gpointer v;
276 g_hash_table_iter_init (&iter, self->priv->roster_groups);
277 while (g_hash_table_iter_next (&iter, NULL, &v))
279 EmpathyRosterGroup *group = EMPATHY_ROSTER_GROUP (v);
281 if (filter_group (self, group))
282 return TRUE;
285 return FALSE;
288 static void
289 check_if_empty (EmpathyRosterView *self)
291 /* Roster is considered as empty if there is no contact *and* no group
292 * currently displayed. */
293 if (g_hash_table_size (self->priv->displayed_contacts) != 0 ||
294 at_least_one_group_displayed (self))
296 update_empty (self, FALSE);
297 return;
300 update_empty (self, TRUE);
303 static void
304 update_group_widgets (EmpathyRosterView *self,
305 EmpathyRosterGroup *group,
306 EmpathyRosterContact *contact,
307 gboolean add)
309 guint old_count, count;
311 old_count = empathy_roster_group_get_widgets_count (group);
313 if (add)
314 count = empathy_roster_group_add_widget (group, GTK_WIDGET (contact));
315 else
316 count = empathy_roster_group_remove_widget (group, GTK_WIDGET (contact));
318 if (count != old_count)
320 gtk_list_box_row_changed (GTK_LIST_BOX_ROW (group));
322 check_if_empty (self);
326 static void
327 add_to_group (EmpathyRosterView *self,
328 FolksIndividual *individual,
329 const gchar *group)
331 GtkWidget *contact;
332 GHashTable *contacts;
333 EmpathyRosterGroup *roster_group = NULL;
335 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
336 if (contacts == NULL)
337 return;
339 if (g_hash_table_lookup (contacts, group) != NULL)
340 return;
342 if (tp_strdiff (group, NO_GROUP))
343 roster_group = ensure_roster_group (self, group);
345 contact = add_roster_contact (self, individual, group);
346 g_hash_table_insert (contacts, g_strdup (group), contact);
348 if (roster_group != NULL)
350 update_group_widgets (self, roster_group,
351 EMPATHY_ROSTER_CONTACT (contact), TRUE);
354 if (tp_strdiff (group, NO_GROUP) &&
355 tp_strdiff (group, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED) &&
356 g_hash_table_size (contacts) == 2 /* 1:Ungrouped and 2:first group */)
358 remove_from_group (self, individual,
359 EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
363 static void
364 individual_favourite_change_cb (FolksIndividual *individual,
365 GParamSpec *spec,
366 EmpathyRosterView *self)
368 /* We may have to refilter the contact as only favorite contacts are always
369 * displayed regardless of their presence. */
370 GHashTable *contacts;
371 GtkWidget *contact;
373 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
374 if (contacts == NULL)
375 return;
377 if (self->priv->show_groups)
378 contact = g_hash_table_lookup (contacts,
379 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP);
380 else
381 contact = g_hash_table_lookup (contacts, NO_GROUP);
383 if (contact == NULL)
384 return;
386 gtk_list_box_row_changed (GTK_LIST_BOX_ROW (contact));
389 static void
390 individual_added (EmpathyRosterView *self,
391 FolksIndividual *individual)
393 GHashTable *contacts;
395 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
396 if (contacts != NULL)
397 return;
399 contacts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
401 g_hash_table_insert (self->priv->roster_contacts, individual, contacts);
403 if (!self->priv->show_groups)
405 add_to_group (self, individual, NO_GROUP);
407 else
409 GList *groups, *l;
411 groups = empathy_roster_model_dup_groups_for_individual (self->priv->model,
412 individual);
414 if (g_list_length (groups) > 0)
416 for (l = groups; l != NULL; l = g_list_next (l))
418 add_to_group (self, individual, l->data);
421 else
423 /* No group, adds to Ungrouped */
424 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
427 g_list_free_full (groups, g_free);
430 tp_g_signal_connect_object (individual, "notify::is-favourite",
431 G_CALLBACK (individual_favourite_change_cb), self, 0);
434 static void
435 set_event_icon_on_individual (EmpathyRosterView *self,
436 FolksIndividual *individual,
437 const gchar *icon)
439 GHashTable *contacts;
440 GHashTableIter iter;
441 gpointer v;
443 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
444 if (contacts == NULL)
445 return;
447 g_hash_table_iter_init (&iter, contacts);
448 while (g_hash_table_iter_next (&iter, NULL, &v))
450 EmpathyRosterContact *contact =v;
452 empathy_roster_contact_set_event_icon (contact, icon);
456 static void
457 flash_event (Event *event,
458 EmpathyRosterView *self)
460 set_event_icon_on_individual (self, event->individual, event->icon);
463 static void
464 unflash_event (Event *event,
465 EmpathyRosterView *self)
467 set_event_icon_on_individual (self, event->individual, NULL);
470 static gboolean
471 flash_cb (gpointer data)
473 EmpathyRosterView *self = data;
475 if (self->priv->display_flash_event)
477 g_queue_foreach (self->priv->events, (GFunc) flash_event, self);
478 self->priv->display_flash_event = FALSE;
480 else
482 g_queue_foreach (self->priv->events, (GFunc) unflash_event, self);
483 self->priv->display_flash_event = TRUE;
486 return TRUE;
489 static void
490 start_flashing (EmpathyRosterView *self)
492 if (self->priv->flash_id != 0)
493 return;
495 self->priv->display_flash_event = TRUE;
497 self->priv->flash_id = g_timeout_add (FLASH_TIMEOUT,
498 flash_cb, self);
501 static void
502 stop_flashing (EmpathyRosterView *self)
504 if (self->priv->flash_id == 0)
505 return;
507 g_source_remove (self->priv->flash_id);
508 self->priv->flash_id = 0;
511 static void
512 remove_event (EmpathyRosterView *self,
513 Event *event)
515 unflash_event (event, self);
516 g_queue_remove (self->priv->events, event);
518 if (g_queue_get_length (self->priv->events) == 0)
520 stop_flashing (self);
524 static void
525 remove_all_individual_event (EmpathyRosterView *self,
526 FolksIndividual *individual)
528 GList *l;
530 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
531 l = g_list_next (l))
533 Event *event = l->data;
535 if (event->individual == individual)
537 remove_event (self, event);
538 return;
543 static void
544 individual_removed (EmpathyRosterView *self,
545 FolksIndividual *individual)
547 GHashTable *contacts;
548 GHashTableIter iter;
549 gpointer key, value;
551 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
552 if (contacts == NULL)
553 return;
555 remove_all_individual_event (self, individual);
557 g_hash_table_iter_init (&iter, contacts);
558 while (g_hash_table_iter_next (&iter, &key, &value))
560 const gchar *group_name = key;
561 GtkWidget *contact = value;
562 EmpathyRosterGroup *group;
564 group = lookup_roster_group (self, group_name);
565 if (group != NULL)
567 update_group_widgets (self, group,
568 EMPATHY_ROSTER_CONTACT (contact), FALSE);
571 gtk_container_remove (GTK_CONTAINER (self), contact);
574 g_hash_table_remove (self->priv->roster_contacts, individual);
577 static void
578 individual_added_cb (EmpathyRosterModel *model,
579 FolksIndividual *individual,
580 EmpathyRosterView *self)
582 individual_added (self, individual);
585 static void
586 individual_removed_cb (EmpathyRosterModel *model,
587 FolksIndividual *individual,
588 EmpathyRosterView *self)
590 individual_removed (self, individual);
593 static gboolean
594 contact_in_top (EmpathyRosterView *self,
595 EmpathyRosterContact *contact)
597 if (!self->priv->show_groups)
599 /* Always display top contacts in non-group mode. */
600 GList *groups;
601 FolksIndividual *individual;
602 gboolean result = FALSE;
604 individual = empathy_roster_contact_get_individual (contact);
606 groups = empathy_roster_model_dup_groups_for_individual (
607 self->priv->model, individual);
609 if (g_list_find_custom (groups, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP,
610 (GCompareFunc) g_strcmp0) != NULL)
611 result = TRUE;
613 g_list_free_full (groups, g_free);
615 return result;
618 if (!tp_strdiff (empathy_roster_contact_get_group (contact),
619 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
620 /* If we are displaying contacts, we only want to *always* display the
621 * RosterContact which is displayed at the top; not the ones displayed in
622 * the 'normal' group sections */
623 return TRUE;
625 return FALSE;
628 static gint
629 compare_roster_contacts_by_alias (EmpathyRosterContact *a,
630 EmpathyRosterContact *b)
632 FolksIndividual *ind_a, *ind_b;
633 const gchar *alias_a, *alias_b;
635 ind_a = empathy_roster_contact_get_individual (a);
636 ind_b = empathy_roster_contact_get_individual (b);
638 alias_a = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a));
639 alias_b = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b));
641 return g_utf8_collate (alias_a, alias_b);
644 static gint
645 compare_roster_contacts_no_group (EmpathyRosterView *self,
646 EmpathyRosterContact *a,
647 EmpathyRosterContact *b)
649 gboolean top_a, top_b;
651 top_a = contact_in_top (self, a);
652 top_b = contact_in_top (self, b);
654 if (top_a == top_b)
655 /* Both contacts are in the top of the roster (or not). Sort them
656 * alphabetically */
657 return compare_roster_contacts_by_alias (a, b);
658 else if (top_a)
659 return -1;
660 else
661 return 1;
664 static gint
665 compare_group_names (const gchar *group_a,
666 const gchar *group_b)
668 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
669 return -1;
671 if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP))
672 return 1;
674 if (!tp_strdiff (group_a, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
675 return 1;
676 else if (!tp_strdiff (group_b, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED))
677 return -1;
679 return g_utf8_collate (group_a, group_b);
682 static gint
683 compare_roster_contacts_with_groups (EmpathyRosterView *self,
684 EmpathyRosterContact *a,
685 EmpathyRosterContact *b)
687 const gchar *group_a, *group_b;
689 group_a = empathy_roster_contact_get_group (a);
690 group_b = empathy_roster_contact_get_group (b);
692 if (!tp_strdiff (group_a, group_b))
693 /* Same group, compare the contacts */
694 return compare_roster_contacts_by_alias (a, b);
696 /* Sort by group */
697 return compare_group_names (group_a, group_b);
700 static gint
701 compare_roster_contacts (EmpathyRosterView *self,
702 EmpathyRosterContact *a,
703 EmpathyRosterContact *b)
705 if (!self->priv->show_groups)
706 return compare_roster_contacts_no_group (self, a, b);
707 else
708 return compare_roster_contacts_with_groups (self, a, b);
711 static gint
712 compare_roster_groups (EmpathyRosterGroup *a,
713 EmpathyRosterGroup *b)
715 const gchar *name_a, *name_b;
717 name_a = empathy_roster_group_get_name (a);
718 name_b = empathy_roster_group_get_name (b);
720 return compare_group_names (name_a, name_b);
723 static gint
724 compare_contact_group (EmpathyRosterContact *contact,
725 EmpathyRosterGroup *group)
727 const char *contact_group, *group_name;
729 contact_group = empathy_roster_contact_get_group (contact);
730 group_name = empathy_roster_group_get_name (group);
732 if (!tp_strdiff (contact_group, group_name))
733 /* @contact is in @group, @group has to be displayed first */
734 return 1;
736 /* @contact is in a different group, sort by group name */
737 return compare_group_names (contact_group, group_name);
740 static gint
741 roster_view_sort (GtkListBoxRow *a,
742 GtkListBoxRow *b,
743 gpointer user_data)
745 EmpathyRosterView *self = user_data;
747 if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_CONTACT (b))
748 return compare_roster_contacts (self, EMPATHY_ROSTER_CONTACT (a),
749 EMPATHY_ROSTER_CONTACT (b));
750 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_GROUP (b))
751 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a),
752 EMPATHY_ROSTER_GROUP (b));
753 else if (EMPATHY_IS_ROSTER_CONTACT (a) && EMPATHY_IS_ROSTER_GROUP (b))
754 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a),
755 EMPATHY_ROSTER_GROUP (b));
756 else if (EMPATHY_IS_ROSTER_GROUP (a) && EMPATHY_IS_ROSTER_CONTACT (b))
757 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b),
758 EMPATHY_ROSTER_GROUP (a));
760 g_return_val_if_reached (0);
763 static void
764 update_header (GtkListBoxRow *row,
765 GtkListBoxRow *before,
766 gpointer user_data)
768 if (before == NULL)
770 /* No separator before the first row */
771 gtk_list_box_row_set_header (row, NULL);
772 return;
775 if (gtk_list_box_row_get_header (row) != NULL)
776 return;
778 gtk_list_box_row_set_header (row,
779 gtk_separator_new (GTK_ORIENTATION_HORIZONTAL));
782 static gboolean
783 is_searching (EmpathyRosterView *self)
785 if (self->priv->search == NULL)
786 return FALSE;
788 return gtk_widget_get_visible (GTK_WIDGET (self->priv->search));
791 static void
792 add_to_displayed (EmpathyRosterView *self,
793 EmpathyRosterContact *contact)
795 FolksIndividual *individual;
796 GHashTable *contacts;
797 GHashTableIter iter;
798 gpointer k;
800 if (g_hash_table_lookup (self->priv->displayed_contacts, contact) != NULL)
801 return;
803 g_hash_table_add (self->priv->displayed_contacts, contact);
804 update_empty (self, FALSE);
806 /* Groups of this contact may now be displayed if we just displays the first
807 * child in this group. */
809 if (!self->priv->show_groups)
810 return;
812 individual = empathy_roster_contact_get_individual (contact);
813 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
814 if (contacts == NULL)
815 return;
817 g_hash_table_iter_init (&iter, contacts);
818 while (g_hash_table_iter_next (&iter, &k, NULL))
820 const gchar *group_name = k;
821 GtkListBoxRow *group;
823 group = g_hash_table_lookup (self->priv->roster_groups, group_name);
824 if (group == NULL)
825 continue;
827 gtk_list_box_row_changed (group);
831 static void
832 remove_from_displayed (EmpathyRosterView *self,
833 EmpathyRosterContact *contact)
835 g_hash_table_remove (self->priv->displayed_contacts, contact);
837 check_if_empty (self);
840 static gboolean
841 contact_is_favorite (EmpathyRosterContact *contact)
843 FolksIndividual *individual;
845 individual = empathy_roster_contact_get_individual (contact);
847 return folks_favourite_details_get_is_favourite (
848 FOLKS_FAVOURITE_DETAILS (individual));
852 * check if @contact should be displayed according to @self's current status
853 * and without consideration for the state of @contact's groups.
855 static gboolean
856 contact_should_be_displayed (EmpathyRosterView *self,
857 EmpathyRosterContact *contact)
859 if (is_searching (self))
861 FolksIndividual *individual;
863 individual = empathy_roster_contact_get_individual (contact);
865 return empathy_individual_match_string (individual,
866 tpaw_live_search_get_text (self->priv->search),
867 tpaw_live_search_get_words (self->priv->search));
870 if (self->priv->show_offline)
871 return TRUE;
873 if (contact_in_top (self, contact) &&
874 contact_is_favorite (contact))
875 /* Favorite top contacts are always displayed */
876 return TRUE;
878 return empathy_roster_contact_is_online (contact);
882 static gboolean
883 filter_contact (EmpathyRosterView *self,
884 EmpathyRosterContact *contact)
886 gboolean displayed;
888 displayed = contact_should_be_displayed (self, contact);
890 if (self->priv->show_groups)
892 const gchar *group_name;
893 EmpathyRosterGroup *group;
895 group_name = empathy_roster_contact_get_group (contact);
896 group = lookup_roster_group (self, group_name);
898 if (group != NULL)
900 /* When searching, always display even if the group is closed */
901 if (!is_searching (self) &&
902 !gtk_expander_get_expanded (group->expander))
903 displayed = FALSE;
907 if (displayed)
909 add_to_displayed (self, contact);
911 else
913 remove_from_displayed (self, contact);
916 return displayed;
919 static gboolean
920 filter_group (EmpathyRosterView *self,
921 EmpathyRosterGroup *group)
923 GList *widgets, *l;
924 gboolean result = FALSE;
926 /* Display the group if it contains at least one displayed contact */
927 widgets = empathy_roster_group_get_widgets (group);
928 for (l = widgets; l != NULL; l = g_list_next (l))
930 EmpathyRosterContact *contact = l->data;
932 if (contact_should_be_displayed (self, contact))
934 result = TRUE;
935 break;
939 g_list_free (widgets);
941 return result;
944 static gboolean
945 filter_list (GtkListBoxRow *child,
946 gpointer user_data)
948 EmpathyRosterView *self = user_data;
950 if (EMPATHY_IS_ROSTER_CONTACT (child))
951 return filter_contact (self, EMPATHY_ROSTER_CONTACT (child));
953 else if (EMPATHY_IS_ROSTER_GROUP (child))
954 return filter_group (self, EMPATHY_ROSTER_GROUP (child));
956 g_return_val_if_reached (FALSE);
959 static void
960 populate_view (EmpathyRosterView *self)
962 GList *individuals, *l;
964 individuals = empathy_roster_model_get_individuals (self->priv->model);
965 for (l = individuals; l != NULL; l = g_list_next (l))
967 FolksIndividual *individual = l->data;
969 individual_added (self, individual);
972 g_list_free (individuals);
975 static void
976 remove_from_group (EmpathyRosterView *self,
977 FolksIndividual *individual,
978 const gchar *group)
980 GHashTable *contacts;
981 GtkWidget *contact;
982 EmpathyRosterGroup *roster_group;
984 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
985 if (contacts == NULL)
986 return;
988 contact = g_hash_table_lookup (contacts, group);
989 if (contact == NULL)
990 return;
992 g_hash_table_remove (contacts, group);
994 if (g_hash_table_size (contacts) == 0)
996 add_to_group (self, individual, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED);
999 roster_group = lookup_roster_group (self, group);
1001 if (roster_group != NULL)
1003 update_group_widgets (self, roster_group,
1004 EMPATHY_ROSTER_CONTACT (contact), FALSE);
1007 gtk_container_remove (GTK_CONTAINER (self), contact);
1010 static void
1011 groups_changed_cb (EmpathyRosterModel *model,
1012 FolksIndividual *individual,
1013 const gchar *group,
1014 gboolean is_member,
1015 EmpathyRosterView *self)
1017 if (!self->priv->show_groups)
1019 gtk_list_box_invalidate_sort (GTK_LIST_BOX (self));
1020 return;
1023 if (is_member)
1025 add_to_group (self, individual, group);
1027 else
1029 remove_from_group (self, individual, group);
1033 static void
1034 empathy_roster_view_constructed (GObject *object)
1036 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1037 void (*chain_up) (GObject *) =
1038 ((GObjectClass *) empathy_roster_view_parent_class)->constructed;
1040 if (chain_up != NULL)
1041 chain_up (object);
1043 g_assert (EMPATHY_IS_ROSTER_MODEL (self->priv->model));
1045 /* Get saved group states. */
1046 empathy_contact_groups_get_all ();
1048 populate_view (self);
1050 tp_g_signal_connect_object (self->priv->model, "individual-added",
1051 G_CALLBACK (individual_added_cb), self, 0);
1052 tp_g_signal_connect_object (self->priv->model, "individual-removed",
1053 G_CALLBACK (individual_removed_cb), self, 0);
1054 tp_g_signal_connect_object (self->priv->model, "groups-changed",
1055 G_CALLBACK (groups_changed_cb), self, 0);
1057 gtk_list_box_set_sort_func (GTK_LIST_BOX (self),
1058 roster_view_sort, self, NULL);
1060 gtk_list_box_set_header_func (GTK_LIST_BOX (self), update_header, self, NULL);
1062 gtk_list_box_set_filter_func (GTK_LIST_BOX (self), filter_list, self, NULL);
1064 gtk_list_box_set_activate_on_single_click (GTK_LIST_BOX (self), FALSE);
1067 static void
1068 clear_view (EmpathyRosterView *self)
1070 g_hash_table_remove_all (self->priv->roster_contacts);
1071 g_hash_table_remove_all (self->priv->roster_groups);
1072 g_hash_table_remove_all (self->priv->displayed_contacts);
1074 gtk_container_foreach (GTK_CONTAINER (self),
1075 (GtkCallback) gtk_widget_destroy, NULL);
1078 static void
1079 empathy_roster_view_dispose (GObject *object)
1081 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1082 void (*chain_up) (GObject *) =
1083 ((GObjectClass *) empathy_roster_view_parent_class)->dispose;
1085 /* Start by clearing the view so our internal hash tables are cleared from
1086 * objects being destroyed. */
1087 clear_view (self);
1089 stop_flashing (self);
1091 empathy_roster_view_set_live_search (self, NULL);
1092 g_clear_object (&self->priv->model);
1094 if (self->priv->search_id != 0)
1096 g_source_remove (self->priv->search_id);
1097 self->priv->search_id = 0;
1100 if (chain_up != NULL)
1101 chain_up (object);
1104 static void
1105 empathy_roster_view_finalize (GObject *object)
1107 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (object);
1108 void (*chain_up) (GObject *) =
1109 ((GObjectClass *) empathy_roster_view_parent_class)->finalize;
1111 g_hash_table_unref (self->priv->roster_contacts);
1112 g_hash_table_unref (self->priv->roster_groups);
1113 g_hash_table_unref (self->priv->displayed_contacts);
1114 g_queue_free_full (self->priv->events, event_free);
1116 if (chain_up != NULL)
1117 chain_up (object);
1120 static void
1121 empathy_roster_view_row_activated (GtkListBox *box,
1122 GtkListBoxRow *row)
1124 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (box);
1125 EmpathyRosterContact *contact;
1126 FolksIndividual *individual;
1127 GList *l;
1129 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1130 return;
1132 contact = EMPATHY_ROSTER_CONTACT (row);
1133 individual = empathy_roster_contact_get_individual (contact);
1135 /* Activate the oldest event associated with this contact, if any */
1136 for (l = g_queue_peek_tail_link (self->priv->events); l != NULL;
1137 l = g_list_previous (l))
1139 Event *event = l->data;
1141 if (event->individual == individual)
1143 g_signal_emit (box, signals[SIG_EVENT_ACTIVATED], 0, individual,
1144 event->user_data);
1145 return;
1149 g_signal_emit (box, signals[SIG_INDIVIDUAL_ACTIVATED], 0, individual);
1152 static void
1153 fire_popup_individual_menu (EmpathyRosterView *self,
1154 GtkListBoxRow *row,
1155 guint button,
1156 guint time)
1158 EmpathyRosterContact *contact;
1159 FolksIndividual *individual;
1160 const gchar *active_group;
1162 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1163 return;
1165 contact = EMPATHY_ROSTER_CONTACT (row);
1166 individual = empathy_roster_contact_get_individual (contact);
1168 active_group = empathy_roster_contact_get_group (contact);
1169 g_signal_emit (self, signals[SIG_POPUP_INDIVIDUAL_MENU], 0,
1170 active_group, individual, button, time);
1173 static gboolean
1174 empathy_roster_view_button_press_event (GtkWidget *widget,
1175 GdkEventButton *event)
1177 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1178 gboolean (*chain_up) (GtkWidget *, GdkEventButton *) =
1179 ((GtkWidgetClass *) empathy_roster_view_parent_class)->button_press_event;
1181 if (event->button == 3)
1183 GtkListBoxRow *row;
1185 row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), event->y);
1187 if (row != NULL)
1189 gtk_list_box_select_row (GTK_LIST_BOX (self), row);
1191 fire_popup_individual_menu (self, row, event->button, event->time);
1195 return chain_up (widget, event);
1198 static gboolean
1199 empathy_roster_view_key_press_event (GtkWidget *widget,
1200 GdkEventKey *event)
1202 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1203 gboolean (*chain_up) (GtkWidget *, GdkEventKey *) =
1204 ((GtkWidgetClass *) empathy_roster_view_parent_class)->key_press_event;
1206 if (event->keyval == GDK_KEY_Menu)
1208 GtkListBoxRow *row;
1210 row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
1212 if (row != NULL)
1213 fire_popup_individual_menu (self, row, 0, event->time);
1216 return chain_up (widget, event);
1220 * @out_row: (out) (allow-none)
1222 FolksIndividual *
1223 empathy_roster_view_get_individual_at_y (EmpathyRosterView *self,
1224 gint y,
1225 GtkListBoxRow **out_row)
1227 GtkListBoxRow *row;
1229 row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
1231 if (out_row != NULL)
1232 *out_row = row;
1234 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1235 return NULL;
1237 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));
1240 const gchar *
1241 empathy_roster_view_get_group_at_y (EmpathyRosterView *self,
1242 gint y)
1244 GtkListBoxRow *row;
1246 row = gtk_list_box_get_row_at_y (GTK_LIST_BOX (self), y);
1248 if (EMPATHY_IS_ROSTER_CONTACT (row))
1249 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (row));
1250 else if (EMPATHY_IS_ROSTER_GROUP (row))
1251 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (row));
1253 return NULL;
1256 static gboolean
1257 empathy_roster_view_query_tooltip (GtkWidget *widget,
1258 gint x,
1259 gint y,
1260 gboolean keyboard_mode,
1261 GtkTooltip *tooltip)
1263 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (widget);
1264 FolksIndividual *individual;
1265 gboolean result;
1266 GtkListBoxRow *row;
1268 individual = empathy_roster_view_get_individual_at_y (self, y, &row);
1269 if (individual == NULL)
1270 return FALSE;
1272 g_signal_emit (self, signals[SIG_INDIVIDUAL_TOOLTIP], 0,
1273 individual, keyboard_mode, tooltip, &result);
1275 if (result)
1277 GtkAllocation allocation;
1279 gtk_widget_get_allocation (GTK_WIDGET (row), &allocation);
1280 gtk_tooltip_set_tip_area (tooltip, (GdkRectangle *) &allocation);
1283 return result;
1286 static void
1287 empathy_roster_view_remove (GtkContainer *container,
1288 GtkWidget *widget)
1290 EmpathyRosterView *self = EMPATHY_ROSTER_VIEW (container);
1291 void (*chain_up) (GtkContainer *, GtkWidget *) =
1292 ((GtkContainerClass *) empathy_roster_view_parent_class)->remove;
1294 chain_up (container, widget);
1296 if (EMPATHY_IS_ROSTER_CONTACT (widget))
1297 remove_from_displayed (self, (EmpathyRosterContact *) widget);
1300 static void
1301 empathy_roster_view_class_init (
1302 EmpathyRosterViewClass *klass)
1304 GObjectClass *oclass = G_OBJECT_CLASS (klass);
1305 GtkListBoxClass *box_class = GTK_LIST_BOX_CLASS (klass);
1306 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1307 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
1308 GParamSpec *spec;
1310 oclass->get_property = empathy_roster_view_get_property;
1311 oclass->set_property = empathy_roster_view_set_property;
1312 oclass->constructed = empathy_roster_view_constructed;
1313 oclass->dispose = empathy_roster_view_dispose;
1314 oclass->finalize = empathy_roster_view_finalize;
1316 widget_class->button_press_event = empathy_roster_view_button_press_event;
1317 widget_class->key_press_event = empathy_roster_view_key_press_event;
1318 widget_class->query_tooltip = empathy_roster_view_query_tooltip;
1320 container_class->remove = empathy_roster_view_remove;
1322 box_class->row_activated = empathy_roster_view_row_activated;
1324 spec = g_param_spec_object ("model", "Model",
1325 "EmpathyRosterModel",
1326 EMPATHY_TYPE_ROSTER_MODEL,
1327 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1328 g_object_class_install_property (oclass, PROP_MODEL, spec);
1330 spec = g_param_spec_boolean ("show-offline", "Show Offline",
1331 "Show offline contacts",
1332 FALSE,
1333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1334 g_object_class_install_property (oclass, PROP_SHOW_OFFLINE, spec);
1336 spec = g_param_spec_boolean ("show-groups", "Show Groups",
1337 "Show groups",
1338 FALSE,
1339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1340 g_object_class_install_property (oclass, PROP_SHOW_GROUPS, spec);
1342 spec = g_param_spec_boolean ("empty", "Empty",
1343 "Is the view currently empty?",
1344 FALSE,
1345 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1346 g_object_class_install_property (oclass, PROP_EMPTY, spec);
1348 signals[SIG_INDIVIDUAL_ACTIVATED] = g_signal_new ("individual-activated",
1349 G_OBJECT_CLASS_TYPE (klass),
1350 G_SIGNAL_RUN_LAST,
1351 0, NULL, NULL, NULL,
1352 G_TYPE_NONE,
1353 1, FOLKS_TYPE_INDIVIDUAL);
1355 signals[SIG_POPUP_INDIVIDUAL_MENU] = g_signal_new ("popup-individual-menu",
1356 G_OBJECT_CLASS_TYPE (klass),
1357 G_SIGNAL_RUN_LAST,
1358 0, NULL, NULL, NULL,
1359 G_TYPE_NONE,
1360 4, G_TYPE_STRING, FOLKS_TYPE_INDIVIDUAL, G_TYPE_UINT,
1361 G_TYPE_UINT);
1363 signals[SIG_EVENT_ACTIVATED] = g_signal_new ("event-activated",
1364 G_OBJECT_CLASS_TYPE (klass),
1365 G_SIGNAL_RUN_LAST,
1366 0, NULL, NULL, NULL,
1367 G_TYPE_NONE,
1368 2, FOLKS_TYPE_INDIVIDUAL, G_TYPE_POINTER);
1370 signals[SIG_INDIVIDUAL_TOOLTIP] = g_signal_new ("individual-tooltip",
1371 G_OBJECT_CLASS_TYPE (klass),
1372 G_SIGNAL_RUN_LAST,
1373 0, g_signal_accumulator_true_handled, NULL, NULL,
1374 G_TYPE_BOOLEAN,
1375 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_BOOLEAN, GTK_TYPE_TOOLTIP);
1377 g_type_class_add_private (klass, sizeof (EmpathyRosterViewPriv));
1380 static void
1381 empathy_roster_view_init (EmpathyRosterView *self)
1383 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1384 EMPATHY_TYPE_ROSTER_VIEW, EmpathyRosterViewPriv);
1386 self->priv->roster_contacts = g_hash_table_new_full (NULL, NULL,
1387 NULL, (GDestroyNotify) g_hash_table_unref);
1388 self->priv->roster_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
1389 g_free, NULL);
1390 self->priv->displayed_contacts = g_hash_table_new (NULL, NULL);
1392 self->priv->events = g_queue_new ();
1394 self->priv->empty = TRUE;
1397 GtkWidget *
1398 empathy_roster_view_new (EmpathyRosterModel *model)
1400 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model), NULL);
1402 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW,
1403 "model", model,
1404 NULL);
1407 void
1408 empathy_roster_view_show_offline (EmpathyRosterView *self,
1409 gboolean show)
1411 if (self->priv->show_offline == show)
1412 return;
1414 self->priv->show_offline = show;
1415 gtk_list_box_invalidate_filter (GTK_LIST_BOX (self));
1417 g_object_notify (G_OBJECT (self), "show-offline");
1420 void
1421 empathy_roster_view_show_groups (EmpathyRosterView *self,
1422 gboolean show)
1424 if (self->priv->show_groups == show)
1425 return;
1427 self->priv->show_groups = show;
1429 /* TODO: block sort/filter? */
1430 clear_view (self);
1431 populate_view (self);
1433 g_object_notify (G_OBJECT (self), "show-groups");
1436 static void
1437 select_first_contact (EmpathyRosterView *self)
1439 GList *children, *l;
1441 children = gtk_container_get_children (GTK_CONTAINER (self));
1442 for (l = children; l != NULL; l = g_list_next (l))
1444 GtkWidget *child = l->data;
1446 if (!gtk_widget_get_child_visible (child))
1447 continue;
1449 if (!EMPATHY_IS_ROSTER_CONTACT (child))
1450 continue;
1452 gtk_list_box_select_row (GTK_LIST_BOX (self), GTK_LIST_BOX_ROW (child));
1453 break;
1456 g_list_free (children);
1459 static gboolean
1460 search_timeout_cb (EmpathyRosterView *self)
1462 gtk_list_box_invalidate_filter (GTK_LIST_BOX (self));
1464 select_first_contact (self);
1466 self->priv->search_id = 0;
1467 return G_SOURCE_REMOVE;
1470 static void
1471 search_text_notify_cb (TpawLiveSearch *search,
1472 GParamSpec *pspec,
1473 EmpathyRosterView *self)
1475 if (self->priv->search_id != 0)
1476 g_source_remove (self->priv->search_id);
1478 self->priv->search_id = g_timeout_add (SEARCH_TIMEOUT,
1479 (GSourceFunc) search_timeout_cb, self);
1482 static void
1483 search_activate_cb (GtkWidget *search,
1484 EmpathyRosterView *self)
1486 GtkListBox *box = GTK_LIST_BOX (self);
1487 GtkListBoxRow *row;
1489 row = gtk_list_box_get_selected_row (box);
1490 if (row == NULL)
1491 return;
1493 empathy_roster_view_row_activated (box, row);
1496 void
1497 empathy_roster_view_set_live_search (EmpathyRosterView *self,
1498 TpawLiveSearch *search)
1500 if (self->priv->search != NULL)
1502 g_signal_handlers_disconnect_by_func (self->priv->search,
1503 search_text_notify_cb, self);
1504 g_signal_handlers_disconnect_by_func (self->priv->search,
1505 search_activate_cb, self);
1507 g_clear_object (&self->priv->search);
1510 if (search == NULL)
1511 return;
1513 self->priv->search = g_object_ref (search);
1515 g_signal_connect (self->priv->search, "notify::text",
1516 G_CALLBACK (search_text_notify_cb), self);
1517 g_signal_connect (self->priv->search, "activate",
1518 G_CALLBACK (search_activate_cb), self);
1521 gboolean
1522 empathy_roster_view_is_empty (EmpathyRosterView *self)
1524 return self->priv->empty;
1527 gboolean
1528 empathy_roster_view_is_searching (EmpathyRosterView *self)
1530 return (self->priv->search != NULL &&
1531 gtk_widget_get_visible (GTK_WIDGET (self->priv->search)));
1534 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1535 * Empathy's internals. */
1536 guint
1537 empathy_roster_view_add_event (EmpathyRosterView *self,
1538 FolksIndividual *individual,
1539 const gchar *icon,
1540 gpointer user_data)
1542 GHashTable *contacts;
1544 contacts = g_hash_table_lookup (self->priv->roster_contacts, individual);
1545 if (contacts == NULL)
1546 return 0;
1548 self->priv->last_event_id++;
1550 g_queue_push_head (self->priv->events,
1551 event_new (self->priv->last_event_id, individual, icon, user_data));
1553 start_flashing (self);
1555 return self->priv->last_event_id;
1558 void
1559 empathy_roster_view_remove_event (EmpathyRosterView *self,
1560 guint event_id)
1562 GList *l;
1564 for (l = g_queue_peek_head_link (self->priv->events); l != NULL;
1565 l = g_list_next (l))
1567 Event *event = l->data;
1569 if (event->id == event_id)
1571 remove_event (self, event);
1572 return;
1577 FolksIndividual *
1578 empathy_roster_view_get_selected_individual (EmpathyRosterView *self)
1580 GtkListBoxRow *row;
1582 row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self));
1584 if (!EMPATHY_IS_ROSTER_CONTACT (row))
1585 return NULL;
1587 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row));