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