1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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>
23 #include "empathy-contact-widget.h"
25 #include <glib/gi18n-lib.h>
27 #include "empathy-avatar-image.h"
28 #include "empathy-client-factory.h"
29 #include "empathy-groups-widget.h"
30 #include "empathy-string-parser.h"
31 #include "empathy-ui-utils.h"
32 #include "empathy-utils.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
35 #include "empathy-debug.h"
38 * SECTION:empathy-contact-widget
39 * @title:EmpathyContactWidget
40 * @short_description: A widget used to display and edit details about a contact
41 * @include: libempathy-empathy-contact-widget.h
43 * #EmpathyContactWidget is a widget which displays appropriate widgets
44 * with details about a contact, also allowing changing these details,
49 * EmpathyContactWidget:
50 * @parent: parent object
52 * Widget which displays appropriate widgets with details about a contact,
53 * also allowing changing these details, if desired.
56 G_DEFINE_TYPE (EmpathyContactWidget, empathy_contact_widget, GTK_TYPE_BOX)
58 /* Delay before updating the widget when the id entry changed (seconds) */
59 #define ID_CHANGED_TIMEOUT 1
61 #define DATA_FIELD "contact-info-field"
63 struct _EmpathyContactWidgetPriv
65 EmpathyContact *contact;
66 guint widget_id_timeout;
70 GtkWidget *widget_avatar;
71 GtkWidget *widget_account;
72 GtkWidget *image_account;
73 GtkWidget *label_account;
75 GtkWidget *widget_alias;
76 GtkWidget *label_alias;
77 GtkWidget *hbox_presence;
78 GtkWidget *image_state;
79 GtkWidget *label_status;
80 GtkWidget *grid_contact;
81 GtkWidget *vbox_avatar;
82 GtkWidget *favourite_checkbox;
83 GtkWidget *label_details;
84 GtkWidget *label_left_account;
87 GtkWidget *groups_widget;
90 GtkWidget *vbox_client;
91 GtkWidget *grid_client;
92 GtkWidget *hbox_client_requested;
97 EmpathyContactWidget *self;
100 GtkTreeIter found_iter;
113 contact_widget_client_update (EmpathyContactWidget *self)
115 /* FIXME: Needs new telepathy spec */
119 contact_widget_client_setup (EmpathyContactWidget *self)
121 /* FIXME: Needs new telepathy spec */
122 gtk_widget_hide (self->priv->vbox_client);
126 contact_widget_groups_update (EmpathyContactWidget *self)
128 if (self->priv->contact != NULL)
130 FolksPersona *persona =
131 empathy_contact_get_persona (self->priv->contact);
133 if (FOLKS_IS_GROUP_DETAILS (persona))
135 empathy_groups_widget_set_group_details (
136 EMPATHY_GROUPS_WIDGET (self->priv->groups_widget),
137 FOLKS_GROUP_DETAILS (persona));
138 gtk_widget_show (self->priv->groups_widget);
144 /* In case of failure */
145 gtk_widget_hide (self->priv->groups_widget);
149 save_avatar_menu_activate_cb (GtkWidget *widget,
150 EmpathyContactWidget *self)
153 EmpathyAvatar *avatar;
154 gchar *ext = NULL, *filename;
156 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
158 GTK_FILE_CHOOSER_ACTION_SAVE,
159 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
160 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
163 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
166 /* look for the avatar extension */
167 avatar = empathy_contact_get_avatar (self->priv->contact);
168 if (avatar->format != NULL)
172 splitted = g_strsplit (avatar->format, "/", 2);
173 if (splitted[0] != NULL && splitted[1] != NULL)
174 ext = g_strdup (splitted[1]);
176 g_strfreev (splitted);
180 /* Avatar was loaded from the cache so was converted to PNG */
181 ext = g_strdup ("png");
188 id = tp_escape_as_identifier (empathy_contact_get_id (
189 self->priv->contact));
191 filename = g_strdup_printf ("%s.%s", id, ext);
192 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
199 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
201 GError *error = NULL;
203 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
205 if (!empathy_avatar_save_to_file (avatar, filename, &error))
208 GtkWidget *error_dialog;
210 error_dialog = gtk_message_dialog_new (NULL, 0,
211 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
212 _("Unable to save avatar"));
214 gtk_message_dialog_format_secondary_text (
215 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
217 g_signal_connect (error_dialog, "response",
218 G_CALLBACK (gtk_widget_destroy), NULL);
220 gtk_window_present (GTK_WINDOW (error_dialog));
222 g_clear_error (&error);
228 gtk_widget_destroy (dialog);
232 popup_avatar_menu (EmpathyContactWidget *self,
234 GdkEventButton *event)
236 GtkWidget *menu, *item;
237 gint button, event_time;
239 if (self->priv->contact == NULL ||
240 empathy_contact_get_avatar (self->priv->contact) == NULL)
243 menu = empathy_context_menu_new (parent);
245 /* Add "Save as..." entry */
246 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
247 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
248 gtk_widget_show (item);
250 g_signal_connect (item, "activate",
251 G_CALLBACK (save_avatar_menu_activate_cb), self);
255 button = event->button;
256 event_time = event->time;
261 event_time = gtk_get_current_event_time ();
264 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
269 widget_avatar_popup_menu_cb (GtkWidget *widget,
270 EmpathyContactWidget *self)
272 popup_avatar_menu (self, widget, NULL);
278 widget_avatar_button_press_event_cb (GtkWidget *widget,
279 GdkEventButton *event,
280 EmpathyContactWidget *self)
282 /* Ignore double-clicks and triple-clicks */
283 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
285 popup_avatar_menu (self, widget, event);
293 set_nickname_cb (GObject *source,
297 GError *error = NULL;
299 if (!tp_account_set_nickname_finish (TP_ACCOUNT (source), res, &error))
301 DEBUG ("Failed to set Account.Nickname: %s", error->message);
302 g_error_free (error);
307 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
308 GdkEventFocus *event,
309 EmpathyContactWidget *self)
311 if (self->priv->contact)
315 alias = gtk_entry_get_text (GTK_ENTRY (editable));
317 if (empathy_contact_is_user (self->priv->contact))
320 const gchar *current_nickname;
322 account = empathy_contact_get_account (self->priv->contact);
323 current_nickname = tp_account_get_nickname (account);
325 if (tp_strdiff (current_nickname, alias))
327 DEBUG ("Set Account.Nickname to %s", alias);
329 tp_account_set_nickname_async (account, alias, set_nickname_cb,
335 empathy_contact_set_alias (self->priv->contact, alias);
343 contact_widget_name_notify_cb (EmpathyContactWidget *self)
345 if (GTK_IS_ENTRY (self->priv->widget_alias))
346 gtk_entry_set_text (GTK_ENTRY (self->priv->widget_alias),
347 empathy_contact_get_alias (self->priv->contact));
349 gtk_label_set_label (GTK_LABEL (self->priv->widget_alias),
350 empathy_contact_get_alias (self->priv->contact));
354 contact_widget_presence_notify_cb (EmpathyContactWidget *self)
357 gchar *markup_text = NULL;
359 status = empathy_contact_get_status (self->priv->contact);
361 markup_text = empathy_add_link_markup (status);
362 gtk_label_set_markup (GTK_LABEL (self->priv->label_status), markup_text);
363 g_free (markup_text);
365 gtk_image_set_from_icon_name (GTK_IMAGE (self->priv->image_state),
366 empathy_icon_name_for_contact (self->priv->contact),
367 GTK_ICON_SIZE_BUTTON);
368 gtk_widget_show (self->priv->image_state);
372 contact_widget_remove_contact (EmpathyContactWidget *self)
374 if (self->priv->contact)
376 g_signal_handlers_disconnect_by_func (self->priv->contact,
377 contact_widget_name_notify_cb, self);
378 g_signal_handlers_disconnect_by_func (self->priv->contact,
379 contact_widget_presence_notify_cb, self);
381 g_object_unref (self->priv->contact);
382 self->priv->contact = NULL;
386 static void contact_widget_change_contact (EmpathyContactWidget *self);
389 contact_widget_contact_update (EmpathyContactWidget *self)
391 TpAccount *account = NULL;
392 const gchar *id = NULL;
394 /* Connect and get info from new contact */
395 if (self->priv->contact)
397 g_signal_connect_swapped (self->priv->contact, "notify::name",
398 G_CALLBACK (contact_widget_name_notify_cb), self);
399 g_signal_connect_swapped (self->priv->contact, "notify::presence",
400 G_CALLBACK (contact_widget_presence_notify_cb), self);
401 g_signal_connect_swapped (self->priv->contact,
402 "notify::presence-message",
403 G_CALLBACK (contact_widget_presence_notify_cb), self);
405 account = empathy_contact_get_account (self->priv->contact);
406 id = empathy_contact_get_id (self->priv->contact);
409 /* Update account widget */
412 g_signal_handlers_block_by_func (self->priv->widget_account,
413 contact_widget_change_contact,
415 empathy_account_chooser_set_account (
416 EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account), account);
417 g_signal_handlers_unblock_by_func (self->priv->widget_account,
418 contact_widget_change_contact, self);
421 /* Update id widget */
422 gtk_entry_set_text (GTK_ENTRY (self->priv->widget_id), id ? id : "");
424 /* Update other widgets */
425 if (self->priv->contact)
427 contact_widget_name_notify_cb (self);
428 contact_widget_presence_notify_cb (self);
430 gtk_widget_show (self->priv->label_alias);
431 gtk_widget_show (self->priv->widget_alias);
432 gtk_widget_show (self->priv->widget_avatar);
434 gtk_widget_set_visible (self->priv->hbox_presence, TRUE);
438 gtk_widget_hide (self->priv->label_alias);
439 gtk_widget_hide (self->priv->widget_alias);
440 gtk_widget_hide (self->priv->hbox_presence);
441 gtk_widget_hide (self->priv->widget_avatar);
446 contact_widget_set_contact (EmpathyContactWidget *self,
447 EmpathyContact *contact)
449 if (contact == self->priv->contact)
452 contact_widget_remove_contact (self);
454 self->priv->contact = g_object_ref (contact);
456 /* set the selected account to be the account this contact came from */
457 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (self->priv->widget_account)) {
458 empathy_account_chooser_set_account (
459 EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account),
460 empathy_contact_get_account (contact));
463 /* Update self for widgets */
464 contact_widget_contact_update (self);
465 contact_widget_groups_update (self);
466 contact_widget_client_update (self);
470 contact_widget_got_contact_cb (GObject *source,
471 GAsyncResult *result,
474 EmpathyContactWidget *self = user_data;
475 GError *error = NULL;
476 EmpathyContact *contact;
478 contact = empathy_client_factory_dup_contact_by_id_finish (
479 EMPATHY_CLIENT_FACTORY (source), result, &error);
483 DEBUG ("Error: %s", error->message);
484 g_error_free (error);
488 contact_widget_set_contact (self, contact);
490 g_object_unref (contact);
492 g_object_unref (self);
496 contact_widget_change_contact (EmpathyContactWidget *self)
498 TpConnection *connection;
501 connection = empathy_account_chooser_get_connection (
502 EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account));
506 id = gtk_entry_get_text (GTK_ENTRY (self->priv->widget_id));
507 if (!EMP_STR_EMPTY (id))
509 EmpathyClientFactory *factory;
511 factory = empathy_client_factory_dup ();
513 empathy_client_factory_dup_contact_by_id_async (factory, connection,
514 id, contact_widget_got_contact_cb, g_object_ref (self));
516 g_object_unref (factory);
521 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
523 contact_widget_change_contact (self);
528 contact_widget_id_changed_cb (GtkEntry *entry,
529 EmpathyContactWidget *self)
531 if (self->priv->widget_id_timeout != 0)
533 g_source_remove (self->priv->widget_id_timeout);
536 self->priv->widget_id_timeout =
537 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
538 (GSourceFunc) contact_widget_id_activate_timeout, self);
542 contact_widget_id_focus_out_cb (GtkWidget *widget,
543 GdkEventFocus *event,
544 EmpathyContactWidget *self)
546 contact_widget_change_contact (self);
551 contact_widget_contact_setup (EmpathyContactWidget *self)
553 self->priv->label_status = gtk_label_new ("");
554 gtk_label_set_line_wrap_mode (GTK_LABEL (self->priv->label_status),
555 PANGO_WRAP_WORD_CHAR);
556 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label_status),
558 gtk_misc_set_alignment (GTK_MISC (self->priv->label_status), 0, 0.5);
560 gtk_label_set_selectable (GTK_LABEL (self->priv->label_status), TRUE);
562 gtk_box_pack_start (GTK_BOX (self->priv->hbox_presence),
563 self->priv->label_status, TRUE, TRUE, 0);
564 gtk_widget_show (self->priv->label_status);
566 /* Setup account chooser */
567 self->priv->widget_account = empathy_account_chooser_new ();
568 g_signal_connect_swapped (self->priv->widget_account, "changed",
569 G_CALLBACK (contact_widget_change_contact),
571 gtk_grid_attach (GTK_GRID (self->priv->grid_contact),
572 self->priv->widget_account,
574 gtk_widget_show (self->priv->widget_account);
576 /* Set up avatar display */
577 self->priv->widget_avatar = empathy_avatar_image_new ();
579 g_signal_connect (self->priv->widget_avatar, "popup-menu",
580 G_CALLBACK (widget_avatar_popup_menu_cb), self);
581 g_signal_connect (self->priv->widget_avatar, "button-press-event",
582 G_CALLBACK (widget_avatar_button_press_event_cb), self);
583 gtk_box_pack_start (GTK_BOX (self->priv->vbox_avatar),
584 self->priv->widget_avatar,
587 gtk_widget_show (self->priv->widget_avatar);
590 self->priv->widget_id = gtk_entry_new ();
591 g_signal_connect (self->priv->widget_id, "focus-out-event",
592 G_CALLBACK (contact_widget_id_focus_out_cb),
594 g_signal_connect (self->priv->widget_id, "changed",
595 G_CALLBACK (contact_widget_id_changed_cb),
597 gtk_grid_attach (GTK_GRID (self->priv->grid_contact), self->priv->widget_id,
599 gtk_widget_set_hexpand (self->priv->widget_id, TRUE);
601 gtk_widget_show (self->priv->widget_id);
603 /* Setup alias entry */
604 self->priv->widget_alias = gtk_entry_new ();
606 g_signal_connect (self->priv->widget_alias, "focus-out-event",
607 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
610 /* Make return activate the window default (the Close button) */
611 gtk_entry_set_activates_default (GTK_ENTRY (self->priv->widget_alias),
614 gtk_grid_attach (GTK_GRID (self->priv->grid_contact),
615 self->priv->widget_alias, 2, 2, 1, 1);
616 gtk_widget_set_hexpand (self->priv->widget_alias, TRUE);
618 gtk_label_set_selectable (GTK_LABEL (self->priv->label_status), FALSE);
619 gtk_widget_show (self->priv->widget_alias);
623 empathy_contact_widget_finalize (GObject *object)
625 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (object);
626 void (*chain_up) (GObject *) =
627 ((GObjectClass *) empathy_contact_widget_parent_class)->finalize;
629 contact_widget_remove_contact (self);
631 if (self->priv->widget_id_timeout != 0)
633 g_source_remove (self->priv->widget_id_timeout);
637 if (chain_up != NULL)
642 * empathy_contact_widget_new:
643 * @contact: an #EmpathyContact
645 * Creates a new #EmpathyContactWidget.
647 * Return value: a new #EmpathyContactWidget
650 empathy_contact_widget_new (EmpathyContact *contact)
652 EmpathyContactWidget *self;
654 GtkWidget *main_vbox;
657 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
659 self = g_object_new (EMPATHY_TYPE_CONTACT_WIDGET, NULL);
661 filename = empathy_file_lookup ("empathy-contact-widget.ui",
663 gui = empathy_builder_get_file (filename,
664 "vbox_contact_widget", &main_vbox,
665 "hbox_presence", &self->priv->hbox_presence,
666 "label_alias", &self->priv->label_alias,
667 "image_state", &self->priv->image_state,
668 "grid_contact", &self->priv->grid_contact,
669 "vbox_avatar", &self->priv->vbox_avatar,
670 "groups_widget", &self->priv->groups_widget,
671 "vbox_client", &self->priv->vbox_client,
672 "grid_client", &self->priv->grid_client,
673 "hbox_client_requested", &self->priv->hbox_client_requested,
674 "label_details", &self->priv->label_details,
675 "label_left_account", &self->priv->label_left_account,
679 gtk_container_add (GTK_CONTAINER (self), main_vbox);
680 gtk_widget_show (GTK_WIDGET (main_vbox));
683 contact_widget_contact_setup (self);
684 contact_widget_client_setup (self);
686 gtk_widget_hide (self->priv->label_details);
689 contact_widget_set_contact (self, contact);
691 contact_widget_change_contact (self);
693 g_object_unref (gui);
695 return GTK_WIDGET (self);
699 * empathy_contact_widget_get_contact:
700 * @widget: an #EmpathyContactWidget
702 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
704 * Returns: the #EmpathyContact associated with @widget
707 empathy_contact_widget_get_contact (GtkWidget *widget)
709 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
711 return self->priv->contact;
715 empathy_contact_widget_get_alias (GtkWidget *widget)
717 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
719 return gtk_entry_get_text (GTK_ENTRY (self->priv->widget_alias));
723 * empathy_contact_widget_set_contact:
724 * @widget: an #EmpathyContactWidget
725 * @contact: a different #EmpathyContact
727 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
730 empathy_contact_widget_set_contact (GtkWidget *widget,
731 EmpathyContact *contact)
733 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
735 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
737 contact_widget_set_contact (self, contact);
741 * empathy_contact_widget_set_account_filter:
742 * @widget: an #EmpathyContactWidget
743 * @filter: a #EmpathyAccountChooserFilterFunc
744 * @user_data: user data to pass to @filter, or %NULL
746 * Set a filter on the #EmpathyAccountChooser included in the
747 * #EmpathyContactWidget.
750 empathy_contact_widget_set_account_filter (
752 EmpathyAccountChooserFilterFunc filter,
755 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
756 EmpathyAccountChooser *chooser;
758 chooser = EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account);
760 empathy_account_chooser_set_filter (chooser, filter, user_data);
764 empathy_contact_widget_class_init (
765 EmpathyContactWidgetClass *klass)
767 GObjectClass *oclass = G_OBJECT_CLASS (klass);
769 oclass->finalize = empathy_contact_widget_finalize;
771 g_type_class_add_private (klass, sizeof (EmpathyContactWidgetPriv));
775 empathy_contact_widget_init (EmpathyContactWidget *self)
777 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
778 EMPATHY_TYPE_CONTACT_WIDGET, EmpathyContactWidgetPriv);