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