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