]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Allow changing of individuals' aliases
[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       FolksIndividual *individual = folks_individual_from_empathy_contact (
583           information->contact);
584
585       if (individual)
586         {
587           folks_groups_change_group (FOLKS_GROUPS (individual), group, !enabled);
588           g_object_unref (individual);
589         }
590
591       g_free (group);
592     }
593 }
594
595 static void
596 contact_widget_model_populate_columns (EmpathyContactWidget *information)
597 {
598   GtkTreeView *view;
599   GtkTreeModel *model;
600   GtkTreeViewColumn *column;
601   GtkCellRenderer  *renderer;
602   guint col_offset;
603
604   view = GTK_TREE_VIEW (information->treeview_groups);
605   model = gtk_tree_view_get_model (view);
606
607   renderer = gtk_cell_renderer_toggle_new ();
608   g_signal_connect (renderer, "toggled",
609       G_CALLBACK (contact_widget_cell_toggled), information);
610
611   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
612       "active", COL_ENABLED, NULL);
613
614   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
615   gtk_tree_view_column_set_fixed_width (column, 50);
616   gtk_tree_view_append_column (view, column);
617
618   renderer = gtk_cell_renderer_text_new ();
619   col_offset = gtk_tree_view_insert_column_with_attributes (view,
620       -1, _("Group"),
621       renderer,
622       "text", COL_NAME,
623       /* "editable", COL_EDITABLE, */
624       NULL);
625
626   g_object_set_data (G_OBJECT (renderer),
627       "column", GINT_TO_POINTER (COL_NAME));
628
629   column = gtk_tree_view_get_column (view, col_offset - 1);
630   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
631   gtk_tree_view_column_set_resizable (column,FALSE);
632   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
633 }
634
635 static void
636 contact_widget_model_setup (EmpathyContactWidget *information)
637 {
638   GtkTreeView *view;
639   GtkListStore *store;
640   GtkTreeSelection *selection;
641
642   view = GTK_TREE_VIEW (information->treeview_groups);
643
644   store = gtk_list_store_new (COL_COUNT,
645       G_TYPE_STRING,   /* name */
646       G_TYPE_BOOLEAN,  /* enabled */
647       G_TYPE_BOOLEAN); /* editable */
648
649   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
650
651   selection = gtk_tree_view_get_selection (view);
652   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
653
654   contact_widget_model_populate_columns (information);
655
656   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
657       COL_NAME, GTK_SORT_ASCENDING);
658
659   g_object_unref (store);
660 }
661
662 static void
663 contact_widget_groups_populate_data (EmpathyContactWidget *information)
664 {
665   GtkTreeView *view;
666   GtkListStore *store;
667   GtkTreeIter iter;
668   GList *my_groups, *l;
669   GList *all_groups;
670
671   view = GTK_TREE_VIEW (information->treeview_groups);
672   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
673   gtk_list_store_clear (store);
674
675   all_groups = empathy_contact_list_get_all_groups (
676       EMPATHY_CONTACT_LIST (information->manager));
677   my_groups = empathy_contact_list_get_groups (
678       EMPATHY_CONTACT_LIST (information->manager),
679       information->contact);
680
681   for (l = all_groups; l; l = l->next)
682     {
683       const gchar *group_str;
684       gboolean enabled;
685
686       group_str = l->data;
687
688       enabled = g_list_find_custom (my_groups,
689           group_str, (GCompareFunc) strcmp) != NULL;
690
691       gtk_list_store_append (store, &iter);
692       gtk_list_store_set (store, &iter,
693           COL_NAME, group_str,
694           COL_EDITABLE, TRUE,
695           COL_ENABLED, enabled,
696           -1);
697     }
698
699   g_list_foreach (all_groups, (GFunc) g_free, NULL);
700   g_list_foreach (my_groups, (GFunc) g_free, NULL);
701   g_list_free (all_groups);
702   g_list_free (my_groups);
703 }
704
705 static gboolean
706 contact_widget_model_find_name_foreach (GtkTreeModel *model,
707                                         GtkTreePath *path,
708                                         GtkTreeIter *iter,
709                                         FindName *data)
710 {
711   gchar *name;
712
713   gtk_tree_model_get (model, iter,
714       COL_NAME, &name,
715       -1);
716
717   if (!name)
718       return FALSE;
719
720   if (data->name && strcmp (data->name, name) == 0)
721     {
722       data->found = TRUE;
723       data->found_iter = *iter;
724
725       g_free (name);
726
727       return TRUE;
728     }
729
730   g_free (name);
731
732   return FALSE;
733 }
734
735 static gboolean
736 contact_widget_model_find_name (EmpathyContactWidget *information,
737                                 const gchar *name,
738                                 GtkTreeIter *iter)
739 {
740   GtkTreeView *view;
741   GtkTreeModel *model;
742   FindName data;
743
744   if (EMP_STR_EMPTY (name))
745       return FALSE;
746
747   data.information = information;
748   data.name = name;
749   data.found = FALSE;
750
751   view = GTK_TREE_VIEW (information->treeview_groups);
752   model = gtk_tree_view_get_model (view);
753
754   gtk_tree_model_foreach (model,
755       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
756       &data);
757
758   if (data.found == TRUE)
759     {
760       *iter = data.found_iter;
761       return TRUE;
762     }
763
764   return FALSE;
765 }
766
767 static void
768 contact_widget_entry_group_changed_cb (GtkEditable *editable,
769                                        EmpathyContactWidget *information)
770 {
771   GtkTreeIter iter;
772   const gchar *group;
773
774   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
775
776   if (contact_widget_model_find_name (information, group, &iter))
777       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
778   else
779       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
780           !EMP_STR_EMPTY (group));
781 }
782
783 static void
784 contact_widget_entry_group_activate_cb (GtkEntry *entry,
785                                         EmpathyContactWidget  *information)
786 {
787   gtk_widget_activate (GTK_WIDGET (information->button_group));
788 }
789
790 static void
791 contact_widget_button_group_clicked_cb (GtkButton *button,
792                                         EmpathyContactWidget *information)
793 {
794   GtkTreeView *view;
795   GtkListStore *store;
796   GtkTreeIter iter;
797   FolksIndividual *individual;
798   const gchar *group;
799
800   view = GTK_TREE_VIEW (information->treeview_groups);
801   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
802
803   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
804
805   gtk_list_store_append (store, &iter);
806   gtk_list_store_set (store, &iter,
807       COL_NAME, group,
808       COL_ENABLED, TRUE,
809       -1);
810
811   individual = folks_individual_from_empathy_contact (information->contact);
812
813   if (individual)
814     {
815       folks_groups_change_group (FOLKS_GROUPS (individual), group, TRUE);
816       g_object_unref (individual);
817     }
818 }
819
820 static void
821 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
822 {
823   /* FIXME: not implemented */
824 }
825
826 static void
827 contact_widget_groups_setup (EmpathyContactWidget *information)
828 {
829   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
830     {
831       contact_widget_model_setup (information);
832     }
833 }
834
835 static void
836 contact_widget_groups_update (EmpathyContactWidget *information)
837 {
838   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
839       information->contact)
840     {
841       g_signal_connect_swapped (information->contact, "notify::groups",
842           G_CALLBACK (contact_widget_groups_notify_cb), information);
843       contact_widget_groups_populate_data (information);
844
845       gtk_widget_show (information->vbox_groups);
846     }
847   else
848       gtk_widget_hide (information->vbox_groups);
849 }
850
851 /* Converts the Location's GHashTable's key to a user readable string */
852 static const gchar *
853 location_key_to_label (const gchar *key)
854 {
855   if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
856     return _("Country ISO Code:");
857   else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
858     return _("Country:");
859   else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
860     return _("State:");
861   else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
862     return _("City:");
863   else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
864     return _("Area:");
865   else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
866     return _("Postal Code:");
867   else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
868     return _("Street:");
869   else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
870     return _("Building:");
871   else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
872     return _("Floor:");
873   else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
874     return _("Room:");
875   else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
876     return _("Text:");
877   else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
878     return _("Description:");
879   else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
880     return _("URI:");
881   else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
882     return _("Accuracy Level:");
883   else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
884     return _("Error:");
885   else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
886     return _("Vertical Error (meters):");
887   else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
888     return _("Horizontal Error (meters):");
889   else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
890     return _("Speed:");
891   else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
892     return _("Bearing:");
893   else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
894     return _("Climb Speed:");
895   else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
896     return _("Last Updated on:");
897   else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
898     return _("Longitude:");
899   else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
900     return _("Latitude:");
901   else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
902     return _("Altitude:");
903   else
904   {
905     DEBUG ("Unexpected Location key: %s", key);
906     return key;
907   }
908 }
909
910 static void
911 contact_widget_location_update (EmpathyContactWidget *information)
912 {
913   GHashTable *location;
914   GValue *value;
915   gdouble lat = 0.0, lon = 0.0;
916   gboolean has_position = TRUE;
917   GtkWidget *label;
918   guint row = 0;
919   static const gchar* ordered_geolocation_keys[] = {
920     EMPATHY_LOCATION_TEXT,
921     EMPATHY_LOCATION_URI,
922     EMPATHY_LOCATION_DESCRIPTION,
923     EMPATHY_LOCATION_BUILDING,
924     EMPATHY_LOCATION_FLOOR,
925     EMPATHY_LOCATION_ROOM,
926     EMPATHY_LOCATION_STREET,
927     EMPATHY_LOCATION_AREA,
928     EMPATHY_LOCATION_LOCALITY,
929     EMPATHY_LOCATION_REGION,
930     EMPATHY_LOCATION_COUNTRY,
931     NULL
932   };
933   int i;
934   const gchar *skey;
935   gboolean display_map = FALSE;
936
937   if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
938     {
939       gtk_widget_hide (information->vbox_location);
940       return;
941     }
942
943   location = empathy_contact_get_location (information->contact);
944   if (location == NULL || g_hash_table_size (location) == 0)
945     {
946       gtk_widget_hide (information->vbox_location);
947       return;
948     }
949
950   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
951   if (value == NULL)
952       has_position = FALSE;
953   else
954       lat = g_value_get_double (value);
955
956   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
957   if (value == NULL)
958       has_position = FALSE;
959   else
960       lon = g_value_get_double (value);
961
962   value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
963   if (value == NULL)
964     {
965       gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
966       gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
967       g_free (loc);
968     }
969   else
970     {
971       gchar *user_date;
972       gchar *text;
973       gint64 stamp;
974       time_t time_;
975
976       stamp = g_value_get_int64 (value);
977       time_ = stamp;
978
979       user_date = empathy_time_to_string_relative (time_);
980
981       text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
982       gtk_label_set_markup (GTK_LABEL (information->label_location), text);
983       g_free (user_date);
984       g_free (text);
985     }
986
987
988   /* Prepare the location information table */
989   if (information->table_location != NULL)
990     {
991       gtk_widget_destroy (information->table_location);
992     }
993
994   information->table_location = gtk_table_new (1, 2, FALSE);
995   gtk_box_pack_start (GTK_BOX (information->subvbox_location),
996       information->table_location, FALSE, FALSE, 5);
997
998
999   for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
1000     {
1001       const gchar* user_label;
1002       GValue *gvalue;
1003       char *svalue = NULL;
1004
1005       gvalue = g_hash_table_lookup (location, (gpointer) skey);
1006       if (gvalue == NULL)
1007         continue;
1008
1009       user_label = location_key_to_label (skey);
1010
1011       label = gtk_label_new (user_label);
1012       gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1013       gtk_table_attach (GTK_TABLE (information->table_location),
1014           label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
1015       gtk_widget_show (label);
1016
1017       if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
1018         {
1019           gdouble dvalue;
1020           dvalue = g_value_get_double (gvalue);
1021           svalue = g_strdup_printf ("%f", dvalue);
1022         }
1023       else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
1024         {
1025           svalue = g_value_dup_string (gvalue);
1026         }
1027       else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
1028         {
1029           time_t time_;
1030
1031           time_ = g_value_get_int64 (value);
1032           svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
1033         }
1034
1035       if (svalue != NULL)
1036         {
1037           label = gtk_label_new (svalue);
1038           gtk_table_attach_defaults (GTK_TABLE (information->table_location),
1039               label, 1, 2, row, row + 1);
1040           gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1041           gtk_widget_show (label);
1042
1043           if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1044             gtk_label_set_selectable (GTK_LABEL (label), TRUE);
1045         }
1046
1047       g_free (svalue);
1048       row++;
1049     }
1050
1051 #if HAVE_LIBCHAMPLAIN
1052   if (has_position &&
1053       !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1054     {
1055       /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
1056        * windows */
1057       display_map = TRUE;
1058     }
1059 #endif
1060
1061   if (row > 0)
1062     {
1063       /* We can display some fields */
1064       gtk_widget_show (information->table_location);
1065     }
1066   else if (!display_map)
1067     {
1068       /* Can't display either fields or map */
1069       gtk_widget_hide (information->vbox_location);
1070       return;
1071     }
1072
1073 #if HAVE_LIBCHAMPLAIN
1074   if (display_map)
1075     {
1076       ClutterActor *marker;
1077       ChamplainLayer *layer;
1078
1079       information->map_view_embed = gtk_champlain_embed_new ();
1080       information->map_view = gtk_champlain_embed_get_view (
1081           GTK_CHAMPLAIN_EMBED (information->map_view_embed));
1082
1083       gtk_container_add (GTK_CONTAINER (information->viewport_map),
1084           information->map_view_embed);
1085       g_object_set (G_OBJECT (information->map_view),
1086           "show-license", TRUE,
1087           "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
1088           "zoom-level", 10,
1089           NULL);
1090
1091       layer = champlain_layer_new ();
1092       champlain_view_add_layer (information->map_view, layer);
1093
1094       marker = champlain_marker_new_with_text (
1095           empathy_contact_get_name (information->contact), NULL, NULL, NULL);
1096       champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
1097       clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);
1098
1099       champlain_view_center_on (information->map_view, lat, lon);
1100       gtk_widget_show_all (information->viewport_map);
1101     }
1102 #endif
1103
1104     gtk_widget_show (information->vbox_location);
1105 }
1106
1107 static void
1108 save_avatar_menu_activate_cb (GtkWidget *widget,
1109                               EmpathyContactWidget *information)
1110 {
1111   GtkWidget *dialog;
1112   EmpathyAvatar *avatar;
1113   gchar *ext = NULL, *filename;
1114
1115   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
1116       NULL,
1117       GTK_FILE_CHOOSER_ACTION_SAVE,
1118       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1119       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1120       NULL);
1121
1122   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
1123       TRUE);
1124
1125   /* look for the avatar extension */
1126   avatar = empathy_contact_get_avatar (information->contact);
1127   if (avatar->format != NULL)
1128     {
1129       gchar **splitted;
1130
1131       splitted = g_strsplit (avatar->format, "/", 2);
1132       if (splitted[0] != NULL && splitted[1] != NULL)
1133           ext = g_strdup (splitted[1]);
1134
1135       g_strfreev (splitted);
1136     }
1137   else
1138     {
1139       /* Avatar was loaded from the cache so was converted to PNG */
1140       ext = g_strdup ("png");
1141     }
1142
1143   if (ext != NULL)
1144     {
1145       gchar *id;
1146
1147       id = tp_escape_as_identifier (empathy_contact_get_id (
1148             information->contact));
1149
1150       filename = g_strdup_printf ("%s.%s", id, ext);
1151       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
1152
1153       g_free (id);
1154       g_free (ext);
1155       g_free (filename);
1156     }
1157
1158   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
1159     {
1160       GError *error = NULL;
1161
1162       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1163
1164       if (!empathy_avatar_save_to_file (avatar, filename, &error))
1165         {
1166           /* Save error */
1167           GtkWidget *error_dialog;
1168
1169           error_dialog = gtk_message_dialog_new (NULL, 0,
1170               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1171               _("Unable to save avatar"));
1172
1173           gtk_message_dialog_format_secondary_text (
1174               GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
1175
1176           g_signal_connect (error_dialog, "response",
1177               G_CALLBACK (gtk_widget_destroy), NULL);
1178
1179           gtk_window_present (GTK_WINDOW (error_dialog));
1180
1181           g_clear_error (&error);
1182         }
1183
1184       g_free (filename);
1185     }
1186
1187   gtk_widget_destroy (dialog);
1188 }
1189
1190 static void
1191 popup_avatar_menu (EmpathyContactWidget *information,
1192                    GtkWidget *parent,
1193                    GdkEventButton *event)
1194 {
1195   GtkWidget *menu, *item;
1196   gint button, event_time;
1197
1198   if (information->contact == NULL ||
1199       empathy_contact_get_avatar (information->contact) == NULL)
1200       return;
1201
1202   menu = gtk_menu_new ();
1203
1204   /* Add "Save as..." entry */
1205   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1206   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1207   gtk_widget_show (item);
1208
1209   g_signal_connect (item, "activate",
1210       G_CALLBACK (save_avatar_menu_activate_cb), information);
1211
1212   if (event)
1213     {
1214       button = event->button;
1215       event_time = event->time;
1216     }
1217   else
1218     {
1219       button = 0;
1220       event_time = gtk_get_current_event_time ();
1221     }
1222
1223   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1224       button, event_time);
1225   g_object_ref_sink (menu);
1226   g_object_unref (menu);
1227 }
1228
1229 static gboolean
1230 widget_avatar_popup_menu_cb (GtkWidget *widget,
1231                              EmpathyContactWidget *information)
1232 {
1233   popup_avatar_menu (information, widget, NULL);
1234
1235   return TRUE;
1236 }
1237
1238 static gboolean
1239 widget_avatar_button_press_event_cb (GtkWidget *widget,
1240                                      GdkEventButton *event,
1241                                      EmpathyContactWidget *information)
1242 {
1243   /* Ignore double-clicks and triple-clicks */
1244   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1245     {
1246       popup_avatar_menu (information, widget, event);
1247       return TRUE;
1248     }
1249
1250   return FALSE;
1251 }
1252
1253 static void
1254 set_avatar_cb (GObject *source,
1255     GAsyncResult *res,
1256     gpointer user_data)
1257 {
1258   GError *error = NULL;
1259
1260   if (!tp_account_set_avatar_finish (TP_ACCOUNT (source), res, &error)) {
1261       DEBUG ("Failed to set Account.Avatar: %s", error->message);
1262       g_error_free (error);
1263   }
1264 }
1265
1266 static void
1267 set_avatar_on_account (TpAccount *account,
1268     const gchar *data,
1269     gsize size,
1270     const gchar *mime_type)
1271 {
1272   DEBUG ("%s Account.Avatar on %s", size > 0 ? "Set": "Clear",
1273       tp_proxy_get_object_path (account));
1274
1275   tp_account_set_avatar_async (account, (const guchar *) data, size,
1276       mime_type, set_avatar_cb, NULL);
1277 }
1278
1279 static void
1280 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
1281                                   EmpathyContactWidget *information)
1282 {
1283   const gchar *data;
1284   gsize size;
1285   const gchar *mime_type;
1286   TpAccount *account;
1287
1288   empathy_avatar_chooser_get_image_data (
1289       EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
1290       &data, &size, &mime_type);
1291
1292   account = empathy_contact_get_account (information->contact);
1293   set_avatar_on_account (account, data, size, mime_type);
1294 }
1295
1296 static void
1297 set_nickname_cb (GObject *source,
1298     GAsyncResult *res,
1299     gpointer user_data)
1300 {
1301   GError *error = NULL;
1302
1303   if (!tp_account_set_nickname_finish (TP_ACCOUNT (source), res, &error))
1304     {
1305       DEBUG ("Failed to set Account.Nickname: %s", error->message);
1306       g_error_free (error);
1307     }
1308 }
1309
1310 static void
1311 set_alias_on_account (TpAccount *account,
1312     const gchar *alias)
1313 {
1314   DEBUG ("Set Account.Nickname to %s", alias);
1315
1316   tp_account_set_nickname_async (account, alias, set_nickname_cb, NULL);
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
1334           account = empathy_contact_get_account (information->contact);
1335           set_alias_on_account (account, alias);
1336         }
1337       else
1338         {
1339           FolksIndividual *individual = folks_individual_from_empathy_contact (
1340               information->contact);
1341
1342           if (individual)
1343             {
1344               folks_alias_set_alias (FOLKS_ALIAS (individual), alias);
1345               g_object_unref (individual);
1346             }
1347         }
1348     }
1349
1350   return FALSE;
1351 }
1352
1353 static void
1354 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
1355                                   EmpathyAvatarChooser *avatar_chooser)
1356 {
1357   TpAccount *account;
1358
1359   account = empathy_account_chooser_get_account (account_chooser);
1360   if (account == NULL)
1361     return;
1362
1363   empathy_avatar_chooser_set_account (avatar_chooser, account);
1364 }
1365
1366 static void
1367 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
1368 {
1369   EmpathyAvatar *avatar = NULL;
1370
1371   if (information->contact)
1372       avatar = empathy_contact_get_avatar (information->contact);
1373
1374   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1375     {
1376       g_signal_handlers_block_by_func (information->widget_avatar,
1377           contact_widget_avatar_changed_cb,
1378           information);
1379       empathy_avatar_chooser_set (
1380           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
1381       g_signal_handlers_unblock_by_func (information->widget_avatar,
1382           contact_widget_avatar_changed_cb, information);
1383     }
1384   else
1385       empathy_avatar_image_set (
1386           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
1387 }
1388
1389 static void
1390 contact_widget_name_notify_cb (EmpathyContactWidget *information)
1391 {
1392   if (GTK_IS_ENTRY (information->widget_alias))
1393       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
1394           empathy_contact_get_name (information->contact));
1395   else
1396       gtk_label_set_label (GTK_LABEL (information->widget_alias),
1397           empathy_contact_get_name (information->contact));
1398 }
1399
1400 static void
1401 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
1402 {
1403   const gchar *status;
1404   gchar *markup_text = NULL;
1405
1406   status = empathy_contact_get_status (information->contact);
1407   if (status != NULL)
1408     markup_text = empathy_add_link_markup (status);
1409   gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
1410   g_free (markup_text);
1411
1412   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
1413       empathy_icon_name_for_contact (information->contact),
1414       GTK_ICON_SIZE_BUTTON);
1415   gtk_widget_show (information->image_state);
1416 }
1417
1418 static void
1419 contact_widget_favourites_changed_cb (EmpathyContactManager *manager,
1420     EmpathyContact *contact,
1421     gboolean is_favourite,
1422     EmpathyContactWidget *information)
1423 {
1424   if (contact != information->contact)
1425     return;
1426
1427   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
1428             information->favourite_checkbox), is_favourite);
1429 }
1430
1431 static void
1432 contact_widget_remove_contact (EmpathyContactWidget *information)
1433 {
1434   if (information->contact)
1435     {
1436       TpContact *tp_contact;
1437
1438       contact_widget_save (information);
1439
1440       g_signal_handlers_disconnect_by_func (information->contact,
1441           contact_widget_name_notify_cb, information);
1442       g_signal_handlers_disconnect_by_func (information->contact,
1443           contact_widget_presence_notify_cb, information);
1444       g_signal_handlers_disconnect_by_func (information->contact,
1445           contact_widget_avatar_notify_cb, information);
1446       g_signal_handlers_disconnect_by_func (information->contact,
1447           contact_widget_groups_notify_cb, information);
1448
1449       tp_contact = empathy_contact_get_tp_contact (information->contact);
1450       if (tp_contact != NULL)
1451         {
1452           g_signal_handlers_disconnect_by_func (tp_contact,
1453               contact_widget_details_notify_cb, information);
1454         }
1455
1456       g_object_unref (information->contact);
1457       information->contact = NULL;
1458     }
1459
1460   if (information->details_cancellable != NULL)
1461     {
1462       g_cancellable_cancel (information->details_cancellable);
1463       g_object_unref (information->details_cancellable);
1464       information->details_cancellable = NULL;
1465     }
1466 }
1467
1468 static void contact_widget_change_contact (EmpathyContactWidget *information);
1469
1470 static void
1471 contact_widget_contact_update (EmpathyContactWidget *information)
1472 {
1473   TpAccount *account = NULL;
1474   const gchar *id = NULL;
1475
1476   /* Connect and get info from new contact */
1477   if (information->contact)
1478     {
1479       g_signal_connect_swapped (information->contact, "notify::name",
1480           G_CALLBACK (contact_widget_name_notify_cb), information);
1481       g_signal_connect_swapped (information->contact, "notify::presence",
1482           G_CALLBACK (contact_widget_presence_notify_cb), information);
1483       g_signal_connect_swapped (information->contact,
1484           "notify::presence-message",
1485           G_CALLBACK (contact_widget_presence_notify_cb), information);
1486       g_signal_connect_swapped (information->contact, "notify::avatar",
1487           G_CALLBACK (contact_widget_avatar_notify_cb), information);
1488
1489       account = empathy_contact_get_account (information->contact);
1490       id = empathy_contact_get_id (information->contact);
1491     }
1492
1493   /* Update account widget */
1494   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1495     {
1496       if (account)
1497         {
1498           g_signal_handlers_block_by_func (information->widget_account,
1499                    contact_widget_change_contact,
1500                    information);
1501           empathy_account_chooser_set_account (
1502               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1503           g_signal_handlers_unblock_by_func (information->widget_account,
1504               contact_widget_change_contact, information);
1505         }
1506     }
1507   else
1508     {
1509       if (account)
1510         {
1511           const gchar *name;
1512
1513           name = tp_account_get_display_name (account);
1514           gtk_label_set_label (GTK_LABEL (information->label_account), name);
1515
1516           name = tp_account_get_icon_name (account);
1517           gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1518               name, GTK_ICON_SIZE_MENU);
1519         }
1520     }
1521
1522   /* Update id widget */
1523   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1524       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1525   else
1526       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1527
1528   /* Update other widgets */
1529   if (information->contact)
1530     {
1531       contact_widget_name_notify_cb (information);
1532       contact_widget_presence_notify_cb (information);
1533       contact_widget_avatar_notify_cb (information);
1534
1535       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1536         {
1537           gboolean is_favourite;
1538
1539           is_favourite = empathy_contact_list_is_favourite (
1540               EMPATHY_CONTACT_LIST (information->manager),
1541               information->contact);
1542
1543           contact_widget_favourites_changed_cb (information->manager,
1544               information->contact, is_favourite, information);
1545         }
1546
1547       gtk_widget_show (information->label_alias);
1548       gtk_widget_show (information->widget_alias);
1549       gtk_widget_show (information->hbox_presence);
1550       gtk_widget_show (information->widget_avatar);
1551     }
1552   else
1553     {
1554       gtk_widget_hide (information->label_alias);
1555       gtk_widget_hide (information->widget_alias);
1556       gtk_widget_hide (information->hbox_presence);
1557       gtk_widget_hide (information->widget_avatar);
1558     }
1559 }
1560
1561 static void
1562 contact_widget_set_contact (EmpathyContactWidget *information,
1563                             EmpathyContact *contact)
1564 {
1565   if (contact == information->contact)
1566     return;
1567
1568   contact_widget_remove_contact (information);
1569   if (contact)
1570     information->contact = g_object_ref (contact);
1571
1572   /* set the selected account to be the account this contact came from */
1573   if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1574       empathy_account_chooser_set_account (
1575                       EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1576                       empathy_contact_get_account (contact));
1577   }
1578
1579   /* Update information for widgets */
1580   contact_widget_contact_update (information);
1581   contact_widget_groups_update (information);
1582   contact_widget_details_update (information);
1583   contact_widget_client_update (information);
1584   contact_widget_location_update (information);
1585 }
1586
1587 static void
1588 contact_widget_got_contact_cb (TpConnection *connection,
1589                                EmpathyContact *contact,
1590                                const GError *error,
1591                                gpointer user_data,
1592                                GObject *weak_object)
1593 {
1594   EmpathyContactWidget *information = user_data;
1595
1596   if (error != NULL)
1597     {
1598       DEBUG ("Error: %s", error->message);
1599       return;
1600     }
1601
1602   contact_widget_set_contact (information, contact);
1603 }
1604
1605 static void
1606 contact_widget_change_contact (EmpathyContactWidget *information)
1607 {
1608   TpConnection *connection;
1609
1610   connection = empathy_account_chooser_get_connection (
1611       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1612   if (!connection)
1613       return;
1614
1615   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1616     {
1617       const gchar *id;
1618
1619       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1620       if (!EMP_STR_EMPTY (id))
1621         {
1622           empathy_tp_contact_factory_get_from_id (connection, id,
1623               contact_widget_got_contact_cb, information, NULL,
1624               G_OBJECT (information->vbox_contact_widget));
1625         }
1626     }
1627   else
1628     {
1629       empathy_tp_contact_factory_get_from_handle (connection,
1630           tp_connection_get_self_handle (connection),
1631           contact_widget_got_contact_cb, information, NULL,
1632           G_OBJECT (information->vbox_contact_widget));
1633     }
1634 }
1635
1636 static gboolean
1637 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1638 {
1639   contact_widget_change_contact (self);
1640   return FALSE;
1641 }
1642
1643 static void
1644 contact_widget_id_changed_cb (GtkEntry *entry,
1645                               EmpathyContactWidget *self)
1646 {
1647   if (self->widget_id_timeout != 0)
1648     {
1649       g_source_remove (self->widget_id_timeout);
1650     }
1651
1652   self->widget_id_timeout =
1653     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1654         (GSourceFunc) contact_widget_id_activate_timeout, self);
1655 }
1656
1657 static gboolean
1658 contact_widget_id_focus_out_cb (GtkWidget *widget,
1659                                 GdkEventFocus *event,
1660                                 EmpathyContactWidget *information)
1661 {
1662   contact_widget_change_contact (information);
1663   return FALSE;
1664 }
1665
1666 static void
1667 favourite_toggled_cb (GtkToggleButton *button,
1668     EmpathyContactWidget *information)
1669 {
1670   gboolean active;
1671
1672   active = gtk_toggle_button_get_active (button);
1673
1674   if (active)
1675     {
1676       empathy_contact_list_add_to_favourites (
1677           EMPATHY_CONTACT_LIST (information->manager), information->contact);
1678     }
1679   else
1680     {
1681       empathy_contact_list_remove_from_favourites (
1682           EMPATHY_CONTACT_LIST (information->manager), information->contact);
1683     }
1684 }
1685
1686 static void
1687 contact_widget_contact_setup (EmpathyContactWidget *information)
1688 {
1689   /* Setup label_status as a KludgeLabel */
1690   information->label_status = empathy_kludge_label_new ("");
1691   gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1692                                 PANGO_WRAP_WORD_CHAR);
1693   gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1694                            TRUE);
1695
1696   if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1697     gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1698
1699   gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1700         information->label_status, TRUE, TRUE, 0);
1701   gtk_widget_show (information->label_status);
1702
1703   /* Setup account label/chooser */
1704   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1705     {
1706       information->widget_account = empathy_account_chooser_new ();
1707
1708       g_signal_connect_swapped (information->widget_account, "changed",
1709             G_CALLBACK (contact_widget_change_contact),
1710             information);
1711     }
1712   else
1713     {
1714       /* Pack the protocol icon with the account name in an hbox */
1715       information->widget_account = gtk_hbox_new (FALSE, 6);
1716
1717       information->label_account = gtk_label_new (NULL);
1718       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1719         gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1720       }
1721       gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1722       gtk_widget_show (information->label_account);
1723
1724       information->image_account = gtk_image_new ();
1725       gtk_widget_show (information->image_account);
1726
1727       gtk_box_pack_start (GTK_BOX (information->widget_account),
1728           information->image_account, FALSE, FALSE, 0);
1729       gtk_box_pack_start (GTK_BOX (information->widget_account),
1730           information->label_account, FALSE, TRUE, 0);
1731     }
1732   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1733            information->widget_account,
1734            1, 2, 0, 1);
1735   gtk_widget_show (information->widget_account);
1736
1737   /* Set up avatar chooser/display */
1738   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1739     {
1740       information->widget_avatar = empathy_avatar_chooser_new ();
1741       g_signal_connect (information->widget_avatar, "changed",
1742             G_CALLBACK (contact_widget_avatar_changed_cb),
1743             information);
1744       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1745         {
1746           g_signal_connect (information->widget_account, "changed",
1747               G_CALLBACK (update_avatar_chooser_account_cb),
1748               information->widget_avatar);
1749           update_avatar_chooser_account_cb (
1750               EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1751               EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1752         }
1753     }
1754   else
1755     {
1756       information->widget_avatar = empathy_avatar_image_new ();
1757
1758       g_signal_connect (information->widget_avatar, "popup-menu",
1759           G_CALLBACK (widget_avatar_popup_menu_cb), information);
1760       g_signal_connect (information->widget_avatar, "button-press-event",
1761           G_CALLBACK (widget_avatar_button_press_event_cb), information);
1762     }
1763
1764   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1765           information->widget_avatar,
1766           FALSE, FALSE,
1767           6);
1768   gtk_widget_show (information->widget_avatar);
1769
1770   /* Setup id label/entry */
1771   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1772     {
1773       information->widget_id = gtk_entry_new ();
1774       g_signal_connect (information->widget_id, "focus-out-event",
1775             G_CALLBACK (contact_widget_id_focus_out_cb),
1776             information);
1777       g_signal_connect (information->widget_id, "changed",
1778             G_CALLBACK (contact_widget_id_changed_cb),
1779             information);
1780     }
1781   else
1782     {
1783       information->widget_id = gtk_label_new (NULL);
1784       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1785         gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1786       }
1787       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1788     }
1789   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1790            information->widget_id,
1791            1, 2, 1, 2);
1792   gtk_widget_show (information->widget_id);
1793
1794   /* Setup alias label/entry */
1795   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1796     {
1797       information->widget_alias = gtk_entry_new ();
1798
1799       if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1800         g_signal_connect (information->widget_alias, "focus-out-event",
1801               G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1802               information);
1803
1804       /* Make return activate the window default (the Close button) */
1805       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1806           TRUE);
1807     }
1808   else
1809     {
1810       information->widget_alias = gtk_label_new (NULL);
1811       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1812         gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1813       }
1814       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1815     }
1816   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1817            information->widget_alias,
1818            1, 2, 2, 3);
1819   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1820     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1821   }
1822   gtk_widget_show (information->widget_alias);
1823
1824   /* Favorite */
1825   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1826     {
1827       information->favourite_checkbox = gtk_check_button_new_with_label (
1828           _("Favorite"));
1829
1830       g_signal_connect (information->favourite_checkbox, "toggled",
1831           G_CALLBACK (favourite_toggled_cb), information);
1832
1833       gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1834            information->favourite_checkbox, 0, 2, 3, 4);
1835
1836       information->fav_sig_id = g_signal_connect (information->manager,
1837           "favourites-changed",
1838           G_CALLBACK (contact_widget_favourites_changed_cb), information);
1839
1840       gtk_widget_show (information->favourite_checkbox);
1841     }
1842 }
1843
1844 static void
1845 contact_widget_destroy_cb (GtkWidget *widget,
1846                            EmpathyContactWidget *information)
1847 {
1848   contact_widget_remove_contact (information);
1849
1850   if (information->widget_id_timeout != 0)
1851     {
1852       g_source_remove (information->widget_id_timeout);
1853     }
1854
1855   if (information->fav_sig_id != 0)
1856     g_signal_handler_disconnect (information->manager, information->fav_sig_id);
1857
1858   g_object_unref (information->manager);
1859
1860   g_slice_free (EmpathyContactWidget, information);
1861 }
1862
1863 /**
1864  * empathy_contact_widget_new:
1865  * @contact: an #EmpathyContact
1866  * @flags: #EmpathyContactWidgetFlags for the new contact widget
1867  *
1868  * Creates a new #EmpathyContactWidget.
1869  *
1870  * Return value: a new #EmpathyContactWidget
1871  */
1872 GtkWidget *
1873 empathy_contact_widget_new (EmpathyContact *contact,
1874                             EmpathyContactWidgetFlags flags)
1875 {
1876   EmpathyContactWidget *information;
1877   GtkBuilder *gui;
1878   gchar *filename;
1879
1880   g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1881
1882   information = g_slice_new0 (EmpathyContactWidget);
1883   information->flags = flags;
1884
1885   filename = empathy_file_lookup ("empathy-contact-widget.ui",
1886       "libempathy-gtk");
1887   gui = empathy_builder_get_file (filename,
1888        "vbox_contact_widget", &information->vbox_contact_widget,
1889        "hbox_contact", &information->hbox_contact,
1890        "hbox_presence", &information->hbox_presence,
1891        "label_alias", &information->label_alias,
1892        "image_state", &information->image_state,
1893        "table_contact", &information->table_contact,
1894        "vbox_avatar", &information->vbox_avatar,
1895        "vbox_location", &information->vbox_location,
1896        "subvbox_location", &information->subvbox_location,
1897        "label_location", &information->label_location,
1898 #if HAVE_LIBCHAMPLAIN
1899        "viewport_map", &information->viewport_map,
1900 #endif
1901        "vbox_groups", &information->vbox_groups,
1902        "entry_group", &information->entry_group,
1903        "button_group", &information->button_group,
1904        "treeview_groups", &information->treeview_groups,
1905        "vbox_details", &information->vbox_details,
1906        "table_details", &information->table_details,
1907        "hbox_details_requested", &information->hbox_details_requested,
1908        "vbox_client", &information->vbox_client,
1909        "table_client", &information->table_client,
1910        "hbox_client_requested", &information->hbox_client_requested,
1911        NULL);
1912   g_free (filename);
1913
1914   empathy_builder_connect (gui, information,
1915       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1916       "entry_group", "changed", contact_widget_entry_group_changed_cb,
1917       "entry_group", "activate", contact_widget_entry_group_activate_cb,
1918       "button_group", "clicked", contact_widget_button_group_clicked_cb,
1919       NULL);
1920   information->table_location = NULL;
1921
1922   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1923       "EmpathyContactWidget",
1924       information);
1925
1926   information->manager = empathy_contact_manager_dup_singleton ();
1927
1928   /* Create widgets */
1929   contact_widget_contact_setup (information);
1930   contact_widget_groups_setup (information);
1931   contact_widget_details_setup (information);
1932   contact_widget_client_setup (information);
1933
1934   if (contact != NULL)
1935     contact_widget_set_contact (information, contact);
1936   else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1937       information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1938     contact_widget_change_contact (information);
1939
1940   return empathy_builder_unref_and_keep_widget (gui,
1941     information->vbox_contact_widget);
1942 }
1943
1944 /**
1945  * empathy_contact_widget_get_contact:
1946  * @widget: an #EmpathyContactWidget
1947  *
1948  * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1949  *
1950  * Returns: the #EmpathyContact associated with @widget
1951  */
1952 EmpathyContact *
1953 empathy_contact_widget_get_contact (GtkWidget *widget)
1954 {
1955   EmpathyContactWidget *information;
1956
1957   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1958
1959   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1960   if (!information)
1961       return NULL;
1962
1963   return information->contact;
1964 }
1965
1966 const gchar *
1967 empathy_contact_widget_get_alias (GtkWidget *widget)
1968 {
1969   EmpathyContactWidget *information;
1970
1971   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1972
1973   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1974   if (!information)
1975       return NULL;
1976
1977   return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1978 }
1979
1980 /**
1981  * empathy_contact_widget_set_contact:
1982  * @widget: an #EmpathyContactWidget
1983  * @contact: a different #EmpathyContact
1984  *
1985  * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1986  */
1987 void
1988 empathy_contact_widget_set_contact (GtkWidget *widget,
1989                                     EmpathyContact *contact)
1990 {
1991   EmpathyContactWidget *information;
1992
1993   g_return_if_fail (GTK_IS_WIDGET (widget));
1994   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1995
1996   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1997   if (!information)
1998     return;
1999
2000   contact_widget_set_contact (information, contact);
2001 }
2002
2003 /**
2004  * empathy_contact_widget_set_account_filter:
2005  * @widget: an #EmpathyContactWidget
2006  * @filter: a #EmpathyAccountChooserFilterFunc
2007  * @user_data: user data to pass to @filter, or %NULL
2008  *
2009  * Set a filter on the #EmpathyAccountChooser included in the
2010  * #EmpathyContactWidget.
2011  */
2012 void
2013 empathy_contact_widget_set_account_filter (
2014     GtkWidget *widget,
2015     EmpathyAccountChooserFilterFunc filter,
2016     gpointer user_data)
2017 {
2018   EmpathyContactWidget *information;
2019   EmpathyAccountChooser *chooser;
2020
2021   g_return_if_fail (GTK_IS_WIDGET (widget));
2022
2023   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
2024   if (!information)
2025     return;
2026
2027   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
2028   if (chooser)
2029       empathy_account_chooser_set_filter (chooser, filter, user_data);
2030 }
2031