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