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