]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Don't display context menu when right clicking on a fake group
[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 = NULL;
954
955   status = empathy_contact_get_status (information->contact);
956   if (status != NULL)
957     markup_text = empathy_add_link_markup (status);
958   gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
959   g_free (markup_text);
960
961   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
962       empathy_icon_name_for_contact (information->contact),
963       GTK_ICON_SIZE_BUTTON);
964   gtk_widget_show (information->image_state);
965 }
966
967 static void
968 contact_widget_remove_contact (EmpathyContactWidget *information)
969 {
970   if (information->contact)
971     {
972       g_signal_handlers_disconnect_by_func (information->contact,
973           contact_widget_name_notify_cb, information);
974       g_signal_handlers_disconnect_by_func (information->contact,
975           contact_widget_presence_notify_cb, information);
976       g_signal_handlers_disconnect_by_func (information->contact,
977           contact_widget_avatar_notify_cb, information);
978       g_signal_handlers_disconnect_by_func (information->contact,
979           contact_widget_groups_notify_cb, information);
980
981       g_object_unref (information->contact);
982       g_object_unref (information->factory);
983       information->contact = NULL;
984       information->factory = NULL;
985     }
986 }
987
988 static void contact_widget_change_contact (EmpathyContactWidget *information);
989
990 static void
991 contact_widget_contact_update (EmpathyContactWidget *information)
992 {
993   TpAccount *account = NULL;
994   const gchar *id = NULL;
995
996   /* Connect and get info from new contact */
997   if (information->contact)
998     {
999       g_signal_connect_swapped (information->contact, "notify::name",
1000           G_CALLBACK (contact_widget_name_notify_cb), information);
1001       g_signal_connect_swapped (information->contact, "notify::presence",
1002           G_CALLBACK (contact_widget_presence_notify_cb), information);
1003       g_signal_connect_swapped (information->contact,
1004           "notify::presence-message",
1005           G_CALLBACK (contact_widget_presence_notify_cb), information);
1006       g_signal_connect_swapped (information->contact, "notify::avatar",
1007           G_CALLBACK (contact_widget_avatar_notify_cb), information);
1008
1009       account = empathy_contact_get_account (information->contact);
1010       id = empathy_contact_get_id (information->contact);
1011     }
1012
1013   /* Update account widget */
1014   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1015     {
1016       if (account)
1017         {
1018           g_signal_handlers_block_by_func (information->widget_account,
1019                    contact_widget_change_contact,
1020                    information);
1021           empathy_account_chooser_set_account (
1022               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1023           g_signal_handlers_unblock_by_func (information->widget_account,
1024               contact_widget_change_contact, information);
1025         }
1026     }
1027   else
1028     {
1029       if (account)
1030         {
1031           const gchar *name;
1032
1033           name = tp_account_get_display_name (account);
1034           gtk_label_set_label (GTK_LABEL (information->label_account), name);
1035
1036           name = tp_account_get_icon_name (account);
1037           gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1038               name, GTK_ICON_SIZE_MENU);
1039         }
1040     }
1041
1042   /* Update id widget */
1043   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1044       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1045   else
1046       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1047
1048   /* Update other widgets */
1049   if (information->contact)
1050     {
1051       contact_widget_name_notify_cb (information);
1052       contact_widget_presence_notify_cb (information);
1053       contact_widget_avatar_notify_cb (information);
1054
1055       gtk_widget_show (information->label_alias);
1056       gtk_widget_show (information->widget_alias);
1057       gtk_widget_show (information->hbox_presence);
1058       gtk_widget_show (information->widget_avatar);
1059     }
1060   else
1061     {
1062       gtk_widget_hide (information->label_alias);
1063       gtk_widget_hide (information->widget_alias);
1064       gtk_widget_hide (information->hbox_presence);
1065       gtk_widget_hide (information->widget_avatar);
1066     }
1067 }
1068
1069 static void
1070 contact_widget_set_contact (EmpathyContactWidget *information,
1071                             EmpathyContact *contact)
1072 {
1073   if (contact == information->contact)
1074     return;
1075
1076   contact_widget_remove_contact (information);
1077   if (contact)
1078     {
1079       TpConnection *connection;
1080
1081       connection = empathy_contact_get_connection (contact);
1082       information->contact = g_object_ref (contact);
1083       information->factory = empathy_tp_contact_factory_dup_singleton (connection);
1084     }
1085
1086   /* set the selected account to be the account this contact came from */
1087   if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1088       empathy_account_chooser_set_account (
1089                       EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1090                       empathy_contact_get_account (contact));
1091   }
1092
1093   /* Update information for widgets */
1094   contact_widget_contact_update (information);
1095   contact_widget_groups_update (information);
1096   contact_widget_details_update (information);
1097   contact_widget_client_update (information);
1098   contact_widget_location_update (information);
1099 }
1100
1101 static void
1102 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
1103                                EmpathyContact *contact,
1104                                const GError *error,
1105                                gpointer user_data,
1106                                GObject *weak_object)
1107 {
1108   EmpathyContactWidget *information = user_data;
1109
1110   if (error != NULL)
1111     {
1112       DEBUG ("Error: %s", error->message);
1113       return;
1114     }
1115
1116   contact_widget_set_contact (information, contact);
1117 }
1118
1119 static void
1120 contact_widget_change_contact (EmpathyContactWidget *information)
1121 {
1122   EmpathyTpContactFactory *factory;
1123   TpConnection *connection;
1124
1125   connection = empathy_account_chooser_get_connection (
1126       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1127   if (!connection)
1128       return;
1129
1130   factory = empathy_tp_contact_factory_dup_singleton (connection);
1131   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1132     {
1133       const gchar *id;
1134
1135       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1136       if (!EMP_STR_EMPTY (id))
1137         {
1138           empathy_tp_contact_factory_get_from_id (factory, id,
1139               contact_widget_got_contact_cb, information, NULL,
1140               G_OBJECT (information->vbox_contact_widget));
1141         }
1142     }
1143   else
1144     {
1145       empathy_tp_contact_factory_get_from_handle (factory,
1146           tp_connection_get_self_handle (connection),
1147           contact_widget_got_contact_cb, information, NULL,
1148           G_OBJECT (information->vbox_contact_widget));
1149     }
1150
1151   g_object_unref (factory);
1152 }
1153
1154 static gboolean
1155 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1156 {
1157   contact_widget_change_contact (self);
1158   return FALSE;
1159 }
1160
1161 static void
1162 contact_widget_id_changed_cb (GtkEntry *entry,
1163                               EmpathyContactWidget *self)
1164 {
1165   if (self->widget_id_timeout != 0)
1166     {
1167       g_source_remove (self->widget_id_timeout);
1168     }
1169
1170   self->widget_id_timeout =
1171     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1172         (GSourceFunc) contact_widget_id_activate_timeout, self);
1173 }
1174
1175 static gboolean
1176 contact_widget_id_focus_out_cb (GtkWidget *widget,
1177                                 GdkEventFocus *event,
1178                                 EmpathyContactWidget *information)
1179 {
1180   contact_widget_change_contact (information);
1181   return FALSE;
1182 }
1183
1184 static void
1185 contact_widget_contact_setup (EmpathyContactWidget *information)
1186 {
1187   /* Setup label_status as a KludgeLabel */
1188   information->label_status = empathy_kludge_label_new ("");
1189   gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1190                                 PANGO_WRAP_WORD_CHAR);
1191   gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1192                            TRUE);
1193
1194   if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1195     gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1196
1197   gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1198         information->label_status, TRUE, TRUE, 0);
1199   gtk_widget_show (information->label_status);
1200
1201   /* Setup account label/chooser */
1202   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1203     {
1204       information->widget_account = empathy_account_chooser_new ();
1205
1206       g_signal_connect_swapped (information->widget_account, "changed",
1207             G_CALLBACK (contact_widget_change_contact),
1208             information);
1209     }
1210   else
1211     {
1212       /* Pack the protocol icon with the account name in an hbox */
1213       information->widget_account = gtk_hbox_new (FALSE, 6);
1214
1215       information->label_account = gtk_label_new (NULL);
1216       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1217         gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1218       }
1219       gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1220       gtk_widget_show (information->label_account);
1221
1222       information->image_account = gtk_image_new ();
1223       gtk_widget_show (information->image_account);
1224
1225       gtk_box_pack_start (GTK_BOX (information->widget_account),
1226           information->image_account, FALSE, FALSE, 0);
1227       gtk_box_pack_start (GTK_BOX (information->widget_account),
1228           information->label_account, FALSE, TRUE, 0);
1229     }
1230   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1231            information->widget_account,
1232            1, 2, 0, 1);
1233   gtk_widget_show (information->widget_account);
1234
1235   /* Set up avatar chooser/display */
1236   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1237     {
1238       information->widget_avatar = empathy_avatar_chooser_new ();
1239       g_signal_connect (information->widget_avatar, "changed",
1240             G_CALLBACK (contact_widget_avatar_changed_cb),
1241             information);
1242       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1243         {
1244           g_signal_connect (information->widget_account, "changed",
1245               G_CALLBACK (update_avatar_chooser_account_cb),
1246               information->widget_avatar);
1247           update_avatar_chooser_account_cb (
1248               EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1249               EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1250         }
1251     }
1252   else
1253     {
1254       information->widget_avatar = empathy_avatar_image_new ();
1255
1256       g_signal_connect (information->widget_avatar, "popup-menu",
1257           G_CALLBACK (widget_avatar_popup_menu_cb), information);
1258       g_signal_connect (information->widget_avatar, "button-press-event",
1259           G_CALLBACK (widget_avatar_button_press_event_cb), information);
1260     }
1261
1262   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1263           information->widget_avatar,
1264           FALSE, FALSE,
1265           6);
1266   gtk_widget_show (information->widget_avatar);
1267
1268   /* Setup id label/entry */
1269   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1270     {
1271       information->widget_id = gtk_entry_new ();
1272       g_signal_connect (information->widget_id, "focus-out-event",
1273             G_CALLBACK (contact_widget_id_focus_out_cb),
1274             information);
1275       g_signal_connect (information->widget_id, "changed",
1276             G_CALLBACK (contact_widget_id_changed_cb),
1277             information);
1278     }
1279   else
1280     {
1281       information->widget_id = gtk_label_new (NULL);
1282       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1283         gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1284       }
1285       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1286     }
1287   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1288            information->widget_id,
1289            1, 2, 1, 2);
1290   gtk_widget_show (information->widget_id);
1291
1292   /* Setup alias label/entry */
1293   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1294     {
1295       information->widget_alias = gtk_entry_new ();
1296
1297       if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1298         g_signal_connect (information->widget_alias, "focus-out-event",
1299               G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1300               information);
1301
1302       /* Make return activate the window default (the Close button) */
1303       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1304           TRUE);
1305     }
1306   else
1307     {
1308       information->widget_alias = gtk_label_new (NULL);
1309       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1310         gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1311       }
1312       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1313     }
1314   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1315            information->widget_alias,
1316            1, 2, 2, 3);
1317   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1318     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1319   }
1320   gtk_widget_show (information->widget_alias);
1321 }
1322
1323 static void
1324 contact_widget_destroy_cb (GtkWidget *widget,
1325                            EmpathyContactWidget *information)
1326 {
1327   contact_widget_remove_contact (information);
1328
1329   if (information->widget_id_timeout != 0)
1330     {
1331       g_source_remove (information->widget_id_timeout);
1332     }
1333   if (information->manager)
1334     {
1335       g_object_unref (information->manager);
1336     }
1337
1338   g_slice_free (EmpathyContactWidget, information);
1339 }
1340
1341 /**
1342  * empathy_contact_widget_new:
1343  * @contact: an #EmpathyContact
1344  * @flags: #EmpathyContactWidgetFlags for the new contact widget
1345  *
1346  * Creates a new #EmpathyContactWidget.
1347  *
1348  * Return value: a new #EmpathyContactWidget
1349  */
1350 GtkWidget *
1351 empathy_contact_widget_new (EmpathyContact *contact,
1352                             EmpathyContactWidgetFlags flags)
1353 {
1354   EmpathyContactWidget *information;
1355   GtkBuilder *gui;
1356   gchar *filename;
1357
1358   g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1359
1360   information = g_slice_new0 (EmpathyContactWidget);
1361   information->flags = flags;
1362
1363   filename = empathy_file_lookup ("empathy-contact-widget.ui",
1364       "libempathy-gtk");
1365   gui = empathy_builder_get_file (filename,
1366        "vbox_contact_widget", &information->vbox_contact_widget,
1367        "hbox_contact", &information->hbox_contact,
1368        "hbox_presence", &information->hbox_presence,
1369        "label_alias", &information->label_alias,
1370        "image_state", &information->image_state,
1371        "table_contact", &information->table_contact,
1372        "vbox_avatar", &information->vbox_avatar,
1373        "vbox_location", &information->vbox_location,
1374        "subvbox_location", &information->subvbox_location,
1375        "label_location", &information->label_location,
1376 #if HAVE_LIBCHAMPLAIN
1377        "viewport_map", &information->viewport_map,
1378 #endif
1379        "vbox_groups", &information->vbox_groups,
1380        "entry_group", &information->entry_group,
1381        "button_group", &information->button_group,
1382        "treeview_groups", &information->treeview_groups,
1383        "vbox_details", &information->vbox_details,
1384        "table_details", &information->table_details,
1385        "hbox_details_requested", &information->hbox_details_requested,
1386        "vbox_client", &information->vbox_client,
1387        "table_client", &information->table_client,
1388        "hbox_client_requested", &information->hbox_client_requested,
1389        NULL);
1390   g_free (filename);
1391
1392   empathy_builder_connect (gui, information,
1393       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1394       "entry_group", "changed", contact_widget_entry_group_changed_cb,
1395       "entry_group", "activate", contact_widget_entry_group_activate_cb,
1396       "button_group", "clicked", contact_widget_button_group_clicked_cb,
1397       NULL);
1398   information->table_location = NULL;
1399
1400   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1401       "EmpathyContactWidget",
1402       information);
1403
1404   /* Create widgets */
1405   contact_widget_contact_setup (information);
1406   contact_widget_groups_setup (information);
1407   contact_widget_details_setup (information);
1408   contact_widget_client_setup (information);
1409
1410   if (contact != NULL)
1411     contact_widget_set_contact (information, contact);
1412   else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1413       information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1414     contact_widget_change_contact (information);
1415
1416   return empathy_builder_unref_and_keep_widget (gui,
1417     information->vbox_contact_widget);
1418 }
1419
1420 /**
1421  * empathy_contact_widget_get_contact:
1422  * @widget: an #EmpathyContactWidget
1423  *
1424  * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1425  *
1426  * Returns: the #EmpathyContact associated with @widget
1427  */
1428 EmpathyContact *
1429 empathy_contact_widget_get_contact (GtkWidget *widget)
1430 {
1431   EmpathyContactWidget *information;
1432
1433   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1434
1435   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1436   if (!information)
1437       return NULL;
1438
1439   return information->contact;
1440 }
1441
1442 const gchar *
1443 empathy_contact_widget_get_alias (GtkWidget *widget)
1444 {
1445   EmpathyContactWidget *information;
1446
1447   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1448
1449   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1450   if (!information)
1451       return NULL;
1452
1453   return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1454 }
1455
1456 /**
1457  * empathy_contact_widget_set_contact:
1458  * @widget: an #EmpathyContactWidget
1459  * @contact: a different #EmpathyContact
1460  *
1461  * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1462  */
1463 void
1464 empathy_contact_widget_set_contact (GtkWidget *widget,
1465                                     EmpathyContact *contact)
1466 {
1467   EmpathyContactWidget *information;
1468
1469   g_return_if_fail (GTK_IS_WIDGET (widget));
1470   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1471
1472   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1473   if (!information)
1474     return;
1475
1476   contact_widget_set_contact (information, contact);
1477 }
1478
1479 /**
1480  * empathy_contact_widget_set_account_filter:
1481  * @widget: an #EmpathyContactWidget
1482  * @filter: a #EmpathyAccountChooserFilterFunc
1483  * @user_data: user data to pass to @filter, or %NULL
1484  *
1485  * Set a filter on the #EmpathyAccountChooser included in the
1486  * #EmpathyContactWidget.
1487  */
1488 void
1489 empathy_contact_widget_set_account_filter (
1490     GtkWidget *widget,
1491     EmpathyAccountChooserFilterFunc filter,
1492     gpointer user_data)
1493 {
1494   EmpathyContactWidget *information;
1495   EmpathyAccountChooser *chooser;
1496
1497   g_return_if_fail (GTK_IS_WIDGET (widget));
1498
1499   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1500   if (!information)
1501     return;
1502
1503   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1504   if (chooser)
1505       empathy_account_chooser_set_filter (chooser, filter, user_data);
1506 }
1507