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