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