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