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