3 #include "empathy-roster-view.h"
5 #include <glib/gi18n-lib.h>
7 #include <libempathy/empathy-contact-groups.h>
9 #include <libempathy-gtk/empathy-roster-contact.h>
10 #include <libempathy-gtk/empathy-roster-group.h>
11 #include <libempathy-gtk/empathy-ui-utils.h>
13 #include <libempathy/empathy-utils.h>
15 G_DEFINE_TYPE (EmpathyRosterView
, empathy_roster_view
, EGG_TYPE_LIST_BOX
)
17 /* Flashing delay for icons (milliseconds). */
18 #define FLASH_TIMEOUT 500
20 /* Delay in milliseconds between the last stroke on the keyboard and the start
21 * of the live search. */
22 #define SEARCH_TIMEOUT 500
35 SIG_INDIVIDUAL_ACTIVATED
,
36 SIG_POPUP_INDIVIDUAL_MENU
,
38 SIG_INDIVIDUAL_TOOLTIP
,
42 static guint signals
[LAST_SIGNAL
];
44 #define NO_GROUP "X-no-group"
46 struct _EmpathyRosterViewPriv
48 /* FolksIndividual (borrowed) -> GHashTable (
49 * (gchar * group_name) -> EmpathyRosterContact (borrowed))
51 * When not using groups, this hash just have one element mapped
52 * from the special NO_GROUP key. We could use it as a set but
53 * I prefer to stay coherent in the way this hash is managed.
55 GHashTable
*roster_contacts
;
56 /* (gchar *group_name) -> EmpathyRosterGroup (borrowed) */
57 GHashTable
*roster_groups
;
58 /* Hash of the EmpathyRosterContact currently displayed */
59 GHashTable
*displayed_contacts
;
62 /* queue of (Event *). The most recent events are in the head of the queue
63 * so we always display the icon of the oldest one. */
66 gboolean display_flash_event
;
70 gboolean show_offline
;
74 EmpathyLiveSearch
*search
;
76 EmpathyRosterModel
*model
;
82 FolksIndividual
*individual
;
89 FolksIndividual
*individual
,
93 Event
*event
= g_slice_new (Event
);
96 event
->individual
= g_object_ref (individual
);
97 event
->icon
= g_strdup (icon
);
98 event
->user_data
= user_data
;
103 event_free (gpointer data
)
106 g_object_unref (event
->individual
);
107 g_free (event
->icon
);
109 g_slice_free (Event
, event
);
113 empathy_roster_view_get_property (GObject
*object
,
118 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
123 g_value_set_object (value
, self
->priv
->model
);
125 case PROP_SHOW_OFFLINE
:
126 g_value_set_boolean (value
, self
->priv
->show_offline
);
128 case PROP_SHOW_GROUPS
:
129 g_value_set_boolean (value
, self
->priv
->show_groups
);
132 g_value_set_boolean (value
, self
->priv
->empty
);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
141 empathy_roster_view_set_property (GObject
*object
,
146 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
151 g_assert (self
->priv
->model
== NULL
);
152 self
->priv
->model
= g_value_dup_object (value
);
154 case PROP_SHOW_OFFLINE
:
155 empathy_roster_view_show_offline (self
, g_value_get_boolean (value
));
157 case PROP_SHOW_GROUPS
:
158 empathy_roster_view_show_groups (self
, g_value_get_boolean (value
));
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
167 roster_contact_changed_cb (GtkWidget
*child
,
169 EmpathyRosterView
*self
)
171 egg_list_box_child_changed (EGG_LIST_BOX (self
), child
);
175 add_roster_contact (EmpathyRosterView
*self
,
176 FolksIndividual
*individual
,
181 contact
= empathy_roster_contact_new (individual
, group
);
183 /* Need to refilter if online is changed */
184 g_signal_connect (contact
, "notify::online",
185 G_CALLBACK (roster_contact_changed_cb
), self
);
187 /* Need to resort if alias is changed */
188 g_signal_connect (contact
, "notify::alias",
189 G_CALLBACK (roster_contact_changed_cb
), self
);
191 gtk_widget_show (contact
);
192 gtk_container_add (GTK_CONTAINER (self
), contact
);
198 group_expanded_cb (EmpathyRosterGroup
*group
,
200 EmpathyRosterView
*self
)
204 widgets
= empathy_roster_group_get_widgets (group
);
205 for (l
= widgets
; l
!= NULL
; l
= g_list_next (l
))
207 egg_list_box_child_changed (EGG_LIST_BOX (self
), l
->data
);
210 g_list_free (widgets
);
212 empathy_contact_group_set_expanded (empathy_roster_group_get_name (group
),
213 gtk_expander_get_expanded (GTK_EXPANDER (group
)));
216 static EmpathyRosterGroup
*
217 lookup_roster_group (EmpathyRosterView
*self
,
220 return g_hash_table_lookup (self
->priv
->roster_groups
, group
);
223 static EmpathyRosterGroup
*
224 ensure_roster_group (EmpathyRosterView
*self
,
227 GtkWidget
*roster_group
;
229 roster_group
= (GtkWidget
*) lookup_roster_group (self
, group
);
230 if (roster_group
!= NULL
)
231 return EMPATHY_ROSTER_GROUP (roster_group
);
233 if (!tp_strdiff (group
, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
))
234 roster_group
= empathy_roster_group_new (group
, "emblem-favorite-symbolic");
235 else if (!tp_strdiff (group
, EMPATHY_ROSTER_MODEL_GROUP_PEOPLE_NEARBY
))
236 roster_group
= empathy_roster_group_new (group
, "im-local-xmpp");
238 roster_group
= empathy_roster_group_new (group
, NULL
);
240 gtk_expander_set_expanded (GTK_EXPANDER (roster_group
),
241 empathy_contact_group_get_expanded (group
));
243 g_signal_connect (roster_group
, "notify::expanded",
244 G_CALLBACK (group_expanded_cb
), self
);
246 gtk_widget_show (roster_group
);
247 gtk_container_add (GTK_CONTAINER (self
), roster_group
);
249 g_hash_table_insert (self
->priv
->roster_groups
, g_strdup (group
),
252 return EMPATHY_ROSTER_GROUP (roster_group
);
256 update_empty (EmpathyRosterView
*self
,
259 if (self
->priv
->empty
== empty
)
262 self
->priv
->empty
= empty
;
263 g_object_notify (G_OBJECT (self
), "empty");
266 static gboolean
filter_group (EmpathyRosterView
*self
,
267 EmpathyRosterGroup
*group
);
270 at_least_one_group_displayed (EmpathyRosterView
*self
)
275 g_hash_table_iter_init (&iter
, self
->priv
->roster_groups
);
276 while (g_hash_table_iter_next (&iter
, NULL
, &v
))
278 EmpathyRosterGroup
*group
= EMPATHY_ROSTER_GROUP (v
);
280 if (filter_group (self
, group
))
288 check_if_empty (EmpathyRosterView
*self
)
290 /* Roster is considered as empty if there is no contact *and* no group
291 * currently displayed. */
292 if (g_hash_table_size (self
->priv
->displayed_contacts
) != 0 ||
293 at_least_one_group_displayed (self
))
295 update_empty (self
, FALSE
);
299 update_empty (self
, TRUE
);
303 update_group_widgets (EmpathyRosterView
*self
,
304 EmpathyRosterGroup
*group
,
305 EmpathyRosterContact
*contact
,
308 guint old_count
, count
;
310 old_count
= empathy_roster_group_get_widgets_count (group
);
313 count
= empathy_roster_group_add_widget (group
, GTK_WIDGET (contact
));
315 count
= empathy_roster_group_remove_widget (group
, GTK_WIDGET (contact
));
317 if (count
!= old_count
)
319 egg_list_box_child_changed (EGG_LIST_BOX (self
), GTK_WIDGET (group
));
321 check_if_empty (self
);
326 add_to_group (EmpathyRosterView
*self
,
327 FolksIndividual
*individual
,
331 GHashTable
*contacts
;
332 EmpathyRosterGroup
*roster_group
= NULL
;
334 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
335 if (contacts
== NULL
)
338 if (g_hash_table_lookup (contacts
, group
) != NULL
)
341 if (tp_strdiff (group
, NO_GROUP
))
342 roster_group
= ensure_roster_group (self
, group
);
344 contact
= add_roster_contact (self
, individual
, group
);
345 g_hash_table_insert (contacts
, g_strdup (group
), contact
);
347 if (roster_group
!= NULL
)
349 update_group_widgets (self
, roster_group
,
350 EMPATHY_ROSTER_CONTACT (contact
), TRUE
);
355 individual_favourite_change_cb (FolksIndividual
*individual
,
357 EmpathyRosterView
*self
)
359 /* We may have to refilter the contact as only favorite contacts are always
360 * displayed regardless of their presence. */
361 GHashTable
*contacts
;
364 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
365 if (contacts
== NULL
)
368 if (self
->priv
->show_groups
)
369 contact
= g_hash_table_lookup (contacts
,
370 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
);
372 contact
= g_hash_table_lookup (contacts
, NO_GROUP
);
377 egg_list_box_child_changed (EGG_LIST_BOX (self
), contact
);
381 individual_added (EmpathyRosterView
*self
,
382 FolksIndividual
*individual
)
384 GHashTable
*contacts
;
386 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
387 if (contacts
!= NULL
)
390 contacts
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
392 g_hash_table_insert (self
->priv
->roster_contacts
, individual
, contacts
);
394 if (!self
->priv
->show_groups
)
396 add_to_group (self
, individual
, NO_GROUP
);
402 groups
= empathy_roster_model_dup_groups_for_individual (self
->priv
->model
,
405 if (g_list_length (groups
) > 0)
407 for (l
= groups
; l
!= NULL
; l
= g_list_next (l
))
409 add_to_group (self
, individual
, l
->data
);
414 /* No group, adds to Ungrouped */
415 add_to_group (self
, individual
, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED
);
418 g_list_free_full (groups
, g_free
);
421 tp_g_signal_connect_object (individual
, "notify::is-favourite",
422 G_CALLBACK (individual_favourite_change_cb
), self
, 0);
426 set_event_icon_on_individual (EmpathyRosterView
*self
,
427 FolksIndividual
*individual
,
430 GHashTable
*contacts
;
434 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
435 if (contacts
== NULL
)
438 g_hash_table_iter_init (&iter
, contacts
);
439 while (g_hash_table_iter_next (&iter
, NULL
, &v
))
441 EmpathyRosterContact
*contact
=v
;
443 empathy_roster_contact_set_event_icon (contact
, icon
);
448 flash_event (Event
*event
,
449 EmpathyRosterView
*self
)
451 set_event_icon_on_individual (self
, event
->individual
, event
->icon
);
455 unflash_event (Event
*event
,
456 EmpathyRosterView
*self
)
458 set_event_icon_on_individual (self
, event
->individual
, NULL
);
462 flash_cb (gpointer data
)
464 EmpathyRosterView
*self
= data
;
466 if (self
->priv
->display_flash_event
)
468 g_queue_foreach (self
->priv
->events
, (GFunc
) flash_event
, self
);
469 self
->priv
->display_flash_event
= FALSE
;
473 g_queue_foreach (self
->priv
->events
, (GFunc
) unflash_event
, self
);
474 self
->priv
->display_flash_event
= TRUE
;
481 start_flashing (EmpathyRosterView
*self
)
483 if (self
->priv
->flash_id
!= 0)
486 self
->priv
->display_flash_event
= TRUE
;
488 self
->priv
->flash_id
= g_timeout_add (FLASH_TIMEOUT
,
493 stop_flashing (EmpathyRosterView
*self
)
495 if (self
->priv
->flash_id
== 0)
498 g_source_remove (self
->priv
->flash_id
);
499 self
->priv
->flash_id
= 0;
503 remove_event (EmpathyRosterView
*self
,
506 unflash_event (event
, self
);
507 g_queue_remove (self
->priv
->events
, event
);
509 if (g_queue_get_length (self
->priv
->events
) == 0)
511 stop_flashing (self
);
516 remove_all_individual_event (EmpathyRosterView
*self
,
517 FolksIndividual
*individual
)
521 for (l
= g_queue_peek_head_link (self
->priv
->events
); l
!= NULL
;
524 Event
*event
= l
->data
;
526 if (event
->individual
== individual
)
528 remove_event (self
, event
);
535 individual_removed (EmpathyRosterView
*self
,
536 FolksIndividual
*individual
)
538 GHashTable
*contacts
;
542 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
543 if (contacts
== NULL
)
546 remove_all_individual_event (self
, individual
);
548 g_hash_table_iter_init (&iter
, contacts
);
549 while (g_hash_table_iter_next (&iter
, &key
, &value
))
551 const gchar
*group_name
= key
;
552 GtkWidget
*contact
= value
;
553 EmpathyRosterGroup
*group
;
555 group
= lookup_roster_group (self
, group_name
);
558 update_group_widgets (self
, group
,
559 EMPATHY_ROSTER_CONTACT (contact
), FALSE
);
562 gtk_container_remove (GTK_CONTAINER (self
), contact
);
565 g_hash_table_remove (self
->priv
->roster_contacts
, individual
);
569 individual_added_cb (EmpathyRosterModel
*model
,
570 FolksIndividual
*individual
,
571 EmpathyRosterView
*self
)
573 individual_added (self
, individual
);
577 individual_removed_cb (EmpathyRosterModel
*model
,
578 FolksIndividual
*individual
,
579 EmpathyRosterView
*self
)
581 individual_removed (self
, individual
);
585 contact_in_top (EmpathyRosterView
*self
,
586 EmpathyRosterContact
*contact
)
588 if (!self
->priv
->show_groups
)
590 /* Always display top contacts in non-group mode. */
592 FolksIndividual
*individual
;
593 gboolean result
= FALSE
;
595 individual
= empathy_roster_contact_get_individual (contact
);
597 groups
= empathy_roster_model_dup_groups_for_individual (
598 self
->priv
->model
, individual
);
600 if (g_list_find_custom (groups
, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
,
601 (GCompareFunc
) g_strcmp0
) != NULL
)
604 g_list_free_full (groups
, g_free
);
609 if (!tp_strdiff (empathy_roster_contact_get_group (contact
),
610 EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
))
611 /* If we are displaying contacts, we only want to *always* display the
612 * RosterContact which is displayed at the top; not the ones displayed in
613 * the 'normal' group sections */
620 compare_roster_contacts_by_alias (EmpathyRosterContact
*a
,
621 EmpathyRosterContact
*b
)
623 FolksIndividual
*ind_a
, *ind_b
;
624 const gchar
*alias_a
, *alias_b
;
626 ind_a
= empathy_roster_contact_get_individual (a
);
627 ind_b
= empathy_roster_contact_get_individual (b
);
629 alias_a
= folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_a
));
630 alias_b
= folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (ind_b
));
632 return g_ascii_strcasecmp (alias_a
, alias_b
);
636 compare_roster_contacts_no_group (EmpathyRosterView
*self
,
637 EmpathyRosterContact
*a
,
638 EmpathyRosterContact
*b
)
640 gboolean top_a
, top_b
;
642 top_a
= contact_in_top (self
, a
);
643 top_b
= contact_in_top (self
, b
);
646 /* Both contacts are in the top of the roster (or not). Sort them
648 return compare_roster_contacts_by_alias (a
, b
);
656 compare_group_names (const gchar
*group_a
,
657 const gchar
*group_b
)
659 if (!tp_strdiff (group_a
, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
))
662 if (!tp_strdiff (group_b
, EMPATHY_ROSTER_MODEL_GROUP_TOP_GROUP
))
665 if (!tp_strdiff (group_a
, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED
))
667 else if (!tp_strdiff (group_b
, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED
))
670 return g_ascii_strcasecmp (group_a
, group_b
);
674 compare_roster_contacts_with_groups (EmpathyRosterView
*self
,
675 EmpathyRosterContact
*a
,
676 EmpathyRosterContact
*b
)
678 const gchar
*group_a
, *group_b
;
680 group_a
= empathy_roster_contact_get_group (a
);
681 group_b
= empathy_roster_contact_get_group (b
);
683 if (!tp_strdiff (group_a
, group_b
))
684 /* Same group, compare the contacts */
685 return compare_roster_contacts_by_alias (a
, b
);
688 return compare_group_names (group_a
, group_b
);
692 compare_roster_contacts (EmpathyRosterView
*self
,
693 EmpathyRosterContact
*a
,
694 EmpathyRosterContact
*b
)
696 if (!self
->priv
->show_groups
)
697 return compare_roster_contacts_no_group (self
, a
, b
);
699 return compare_roster_contacts_with_groups (self
, a
, b
);
703 compare_roster_groups (EmpathyRosterGroup
*a
,
704 EmpathyRosterGroup
*b
)
706 const gchar
*name_a
, *name_b
;
708 name_a
= empathy_roster_group_get_name (a
);
709 name_b
= empathy_roster_group_get_name (b
);
711 return compare_group_names (name_a
, name_b
);
715 compare_contact_group (EmpathyRosterContact
*contact
,
716 EmpathyRosterGroup
*group
)
718 const char *contact_group
, *group_name
;
720 contact_group
= empathy_roster_contact_get_group (contact
);
721 group_name
= empathy_roster_group_get_name (group
);
723 if (!tp_strdiff (contact_group
, group_name
))
724 /* @contact is in @group, @group has to be displayed first */
727 /* @contact is in a different group, sort by group name */
728 return compare_group_names (contact_group
, group_name
);
732 roster_view_sort (gconstpointer a
,
736 EmpathyRosterView
*self
= user_data
;
738 if (EMPATHY_IS_ROSTER_CONTACT (a
) && EMPATHY_IS_ROSTER_CONTACT (b
))
739 return compare_roster_contacts (self
, EMPATHY_ROSTER_CONTACT (a
),
740 EMPATHY_ROSTER_CONTACT (b
));
741 else if (EMPATHY_IS_ROSTER_GROUP (a
) && EMPATHY_IS_ROSTER_GROUP (b
))
742 return compare_roster_groups (EMPATHY_ROSTER_GROUP (a
),
743 EMPATHY_ROSTER_GROUP (b
));
744 else if (EMPATHY_IS_ROSTER_CONTACT (a
) && EMPATHY_IS_ROSTER_GROUP (b
))
745 return compare_contact_group (EMPATHY_ROSTER_CONTACT (a
),
746 EMPATHY_ROSTER_GROUP (b
));
747 else if (EMPATHY_IS_ROSTER_GROUP (a
) && EMPATHY_IS_ROSTER_CONTACT (b
))
748 return -1 * compare_contact_group (EMPATHY_ROSTER_CONTACT (b
),
749 EMPATHY_ROSTER_GROUP (a
));
751 g_return_val_if_reached (0);
755 update_separator (GtkWidget
**separator
,
762 /* No separator before the first row */
763 g_clear_object (separator
);
767 if (*separator
!= NULL
)
770 *separator
= gtk_separator_new (GTK_ORIENTATION_HORIZONTAL
);
771 g_object_ref_sink (*separator
);
775 is_searching (EmpathyRosterView
*self
)
777 if (self
->priv
->search
== NULL
)
780 return gtk_widget_get_visible (GTK_WIDGET (self
->priv
->search
));
784 add_to_displayed (EmpathyRosterView
*self
,
785 EmpathyRosterContact
*contact
)
787 FolksIndividual
*individual
;
788 GHashTable
*contacts
;
792 if (g_hash_table_lookup (self
->priv
->displayed_contacts
, contact
) != NULL
)
795 g_hash_table_add (self
->priv
->displayed_contacts
, contact
);
796 update_empty (self
, FALSE
);
798 /* Groups of this contact may now be displayed if we just displays the first
799 * child in this group. */
801 if (!self
->priv
->show_groups
)
804 individual
= empathy_roster_contact_get_individual (contact
);
805 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
806 if (contacts
== NULL
)
809 g_hash_table_iter_init (&iter
, contacts
);
810 while (g_hash_table_iter_next (&iter
, &k
, NULL
))
812 const gchar
*group_name
= k
;
815 group
= g_hash_table_lookup (self
->priv
->roster_groups
, group_name
);
819 egg_list_box_child_changed (EGG_LIST_BOX (self
), group
);
824 remove_from_displayed (EmpathyRosterView
*self
,
825 EmpathyRosterContact
*contact
)
827 g_hash_table_remove (self
->priv
->displayed_contacts
, contact
);
829 check_if_empty (self
);
833 contact_is_favorite (EmpathyRosterContact
*contact
)
835 FolksIndividual
*individual
;
837 individual
= empathy_roster_contact_get_individual (contact
);
839 return folks_favourite_details_get_is_favourite (
840 FOLKS_FAVOURITE_DETAILS (individual
));
844 * check if @contact should be displayed according to @self's current status
845 * and without consideration for the state of @contact's groups.
848 contact_should_be_displayed (EmpathyRosterView
*self
,
849 EmpathyRosterContact
*contact
)
851 if (is_searching (self
))
853 FolksIndividual
*individual
;
855 individual
= empathy_roster_contact_get_individual (contact
);
857 return empathy_individual_match_string (individual
,
858 empathy_live_search_get_text (self
->priv
->search
),
859 empathy_live_search_get_words (self
->priv
->search
));
862 if (self
->priv
->show_offline
)
865 if (contact_in_top (self
, contact
) &&
866 contact_is_favorite (contact
))
867 /* Favorite top contacts are always displayed */
870 return empathy_roster_contact_is_online (contact
);
875 filter_contact (EmpathyRosterView
*self
,
876 EmpathyRosterContact
*contact
)
880 displayed
= contact_should_be_displayed (self
, contact
);
882 if (self
->priv
->show_groups
)
884 const gchar
*group_name
;
885 EmpathyRosterGroup
*group
;
887 group_name
= empathy_roster_contact_get_group (contact
);
888 group
= lookup_roster_group (self
, group_name
);
892 /* When searching, always display even if the group is closed */
893 if (!is_searching (self
) &&
894 !gtk_expander_get_expanded (GTK_EXPANDER (group
)))
901 add_to_displayed (self
, contact
);
905 remove_from_displayed (self
, contact
);
912 filter_group (EmpathyRosterView
*self
,
913 EmpathyRosterGroup
*group
)
916 gboolean result
= FALSE
;
918 /* Display the group if it contains at least one displayed contact */
919 widgets
= empathy_roster_group_get_widgets (group
);
920 for (l
= widgets
; l
!= NULL
; l
= g_list_next (l
))
922 EmpathyRosterContact
*contact
= l
->data
;
924 if (contact_should_be_displayed (self
, contact
))
931 g_list_free (widgets
);
937 filter_list (GtkWidget
*child
,
940 EmpathyRosterView
*self
= user_data
;
942 if (EMPATHY_IS_ROSTER_CONTACT (child
))
943 return filter_contact (self
, EMPATHY_ROSTER_CONTACT (child
));
945 else if (EMPATHY_IS_ROSTER_GROUP (child
))
946 return filter_group (self
, EMPATHY_ROSTER_GROUP (child
));
948 g_return_val_if_reached (FALSE
);
952 populate_view (EmpathyRosterView
*self
)
954 GList
*individuals
, *l
;
956 individuals
= empathy_roster_model_get_individuals (self
->priv
->model
);
957 for (l
= individuals
; l
!= NULL
; l
= g_list_next (l
))
959 FolksIndividual
*individual
= l
->data
;
961 individual_added (self
, individual
);
964 g_list_free (individuals
);
968 remove_from_group (EmpathyRosterView
*self
,
969 FolksIndividual
*individual
,
972 GHashTable
*contacts
;
974 EmpathyRosterGroup
*roster_group
;
976 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
977 if (contacts
== NULL
)
980 contact
= g_hash_table_lookup (contacts
, group
);
984 g_hash_table_remove (contacts
, group
);
986 if (g_hash_table_size (contacts
) == 0)
988 add_to_group (self
, individual
, EMPATHY_ROSTER_MODEL_GROUP_UNGROUPED
);
991 roster_group
= lookup_roster_group (self
, group
);
993 if (roster_group
!= NULL
)
995 update_group_widgets (self
, roster_group
,
996 EMPATHY_ROSTER_CONTACT (contact
), FALSE
);
999 gtk_container_remove (GTK_CONTAINER (self
), contact
);
1003 groups_changed_cb (EmpathyRosterModel
*model
,
1004 FolksIndividual
*individual
,
1007 EmpathyRosterView
*self
)
1009 if (!self
->priv
->show_groups
)
1011 egg_list_box_resort (EGG_LIST_BOX (self
));
1017 add_to_group (self
, individual
, group
);
1021 remove_from_group (self
, individual
, group
);
1026 empathy_roster_view_constructed (GObject
*object
)
1028 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
1029 void (*chain_up
) (GObject
*) =
1030 ((GObjectClass
*) empathy_roster_view_parent_class
)->constructed
;
1032 if (chain_up
!= NULL
)
1035 g_assert (EMPATHY_IS_ROSTER_MODEL (self
->priv
->model
));
1037 /* Get saved group states. */
1038 empathy_contact_groups_get_all ();
1040 populate_view (self
);
1042 tp_g_signal_connect_object (self
->priv
->model
, "individual-added",
1043 G_CALLBACK (individual_added_cb
), self
, 0);
1044 tp_g_signal_connect_object (self
->priv
->model
, "individual-removed",
1045 G_CALLBACK (individual_removed_cb
), self
, 0);
1046 tp_g_signal_connect_object (self
->priv
->model
, "groups-changed",
1047 G_CALLBACK (groups_changed_cb
), self
, 0);
1049 egg_list_box_set_sort_func (EGG_LIST_BOX (self
),
1050 roster_view_sort
, self
, NULL
);
1052 egg_list_box_set_separator_funcs (EGG_LIST_BOX (self
), update_separator
,
1055 egg_list_box_set_filter_func (EGG_LIST_BOX (self
), filter_list
, self
, NULL
);
1057 egg_list_box_set_activate_on_single_click (EGG_LIST_BOX (self
), FALSE
);
1061 clear_view (EmpathyRosterView
*self
)
1063 g_hash_table_remove_all (self
->priv
->roster_contacts
);
1064 g_hash_table_remove_all (self
->priv
->roster_groups
);
1065 g_hash_table_remove_all (self
->priv
->displayed_contacts
);
1067 gtk_container_foreach (GTK_CONTAINER (self
),
1068 (GtkCallback
) gtk_widget_destroy
, NULL
);
1072 empathy_roster_view_dispose (GObject
*object
)
1074 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
1075 void (*chain_up
) (GObject
*) =
1076 ((GObjectClass
*) empathy_roster_view_parent_class
)->dispose
;
1078 /* Start by clearing the view so our internal hash tables are cleared from
1079 * objects being destroyed. */
1082 stop_flashing (self
);
1084 empathy_roster_view_set_live_search (self
, NULL
);
1085 g_clear_object (&self
->priv
->model
);
1087 if (self
->priv
->search_id
!= 0)
1089 g_source_remove (self
->priv
->search_id
);
1090 self
->priv
->search_id
= 0;
1093 if (chain_up
!= NULL
)
1098 empathy_roster_view_finalize (GObject
*object
)
1100 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (object
);
1101 void (*chain_up
) (GObject
*) =
1102 ((GObjectClass
*) empathy_roster_view_parent_class
)->finalize
;
1104 g_hash_table_unref (self
->priv
->roster_contacts
);
1105 g_hash_table_unref (self
->priv
->roster_groups
);
1106 g_hash_table_unref (self
->priv
->displayed_contacts
);
1107 g_queue_free_full (self
->priv
->events
, event_free
);
1109 if (chain_up
!= NULL
)
1114 empathy_roster_view_child_activated (EggListBox
*box
,
1117 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (box
);
1118 EmpathyRosterContact
*contact
;
1119 FolksIndividual
*individual
;
1122 if (!EMPATHY_IS_ROSTER_CONTACT (child
))
1125 contact
= EMPATHY_ROSTER_CONTACT (child
);
1126 individual
= empathy_roster_contact_get_individual (contact
);
1128 /* Activate the oldest event associated with this contact, if any */
1129 for (l
= g_queue_peek_tail_link (self
->priv
->events
); l
!= NULL
;
1130 l
= g_list_previous (l
))
1132 Event
*event
= l
->data
;
1134 if (event
->individual
== individual
)
1136 g_signal_emit (box
, signals
[SIG_EVENT_ACTIVATED
], 0, individual
,
1142 g_signal_emit (box
, signals
[SIG_INDIVIDUAL_ACTIVATED
], 0, individual
);
1146 fire_popup_individual_menu (EmpathyRosterView
*self
,
1151 EmpathyRosterContact
*contact
;
1152 FolksIndividual
*individual
;
1154 if (!EMPATHY_IS_ROSTER_CONTACT (child
))
1157 contact
= EMPATHY_ROSTER_CONTACT (child
);
1158 individual
= empathy_roster_contact_get_individual (contact
);
1160 g_signal_emit (self
, signals
[SIG_POPUP_INDIVIDUAL_MENU
], 0,
1161 individual
, button
, time
);
1165 empathy_roster_view_button_press_event (GtkWidget
*widget
,
1166 GdkEventButton
*event
)
1168 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (widget
);
1169 gboolean (*chain_up
) (GtkWidget
*, GdkEventButton
*) =
1170 ((GtkWidgetClass
*) empathy_roster_view_parent_class
)->button_press_event
;
1172 if (event
->button
== 3)
1176 child
= egg_list_box_get_child_at_y (EGG_LIST_BOX (self
), event
->y
);
1180 egg_list_box_select_child (EGG_LIST_BOX (self
), child
);
1182 fire_popup_individual_menu (self
, child
, event
->button
, event
->time
);
1186 return chain_up (widget
, event
);
1190 empathy_roster_view_key_press_event (GtkWidget
*widget
,
1193 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (widget
);
1194 gboolean (*chain_up
) (GtkWidget
*, GdkEventKey
*) =
1195 ((GtkWidgetClass
*) empathy_roster_view_parent_class
)->key_press_event
;
1197 if (event
->keyval
== GDK_KEY_Menu
)
1201 child
= egg_list_box_get_selected_child (EGG_LIST_BOX (self
));
1204 fire_popup_individual_menu (self
, child
, 0, event
->time
);
1207 return chain_up (widget
, event
);
1211 * @out_child: (out) (allow-none)
1214 empathy_roster_view_get_individual_at_y (EmpathyRosterView
*self
,
1216 GtkWidget
**out_child
)
1220 child
= egg_list_box_get_child_at_y (EGG_LIST_BOX (self
), y
);
1222 if (out_child
!= NULL
)
1225 if (!EMPATHY_IS_ROSTER_CONTACT (child
))
1228 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child
));
1232 * @out_child: (out) (allow-none)
1235 empathy_roster_view_get_group_at_y (EmpathyRosterView
*self
,
1240 child
= egg_list_box_get_child_at_y (EGG_LIST_BOX (self
), y
);
1242 if (EMPATHY_IS_ROSTER_CONTACT (child
))
1243 return empathy_roster_contact_get_group (EMPATHY_ROSTER_CONTACT (child
));
1244 else if (EMPATHY_IS_ROSTER_GROUP (child
))
1245 return empathy_roster_group_get_name (EMPATHY_ROSTER_GROUP (child
));
1251 empathy_roster_view_query_tooltip (GtkWidget
*widget
,
1254 gboolean keyboard_mode
,
1255 GtkTooltip
*tooltip
)
1257 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (widget
);
1258 FolksIndividual
*individual
;
1262 individual
= empathy_roster_view_get_individual_at_y (self
, y
, &child
);
1263 if (individual
== NULL
)
1266 g_signal_emit (self
, signals
[SIG_INDIVIDUAL_TOOLTIP
], 0,
1267 individual
, keyboard_mode
, tooltip
, &result
);
1271 GtkAllocation allocation
;
1273 gtk_widget_get_allocation (child
, &allocation
);
1274 gtk_tooltip_set_tip_area (tooltip
, (GdkRectangle
*) &allocation
);
1281 empathy_roster_view_remove (GtkContainer
*container
,
1284 EmpathyRosterView
*self
= EMPATHY_ROSTER_VIEW (container
);
1285 void (*chain_up
) (GtkContainer
*, GtkWidget
*) =
1286 ((GtkContainerClass
*) empathy_roster_view_parent_class
)->remove
;
1288 chain_up (container
, widget
);
1290 if (EMPATHY_IS_ROSTER_CONTACT (widget
))
1291 remove_from_displayed (self
, (EmpathyRosterContact
*) widget
);
1295 empathy_roster_view_class_init (
1296 EmpathyRosterViewClass
*klass
)
1298 GObjectClass
*oclass
= G_OBJECT_CLASS (klass
);
1299 EggListBoxClass
*box_class
= EGG_LIST_BOX_CLASS (klass
);
1300 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS (klass
);
1301 GtkContainerClass
*container_class
= GTK_CONTAINER_CLASS (klass
);
1304 oclass
->get_property
= empathy_roster_view_get_property
;
1305 oclass
->set_property
= empathy_roster_view_set_property
;
1306 oclass
->constructed
= empathy_roster_view_constructed
;
1307 oclass
->dispose
= empathy_roster_view_dispose
;
1308 oclass
->finalize
= empathy_roster_view_finalize
;
1310 widget_class
->button_press_event
= empathy_roster_view_button_press_event
;
1311 widget_class
->key_press_event
= empathy_roster_view_key_press_event
;
1312 widget_class
->query_tooltip
= empathy_roster_view_query_tooltip
;
1314 container_class
->remove
= empathy_roster_view_remove
;
1316 box_class
->child_activated
= empathy_roster_view_child_activated
;
1318 spec
= g_param_spec_object ("model", "Model",
1319 "EmpathyRosterModel",
1320 EMPATHY_TYPE_ROSTER_MODEL
,
1321 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
| G_PARAM_STATIC_STRINGS
);
1322 g_object_class_install_property (oclass
, PROP_MODEL
, spec
);
1324 spec
= g_param_spec_boolean ("show-offline", "Show Offline",
1325 "Show offline contacts",
1327 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
1328 g_object_class_install_property (oclass
, PROP_SHOW_OFFLINE
, spec
);
1330 spec
= g_param_spec_boolean ("show-groups", "Show Groups",
1333 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
1334 g_object_class_install_property (oclass
, PROP_SHOW_GROUPS
, spec
);
1336 spec
= g_param_spec_boolean ("empty", "Empty",
1337 "Is the view currently empty?",
1339 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
);
1340 g_object_class_install_property (oclass
, PROP_EMPTY
, spec
);
1342 signals
[SIG_INDIVIDUAL_ACTIVATED
] = g_signal_new ("individual-activated",
1343 G_OBJECT_CLASS_TYPE (klass
),
1345 0, NULL
, NULL
, NULL
,
1347 1, FOLKS_TYPE_INDIVIDUAL
);
1349 signals
[SIG_POPUP_INDIVIDUAL_MENU
] = g_signal_new ("popup-individual-menu",
1350 G_OBJECT_CLASS_TYPE (klass
),
1352 0, NULL
, NULL
, NULL
,
1354 3, FOLKS_TYPE_INDIVIDUAL
, G_TYPE_UINT
, G_TYPE_UINT
);
1356 signals
[SIG_EVENT_ACTIVATED
] = g_signal_new ("event-activated",
1357 G_OBJECT_CLASS_TYPE (klass
),
1359 0, NULL
, NULL
, NULL
,
1361 2, FOLKS_TYPE_INDIVIDUAL
, G_TYPE_POINTER
);
1363 signals
[SIG_INDIVIDUAL_TOOLTIP
] = g_signal_new ("individual-tooltip",
1364 G_OBJECT_CLASS_TYPE (klass
),
1366 0, g_signal_accumulator_true_handled
, NULL
, NULL
,
1368 3, FOLKS_TYPE_INDIVIDUAL
, G_TYPE_BOOLEAN
, GTK_TYPE_TOOLTIP
);
1370 g_type_class_add_private (klass
, sizeof (EmpathyRosterViewPriv
));
1374 empathy_roster_view_init (EmpathyRosterView
*self
)
1376 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
1377 EMPATHY_TYPE_ROSTER_VIEW
, EmpathyRosterViewPriv
);
1379 self
->priv
->roster_contacts
= g_hash_table_new_full (NULL
, NULL
,
1380 NULL
, (GDestroyNotify
) g_hash_table_unref
);
1381 self
->priv
->roster_groups
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
1383 self
->priv
->displayed_contacts
= g_hash_table_new (NULL
, NULL
);
1385 self
->priv
->events
= g_queue_new ();
1387 self
->priv
->empty
= TRUE
;
1391 empathy_roster_view_new (EmpathyRosterModel
*model
)
1393 g_return_val_if_fail (EMPATHY_IS_ROSTER_MODEL (model
), NULL
);
1395 return g_object_new (EMPATHY_TYPE_ROSTER_VIEW
,
1401 empathy_roster_view_show_offline (EmpathyRosterView
*self
,
1404 if (self
->priv
->show_offline
== show
)
1407 self
->priv
->show_offline
= show
;
1408 egg_list_box_refilter (EGG_LIST_BOX (self
));
1410 g_object_notify (G_OBJECT (self
), "show-offline");
1414 empathy_roster_view_show_groups (EmpathyRosterView
*self
,
1417 if (self
->priv
->show_groups
== show
)
1420 self
->priv
->show_groups
= show
;
1422 /* TODO: block sort/filter? */
1424 populate_view (self
);
1426 g_object_notify (G_OBJECT (self
), "show-groups");
1430 select_first_contact (EmpathyRosterView
*self
)
1432 GList
*children
, *l
;
1434 children
= gtk_container_get_children (GTK_CONTAINER (self
));
1435 for (l
= children
; l
!= NULL
; l
= g_list_next (l
))
1437 GtkWidget
*child
= l
->data
;
1439 if (!gtk_widget_get_child_visible (child
))
1442 if (!EMPATHY_IS_ROSTER_CONTACT (child
))
1445 egg_list_box_select_child (EGG_LIST_BOX (self
), child
);
1449 g_list_free (children
);
1453 search_timeout_cb (EmpathyRosterView
*self
)
1455 egg_list_box_refilter (EGG_LIST_BOX (self
));
1457 select_first_contact (self
);
1459 self
->priv
->search_id
= 0;
1460 return G_SOURCE_REMOVE
;
1464 search_text_notify_cb (EmpathyLiveSearch
*search
,
1466 EmpathyRosterView
*self
)
1468 if (self
->priv
->search_id
!= 0)
1469 g_source_remove (self
->priv
->search_id
);
1471 self
->priv
->search_id
= g_timeout_add (SEARCH_TIMEOUT
,
1472 (GSourceFunc
) search_timeout_cb
, self
);
1476 search_activate_cb (GtkWidget
*search
,
1477 EmpathyRosterView
*self
)
1479 EggListBox
*box
= EGG_LIST_BOX (self
);
1482 child
= egg_list_box_get_selected_child (box
);
1486 empathy_roster_view_child_activated (box
, child
);
1490 empathy_roster_view_set_live_search (EmpathyRosterView
*self
,
1491 EmpathyLiveSearch
*search
)
1493 if (self
->priv
->search
!= NULL
)
1495 g_signal_handlers_disconnect_by_func (self
->priv
->search
,
1496 search_text_notify_cb
, self
);
1497 g_signal_handlers_disconnect_by_func (self
->priv
->search
,
1498 search_activate_cb
, self
);
1500 g_clear_object (&self
->priv
->search
);
1506 self
->priv
->search
= g_object_ref (search
);
1508 g_signal_connect (self
->priv
->search
, "notify::text",
1509 G_CALLBACK (search_text_notify_cb
), self
);
1510 g_signal_connect (self
->priv
->search
, "activate",
1511 G_CALLBACK (search_activate_cb
), self
);
1515 empathy_roster_view_is_empty (EmpathyRosterView
*self
)
1517 return self
->priv
->empty
;
1521 empathy_roster_view_is_searching (EmpathyRosterView
*self
)
1523 return (self
->priv
->search
!= NULL
&&
1524 gtk_widget_get_visible (GTK_WIDGET (self
->priv
->search
)));
1527 /* Don't use EmpathyEvent as I prefer to keep this object not too specific to
1528 * Empathy's internals. */
1530 empathy_roster_view_add_event (EmpathyRosterView
*self
,
1531 FolksIndividual
*individual
,
1535 GHashTable
*contacts
;
1537 contacts
= g_hash_table_lookup (self
->priv
->roster_contacts
, individual
);
1538 if (contacts
== NULL
)
1541 self
->priv
->last_event_id
++;
1543 g_queue_push_head (self
->priv
->events
,
1544 event_new (self
->priv
->last_event_id
, individual
, icon
, user_data
));
1546 start_flashing (self
);
1548 return self
->priv
->last_event_id
;
1552 empathy_roster_view_remove_event (EmpathyRosterView
*self
,
1557 for (l
= g_queue_peek_head_link (self
->priv
->events
); l
!= NULL
;
1558 l
= g_list_next (l
))
1560 Event
*event
= l
->data
;
1562 if (event
->id
== event_id
)
1564 remove_event (self
, event
);
1571 empathy_roster_view_get_selected_individual (EmpathyRosterView
*self
)
1575 child
= egg_list_box_get_selected_child (EGG_LIST_BOX (self
));
1577 if (!EMPATHY_IS_ROSTER_CONTACT (child
))
1580 return empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child
));