2 * empathy-user-info.c - Source for EmpathyUserInfo
4 * Copyright (C) 2012 - Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with This library. If not, see <http://www.gnu.org/licenses/>.
21 #include "empathy-user-info.h"
23 #include <glib/gi18n-lib.h>
25 #include "empathy-avatar-chooser.h"
26 #include "empathy-calendar-button.h"
27 #include "empathy-contactinfo-utils.h"
28 #include "empathy-time.h"
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
32 #include "empathy-debug.h"
34 G_DEFINE_TYPE (EmpathyUserInfo, empathy_user_info, GTK_TYPE_GRID)
36 struct _EmpathyUserInfoPrivate
40 GtkWidget *avatar_chooser;
41 GtkWidget *nickname_entry;
42 GtkWidget *details_label;
43 GtkWidget *details_spinner;
45 GList *details_to_set;
46 gboolean details_changed;
47 GCancellable *details_cancellable;
56 #define DATA_FIELD "contact-info-field"
57 #define DATA_IS_CONTACT_INFO "is-contact-info"
60 contact_info_changed_cb (GtkEntry *entry,
61 EmpathyUserInfo *self)
63 const gchar *strv[] = { NULL, NULL };
64 TpContactInfoField *field;
66 self->priv->details_changed = TRUE;
68 field = g_object_get_data ((GObject *) entry, DATA_FIELD);
69 g_assert (field != NULL);
71 strv[0] = gtk_entry_get_text (entry);
73 if (field->field_value != NULL)
74 g_strfreev (field->field_value);
75 field->field_value = g_strdupv ((GStrv) strv);
79 bday_changed_cb (EmpathyCalendarButton *button,
81 EmpathyUserInfo *self)
83 const gchar *strv[] = { NULL, NULL };
84 TpContactInfoField *field;
86 self->priv->details_changed = TRUE;
88 field = g_object_get_data ((GObject *) button, DATA_FIELD);
89 g_assert (field != NULL);
95 g_date_strftime (tmp, sizeof (tmp), EMPATHY_DATE_FORMAT_DISPLAY_SHORT,
100 if (field->field_value != NULL)
101 g_strfreev (field->field_value);
102 field->field_value = g_strdupv ((GStrv) strv);
106 field_name_in_field_list (GList *list,
111 for (l = list; l != NULL; l = g_list_next (l))
113 TpContactInfoField *field = l->data;
115 if (!tp_strdiff (field->field_name, name))
122 static TpContactInfoFieldSpec *
123 get_spec_from_list (GList *list,
128 for (l = list; l != NULL; l = g_list_next (l))
130 TpContactInfoFieldSpec *spec = l->data;
132 if (!tp_strdiff (spec->name, name))
140 add_row (GtkGrid *grid,
143 gboolean contact_info)
146 gtk_grid_attach_next_to (grid, title, NULL, GTK_POS_BOTTOM, 1, 1);
147 gtk_misc_set_alignment (GTK_MISC (title), 1, 0.5);
148 gtk_style_context_add_class (gtk_widget_get_style_context (title),
149 GTK_STYLE_CLASS_DIM_LABEL);
150 gtk_widget_show (title);
153 gtk_grid_attach_next_to (grid, value, title, GTK_POS_RIGHT,
154 contact_info ? 2 : 1, 1);
155 gtk_widget_set_hexpand (value, TRUE);
156 if (GTK_IS_LABEL (value))
158 gtk_misc_set_alignment (GTK_MISC (value), 0, 0.5);
159 gtk_label_set_selectable (GTK_LABEL (value), TRUE);
161 gtk_widget_show (value);
165 g_object_set_data (G_OBJECT (title),
166 DATA_IS_CONTACT_INFO, (gpointer) TRUE);
167 g_object_set_data (G_OBJECT (value),
168 DATA_IS_CONTACT_INFO, (gpointer) TRUE);
173 fill_contact_info_grid (EmpathyUserInfo *self)
175 TpConnection *connection;
180 const char **field_names = empathy_contact_info_get_field_names (NULL);
183 g_assert (self->priv->details_to_set == NULL);
185 connection = tp_account_get_connection (self->priv->account);
186 contact = tp_connection_get_self_contact (connection);
187 specs = tp_connection_dup_contact_info_supported_fields (connection);
188 info = tp_contact_dup_contact_info (contact);
190 /* Look at the fields set in our vCard */
191 for (l = info; l != NULL; l = l->next)
193 TpContactInfoField *field = l->data;
195 /* make a copy for the details_to_set list */
196 field = tp_contact_info_field_copy (field);
197 DEBUG ("Field %s is in our vCard", field->field_name);
199 self->priv->details_to_set = g_list_prepend (self->priv->details_to_set,
203 /* Add fields which are supported but not in the vCard */
204 for (i = 0; field_names[i] != NULL; i++)
206 TpContactInfoFieldSpec *spec;
207 TpContactInfoField *field;
209 /* Check if the field was in the vCard */
210 if (field_name_in_field_list (self->priv->details_to_set,
214 /* Check if the CM supports the field */
215 spec = get_spec_from_list (specs, field_names[i]);
219 /* add an empty field so user can set a value */
220 field = tp_contact_info_field_new (spec->name, spec->parameters, NULL);
222 self->priv->details_to_set = g_list_prepend (self->priv->details_to_set,
226 /* Add widgets for supported fields */
227 self->priv->details_to_set = g_list_sort (self->priv->details_to_set,
228 (GCompareFunc) empathy_contact_info_field_spec_cmp);
230 for (l = self->priv->details_to_set; l != NULL; l= g_list_next (l))
232 TpContactInfoField *field = l->data;
233 GtkWidget *label, *w;
234 TpContactInfoFieldSpec *spec;
238 has_field = empathy_contact_info_lookup_field (field->field_name,
242 /* Empathy doesn't display this field so we can't change it.
243 * But we put it in the details_to_set list so it won't be erased
244 * when calling SetContactInfo (bgo #630427) */
245 DEBUG ("Unhandled ContactInfo field spec: %s", field->field_name);
249 spec = get_spec_from_list (specs, field->field_name);
250 /* We shouldn't have added the field to details_to_set if it's not
251 * supported by the CM */
252 g_assert (spec != NULL);
254 if (spec->flags & TP_CONTACT_INFO_FIELD_FLAG_OVERWRITTEN_BY_NICKNAME)
256 DEBUG ("Ignoring field '%s' due it to having the "
257 "Overwritten_By_Nickname flag", field->field_name);
262 title = empathy_contact_info_field_label (field->field_name,
264 (spec->flags & TP_CONTACT_INFO_FIELD_FLAG_PARAMETERS_EXACT));
265 label = gtk_label_new (title);
268 /* TODO: if TP_CONTACT_INFO_FIELD_FLAG_PARAMETERS_EXACT is not set we
269 * should allow user to tag the vCard fields (bgo#672034) */
272 if (!tp_strdiff (field->field_name, "bday"))
274 w = empathy_calendar_button_new ();
276 if (field->field_value[0])
280 g_date_set_parse (&date, field->field_value[0]);
281 if (g_date_valid (&date))
283 empathy_calendar_button_set_date (EMPATHY_CALENDAR_BUTTON (w),
288 g_signal_connect (w, "date-changed",
289 G_CALLBACK (bday_changed_cb), self);
293 w = gtk_entry_new ();
294 gtk_entry_set_text (GTK_ENTRY (w),
295 field->field_value[0] ? field->field_value[0] : "");
296 g_signal_connect (w, "changed",
297 G_CALLBACK (contact_info_changed_cb), self);
300 add_row (GTK_GRID (self), label, w, TRUE);
302 g_object_set_data ((GObject *) w, DATA_FIELD, field);
307 tp_contact_info_spec_list_free (specs);
308 tp_contact_info_list_free (info);
314 grid_foreach_cb (GtkWidget *widget,
317 if (g_object_get_data (G_OBJECT (widget), DATA_IS_CONTACT_INFO) != NULL)
318 gtk_widget_destroy (widget);
322 request_contact_info_cb (GObject *object,
326 EmpathyUserInfo *self = user_data;
327 TpContact *contact = TP_CONTACT (object);
329 GError *error = NULL;
331 if (!tp_contact_request_contact_info_finish (contact, res, &error))
333 /* If the request got cancelled it could mean the contact widget is
334 * destroyed, so we should not dereference self */
335 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
337 g_clear_error (&error);
340 g_clear_error (&error);
343 n_rows = fill_contact_info_grid (self);
345 gtk_widget_set_visible (self->priv->details_label, n_rows > 0);
346 gtk_spinner_stop (GTK_SPINNER (self->priv->details_spinner));
347 gtk_widget_hide (self->priv->details_spinner);
351 reload_contact_info (EmpathyUserInfo *self)
353 TpConnection *connection;
354 TpContact *contact = NULL;
355 TpContactInfoFlags flags;
357 /* Cancel previous RequestContactInfo, if any */
358 if (self->priv->details_cancellable != NULL)
359 g_cancellable_cancel (self->priv->details_cancellable);
360 g_clear_object (&self->priv->details_cancellable);
362 /* Remove current contact info widgets, if any */
363 gtk_container_foreach (GTK_CONTAINER (self), grid_foreach_cb, NULL);
364 gtk_widget_hide (self->priv->details_label);
365 gtk_widget_hide (self->priv->details_spinner);
367 tp_clear_pointer (&self->priv->details_to_set, tp_contact_info_list_free);
368 self->priv->details_changed = FALSE;
370 connection = tp_account_get_connection (self->priv->account);
371 if (connection != NULL)
372 contact = tp_connection_get_self_contact (connection);
374 /* Display infobar if we don't have a self contact (probably offline) */
381 infobar = gtk_info_bar_new ();
382 gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), GTK_MESSAGE_INFO);
383 content = gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar));
384 label = gtk_label_new (_("Go online to edit your personal information."));
385 gtk_container_add (GTK_CONTAINER (content), label);
386 gtk_widget_show (label);
388 gtk_grid_attach_next_to ((GtkGrid *) self, infobar,
389 NULL, GTK_POS_BOTTOM, 3, 1);
390 gtk_widget_show (infobar);
392 g_object_set_data (G_OBJECT (infobar),
393 DATA_IS_CONTACT_INFO, (gpointer) TRUE);
397 if (!tp_proxy_has_interface_by_id (connection,
398 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_INFO))
401 flags = tp_connection_get_contact_info_flags (connection);
402 if ((flags & TP_CONTACT_INFO_FLAG_CAN_SET) == 0)
405 /* Request the contact's info */
406 gtk_widget_show (self->priv->details_spinner);
407 gtk_spinner_start (GTK_SPINNER (self->priv->details_spinner));
409 g_assert (self->priv->details_cancellable == NULL);
410 self->priv->details_cancellable = g_cancellable_new ();
411 tp_contact_request_contact_info_async (contact,
412 self->priv->details_cancellable, request_contact_info_cb,
417 connection_notify_cb (EmpathyUserInfo *self)
419 TpConnection *connection = tp_account_get_connection (self->priv->account);
421 if (connection != NULL)
423 tp_g_signal_connect_object (connection, "notify::self-contact",
424 G_CALLBACK (reload_contact_info), self, G_CONNECT_SWAPPED);
427 reload_contact_info (self);
431 empathy_user_info_constructed (GObject *object)
433 EmpathyUserInfo *self = (EmpathyUserInfo *) object;
434 GtkGrid *grid = (GtkGrid *) self;
438 G_OBJECT_CLASS (empathy_user_info_parent_class)->constructed (object);
440 gtk_grid_set_column_spacing (grid, 6);
441 gtk_grid_set_row_spacing (grid, 6);
444 title = gtk_label_new (_("Identifier"));
445 value = gtk_label_new (tp_account_get_normalized_name (self->priv->account));
446 add_row (grid, title, value, FALSE);
448 /* Setup nickname entry */
449 title = gtk_label_new (_("Alias"));
450 self->priv->nickname_entry = gtk_entry_new ();
451 gtk_entry_set_text (GTK_ENTRY (self->priv->nickname_entry),
452 tp_account_get_nickname (self->priv->account));
453 add_row (grid, title, self->priv->nickname_entry, FALSE);
455 /* Set up avatar chooser */
456 self->priv->avatar_chooser = empathy_avatar_chooser_new (self->priv->account);
457 gtk_grid_attach (grid, self->priv->avatar_chooser,
459 gtk_widget_show (self->priv->avatar_chooser);
462 self->priv->details_label = gtk_label_new (NULL);
463 gtk_label_set_markup (GTK_LABEL (self->priv->details_label),
464 _("<b>Personal Details</b>"));
465 gtk_misc_set_alignment (GTK_MISC (self->priv->details_label), 0, 0.5);
466 gtk_grid_attach_next_to (grid, self->priv->details_label, NULL,
467 GTK_POS_BOTTOM, 3, 1);
469 /* Details spinner */
470 self->priv->details_spinner = gtk_spinner_new ();
471 gtk_widget_set_hexpand (self->priv->details_spinner, TRUE);
472 gtk_widget_set_vexpand (self->priv->details_spinner, TRUE);
473 gtk_grid_attach_next_to (grid, self->priv->details_spinner, NULL,
474 GTK_POS_BOTTOM, 3, 1);
476 g_signal_connect_swapped (self->priv->account, "notify::connection",
477 G_CALLBACK (connection_notify_cb), self);
478 connection_notify_cb (self);
482 empathy_user_info_dispose (GObject *object)
484 EmpathyUserInfo *self = (EmpathyUserInfo *) object;
486 if (self->priv->account != NULL)
488 /* Disconnect the signal manually, because TpAccount::dispose will emit
489 * "notify::connection" signal before tp_g_signal_connect_object() had
490 * a chance to disconnect. */
491 g_signal_handlers_disconnect_by_func (self->priv->account,
492 connection_notify_cb, self);
493 g_clear_object (&self->priv->account);
496 if (self->priv->details_cancellable != NULL)
497 g_cancellable_cancel (self->priv->details_cancellable);
498 g_clear_object (&self->priv->details_cancellable);
500 G_OBJECT_CLASS (empathy_user_info_parent_class)->dispose (object);
504 empathy_user_info_get_property (GObject *object,
509 EmpathyUserInfo *self = (EmpathyUserInfo *) object;
514 g_value_set_object (value, self->priv->account);
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
523 empathy_user_info_set_property (GObject *object,
528 EmpathyUserInfo *self = (EmpathyUserInfo *) object;
533 g_assert (self->priv->account == NULL); /* construct-only */
534 self->priv->account = g_value_dup_object (value);
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
543 empathy_user_info_init (EmpathyUserInfo *self)
545 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
546 EMPATHY_TYPE_USER_INFO, EmpathyUserInfoPrivate);
550 empathy_user_info_class_init (EmpathyUserInfoClass *klass)
552 GObjectClass *object_class = G_OBJECT_CLASS (klass);
553 GParamSpec *param_spec;
555 object_class->constructed = empathy_user_info_constructed;
556 object_class->dispose = empathy_user_info_dispose;
557 object_class->get_property = empathy_user_info_get_property;
558 object_class->set_property = empathy_user_info_set_property;
560 g_type_class_add_private (object_class, sizeof (EmpathyUserInfoPrivate));
562 param_spec = g_param_spec_object ("account",
564 "The #TpAccount on which user info should be edited",
566 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
567 g_object_class_install_property (object_class, PROP_ACCOUNT, param_spec);
571 empathy_user_info_new (TpAccount *account)
573 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
575 return g_object_new (EMPATHY_TYPE_USER_INFO,
581 empathy_user_info_discard (EmpathyUserInfo *self)
583 g_return_if_fail (EMPATHY_IS_USER_INFO (self));
585 reload_contact_info (self);
586 gtk_entry_set_text ((GtkEntry *) self->priv->nickname_entry,
587 tp_account_get_nickname (self->priv->account));
591 apply_complete_one (GSimpleAsyncResult *result)
595 count = g_simple_async_result_get_op_res_gssize (result);
597 g_simple_async_result_set_op_res_gssize (result, count);
600 g_simple_async_result_complete (result);
604 avatar_chooser_apply_cb (GObject *source,
605 GAsyncResult *result,
608 EmpathyAvatarChooser *avatar_chooser = (EmpathyAvatarChooser *) source;
609 GSimpleAsyncResult *my_result = user_data;
610 GError *error = NULL;
612 if (!empathy_avatar_chooser_apply_finish (avatar_chooser, result, &error))
613 g_simple_async_result_take_error (my_result, error);
615 apply_complete_one (my_result);
616 g_object_unref (my_result);
620 set_nickname_cb (GObject *source,
621 GAsyncResult *result,
624 TpAccount *account = (TpAccount *) source;
625 GSimpleAsyncResult *my_result = user_data;
626 GError *error = NULL;
628 if (!tp_account_set_nickname_finish (account, result, &error))
629 g_simple_async_result_take_error (my_result, error);
631 apply_complete_one (my_result);
632 g_object_unref (my_result);
636 set_contact_info_cb (GObject *source,
637 GAsyncResult *result,
640 TpConnection *connection = (TpConnection *) source;
641 GSimpleAsyncResult *my_result = user_data;
642 GError *error = NULL;
644 if (!tp_connection_set_contact_info_finish (connection, result, &error))
645 g_simple_async_result_take_error (my_result, error);
647 apply_complete_one (my_result);
648 g_object_unref (my_result);
652 field_value_is_empty (TpContactInfoField *field)
656 if (field->field_value == NULL)
659 /* Field is empty if all its values are empty */
660 for (i = 0; field->field_value[i] != NULL; i++)
662 if (!tp_str_empty (field->field_value[i]))
670 empathy_user_info_apply_async (EmpathyUserInfo *self,
671 GAsyncReadyCallback callback,
674 GSimpleAsyncResult *result;
675 const gchar *new_nickname;
679 g_return_if_fail (EMPATHY_IS_USER_INFO (self));
681 result = g_simple_async_result_new ((GObject *) self, callback, user_data,
682 empathy_user_info_apply_async);
685 empathy_avatar_chooser_apply_async (
686 (EmpathyAvatarChooser *) self->priv->avatar_chooser,
687 avatar_chooser_apply_cb, g_object_ref (result));
691 new_nickname = gtk_entry_get_text (GTK_ENTRY (self->priv->nickname_entry));
692 if (tp_strdiff (new_nickname, tp_account_get_nickname (self->priv->account)))
694 tp_account_set_nickname_async (self->priv->account, new_nickname,
695 set_nickname_cb, g_object_ref (result));
699 /* Remove empty fields */
700 for (l = self->priv->details_to_set; l != NULL; l = next)
702 TpContactInfoField *field = l->data;
705 if (field_value_is_empty (field))
707 DEBUG ("Drop empty field: %s", field->field_name);
708 tp_contact_info_field_free (field);
709 self->priv->details_to_set =
710 g_list_delete_link (self->priv->details_to_set, l);
714 if (self->priv->details_to_set != NULL)
716 if (self->priv->details_changed)
718 tp_connection_set_contact_info_async (
719 tp_account_get_connection (self->priv->account),
720 self->priv->details_to_set, set_contact_info_cb,
721 g_object_ref (result));
725 tp_contact_info_list_free (self->priv->details_to_set);
726 self->priv->details_to_set = NULL;
729 self->priv->details_changed = FALSE;
731 g_simple_async_result_set_op_res_gssize (result, count);
733 g_object_unref (result);
737 empathy_user_info_apply_finish (EmpathyUserInfo *self,
738 GAsyncResult *result,
741 empathy_implement_finish_void (self, empathy_user_info_apply_async);