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