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