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