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