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>
28 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/account.h>
31 #include <telepathy-glib/util.h>
32 #include <telepathy-glib/interfaces.h>
34 #include <libempathy/empathy-time.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-client-factory.h>
38 #include "empathy-calendar-button.h"
39 #include "empathy-contact-widget.h"
40 #include "empathy-contactinfo-utils.h"
41 #include "empathy-account-chooser.h"
42 #include "empathy-avatar-chooser.h"
43 #include "empathy-avatar-image.h"
44 #include "empathy-groups-widget.h"
45 #include "empathy-ui-utils.h"
46 #include "empathy-string-parser.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
49 #include <libempathy/empathy-debug.h>
52 * SECTION:empathy-contact-widget
53 * @title:EmpathyContactWidget
54 * @short_description: A widget used to display and edit details about a contact
55 * @include: libempathy-empathy-contact-widget.h
57 * #EmpathyContactWidget is a widget which displays appropriate widgets
58 * with details about a contact, also allowing changing these details,
63 * EmpathyContactWidget:
64 * @parent: parent object
66 * Widget which displays appropriate widgets with details about a contact,
67 * also allowing changing these details, if desired.
70 G_DEFINE_TYPE (EmpathyContactWidget, empathy_contact_widget, GTK_TYPE_BOX)
72 /* Delay before updating the widget when the id entry changed (seconds) */
73 #define ID_CHANGED_TIMEOUT 1
75 #define DATA_FIELD "contact-info-field"
77 struct _EmpathyContactWidgetPriv
79 EmpathyContact *contact;
80 guint widget_id_timeout;
84 GtkWidget *widget_avatar;
85 GtkWidget *widget_account;
86 GtkWidget *image_account;
87 GtkWidget *label_account;
89 GtkWidget *widget_alias;
90 GtkWidget *label_alias;
91 GtkWidget *hbox_presence;
92 GtkWidget *image_state;
93 GtkWidget *label_status;
94 GtkWidget *grid_contact;
95 GtkWidget *vbox_avatar;
96 GtkWidget *favourite_checkbox;
97 GtkWidget *label_details;
98 GtkWidget *label_left_account;
101 GtkWidget *groups_widget;
104 GtkWidget *vbox_client;
105 GtkWidget *grid_client;
106 GtkWidget *hbox_client_requested;
111 EmpathyContactWidget *self;
114 GtkTreeIter found_iter;
127 contact_widget_client_update (EmpathyContactWidget *self)
129 /* FIXME: Needs new telepathy spec */
133 contact_widget_client_setup (EmpathyContactWidget *self)
135 /* FIXME: Needs new telepathy spec */
136 gtk_widget_hide (self->priv->vbox_client);
140 contact_widget_groups_update (EmpathyContactWidget *self)
142 if (self->priv->contact != NULL)
144 FolksPersona *persona =
145 empathy_contact_get_persona (self->priv->contact);
147 if (FOLKS_IS_GROUP_DETAILS (persona))
149 empathy_groups_widget_set_group_details (
150 EMPATHY_GROUPS_WIDGET (self->priv->groups_widget),
151 FOLKS_GROUP_DETAILS (persona));
152 gtk_widget_show (self->priv->groups_widget);
158 /* In case of failure */
159 gtk_widget_hide (self->priv->groups_widget);
163 save_avatar_menu_activate_cb (GtkWidget *widget,
164 EmpathyContactWidget *self)
167 EmpathyAvatar *avatar;
168 gchar *ext = NULL, *filename;
170 dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
172 GTK_FILE_CHOOSER_ACTION_SAVE,
173 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
174 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
177 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
180 /* look for the avatar extension */
181 avatar = empathy_contact_get_avatar (self->priv->contact);
182 if (avatar->format != NULL)
186 splitted = g_strsplit (avatar->format, "/", 2);
187 if (splitted[0] != NULL && splitted[1] != NULL)
188 ext = g_strdup (splitted[1]);
190 g_strfreev (splitted);
194 /* Avatar was loaded from the cache so was converted to PNG */
195 ext = g_strdup ("png");
202 id = tp_escape_as_identifier (empathy_contact_get_id (
203 self->priv->contact));
205 filename = g_strdup_printf ("%s.%s", id, ext);
206 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
213 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
215 GError *error = NULL;
217 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
219 if (!empathy_avatar_save_to_file (avatar, filename, &error))
222 GtkWidget *error_dialog;
224 error_dialog = gtk_message_dialog_new (NULL, 0,
225 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
226 _("Unable to save avatar"));
228 gtk_message_dialog_format_secondary_text (
229 GTK_MESSAGE_DIALOG (error_dialog), "%s", error->message);
231 g_signal_connect (error_dialog, "response",
232 G_CALLBACK (gtk_widget_destroy), NULL);
234 gtk_window_present (GTK_WINDOW (error_dialog));
236 g_clear_error (&error);
242 gtk_widget_destroy (dialog);
246 popup_avatar_menu (EmpathyContactWidget *self,
248 GdkEventButton *event)
250 GtkWidget *menu, *item;
251 gint button, event_time;
253 if (self->priv->contact == NULL ||
254 empathy_contact_get_avatar (self->priv->contact) == NULL)
257 menu = empathy_context_menu_new (parent);
259 /* Add "Save as..." entry */
260 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
261 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
262 gtk_widget_show (item);
264 g_signal_connect (item, "activate",
265 G_CALLBACK (save_avatar_menu_activate_cb), self);
269 button = event->button;
270 event_time = event->time;
275 event_time = gtk_get_current_event_time ();
278 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
283 widget_avatar_popup_menu_cb (GtkWidget *widget,
284 EmpathyContactWidget *self)
286 popup_avatar_menu (self, widget, NULL);
292 widget_avatar_button_press_event_cb (GtkWidget *widget,
293 GdkEventButton *event,
294 EmpathyContactWidget *self)
296 /* Ignore double-clicks and triple-clicks */
297 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
299 popup_avatar_menu (self, widget, event);
307 set_nickname_cb (GObject *source,
311 GError *error = NULL;
313 if (!tp_account_set_nickname_finish (TP_ACCOUNT (source), res, &error))
315 DEBUG ("Failed to set Account.Nickname: %s", error->message);
316 g_error_free (error);
321 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
322 GdkEventFocus *event,
323 EmpathyContactWidget *self)
325 if (self->priv->contact)
329 alias = gtk_entry_get_text (GTK_ENTRY (editable));
331 if (empathy_contact_is_user (self->priv->contact))
334 const gchar *current_nickname;
336 account = empathy_contact_get_account (self->priv->contact);
337 current_nickname = tp_account_get_nickname (account);
339 if (tp_strdiff (current_nickname, alias))
341 DEBUG ("Set Account.Nickname to %s", alias);
343 tp_account_set_nickname_async (account, alias, set_nickname_cb,
349 empathy_contact_set_alias (self->priv->contact, alias);
357 contact_widget_name_notify_cb (EmpathyContactWidget *self)
359 if (GTK_IS_ENTRY (self->priv->widget_alias))
360 gtk_entry_set_text (GTK_ENTRY (self->priv->widget_alias),
361 empathy_contact_get_alias (self->priv->contact));
363 gtk_label_set_label (GTK_LABEL (self->priv->widget_alias),
364 empathy_contact_get_alias (self->priv->contact));
368 contact_widget_presence_notify_cb (EmpathyContactWidget *self)
371 gchar *markup_text = NULL;
373 status = empathy_contact_get_status (self->priv->contact);
375 markup_text = empathy_add_link_markup (status);
376 gtk_label_set_markup (GTK_LABEL (self->priv->label_status), markup_text);
377 g_free (markup_text);
379 gtk_image_set_from_icon_name (GTK_IMAGE (self->priv->image_state),
380 empathy_icon_name_for_contact (self->priv->contact),
381 GTK_ICON_SIZE_BUTTON);
382 gtk_widget_show (self->priv->image_state);
386 contact_widget_remove_contact (EmpathyContactWidget *self)
388 if (self->priv->contact)
390 g_signal_handlers_disconnect_by_func (self->priv->contact,
391 contact_widget_name_notify_cb, self);
392 g_signal_handlers_disconnect_by_func (self->priv->contact,
393 contact_widget_presence_notify_cb, self);
395 g_object_unref (self->priv->contact);
396 self->priv->contact = NULL;
400 static void contact_widget_change_contact (EmpathyContactWidget *self);
403 contact_widget_contact_update (EmpathyContactWidget *self)
405 TpAccount *account = NULL;
406 const gchar *id = NULL;
408 /* Connect and get info from new contact */
409 if (self->priv->contact)
411 g_signal_connect_swapped (self->priv->contact, "notify::name",
412 G_CALLBACK (contact_widget_name_notify_cb), self);
413 g_signal_connect_swapped (self->priv->contact, "notify::presence",
414 G_CALLBACK (contact_widget_presence_notify_cb), self);
415 g_signal_connect_swapped (self->priv->contact,
416 "notify::presence-message",
417 G_CALLBACK (contact_widget_presence_notify_cb), self);
419 account = empathy_contact_get_account (self->priv->contact);
420 id = empathy_contact_get_id (self->priv->contact);
423 /* Update account widget */
426 g_signal_handlers_block_by_func (self->priv->widget_account,
427 contact_widget_change_contact,
429 empathy_account_chooser_set_account (
430 EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account), account);
431 g_signal_handlers_unblock_by_func (self->priv->widget_account,
432 contact_widget_change_contact, self);
435 /* Update id widget */
436 gtk_entry_set_text (GTK_ENTRY (self->priv->widget_id), id ? id : "");
438 /* Update other widgets */
439 if (self->priv->contact)
441 contact_widget_name_notify_cb (self);
442 contact_widget_presence_notify_cb (self);
444 gtk_widget_show (self->priv->label_alias);
445 gtk_widget_show (self->priv->widget_alias);
446 gtk_widget_show (self->priv->widget_avatar);
448 gtk_widget_set_visible (self->priv->hbox_presence, TRUE);
452 gtk_widget_hide (self->priv->label_alias);
453 gtk_widget_hide (self->priv->widget_alias);
454 gtk_widget_hide (self->priv->hbox_presence);
455 gtk_widget_hide (self->priv->widget_avatar);
460 contact_widget_set_contact (EmpathyContactWidget *self,
461 EmpathyContact *contact)
463 if (contact == self->priv->contact)
466 contact_widget_remove_contact (self);
468 self->priv->contact = g_object_ref (contact);
470 /* set the selected account to be the account this contact came from */
471 if (contact && EMPATHY_IS_ACCOUNT_CHOOSER (self->priv->widget_account)) {
472 empathy_account_chooser_set_account (
473 EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account),
474 empathy_contact_get_account (contact));
477 /* Update self for widgets */
478 contact_widget_contact_update (self);
479 contact_widget_groups_update (self);
480 contact_widget_client_update (self);
484 contact_widget_got_contact_cb (GObject *source,
485 GAsyncResult *result,
488 EmpathyContactWidget *self = user_data;
489 GError *error = NULL;
490 EmpathyContact *contact;
492 contact = empathy_client_factory_dup_contact_by_id_finish (
493 EMPATHY_CLIENT_FACTORY (source), result, &error);
497 DEBUG ("Error: %s", error->message);
498 g_error_free (error);
502 contact_widget_set_contact (self, contact);
504 g_object_unref (contact);
506 g_object_unref (self);
510 contact_widget_change_contact (EmpathyContactWidget *self)
512 TpConnection *connection;
515 connection = empathy_account_chooser_get_connection (
516 EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account));
520 id = gtk_entry_get_text (GTK_ENTRY (self->priv->widget_id));
521 if (!EMP_STR_EMPTY (id))
523 EmpathyClientFactory *factory;
525 factory = empathy_client_factory_dup ();
527 empathy_client_factory_dup_contact_by_id_async (factory, connection,
528 id, contact_widget_got_contact_cb, g_object_ref (self));
530 g_object_unref (factory);
535 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
537 contact_widget_change_contact (self);
542 contact_widget_id_changed_cb (GtkEntry *entry,
543 EmpathyContactWidget *self)
545 if (self->priv->widget_id_timeout != 0)
547 g_source_remove (self->priv->widget_id_timeout);
550 self->priv->widget_id_timeout =
551 g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
552 (GSourceFunc) contact_widget_id_activate_timeout, self);
556 contact_widget_id_focus_out_cb (GtkWidget *widget,
557 GdkEventFocus *event,
558 EmpathyContactWidget *self)
560 contact_widget_change_contact (self);
565 contact_widget_contact_setup (EmpathyContactWidget *self)
567 self->priv->label_status = gtk_label_new ("");
568 gtk_label_set_line_wrap_mode (GTK_LABEL (self->priv->label_status),
569 PANGO_WRAP_WORD_CHAR);
570 gtk_label_set_line_wrap (GTK_LABEL (self->priv->label_status),
572 gtk_misc_set_alignment (GTK_MISC (self->priv->label_status), 0, 0.5);
574 gtk_label_set_selectable (GTK_LABEL (self->priv->label_status), TRUE);
576 gtk_box_pack_start (GTK_BOX (self->priv->hbox_presence),
577 self->priv->label_status, TRUE, TRUE, 0);
578 gtk_widget_show (self->priv->label_status);
580 /* Setup account chooser */
581 self->priv->widget_account = empathy_account_chooser_new ();
582 g_signal_connect_swapped (self->priv->widget_account, "changed",
583 G_CALLBACK (contact_widget_change_contact),
585 gtk_grid_attach (GTK_GRID (self->priv->grid_contact),
586 self->priv->widget_account,
588 gtk_widget_show (self->priv->widget_account);
590 /* Set up avatar display */
591 self->priv->widget_avatar = empathy_avatar_image_new ();
593 g_signal_connect (self->priv->widget_avatar, "popup-menu",
594 G_CALLBACK (widget_avatar_popup_menu_cb), self);
595 g_signal_connect (self->priv->widget_avatar, "button-press-event",
596 G_CALLBACK (widget_avatar_button_press_event_cb), self);
597 gtk_box_pack_start (GTK_BOX (self->priv->vbox_avatar),
598 self->priv->widget_avatar,
601 gtk_widget_show (self->priv->widget_avatar);
604 self->priv->widget_id = gtk_entry_new ();
605 g_signal_connect (self->priv->widget_id, "focus-out-event",
606 G_CALLBACK (contact_widget_id_focus_out_cb),
608 g_signal_connect (self->priv->widget_id, "changed",
609 G_CALLBACK (contact_widget_id_changed_cb),
611 gtk_grid_attach (GTK_GRID (self->priv->grid_contact), self->priv->widget_id,
613 gtk_widget_set_hexpand (self->priv->widget_id, TRUE);
615 gtk_widget_show (self->priv->widget_id);
617 /* Setup alias entry */
618 self->priv->widget_alias = gtk_entry_new ();
620 g_signal_connect (self->priv->widget_alias, "focus-out-event",
621 G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
624 /* Make return activate the window default (the Close button) */
625 gtk_entry_set_activates_default (GTK_ENTRY (self->priv->widget_alias),
628 gtk_grid_attach (GTK_GRID (self->priv->grid_contact),
629 self->priv->widget_alias, 2, 2, 1, 1);
630 gtk_widget_set_hexpand (self->priv->widget_alias, TRUE);
632 gtk_label_set_selectable (GTK_LABEL (self->priv->label_status), FALSE);
633 gtk_widget_show (self->priv->widget_alias);
637 empathy_contact_widget_finalize (GObject *object)
639 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (object);
640 void (*chain_up) (GObject *) =
641 ((GObjectClass *) empathy_contact_widget_parent_class)->finalize;
643 contact_widget_remove_contact (self);
645 if (self->priv->widget_id_timeout != 0)
647 g_source_remove (self->priv->widget_id_timeout);
651 if (chain_up != NULL)
656 * empathy_contact_widget_new:
657 * @contact: an #EmpathyContact
659 * Creates a new #EmpathyContactWidget.
661 * Return value: a new #EmpathyContactWidget
664 empathy_contact_widget_new (EmpathyContact *contact)
666 EmpathyContactWidget *self;
668 GtkWidget *main_vbox;
671 g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
673 self = g_object_new (EMPATHY_TYPE_CONTACT_WIDGET, NULL);
675 filename = empathy_file_lookup ("empathy-contact-widget.ui",
677 gui = empathy_builder_get_file (filename,
678 "vbox_contact_widget", &main_vbox,
679 "hbox_presence", &self->priv->hbox_presence,
680 "label_alias", &self->priv->label_alias,
681 "image_state", &self->priv->image_state,
682 "grid_contact", &self->priv->grid_contact,
683 "vbox_avatar", &self->priv->vbox_avatar,
684 "groups_widget", &self->priv->groups_widget,
685 "vbox_client", &self->priv->vbox_client,
686 "grid_client", &self->priv->grid_client,
687 "hbox_client_requested", &self->priv->hbox_client_requested,
688 "label_details", &self->priv->label_details,
689 "label_left_account", &self->priv->label_left_account,
693 gtk_container_add (GTK_CONTAINER (self), main_vbox);
694 gtk_widget_show (GTK_WIDGET (main_vbox));
697 contact_widget_contact_setup (self);
698 contact_widget_client_setup (self);
700 gtk_widget_hide (self->priv->label_details);
703 contact_widget_set_contact (self, contact);
705 contact_widget_change_contact (self);
707 g_object_unref (gui);
709 return GTK_WIDGET (self);
713 * empathy_contact_widget_get_contact:
714 * @widget: an #EmpathyContactWidget
716 * Get the #EmpathyContact related with the #EmpathyContactWidget @widget.
718 * Returns: the #EmpathyContact associated with @widget
721 empathy_contact_widget_get_contact (GtkWidget *widget)
723 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
725 return self->priv->contact;
729 empathy_contact_widget_get_alias (GtkWidget *widget)
731 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
733 return gtk_entry_get_text (GTK_ENTRY (self->priv->widget_alias));
737 * empathy_contact_widget_set_contact:
738 * @widget: an #EmpathyContactWidget
739 * @contact: a different #EmpathyContact
741 * Change the #EmpathyContact related with the #EmpathyContactWidget @widget.
744 empathy_contact_widget_set_contact (GtkWidget *widget,
745 EmpathyContact *contact)
747 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
749 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
751 contact_widget_set_contact (self, contact);
755 * empathy_contact_widget_set_account_filter:
756 * @widget: an #EmpathyContactWidget
757 * @filter: a #EmpathyAccountChooserFilterFunc
758 * @user_data: user data to pass to @filter, or %NULL
760 * Set a filter on the #EmpathyAccountChooser included in the
761 * #EmpathyContactWidget.
764 empathy_contact_widget_set_account_filter (
766 EmpathyAccountChooserFilterFunc filter,
769 EmpathyContactWidget *self = EMPATHY_CONTACT_WIDGET (widget);
770 EmpathyAccountChooser *chooser;
772 chooser = EMPATHY_ACCOUNT_CHOOSER (self->priv->widget_account);
774 empathy_account_chooser_set_filter (chooser, filter, user_data);
778 empathy_contact_widget_class_init (
779 EmpathyContactWidgetClass *klass)
781 GObjectClass *oclass = G_OBJECT_CLASS (klass);
783 oclass->finalize = empathy_contact_widget_finalize;
785 g_type_class_add_private (klass, sizeof (EmpathyContactWidgetPriv));
789 empathy_contact_widget_init (EmpathyContactWidget *self)
791 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
792 EMPATHY_TYPE_CONTACT_WIDGET, EmpathyContactWidgetPriv);