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