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