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
31 SIG_INDIVIDUAL_ACTIVATED
,
32 SIG_POPUP_INDIVIDUAL_MENU
,
34 SIG_INDIVIDUAL_TOOLTIP
,
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
;
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. */
62 gboolean display_flash_event
;
66 gboolean show_offline
;
70 TpawLiveSearch
*search
;
72 EmpathyRosterModel
*model
;
75 /* Prototypes to break cycles */
76 static void remove_from_group (EmpathyRosterView
*self
,
77 FolksIndividual
*individual
,
83 FolksIndividual
*individual
;
90 FolksIndividual
*individual
,
94 Event
*event
= g_slice_new (Event
);
97 event
->individual
= g_object_ref (individual
);
98 event
->icon
= g_strdup (icon
);
99 event
->user_data
= user_data
;
104 event_free (gpointer data
)
107 g_object_unref (event
->individual
);
108 g_free (event
->icon
);
110 g_slice_free (Event
, event
);
114 empathy_roster_view_get_property (GObject
*object
,
119 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
124 g_value_set_object (value
, self
->priv
->model
);
126 case PROP_SHOW_OFFLINE
:
127 g_value_set_boolean (value
, self
->priv
->show_offline
);
129 case PROP_SHOW_GROUPS
:
130 g_value_set_boolean (value
, self
->priv
->show_groups
);
133 g_value_set_boolean (value
, self
->priv
->empty
);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
142 empathy_roster_view_set_property (GObject
*object
,
147 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
152 g_assert (self
->priv
->model
== NULL
);
153 self
->priv
->model
= g_value_dup_object (value
);
155 case PROP_SHOW_OFFLINE
:
156 empathy_roster_view_show_offline (self
, g_value_get_boolean (value
));
158 case PROP_SHOW_GROUPS
:
159 empathy_roster_view_show_groups (self
, g_value_get_boolean (value
));
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
168 roster_contact_changed_cb (GtkListBoxRow
*child
,
170 EmpathyRosterView
*self
)
172 gtk_list_box_row_changed (child
);
176 add_roster_contact (EmpathyRosterView
*self
,
177 FolksIndividual
*individual
,
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
);
199 group_expanded_cb (GtkWidget
*expander
,
201 EmpathyRosterGroup
*group
)
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
,
221 return g_hash_table_lookup (self
->priv
->roster_groups
, group
);
224 static EmpathyRosterGroup
*
225 ensure_roster_group (EmpathyRosterView
*self
,
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");
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
),
253 return EMPATHY_ROSTER_GROUP (roster_group
);
257 update_empty (EmpathyRosterView
*self
,
260 if (self
->priv
->empty
== empty
)
263 self
->priv
->empty
= empty
;
264 g_object_notify (G_OBJECT (self
), "empty");
267 static gboolean
filter_group (EmpathyRosterView
*self
,
268 EmpathyRosterGroup
*group
);
271 at_least_one_group_displayed (EmpathyRosterView
*self
)
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
))
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
);
300 update_empty (self
, TRUE
);
304 update_group_widgets (EmpathyRosterView
*self
,
305 EmpathyRosterGroup
*group
,
306 EmpathyRosterContact
*contact
,
309 guint old_count
, count
;
311 old_count
= empathy_roster_group_get_widgets_count (group
);
314 count
= empathy_roster_group_add_widget (group
, GTK_WIDGET (contact
));
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
);
327 add_to_group (EmpathyRosterView
*self
,
328 FolksIndividual
*individual
,
332 GHashTable
*contacts
;
333 EmpathyRosterGroup
*roster_group
= NULL
;
335 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
336 if (contacts
== NULL
)
339 if (g_hash_table_lookup (contacts
, group
) != NULL
)
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
);
364 individual_favourite_change_cb (FolksIndividual
*individual
,
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
;
373 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
374 if (contacts
== NULL
)
377 if (self
->priv
->show_groups
)
378 contact
= g_hash_table_lookup (contacts
,
379 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
);
381 contact
= g_hash_table_lookup (contacts
, NO_GROUP
);
386 gtk_list_box_row_changed (GTK_LIST_BOX_ROW (contact
));
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
)
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
);
411 groups
= empathy_roster_model_dup_groups_for_individual (self
->priv
->model
,
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
);
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);
435 set_event_icon_on_individual (EmpathyRosterView
*self
,
436 FolksIndividual
*individual
,
439 GHashTable
*contacts
;
443 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
444 if (contacts
== NULL
)
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
);
457 flash_event (Event
*event
,
458 EmpathyRosterView
*self
)
460 set_event_icon_on_individual (self
, event
->individual
, event
->icon
);
464 unflash_event (Event
*event
,
465 EmpathyRosterView
*self
)
467 set_event_icon_on_individual (self
, event
->individual
, NULL
);
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
;
482 g_queue_foreach (self
->priv
->events
, (GFunc
) unflash_event
, self
);
483 self
->priv
->display_flash_event
= TRUE
;
490 start_flashing (EmpathyRosterView
*self
)
492 if (self
->priv
->flash_id
!= 0)
495 self
->priv
->display_flash_event
= TRUE
;
497 self
->priv
->flash_id
= g_timeout_add (FLASH_TIMEOUT
,
502 stop_flashing (EmpathyRosterView
*self
)
504 if (self
->priv
->flash_id
== 0)
507 g_source_remove (self
->priv
->flash_id
);
508 self
->priv
->flash_id
= 0;
512 remove_event (EmpathyRosterView
*self
,
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
);
525 remove_all_individual_event (EmpathyRosterView
*self
,
526 FolksIndividual
*individual
)
530 for (l
= g_queue_peek_head_link (self
->priv
->events
); l
!= NULL
;
533 Event
*event
= l
->data
;
535 if (event
->individual
== individual
)
537 remove_event (self
, event
);
544 individual_removed (EmpathyRosterView
*self
,
545 FolksIndividual
*individual
)
547 GHashTable
*contacts
;
551 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
552 if (contacts
== NULL
)
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
);
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
);
578 individual_added_cb (EmpathyRosterModel
*model
,
579 FolksIndividual
*individual
,
580 EmpathyRosterView
*self
)
582 individual_added (self
, individual
);
586 individual_removed_cb (EmpathyRosterModel
*model
,
587 FolksIndividual
*individual
,
588 EmpathyRosterView
*self
)
590 individual_removed (self
, individual
);
594 contact_in_top (EmpathyRosterView
*self
,
595 EmpathyRosterContact
*contact
)
597 if (!self
->priv
->show_groups
)
599 /* Always display top contacts in non-group mode. */
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
)
613 g_list_free_full (groups
, g_free
);
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 */
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
);
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
);
655 /* Both contacts are in the top of the roster (or not). Sort them
657 return compare_roster_contacts_by_alias (a
, b
);
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
))
671 if (!tp_strdiff (group_b
, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
))
674 if (!tp_strdiff (group_a
, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED
))
676 else if (!tp_strdiff (group_b
, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED
))
679 return g_utf8_collate (group_a
, group_b
);
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
);
697 return compare_group_names (group_a
, group_b
);
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
);
708 return compare_roster_contacts_with_groups (self
, a
, b
);
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
);
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 */
736 /* @contact is in a different group, sort by group name */
737 return compare_group_names (contact_group
, group_name
);
741 roster_view_sort (GtkListBoxRow
*a
,
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);
764 update_header (GtkListBoxRow
*row
,
765 GtkListBoxRow
*before
,
770 /* No separator before the first row */
771 gtk_list_box_row_set_header (row
, NULL
);
775 if (gtk_list_box_row_get_header (row
) != NULL
)
778 gtk_list_box_row_set_header (row
,
779 gtk_separator_new (GTK_ORIENTATION_HORIZONTAL
));
783 is_searching (EmpathyRosterView
*self
)
785 if (self
->priv
->search
== NULL
)
788 return gtk_widget_get_visible (GTK_WIDGET (self
->priv
->search
));
792 add_to_displayed (EmpathyRosterView
*self
,
793 EmpathyRosterContact
*contact
)
795 FolksIndividual
*individual
;
796 GHashTable
*contacts
;
800 if (g_hash_table_lookup (self
->priv
->displayed_contacts
, contact
) != NULL
)
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
)
812 individual
= empathy_roster_contact_get_individual (contact
);
813 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
814 if (contacts
== NULL
)
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
);
827 gtk_list_box_row_changed (group
);
832 remove_from_displayed (EmpathyRosterView
*self
,
833 EmpathyRosterContact
*contact
)
835 g_hash_table_remove (self
->priv
->displayed_contacts
, contact
);
837 check_if_empty (self
);
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.
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
)
873 if (contact_in_top (self
, contact
) &&
874 contact_is_favorite (contact
))
875 /* Favorite top contacts are always displayed */
878 return empathy_roster_contact_is_online (contact
);
883 filter_contact (EmpathyRosterView
*self
,
884 EmpathyRosterContact
*contact
)
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
);
900 /* When searching, always display even if the group is closed */
901 if (!is_searching (self
) &&
902 !gtk_expander_get_expanded (group
->expander
))
909 add_to_displayed (self
, contact
);
913 remove_from_displayed (self
, contact
);
920 filter_group (EmpathyRosterView
*self
,
921 EmpathyRosterGroup
*group
)
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
))
939 g_list_free (widgets
);
945 filter_list (GtkListBoxRow
*child
,
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
);
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
);
976 remove_from_group (EmpathyRosterView
*self
,
977 FolksIndividual
*individual
,
980 GHashTable
*contacts
;
982 EmpathyRosterGroup
*roster_group
;
984 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
985 if (contacts
== NULL
)
988 contact
= g_hash_table_lookup (contacts
, group
);
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
);
1011 groups_changed_cb (EmpathyRosterModel
*model
,
1012 FolksIndividual
*individual
,
1015 EmpathyRosterView
*self
)
1017 if (!self
->priv
->show_groups
)
1019 gtk_list_box_invalidate_sort (GTK_LIST_BOX (self
));
1025 add_to_group (self
, individual
, group
);
1029 remove_from_group (self
, individual
, group
);
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
)
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
);
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
);
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. */
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
)
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
)
1121 empathy_roster_view_row_activated (GtkListBox
*box
,
1124 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (box
);
1125 EmpathyRosterContact
*contact
;
1126 FolksIndividual
*individual
;
1129 if (!EMPATHY_IS_ROSTER_CONTACT (row
))
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
,
1149 g_signal_emit (box
, signals
[SIG_INDIVIDUAL_ACTIVATED
], 0, individual
);
1153 fire_popup_individual_menu (EmpathyRosterView
*self
,
1158 EmpathyRosterContact
*contact
;
1159 FolksIndividual
*individual
;
1160 const gchar
*active_group
;
1162 if (!EMPATHY_IS_ROSTER_CONTACT (row
))
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
);
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)
1185 row
= gtk_list_box_get_row_at_y (GTK_LIST_BOX (self
), event
->y
);
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
);
1199 empathy_roster_view_key_press_event (GtkWidget
*widget
,
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
)
1210 row
= gtk_list_box_get_selected_row (GTK_LIST_BOX (self
));
1213 fire_popup_individual_menu (self
, row
, 0, event
->time
);
1216 return chain_up (widget
, event
);
1220 * @out_row: (out) (allow-none)
1223 empathy_roster_view_get_individual_at_y (EmpathyRosterView
*self
,
1225 GtkListBoxRow
**out_row
)
1229 row
= gtk_list_box_get_row_at_y (GTK_LIST_BOX (self
), y
);
1231 if (out_row
!= NULL
)
1234 if (!EMPATHY_IS_ROSTER_CONTACT (row
))
1237 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row
));
1241 empathy_roster_view_get_group_at_y (EmpathyRosterView
*self
,
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
));
1257 empathy_roster_view_query_tooltip (GtkWidget
*widget
,
1260 gboolean keyboard_mode
,
1261 GtkTooltip
*tooltip
)
1263 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (widget
);
1264 FolksIndividual
*individual
;
1268 individual
= empathy_roster_view_get_individual_at_y (self
, y
, &row
);
1269 if (individual
== NULL
)
1272 g_signal_emit (self
, signals
[SIG_INDIVIDUAL_TOOLTIP
], 0,
1273 individual
, keyboard_mode
, tooltip
, &result
);
1277 GtkAllocation allocation
;
1279 gtk_widget_get_allocation (GTK_WIDGET (row
), &allocation
);
1280 gtk_tooltip_set_tip_area (tooltip
, (GdkRectangle
*) &allocation
);
1287 empathy_roster_view_remove (GtkContainer
*container
,
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
);
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
);
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",
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",
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?",
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
),
1351 0, NULL
, NULL
, NULL
,
1353 1, FOLKS_TYPE_INDIVIDUAL
);
1355 signals
[SIG_POPUP_INDIVIDUAL_MENU
] = g_signal_new ("popup-individual-menu",
1356 G_OBJECT_CLASS_TYPE (klass
),
1358 0, NULL
, NULL
, NULL
,
1360 4, G_TYPE_STRING
, FOLKS_TYPE_INDIVIDUAL
, G_TYPE_UINT
,
1363 signals
[SIG_EVENT_ACTIVATED
] = g_signal_new ("event-activated",
1364 G_OBJECT_CLASS_TYPE (klass
),
1366 0, NULL
, NULL
, NULL
,
1368 2, FOLKS_TYPE_INDIVIDUAL
, G_TYPE_POINTER
);
1370 signals
[SIG_INDIVIDUAL_TOOLTIP
] = g_signal_new ("individual-tooltip",
1371 G_OBJECT_CLASS_TYPE (klass
),
1373 0, g_signal_accumulator_true_handled
, NULL
, NULL
,
1375 3, FOLKS_TYPE_INDIVIDUAL
, G_TYPE_BOOLEAN
, GTK_TYPE_TOOLTIP
);
1377 g_type_class_add_private (klass
, sizeof (EmpathyRosterViewPriv
));
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
,
1390 self
->priv
->displayed_contacts
= g_hash_table_new (NULL
, NULL
);
1392 self
->priv
->events
= g_queue_new ();
1394 self
->priv
->empty
= TRUE
;
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
,
1408 empathy_roster_view_show_offline (EmpathyRosterView
*self
,
1411 if (self
->priv
->show_offline
== show
)
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");
1421 empathy_roster_view_show_groups (EmpathyRosterView
*self
,
1424 if (self
->priv
->show_groups
== show
)
1427 self
->priv
->show_groups
= show
;
1429 /* TODO: block sort/filter? */
1431 populate_view (self
);
1433 g_object_notify (G_OBJECT (self
), "show-groups");
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
))
1449 if (!EMPATHY_IS_ROSTER_CONTACT (child
))
1452 gtk_list_box_select_row (GTK_LIST_BOX (self
), GTK_LIST_BOX_ROW (child
));
1456 g_list_free (children
);
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
;
1471 search_text_notify_cb (TpawLiveSearch
*search
,
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
);
1483 search_activate_cb (GtkWidget
*search
,
1484 EmpathyRosterView
*self
)
1486 GtkListBox
*box
= GTK_LIST_BOX (self
);
1489 row
= gtk_list_box_get_selected_row (box
);
1493 empathy_roster_view_row_activated (box
, row
);
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
);
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
);
1522 empathy_roster_view_is_empty (EmpathyRosterView
*self
)
1524 return self
->priv
->empty
;
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. */
1537 empathy_roster_view_add_event (EmpathyRosterView
*self
,
1538 FolksIndividual
*individual
,
1542 GHashTable
*contacts
;
1544 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
1545 if (contacts
== NULL
)
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
;
1559 empathy_roster_view_remove_event (EmpathyRosterView
*self
,
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
);
1578 empathy_roster_view_get_selected_individual (EmpathyRosterView
*self
)
1582 row
= gtk_list_box_get_selected_row (GTK_LIST_BOX (self
));
1584 if (!EMPATHY_IS_ROSTER_CONTACT (row
))
1587 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (row
));