]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Merge branch 'master' into tp-tube
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <libmissioncontrol/mc-account.h>
31 #include <telepathy-glib/util.h>
32
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-contact-list.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include "empathy-contact-widget.h"
39 #include "empathy-account-chooser.h"
40 #include "empathy-avatar-chooser.h"
41 #include "empathy-avatar-image.h"
42 #include "empathy-ui-utils.h"
43
44 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
45 #include <libempathy/empathy-debug.h>
46
47 /**
48  * SECTION:empathy-contact-widget
49  * @title:EmpathyContactWidget
50  * @short_description: A widget used to display and edit details about a contact
51  * @include: libempathy-empathy-contact-widget.h
52  *
53  * #EmpathyContactWidget is a widget which displays appropriate widgets
54  * with details about a contact, also allowing changing these details,
55  * if desired.
56  */
57
58 /**
59  * EmpathyContactWidget:
60  * @parent: parent object
61  *
62  * Widget which displays appropriate widgets with details about a contact,
63  * also allowing changing these details, if desired.
64  */
65
66 /* Delay before updating the widget when the id entry changed (seconds) */
67 #define ID_CHANGED_TIMEOUT 1
68
69 typedef struct
70 {
71   EmpathyTpContactFactory *factory;
72   EmpathyContactManager *manager;
73   EmpathyContact *contact;
74   EmpathyContactWidgetFlags flags;
75   guint widget_id_timeout;
76
77   GtkWidget *vbox_contact_widget;
78
79   /* Contact */
80   GtkWidget *vbox_contact;
81   GtkWidget *widget_avatar;
82   GtkWidget *widget_account;
83   GtkWidget *widget_id;
84   GtkWidget *widget_alias;
85   GtkWidget *label_alias;
86   GtkWidget *entry_alias;
87   GtkWidget *hbox_presence;
88   GtkWidget *image_state;
89   GtkWidget *label_status;
90   GtkWidget *table_contact;
91   GtkWidget *vbox_avatar;
92
93   /* Groups */
94   GtkWidget *vbox_groups;
95   GtkWidget *entry_group;
96   GtkWidget *button_group;
97   GtkWidget *treeview_groups;
98
99   /* Details */
100   GtkWidget *vbox_details;
101   GtkWidget *table_details;
102   GtkWidget *hbox_details_requested;
103
104   /* Client */
105   GtkWidget *vbox_client;
106   GtkWidget *table_client;
107   GtkWidget *hbox_client_requested;
108 } EmpathyContactWidget;
109
110 typedef struct
111 {
112   EmpathyContactWidget *information;
113   const gchar *name;
114   gboolean found;
115   GtkTreeIter found_iter;
116 } FindName;
117
118 static void contact_widget_destroy_cb (GtkWidget *widget,
119     EmpathyContactWidget *information);
120 static void contact_widget_remove_contact (EmpathyContactWidget *information);
121 static void contact_widget_set_contact (EmpathyContactWidget *information,
122     EmpathyContact *contact);
123 static void contact_widget_contact_setup (EmpathyContactWidget *information);
124 static void contact_widget_contact_update (EmpathyContactWidget *information);
125 static void contact_widget_change_contact (EmpathyContactWidget *information);
126 static void contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
127     EmpathyContactWidget *information);
128 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
129     GdkEventFocus *event, EmpathyContactWidget *information);
130 static gboolean contact_widget_entry_alias_focus_event_cb (
131     GtkEditable *editable, GdkEventFocus *event,
132     EmpathyContactWidget *information);
133 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
134 static void contact_widget_presence_notify_cb (
135     EmpathyContactWidget *information);
136 static void contact_widget_avatar_notify_cb (
137     EmpathyContactWidget *information);
138 static void contact_widget_groups_setup (
139     EmpathyContactWidget *information);
140 static void contact_widget_groups_update (EmpathyContactWidget *information);
141 static void contact_widget_model_setup (EmpathyContactWidget *information);
142 static void contact_widget_model_populate_columns (
143     EmpathyContactWidget *information);
144 static void contact_widget_groups_populate_data (
145     EmpathyContactWidget *information);
146 static void contact_widget_groups_notify_cb (
147     EmpathyContactWidget *information);
148 static gboolean contact_widget_model_find_name (
149     EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
150 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
151     GtkTreePath *path, GtkTreeIter *iter, FindName *data);
152 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
153     gchar *path_string, EmpathyContactWidget *information);
154 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
155     EmpathyContactWidget *information);
156 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
157     EmpathyContactWidget *information);
158 static void contact_widget_button_group_clicked_cb (GtkButton *button,
159     EmpathyContactWidget *information);
160 static void contact_widget_details_setup (EmpathyContactWidget *information);
161 static void contact_widget_details_update (EmpathyContactWidget *information);
162 static void contact_widget_client_setup (EmpathyContactWidget *information);
163 static void contact_widget_client_update (EmpathyContactWidget *information);
164
165 enum
166 {
167   COL_NAME,
168   COL_ENABLED,
169   COL_EDITABLE,
170   COL_COUNT
171 };
172
173 /**
174  * empathy_contact_widget_new:
175  * @contact: an #EmpathyContact
176  * @flags: #EmpathyContactWidgetFlags for the new contact widget
177  *
178  * Creates a new #EmpathyContactWidget.
179  *
180  * Return value: a new #EmpathyContactWidget
181  */
182 GtkWidget *
183 empathy_contact_widget_new (EmpathyContact *contact,
184                             EmpathyContactWidgetFlags flags)
185 {
186   EmpathyContactWidget *information;
187   GtkBuilder *gui;
188   gchar *filename;
189
190   g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
191
192   information = g_slice_new0 (EmpathyContactWidget);
193   information->flags = flags;
194
195   filename = empathy_file_lookup ("empathy-contact-widget.ui",
196       "libempathy-gtk");
197   gui = empathy_builder_get_file (filename,
198        "vbox_contact_widget", &information->vbox_contact_widget,
199        "vbox_contact", &information->vbox_contact,
200        "hbox_presence", &information->hbox_presence,
201        "label_alias", &information->label_alias,
202        "image_state", &information->image_state,
203        "label_status", &information->label_status,
204        "table_contact", &information->table_contact,
205        "vbox_avatar", &information->vbox_avatar,
206        "vbox_groups", &information->vbox_groups,
207        "entry_group", &information->entry_group,
208        "button_group", &information->button_group,
209        "treeview_groups", &information->treeview_groups,
210        "vbox_details", &information->vbox_details,
211        "table_details", &information->table_details,
212        "hbox_details_requested", &information->hbox_details_requested,
213        "vbox_client", &information->vbox_client,
214        "table_client", &information->table_client,
215        "hbox_client_requested", &information->hbox_client_requested,
216        NULL);
217   g_free (filename);
218
219   empathy_builder_connect (gui, information,
220       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
221       "entry_group", "changed", contact_widget_entry_group_changed_cb,
222       "entry_group", "activate", contact_widget_entry_group_activate_cb,
223       "button_group", "clicked", contact_widget_button_group_clicked_cb,
224       NULL);
225
226   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
227       "EmpathyContactWidget",
228       information);
229
230   /* Create widgets */
231   contact_widget_contact_setup (information);
232   contact_widget_groups_setup (information);
233   contact_widget_details_setup (information);
234   contact_widget_client_setup (information);
235
236   if (contact != NULL)
237     contact_widget_set_contact (information, contact);
238
239   else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
240       information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
241     contact_widget_change_contact (information);
242
243   return empathy_builder_unref_and_keep_widget (gui,
244     information->vbox_contact_widget);
245 }
246
247 /**
248  * empathy_contact_widget_get_contact:
249  * @widget: an #EmpathyContactWidget
250  *
251  * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
252  *
253  * Returns: the #EmpathyContact associated with @widget
254  */
255 EmpathyContact *
256 empathy_contact_widget_get_contact (GtkWidget *widget)
257 {
258   EmpathyContactWidget *information;
259
260   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
261
262   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
263   if (!information)
264       return NULL;
265
266   return information->contact;
267 }
268
269 /**
270  * empathy_contact_widget_set_contact:
271  * @widget: an #EmpathyContactWidget
272  * @contact: a different #EmpathyContact
273  *
274  * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
275  */
276 void
277 empathy_contact_widget_set_contact (GtkWidget *widget,
278                                     EmpathyContact *contact)
279 {
280   EmpathyContactWidget *information;
281
282   g_return_if_fail (GTK_IS_WIDGET (widget));
283   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
284
285   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
286   if (!information)
287     return;
288
289   contact_widget_set_contact (information, contact);
290 }
291
292 /**
293  * empathy_contact_widget_set_account_filter:
294  * @widget: an #EmpathyContactWidget
295  * @filter: a #EmpathyAccountChooserFilterFunc
296  * @user_data: user data to pass to @filter, or %NULL
297  *
298  * Set a filter on the #EmpathyAccountChooser included in the
299  * #EmpathyContactWidget.
300  */
301 void
302 empathy_contact_widget_set_account_filter (
303     GtkWidget *widget,
304     EmpathyAccountChooserFilterFunc filter,
305     gpointer user_data)
306 {
307   EmpathyContactWidget *information;
308   EmpathyAccountChooser *chooser;
309
310   g_return_if_fail (GTK_IS_WIDGET (widget));
311
312   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
313   if (!information)
314     return;
315
316   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
317   if (chooser)
318       empathy_account_chooser_set_filter (chooser, filter, user_data);
319 }
320   
321 static void
322 contact_widget_destroy_cb (GtkWidget *widget,
323                            EmpathyContactWidget *information)
324 {
325   contact_widget_remove_contact (information);
326
327   if (information->widget_id_timeout != 0)
328     {
329       g_source_remove (information->widget_id_timeout);
330     }
331   if (information->manager)
332     {
333       g_object_unref (information->manager);
334     }   
335
336   g_slice_free (EmpathyContactWidget, information);
337 }
338
339 static void
340 contact_widget_remove_contact (EmpathyContactWidget *information)
341 {
342   if (information->contact)
343     {
344       g_signal_handlers_disconnect_by_func (information->contact,
345           contact_widget_name_notify_cb, information);
346       g_signal_handlers_disconnect_by_func (information->contact,
347           contact_widget_presence_notify_cb, information);
348       g_signal_handlers_disconnect_by_func (information->contact,
349           contact_widget_avatar_notify_cb, information);
350       g_signal_handlers_disconnect_by_func (information->contact,
351           contact_widget_groups_notify_cb, information);
352
353       g_object_unref (information->contact);
354       g_object_unref (information->factory);
355       information->contact = NULL;
356       information->factory = NULL;
357     }
358 }
359
360 static void
361 contact_widget_set_contact (EmpathyContactWidget *information,
362                             EmpathyContact *contact)
363 {
364   if (contact == information->contact)
365     return;
366
367   contact_widget_remove_contact (information);
368   if (contact)
369     {
370       TpConnection *connection;
371
372       connection = empathy_contact_get_connection (contact);
373       information->contact = g_object_ref (contact);
374       information->factory = empathy_tp_contact_factory_dup_singleton (connection);
375     }
376
377   /* Update information for widgets */
378   contact_widget_contact_update (information);
379   contact_widget_groups_update (information);
380   contact_widget_details_update (information);
381   contact_widget_client_update (information);
382 }
383
384 static gboolean
385 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
386 {
387   contact_widget_change_contact (self);
388   return FALSE;
389 }
390
391 static void
392 contact_widget_id_changed_cb (GtkEntry *entry,
393                               EmpathyContactWidget *self)
394 {
395   if (self->widget_id_timeout != 0)
396     {   
397       g_source_remove (self->widget_id_timeout);
398     }
399
400   self->widget_id_timeout =
401     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
402         (GSourceFunc) contact_widget_id_activate_timeout, self);
403 }
404
405 static void
406 save_avatar_menu_activate_cb (GtkWidget *widget,
407                               EmpathyContactWidget *information)
408 {
409   GtkWidget *dialog;
410   EmpathyAvatar *avatar;
411   gchar *ext = NULL, *filename;
412
413   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
414       NULL,
415       GTK_FILE_CHOOSER_ACTION_SAVE,
416       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
417       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
418       NULL);
419
420   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
421       TRUE);
422
423   /* look for the avatar extension */
424   avatar = empathy_contact_get_avatar (information->contact);
425   if (avatar->format != NULL)
426     {
427       gchar **splitted;
428
429       splitted = g_strsplit (avatar->format, "/", 2);
430       if (splitted[0] != NULL && splitted[1] != NULL)
431           ext = g_strdup (splitted[1]);
432
433       g_strfreev (splitted);
434     }
435   else
436     {
437       /* Avatar was loaded from the cache so was converted to PNG */
438       ext = g_strdup ("png");
439     }
440
441   if (ext != NULL)
442     {
443       gchar *id;
444
445       id = tp_escape_as_identifier (empathy_contact_get_id (
446             information->contact));
447
448       filename = g_strdup_printf ("%s.%s", id, ext);
449       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
450
451       g_free (id);
452       g_free (ext);
453       g_free (filename);
454     }
455
456   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
457     {
458       GError *error = NULL;
459
460       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
461
462       if (!empathy_avatar_save_to_file (avatar, filename, &error))
463         {
464           /* Save error */
465           GtkWidget *dialog;
466
467           dialog = gtk_message_dialog_new (NULL, 0,
468               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 
469               _("Unable to save avatar"));
470
471           gtk_message_dialog_format_secondary_text (
472               GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
473
474           g_signal_connect (dialog, "response",
475               G_CALLBACK (gtk_widget_destroy), NULL);
476
477           gtk_window_present (GTK_WINDOW (dialog));
478
479           g_clear_error (&error);
480         }
481
482       g_free (filename);
483     }
484
485   gtk_widget_destroy (dialog);
486 }
487
488 static void
489 popup_avatar_menu (EmpathyContactWidget *information,
490                    GtkWidget *parent,
491                    GdkEventButton *event)
492 {
493   GtkWidget *menu, *item;
494   gint button, event_time;
495
496   if (information->contact == NULL ||
497       empathy_contact_get_avatar (information->contact) == NULL)
498       return;
499
500   menu = gtk_menu_new ();
501
502   /* Add "Save as..." entry */
503   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
504   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
505   gtk_widget_show (item);
506
507   g_signal_connect (item, "activate",
508       G_CALLBACK (save_avatar_menu_activate_cb), information);
509
510   if (event)
511     {
512       button = event->button;
513       event_time = event->time;
514     }
515   else
516     {
517       button = 0;
518       event_time = gtk_get_current_event_time ();
519     }
520
521   gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
522   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
523       button, event_time);
524 }
525
526 static gboolean
527 widget_avatar_popup_menu_cb (GtkWidget *widget,
528                              EmpathyContactWidget *information)
529 {
530   popup_avatar_menu (information, widget, NULL);
531
532   return TRUE;
533 }
534
535 static gboolean
536 widget_avatar_button_press_event_cb (GtkWidget *widget,
537                                      GdkEventButton *event,
538                                      EmpathyContactWidget *information)
539 {
540   /* Ignore double-clicks and triple-clicks */
541   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
542     {
543       popup_avatar_menu (information, widget, event);
544       return TRUE;
545     }
546
547   return FALSE;
548 }
549
550 static void
551 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
552                                   EmpathyAvatarChooser *avatar_chooser)
553 {
554   TpConnection *connection;
555
556   connection = empathy_account_chooser_get_connection (account_chooser);
557   g_object_set (avatar_chooser, "connection", connection, NULL);
558 }
559
560 static void
561 contact_widget_contact_setup (EmpathyContactWidget *information)
562 {
563   /* Setup account label/chooser */
564   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
565     {
566       information->widget_account = empathy_account_chooser_new ();
567
568       g_signal_connect_swapped (information->widget_account, "changed",
569             G_CALLBACK (contact_widget_change_contact),
570             information);
571     }
572   else
573     {
574       information->widget_account = gtk_label_new (NULL);
575       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
576         gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
577       }
578       gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
579     }
580   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
581            information->widget_account,
582            1, 2, 0, 1);
583   gtk_widget_show (information->widget_account);
584
585   /* Set up avatar chooser/display */
586   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
587     {
588       information->widget_avatar = empathy_avatar_chooser_new ();
589       g_signal_connect (information->widget_avatar, "changed",
590             G_CALLBACK (contact_widget_avatar_changed_cb),
591             information);
592       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
593         {
594           g_signal_connect (information->widget_account, "changed",
595               G_CALLBACK (update_avatar_chooser_account_cb),
596               information->widget_avatar);
597           update_avatar_chooser_account_cb (
598               EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
599               EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
600         }
601     }
602   else
603     {
604       information->widget_avatar = empathy_avatar_image_new ();
605
606       g_signal_connect (information->widget_avatar, "popup-menu",
607           G_CALLBACK (widget_avatar_popup_menu_cb), information);
608       g_signal_connect (information->widget_avatar, "button-press-event",
609           G_CALLBACK (widget_avatar_button_press_event_cb), information);
610     }
611
612   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
613           information->widget_avatar,
614           FALSE, FALSE,
615           6);
616   gtk_widget_show (information->widget_avatar);
617
618   /* Setup id label/entry */
619   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
620     {
621       information->widget_id = gtk_entry_new ();
622       g_signal_connect (information->widget_id, "focus-out-event",
623             G_CALLBACK (contact_widget_id_focus_out_cb),
624             information);
625       g_signal_connect (information->widget_id, "changed",
626             G_CALLBACK (contact_widget_id_changed_cb),
627             information);
628     }
629   else
630     {
631       information->widget_id = gtk_label_new (NULL);
632       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
633         gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
634       }
635       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
636     }
637   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
638            information->widget_id,
639            1, 2, 1, 2);
640   gtk_widget_show (information->widget_id);
641
642   /* Setup alias label/entry */
643   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
644     {
645       information->widget_alias = gtk_entry_new ();
646       g_signal_connect (information->widget_alias, "focus-out-event",
647             G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
648             information);
649       /* Make return activate the window default (the Close button) */
650       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
651           TRUE);
652     }
653   else
654     {
655       information->widget_alias = gtk_label_new (NULL);
656       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
657         gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
658       }
659       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
660     }
661   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
662            information->widget_alias,
663            1, 2, 2, 3);
664   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
665     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
666   }
667   gtk_widget_show (information->widget_alias);
668 }
669
670 static void
671 contact_widget_contact_update (EmpathyContactWidget *information)
672 {
673   McAccount *account = NULL;
674   const gchar *id = NULL;
675
676   /* Connect and get info from new contact */
677   if (information->contact)
678     {
679       g_signal_connect_swapped (information->contact, "notify::name",
680           G_CALLBACK (contact_widget_name_notify_cb), information);
681       g_signal_connect_swapped (information->contact, "notify::presence",
682           G_CALLBACK (contact_widget_presence_notify_cb), information);
683       g_signal_connect_swapped (information->contact,
684           "notify::presence-message",
685           G_CALLBACK (contact_widget_presence_notify_cb), information);
686       g_signal_connect_swapped (information->contact, "notify::avatar",
687           G_CALLBACK (contact_widget_avatar_notify_cb), information);
688
689       account = empathy_contact_get_account (information->contact);
690       id = empathy_contact_get_id (information->contact);
691     }
692
693   /* Update account widget */
694   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
695     {
696       if (account)
697         {
698           g_signal_handlers_block_by_func (information->widget_account,
699                    contact_widget_change_contact,
700                    information);
701           empathy_account_chooser_set_account (
702               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
703           g_signal_handlers_unblock_by_func (information->widget_account,
704               contact_widget_change_contact, information);
705         }
706     }
707   else
708     {
709       if (account)
710         {
711           const gchar *name;
712
713           name = mc_account_get_display_name (account);
714           gtk_label_set_label (GTK_LABEL (information->widget_account), name);
715         }
716     }
717
718   /* Update id widget */
719   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
720       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
721   else
722       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
723
724   /* Update other widgets */
725   if (information->contact)
726     {
727       contact_widget_name_notify_cb (information);
728       contact_widget_presence_notify_cb (information);
729       contact_widget_avatar_notify_cb (information);
730
731       gtk_widget_show (information->label_alias);
732       gtk_widget_show (information->widget_alias);
733       gtk_widget_show (information->hbox_presence);
734       gtk_widget_show (information->widget_avatar);
735     }
736   else
737     {
738       gtk_widget_hide (information->label_alias);
739       gtk_widget_hide (information->widget_alias);
740       gtk_widget_hide (information->hbox_presence);
741       gtk_widget_hide (information->widget_avatar);
742     }
743 }
744
745 static void
746 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
747                                EmpathyContact *contact,
748                                const GError *error,
749                                gpointer user_data,
750                                GObject *weak_object)
751 {
752   EmpathyContactWidget *information = user_data;
753
754   if (error != NULL)
755     {
756       DEBUG ("Error: %s", error->message);
757       return;
758     }
759
760   contact_widget_set_contact (information, contact);
761 }
762
763 static void
764 contact_widget_change_contact (EmpathyContactWidget *information)
765 {
766   EmpathyTpContactFactory *factory;
767   TpConnection *connection;
768
769   connection = empathy_account_chooser_get_connection (
770       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
771   if (!connection)
772       return;
773
774   factory = empathy_tp_contact_factory_dup_singleton (connection);
775   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
776     {
777       const gchar *id;
778
779       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
780       if (!EMP_STR_EMPTY (id))
781         {
782           empathy_tp_contact_factory_get_from_id (factory, id,
783               contact_widget_got_contact_cb, information, NULL,
784               G_OBJECT (information->vbox_contact_widget));
785         }
786     }
787   else
788     {
789       empathy_tp_contact_factory_get_from_handle (factory,
790           tp_connection_get_self_handle (connection),
791           contact_widget_got_contact_cb, information, NULL,
792           G_OBJECT (information->vbox_contact_widget));
793     }
794
795   g_object_unref (factory);
796 }
797
798 static void
799 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
800                                   EmpathyContactWidget *information)
801 {
802   const gchar *data;
803   gsize size;
804   const gchar *mime_type;
805
806   empathy_avatar_chooser_get_image_data (
807       EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
808       &data, &size, &mime_type);
809   empathy_tp_contact_factory_set_avatar (information->factory,
810       data, size, mime_type);
811 }
812
813 static gboolean
814 contact_widget_id_focus_out_cb (GtkWidget *widget,
815                                 GdkEventFocus *event,
816                                 EmpathyContactWidget *information)
817 {
818   contact_widget_change_contact (information);
819   return FALSE;
820 }
821
822 static gboolean
823 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
824                                            GdkEventFocus *event,
825                                            EmpathyContactWidget *information)
826 {
827   if (information->contact)
828     {
829       const gchar *alias;
830
831       alias = gtk_entry_get_text (GTK_ENTRY (editable));
832       empathy_tp_contact_factory_set_alias (information->factory,
833           information->contact, alias);
834     }
835
836   return FALSE;
837 }
838
839 static void
840 contact_widget_name_notify_cb (EmpathyContactWidget *information)
841 {
842   if (GTK_IS_ENTRY (information->widget_alias))
843       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
844           empathy_contact_get_name (information->contact));
845   else
846       gtk_label_set_label (GTK_LABEL (information->widget_alias),
847           empathy_contact_get_name (information->contact));
848 }
849
850 static void
851 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
852 {
853   gtk_label_set_text (GTK_LABEL (information->label_status),
854       empathy_contact_get_status (information->contact));
855   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
856       empathy_icon_name_for_contact (information->contact),
857       GTK_ICON_SIZE_BUTTON);
858 }
859
860 static void
861 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
862 {
863   EmpathyAvatar *avatar = NULL;
864
865   if (information->contact)
866       avatar = empathy_contact_get_avatar (information->contact);
867
868   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
869     {
870       g_signal_handlers_block_by_func (information->widget_avatar,
871           contact_widget_avatar_changed_cb,
872           information);
873       empathy_avatar_chooser_set (
874           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
875       g_signal_handlers_unblock_by_func (information->widget_avatar,
876           contact_widget_avatar_changed_cb, information);
877     }
878   else
879       empathy_avatar_image_set (
880           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
881 }
882
883 static void
884 contact_widget_groups_setup (EmpathyContactWidget *information)
885 {
886   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
887     {
888       information->manager = empathy_contact_manager_dup_singleton ();
889       contact_widget_model_setup (information);
890     }
891 }
892
893 static void
894 contact_widget_groups_update (EmpathyContactWidget *information)
895 {
896   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
897       information->contact)
898     {
899       g_signal_connect_swapped (information->contact, "notify::groups",
900           G_CALLBACK (contact_widget_groups_notify_cb), information);
901       contact_widget_groups_populate_data (information);
902
903       gtk_widget_show (information->vbox_groups);
904     }
905   else
906       gtk_widget_hide (information->vbox_groups);
907 }
908
909 static void
910 contact_widget_model_setup (EmpathyContactWidget *information)
911 {
912   GtkTreeView *view;
913   GtkListStore *store;
914   GtkTreeSelection *selection;
915
916   view = GTK_TREE_VIEW (information->treeview_groups);
917
918   store = gtk_list_store_new (COL_COUNT,
919       G_TYPE_STRING,   /* name */
920       G_TYPE_BOOLEAN,  /* enabled */
921       G_TYPE_BOOLEAN); /* editable */
922
923   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
924
925   selection = gtk_tree_view_get_selection (view);
926   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
927
928   contact_widget_model_populate_columns (information);
929
930   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
931       COL_NAME, GTK_SORT_ASCENDING);
932
933   g_object_unref (store);
934 }
935
936 static void
937 contact_widget_model_populate_columns (EmpathyContactWidget *information)
938 {
939   GtkTreeView *view;
940   GtkTreeModel *model;
941   GtkTreeViewColumn *column;
942   GtkCellRenderer  *renderer;
943   guint col_offset;
944
945   view = GTK_TREE_VIEW (information->treeview_groups);
946   model = gtk_tree_view_get_model (view);
947
948   renderer = gtk_cell_renderer_toggle_new ();
949   g_signal_connect (renderer, "toggled",
950       G_CALLBACK (contact_widget_cell_toggled), information);
951
952   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
953       "active", COL_ENABLED, NULL);
954
955   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
956   gtk_tree_view_column_set_fixed_width (column, 50);
957   gtk_tree_view_append_column (view, column);
958
959   renderer = gtk_cell_renderer_text_new ();
960   col_offset = gtk_tree_view_insert_column_with_attributes (view,
961       -1, _("Group"),
962       renderer,
963       "text", COL_NAME,
964       /* "editable", COL_EDITABLE, */
965       NULL);
966
967   g_object_set_data (G_OBJECT (renderer),
968       "column", GINT_TO_POINTER (COL_NAME));
969
970   column = gtk_tree_view_get_column (view, col_offset - 1);
971   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
972   gtk_tree_view_column_set_resizable (column,FALSE);
973   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
974 }
975
976 static void
977 contact_widget_groups_populate_data (EmpathyContactWidget *information)
978 {
979   GtkTreeView *view;
980   GtkListStore *store;
981   GtkTreeIter iter;
982   GList *my_groups, *l;
983   GList *all_groups;
984
985   view = GTK_TREE_VIEW (information->treeview_groups);
986   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
987   gtk_list_store_clear (store);
988
989   all_groups = empathy_contact_list_get_all_groups (
990       EMPATHY_CONTACT_LIST (information->manager));
991   my_groups = empathy_contact_list_get_groups (
992       EMPATHY_CONTACT_LIST (information->manager),
993       information->contact);
994
995   for (l = all_groups; l; l = l->next)
996     {
997       const gchar *group_str;
998       gboolean enabled;
999
1000       group_str = l->data;
1001
1002       enabled = g_list_find_custom (my_groups,
1003           group_str, (GCompareFunc) strcmp) != NULL;
1004
1005       gtk_list_store_append (store, &iter);
1006       gtk_list_store_set (store, &iter,
1007           COL_NAME, group_str,
1008           COL_EDITABLE, TRUE,
1009           COL_ENABLED, enabled,
1010           -1);
1011     }
1012
1013   g_list_foreach (all_groups, (GFunc) g_free, NULL);
1014   g_list_foreach (my_groups, (GFunc) g_free, NULL);
1015   g_list_free (all_groups);
1016   g_list_free (my_groups);
1017 }
1018
1019 static void
1020 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
1021 {
1022   /* FIXME: not implemented */
1023 }
1024
1025 static gboolean
1026 contact_widget_model_find_name (EmpathyContactWidget *information,
1027                                 const gchar *name,
1028                                 GtkTreeIter *iter)
1029 {
1030   GtkTreeView *view;
1031   GtkTreeModel *model;
1032   FindName data;
1033
1034   if (EMP_STR_EMPTY (name))
1035       return FALSE;
1036
1037   data.information = information;
1038   data.name = name;
1039   data.found = FALSE;
1040
1041   view = GTK_TREE_VIEW (information->treeview_groups);
1042   model = gtk_tree_view_get_model (view);
1043
1044   gtk_tree_model_foreach (model,
1045       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
1046       &data);
1047
1048   if (data.found == TRUE)
1049     {
1050       *iter = data.found_iter;
1051       return TRUE;
1052     }
1053
1054   return FALSE;
1055 }
1056
1057 static gboolean
1058 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1059                                         GtkTreePath *path,
1060                                         GtkTreeIter *iter,
1061                                         FindName *data)
1062 {
1063   gchar *name;
1064
1065   gtk_tree_model_get (model, iter,
1066       COL_NAME, &name,
1067       -1);
1068
1069   if (!name)
1070       return FALSE;
1071
1072   if (data->name && strcmp (data->name, name) == 0)
1073     {
1074       data->found = TRUE;
1075       data->found_iter = *iter;
1076
1077       g_free (name);
1078
1079       return TRUE;
1080     }
1081
1082   g_free (name);
1083
1084   return FALSE;
1085 }
1086
1087 static void
1088 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1089                              gchar *path_string,
1090                              EmpathyContactWidget *information)
1091 {
1092   GtkTreeView *view;
1093   GtkTreeModel *model;
1094   GtkListStore *store;
1095   GtkTreePath *path;
1096   GtkTreeIter iter;
1097   gboolean enabled;
1098   gchar *group;
1099
1100   view = GTK_TREE_VIEW (information->treeview_groups);
1101   model = gtk_tree_view_get_model (view);
1102   store = GTK_LIST_STORE (model);
1103
1104   path = gtk_tree_path_new_from_string (path_string);
1105
1106   gtk_tree_model_get_iter (model, &iter, path);
1107   gtk_tree_model_get (model, &iter,
1108       COL_ENABLED, &enabled,
1109       COL_NAME, &group,
1110       -1);
1111
1112   gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1113   gtk_tree_path_free (path);
1114
1115   if (group)
1116     {
1117       if (enabled)
1118         {
1119           empathy_contact_list_remove_from_group (
1120               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1121               group);
1122         }
1123       else
1124         {
1125           empathy_contact_list_add_to_group (
1126               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1127               group);
1128         }
1129       g_free (group);
1130     }
1131 }
1132
1133 static void
1134 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1135                                        EmpathyContactWidget *information)
1136 {
1137   GtkTreeIter iter;
1138   const gchar *group;
1139
1140   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1141
1142   if (contact_widget_model_find_name (information, group, &iter))
1143       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1144   else
1145       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1146           !EMP_STR_EMPTY (group));
1147 }
1148
1149 static void
1150 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1151                                         EmpathyContactWidget  *information)
1152 {
1153   gtk_widget_activate (GTK_WIDGET (information->button_group));
1154 }
1155
1156 static void
1157 contact_widget_button_group_clicked_cb (GtkButton *button,
1158                                         EmpathyContactWidget *information)
1159 {
1160   GtkTreeView *view;
1161   GtkListStore *store;
1162   GtkTreeIter iter;
1163   const gchar *group;
1164
1165   view = GTK_TREE_VIEW (information->treeview_groups);
1166   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1167
1168   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1169
1170   gtk_list_store_append (store, &iter);
1171   gtk_list_store_set (store, &iter,
1172       COL_NAME, group,
1173       COL_ENABLED, TRUE,
1174       -1);
1175
1176   empathy_contact_list_add_to_group (
1177       EMPATHY_CONTACT_LIST (information->manager), information->contact,
1178       group);
1179 }
1180
1181 static void
1182 contact_widget_details_setup (EmpathyContactWidget *information)
1183 {
1184   /* FIXME: Needs new telepathy spec */
1185   gtk_widget_hide (information->vbox_details);
1186 }
1187
1188 static void
1189 contact_widget_details_update (EmpathyContactWidget *information)
1190 {
1191   /* FIXME: Needs new telepathy spec */
1192 }
1193
1194 static void
1195 contact_widget_client_setup (EmpathyContactWidget *information)
1196 {
1197   /* FIXME: Needs new telepathy spec */
1198   gtk_widget_hide (information->vbox_client);
1199 }
1200
1201 static void
1202 contact_widget_client_update (EmpathyContactWidget *information)
1203 {
1204   /* FIXME: Needs new telepathy spec */
1205 }