]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Merge branch 'people-nearby-fake-group-613558'
[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 <telepathy-glib/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-location.h>
42 #include <libempathy/empathy-time.h>
43 #include <libempathy/empathy-utils.h>
44
45 #include "empathy-contact-widget.h"
46 #include "empathy-account-chooser.h"
47 #include "empathy-avatar-chooser.h"
48 #include "empathy-avatar-image.h"
49 #include "empathy-ui-utils.h"
50 #include "empathy-string-parser.h"
51 #include "empathy-kludge-label.h"
52
53 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
54 #include <libempathy/empathy-debug.h>
55
56 /**
57  * SECTION:empathy-contact-widget
58  * @title:EmpathyContactWidget
59  * @short_description: A widget used to display and edit details about a contact
60  * @include: libempathy-empathy-contact-widget.h
61  *
62  * #EmpathyContactWidget is a widget which displays appropriate widgets
63  * with details about a contact, also allowing changing these details,
64  * if desired.
65  */
66
67 /**
68  * EmpathyContactWidget:
69  * @parent: parent object
70  *
71  * Widget which displays appropriate widgets with details about a contact,
72  * also allowing changing these details, if desired.
73  */
74
75 /* Delay before updating the widget when the id entry changed (seconds) */
76 #define ID_CHANGED_TIMEOUT 1
77
78 typedef struct
79 {
80   EmpathyTpContactFactory *factory;
81   EmpathyContactManager *manager;
82   EmpathyContact *contact;
83   EmpathyContactWidgetFlags flags;
84   guint widget_id_timeout;
85   gulong fav_sig_id;
86
87   GtkWidget *vbox_contact_widget;
88
89   /* Contact */
90   GtkWidget *hbox_contact;
91   GtkWidget *widget_avatar;
92   GtkWidget *widget_account;
93   GtkWidget *image_account;
94   GtkWidget *label_account;
95   GtkWidget *widget_id;
96   GtkWidget *widget_alias;
97   GtkWidget *label_alias;
98   GtkWidget *entry_alias;
99   GtkWidget *hbox_presence;
100   GtkWidget *image_state;
101   GtkWidget *label_status;
102   GtkWidget *table_contact;
103   GtkWidget *vbox_avatar;
104   GtkWidget *favourite_checkbox;
105
106   /* Location */
107   GtkWidget *vbox_location;
108   GtkWidget *subvbox_location;
109   GtkWidget *table_location;
110   GtkWidget *label_location;
111 #if HAVE_LIBCHAMPLAIN
112   GtkWidget *viewport_map;
113   GtkWidget *map_view_embed;
114   ChamplainView *map_view;
115 #endif
116
117   /* Groups */
118   GtkWidget *vbox_groups;
119   GtkWidget *entry_group;
120   GtkWidget *button_group;
121   GtkWidget *treeview_groups;
122
123   /* Details */
124   GtkWidget *vbox_details;
125   GtkWidget *table_details;
126   GtkWidget *hbox_details_requested;
127
128   /* Client */
129   GtkWidget *vbox_client;
130   GtkWidget *table_client;
131   GtkWidget *hbox_client_requested;
132 } EmpathyContactWidget;
133
134 typedef struct
135 {
136   EmpathyContactWidget *information;
137   const gchar *name;
138   gboolean found;
139   GtkTreeIter found_iter;
140 } FindName;
141
142 enum
143 {
144   COL_NAME,
145   COL_ENABLED,
146   COL_EDITABLE,
147   COL_COUNT
148 };
149
150 static void
151 contact_widget_details_setup (EmpathyContactWidget *information)
152 {
153   /* FIXME: Needs new telepathy spec */
154   gtk_widget_hide (information->vbox_details);
155 }
156
157 static void
158 contact_widget_details_update (EmpathyContactWidget *information)
159 {
160   /* FIXME: Needs new telepathy spec */
161 }
162
163 static void
164 contact_widget_client_update (EmpathyContactWidget *information)
165 {
166   /* FIXME: Needs new telepathy spec */
167 }
168
169 static void
170 contact_widget_client_setup (EmpathyContactWidget *information)
171 {
172   /* FIXME: Needs new telepathy spec */
173   gtk_widget_hide (information->vbox_client);
174 }
175
176 static void
177 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
178                              gchar *path_string,
179                              EmpathyContactWidget *information)
180 {
181   GtkTreeView *view;
182   GtkTreeModel *model;
183   GtkListStore *store;
184   GtkTreePath *path;
185   GtkTreeIter iter;
186   gboolean enabled;
187   gchar *group;
188
189   view = GTK_TREE_VIEW (information->treeview_groups);
190   model = gtk_tree_view_get_model (view);
191   store = GTK_LIST_STORE (model);
192
193   path = gtk_tree_path_new_from_string (path_string);
194
195   gtk_tree_model_get_iter (model, &iter, path);
196   gtk_tree_model_get (model, &iter,
197       COL_ENABLED, &enabled,
198       COL_NAME, &group,
199       -1);
200
201   gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
202   gtk_tree_path_free (path);
203
204   if (group)
205     {
206       if (enabled)
207         {
208           empathy_contact_list_remove_from_group (
209               EMPATHY_CONTACT_LIST (information->manager), information->contact,
210               group);
211         }
212       else
213         {
214           empathy_contact_list_add_to_group (
215               EMPATHY_CONTACT_LIST (information->manager), information->contact,
216               group);
217         }
218       g_free (group);
219     }
220 }
221
222 static void
223 contact_widget_model_populate_columns (EmpathyContactWidget *information)
224 {
225   GtkTreeView *view;
226   GtkTreeModel *model;
227   GtkTreeViewColumn *column;
228   GtkCellRenderer  *renderer;
229   guint col_offset;
230
231   view = GTK_TREE_VIEW (information->treeview_groups);
232   model = gtk_tree_view_get_model (view);
233
234   renderer = gtk_cell_renderer_toggle_new ();
235   g_signal_connect (renderer, "toggled",
236       G_CALLBACK (contact_widget_cell_toggled), information);
237
238   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
239       "active", COL_ENABLED, NULL);
240
241   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
242   gtk_tree_view_column_set_fixed_width (column, 50);
243   gtk_tree_view_append_column (view, column);
244
245   renderer = gtk_cell_renderer_text_new ();
246   col_offset = gtk_tree_view_insert_column_with_attributes (view,
247       -1, _("Group"),
248       renderer,
249       "text", COL_NAME,
250       /* "editable", COL_EDITABLE, */
251       NULL);
252
253   g_object_set_data (G_OBJECT (renderer),
254       "column", GINT_TO_POINTER (COL_NAME));
255
256   column = gtk_tree_view_get_column (view, col_offset - 1);
257   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
258   gtk_tree_view_column_set_resizable (column,FALSE);
259   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
260 }
261
262 static void
263 contact_widget_model_setup (EmpathyContactWidget *information)
264 {
265   GtkTreeView *view;
266   GtkListStore *store;
267   GtkTreeSelection *selection;
268
269   view = GTK_TREE_VIEW (information->treeview_groups);
270
271   store = gtk_list_store_new (COL_COUNT,
272       G_TYPE_STRING,   /* name */
273       G_TYPE_BOOLEAN,  /* enabled */
274       G_TYPE_BOOLEAN); /* editable */
275
276   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
277
278   selection = gtk_tree_view_get_selection (view);
279   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
280
281   contact_widget_model_populate_columns (information);
282
283   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
284       COL_NAME, GTK_SORT_ASCENDING);
285
286   g_object_unref (store);
287 }
288
289 static void
290 contact_widget_groups_populate_data (EmpathyContactWidget *information)
291 {
292   GtkTreeView *view;
293   GtkListStore *store;
294   GtkTreeIter iter;
295   GList *my_groups, *l;
296   GList *all_groups;
297
298   view = GTK_TREE_VIEW (information->treeview_groups);
299   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
300   gtk_list_store_clear (store);
301
302   all_groups = empathy_contact_list_get_all_groups (
303       EMPATHY_CONTACT_LIST (information->manager));
304   my_groups = empathy_contact_list_get_groups (
305       EMPATHY_CONTACT_LIST (information->manager),
306       information->contact);
307
308   for (l = all_groups; l; l = l->next)
309     {
310       const gchar *group_str;
311       gboolean enabled;
312
313       group_str = l->data;
314
315       enabled = g_list_find_custom (my_groups,
316           group_str, (GCompareFunc) strcmp) != NULL;
317
318       gtk_list_store_append (store, &iter);
319       gtk_list_store_set (store, &iter,
320           COL_NAME, group_str,
321           COL_EDITABLE, TRUE,
322           COL_ENABLED, enabled,
323           -1);
324     }
325
326   g_list_foreach (all_groups, (GFunc) g_free, NULL);
327   g_list_foreach (my_groups, (GFunc) g_free, NULL);
328   g_list_free (all_groups);
329   g_list_free (my_groups);
330 }
331
332 static gboolean
333 contact_widget_model_find_name_foreach (GtkTreeModel *model,
334                                         GtkTreePath *path,
335                                         GtkTreeIter *iter,
336                                         FindName *data)
337 {
338   gchar *name;
339
340   gtk_tree_model_get (model, iter,
341       COL_NAME, &name,
342       -1);
343
344   if (!name)
345       return FALSE;
346
347   if (data->name && strcmp (data->name, name) == 0)
348     {
349       data->found = TRUE;
350       data->found_iter = *iter;
351
352       g_free (name);
353
354       return TRUE;
355     }
356
357   g_free (name);
358
359   return FALSE;
360 }
361
362 static gboolean
363 contact_widget_model_find_name (EmpathyContactWidget *information,
364                                 const gchar *name,
365                                 GtkTreeIter *iter)
366 {
367   GtkTreeView *view;
368   GtkTreeModel *model;
369   FindName data;
370
371   if (EMP_STR_EMPTY (name))
372       return FALSE;
373
374   data.information = information;
375   data.name = name;
376   data.found = FALSE;
377
378   view = GTK_TREE_VIEW (information->treeview_groups);
379   model = gtk_tree_view_get_model (view);
380
381   gtk_tree_model_foreach (model,
382       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
383       &data);
384
385   if (data.found == TRUE)
386     {
387       *iter = data.found_iter;
388       return TRUE;
389     }
390
391   return FALSE;
392 }
393
394 static void
395 contact_widget_entry_group_changed_cb (GtkEditable *editable,
396                                        EmpathyContactWidget *information)
397 {
398   GtkTreeIter iter;
399   const gchar *group;
400
401   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
402
403   if (contact_widget_model_find_name (information, group, &iter))
404       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
405   else
406       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
407           !EMP_STR_EMPTY (group));
408 }
409
410 static void
411 contact_widget_entry_group_activate_cb (GtkEntry *entry,
412                                         EmpathyContactWidget  *information)
413 {
414   gtk_widget_activate (GTK_WIDGET (information->button_group));
415 }
416
417 static void
418 contact_widget_button_group_clicked_cb (GtkButton *button,
419                                         EmpathyContactWidget *information)
420 {
421   GtkTreeView *view;
422   GtkListStore *store;
423   GtkTreeIter iter;
424   const gchar *group;
425
426   view = GTK_TREE_VIEW (information->treeview_groups);
427   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
428
429   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
430
431   gtk_list_store_append (store, &iter);
432   gtk_list_store_set (store, &iter,
433       COL_NAME, group,
434       COL_ENABLED, TRUE,
435       -1);
436
437   empathy_contact_list_add_to_group (
438       EMPATHY_CONTACT_LIST (information->manager), information->contact,
439       group);
440 }
441
442 static void
443 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
444 {
445   /* FIXME: not implemented */
446 }
447
448 static void
449 contact_widget_groups_setup (EmpathyContactWidget *information)
450 {
451   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
452     {
453       contact_widget_model_setup (information);
454     }
455 }
456
457 static void
458 contact_widget_groups_update (EmpathyContactWidget *information)
459 {
460   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
461       information->contact)
462     {
463       g_signal_connect_swapped (information->contact, "notify::groups",
464           G_CALLBACK (contact_widget_groups_notify_cb), information);
465       contact_widget_groups_populate_data (information);
466
467       gtk_widget_show (information->vbox_groups);
468     }
469   else
470       gtk_widget_hide (information->vbox_groups);
471 }
472
473 /* Converts the Location's GHashTable's key to a user readable string */
474 static const gchar *
475 location_key_to_label (const gchar *key)
476 {
477   if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
478     return _("Country ISO Code:");
479   else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
480     return _("Country:");
481   else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
482     return _("State:");
483   else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
484     return _("City:");
485   else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
486     return _("Area:");
487   else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
488     return _("Postal Code:");
489   else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
490     return _("Street:");
491   else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
492     return _("Building:");
493   else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
494     return _("Floor:");
495   else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
496     return _("Room:");
497   else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
498     return _("Text:");
499   else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
500     return _("Description:");
501   else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
502     return _("URI:");
503   else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
504     return _("Accuracy Level:");
505   else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
506     return _("Error:");
507   else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
508     return _("Vertical Error (meters):");
509   else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
510     return _("Horizontal Error (meters):");
511   else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
512     return _("Speed:");
513   else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
514     return _("Bearing:");
515   else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
516     return _("Climb Speed:");
517   else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
518     return _("Last Updated on:");
519   else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
520     return _("Longitude:");
521   else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
522     return _("Latitude:");
523   else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
524     return _("Altitude:");
525   else
526   {
527     DEBUG ("Unexpected Location key: %s", key);
528     return key;
529   }
530 }
531
532 static void
533 contact_widget_location_update (EmpathyContactWidget *information)
534 {
535   GHashTable *location;
536   GValue *value;
537   gdouble lat = 0.0, lon = 0.0;
538   gboolean has_position = TRUE;
539   GtkWidget *label;
540   guint row = 0;
541   static const gchar* ordered_geolocation_keys[] = {
542     EMPATHY_LOCATION_TEXT,
543     EMPATHY_LOCATION_URI,
544     EMPATHY_LOCATION_DESCRIPTION,
545     EMPATHY_LOCATION_BUILDING,
546     EMPATHY_LOCATION_FLOOR,
547     EMPATHY_LOCATION_ROOM,
548     EMPATHY_LOCATION_STREET,
549     EMPATHY_LOCATION_AREA,
550     EMPATHY_LOCATION_LOCALITY,
551     EMPATHY_LOCATION_REGION,
552     EMPATHY_LOCATION_COUNTRY,
553     NULL
554   };
555   int i;
556   const gchar *skey;
557   gboolean display_map = FALSE;
558
559   if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
560     {
561       gtk_widget_hide (information->vbox_location);
562       return;
563     }
564
565   location = empathy_contact_get_location (information->contact);
566   if (location == NULL || g_hash_table_size (location) == 0)
567     {
568       gtk_widget_hide (information->vbox_location);
569       return;
570     }
571
572   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
573   if (value == NULL)
574       has_position = FALSE;
575   else
576       lat = g_value_get_double (value);
577
578   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
579   if (value == NULL)
580       has_position = FALSE;
581   else
582       lon = g_value_get_double (value);
583
584   value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
585   if (value == NULL)
586     {
587       gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
588       gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
589       g_free (loc);
590     }
591   else
592     {
593       gchar *user_date;
594       gchar *text;
595       gint64 stamp;
596       time_t time_;
597
598       stamp = g_value_get_int64 (value);
599       time_ = stamp;
600
601       user_date = empathy_time_to_string_relative (time_);
602
603       text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
604       gtk_label_set_markup (GTK_LABEL (information->label_location), text);
605       g_free (text);
606     }
607
608
609   /* Prepare the location information table */
610   if (information->table_location != NULL)
611     {
612       gtk_widget_destroy (information->table_location);
613     }
614
615   information->table_location = gtk_table_new (1, 2, FALSE);
616   gtk_box_pack_start (GTK_BOX (information->subvbox_location),
617       information->table_location, FALSE, FALSE, 5);
618
619
620   for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
621     {
622       const gchar* user_label;
623       GValue *gvalue;
624       char *svalue = NULL;
625
626       gvalue = g_hash_table_lookup (location, (gpointer) skey);
627       if (gvalue == NULL)
628         continue;
629
630       user_label = location_key_to_label (skey);
631
632       label = gtk_label_new (user_label);
633       gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
634       gtk_table_attach (GTK_TABLE (information->table_location),
635           label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
636       gtk_widget_show (label);
637
638       if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
639         {
640           gdouble dvalue;
641           dvalue = g_value_get_double (gvalue);
642           svalue = g_strdup_printf ("%f", dvalue);
643         }
644       else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
645         {
646           svalue = g_value_dup_string (gvalue);
647         }
648       else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
649         {
650           time_t time_;
651
652           time_ = g_value_get_int64 (value);
653           svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
654         }
655
656       if (svalue != NULL)
657         {
658           label = gtk_label_new (svalue);
659           gtk_table_attach_defaults (GTK_TABLE (information->table_location),
660               label, 1, 2, row, row + 1);
661           gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
662           gtk_widget_show (label);
663
664           if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
665             gtk_label_set_selectable (GTK_LABEL (label), TRUE);
666         }
667
668       g_free (svalue);
669       row++;
670     }
671
672 #if HAVE_LIBCHAMPLAIN
673   if (has_position &&
674       !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
675     {
676       /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
677        * windows */
678       display_map = TRUE;
679     }
680 #endif
681
682   if (row > 0)
683     {
684       /* We can display some fields */
685       gtk_widget_show (information->table_location);
686     }
687   else if (!display_map)
688     {
689       /* Can't display either fields or map */
690       gtk_widget_hide (information->vbox_location);
691       return;
692     }
693
694 #if HAVE_LIBCHAMPLAIN
695   if (display_map)
696     {
697       ClutterActor *marker;
698       ChamplainLayer *layer;
699
700       information->map_view_embed = gtk_champlain_embed_new ();
701       information->map_view = gtk_champlain_embed_get_view (
702           GTK_CHAMPLAIN_EMBED (information->map_view_embed));
703
704       gtk_container_add (GTK_CONTAINER (information->viewport_map),
705           information->map_view_embed);
706       g_object_set (G_OBJECT (information->map_view),
707           "show-license", TRUE,
708           "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
709           "zoom-level", 10,
710           NULL);
711
712       layer = champlain_layer_new ();
713       champlain_view_add_layer (information->map_view, layer);
714
715       marker = champlain_marker_new_with_text (
716           empathy_contact_get_name (information->contact), NULL, NULL, NULL);
717       champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
718       clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
719
720       champlain_view_center_on (information->map_view, lat, lon);
721       gtk_widget_show_all (information->viewport_map);
722     }
723 #endif
724
725     gtk_widget_show (information->vbox_location);
726 }
727
728 static void
729 save_avatar_menu_activate_cb (GtkWidget *widget,
730                               EmpathyContactWidget *information)
731 {
732   GtkWidget *dialog;
733   EmpathyAvatar *avatar;
734   gchar *ext = NULL, *filename;
735
736   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
737       NULL,
738       GTK_FILE_CHOOSER_ACTION_SAVE,
739       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
740       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
741       NULL);
742
743   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
744       TRUE);
745
746   /* look for the avatar extension */
747   avatar = empathy_contact_get_avatar (information->contact);
748   if (avatar->format != NULL)
749     {
750       gchar **splitted;
751
752       splitted = g_strsplit (avatar->format, "/", 2);
753       if (splitted[0] != NULL && splitted[1] != NULL)
754           ext = g_strdup (splitted[1]);
755
756       g_strfreev (splitted);
757     }
758   else
759     {
760       /* Avatar was loaded from the cache so was converted to PNG */
761       ext = g_strdup ("png");
762     }
763
764   if (ext != NULL)
765     {
766       gchar *id;
767
768       id = tp_escape_as_identifier (empathy_contact_get_id (
769             information->contact));
770
771       filename = g_strdup_printf ("%s.%s", id, ext);
772       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
773
774       g_free (id);
775       g_free (ext);
776       g_free (filename);
777     }
778
779   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
780     {
781       GError *error = NULL;
782
783       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
784
785       if (!empathy_avatar_save_to_file (avatar, filename, &error))
786         {
787           /* Save error */
788           GtkWidget *error_dialog;
789
790           error_dialog = gtk_message_dialog_new (NULL, 0,
791               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
792               _("Unable to save avatar"));
793
794           gtk_message_dialog_format_secondary_text (
795               GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
796
797           g_signal_connect (error_dialog, "response",
798               G_CALLBACK (gtk_widget_destroy), NULL);
799
800           gtk_window_present (GTK_WINDOW (error_dialog));
801
802           g_clear_error (&error);
803         }
804
805       g_free (filename);
806     }
807
808   gtk_widget_destroy (dialog);
809 }
810
811 static void
812 popup_avatar_menu (EmpathyContactWidget *information,
813                    GtkWidget *parent,
814                    GdkEventButton *event)
815 {
816   GtkWidget *menu, *item;
817   gint button, event_time;
818
819   if (information->contact == NULL ||
820       empathy_contact_get_avatar (information->contact) == NULL)
821       return;
822
823   menu = gtk_menu_new ();
824
825   /* Add "Save as..." entry */
826   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
827   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
828   gtk_widget_show (item);
829
830   g_signal_connect (item, "activate",
831       G_CALLBACK (save_avatar_menu_activate_cb), information);
832
833   if (event)
834     {
835       button = event->button;
836       event_time = event->time;
837     }
838   else
839     {
840       button = 0;
841       event_time = gtk_get_current_event_time ();
842     }
843
844   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
845       button, event_time);
846   g_object_ref_sink (menu);
847   g_object_unref (menu);
848 }
849
850 static gboolean
851 widget_avatar_popup_menu_cb (GtkWidget *widget,
852                              EmpathyContactWidget *information)
853 {
854   popup_avatar_menu (information, widget, NULL);
855
856   return TRUE;
857 }
858
859 static gboolean
860 widget_avatar_button_press_event_cb (GtkWidget *widget,
861                                      GdkEventButton *event,
862                                      EmpathyContactWidget *information)
863 {
864   /* Ignore double-clicks and triple-clicks */
865   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
866     {
867       popup_avatar_menu (information, widget, event);
868       return TRUE;
869     }
870
871   return FALSE;
872 }
873
874 static void
875 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
876                                   EmpathyContactWidget *information)
877 {
878   const gchar *data;
879   gsize size;
880   const gchar *mime_type;
881
882   empathy_avatar_chooser_get_image_data (
883       EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
884       &data, &size, &mime_type);
885   empathy_tp_contact_factory_set_avatar (information->factory,
886       data, size, mime_type);
887 }
888
889 static gboolean
890 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
891                                            GdkEventFocus *event,
892                                            EmpathyContactWidget *information)
893 {
894   if (information->contact)
895     {
896       const gchar *alias;
897
898       alias = gtk_entry_get_text (GTK_ENTRY (editable));
899       empathy_tp_contact_factory_set_alias (information->factory,
900           information->contact, alias);
901     }
902
903   return FALSE;
904 }
905
906 static void
907 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
908                                   EmpathyAvatarChooser *avatar_chooser)
909 {
910   TpConnection *connection;
911
912   connection = empathy_account_chooser_get_connection (account_chooser);
913   g_object_set (avatar_chooser, "connection", connection, NULL);
914 }
915
916 static void
917 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
918 {
919   EmpathyAvatar *avatar = NULL;
920
921   if (information->contact)
922       avatar = empathy_contact_get_avatar (information->contact);
923
924   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
925     {
926       g_signal_handlers_block_by_func (information->widget_avatar,
927           contact_widget_avatar_changed_cb,
928           information);
929       empathy_avatar_chooser_set (
930           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
931       g_signal_handlers_unblock_by_func (information->widget_avatar,
932           contact_widget_avatar_changed_cb, information);
933     }
934   else
935       empathy_avatar_image_set (
936           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
937 }
938
939 static void
940 contact_widget_name_notify_cb (EmpathyContactWidget *information)
941 {
942   if (GTK_IS_ENTRY (information->widget_alias))
943       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
944           empathy_contact_get_name (information->contact));
945   else
946       gtk_label_set_label (GTK_LABEL (information->widget_alias),
947           empathy_contact_get_name (information->contact));
948 }
949
950 static void
951 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
952 {
953   const gchar *status;
954   gchar *markup_text = NULL;
955
956   status = empathy_contact_get_status (information->contact);
957   if (status != NULL)
958     markup_text = empathy_add_link_markup (status);
959   gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
960   g_free (markup_text);
961
962   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
963       empathy_icon_name_for_contact (information->contact),
964       GTK_ICON_SIZE_BUTTON);
965   gtk_widget_show (information->image_state);
966 }
967
968 #if HAVE_FAVOURITE_CONTACTS
969 static void
970 contact_widget_favourites_changed_cb (EmpathyContactManager *manager,
971     EmpathyContact *contact,
972     gboolean is_favourite,
973     EmpathyContactWidget *information)
974 {
975   if (contact != information->contact)
976     return;
977
978   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
979             information->favourite_checkbox), is_favourite);
980 }
981 #endif
982
983 static void
984 contact_widget_remove_contact (EmpathyContactWidget *information)
985 {
986   if (information->contact)
987     {
988       g_signal_handlers_disconnect_by_func (information->contact,
989           contact_widget_name_notify_cb, information);
990       g_signal_handlers_disconnect_by_func (information->contact,
991           contact_widget_presence_notify_cb, information);
992       g_signal_handlers_disconnect_by_func (information->contact,
993           contact_widget_avatar_notify_cb, information);
994       g_signal_handlers_disconnect_by_func (information->contact,
995           contact_widget_groups_notify_cb, information);
996
997       g_object_unref (information->contact);
998       g_object_unref (information->factory);
999       information->contact = NULL;
1000       information->factory = NULL;
1001     }
1002 }
1003
1004 static void contact_widget_change_contact (EmpathyContactWidget *information);
1005
1006 static void
1007 contact_widget_contact_update (EmpathyContactWidget *information)
1008 {
1009   TpAccount *account = NULL;
1010   const gchar *id = NULL;
1011
1012   /* Connect and get info from new contact */
1013   if (information->contact)
1014     {
1015       g_signal_connect_swapped (information->contact, "notify::name",
1016           G_CALLBACK (contact_widget_name_notify_cb), information);
1017       g_signal_connect_swapped (information->contact, "notify::presence",
1018           G_CALLBACK (contact_widget_presence_notify_cb), information);
1019       g_signal_connect_swapped (information->contact,
1020           "notify::presence-message",
1021           G_CALLBACK (contact_widget_presence_notify_cb), information);
1022       g_signal_connect_swapped (information->contact, "notify::avatar",
1023           G_CALLBACK (contact_widget_avatar_notify_cb), information);
1024
1025       account = empathy_contact_get_account (information->contact);
1026       id = empathy_contact_get_id (information->contact);
1027     }
1028
1029   /* Update account widget */
1030   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1031     {
1032       if (account)
1033         {
1034           g_signal_handlers_block_by_func (information->widget_account,
1035                    contact_widget_change_contact,
1036                    information);
1037           empathy_account_chooser_set_account (
1038               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1039           g_signal_handlers_unblock_by_func (information->widget_account,
1040               contact_widget_change_contact, information);
1041         }
1042     }
1043   else
1044     {
1045       if (account)
1046         {
1047           const gchar *name;
1048
1049           name = tp_account_get_display_name (account);
1050           gtk_label_set_label (GTK_LABEL (information->label_account), name);
1051
1052           name = tp_account_get_icon_name (account);
1053           gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1054               name, GTK_ICON_SIZE_MENU);
1055         }
1056     }
1057
1058   /* Update id widget */
1059   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1060       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1061   else
1062       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1063
1064   /* Update other widgets */
1065   if (information->contact)
1066     {
1067       contact_widget_name_notify_cb (information);
1068       contact_widget_presence_notify_cb (information);
1069       contact_widget_avatar_notify_cb (information);
1070
1071 #if HAVE_FAVOURITE_CONTACTS
1072       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1073         {
1074           gboolean is_favourite;
1075
1076           is_favourite = empathy_contact_list_is_favourite (
1077               EMPATHY_CONTACT_LIST (information->manager),
1078               information->contact);
1079
1080           contact_widget_favourites_changed_cb (information->manager,
1081               information->contact, is_favourite, information);
1082         }
1083 #endif
1084
1085       gtk_widget_show (information->label_alias);
1086       gtk_widget_show (information->widget_alias);
1087       gtk_widget_show (information->hbox_presence);
1088       gtk_widget_show (information->widget_avatar);
1089     }
1090   else
1091     {
1092       gtk_widget_hide (information->label_alias);
1093       gtk_widget_hide (information->widget_alias);
1094       gtk_widget_hide (information->hbox_presence);
1095       gtk_widget_hide (information->widget_avatar);
1096     }
1097 }
1098
1099 static void
1100 contact_widget_set_contact (EmpathyContactWidget *information,
1101                             EmpathyContact *contact)
1102 {
1103   if (contact == information->contact)
1104     return;
1105
1106   contact_widget_remove_contact (information);
1107   if (contact)
1108     {
1109       TpConnection *connection;
1110
1111       connection = empathy_contact_get_connection (contact);
1112       information->contact = g_object_ref (contact);
1113       information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1114     }
1115
1116   /* set the selected account to be the account this contact came from */
1117   if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1118       empathy_account_chooser_set_account (
1119                       EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1120                       empathy_contact_get_account (contact));
1121   }
1122
1123   /* Update information for widgets */
1124   contact_widget_contact_update (information);
1125   contact_widget_groups_update (information);
1126   contact_widget_details_update (information);
1127   contact_widget_client_update (information);
1128   contact_widget_location_update (information);
1129 }
1130
1131 static void
1132 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1133                                EmpathyContact *contact,
1134                                const GError *error,
1135                                gpointer user_data,
1136                                GObject *weak_object)
1137 {
1138   EmpathyContactWidget *information = user_data;
1139
1140   if (error != NULL)
1141     {
1142       DEBUG ("Error: %s", error->message);
1143       return;
1144     }
1145
1146   contact_widget_set_contact (information, contact);
1147 }
1148
1149 static void
1150 contact_widget_change_contact (EmpathyContactWidget *information)
1151 {
1152   EmpathyTpContactFactory *factory;
1153   TpConnection *connection;
1154
1155   connection = empathy_account_chooser_get_connection (
1156       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1157   if (!connection)
1158       return;
1159
1160   factory = empathy_tp_contact_factory_dup_singleton (connection);
1161   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1162     {
1163       const gchar *id;
1164
1165       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1166       if (!EMP_STR_EMPTY (id))
1167         {
1168           empathy_tp_contact_factory_get_from_id (factory, id,
1169               contact_widget_got_contact_cb, information, NULL,
1170               G_OBJECT (information->vbox_contact_widget));
1171         }
1172     }
1173   else
1174     {
1175       empathy_tp_contact_factory_get_from_handle (factory,
1176           tp_connection_get_self_handle (connection),
1177           contact_widget_got_contact_cb, information, NULL,
1178           G_OBJECT (information->vbox_contact_widget));
1179     }
1180
1181   g_object_unref (factory);
1182 }
1183
1184 static gboolean
1185 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1186 {
1187   contact_widget_change_contact (self);
1188   return FALSE;
1189 }
1190
1191 static void
1192 contact_widget_id_changed_cb (GtkEntry *entry,
1193                               EmpathyContactWidget *self)
1194 {
1195   if (self->widget_id_timeout != 0)
1196     {
1197       g_source_remove (self->widget_id_timeout);
1198     }
1199
1200   self->widget_id_timeout =
1201     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1202         (GSourceFunc) contact_widget_id_activate_timeout, self);
1203 }
1204
1205 static gboolean
1206 contact_widget_id_focus_out_cb (GtkWidget *widget,
1207                                 GdkEventFocus *event,
1208                                 EmpathyContactWidget *information)
1209 {
1210   contact_widget_change_contact (information);
1211   return FALSE;
1212 }
1213
1214 #if HAVE_FAVOURITE_CONTACTS
1215 static void
1216 favourite_toggled_cb (GtkToggleButton *button,
1217     EmpathyContactWidget *information)
1218 {
1219   gboolean active;
1220
1221   active = gtk_toggle_button_get_active (button);
1222
1223   if (active)
1224     {
1225       empathy_contact_list_add_to_favourites (
1226           EMPATHY_CONTACT_LIST (information->manager), information->contact);
1227     }
1228   else
1229     {
1230       empathy_contact_list_remove_from_favourites (
1231           EMPATHY_CONTACT_LIST (information->manager), information->contact);
1232     }
1233 }
1234 #endif
1235
1236 static void
1237 contact_widget_contact_setup (EmpathyContactWidget *information)
1238 {
1239   /* Setup label_status as a KludgeLabel */
1240   information->label_status = empathy_kludge_label_new ("");
1241   gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1242                                 PANGO_WRAP_WORD_CHAR);
1243   gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1244                            TRUE);
1245
1246   if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1247     gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1248
1249   gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1250         information->label_status, TRUE, TRUE, 0);
1251   gtk_widget_show (information->label_status);
1252
1253   /* Setup account label/chooser */
1254   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1255     {
1256       information->widget_account = empathy_account_chooser_new ();
1257
1258       g_signal_connect_swapped (information->widget_account, "changed",
1259             G_CALLBACK (contact_widget_change_contact),
1260             information);
1261     }
1262   else
1263     {
1264       /* Pack the protocol icon with the account name in an hbox */
1265       information->widget_account = gtk_hbox_new (FALSE, 6);
1266
1267       information->label_account = gtk_label_new (NULL);
1268       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1269         gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1270       }
1271       gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1272       gtk_widget_show (information->label_account);
1273
1274       information->image_account = gtk_image_new ();
1275       gtk_widget_show (information->image_account);
1276
1277       gtk_box_pack_start (GTK_BOX (information->widget_account),
1278           information->image_account, FALSE, FALSE, 0);
1279       gtk_box_pack_start (GTK_BOX (information->widget_account),
1280           information->label_account, FALSE, TRUE, 0);
1281     }
1282   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1283            information->widget_account,
1284            1, 2, 0, 1);
1285   gtk_widget_show (information->widget_account);
1286
1287   /* Set up avatar chooser/display */
1288   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1289     {
1290       information->widget_avatar = empathy_avatar_chooser_new ();
1291       g_signal_connect (information->widget_avatar, "changed",
1292             G_CALLBACK (contact_widget_avatar_changed_cb),
1293             information);
1294       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1295         {
1296           g_signal_connect (information->widget_account, "changed",
1297               G_CALLBACK (update_avatar_chooser_account_cb),
1298               information->widget_avatar);
1299           update_avatar_chooser_account_cb (
1300               EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1301               EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1302         }
1303     }
1304   else
1305     {
1306       information->widget_avatar = empathy_avatar_image_new ();
1307
1308       g_signal_connect (information->widget_avatar, "popup-menu",
1309           G_CALLBACK (widget_avatar_popup_menu_cb), information);
1310       g_signal_connect (information->widget_avatar, "button-press-event",
1311           G_CALLBACK (widget_avatar_button_press_event_cb), information);
1312     }
1313
1314   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1315           information->widget_avatar,
1316           FALSE, FALSE,
1317           6);
1318   gtk_widget_show (information->widget_avatar);
1319
1320   /* Setup id label/entry */
1321   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1322     {
1323       information->widget_id = gtk_entry_new ();
1324       g_signal_connect (information->widget_id, "focus-out-event",
1325             G_CALLBACK (contact_widget_id_focus_out_cb),
1326             information);
1327       g_signal_connect (information->widget_id, "changed",
1328             G_CALLBACK (contact_widget_id_changed_cb),
1329             information);
1330     }
1331   else
1332     {
1333       information->widget_id = gtk_label_new (NULL);
1334       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1335         gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1336       }
1337       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1338     }
1339   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1340            information->widget_id,
1341            1, 2, 1, 2);
1342   gtk_widget_show (information->widget_id);
1343
1344   /* Setup alias label/entry */
1345   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1346     {
1347       information->widget_alias = gtk_entry_new ();
1348
1349       if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1350         g_signal_connect (information->widget_alias, "focus-out-event",
1351               G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1352               information);
1353
1354       /* Make return activate the window default (the Close button) */
1355       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1356           TRUE);
1357     }
1358   else
1359     {
1360       information->widget_alias = gtk_label_new (NULL);
1361       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1362         gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1363       }
1364       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1365     }
1366   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1367            information->widget_alias,
1368            1, 2, 2, 3);
1369   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1370     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1371   }
1372   gtk_widget_show (information->widget_alias);
1373
1374 #if HAVE_FAVOURITE_CONTACTS
1375   /* Favorite */
1376   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1377     {
1378       information->favourite_checkbox = gtk_check_button_new_with_label (
1379           _("Favorite"));
1380
1381       g_signal_connect (information->favourite_checkbox, "toggled",
1382           G_CALLBACK (favourite_toggled_cb), information);
1383
1384       gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1385            information->favourite_checkbox, 0, 2, 3, 4);
1386
1387       information->fav_sig_id = g_signal_connect (information->manager,
1388           "favourites-changed",
1389           G_CALLBACK (contact_widget_favourites_changed_cb), information);
1390
1391       gtk_widget_show (information->favourite_checkbox);
1392     }
1393 #endif
1394 }
1395
1396 static void
1397 contact_widget_destroy_cb (GtkWidget *widget,
1398                            EmpathyContactWidget *information)
1399 {
1400   contact_widget_remove_contact (information);
1401
1402   if (information->widget_id_timeout != 0)
1403     {
1404       g_source_remove (information->widget_id_timeout);
1405     }
1406
1407   if (information->fav_sig_id != 0)
1408     g_signal_handler_disconnect (information->manager, information->fav_sig_id);
1409
1410   g_object_unref (information->manager);
1411
1412   g_slice_free (EmpathyContactWidget, information);
1413 }
1414
1415 /**
1416  * empathy_contact_widget_new:
1417  * @contact: an #EmpathyContact
1418  * @flags: #EmpathyContactWidgetFlags for the new contact widget
1419  *
1420  * Creates a new #EmpathyContactWidget.
1421  *
1422  * Return value: a new #EmpathyContactWidget
1423  */
1424 GtkWidget *
1425 empathy_contact_widget_new (EmpathyContact *contact,
1426                             EmpathyContactWidgetFlags flags)
1427 {
1428   EmpathyContactWidget *information;
1429   GtkBuilder *gui;
1430   gchar *filename;
1431
1432   g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1433
1434   information = g_slice_new0 (EmpathyContactWidget);
1435   information->flags = flags;
1436
1437   filename = empathy_file_lookup ("empathy-contact-widget.ui",
1438       "libempathy-gtk");
1439   gui = empathy_builder_get_file (filename,
1440        "vbox_contact_widget", &information->vbox_contact_widget,
1441        "hbox_contact", &information->hbox_contact,
1442        "hbox_presence", &information->hbox_presence,
1443        "label_alias", &information->label_alias,
1444        "image_state", &information->image_state,
1445        "table_contact", &information->table_contact,
1446        "vbox_avatar", &information->vbox_avatar,
1447        "vbox_location", &information->vbox_location,
1448        "subvbox_location", &information->subvbox_location,
1449        "label_location", &information->label_location,
1450 #if HAVE_LIBCHAMPLAIN
1451        "viewport_map", &information->viewport_map,
1452 #endif
1453        "vbox_groups", &information->vbox_groups,
1454        "entry_group", &information->entry_group,
1455        "button_group", &information->button_group,
1456        "treeview_groups", &information->treeview_groups,
1457        "vbox_details", &information->vbox_details,
1458        "table_details", &information->table_details,
1459        "hbox_details_requested", &information->hbox_details_requested,
1460        "vbox_client", &information->vbox_client,
1461        "table_client", &information->table_client,
1462        "hbox_client_requested", &information->hbox_client_requested,
1463        NULL);
1464   g_free (filename);
1465
1466   empathy_builder_connect (gui, information,
1467       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1468       "entry_group", "changed", contact_widget_entry_group_changed_cb,
1469       "entry_group", "activate", contact_widget_entry_group_activate_cb,
1470       "button_group", "clicked", contact_widget_button_group_clicked_cb,
1471       NULL);
1472   information->table_location = NULL;
1473
1474   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1475       "EmpathyContactWidget",
1476       information);
1477
1478   information->manager = empathy_contact_manager_dup_singleton ();
1479
1480   /* Create widgets */
1481   contact_widget_contact_setup (information);
1482   contact_widget_groups_setup (information);
1483   contact_widget_details_setup (information);
1484   contact_widget_client_setup (information);
1485
1486   if (contact != NULL)
1487     contact_widget_set_contact (information, contact);
1488   else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1489       information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1490     contact_widget_change_contact (information);
1491
1492   return empathy_builder_unref_and_keep_widget (gui,
1493     information->vbox_contact_widget);
1494 }
1495
1496 /**
1497  * empathy_contact_widget_get_contact:
1498  * @widget: an #EmpathyContactWidget
1499  *
1500  * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1501  *
1502  * Returns: the #EmpathyContact associated with @widget
1503  */
1504 EmpathyContact *
1505 empathy_contact_widget_get_contact (GtkWidget *widget)
1506 {
1507   EmpathyContactWidget *information;
1508
1509   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1510
1511   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1512   if (!information)
1513       return NULL;
1514
1515   return information->contact;
1516 }
1517
1518 const gchar *
1519 empathy_contact_widget_get_alias (GtkWidget *widget)
1520 {
1521   EmpathyContactWidget *information;
1522
1523   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1524
1525   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1526   if (!information)
1527       return NULL;
1528
1529   return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1530 }
1531
1532 /**
1533  * empathy_contact_widget_set_contact:
1534  * @widget: an #EmpathyContactWidget
1535  * @contact: a different #EmpathyContact
1536  *
1537  * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1538  */
1539 void
1540 empathy_contact_widget_set_contact (GtkWidget *widget,
1541                                     EmpathyContact *contact)
1542 {
1543   EmpathyContactWidget *information;
1544
1545   g_return_if_fail (GTK_IS_WIDGET (widget));
1546   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1547
1548   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1549   if (!information)
1550     return;
1551
1552   contact_widget_set_contact (information, contact);
1553 }
1554
1555 /**
1556  * empathy_contact_widget_set_account_filter:
1557  * @widget: an #EmpathyContactWidget
1558  * @filter: a #EmpathyAccountChooserFilterFunc
1559  * @user_data: user data to pass to @filter, or %NULL
1560  *
1561  * Set a filter on the #EmpathyAccountChooser included in the
1562  * #EmpathyContactWidget.
1563  */
1564 void
1565 empathy_contact_widget_set_account_filter (
1566     GtkWidget *widget,
1567     EmpathyAccountChooserFilterFunc filter,
1568     gpointer user_data)
1569 {
1570   EmpathyContactWidget *information;
1571   EmpathyAccountChooser *chooser;
1572
1573   g_return_if_fail (GTK_IS_WIDGET (widget));
1574
1575   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1576   if (!information)
1577     return;
1578
1579   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1580   if (chooser)
1581       empathy_account_chooser_set_filter (chooser, filter, user_data);
1582 }
1583