]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Update gitignore files
[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.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 *hbow_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_new ();
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->hbow_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 contact_widget_contact_setup (EmpathyContactWidget *information)
497 {
498   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
499     {
500       information->widget_avatar = empathy_avatar_chooser_new ();
501       g_signal_connect (information->widget_avatar, "changed",
502             G_CALLBACK (contact_widget_avatar_changed_cb),
503             information);
504     }
505   else
506     {
507       information->widget_avatar = empathy_avatar_image_new ();
508
509       g_signal_connect (information->widget_avatar, "popup-menu",
510           G_CALLBACK (widget_avatar_popup_menu_cb), information);
511       g_signal_connect (information->widget_avatar, "button-press-event",
512           G_CALLBACK (widget_avatar_button_press_event_cb), information);
513     }
514   
515   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
516           information->widget_avatar,
517           FALSE, FALSE,
518           6);
519   gtk_widget_show (information->widget_avatar);
520
521   /* Setup account label/chooser */
522   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
523     {
524       information->widget_account = empathy_account_chooser_new ();
525
526       g_signal_connect (information->widget_account, "changed",
527             G_CALLBACK (contact_widget_account_changed_cb),
528             information);
529     }
530   else
531     {
532       information->widget_account = gtk_label_new (NULL);
533       gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
534       gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
535     }
536   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
537            information->widget_account,
538            1, 2, 0, 1);
539   gtk_widget_show (information->widget_account);
540
541   /* Setup id label/entry */
542   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
543     {
544       information->widget_id = gtk_entry_new ();
545       g_signal_connect (information->widget_id, "focus-out-event",
546             G_CALLBACK (contact_widget_id_focus_out_cb),
547             information);
548       g_signal_connect (information->widget_id, "changed",
549             G_CALLBACK (contact_widget_id_changed_cb),
550             information);
551     }
552   else
553     {
554       information->widget_id = gtk_label_new (NULL);
555       gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
556       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
557     }
558   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
559            information->widget_id,
560            1, 2, 1, 2);
561   gtk_widget_show (information->widget_id);
562
563   /* Setup alias label/entry */
564   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
565     {
566       information->widget_alias = gtk_entry_new ();
567       g_signal_connect (information->widget_alias, "focus-out-event",
568             G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
569             information);
570       /* Make return activate the window default (the Close button) */
571       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
572           TRUE);
573     }
574   else
575     {
576       information->widget_alias = gtk_label_new (NULL);
577       gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
578       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
579     }
580   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
581            information->widget_alias,
582            1, 2, 2, 3);
583   gtk_widget_show (information->widget_alias);
584 }
585
586 static void
587 contact_widget_contact_update (EmpathyContactWidget *information)
588 {
589   McAccount *account = NULL;
590   const gchar *id = NULL;
591
592   /* Connect and get info from new contact */
593   if (information->contact)
594     {
595       g_signal_connect_swapped (information->contact, "notify::name",
596           G_CALLBACK (contact_widget_name_notify_cb), information);
597       g_signal_connect_swapped (information->contact, "notify::presence",
598           G_CALLBACK (contact_widget_presence_notify_cb), information);
599       g_signal_connect_swapped (information->contact,
600           "notify::presence-message",
601           G_CALLBACK (contact_widget_presence_notify_cb), information);
602       g_signal_connect_swapped (information->contact, "notify::avatar",
603           G_CALLBACK (contact_widget_avatar_notify_cb), information);
604
605       account = empathy_contact_get_account (information->contact);
606       id = empathy_contact_get_id (information->contact);
607     }
608
609   /* Update account widget */
610   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
611     {
612       if (account)
613         {
614           g_signal_handlers_block_by_func (information->widget_account,
615                    contact_widget_account_changed_cb,
616                    information);
617           empathy_account_chooser_set_account (
618               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
619           g_signal_handlers_unblock_by_func (information->widget_account,
620               contact_widget_account_changed_cb, information);
621         }
622     }
623   else
624     {
625       if (account)
626         {
627           const gchar *name;
628
629           name = mc_account_get_display_name (account);
630           gtk_label_set_label (GTK_LABEL (information->widget_account), name);
631         }
632     }
633
634   /* Update id widget */
635   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
636       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
637   else
638       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
639
640   /* Update other widgets */
641   if (information->contact)
642     {
643       contact_widget_name_notify_cb (information);
644       contact_widget_presence_notify_cb (information);
645       contact_widget_avatar_notify_cb (information);
646
647       gtk_widget_show (information->label_alias);
648       gtk_widget_show (information->widget_alias);
649       gtk_widget_show (information->hbox_presence);
650       gtk_widget_show (information->widget_avatar);
651     }
652   else
653     {
654       gtk_widget_hide (information->label_alias);
655       gtk_widget_hide (information->widget_alias);
656       gtk_widget_hide (information->hbox_presence);
657       gtk_widget_hide (information->widget_avatar);
658     }
659 }
660
661 static void
662 contact_widget_change_contact (EmpathyContactWidget *information)
663 {
664   EmpathyContact *contact;
665   McAccount *account;
666
667   account = empathy_account_chooser_get_account (
668       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
669   if (!account)
670       return;
671
672   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
673     {
674       const gchar *id;
675
676       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
677       if (G_STR_EMPTY (id))
678           return;
679
680       contact = empathy_contact_factory_get_from_id (information->factory,
681           account, id);
682     }
683   else
684     {
685       contact = empathy_contact_factory_get_user (information->factory,
686           account);
687     }
688
689   if (contact)
690     {
691       empathy_contact_run_until_ready (contact,
692           EMPATHY_CONTACT_READY_HANDLE |
693           EMPATHY_CONTACT_READY_ID,
694           NULL);
695       contact_widget_set_contact (information, contact);
696       g_object_unref (contact);
697     }
698 }
699
700 static void
701 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
702                                   EmpathyContactWidget *information)
703 {
704   if (information->contact && empathy_contact_is_user (information->contact))
705     {
706       McAccount *account;
707       const gchar *data;
708       gsize size;
709       const gchar *mime_type;
710
711       account = empathy_contact_get_account (information->contact);
712       empathy_avatar_chooser_get_image_data (
713           EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
714           &data, &size, &mime_type);
715       empathy_contact_factory_set_avatar (information->factory, account,
716           data, size, mime_type);
717     }
718 }
719
720 static void
721 contact_widget_account_changed_cb (GtkComboBox *widget,
722                                    EmpathyContactWidget *information)
723 {
724   contact_widget_change_contact (information);
725 }
726
727 static gboolean
728 contact_widget_id_focus_out_cb (GtkWidget *widget,
729                                 GdkEventFocus *event,
730                                 EmpathyContactWidget *information)
731 {
732   contact_widget_change_contact (information);
733   return FALSE;
734 }
735
736 static gboolean
737 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
738                                            GdkEventFocus *event,
739                                            EmpathyContactWidget *information)
740 {
741   if (information->contact)
742     {
743       const gchar *alias;
744
745       alias = gtk_entry_get_text (GTK_ENTRY (editable));
746       empathy_contact_factory_set_alias (information->factory,
747           information->contact, alias);
748     }
749
750   return FALSE;
751 }
752
753 static void
754 contact_widget_name_notify_cb (EmpathyContactWidget *information)
755 {
756   if (GTK_IS_ENTRY (information->widget_alias))
757       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
758           empathy_contact_get_name (information->contact));
759   else
760       gtk_label_set_label (GTK_LABEL (information->widget_alias),
761           empathy_contact_get_name (information->contact));
762 }
763
764 static void
765 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
766 {
767   gtk_label_set_text (GTK_LABEL (information->label_status),
768       empathy_contact_get_status (information->contact));
769   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
770       empathy_icon_name_for_contact (information->contact),
771       GTK_ICON_SIZE_BUTTON);
772 }
773
774 static void
775 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
776 {
777   EmpathyAvatar *avatar = NULL;
778
779   if (information->contact)
780       avatar = empathy_contact_get_avatar (information->contact);
781
782   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
783     {
784       g_signal_handlers_block_by_func (information->widget_avatar,
785           contact_widget_avatar_changed_cb,
786           information);
787       empathy_avatar_chooser_set (
788           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
789       g_signal_handlers_unblock_by_func (information->widget_avatar,
790           contact_widget_avatar_changed_cb, information);
791     }
792   else
793       empathy_avatar_image_set (
794           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
795 }
796
797 static void
798 contact_widget_groups_setup (EmpathyContactWidget *information)
799 {
800   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
801     {
802       information->manager = empathy_contact_manager_new ();
803       contact_widget_model_setup (information);
804     }
805 }
806
807 static void
808 contact_widget_groups_update (EmpathyContactWidget *information)
809 {
810   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
811       information->contact)
812     {
813       g_signal_connect_swapped (information->contact, "notify::groups",
814           G_CALLBACK (contact_widget_groups_notify_cb), information);
815       contact_widget_groups_populate_data (information);
816
817       gtk_widget_show (information->vbox_groups);
818     }
819   else
820       gtk_widget_hide (information->vbox_groups);
821 }
822
823 static void
824 contact_widget_model_setup (EmpathyContactWidget *information)
825 {
826   GtkTreeView *view;
827   GtkListStore *store;
828   GtkTreeSelection *selection;
829
830   view = GTK_TREE_VIEW (information->treeview_groups);
831
832   store = gtk_list_store_new (COL_COUNT,
833       G_TYPE_STRING,   /* name */
834       G_TYPE_BOOLEAN,  /* enabled */
835       G_TYPE_BOOLEAN); /* editable */
836
837   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
838
839   selection = gtk_tree_view_get_selection (view);
840   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
841
842   contact_widget_model_populate_columns (information);
843
844   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
845       COL_NAME, GTK_SORT_ASCENDING);
846
847   g_object_unref (store);
848 }
849
850 static void
851 contact_widget_model_populate_columns (EmpathyContactWidget *information)
852 {
853   GtkTreeView *view;
854   GtkTreeModel *model;
855   GtkTreeViewColumn *column;
856   GtkCellRenderer  *renderer;
857   guint col_offset;
858
859   view = GTK_TREE_VIEW (information->treeview_groups);
860   model = gtk_tree_view_get_model (view);
861
862   renderer = gtk_cell_renderer_toggle_new ();
863   g_signal_connect (renderer, "toggled",
864       G_CALLBACK (contact_widget_cell_toggled), information);
865
866   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
867       "active", COL_ENABLED, NULL);
868
869   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
870   gtk_tree_view_column_set_fixed_width (column, 50);
871   gtk_tree_view_append_column (view, column);
872
873   renderer = gtk_cell_renderer_text_new ();
874   col_offset = gtk_tree_view_insert_column_with_attributes (view,
875       -1, _("Group"),
876       renderer,
877       "text", COL_NAME,
878       /* "editable", COL_EDITABLE, */
879       NULL);
880
881   g_object_set_data (G_OBJECT (renderer),
882       "column", GINT_TO_POINTER (COL_NAME));
883
884   column = gtk_tree_view_get_column (view, col_offset - 1);
885   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
886   gtk_tree_view_column_set_resizable (column,FALSE);
887   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
888
889   if (information->renderer)
890       g_object_unref (information->renderer);
891
892   information->renderer = g_object_ref (renderer);
893 }
894
895 static void
896 contact_widget_groups_populate_data (EmpathyContactWidget *information)
897 {
898   GtkTreeView *view;
899   GtkListStore *store;
900   GtkTreeIter iter;
901   GList *my_groups, *l;
902   GList *all_groups;
903
904   view = GTK_TREE_VIEW (information->treeview_groups);
905   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
906   gtk_list_store_clear (store);
907
908   all_groups = empathy_contact_list_get_all_groups (
909       EMPATHY_CONTACT_LIST (information->manager));
910   my_groups = empathy_contact_list_get_groups (
911       EMPATHY_CONTACT_LIST (information->manager),
912       information->contact);
913
914   for (l = all_groups; l; l = l->next)
915     {
916       const gchar *group_str;
917       gboolean enabled;
918
919       group_str = l->data;
920
921       enabled = g_list_find_custom (my_groups,
922           group_str, (GCompareFunc) strcmp) != NULL;
923
924       gtk_list_store_append (store, &iter);
925       gtk_list_store_set (store, &iter,
926           COL_NAME, group_str,
927           COL_EDITABLE, TRUE,
928           COL_ENABLED, enabled,
929           -1);
930     }
931
932   g_list_foreach (all_groups, (GFunc) g_free, NULL);
933   g_list_foreach (my_groups, (GFunc) g_free, NULL);
934   g_list_free (all_groups);
935   g_list_free (my_groups);
936 }
937
938 static void
939 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
940 {
941   /* FIXME: not implemented */
942 }
943
944 static gboolean
945 contact_widget_model_find_name (EmpathyContactWidget *information,
946                                 const gchar *name,
947                                 GtkTreeIter *iter)
948 {
949   GtkTreeView *view;
950   GtkTreeModel *model;
951   FindName data;
952
953   if (G_STR_EMPTY (name))
954       return FALSE;
955
956   data.information = information;
957   data.name = name;
958   data.found = FALSE;
959
960   view = GTK_TREE_VIEW (information->treeview_groups);
961   model = gtk_tree_view_get_model (view);
962
963   gtk_tree_model_foreach (model,
964       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
965       &data);
966
967   if (data.found == TRUE)
968     {
969       *iter = data.found_iter;
970       return TRUE;
971     }
972
973   return FALSE;
974 }
975
976 static gboolean
977 contact_widget_model_find_name_foreach (GtkTreeModel *model,
978                                         GtkTreePath *path,
979                                         GtkTreeIter *iter,
980                                         FindName *data)
981 {
982   gchar *name;
983
984   gtk_tree_model_get (model, iter,
985       COL_NAME, &name,
986       -1);
987
988   if (!name)
989       return FALSE;
990
991   if (data->name && strcmp (data->name, name) == 0)
992     {
993       data->found = TRUE;
994       data->found_iter = *iter;
995
996       g_free (name);
997
998       return TRUE;
999     }
1000
1001   g_free (name);
1002
1003   return FALSE;
1004 }
1005
1006 static void
1007 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1008                              gchar *path_string,
1009                              EmpathyContactWidget *information)
1010 {
1011   GtkTreeView *view;
1012   GtkTreeModel *model;
1013   GtkListStore *store;
1014   GtkTreePath *path;
1015   GtkTreeIter iter;
1016   gboolean enabled;
1017   gchar *group;
1018
1019   view = GTK_TREE_VIEW (information->treeview_groups);
1020   model = gtk_tree_view_get_model (view);
1021   store = GTK_LIST_STORE (model);
1022
1023   path = gtk_tree_path_new_from_string (path_string);
1024
1025   gtk_tree_model_get_iter (model, &iter, path);
1026   gtk_tree_model_get (model, &iter,
1027       COL_ENABLED, &enabled,
1028       COL_NAME, &group,
1029       -1);
1030
1031   gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1032   gtk_tree_path_free (path);
1033
1034   if (group)
1035     {
1036       if (enabled)
1037         {
1038           empathy_contact_list_remove_from_group (
1039               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1040               group);
1041         }
1042       else
1043         {
1044           empathy_contact_list_add_to_group (
1045               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1046               group);
1047         }
1048       g_free (group);
1049     }
1050 }
1051
1052 static void
1053 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1054                                        EmpathyContactWidget *information)
1055 {
1056   GtkTreeIter iter;
1057   const gchar *group;
1058
1059   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1060
1061   if (contact_widget_model_find_name (information, group, &iter))
1062       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1063   else
1064       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1065           !G_STR_EMPTY (group));
1066 }
1067
1068 static void
1069 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1070                                         EmpathyContactWidget  *information)
1071 {
1072   gtk_widget_activate (GTK_WIDGET (information->button_group));
1073 }
1074
1075 static void
1076 contact_widget_button_group_clicked_cb (GtkButton *button,
1077                                         EmpathyContactWidget *information)
1078 {
1079   GtkTreeView *view;
1080   GtkListStore *store;
1081   GtkTreeIter iter;
1082   const gchar *group;
1083
1084   view = GTK_TREE_VIEW (information->treeview_groups);
1085   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1086
1087   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1088
1089   gtk_list_store_append (store, &iter);
1090   gtk_list_store_set (store, &iter,
1091       COL_NAME, group,
1092       COL_ENABLED, TRUE,
1093       -1);
1094
1095   empathy_contact_list_add_to_group (
1096       EMPATHY_CONTACT_LIST (information->manager), information->contact,
1097       group);
1098 }
1099
1100 static void
1101 contact_widget_details_setup (EmpathyContactWidget *information)
1102 {
1103   /* FIXME: Needs new telepathy spec */
1104   gtk_widget_hide (information->vbox_details);
1105 }
1106
1107 static void
1108 contact_widget_details_update (EmpathyContactWidget *information)
1109 {
1110   /* FIXME: Needs new telepathy spec */
1111 }
1112
1113 static void
1114 contact_widget_client_setup (EmpathyContactWidget *information)
1115 {
1116   /* FIXME: Needs new telepathy spec */
1117   gtk_widget_hide (information->vbox_client);
1118 }
1119
1120 static void
1121 contact_widget_client_update (EmpathyContactWidget *information)
1122 {
1123   /* FIXME: Needs new telepathy spec */
1124 }