account-widget-irc: set password-prompt param when needed (#644406)
[empathy-mirror.git] / libempathy-gtk / empathy-contact-widget.c
blob15b8ed39b7cb9e6858e4d1fd517e1bd61bcddb5a
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007-2009 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 #include <config.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
30 #ifdef HAVE_LIBCHAMPLAIN
31 #include <champlain/champlain.h>
32 #include <champlain-gtk/champlain-gtk.h>
33 #endif
35 #include <telepathy-glib/account.h>
36 #include <telepathy-glib/util.h>
37 #include <telepathy-glib/interfaces.h>
39 #include <libempathy/empathy-tp-contact-factory.h>
40 #include <libempathy/empathy-contact-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-location.h>
43 #include <libempathy/empathy-time.h>
44 #include <libempathy/empathy-utils.h>
46 #include "empathy-contact-widget.h"
47 #include "empathy-account-chooser.h"
48 #include "empathy-avatar-chooser.h"
49 #include "empathy-avatar-image.h"
50 #include "empathy-groups-widget.h"
51 #include "empathy-ui-utils.h"
52 #include "empathy-string-parser.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
55 #include <libempathy/empathy-debug.h>
57 /**
58 * SECTION:empathy-contact-widget
59 * @title:EmpathyContactWidget
60 * @short_description: A widget used to display and edit details about a contact
61 * @include: libempathy-empathy-contact-widget.h
63 * #EmpathyContactWidget is a widget which displays appropriate widgets
64 * with details about a contact, also allowing changing these details,
65 * if desired.
68 /**
69 * EmpathyContactWidget:
70 * @parent: parent object
72 * Widget which displays appropriate widgets with details about a contact,
73 * also allowing changing these details, if desired.
76 /* Delay before updating the widget when the id entry changed (seconds) */
77 #define ID_CHANGED_TIMEOUT 1
79 #define DATA_FIELD "contact-info-field"
81 typedef struct
83 EmpathyContactManager *manager;
84 EmpathyContact *contact;
85 EmpathyContactWidgetFlags flags;
86 guint widget_id_timeout;
87 gulong fav_sig_id;
89 GtkWidget *vbox_contact_widget;
91 /* Contact */
92 GtkWidget *hbox_contact;
93 GtkWidget *widget_avatar;
94 GtkWidget *widget_account;
95 GtkWidget *image_account;
96 GtkWidget *label_account;
97 GtkWidget *widget_id;
98 GtkWidget *widget_alias;
99 GtkWidget *label_alias;
100 GtkWidget *hbox_presence;
101 GtkWidget *image_state;
102 GtkWidget *label_status;
103 GtkWidget *table_contact;
104 GtkWidget *vbox_avatar;
105 GtkWidget *favourite_checkbox;
107 /* Location */
108 GtkWidget *vbox_location;
109 GtkWidget *subvbox_location;
110 GtkWidget *table_location;
111 GtkWidget *label_location;
112 #ifdef HAVE_LIBCHAMPLAIN
113 GtkWidget *viewport_map;
114 GtkWidget *map_view_embed;
115 ChamplainView *map_view;
116 #endif
118 /* Groups */
119 GtkWidget *groups_widget;
121 /* Details */
122 GtkWidget *vbox_details;
123 GtkWidget *table_details;
124 GtkWidget *hbox_details_requested;
125 GtkWidget *spinner_details;
126 GList *details_to_set;
127 GCancellable *details_cancellable;
128 gboolean details_changed;
130 /* Client */
131 GtkWidget *vbox_client;
132 GtkWidget *table_client;
133 GtkWidget *hbox_client_requested;
134 } EmpathyContactWidget;
136 typedef struct
138 EmpathyContactWidget *information;
139 const gchar *name;
140 gboolean found;
141 GtkTreeIter found_iter;
142 } FindName;
144 enum
146 COL_NAME,
147 COL_ENABLED,
148 COL_EDITABLE,
149 COL_COUNT
152 static gboolean
153 field_value_is_empty (TpContactInfoField *field)
155 guint i;
157 if (field->field_value == NULL)
158 return TRUE;
160 /* Field is empty if all its values are empty */
161 for (i = 0; field->field_value[i] != NULL; i++)
163 if (!tp_str_empty (field->field_value[i]))
164 return FALSE;
167 return TRUE;
170 static void
171 set_contact_info_cb (GObject *source,
172 GAsyncResult *result,
173 gpointer user_data)
175 GError *error = NULL;
177 if (!tp_connection_set_contact_info_finish (TP_CONNECTION (source), result,
178 &error))
180 DEBUG ("SetContactInfo() failed: %s", error->message);
181 g_error_free (error);
182 return;
185 DEBUG ("SetContactInfo() succeeded");
188 static void
189 contact_widget_save (EmpathyContactWidget *information)
191 TpConnection *connection;
192 GList *l, *next;
194 connection = empathy_contact_get_connection (information->contact);
196 /* Remove empty fields */
197 for (l = information->details_to_set; l != NULL; l = next)
199 TpContactInfoField *field = l->data;
201 next = l->next;
202 if (field_value_is_empty (field))
204 DEBUG ("Drop empty field: %s", field->field_name);
205 tp_contact_info_field_free (field);
206 information->details_to_set =
207 g_list_delete_link (information->details_to_set, l);
211 if (information->details_to_set != NULL)
213 if (information->details_changed)
215 tp_connection_set_contact_info_async (connection,
216 information->details_to_set, set_contact_info_cb, NULL);
219 tp_contact_info_list_free (information->details_to_set);
220 information->details_to_set = NULL;
224 static void
225 contact_widget_details_setup (EmpathyContactWidget *information)
227 gtk_widget_hide (information->vbox_details);
229 information->spinner_details = gtk_spinner_new ();
230 gtk_box_pack_end (GTK_BOX (information->hbox_details_requested),
231 information->spinner_details, TRUE, TRUE, 0);
232 gtk_widget_show (information->spinner_details);
235 static void
236 contact_widget_details_changed_cb (GtkEntry *entry,
237 EmpathyContactWidget *self)
239 const gchar *strv[] = { NULL, NULL };
240 TpContactInfoField *field;
242 self->details_changed = TRUE;
244 field = g_object_get_data ((GObject *) entry, DATA_FIELD);
245 g_assert (field != NULL);
247 strv[0] = gtk_entry_get_text (entry);
249 if (field->field_value != NULL)
250 g_strfreev (field->field_value);
251 field->field_value = g_strdupv ((GStrv) strv);
254 static void
255 contact_widget_bday_changed_cb (GtkCalendar *calendar,
256 EmpathyContactWidget *self)
258 guint year, month, day;
259 GDate *date;
260 gchar tmp[255];
261 const gchar *strv[] = { NULL, NULL };
262 TpContactInfoField *field;
264 self->details_changed = TRUE;
266 field = g_object_get_data ((GObject *) calendar, DATA_FIELD);
267 g_assert (field != NULL);
269 gtk_calendar_get_date (calendar, &year, &month, &day);
270 date = g_date_new_dmy (day, month+1, year);
272 gtk_calendar_clear_marks (calendar);
273 gtk_calendar_mark_day (calendar, g_date_get_day (date));
275 g_date_strftime (tmp, sizeof (tmp), EMPATHY_DATE_FORMAT_DISPLAY_SHORT, date);
276 strv[0] = tmp;
277 if (field->field_value != NULL)
278 g_strfreev (field->field_value);
279 field->field_value = g_strdupv ((GStrv) strv);
281 g_date_free (date);
284 static void contact_widget_details_notify_cb (EmpathyContactWidget *information);
286 typedef struct
288 const gchar *field_name;
289 const gchar *title;
290 gboolean linkify;
291 } InfoFieldData;
293 static InfoFieldData info_field_datas[] =
295 { "fn", N_("Full name:"), FALSE },
296 { "tel", N_("Phone number:"), FALSE },
297 { "email", N_("E-mail address:"), TRUE },
298 { "url", N_("Website:"), TRUE },
299 { "bday", N_("Birthday:"), FALSE },
300 { NULL, NULL }
303 static InfoFieldData *
304 find_info_field_data (const gchar *field_name)
306 guint i;
308 for (i = 0; info_field_datas[i].field_name != NULL; i++)
310 if (!tp_strdiff (info_field_datas[i].field_name, field_name))
311 return info_field_datas + i;
313 return NULL;
316 static gint
317 contact_info_field_name_cmp (const gchar *name1,
318 const gchar *name2)
320 guint i;
322 if (!tp_strdiff (name1, name2))
323 return 0;
325 /* We use the order of info_field_datas */
326 for (i = 0; info_field_datas[i].field_name != NULL; i++)
328 if (!tp_strdiff (info_field_datas[i].field_name, name1))
329 return -1;
330 if (!tp_strdiff (info_field_datas[i].field_name, name2))
331 return +1;
334 return g_strcmp0 (name1, name2);
337 static gint
338 contact_info_field_cmp (TpContactInfoField *field1,
339 TpContactInfoField *field2)
341 return contact_info_field_name_cmp (field1->field_name, field2->field_name);
344 static gboolean
345 field_name_in_field_list (GList *list,
346 const gchar *name)
348 GList *l;
350 for (l = list; l != NULL; l = g_list_next (l))
352 TpContactInfoField *field = l->data;
354 if (!tp_strdiff (field->field_name, name))
355 return TRUE;
358 return FALSE;
361 static TpContactInfoFieldSpec *
362 get_spec_from_list (GList *list,
363 const gchar *name)
365 GList *l;
367 for (l = list; l != NULL; l = g_list_next (l))
369 TpContactInfoFieldSpec *spec = l->data;
371 if (!tp_strdiff (spec->name, name))
372 return spec;
375 return NULL;
378 static guint
379 contact_widget_details_update_edit (EmpathyContactWidget *information)
381 TpContact *contact;
382 TpConnection *connection;
383 GList *specs, *l;
384 guint n_rows = 0;
385 GList *info;
386 guint i;
388 g_assert (information->details_to_set == NULL);
390 information->details_changed = FALSE;
392 contact = empathy_contact_get_tp_contact (information->contact);
393 connection = tp_contact_get_connection (contact);
395 info = tp_contact_get_contact_info (contact);
397 specs = tp_connection_get_contact_info_supported_fields (connection);
399 /* Look at the fields set in our vCard */
400 for (l = info; l != NULL; l = l->next)
402 TpContactInfoField *field = l->data;
404 /* make a copy for the details_to_set list */
405 field = tp_contact_info_field_copy (field);
406 DEBUG ("Field %s is in our vCard", field->field_name);
408 information->details_to_set = g_list_prepend (information->details_to_set,
409 field);
412 /* Add fields which are supported but not in the vCard */
413 for (i = 0; info_field_datas[i].field_name != NULL; i++)
415 TpContactInfoFieldSpec *spec;
416 TpContactInfoField *field;
418 /* Check if the field was in the vCard */
419 if (field_name_in_field_list (information->details_to_set,
420 info_field_datas[i].field_name))
421 continue;
423 /* Check if the CM supports the field */
424 spec = get_spec_from_list (specs, info_field_datas[i].field_name);
425 if (spec == NULL)
426 continue;
428 /* add an empty field so user can set a value */
429 field = tp_contact_info_field_new (spec->name, spec->parameters, NULL);
431 information->details_to_set = g_list_prepend (information->details_to_set,
432 field);
435 /* Add widgets for supported fields */
436 information->details_to_set = g_list_sort (information->details_to_set,
437 (GCompareFunc) contact_info_field_cmp);
439 for (l = information->details_to_set; l != NULL; l= g_list_next (l))
441 TpContactInfoField *field = l->data;
442 InfoFieldData *field_data;
443 GtkWidget *w;
444 TpContactInfoFieldSpec *spec;
446 field_data = find_info_field_data (field->field_name);
447 if (field_data == NULL)
449 /* Empathy doesn't display this field so we can't change it.
450 * But we put it in the details_to_set list so it won't be erased
451 * when calling SetContactInfo (bgo #630427) */
452 DEBUG ("Unhandled ContactInfo field spec: %s", field->field_name);
453 continue;
456 spec = get_spec_from_list (specs, field->field_name);
457 /* We shouldn't have added the field to details_to_set if it's not
458 * supported by the CM */
459 g_assert (spec != NULL);
461 if (spec->flags & TP_CONTACT_INFO_FIELD_FLAG_OVERWRITTEN_BY_NICKNAME)
463 DEBUG ("Ignoring field '%s' due it to having the "
464 "Overwritten_By_Nickname flag", field->field_name);
465 continue;
468 /* Add Title */
469 w = gtk_label_new (_(field_data->title));
470 gtk_table_attach (GTK_TABLE (information->table_details),
471 w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
472 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
473 gtk_widget_show (w);
475 /* Add Value */
476 if (!tp_strdiff (field->field_name, "bday"))
478 w = gtk_calendar_new ();
479 if (field->field_value[0])
481 GDate date;
483 g_date_set_parse (&date, field->field_value[0]);
484 if (g_date_valid (&date))
486 gtk_calendar_select_day (GTK_CALENDAR (w),
487 g_date_get_day (&date));
488 gtk_calendar_select_month (GTK_CALENDAR (w),
489 g_date_get_month (&date) - 1, g_date_get_year (&date));
490 gtk_calendar_mark_day (GTK_CALENDAR (w),
491 g_date_get_day (&date));
494 gtk_table_attach_defaults (GTK_TABLE (information->table_details),
495 w, 1, 2, n_rows, n_rows + 1);
496 gtk_widget_show (w);
498 g_object_set_data ((GObject *) w, DATA_FIELD, field);
500 g_signal_connect (w, "day-selected-double-click",
501 G_CALLBACK (contact_widget_bday_changed_cb), information);
502 g_signal_connect (w, "month-changed",
503 G_CALLBACK (contact_widget_bday_changed_cb), information);
505 else
507 w = gtk_entry_new ();
508 gtk_entry_set_text (GTK_ENTRY (w),
509 field->field_value[0] ? field->field_value[0] : "");
510 gtk_table_attach_defaults (GTK_TABLE (information->table_details),
511 w, 1, 2, n_rows, n_rows + 1);
512 gtk_widget_show (w);
514 g_object_set_data ((GObject *) w, DATA_FIELD, field);
516 g_signal_connect (w, "changed",
517 G_CALLBACK (contact_widget_details_changed_cb), information);
520 n_rows++;
523 g_list_free (specs);
524 g_list_free (info);
526 return n_rows;
529 static guint
530 contact_widget_details_update_show (EmpathyContactWidget *information)
532 TpContact *contact;
533 GList *info, *l;
534 guint n_rows = 0;
536 contact = empathy_contact_get_tp_contact (information->contact);
537 info = tp_contact_get_contact_info (contact);
538 info = g_list_sort (info, (GCompareFunc) contact_info_field_cmp);
539 for (l = info; l != NULL; l = l->next)
541 TpContactInfoField *field = l->data;
542 InfoFieldData *field_data;
543 const gchar *value;
544 GtkWidget *w;
546 if (field->field_value == NULL || field->field_value[0] == NULL)
547 continue;
549 value = field->field_value[0];
551 field_data = find_info_field_data (field->field_name);
552 if (field_data == NULL)
554 DEBUG ("Unhandled ContactInfo field: %s", field->field_name);
555 continue;
558 /* Add Title */
559 w = gtk_label_new (_(field_data->title));
560 gtk_table_attach (GTK_TABLE (information->table_details),
561 w, 0, 1, n_rows, n_rows + 1, GTK_FILL, 0, 0, 0);
562 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
563 gtk_widget_show (w);
565 /* Add Value */
566 w = gtk_label_new (value);
567 if (field_data->linkify)
569 gchar *markup;
571 markup = empathy_add_link_markup (value);
572 gtk_label_set_markup (GTK_LABEL (w), markup);
573 g_free (markup);
576 if ((information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) == 0)
577 gtk_label_set_selectable (GTK_LABEL (w), TRUE);
579 gtk_table_attach_defaults (GTK_TABLE (information->table_details),
580 w, 1, 2, n_rows, n_rows + 1);
581 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
582 gtk_widget_show (w);
584 n_rows++;
586 g_list_free (info);
588 return n_rows;
591 static void
592 contact_widget_details_notify_cb (EmpathyContactWidget *information)
594 guint n_rows;
596 gtk_container_foreach (GTK_CONTAINER (information->table_details),
597 (GtkCallback) gtk_widget_destroy, NULL);
599 if ((information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) != 0)
600 n_rows = contact_widget_details_update_edit (information);
601 else
602 n_rows = contact_widget_details_update_show (information);
604 if (n_rows > 0)
606 gtk_widget_show (information->vbox_details);
607 gtk_widget_show (information->table_details);
609 else
611 gtk_widget_hide (information->vbox_details);
614 gtk_widget_hide (information->hbox_details_requested);
615 gtk_spinner_stop (GTK_SPINNER (information->spinner_details));
618 static void
619 contact_widget_details_request_cb (GObject *object,
620 GAsyncResult *res,
621 gpointer user_data)
623 TpContact *contact = TP_CONTACT (object);
624 EmpathyContactWidget *information = user_data;
625 GError *error = NULL;
627 if (!tp_contact_request_contact_info_finish (contact, res, &error))
629 /* If the request got cancelled it could mean the contact widget is
630 * destroyed, so we should not dereference information */
631 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
633 g_clear_error (&error);
634 return;
637 gtk_widget_hide (information->vbox_details);
638 g_clear_error (&error);
640 else
642 contact_widget_details_notify_cb (information);
645 /* If we are going to edit ContactInfo, we don't want live updates */
646 if ((information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) == 0)
648 g_signal_connect_swapped (contact, "notify::contact-info",
649 G_CALLBACK (contact_widget_details_notify_cb), information);
652 tp_clear_object (&information->details_cancellable);
655 static void
656 contact_widget_details_feature_prepared_cb (GObject *object,
657 GAsyncResult *res,
658 gpointer user_data)
660 TpConnection *connection = TP_CONNECTION (object);
661 EmpathyContactWidget *information = user_data;
662 TpContact *contact;
663 TpContactInfoFlags flags;
665 if (!tp_proxy_prepare_finish (connection, res, NULL))
667 gtk_widget_hide (information->vbox_details);
668 return;
671 /* If we want to edit info, but connection does not support that, stop */
672 flags = tp_connection_get_contact_info_flags (connection);
673 if ((flags & TP_CONTACT_INFO_FLAG_CAN_SET) == 0 &&
674 (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) != 0)
676 gtk_widget_hide (information->vbox_details);
677 return;
680 /* Request the contact's info */
681 gtk_widget_show (information->vbox_details);
682 gtk_widget_show (information->hbox_details_requested);
683 gtk_widget_hide (information->table_details);
684 gtk_spinner_start (GTK_SPINNER (information->spinner_details));
686 contact = empathy_contact_get_tp_contact (information->contact);
687 g_assert (information->details_cancellable == NULL);
688 information->details_cancellable = g_cancellable_new ();
689 tp_contact_request_contact_info_async (contact,
690 information->details_cancellable, contact_widget_details_request_cb,
691 information);
694 static void
695 contact_widget_details_update (EmpathyContactWidget *information)
697 TpContact *tp_contact = NULL;
699 if ((information->flags & EMPATHY_CONTACT_WIDGET_SHOW_DETAILS) == 0 &&
700 (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_DETAILS) == 0)
701 return;
703 gtk_widget_hide (information->vbox_details);
705 if (information->contact != NULL)
706 tp_contact = empathy_contact_get_tp_contact (information->contact);
708 if (tp_contact != NULL)
710 GQuark features[] = { TP_CONNECTION_FEATURE_CONTACT_INFO, 0 };
711 TpConnection *connection;
713 /* First, make sure the CONTACT_INFO feature is ready on the connection */
714 connection = tp_contact_get_connection (tp_contact);
715 tp_proxy_prepare_async (connection, features,
716 contact_widget_details_feature_prepared_cb, information);
720 static void
721 contact_widget_client_update (EmpathyContactWidget *information)
723 /* FIXME: Needs new telepathy spec */
726 static void
727 contact_widget_client_setup (EmpathyContactWidget *information)
729 /* FIXME: Needs new telepathy spec */
730 gtk_widget_hide (information->vbox_client);
733 static void
734 contact_widget_groups_update (EmpathyContactWidget *information)
736 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
737 information->contact != NULL)
739 FolksPersona *persona =
740 empathy_contact_get_persona (information->contact);
742 if (FOLKS_IS_GROUP_DETAILS (persona))
744 empathy_groups_widget_set_group_details (
745 EMPATHY_GROUPS_WIDGET (information->groups_widget),
746 FOLKS_GROUP_DETAILS (persona));
747 gtk_widget_show (information->groups_widget);
749 return;
753 /* In case of failure */
754 gtk_widget_hide (information->groups_widget);
757 /* Converts the Location's GHashTable's key to a user readable string */
758 static const gchar *
759 location_key_to_label (const gchar *key)
761 if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY_CODE) == FALSE)
762 return _("Country ISO Code:");
763 else if (tp_strdiff (key, EMPATHY_LOCATION_COUNTRY) == FALSE)
764 return _("Country:");
765 else if (tp_strdiff (key, EMPATHY_LOCATION_REGION) == FALSE)
766 return _("State:");
767 else if (tp_strdiff (key, EMPATHY_LOCATION_LOCALITY) == FALSE)
768 return _("City:");
769 else if (tp_strdiff (key, EMPATHY_LOCATION_AREA) == FALSE)
770 return _("Area:");
771 else if (tp_strdiff (key, EMPATHY_LOCATION_POSTAL_CODE) == FALSE)
772 return _("Postal Code:");
773 else if (tp_strdiff (key, EMPATHY_LOCATION_STREET) == FALSE)
774 return _("Street:");
775 else if (tp_strdiff (key, EMPATHY_LOCATION_BUILDING) == FALSE)
776 return _("Building:");
777 else if (tp_strdiff (key, EMPATHY_LOCATION_FLOOR) == FALSE)
778 return _("Floor:");
779 else if (tp_strdiff (key, EMPATHY_LOCATION_ROOM) == FALSE)
780 return _("Room:");
781 else if (tp_strdiff (key, EMPATHY_LOCATION_TEXT) == FALSE)
782 return _("Text:");
783 else if (tp_strdiff (key, EMPATHY_LOCATION_DESCRIPTION) == FALSE)
784 return _("Description:");
785 else if (tp_strdiff (key, EMPATHY_LOCATION_URI) == FALSE)
786 return _("URI:");
787 else if (tp_strdiff (key, EMPATHY_LOCATION_ACCURACY_LEVEL) == FALSE)
788 return _("Accuracy Level:");
789 else if (tp_strdiff (key, EMPATHY_LOCATION_ERROR) == FALSE)
790 return _("Error:");
791 else if (tp_strdiff (key, EMPATHY_LOCATION_VERTICAL_ERROR_M) == FALSE)
792 return _("Vertical Error (meters):");
793 else if (tp_strdiff (key, EMPATHY_LOCATION_HORIZONTAL_ERROR_M) == FALSE)
794 return _("Horizontal Error (meters):");
795 else if (tp_strdiff (key, EMPATHY_LOCATION_SPEED) == FALSE)
796 return _("Speed:");
797 else if (tp_strdiff (key, EMPATHY_LOCATION_BEARING) == FALSE)
798 return _("Bearing:");
799 else if (tp_strdiff (key, EMPATHY_LOCATION_CLIMB) == FALSE)
800 return _("Climb Speed:");
801 else if (tp_strdiff (key, EMPATHY_LOCATION_TIMESTAMP) == FALSE)
802 return _("Last Updated on:");
803 else if (tp_strdiff (key, EMPATHY_LOCATION_LON) == FALSE)
804 return _("Longitude:");
805 else if (tp_strdiff (key, EMPATHY_LOCATION_LAT) == FALSE)
806 return _("Latitude:");
807 else if (tp_strdiff (key, EMPATHY_LOCATION_ALT) == FALSE)
808 return _("Altitude:");
809 else
811 DEBUG ("Unexpected Location key: %s", key);
812 return key;
816 static void
817 contact_widget_location_update (EmpathyContactWidget *information)
819 GHashTable *location;
820 GValue *value;
821 gdouble lat = 0.0, lon = 0.0;
822 gboolean has_position = TRUE;
823 GtkWidget *label;
824 guint row = 0;
825 static const gchar* ordered_geolocation_keys[] = {
826 EMPATHY_LOCATION_TEXT,
827 EMPATHY_LOCATION_URI,
828 EMPATHY_LOCATION_DESCRIPTION,
829 EMPATHY_LOCATION_BUILDING,
830 EMPATHY_LOCATION_FLOOR,
831 EMPATHY_LOCATION_ROOM,
832 EMPATHY_LOCATION_STREET,
833 EMPATHY_LOCATION_AREA,
834 EMPATHY_LOCATION_LOCALITY,
835 EMPATHY_LOCATION_REGION,
836 EMPATHY_LOCATION_COUNTRY,
837 NULL
839 int i;
840 const gchar *skey;
841 gboolean display_map = FALSE;
843 if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
845 gtk_widget_hide (information->vbox_location);
846 return;
849 location = empathy_contact_get_location (information->contact);
850 if (location == NULL || g_hash_table_size (location) == 0)
852 gtk_widget_hide (information->vbox_location);
853 return;
856 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
857 if (value == NULL)
858 has_position = FALSE;
859 else
860 lat = g_value_get_double (value);
862 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
863 if (value == NULL)
864 has_position = FALSE;
865 else
866 lon = g_value_get_double (value);
868 value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
869 if (value == NULL)
871 gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
872 gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
873 g_free (loc);
875 else
877 gchar *user_date;
878 gchar *text;
879 gint64 stamp;
880 time_t time_;
881 gchar *tmp;
883 stamp = g_value_get_int64 (value);
884 time_ = stamp;
886 user_date = empathy_time_to_string_relative (time_);
888 tmp = g_strdup_printf ("<b>%s</b>", _("Location"));
889 /* translators: format is "Location, $date" */
890 text = g_strdup_printf (_("%s, %s"), tmp, user_date);
891 g_free (tmp);
892 gtk_label_set_markup (GTK_LABEL (information->label_location), text);
893 g_free (user_date);
894 g_free (text);
898 /* Prepare the location information table */
899 if (information->table_location != NULL)
901 gtk_widget_destroy (information->table_location);
904 information->table_location = gtk_table_new (1, 2, FALSE);
905 gtk_box_pack_start (GTK_BOX (information->subvbox_location),
906 information->table_location, FALSE, FALSE, 5);
909 for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
911 const gchar* user_label;
912 GValue *gvalue;
913 char *svalue = NULL;
915 gvalue = g_hash_table_lookup (location, (gpointer) skey);
916 if (gvalue == NULL)
917 continue;
919 user_label = location_key_to_label (skey);
921 label = gtk_label_new (user_label);
922 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
923 gtk_table_attach (GTK_TABLE (information->table_location),
924 label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
925 gtk_widget_show (label);
927 if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
929 gdouble dvalue;
930 dvalue = g_value_get_double (gvalue);
931 svalue = g_strdup_printf ("%f", dvalue);
933 else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
935 svalue = g_value_dup_string (gvalue);
937 else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
939 time_t time_;
941 time_ = g_value_get_int64 (value);
942 svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
945 if (svalue != NULL)
947 label = gtk_label_new (svalue);
948 gtk_table_attach_defaults (GTK_TABLE (information->table_location),
949 label, 1, 2, row, row + 1);
950 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
951 gtk_widget_show (label);
953 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
954 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
957 g_free (svalue);
958 row++;
961 #ifdef HAVE_LIBCHAMPLAIN
962 if (has_position &&
963 !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
965 /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
966 * windows */
967 display_map = TRUE;
969 #endif
971 if (row > 0)
973 /* We can display some fields */
974 gtk_widget_show (information->table_location);
976 else if (!display_map)
978 /* Can't display either fields or map */
979 gtk_widget_hide (information->vbox_location);
980 return;
983 #ifdef HAVE_LIBCHAMPLAIN
984 if (display_map)
986 ClutterActor *marker;
987 ChamplainMarkerLayer *layer;
989 information->map_view_embed = gtk_champlain_embed_new ();
990 information->map_view = gtk_champlain_embed_get_view (
991 GTK_CHAMPLAIN_EMBED (information->map_view_embed));
993 gtk_container_add (GTK_CONTAINER (information->viewport_map),
994 information->map_view_embed);
995 g_object_set (G_OBJECT (information->map_view),
996 "kinetic-mode", TRUE,
997 "zoom-level", 10,
998 NULL);
1000 layer = champlain_marker_layer_new ();
1001 champlain_view_add_layer (information->map_view, CHAMPLAIN_LAYER (layer));
1003 marker = champlain_label_new_with_text (
1004 empathy_contact_get_alias (information->contact), NULL, NULL, NULL);
1005 champlain_location_set_location (CHAMPLAIN_LOCATION (marker), lat, lon);
1006 champlain_marker_layer_add_marker (layer, CHAMPLAIN_MARKER (marker));
1008 champlain_view_center_on (information->map_view, lat, lon);
1009 gtk_widget_show_all (information->viewport_map);
1011 #endif
1013 gtk_widget_show (information->vbox_location);
1016 static void
1017 save_avatar_menu_activate_cb (GtkWidget *widget,
1018 EmpathyContactWidget *information)
1020 GtkWidget *dialog;
1021 EmpathyAvatar *avatar;
1022 gchar *ext = NULL, *filename;
1024 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
1025 NULL,
1026 GTK_FILE_CHOOSER_ACTION_SAVE,
1027 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1028 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1029 NULL);
1031 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
1032 TRUE);
1034 /* look for the avatar extension */
1035 avatar = empathy_contact_get_avatar (information->contact);
1036 if (avatar->format != NULL)
1038 gchar **splitted;
1040 splitted = g_strsplit (avatar->format, "/", 2);
1041 if (splitted[0] != NULL && splitted[1] != NULL)
1042 ext = g_strdup (splitted[1]);
1044 g_strfreev (splitted);
1046 else
1048 /* Avatar was loaded from the cache so was converted to PNG */
1049 ext = g_strdup ("png");
1052 if (ext != NULL)
1054 gchar *id;
1056 id = tp_escape_as_identifier (empathy_contact_get_id (
1057 information->contact));
1059 filename = g_strdup_printf ("%s.%s", id, ext);
1060 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
1062 g_free (id);
1063 g_free (ext);
1064 g_free (filename);
1067 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
1069 GError *error = NULL;
1071 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1073 if (!empathy_avatar_save_to_file (avatar, filename, &error))
1075 /* Save error */
1076 GtkWidget *error_dialog;
1078 error_dialog = gtk_message_dialog_new (NULL, 0,
1079 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1080 _("Unable to save avatar"));
1082 gtk_message_dialog_format_secondary_text (
1083 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
1085 g_signal_connect (error_dialog, "response",
1086 G_CALLBACK (gtk_widget_destroy), NULL);
1088 gtk_window_present (GTK_WINDOW (error_dialog));
1090 g_clear_error (&error);
1093 g_free (filename);
1096 gtk_widget_destroy (dialog);
1099 static void
1100 popup_avatar_menu (EmpathyContactWidget *information,
1101 GtkWidget *parent,
1102 GdkEventButton *event)
1104 GtkWidget *menu, *item;
1105 gint button, event_time;
1107 if (information->contact == NULL ||
1108 empathy_contact_get_avatar (information->contact) == NULL)
1109 return;
1111 menu = empathy_context_menu_new (parent);
1113 /* Add "Save as..." entry */
1114 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
1115 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1116 gtk_widget_show (item);
1118 g_signal_connect (item, "activate",
1119 G_CALLBACK (save_avatar_menu_activate_cb), information);
1121 if (event)
1123 button = event->button;
1124 event_time = event->time;
1126 else
1128 button = 0;
1129 event_time = gtk_get_current_event_time ();
1132 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1133 button, event_time);
1136 static gboolean
1137 widget_avatar_popup_menu_cb (GtkWidget *widget,
1138 EmpathyContactWidget *information)
1140 popup_avatar_menu (information, widget, NULL);
1142 return TRUE;
1145 static gboolean
1146 widget_avatar_button_press_event_cb (GtkWidget *widget,
1147 GdkEventButton *event,
1148 EmpathyContactWidget *information)
1150 /* Ignore double-clicks and triple-clicks */
1151 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1153 popup_avatar_menu (information, widget, event);
1154 return TRUE;
1157 return FALSE;
1160 static void
1161 set_avatar_cb (GObject *source,
1162 GAsyncResult *res,
1163 gpointer user_data)
1165 GError *error = NULL;
1167 if (!tp_account_set_avatar_finish (TP_ACCOUNT (source), res, &error)) {
1168 DEBUG ("Failed to set Account.Avatar: %s", error->message);
1169 g_error_free (error);
1173 static void
1174 set_avatar_on_account (TpAccount *account,
1175 const gchar *data,
1176 gsize size,
1177 const gchar *mime_type)
1179 DEBUG ("%s Account.Avatar on %s", size > 0 ? "Set": "Clear",
1180 tp_proxy_get_object_path (account));
1182 tp_account_set_avatar_async (account, (const guchar *) data, size,
1183 mime_type, set_avatar_cb, NULL);
1186 static void
1187 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
1188 EmpathyContactWidget *information)
1190 const gchar *data;
1191 gsize size;
1192 const gchar *mime_type;
1193 TpAccount *account;
1195 empathy_avatar_chooser_get_image_data (
1196 EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
1197 &data, &size, &mime_type);
1199 account = empathy_contact_get_account (information->contact);
1200 set_avatar_on_account (account, data, size, mime_type);
1203 static void
1204 set_nickname_cb (GObject *source,
1205 GAsyncResult *res,
1206 gpointer user_data)
1208 GError *error = NULL;
1210 if (!tp_account_set_nickname_finish (TP_ACCOUNT (source), res, &error))
1212 DEBUG ("Failed to set Account.Nickname: %s", error->message);
1213 g_error_free (error);
1217 /* Update all the contact info fields having the
1218 * Overwritten_By_Nickname flag to the new alias. This avoid accidentally
1219 * reseting the alias when calling SetContactInfo(). See bgo #644298 for
1220 * details. */
1221 static void
1222 update_nickname_in_contact_info (EmpathyContactWidget *self,
1223 TpConnection *connection,
1224 const gchar *nickname)
1226 GList *specs, *l;
1228 specs = tp_connection_get_contact_info_supported_fields (connection);
1230 for (l = self->details_to_set; l != NULL; l= g_list_next (l))
1232 TpContactInfoField *field = l->data;
1233 TpContactInfoFieldSpec *spec;
1235 spec = get_spec_from_list (specs, field->field_name);
1236 /* We shouldn't have added the field to details_to_set if it's not
1237 * supported by the CM */
1238 g_assert (spec != NULL);
1240 if (spec->flags & TP_CONTACT_INFO_FIELD_FLAG_OVERWRITTEN_BY_NICKNAME)
1242 const gchar *strv[] = { nickname, NULL };
1244 DEBUG ("Updating field '%s' to '%s' as it has the "
1245 "Overwritten_By_Nickname flag and Account.Nickname has "
1246 "been updated", field->field_name, nickname);
1248 if (field->field_value != NULL)
1249 g_strfreev (field->field_value);
1250 field->field_value = g_strdupv ((GStrv) strv);
1254 g_list_free (specs);
1257 static gboolean
1258 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
1259 GdkEventFocus *event,
1260 EmpathyContactWidget *information)
1262 if (information->contact)
1264 const gchar *alias;
1266 alias = gtk_entry_get_text (GTK_ENTRY (editable));
1268 if (empathy_contact_is_user (information->contact))
1270 TpAccount * account;
1271 const gchar *current_nickname;
1273 account = empathy_contact_get_account (information->contact);
1274 current_nickname = tp_account_get_nickname (account);
1276 if (tp_strdiff (current_nickname, alias))
1278 DEBUG ("Set Account.Nickname to %s", alias);
1280 tp_account_set_nickname_async (account, alias, set_nickname_cb,
1281 NULL);
1283 update_nickname_in_contact_info (information,
1284 empathy_contact_get_connection (information->contact), alias);
1287 else
1289 empathy_contact_set_alias (information->contact, alias);
1293 return FALSE;
1296 static void
1297 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
1298 EmpathyAvatarChooser *avatar_chooser)
1300 TpAccount *account;
1302 account = empathy_account_chooser_get_account (account_chooser);
1303 if (account == NULL)
1304 return;
1306 empathy_avatar_chooser_set_account (avatar_chooser, account);
1309 static void
1310 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
1312 EmpathyAvatar *avatar = NULL;
1314 if (information->contact)
1315 avatar = empathy_contact_get_avatar (information->contact);
1317 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1319 g_signal_handlers_block_by_func (information->widget_avatar,
1320 contact_widget_avatar_changed_cb,
1321 information);
1322 empathy_avatar_chooser_set (
1323 EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
1324 g_signal_handlers_unblock_by_func (information->widget_avatar,
1325 contact_widget_avatar_changed_cb, information);
1327 else
1328 empathy_avatar_image_set (
1329 EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
1332 static void
1333 contact_widget_name_notify_cb (EmpathyContactWidget *information)
1335 if (GTK_IS_ENTRY (information->widget_alias))
1336 gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
1337 empathy_contact_get_alias (information->contact));
1338 else
1339 gtk_label_set_label (GTK_LABEL (information->widget_alias),
1340 empathy_contact_get_alias (information->contact));
1343 static void
1344 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
1346 const gchar *status;
1347 gchar *markup_text = NULL;
1349 status = empathy_contact_get_status (information->contact);
1350 if (status != NULL)
1351 markup_text = empathy_add_link_markup (status);
1352 gtk_label_set_markup (GTK_LABEL (information->label_status), markup_text);
1353 g_free (markup_text);
1355 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
1356 empathy_icon_name_for_contact (information->contact),
1357 GTK_ICON_SIZE_BUTTON);
1358 gtk_widget_show (information->image_state);
1361 static void
1362 contact_widget_favourites_changed_cb (EmpathyContactManager *manager,
1363 EmpathyContact *contact,
1364 gboolean is_favourite,
1365 EmpathyContactWidget *information)
1367 if (contact != information->contact)
1368 return;
1370 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (
1371 information->favourite_checkbox), is_favourite);
1374 static void
1375 contact_widget_remove_contact (EmpathyContactWidget *information)
1377 if (information->contact)
1379 TpContact *tp_contact;
1381 contact_widget_save (information);
1383 g_signal_handlers_disconnect_by_func (information->contact,
1384 contact_widget_name_notify_cb, information);
1385 g_signal_handlers_disconnect_by_func (information->contact,
1386 contact_widget_presence_notify_cb, information);
1387 g_signal_handlers_disconnect_by_func (information->contact,
1388 contact_widget_avatar_notify_cb, information);
1390 tp_contact = empathy_contact_get_tp_contact (information->contact);
1391 if (tp_contact != NULL)
1393 g_signal_handlers_disconnect_by_func (tp_contact,
1394 contact_widget_details_notify_cb, information);
1397 g_object_unref (information->contact);
1398 information->contact = NULL;
1401 if (information->details_cancellable != NULL)
1403 g_cancellable_cancel (information->details_cancellable);
1404 tp_clear_object (&information->details_cancellable);
1408 static void contact_widget_change_contact (EmpathyContactWidget *information);
1410 static void
1411 contact_widget_contact_update (EmpathyContactWidget *information)
1413 TpAccount *account = NULL;
1414 const gchar *id = NULL;
1416 /* Connect and get info from new contact */
1417 if (information->contact)
1419 g_signal_connect_swapped (information->contact, "notify::name",
1420 G_CALLBACK (contact_widget_name_notify_cb), information);
1421 g_signal_connect_swapped (information->contact, "notify::presence",
1422 G_CALLBACK (contact_widget_presence_notify_cb), information);
1423 g_signal_connect_swapped (information->contact,
1424 "notify::presence-message",
1425 G_CALLBACK (contact_widget_presence_notify_cb), information);
1426 g_signal_connect_swapped (information->contact, "notify::avatar",
1427 G_CALLBACK (contact_widget_avatar_notify_cb), information);
1429 account = empathy_contact_get_account (information->contact);
1430 id = empathy_contact_get_id (information->contact);
1433 /* Update account widget */
1434 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1436 if (account)
1438 g_signal_handlers_block_by_func (information->widget_account,
1439 contact_widget_change_contact,
1440 information);
1441 empathy_account_chooser_set_account (
1442 EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
1443 g_signal_handlers_unblock_by_func (information->widget_account,
1444 contact_widget_change_contact, information);
1447 else
1449 if (account)
1451 const gchar *name;
1453 name = tp_account_get_display_name (account);
1454 gtk_label_set_label (GTK_LABEL (information->label_account), name);
1456 name = tp_account_get_icon_name (account);
1457 gtk_image_set_from_icon_name (GTK_IMAGE (information->image_account),
1458 name, GTK_ICON_SIZE_MENU);
1462 /* Update id widget */
1463 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1464 gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
1465 else
1466 gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
1468 /* Update other widgets */
1469 if (information->contact)
1471 contact_widget_name_notify_cb (information);
1472 contact_widget_presence_notify_cb (information);
1473 contact_widget_avatar_notify_cb (information);
1475 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1477 FolksPersona *persona = empathy_contact_get_persona (
1478 information->contact);
1480 if (persona != NULL && FOLKS_IS_FAVOURITE_DETAILS (persona))
1482 gboolean is_favourite = folks_favourite_details_get_is_favourite (
1483 FOLKS_FAVOURITE_DETAILS (persona));
1484 contact_widget_favourites_changed_cb (information->manager,
1485 information->contact, is_favourite, information);
1489 gtk_widget_show (information->label_alias);
1490 gtk_widget_show (information->widget_alias);
1491 gtk_widget_show (information->hbox_presence);
1492 gtk_widget_show (information->widget_avatar);
1494 else
1496 gtk_widget_hide (information->label_alias);
1497 gtk_widget_hide (information->widget_alias);
1498 gtk_widget_hide (information->hbox_presence);
1499 gtk_widget_hide (information->widget_avatar);
1503 static void
1504 contact_widget_set_contact (EmpathyContactWidget *information,
1505 EmpathyContact *contact)
1507 if (contact == information->contact)
1508 return;
1510 contact_widget_remove_contact (information);
1511 if (contact)
1512 information->contact = g_object_ref (contact);
1514 /* set the selected account to be the account this contact came from */
1515 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (information->widget_account)) {
1516 empathy_account_chooser_set_account (
1517 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1518 empathy_contact_get_account (contact));
1521 /* Update information for widgets */
1522 contact_widget_contact_update (information);
1523 contact_widget_groups_update (information);
1524 contact_widget_details_update (information);
1525 contact_widget_client_update (information);
1526 contact_widget_location_update (information);
1529 static void
1530 contact_widget_got_contact_cb (TpConnection *connection,
1531 EmpathyContact *contact,
1532 const GError *error,
1533 gpointer user_data,
1534 GObject *weak_object)
1536 EmpathyContactWidget *information = user_data;
1538 if (error != NULL)
1540 DEBUG ("Error: %s", error->message);
1541 return;
1544 contact_widget_set_contact (information, contact);
1547 static void
1548 contact_widget_change_contact (EmpathyContactWidget *information)
1550 TpConnection *connection;
1552 connection = empathy_account_chooser_get_connection (
1553 EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
1554 if (!connection)
1555 return;
1557 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1559 const gchar *id;
1561 id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
1562 if (!EMP_STR_EMPTY (id))
1564 empathy_tp_contact_factory_get_from_id (connection, id,
1565 contact_widget_got_contact_cb, information, NULL,
1566 G_OBJECT (information->vbox_contact_widget));
1569 else
1571 empathy_tp_contact_factory_get_from_handle (connection,
1572 tp_connection_get_self_handle (connection),
1573 contact_widget_got_contact_cb, information, NULL,
1574 G_OBJECT (information->vbox_contact_widget));
1578 static gboolean
1579 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
1581 contact_widget_change_contact (self);
1582 return FALSE;
1585 static void
1586 contact_widget_id_changed_cb (GtkEntry *entry,
1587 EmpathyContactWidget *self)
1589 if (self->widget_id_timeout != 0)
1591 g_source_remove (self->widget_id_timeout);
1594 self->widget_id_timeout =
1595 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
1596 (GSourceFunc) contact_widget_id_activate_timeout, self);
1599 static gboolean
1600 contact_widget_id_focus_out_cb (GtkWidget *widget,
1601 GdkEventFocus *event,
1602 EmpathyContactWidget *information)
1604 contact_widget_change_contact (information);
1605 return FALSE;
1608 static void
1609 favourite_toggled_cb (GtkToggleButton *button,
1610 EmpathyContactWidget *information)
1612 FolksPersona *persona = empathy_contact_get_persona (information->contact);
1614 if (persona != NULL && FOLKS_IS_FAVOURITE_DETAILS (persona))
1616 gboolean active = gtk_toggle_button_get_active (button);
1617 folks_favourite_details_set_is_favourite (
1618 FOLKS_FAVOURITE_DETAILS (persona), active);
1622 static void
1623 contact_widget_contact_setup (EmpathyContactWidget *information)
1625 information->label_status = gtk_label_new ("");
1626 gtk_label_set_line_wrap_mode (GTK_LABEL (information->label_status),
1627 PANGO_WRAP_WORD_CHAR);
1628 gtk_label_set_line_wrap (GTK_LABEL (information->label_status),
1629 TRUE);
1631 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
1632 gtk_label_set_selectable (GTK_LABEL (information->label_status), TRUE);
1634 gtk_box_pack_start (GTK_BOX (information->hbox_presence),
1635 information->label_status, TRUE, TRUE, 0);
1636 gtk_widget_show (information->label_status);
1638 /* Setup account label/chooser */
1639 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1641 information->widget_account = empathy_account_chooser_new ();
1643 g_signal_connect_swapped (information->widget_account, "changed",
1644 G_CALLBACK (contact_widget_change_contact),
1645 information);
1647 else
1649 /* Pack the protocol icon with the account name in an hbox */
1650 information->widget_account = gtk_hbox_new (FALSE, 6);
1652 information->label_account = gtk_label_new (NULL);
1653 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1654 gtk_label_set_selectable (GTK_LABEL (information->label_account), TRUE);
1656 gtk_misc_set_alignment (GTK_MISC (information->label_account), 0, 0.5);
1657 gtk_widget_show (information->label_account);
1659 information->image_account = gtk_image_new ();
1660 gtk_widget_show (information->image_account);
1662 gtk_box_pack_start (GTK_BOX (information->widget_account),
1663 information->image_account, FALSE, FALSE, 0);
1664 gtk_box_pack_start (GTK_BOX (information->widget_account),
1665 information->label_account, FALSE, TRUE, 0);
1667 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1668 information->widget_account,
1669 1, 2, 0, 1);
1670 gtk_widget_show (information->widget_account);
1672 /* Set up avatar chooser/display */
1673 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
1675 information->widget_avatar = empathy_avatar_chooser_new ();
1676 g_signal_connect (information->widget_avatar, "changed",
1677 G_CALLBACK (contact_widget_avatar_changed_cb),
1678 information);
1679 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
1681 g_signal_connect (information->widget_account, "changed",
1682 G_CALLBACK (update_avatar_chooser_account_cb),
1683 information->widget_avatar);
1684 update_avatar_chooser_account_cb (
1685 EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
1686 EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
1689 else
1691 information->widget_avatar = empathy_avatar_image_new ();
1693 g_signal_connect (information->widget_avatar, "popup-menu",
1694 G_CALLBACK (widget_avatar_popup_menu_cb), information);
1695 g_signal_connect (information->widget_avatar, "button-press-event",
1696 G_CALLBACK (widget_avatar_button_press_event_cb), information);
1699 gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
1700 information->widget_avatar,
1701 FALSE, FALSE,
1703 gtk_widget_show (information->widget_avatar);
1705 /* Setup id label/entry */
1706 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1708 information->widget_id = gtk_entry_new ();
1709 g_signal_connect (information->widget_id, "focus-out-event",
1710 G_CALLBACK (contact_widget_id_focus_out_cb),
1711 information);
1712 g_signal_connect (information->widget_id, "changed",
1713 G_CALLBACK (contact_widget_id_changed_cb),
1714 information);
1716 else
1718 information->widget_id = gtk_label_new (NULL);
1719 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1720 gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
1722 gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
1724 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1725 information->widget_id,
1726 1, 2, 1, 2);
1727 gtk_widget_show (information->widget_id);
1729 /* Setup alias label/entry */
1730 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
1732 information->widget_alias = gtk_entry_new ();
1734 if (!(information->flags & EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS))
1735 g_signal_connect (information->widget_alias, "focus-out-event",
1736 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
1737 information);
1739 /* Make return activate the window default (the Close button) */
1740 gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
1741 TRUE);
1743 else
1745 information->widget_alias = gtk_label_new (NULL);
1746 if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
1747 gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
1749 gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
1751 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1752 information->widget_alias,
1753 1, 2, 2, 3);
1754 if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
1755 gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
1757 gtk_widget_show (information->widget_alias);
1759 /* Favorite */
1760 if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE)
1762 information->favourite_checkbox = gtk_check_button_new_with_label (
1763 _("Favorite"));
1765 g_signal_connect (information->favourite_checkbox, "toggled",
1766 G_CALLBACK (favourite_toggled_cb), information);
1768 gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
1769 information->favourite_checkbox, 0, 2, 3, 4);
1771 information->fav_sig_id = g_signal_connect (information->manager,
1772 "favourites-changed",
1773 G_CALLBACK (contact_widget_favourites_changed_cb), information);
1775 gtk_widget_show (information->favourite_checkbox);
1779 static void
1780 contact_widget_destroy_cb (GtkWidget *widget,
1781 EmpathyContactWidget *information)
1783 contact_widget_remove_contact (information);
1785 if (information->widget_id_timeout != 0)
1787 g_source_remove (information->widget_id_timeout);
1790 if (information->fav_sig_id != 0)
1791 g_signal_handler_disconnect (information->manager, information->fav_sig_id);
1793 g_object_unref (information->manager);
1795 g_slice_free (EmpathyContactWidget, information);
1799 * empathy_contact_widget_new:
1800 * @contact: an #EmpathyContact
1801 * @flags: #EmpathyContactWidgetFlags for the new contact widget
1803 * Creates a new #EmpathyContactWidget.
1805 * Return value: a new #EmpathyContactWidget
1807 GtkWidget *
1808 empathy_contact_widget_new (EmpathyContact *contact,
1809 EmpathyContactWidgetFlags flags)
1811 EmpathyContactWidget *information;
1812 GtkBuilder *gui;
1813 gchar *filename;
1815 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
1817 information = g_slice_new0 (EmpathyContactWidget);
1818 information->flags = flags;
1820 filename = empathy_file_lookup ("empathy-contact-widget.ui",
1821 "libempathy-gtk");
1822 gui = empathy_builder_get_file (filename,
1823 "vbox_contact_widget", &information->vbox_contact_widget,
1824 "hbox_contact", &information->hbox_contact,
1825 "hbox_presence", &information->hbox_presence,
1826 "label_alias", &information->label_alias,
1827 "image_state", &information->image_state,
1828 "table_contact", &information->table_contact,
1829 "vbox_avatar", &information->vbox_avatar,
1830 "vbox_location", &information->vbox_location,
1831 "subvbox_location", &information->subvbox_location,
1832 "label_location", &information->label_location,
1833 #ifdef HAVE_LIBCHAMPLAIN
1834 "viewport_map", &information->viewport_map,
1835 #endif
1836 "groups_widget", &information->groups_widget,
1837 "vbox_details", &information->vbox_details,
1838 "table_details", &information->table_details,
1839 "hbox_details_requested", &information->hbox_details_requested,
1840 "vbox_client", &information->vbox_client,
1841 "table_client", &information->table_client,
1842 "hbox_client_requested", &information->hbox_client_requested,
1843 NULL);
1844 g_free (filename);
1846 empathy_builder_connect (gui, information,
1847 "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
1848 NULL);
1849 information->table_location = NULL;
1851 g_object_set_data (G_OBJECT (information->vbox_contact_widget),
1852 "EmpathyContactWidget",
1853 information);
1855 information->manager = empathy_contact_manager_dup_singleton ();
1857 /* Create widgets */
1858 contact_widget_contact_setup (information);
1859 contact_widget_details_setup (information);
1860 contact_widget_client_setup (information);
1862 if (contact != NULL)
1863 contact_widget_set_contact (information, contact);
1864 else if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT ||
1865 information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
1866 contact_widget_change_contact (information);
1868 return empathy_builder_unref_and_keep_widget (gui,
1869 information->vbox_contact_widget);
1873 * empathy_contact_widget_get_contact:
1874 * @widget: an #EmpathyContactWidget
1876 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
1878 * Returns: the #EmpathyContact associated with @widget
1880 EmpathyContact *
1881 empathy_contact_widget_get_contact (GtkWidget *widget)
1883 EmpathyContactWidget *information;
1885 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1887 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1888 if (!information)
1889 return NULL;
1891 return information->contact;
1894 const gchar *
1895 empathy_contact_widget_get_alias (GtkWidget *widget)
1897 EmpathyContactWidget *information;
1899 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1901 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1902 if (!information)
1903 return NULL;
1905 return gtk_entry_get_text (GTK_ENTRY (information->widget_alias));
1909 * empathy_contact_widget_set_contact:
1910 * @widget: an #EmpathyContactWidget
1911 * @contact: a different #EmpathyContact
1913 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
1915 void
1916 empathy_contact_widget_set_contact (GtkWidget *widget,
1917 EmpathyContact *contact)
1919 EmpathyContactWidget *information;
1921 g_return_if_fail (GTK_IS_WIDGET (widget));
1922 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1924 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1925 if (!information)
1926 return;
1928 contact_widget_set_contact (information, contact);
1932 * empathy_contact_widget_set_account_filter:
1933 * @widget: an #EmpathyContactWidget
1934 * @filter: a #EmpathyAccountChooserFilterFunc
1935 * @user_data: user data to pass to @filter, or %NULL
1937 * Set a filter on the #EmpathyAccountChooser included in the
1938 * #EmpathyContactWidget.
1940 void
1941 empathy_contact_widget_set_account_filter (
1942 GtkWidget *widget,
1943 EmpathyAccountChooserFilterFunc filter,
1944 gpointer user_data)
1946 EmpathyContactWidget *information;
1947 EmpathyAccountChooser *chooser;
1949 g_return_if_fail (GTK_IS_WIDGET (widget));
1951 information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
1952 if (!information)
1953 return;
1955 chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
1956 if (chooser)
1957 empathy_account_chooser_set_filter (chooser, filter, user_data);