1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
28 #include <glib/gi18n.h>
30 #include <glade/glade.h>
32 #include <libmissioncontrol/mc-account.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/gossip-debug.h>
36 #include <libempathy/empathy-session.h>
38 #include "gossip-contact-list.h"
39 #include "gossip-contact-groups.h"
40 #include "gossip-cell-renderer-expander.h"
41 #include "gossip-cell-renderer-text.h"
42 #include "gossip-stock.h"
43 #include "gossip-ui-utils.h"
44 //#include "gossip-chat-invite.h"
45 //#include "gossip-contact-info-dialog.h"
46 //#include "gossip-edit-contact-dialog.h"
47 //#include "gossip-ft-window.h"
48 //#include "gossip-log-window.h"
50 #define DEBUG_DOMAIN "ContactListUI"
52 /* Flashing delay for icons (milliseconds). */
53 #define FLASH_TIMEOUT 500
55 /* Active users are those which have recently changed state
56 * (e.g. online, offline or from normal to a busy state).
59 /* Time user is shown as active */
60 #define ACTIVE_USER_SHOW_TIME 7000
62 /* Time after connecting which we wait before active users are enabled */
63 #define ACTIVE_USER_WAIT_TO_ENABLE_TIME 5000
65 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CONTACT_LIST, GossipContactListPriv))
67 struct _GossipContactListPriv {
68 EmpathyContactManager *manager;
73 GtkTreeRowReference *drag_row;
79 gboolean show_offline;
80 gboolean show_avatars;
92 GossipContact *contact;
98 GossipContactList *list;
104 GossipContactList *list;
105 GossipContact *contact;
109 static void gossip_contact_list_class_init (GossipContactListClass *klass);
110 static void gossip_contact_list_init (GossipContactList *list);
111 static void contact_list_finalize (GObject *object);
112 static void contact_list_get_property (GObject *object,
116 static void contact_list_set_property (GObject *object,
120 static gboolean contact_list_row_separator_func (GtkTreeModel *model,
123 static void contact_list_contact_update (GossipContactList *list,
124 GossipContact *contact);
125 static void contact_list_contact_added_cb (EmpathyContactManager *manager,
126 GossipContact *contact,
127 GossipContactList *list);
128 static void contact_list_contact_updated_cb (GossipContact *contact,
130 GossipContactList *list);
131 static void contact_list_contact_groups_updated_cb (GossipContact *contact,
133 GossipContactList *list);
134 static void contact_list_contact_removed_cb (EmpathyContactManager *manager,
135 GossipContact *contact,
136 GossipContactList *list);
137 static void contact_list_contact_set_active (GossipContactList *list,
138 GossipContact *contact,
140 gboolean set_changed);
141 static ShowActiveData *
142 contact_list_contact_active_new (GossipContactList *list,
143 GossipContact *contact,
145 static void contact_list_contact_active_free (ShowActiveData *data);
146 static gboolean contact_list_contact_active_cb (ShowActiveData *data);
147 static gchar * contact_list_get_parent_group (GtkTreeModel *model,
149 gboolean *path_is_group);
150 static void contact_list_get_group (GossipContactList *list,
152 GtkTreeIter *iter_group_to_set,
153 GtkTreeIter *iter_separator_to_set,
155 static gboolean contact_list_get_group_foreach (GtkTreeModel *model,
159 static void contact_list_add_contact (GossipContactList *list,
160 GossipContact *contact);
161 static void contact_list_remove_contact (GossipContactList *list,
162 GossipContact *contact);
163 static void contact_list_create_model (GossipContactList *list);
164 static gboolean contact_list_search_equal_func (GtkTreeModel *model,
168 gpointer search_data);
169 static void contact_list_setup_view (GossipContactList *list);
170 static void contact_list_drag_data_received (GtkWidget *widget,
171 GdkDragContext *context,
174 GtkSelectionData *selection,
178 static gboolean contact_list_drag_motion (GtkWidget *widget,
179 GdkDragContext *context,
184 static gboolean contact_list_drag_motion_cb (DragMotionData *data);
185 static void contact_list_drag_begin (GtkWidget *widget,
186 GdkDragContext *context,
188 static void contact_list_drag_data_get (GtkWidget *widget,
189 GdkDragContext *contact,
190 GtkSelectionData *selection,
194 static void contact_list_drag_end (GtkWidget *widget,
195 GdkDragContext *context,
197 static void contact_list_cell_set_background (GossipContactList *list,
198 GtkCellRenderer *cell,
201 static void contact_list_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
202 GtkCellRenderer *cell,
205 GossipContactList *list);
206 static void contact_list_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
207 GtkCellRenderer *cell,
210 GossipContactList *list);
211 static void contact_list_text_cell_data_func (GtkTreeViewColumn *tree_column,
212 GtkCellRenderer *cell,
215 GossipContactList *list);
216 static void contact_list_expander_cell_data_func (GtkTreeViewColumn *column,
217 GtkCellRenderer *cell,
220 GossipContactList *list);
221 static GtkWidget *contact_list_get_contact_menu (GossipContactList *list,
222 gboolean can_send_file,
223 gboolean can_show_log);
224 static gboolean contact_list_button_press_event_cb (GossipContactList *list,
225 GdkEventButton *event,
227 static void contact_list_row_activated_cb (GossipContactList *list,
229 GtkTreeViewColumn *col,
231 static void contact_list_row_expand_or_collapse_cb (GossipContactList *list,
235 static gint contact_list_sort_func (GtkTreeModel *model,
239 static gboolean contact_list_filter_func (GtkTreeModel *model,
241 GossipContactList *list);
242 static GList * contact_list_find_contact (GossipContactList *list,
243 GossipContact *contact);
244 static gboolean contact_list_find_contact_foreach (GtkTreeModel *model,
248 static void contact_list_action_cb (GtkAction *action,
249 GossipContactList *list);
250 static gboolean contact_list_update_list_mode_foreach (GtkTreeModel *model,
253 GossipContactList *list);
266 static guint signals[LAST_SIGNAL];
271 COL_PIXBUF_AVATAR_VISIBLE,
291 static const GtkActionEntry entries[] = {
292 { "ContactMenu", NULL,
293 N_("_Contact"), NULL, NULL,
297 N_("_Group"),NULL, NULL,
300 { "Chat", GOSSIP_STOCK_MESSAGE,
301 N_("_Chat"), NULL, N_("Chat with contact"),
302 G_CALLBACK (contact_list_action_cb)
304 { "Information", GOSSIP_STOCK_CONTACT_INFORMATION,
305 N_("Infor_mation"), "<control>I", N_("View contact information"),
306 G_CALLBACK (contact_list_action_cb)
309 N_("Re_name"), NULL, N_("Rename"),
310 G_CALLBACK (contact_list_action_cb)
312 { "Edit", GTK_STOCK_EDIT,
313 N_("_Edit"), NULL, N_("Edit the groups and name for this contact"),
314 G_CALLBACK (contact_list_action_cb)
316 { "Remove", GTK_STOCK_REMOVE,
317 N_("_Remove"), NULL, N_("Remove contact"),
318 G_CALLBACK (contact_list_action_cb)
320 { "Invite", GOSSIP_STOCK_GROUP_MESSAGE,
321 N_("_Invite to Chat Room"), NULL, N_("Invite to a currently open chat room"),
322 G_CALLBACK (contact_list_action_cb)
325 N_("_Send File..."), NULL, N_("Send a file"),
326 G_CALLBACK (contact_list_action_cb)
328 { "Log", GTK_STOCK_JUSTIFY_LEFT,
329 N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
330 G_CALLBACK (contact_list_action_cb)
334 static guint n_entries = G_N_ELEMENTS (entries);
336 static const gchar *ui_info =
338 " <popup name='Contact'>"
339 " <menuitem action='Chat'/>"
340 " <menuitem action='Log'/>"
341 " <menuitem action='SendFile'/>"
343 " <menuitem action='Invite'/>"
345 " <menuitem action='Edit'/>"
346 " <menuitem action='Remove'/>"
348 " <menuitem action='Information'/>"
350 " <popup name='Group'>"
351 " <menuitem action='Rename'/>"
356 DND_DRAG_TYPE_CONTACT_ID,
358 DND_DRAG_TYPE_STRING,
361 static const GtkTargetEntry drag_types_dest[] = {
362 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
363 { "text/uri-list", 0, DND_DRAG_TYPE_URL },
364 { "text/plain", 0, DND_DRAG_TYPE_STRING },
365 { "STRING", 0, DND_DRAG_TYPE_STRING },
368 static const GtkTargetEntry drag_types_source[] = {
369 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
372 static GdkAtom drag_atoms_dest[G_N_ELEMENTS (drag_types_dest)];
373 static GdkAtom drag_atoms_source[G_N_ELEMENTS (drag_types_source)];
375 G_DEFINE_TYPE (GossipContactList, gossip_contact_list, GTK_TYPE_TREE_VIEW);
378 gossip_contact_list_class_init (GossipContactListClass *klass)
380 GObjectClass *object_class = G_OBJECT_CLASS (klass);
382 object_class->finalize = contact_list_finalize;
383 object_class->get_property = contact_list_get_property;
384 object_class->set_property = contact_list_set_property;
386 signals[CONTACT_CHAT] =
387 g_signal_new ("contact-chat",
388 G_TYPE_FROM_CLASS (klass),
392 g_cclosure_marshal_VOID__OBJECT,
394 1, GOSSIP_TYPE_CONTACT);
395 signals[CONTACT_INFORMATION] =
396 g_signal_new ("contact-information",
397 G_TYPE_FROM_CLASS (klass),
401 g_cclosure_marshal_VOID__OBJECT,
403 1, GOSSIP_TYPE_CONTACT);
404 signals[CONTACT_EDIT] =
405 g_signal_new ("contact-edit",
406 G_TYPE_FROM_CLASS (klass),
410 g_cclosure_marshal_VOID__OBJECT,
412 1, GOSSIP_TYPE_CONTACT);
413 signals[CONTACT_REMOVE] =
414 g_signal_new ("contact-remove",
415 G_TYPE_FROM_CLASS (klass),
419 g_cclosure_marshal_VOID__OBJECT,
421 1, GOSSIP_TYPE_CONTACT);
422 signals[CONTACT_INVITE] =
423 g_signal_new ("contact-invite",
424 G_TYPE_FROM_CLASS (klass),
428 g_cclosure_marshal_VOID__OBJECT,
430 1, GOSSIP_TYPE_CONTACT);
431 signals[CONTACT_SEND_FILE] =
432 g_signal_new ("contact-send-file",
433 G_TYPE_FROM_CLASS (klass),
437 g_cclosure_marshal_VOID__OBJECT,
439 1, GOSSIP_TYPE_CONTACT);
440 signals[CONTACT_LOG] =
441 g_signal_new ("contact-log",
442 G_TYPE_FROM_CLASS (klass),
446 g_cclosure_marshal_VOID__OBJECT,
448 1, GOSSIP_TYPE_CONTACT);
449 signals[GROUP_RENAME] =
450 g_signal_new ("group-rename",
451 G_TYPE_FROM_CLASS (klass),
455 g_cclosure_marshal_VOID__STRING,
460 g_object_class_install_property (object_class,
462 g_param_spec_boolean ("show-offline",
464 "Whether contact list should display "
468 g_object_class_install_property (object_class,
470 g_param_spec_boolean ("show-avatars",
472 "Whether contact list should display "
473 "avatars for contacts",
476 g_object_class_install_property (object_class,
478 g_param_spec_boolean ("is-compact",
480 "Whether the contact list is in compact mode or not",
484 g_object_class_install_property (object_class,
486 g_param_spec_string ("filter",
488 "The text to use to filter the contact list",
492 g_type_class_add_private (object_class, sizeof (GossipContactListPriv));
496 gossip_contact_list_init (GossipContactList *list)
498 GossipContactListPriv *priv;
499 GtkActionGroup *action_group;
501 GError *error = NULL;
503 priv = GET_PRIV (list);
505 priv->manager = empathy_session_get_contact_manager ();
506 g_object_ref (priv->manager);
507 priv->is_compact = FALSE;
508 priv->show_active = TRUE;
509 priv->show_avatars = TRUE;
511 contact_list_create_model (list);
512 contact_list_setup_view (list);
513 empathy_contact_manager_setup (priv->manager);
515 /* Get saved group states. */
516 gossip_contact_groups_get_all ();
518 /* Set up UI Manager */
519 priv->ui = gtk_ui_manager_new ();
521 action_group = gtk_action_group_new ("Actions");
522 gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
523 gtk_action_group_add_actions (action_group, entries, n_entries, list);
524 gtk_ui_manager_insert_action_group (priv->ui, action_group, 0);
526 if (!gtk_ui_manager_add_ui_from_string (priv->ui, ui_info, -1, &error)) {
527 g_warning ("Could not build contact menus from string:'%s'", error->message);
528 g_error_free (error);
531 g_object_unref (action_group);
533 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (list),
534 contact_list_row_separator_func,
537 /* Signal connection. */
538 g_signal_connect (priv->manager,
540 G_CALLBACK (contact_list_contact_added_cb),
542 g_signal_connect (priv->manager,
544 G_CALLBACK (contact_list_contact_removed_cb),
547 /* Connect to tree view signals rather than override. */
548 g_signal_connect (list,
549 "button-press-event",
550 G_CALLBACK (contact_list_button_press_event_cb),
552 g_signal_connect (list,
554 G_CALLBACK (contact_list_row_activated_cb),
556 g_signal_connect (list,
558 G_CALLBACK (contact_list_row_expand_or_collapse_cb),
559 GINT_TO_POINTER (TRUE));
560 g_signal_connect (list,
562 G_CALLBACK (contact_list_row_expand_or_collapse_cb),
563 GINT_TO_POINTER (FALSE));
565 /* Add contacts already created */
566 contacts = empathy_contact_manager_get_contacts (priv->manager);
567 for (l = contacts; l; l = l->next) {
568 GossipContact *contact;
572 contact_list_contact_added_cb (priv->manager, contact, list);
574 g_object_unref (contact);
576 g_list_free (contacts);
580 contact_list_finalize (GObject *object)
582 GossipContactListPriv *priv;
584 priv = GET_PRIV (object);
586 /* FIXME: disconnect all signals on the manager and contacts */
588 g_object_unref (priv->manager);
589 g_object_unref (priv->ui);
590 g_object_unref (priv->store);
591 g_object_unref (priv->filter);
592 g_free (priv->filter_text);
594 G_OBJECT_CLASS (gossip_contact_list_parent_class)->finalize (object);
598 contact_list_get_property (GObject *object,
603 GossipContactListPriv *priv;
605 priv = GET_PRIV (object);
608 case PROP_SHOW_OFFLINE:
609 g_value_set_boolean (value, priv->show_offline);
611 case PROP_SHOW_AVATARS:
612 g_value_set_boolean (value, priv->show_avatars);
614 case PROP_IS_COMPACT:
615 g_value_set_boolean (value, priv->is_compact);
618 g_value_set_string (value, priv->filter_text);
621 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
627 contact_list_set_property (GObject *object,
632 GossipContactListPriv *priv;
634 priv = GET_PRIV (object);
637 case PROP_SHOW_OFFLINE:
638 gossip_contact_list_set_show_offline (GOSSIP_CONTACT_LIST (object),
639 g_value_get_boolean (value));
641 case PROP_SHOW_AVATARS:
642 gossip_contact_list_set_show_avatars (GOSSIP_CONTACT_LIST (object),
643 g_value_get_boolean (value));
645 case PROP_IS_COMPACT:
646 gossip_contact_list_set_is_compact (GOSSIP_CONTACT_LIST (object),
647 g_value_get_boolean (value));
650 gossip_contact_list_set_filter (GOSSIP_CONTACT_LIST (object),
651 g_value_get_string (value));
654 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
660 contact_list_row_separator_func (GtkTreeModel *model,
664 gboolean is_separator = FALSE;
666 gtk_tree_model_get (model, iter,
667 COL_IS_SEPARATOR, &is_separator,
674 contact_list_contact_update (GossipContactList *list,
675 GossipContact *contact)
677 GossipContactListPriv *priv;
678 ShowActiveData *data;
682 gboolean should_be_in_list;
683 gboolean was_online = TRUE;
684 gboolean now_online = FALSE;
685 gboolean set_model = FALSE;
686 gboolean do_remove = FALSE;
687 gboolean do_set_active = FALSE;
688 gboolean do_set_refresh = FALSE;
689 GdkPixbuf *pixbuf_presence;
690 GdkPixbuf *pixbuf_avatar;
692 priv = GET_PRIV (list);
694 model = GTK_TREE_MODEL (priv->store);
696 iters = contact_list_find_contact (list, contact);
703 /* Get online state now. */
704 now_online = gossip_contact_is_online (contact);
706 if (priv->show_offline || now_online) {
707 should_be_in_list = TRUE;
709 should_be_in_list = FALSE;
712 if (!in_list && !should_be_in_list) {
714 gossip_debug (DEBUG_DOMAIN,
715 "Contact:'%s' in list:NO, should be:NO",
716 gossip_contact_get_name (contact));
718 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
722 else if (in_list && !should_be_in_list) {
723 gossip_debug (DEBUG_DOMAIN,
724 "Contact:'%s' in list:YES, should be:NO",
725 gossip_contact_get_name (contact));
727 if (priv->show_active) {
729 do_set_active = TRUE;
730 do_set_refresh = TRUE;
733 gossip_debug (DEBUG_DOMAIN, "Remove item (after timeout)");
735 gossip_debug (DEBUG_DOMAIN, "Remove item (now)!");
736 contact_list_remove_contact (list, contact);
739 else if (!in_list && should_be_in_list) {
740 gossip_debug (DEBUG_DOMAIN,
741 "Contact:'%s' in list:NO, should be:YES",
742 gossip_contact_get_name (contact));
744 contact_list_add_contact (list, contact);
746 if (priv->show_active) {
747 do_set_active = TRUE;
749 gossip_debug (DEBUG_DOMAIN, "Set active (contact added)");
752 gossip_debug (DEBUG_DOMAIN,
753 "Contact:'%s' in list:YES, should be:YES",
754 gossip_contact_get_name (contact));
756 /* Get online state before. */
757 if (iters && g_list_length (iters) > 0) {
758 gtk_tree_model_get (model, iters->data, COL_IS_ONLINE, &was_online, -1);
761 /* Is this really an update or an online/offline. */
762 if (priv->show_active) {
763 if (was_online != now_online) {
766 do_set_active = TRUE;
767 do_set_refresh = TRUE;
770 str = "online -> offline";
772 str = "offline -> online";
775 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated %s)", str);
777 /* Was TRUE for presence updates. */
778 /* do_set_active = FALSE; */
779 do_set_refresh = TRUE;
781 gossip_debug (DEBUG_DOMAIN, "Set active (contact updated)");
788 pixbuf_presence = gossip_pixbuf_for_contact (contact);
789 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
790 for (l = iters; l && set_model; l = l->next) {
791 gtk_tree_store_set (priv->store, l->data,
792 COL_PIXBUF_STATUS, pixbuf_presence,
793 COL_STATUS, gossip_contact_get_status (contact),
794 COL_IS_ONLINE, now_online,
795 COL_NAME, gossip_contact_get_name (contact),
796 COL_PIXBUF_AVATAR, pixbuf_avatar,
800 if (pixbuf_presence) {
801 g_object_unref (pixbuf_presence);
804 g_object_unref (pixbuf_avatar);
807 if (priv->show_active && do_set_active) {
808 contact_list_contact_set_active (list, contact, do_set_active, do_set_refresh);
811 data = contact_list_contact_active_new (list, contact, do_remove);
812 g_timeout_add (ACTIVE_USER_SHOW_TIME,
813 (GSourceFunc) contact_list_contact_active_cb,
818 /* FIXME: when someone goes online then offline quickly, the
819 * first timeout sets the user to be inactive and the second
820 * timeout removes the user from the contact list, really we
821 * should remove the first timeout.
823 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
828 contact_list_contact_added_cb (EmpathyContactManager *manager,
829 GossipContact *contact,
830 GossipContactList *list)
832 GossipContactListPriv *priv;
834 priv = GET_PRIV (list);
836 gossip_debug (DEBUG_DOMAIN,
837 "Contact:'%s' added",
838 gossip_contact_get_name (contact));
840 g_signal_connect (contact, "notify::groups",
841 G_CALLBACK (contact_list_contact_groups_updated_cb),
843 g_signal_connect (contact, "notify::presence",
844 G_CALLBACK (contact_list_contact_updated_cb),
846 g_signal_connect (contact, "notify::name",
847 G_CALLBACK (contact_list_contact_updated_cb),
849 g_signal_connect (contact, "notify::avatar",
850 G_CALLBACK (contact_list_contact_updated_cb),
852 g_signal_connect (contact, "notify::type",
853 G_CALLBACK (contact_list_contact_updated_cb),
856 contact_list_add_contact (list, contact);
860 contact_list_contact_groups_updated_cb (GossipContact *contact,
862 GossipContactList *list)
864 GossipContactListPriv *priv;
866 priv = GET_PRIV (list);
868 if (priv->show_offline || gossip_contact_is_online (contact)) {
872 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' groups updated",
873 gossip_contact_get_name (contact));
875 /* We do this to make sure the groups are correct, if not, we
876 * would have to check the groups already set up for each
877 * contact and then see what has been updated.
879 contact_list_remove_contact (list, contact);
880 contact_list_add_contact (list, contact);
884 contact_list_contact_updated_cb (GossipContact *contact,
886 GossipContactList *list)
888 gossip_debug (DEBUG_DOMAIN,
889 "Contact:'%s' updated, checking roster is in sync...",
890 gossip_contact_get_name (contact));
892 contact_list_contact_update (list, contact);
896 contact_list_contact_removed_cb (EmpathyContactManager *manager,
897 GossipContact *contact,
898 GossipContactList *list)
900 gossip_debug (DEBUG_DOMAIN, "Contact:'%s' removed",
901 gossip_contact_get_name (contact));
903 /* Disconnect signals */
904 g_signal_handlers_disconnect_by_func (contact,
905 G_CALLBACK (contact_list_contact_groups_updated_cb),
907 g_signal_handlers_disconnect_by_func (contact,
908 G_CALLBACK (contact_list_contact_updated_cb),
911 contact_list_remove_contact (list, contact);
915 contact_list_contact_set_active (GossipContactList *list,
916 GossipContact *contact,
918 gboolean set_changed)
920 GossipContactListPriv *priv;
924 priv = GET_PRIV (list);
926 model = GTK_TREE_MODEL (priv->store);
928 iters = contact_list_find_contact (list, contact);
929 for (l = iters; l; l = l->next) {
932 gtk_tree_store_set (priv->store, l->data,
933 COL_IS_ACTIVE, active,
936 gossip_debug (DEBUG_DOMAIN, "Set item %s", active ? "active" : "inactive");
939 path = gtk_tree_model_get_path (model, l->data);
940 gtk_tree_model_row_changed (model, path, l->data);
941 gtk_tree_path_free (path);
945 g_list_foreach (iters, (GFunc)gtk_tree_iter_free, NULL);
950 static ShowActiveData *
951 contact_list_contact_active_new (GossipContactList *list,
952 GossipContact *contact,
955 ShowActiveData *data;
957 g_return_val_if_fail (list != NULL, NULL);
958 g_return_val_if_fail (contact != NULL, NULL);
960 gossip_debug (DEBUG_DOMAIN,
961 "Contact:'%s' now active, and %s be removed",
962 gossip_contact_get_name (contact),
963 remove ? "WILL" : "WILL NOT");
965 data = g_slice_new0 (ShowActiveData);
967 data->list = g_object_ref (list);
968 data->contact = g_object_ref (contact);
970 data->remove = remove;
976 contact_list_contact_active_free (ShowActiveData *data)
978 g_return_if_fail (data != NULL);
980 g_object_unref (data->contact);
981 g_object_unref (data->list);
983 g_slice_free (ShowActiveData, data);
987 contact_list_contact_active_cb (ShowActiveData *data)
989 GossipContactListPriv *priv;
991 g_return_val_if_fail (data != NULL, FALSE);
993 priv = GET_PRIV (data->list);
996 !priv->show_offline &&
997 !gossip_contact_is_online (data->contact)) {
998 gossip_debug (DEBUG_DOMAIN,
999 "Contact:'%s' active timeout, removing item",
1000 gossip_contact_get_name (data->contact));
1001 contact_list_remove_contact (data->list,
1005 gossip_debug (DEBUG_DOMAIN,
1006 "Contact:'%s' no longer active",
1007 gossip_contact_get_name (data->contact));
1008 contact_list_contact_set_active (data->list,
1013 contact_list_contact_active_free (data);
1019 contact_list_get_parent_group (GtkTreeModel *model,
1021 gboolean *path_is_group)
1023 GtkTreeIter parent_iter, iter;
1027 g_return_val_if_fail (model != NULL, NULL);
1028 g_return_val_if_fail (path != NULL, NULL);
1029 g_return_val_if_fail (path_is_group != NULL, NULL);
1031 if (!gtk_tree_model_get_iter (model, &iter, path)) {
1035 gtk_tree_model_get (model, &iter,
1036 COL_IS_GROUP, &is_group,
1040 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter)) {
1046 gtk_tree_model_get (model, &iter,
1047 COL_IS_GROUP, &is_group,
1054 *path_is_group = TRUE;
1057 gtk_tree_model_get (model, &iter,
1065 contact_list_get_group_foreach (GtkTreeModel *model,
1073 /* Groups are only at the top level. */
1074 if (gtk_tree_path_get_depth (path) != 1) {
1078 gtk_tree_model_get (model, iter,
1080 COL_IS_GROUP, &is_group,
1083 if (is_group && strcmp (str, fg->name) == 0) {
1094 contact_list_get_group (GossipContactList *list,
1096 GtkTreeIter *iter_group_to_set,
1097 GtkTreeIter *iter_separator_to_set,
1100 GossipContactListPriv *priv;
1101 GtkTreeModel *model;
1102 GtkTreeIter iter_group, iter_separator;
1105 priv = GET_PRIV (list);
1107 memset (&fg, 0, sizeof (fg));
1111 model = GTK_TREE_MODEL (priv->store);
1112 gtk_tree_model_foreach (model,
1113 (GtkTreeModelForeachFunc) contact_list_get_group_foreach,
1121 gtk_tree_store_append (priv->store, &iter_group, NULL);
1122 gtk_tree_store_set (priv->store, &iter_group,
1123 COL_PIXBUF_STATUS, NULL,
1126 COL_IS_ACTIVE, FALSE,
1127 COL_IS_SEPARATOR, FALSE,
1130 if (iter_group_to_set) {
1131 *iter_group_to_set = iter_group;
1134 gtk_tree_store_append (priv->store,
1137 gtk_tree_store_set (priv->store, &iter_separator,
1138 COL_IS_SEPARATOR, TRUE,
1141 if (iter_separator_to_set) {
1142 *iter_separator_to_set = iter_separator;
1149 if (iter_group_to_set) {
1150 *iter_group_to_set = fg.iter;
1153 iter_separator = fg.iter;
1155 if (gtk_tree_model_iter_next (model, &iter_separator)) {
1156 gboolean is_separator;
1158 gtk_tree_model_get (model, &iter_separator,
1159 COL_IS_SEPARATOR, &is_separator,
1162 if (is_separator && iter_separator_to_set) {
1163 *iter_separator_to_set = iter_separator;
1170 contact_list_add_contact (GossipContactList *list,
1171 GossipContact *contact)
1173 GossipContactListPriv *priv;
1174 GtkTreeIter iter, iter_group, iter_separator;
1175 GtkTreeModel *model;
1178 priv = GET_PRIV (list);
1180 if (!priv->show_offline && !gossip_contact_is_online (contact)) {
1184 model = GTK_TREE_MODEL (priv->store);
1186 /* If no groups just add it at the top level. */
1187 groups = gossip_contact_get_groups (contact);
1189 GdkPixbuf *pixbuf_status;
1190 GdkPixbuf *pixbuf_avatar;
1191 gboolean show_avatar = FALSE;
1193 pixbuf_status = gossip_pixbuf_for_contact (contact);
1194 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1196 if (priv->show_avatars && !priv->is_compact) {
1200 gossip_debug (DEBUG_DOMAIN, "");
1201 gossip_debug (DEBUG_DOMAIN,
1202 "vvvvvvvvvvvvvvvv FIXME: Errors may follow below (since filter work) vvvvvvvvvvvvvvvv");
1204 gossip_debug (DEBUG_DOMAIN,
1205 "**** GossipContact:%p, is GObject:%s, is GossipContact:%s, ADDING CONTACT #1",
1207 G_IS_OBJECT (contact) ? "yes" : "no",
1208 GOSSIP_IS_CONTACT (contact) ? "yes" : "no");
1210 gtk_tree_store_append (priv->store, &iter, NULL);
1211 gtk_tree_store_set (priv->store, &iter,
1212 COL_PIXBUF_STATUS, pixbuf_status,
1213 COL_PIXBUF_AVATAR, pixbuf_avatar,
1214 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1215 COL_NAME, gossip_contact_get_name (contact),
1216 COL_STATUS, gossip_contact_get_status (contact),
1217 COL_STATUS_VISIBLE, !priv->is_compact,
1218 COL_CONTACT, contact,
1219 COL_IS_GROUP, FALSE,
1220 COL_IS_ACTIVE, FALSE,
1221 COL_IS_ONLINE, gossip_contact_is_online (contact),
1222 COL_IS_SEPARATOR, FALSE,
1225 gossip_debug (DEBUG_DOMAIN,
1226 "^^^^^^^^^^^^^^^^ FIXME: Errors may occur above (since filter work) ^^^^^^^^^^^^^^^^");
1227 gossip_debug (DEBUG_DOMAIN, "");
1229 if (pixbuf_avatar) {
1230 g_object_unref (pixbuf_avatar);
1232 if (pixbuf_status) {
1233 g_object_unref (pixbuf_status);
1237 /* Else add to each group. */
1238 for (l = groups; l; l = l->next) {
1240 GtkTreeIter model_iter_group;
1241 GdkPixbuf *pixbuf_status;
1242 GdkPixbuf *pixbuf_avatar;
1246 gboolean show_avatar = FALSE;
1253 pixbuf_status = gossip_pixbuf_for_contact (contact);
1254 pixbuf_avatar = gossip_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
1256 contact_list_get_group (list, name, &iter_group, &iter_separator, &created);
1258 if (priv->show_avatars && !priv->is_compact) {
1262 gossip_debug (DEBUG_DOMAIN, "");
1263 gossip_debug (DEBUG_DOMAIN,
1264 "vvvvvvvvvvvvvvvv FIXME: Errors may follow below (since filter work) vvvvvvvvvvvvvvvv");
1266 gossip_debug (DEBUG_DOMAIN,
1267 "**** GossipContact:%p, is GObject:%s, is GossipContact:%s, ADDING CONTACT #2",
1269 G_IS_OBJECT (contact) ? "yes" : "no",
1270 GOSSIP_IS_CONTACT (contact) ? "yes" : "no");
1272 gtk_tree_store_insert_after (priv->store, &iter, &iter_group, NULL);
1273 gtk_tree_store_set (priv->store, &iter,
1274 COL_PIXBUF_STATUS, pixbuf_status,
1275 COL_PIXBUF_AVATAR, pixbuf_avatar,
1276 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
1277 COL_NAME, gossip_contact_get_name (contact),
1278 COL_STATUS, gossip_contact_get_status (contact),
1279 COL_STATUS_VISIBLE, !priv->is_compact,
1280 COL_CONTACT, contact,
1281 COL_IS_GROUP, FALSE,
1282 COL_IS_ACTIVE, FALSE,
1283 COL_IS_ONLINE, gossip_contact_is_online (contact),
1284 COL_IS_SEPARATOR, FALSE,
1287 gossip_debug (DEBUG_DOMAIN,
1288 "^^^^^^^^^^^^^^^^ FIXME: Errors may occur above (since filter work) ^^^^^^^^^^^^^^^^");
1289 gossip_debug (DEBUG_DOMAIN, "");
1291 if (pixbuf_avatar) {
1292 g_object_unref (pixbuf_avatar);
1294 if (pixbuf_status) {
1295 g_object_unref (pixbuf_status);
1302 found = gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter),
1309 path = gtk_tree_model_get_path (model, &model_iter_group);
1314 if (gossip_contact_group_get_expanded (name)) {
1315 g_signal_handlers_block_by_func (list,
1316 contact_list_row_expand_or_collapse_cb,
1317 GINT_TO_POINTER (TRUE));
1318 gtk_tree_view_expand_row (GTK_TREE_VIEW (list), path, TRUE);
1319 g_signal_handlers_unblock_by_func (list,
1320 contact_list_row_expand_or_collapse_cb,
1321 GINT_TO_POINTER (TRUE));
1323 g_signal_handlers_block_by_func (list,
1324 contact_list_row_expand_or_collapse_cb,
1325 GINT_TO_POINTER (FALSE));
1326 gtk_tree_view_collapse_row (GTK_TREE_VIEW (list), path);
1327 g_signal_handlers_unblock_by_func (list,
1328 contact_list_row_expand_or_collapse_cb,
1329 GINT_TO_POINTER (FALSE));
1332 gtk_tree_path_free (path);
1337 contact_list_remove_contact (GossipContactList *list,
1338 GossipContact *contact)
1340 GossipContactListPriv *priv;
1341 GtkTreeModel *model;
1344 priv = GET_PRIV (list);
1346 iters = contact_list_find_contact (list, contact);
1351 /* Clean up model */
1352 model = GTK_TREE_MODEL (priv->store);
1354 for (l = iters; l; l = l->next) {
1357 /* NOTE: it is only <= 2 here because we have
1358 * separators after the group name, otherwise it
1361 if (gtk_tree_model_iter_parent (model, &parent, l->data) &&
1362 gtk_tree_model_iter_n_children (model, &parent) <= 2) {
1363 gtk_tree_store_remove (priv->store, &parent);
1365 gtk_tree_store_remove (priv->store, l->data);
1369 g_list_foreach (iters, (GFunc) gtk_tree_iter_free, NULL);
1370 g_list_free (iters);
1374 contact_list_create_model (GossipContactList *list)
1376 GossipContactListPriv *priv;
1377 GtkTreeModel *model;
1379 priv = GET_PRIV (list);
1382 g_object_unref (priv->store);
1386 g_object_unref (priv->filter);
1389 priv->store = gtk_tree_store_new (COL_COUNT,
1390 GDK_TYPE_PIXBUF, /* Status pixbuf */
1391 GDK_TYPE_PIXBUF, /* Avatar pixbuf */
1392 G_TYPE_BOOLEAN, /* Avatar pixbuf visible */
1393 G_TYPE_STRING, /* Name */
1394 G_TYPE_STRING, /* Status string */
1395 G_TYPE_BOOLEAN, /* Show status */
1396 GOSSIP_TYPE_CONTACT, /* Contact type */
1397 G_TYPE_BOOLEAN, /* Is group */
1398 G_TYPE_BOOLEAN, /* Is active */
1399 G_TYPE_BOOLEAN, /* Is online */
1400 G_TYPE_BOOLEAN); /* Is separator */
1402 /* Save normal model */
1403 model = GTK_TREE_MODEL (priv->store);
1405 /* Set up sorting */
1406 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (model),
1408 contact_list_sort_func,
1411 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
1413 GTK_SORT_ASCENDING);
1416 priv->filter = gtk_tree_model_filter_new (model, NULL);
1418 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter),
1419 (GtkTreeModelFilterVisibleFunc)
1420 contact_list_filter_func,
1423 gtk_tree_view_set_model (GTK_TREE_VIEW (list), priv->filter);
1427 contact_list_search_equal_func (GtkTreeModel *model,
1431 gpointer search_data)
1433 gchar *name, *name_folded;
1441 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
1447 name_folded = g_utf8_casefold (name, -1);
1448 key_folded = g_utf8_casefold (key, -1);
1450 if (name_folded && key_folded &&
1451 strstr (name_folded, key_folded)) {
1458 g_free (name_folded);
1459 g_free (key_folded);
1465 contact_list_setup_view (GossipContactList *list)
1467 GtkCellRenderer *cell;
1468 GtkTreeViewColumn *col;
1471 gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (list),
1472 contact_list_search_equal_func,
1477 "headers-visible", FALSE,
1478 "reorderable", TRUE,
1479 "show-expanders", FALSE,
1482 col = gtk_tree_view_column_new ();
1485 cell = gtk_cell_renderer_pixbuf_new ();
1486 gtk_tree_view_column_pack_start (col, cell, FALSE);
1487 gtk_tree_view_column_set_cell_data_func (
1489 (GtkTreeCellDataFunc) contact_list_pixbuf_cell_data_func,
1499 cell = gossip_cell_renderer_text_new ();
1500 gtk_tree_view_column_pack_start (col, cell, TRUE);
1501 gtk_tree_view_column_set_cell_data_func (
1503 (GtkTreeCellDataFunc) contact_list_text_cell_data_func,
1506 gtk_tree_view_column_add_attribute (col, cell,
1508 gtk_tree_view_column_add_attribute (col, cell,
1509 "status", COL_STATUS);
1510 gtk_tree_view_column_add_attribute (col, cell,
1511 "is_group", COL_IS_GROUP);
1514 cell = gtk_cell_renderer_pixbuf_new ();
1515 gtk_tree_view_column_pack_start (col, cell, FALSE);
1516 gtk_tree_view_column_set_cell_data_func (
1518 (GtkTreeCellDataFunc) contact_list_avatar_cell_data_func,
1530 cell = gossip_cell_renderer_expander_new ();
1531 gtk_tree_view_column_pack_end (col, cell, FALSE);
1532 gtk_tree_view_column_set_cell_data_func (
1534 (GtkTreeCellDataFunc) contact_list_expander_cell_data_func,
1537 /* Actually add the column now we have added all cell renderers */
1538 gtk_tree_view_append_column (GTK_TREE_VIEW (list), col);
1541 for (i = 0; i < G_N_ELEMENTS (drag_types_dest); ++i) {
1542 drag_atoms_dest[i] = gdk_atom_intern (drag_types_dest[i].target,
1546 for (i = 0; i < G_N_ELEMENTS (drag_types_source); ++i) {
1547 drag_atoms_source[i] = gdk_atom_intern (drag_types_source[i].target,
1551 /* Note: We support the COPY action too, but need to make the
1552 * MOVE action the default.
1554 gtk_drag_source_set (GTK_WIDGET (list),
1557 G_N_ELEMENTS (drag_types_source),
1560 gtk_drag_dest_set (GTK_WIDGET (list),
1561 GTK_DEST_DEFAULT_ALL,
1563 G_N_ELEMENTS (drag_types_dest),
1564 GDK_ACTION_MOVE | GDK_ACTION_LINK);
1566 g_signal_connect (GTK_WIDGET (list),
1567 "drag-data-received",
1568 G_CALLBACK (contact_list_drag_data_received),
1571 /* FIXME: noticed but when you drag the row over the treeview
1572 * fast, it seems to stop redrawing itself, if we don't
1573 * connect this signal, all is fine.
1575 g_signal_connect (GTK_WIDGET (list),
1577 G_CALLBACK (contact_list_drag_motion),
1580 g_signal_connect (GTK_WIDGET (list),
1582 G_CALLBACK (contact_list_drag_begin),
1584 g_signal_connect (GTK_WIDGET (list),
1586 G_CALLBACK (contact_list_drag_data_get),
1588 g_signal_connect (GTK_WIDGET (list),
1590 G_CALLBACK (contact_list_drag_end),
1595 contact_list_drag_data_received (GtkWidget *widget,
1596 GdkDragContext *context,
1599 GtkSelectionData *selection,
1604 GossipContactListPriv *priv;
1605 GtkTreeModel *model;
1607 GtkTreeViewDropPosition position;
1608 GossipContact *contact;
1613 gboolean drag_success = TRUE;
1614 gboolean drag_del = FALSE;
1616 priv = GET_PRIV (widget);
1618 id = (const gchar*) selection->data;
1619 gossip_debug (DEBUG_DOMAIN, "Received %s%s drag & drop contact from roster with id:'%s'",
1620 context->action == GDK_ACTION_MOVE ? "move" : "",
1621 context->action == GDK_ACTION_COPY ? "copy" : "",
1624 /* FIXME: This is ambigous, an id can come from multiple accounts */
1625 contact = empathy_contact_manager_find (priv->manager, id);
1627 gossip_debug (DEBUG_DOMAIN, "No contact found associated with drag & drop");
1631 groups = gossip_contact_get_groups (contact);
1633 is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
1640 if (g_list_length (groups) != 1) {
1641 /* if they have dragged a contact out of a
1642 * group then we would set the contact to have
1643 * NO groups but only if they were ONE group
1644 * to begin with - should we do this
1645 * regardless to how many groups they are in
1646 * already or not at all?
1651 gossip_contact_set_groups (contact, NULL);
1653 GList *l, *new_groups;
1657 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1658 name = contact_list_get_parent_group (model, path, &is_group);
1660 if (groups && name &&
1661 g_list_find_custom (groups, name, (GCompareFunc)strcmp)) {
1666 /* Get source group information. */
1667 priv = GET_PRIV (widget);
1668 if (!priv->drag_row) {
1673 path = gtk_tree_row_reference_get_path (priv->drag_row);
1679 old_group = contact_list_get_parent_group (model, path, &is_group);
1680 gtk_tree_path_free (path);
1682 if (!name && old_group && GDK_ACTION_MOVE) {
1683 drag_success = FALSE;
1686 if (context->action == GDK_ACTION_MOVE) {
1690 /* Create new groups GList. */
1691 for (l = groups, new_groups = NULL; l && drag_success; l = l->next) {
1695 if (context->action == GDK_ACTION_MOVE &&
1696 old_group != NULL &&
1697 strcmp (str, old_group) == 0) {
1705 new_groups = g_list_append (new_groups, g_strdup (str));
1710 new_groups = g_list_append (new_groups, name);
1712 gossip_contact_set_groups (contact, new_groups);
1718 gtk_drag_finish (context, drag_success, drag_del, GDK_CURRENT_TIME);
1722 contact_list_drag_motion (GtkWidget *widget,
1723 GdkDragContext *context,
1729 static DragMotionData *dm = NULL;
1732 gboolean is_different = FALSE;
1733 gboolean cleanup = TRUE;
1735 is_row = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
1746 cleanup &= (dm && gtk_tree_path_compare (dm->path, path) != 0);
1747 is_different = (!dm || (dm && gtk_tree_path_compare (dm->path, path) != 0));
1752 if (!is_different && !cleanup) {
1757 gtk_tree_path_free (dm->path);
1758 if (dm->timeout_id) {
1759 g_source_remove (dm->timeout_id);
1767 if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), path)) {
1768 dm = g_new0 (DragMotionData, 1);
1770 dm->list = GOSSIP_CONTACT_LIST (widget);
1771 dm->path = gtk_tree_path_copy (path);
1773 dm->timeout_id = g_timeout_add (
1775 (GSourceFunc) contact_list_drag_motion_cb,
1783 contact_list_drag_motion_cb (DragMotionData *data)
1785 gtk_tree_view_expand_row (GTK_TREE_VIEW (data->list),
1789 data->timeout_id = 0;
1795 contact_list_drag_begin (GtkWidget *widget,
1796 GdkDragContext *context,
1799 GossipContactListPriv *priv;
1800 GtkTreeSelection *selection;
1801 GtkTreeModel *model;
1805 priv = GET_PRIV (widget);
1807 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
1808 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
1812 path = gtk_tree_model_get_path (model, &iter);
1813 priv->drag_row = gtk_tree_row_reference_new (model, path);
1814 gtk_tree_path_free (path);
1818 contact_list_drag_data_get (GtkWidget *widget,
1819 GdkDragContext *context,
1820 GtkSelectionData *selection,
1825 GossipContactListPriv *priv;
1826 GtkTreePath *src_path;
1828 GtkTreeModel *model;
1829 GossipContact *contact;
1832 priv = GET_PRIV (widget);
1834 model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
1835 if (!priv->drag_row) {
1839 src_path = gtk_tree_row_reference_get_path (priv->drag_row);
1844 if (!gtk_tree_model_get_iter (model, &iter, src_path)) {
1845 gtk_tree_path_free (src_path);
1849 gtk_tree_path_free (src_path);
1851 contact = gossip_contact_list_get_selected (GOSSIP_CONTACT_LIST (widget));
1856 id = gossip_contact_get_id (contact);
1857 g_object_unref (contact);
1860 case DND_DRAG_TYPE_CONTACT_ID:
1861 gtk_selection_data_set (selection, drag_atoms_source[info], 8,
1862 (guchar*)id, strlen (id) + 1);
1871 contact_list_drag_end (GtkWidget *widget,
1872 GdkDragContext *context,
1875 GossipContactListPriv *priv;
1877 priv = GET_PRIV (widget);
1879 if (priv->drag_row) {
1880 gtk_tree_row_reference_free (priv->drag_row);
1881 priv->drag_row = NULL;
1886 contact_list_cell_set_background (GossipContactList *list,
1887 GtkCellRenderer *cell,
1894 g_return_if_fail (list != NULL);
1895 g_return_if_fail (cell != NULL);
1897 style = gtk_widget_get_style (GTK_WIDGET (list));
1901 color = style->bg[GTK_STATE_SELECTED];
1903 /* Here we take the current theme colour and add it to
1904 * the colour for white and average the two. This
1905 * gives a colour which is inline with the theme but
1908 color.red = (color.red + (style->white).red) / 2;
1909 color.green = (color.green + (style->white).green) / 2;
1910 color.blue = (color.blue + (style->white).blue) / 2;
1913 "cell-background-gdk", &color,
1917 "cell-background-gdk", NULL,
1922 "cell-background-gdk", NULL,
1925 gint color_sum_normal;
1926 gint color_sum_selected;
1928 color = style->base[GTK_STATE_SELECTED];
1929 color_sum_normal = color.red+color.green+color.blue;
1930 color = style->base[GTK_STATE_NORMAL];
1931 color_sum_selected = color.red+color.green+color.blue;
1932 color = style->text_aa[GTK_STATE_INSENSITIVE];
1934 if (color_sum_normal < color_sum_selected) {
1935 /* Found a light theme */
1936 color.red = (color.red + (style->white).red) / 2;
1937 color.green = (color.green + (style->white).green) / 2;
1938 color.blue = (color.blue + (style->white).blue) / 2;
1940 /* Found a dark theme */
1941 color.red = (color.red + (style->black).red) / 2;
1942 color.green = (color.green + (style->black).green) / 2;
1943 color.blue = (color.blue + (style->black).blue) / 2;
1947 "cell-background-gdk", &color,
1954 contact_list_pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
1955 GtkCellRenderer *cell,
1956 GtkTreeModel *model,
1958 GossipContactList *list)
1964 gtk_tree_model_get (model, iter,
1965 COL_IS_GROUP, &is_group,
1966 COL_IS_ACTIVE, &is_active,
1967 COL_PIXBUF_STATUS, &pixbuf,
1971 "visible", !is_group,
1976 g_object_unref (pixbuf);
1979 contact_list_cell_set_background (list, cell, is_group, is_active);
1983 contact_list_avatar_cell_data_func (GtkTreeViewColumn *tree_column,
1984 GtkCellRenderer *cell,
1985 GtkTreeModel *model,
1987 GossipContactList *list)
1990 gboolean show_avatar;
1994 gtk_tree_model_get (model, iter,
1995 COL_PIXBUF_AVATAR, &pixbuf,
1996 COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
1997 COL_IS_GROUP, &is_group,
1998 COL_IS_ACTIVE, &is_active,
2002 "visible", !is_group && show_avatar,
2007 g_object_unref (pixbuf);
2010 contact_list_cell_set_background (list, cell, is_group, is_active);
2014 contact_list_text_cell_data_func (GtkTreeViewColumn *tree_column,
2015 GtkCellRenderer *cell,
2016 GtkTreeModel *model,
2018 GossipContactList *list)
2022 gboolean show_status;
2024 gtk_tree_model_get (model, iter,
2025 COL_IS_GROUP, &is_group,
2026 COL_IS_ACTIVE, &is_active,
2027 COL_STATUS_VISIBLE, &show_status,
2031 "show-status", show_status,
2034 contact_list_cell_set_background (list, cell, is_group, is_active);
2038 contact_list_expander_cell_data_func (GtkTreeViewColumn *column,
2039 GtkCellRenderer *cell,
2040 GtkTreeModel *model,
2042 GossipContactList *list)
2047 gtk_tree_model_get (model, iter,
2048 COL_IS_GROUP, &is_group,
2049 COL_IS_ACTIVE, &is_active,
2052 if (gtk_tree_model_iter_has_child (model, iter)) {
2054 gboolean row_expanded;
2056 path = gtk_tree_model_get_path (model, iter);
2057 row_expanded = gtk_tree_view_row_expanded (GTK_TREE_VIEW (column->tree_view), path);
2058 gtk_tree_path_free (path);
2062 "expander-style", row_expanded ? GTK_EXPANDER_EXPANDED : GTK_EXPANDER_COLLAPSED,
2065 g_object_set (cell, "visible", FALSE, NULL);
2068 contact_list_cell_set_background (list, cell, is_group, is_active);
2072 contact_list_get_contact_menu (GossipContactList *list,
2073 gboolean can_send_file,
2074 gboolean can_show_log)
2076 GossipContactListPriv *priv;
2080 priv = GET_PRIV (list);
2082 /* Sort out sensitive items */
2083 action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
2084 gtk_action_set_sensitive (action, can_show_log);
2086 action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
2087 gtk_action_set_visible (action, can_send_file);
2089 widget = gtk_ui_manager_get_widget (priv->ui, "/Contact");
2095 gossip_contact_list_get_group_menu (GossipContactList *list)
2097 GossipContactListPriv *priv;
2100 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2102 priv = GET_PRIV (list);
2104 widget = gtk_ui_manager_get_widget (priv->ui, "/Group");
2110 gossip_contact_list_get_contact_menu (GossipContactList *list,
2111 GossipContact *contact)
2114 gboolean can_show_log;
2115 gboolean can_send_file;
2117 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2118 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
2120 can_show_log = FALSE; /* FIXME: gossip_log_exists_for_contact (contact); */
2121 can_send_file = FALSE;
2123 menu = contact_list_get_contact_menu (list,
2130 contact_list_button_press_event_cb (GossipContactList *list,
2131 GdkEventButton *event,
2134 GossipContactListPriv *priv;
2135 GossipContact *contact;
2137 GtkTreeSelection *selection;
2138 GtkTreeModel *model;
2140 gboolean row_exists;
2143 if (event->button != 3) {
2147 priv = GET_PRIV (list);
2149 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2150 model = GTK_TREE_MODEL (priv->store);
2152 gtk_widget_grab_focus (GTK_WIDGET (list));
2154 row_exists = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (list),
2162 gtk_tree_selection_unselect_all (selection);
2163 gtk_tree_selection_select_path (selection, path);
2165 gtk_tree_model_get_iter (model, &iter, path);
2166 gtk_tree_path_free (path);
2168 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2171 menu = gossip_contact_list_get_contact_menu (list, contact);
2172 g_object_unref (contact);
2174 menu = gossip_contact_list_get_group_menu (list);
2181 gtk_widget_show (menu);
2183 gtk_menu_popup (GTK_MENU (menu),
2184 NULL, NULL, NULL, NULL,
2185 event->button, event->time);
2191 contact_list_row_activated_cb (GossipContactList *list,
2193 GtkTreeViewColumn *col,
2196 GossipContact *contact;
2198 GtkTreeModel *model;
2201 view = GTK_TREE_VIEW (list);
2202 model = gtk_tree_view_get_model (view);
2204 gtk_tree_model_get_iter (model, &iter, path);
2205 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2208 g_signal_emit (list, signals[CONTACT_CHAT], 0, contact);
2209 g_object_unref (contact);
2214 contact_list_row_expand_or_collapse_cb (GossipContactList *list,
2219 GtkTreeModel *model;
2223 model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
2225 gtk_tree_model_get (model, iter,
2229 expanded = GPOINTER_TO_INT (user_data);
2230 gossip_contact_group_set_expanded (name, expanded);
2236 contact_list_sort_func (GtkTreeModel *model,
2237 GtkTreeIter *iter_a,
2238 GtkTreeIter *iter_b,
2241 gchar *name_a, *name_b;
2242 GossipContact *contact_a, *contact_b;
2243 gboolean is_separator_a, is_separator_b;
2246 gtk_tree_model_get (model, iter_a,
2248 COL_CONTACT, &contact_a,
2249 COL_IS_SEPARATOR, &is_separator_a,
2251 gtk_tree_model_get (model, iter_b,
2253 COL_CONTACT, &contact_b,
2254 COL_IS_SEPARATOR, &is_separator_b,
2257 /* If contact is NULL it means it's a group. */
2259 if (is_separator_a || is_separator_b) {
2260 if (is_separator_a) {
2262 } else if (is_separator_b) {
2265 } else if (!contact_a && contact_b) {
2267 } else if (contact_a && !contact_b) {
2270 ret_val = g_utf8_collate (name_a, name_b);
2277 g_object_unref (contact_a);
2281 g_object_unref (contact_b);
2288 contact_list_filter_show_contact (GossipContact *contact,
2289 const gchar *filter)
2294 /* Check contact id */
2295 str = g_utf8_casefold (gossip_contact_get_id (contact), -1);
2296 visible = G_STR_EMPTY (str) || strstr (str, filter);
2303 /* Check contact name */
2304 str = g_utf8_casefold (gossip_contact_get_name (contact), -1);
2305 visible = G_STR_EMPTY (str) || strstr (str, filter);
2312 contact_list_filter_show_group (GossipContactList *list,
2314 const gchar *filter)
2316 GossipContactListPriv *priv;
2317 GList *contacts, *l;
2319 gboolean show_group = FALSE;
2321 priv = GET_PRIV (list);
2323 str = g_utf8_casefold (group, -1);
2328 /* If the filter is the partially the group name, we show the
2331 if (strstr (str, filter)) {
2336 /* At this point, we need to check in advance if this
2337 * group should be shown because a contact we want to
2338 * show exists in it.
2340 contacts = empathy_contact_manager_get_contacts (priv->manager);
2341 for (l = contacts; l && !show_group; l = l->next) {
2342 if (!gossip_contact_is_in_group (l->data, group)) {
2346 if (contact_list_filter_show_contact (l->data, filter)) {
2350 g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
2351 g_list_free (contacts);
2358 contact_list_filter_func (GtkTreeModel *model,
2360 GossipContactList *list)
2362 GossipContactListPriv *priv;
2364 gboolean is_separator;
2365 gboolean visible = TRUE;
2367 priv = GET_PRIV (list);
2369 if (G_STR_EMPTY (priv->filter_text)) {
2373 /* Check to see if iter matches any group names */
2374 gtk_tree_model_get (model, iter,
2375 COL_IS_GROUP, &is_group,
2376 COL_IS_SEPARATOR, &is_separator,
2382 gtk_tree_model_get (model, iter, COL_NAME, &name, -1);
2383 visible &= contact_list_filter_show_group (list,
2387 } else if (is_separator) {
2388 /* Do nothing here */
2390 GossipContact *contact;
2392 /* Check contact id */
2393 gtk_tree_model_get (model, iter, COL_CONTACT, &contact, -1);
2394 visible &= contact_list_filter_show_contact (contact,
2396 g_object_unref (contact);
2403 contact_list_iter_equal_contact (GtkTreeModel *model,
2405 GossipContact *contact)
2410 gtk_tree_model_get (model, iter,
2418 equal = (c == contact);
2425 contact_list_find_contact_foreach (GtkTreeModel *model,
2430 if (contact_list_iter_equal_contact (model, iter, fc->contact)) {
2432 fc->iters = g_list_append (fc->iters, gtk_tree_iter_copy (iter));
2435 /* We want to find ALL contacts that match, this means if we
2436 * have the same contact in 3 groups, all iters should be
2443 contact_list_find_contact (GossipContactList *list,
2444 GossipContact *contact)
2446 GossipContactListPriv *priv;
2447 GtkTreeModel *model;
2451 priv = GET_PRIV (list);
2453 memset (&fc, 0, sizeof (fc));
2455 fc.contact = contact;
2457 model = GTK_TREE_MODEL (priv->store);
2458 gtk_tree_model_foreach (model,
2459 (GtkTreeModelForeachFunc) contact_list_find_contact_foreach,
2470 contact_list_action_cb (GtkAction *action,
2471 GossipContactList *list)
2473 GossipContact *contact;
2477 name = gtk_action_get_name (action);
2482 gossip_debug (DEBUG_DOMAIN, "Action:'%s' activated", name);
2484 contact = gossip_contact_list_get_selected (list);
2485 group = gossip_contact_list_get_selected_group (list);
2487 if (contact && strcmp (name, "Chat") == 0) {
2488 g_signal_emit (list, signals[CONTACT_CHAT], 0, contact);
2490 else if (contact && strcmp (name, "Information") == 0) {
2491 g_signal_emit (list, signals[CONTACT_INFORMATION], 0, contact);
2493 else if (contact && strcmp (name, "Edit") == 0) {
2494 g_signal_emit (list, signals[CONTACT_EDIT], 0, contact);
2496 else if (contact && strcmp (name, "Remove") == 0) {
2497 g_signal_emit (list, signals[CONTACT_REMOVE], 0, contact);
2499 else if (contact && strcmp (name, "Invite") == 0) {
2500 g_signal_emit (list, signals[CONTACT_INVITE], 0, contact);
2502 else if (contact && strcmp (name, "SendFile") == 0) {
2503 g_signal_emit (list, signals[CONTACT_SEND_FILE], 0, contact);
2505 else if (contact && strcmp (name, "Log") == 0) {
2506 g_signal_emit (list, signals[CONTACT_LOG], 0, contact);
2508 else if (group && strcmp (name, "Rename") == 0) {
2509 g_signal_emit (list, signals[GROUP_RENAME], 0, group);
2514 g_object_unref (contact);
2519 contact_list_update_list_mode_foreach (GtkTreeModel *model,
2522 GossipContactList *list)
2524 GossipContactListPriv *priv;
2525 gboolean show_avatar = FALSE;
2527 priv = GET_PRIV (list);
2529 if (priv->show_avatars && !priv->is_compact) {
2533 gtk_tree_store_set (priv->store, iter,
2534 COL_PIXBUF_AVATAR_VISIBLE, show_avatar,
2535 COL_STATUS_VISIBLE, !priv->is_compact,
2542 gossip_contact_list_new (void)
2544 return g_object_new (GOSSIP_TYPE_CONTACT_LIST, NULL);
2548 gossip_contact_list_get_selected (GossipContactList *list)
2550 GossipContactListPriv *priv;
2551 GtkTreeSelection *selection;
2553 GtkTreeModel *model;
2554 GossipContact *contact;
2556 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2558 priv = GET_PRIV (list);
2560 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2561 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
2565 gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
2571 gossip_contact_list_get_selected_group (GossipContactList *list)
2573 GossipContactListPriv *priv;
2574 GtkTreeSelection *selection;
2576 GtkTreeModel *model;
2580 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), NULL);
2582 priv = GET_PRIV (list);
2584 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
2585 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
2589 gtk_tree_model_get (model, &iter,
2590 COL_IS_GROUP, &is_group,
2603 gossip_contact_list_get_show_offline (GossipContactList *list)
2605 GossipContactListPriv *priv;
2607 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), FALSE);
2609 priv = GET_PRIV (list);
2611 return priv->show_offline;
2615 gossip_contact_list_get_show_avatars (GossipContactList *list)
2617 GossipContactListPriv *priv;
2619 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), TRUE);
2621 priv = GET_PRIV (list);
2623 return priv->show_avatars;
2627 gossip_contact_list_get_is_compact (GossipContactList *list)
2629 GossipContactListPriv *priv;
2631 g_return_val_if_fail (GOSSIP_IS_CONTACT_LIST (list), TRUE);
2633 priv = GET_PRIV (list);
2635 return priv->is_compact;
2639 gossip_contact_list_set_show_offline (GossipContactList *list,
2640 gboolean show_offline)
2642 GossipContactListPriv *priv;
2643 GList *contacts, *l;
2644 gboolean show_active;
2646 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2648 priv = GET_PRIV (list);
2650 priv->show_offline = show_offline;
2651 show_active = priv->show_active;
2653 /* Disable temporarily. */
2654 priv->show_active = FALSE;
2656 contacts = empathy_contact_manager_get_contacts (priv->manager);
2657 for (l = contacts; l; l = l->next) {
2658 GossipContact *contact;
2660 contact = GOSSIP_CONTACT (l->data);
2662 contact_list_contact_update (list, contact);
2664 g_object_unref (contact);
2666 g_list_free (contacts);
2668 /* Restore to original setting. */
2669 priv->show_active = show_active;
2673 gossip_contact_list_set_show_avatars (GossipContactList *list,
2674 gboolean show_avatars)
2676 GossipContactListPriv *priv;
2677 GtkTreeModel *model;
2679 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2681 priv = GET_PRIV (list);
2683 priv->show_avatars = show_avatars;
2685 model = GTK_TREE_MODEL (priv->store);
2687 gtk_tree_model_foreach (model,
2688 (GtkTreeModelForeachFunc)
2689 contact_list_update_list_mode_foreach,
2694 gossip_contact_list_set_is_compact (GossipContactList *list,
2695 gboolean is_compact)
2697 GossipContactListPriv *priv;
2698 GtkTreeModel *model;
2700 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2702 priv = GET_PRIV (list);
2704 priv->is_compact = is_compact;
2706 model = GTK_TREE_MODEL (priv->store);
2708 gtk_tree_model_foreach (model,
2709 (GtkTreeModelForeachFunc)
2710 contact_list_update_list_mode_foreach,
2715 gossip_contact_list_set_filter (GossipContactList *list,
2716 const gchar *filter)
2718 GossipContactListPriv *priv;
2720 g_return_if_fail (GOSSIP_IS_CONTACT_LIST (list));
2722 priv = GET_PRIV (list);
2724 g_free (priv->filter_text);
2726 priv->filter_text = g_utf8_casefold (filter, -1);
2728 priv->filter_text = NULL;
2731 gossip_debug (DEBUG_DOMAIN, "Refiltering with filter:'%s' (case folded)", filter);
2732 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));