]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Port EmpathyContactWidget to new API.
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <libmissioncontrol/mc-account.h>
31 #include <telepathy-glib/util.h>
32
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-contact-list.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include "empathy-contact-widget.h"
39 #include "empathy-account-chooser.h"
40 #include "empathy-avatar-chooser.h"
41 #include "empathy-avatar-image.h"
42 #include "empathy-ui-utils.h"
43
44 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
45 #include <libempathy/empathy-debug.h>
46
47 /* Delay before updating the widget when the id entry changed (seconds) */
48 #define ID_CHANGED_TIMEOUT 1
49
50 typedef struct
51 {
52   EmpathyTpContactFactory *factory;
53   EmpathyContactManager *manager;
54   EmpathyContact *contact;
55   EmpathyContactWidgetFlags flags;
56   guint widget_id_timeout;
57
58   GtkWidget *vbox_contact_widget;
59
60   /* Contact */
61   GtkWidget *vbox_contact;
62   GtkWidget *widget_avatar;
63   GtkWidget *widget_account;
64   GtkWidget *widget_id;
65   GtkWidget *widget_alias;
66   GtkWidget *label_alias;
67   GtkWidget *entry_alias;
68   GtkWidget *hbox_presence;
69   GtkWidget *image_state;
70   GtkWidget *label_status;
71   GtkWidget *table_contact;
72   GtkWidget *vbox_avatar;
73
74   /* Groups */
75   GtkWidget *vbox_groups;
76   GtkWidget *entry_group;
77   GtkWidget *button_group;
78   GtkWidget *treeview_groups;
79
80   /* Details */
81   GtkWidget *vbox_details;
82   GtkWidget *table_details;
83   GtkWidget *hbox_details_requested;
84
85   /* Client */
86   GtkWidget *vbox_client;
87   GtkWidget *table_client;
88   GtkWidget *hbox_client_requested;
89 } EmpathyContactWidget;
90
91 typedef struct
92 {
93   EmpathyContactWidget *information;
94   const gchar *name;
95   gboolean found;
96   GtkTreeIter found_iter;
97 } FindName;
98
99 static void contact_widget_destroy_cb (GtkWidget *widget,
100     EmpathyContactWidget *information);
101 static void contact_widget_remove_contact (EmpathyContactWidget *information);
102 static void contact_widget_set_contact (EmpathyContactWidget *information,
103     EmpathyContact *contact);
104 static void contact_widget_contact_setup (EmpathyContactWidget *information);
105 static void contact_widget_contact_update (EmpathyContactWidget *information);
106 static void contact_widget_change_contact (EmpathyContactWidget *information);
107 static void contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
108     EmpathyContactWidget *information);
109 static void contact_widget_account_changed_cb (GtkComboBox *widget,
110     EmpathyContactWidget *information);
111 static gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
112     GdkEventFocus *event, EmpathyContactWidget *information);
113 static gboolean contact_widget_entry_alias_focus_event_cb (
114     GtkEditable *editable, GdkEventFocus *event,
115     EmpathyContactWidget *information);
116 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
117 static void contact_widget_presence_notify_cb (
118     EmpathyContactWidget *information);
119 static void contact_widget_avatar_notify_cb (
120     EmpathyContactWidget *information);
121 static void contact_widget_groups_setup (
122     EmpathyContactWidget *information);
123 static void contact_widget_groups_update (EmpathyContactWidget *information);
124 static void contact_widget_model_setup (EmpathyContactWidget *information);
125 static void contact_widget_model_populate_columns (
126     EmpathyContactWidget *information);
127 static void contact_widget_groups_populate_data (
128     EmpathyContactWidget *information);
129 static void contact_widget_groups_notify_cb (
130     EmpathyContactWidget *information);
131 static gboolean contact_widget_model_find_name (
132     EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
133 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
134     GtkTreePath *path, GtkTreeIter *iter, FindName *data);
135 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
136     gchar *path_string, EmpathyContactWidget *information);
137 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
138     EmpathyContactWidget *information);
139 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
140     EmpathyContactWidget *information);
141 static void contact_widget_button_group_clicked_cb (GtkButton *button,
142     EmpathyContactWidget *information);
143 static void contact_widget_details_setup (EmpathyContactWidget *information);
144 static void contact_widget_details_update (EmpathyContactWidget *information);
145 static void contact_widget_client_setup (EmpathyContactWidget *information);
146 static void contact_widget_client_update (EmpathyContactWidget *information);
147
148 enum
149 {
150   COL_NAME,
151   COL_ENABLED,
152   COL_EDITABLE,
153   COL_COUNT
154 };
155
156 GtkWidget *
157 empathy_contact_widget_new (EmpathyContact *contact,
158                             EmpathyContactWidgetFlags flags)
159 {
160   EmpathyContactWidget *information;
161   GtkBuilder *gui;
162   TpConnection *connection;
163   gchar *filename;
164
165   connection = empathy_contact_get_connection (contact);
166   information = g_slice_new0 (EmpathyContactWidget);
167   information->flags = flags;
168
169   filename = empathy_file_lookup ("empathy-contact-widget.ui",
170       "libempathy-gtk");
171   gui = empathy_builder_get_file (filename,
172        "vbox_contact_widget", &information->vbox_contact_widget,
173        "vbox_contact", &information->vbox_contact,
174        "hbox_presence", &information->hbox_presence,
175        "label_alias", &information->label_alias,
176        "image_state", &information->image_state,
177        "label_status", &information->label_status,
178        "table_contact", &information->table_contact,
179        "vbox_avatar", &information->vbox_avatar,
180        "vbox_groups", &information->vbox_groups,
181        "entry_group", &information->entry_group,
182        "button_group", &information->button_group,
183        "treeview_groups", &information->treeview_groups,
184        "vbox_details", &information->vbox_details,
185        "table_details", &information->table_details,
186        "hbox_details_requested", &information->hbox_details_requested,
187        "vbox_client", &information->vbox_client,
188        "table_client", &information->table_client,
189        "hbox_client_requested", &information->hbox_client_requested,
190        NULL);
191   g_free (filename);
192
193   empathy_builder_connect (gui, information,
194       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
195       "entry_group", "changed", contact_widget_entry_group_changed_cb,
196       "entry_group", "activate", contact_widget_entry_group_activate_cb,
197       "button_group", "clicked", contact_widget_button_group_clicked_cb,
198       NULL);
199
200   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
201       "EmpathyContactWidget",
202       information);
203
204   /* Create widgets */
205   contact_widget_contact_setup (information);
206   contact_widget_groups_setup (information);
207   contact_widget_details_setup (information);
208   contact_widget_client_setup (information);
209
210   contact_widget_set_contact (information, contact);
211
212   return empathy_builder_unref_and_keep_widget (gui,
213     information->vbox_contact_widget);
214 }
215
216 EmpathyContact *
217 empathy_contact_widget_get_contact (GtkWidget *widget)
218 {
219   EmpathyContactWidget *information;
220
221   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
222
223   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
224   if (!information)
225       return NULL;
226
227   return information->contact;
228 }
229
230 void
231 empathy_contact_widget_set_contact (GtkWidget *widget,
232                                     EmpathyContact *contact)
233 {
234   EmpathyContactWidget *information;
235
236   g_return_if_fail (GTK_IS_WIDGET (widget));
237   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
238
239   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
240   if (!information)
241     return;
242
243   contact_widget_set_contact (information, contact);
244 }
245
246 void
247 empathy_contact_widget_set_account_filter (
248     GtkWidget *widget,
249     EmpathyAccountChooserFilterFunc filter,
250     gpointer user_data)
251 {
252   EmpathyContactWidget *information;
253   EmpathyAccountChooser *chooser;
254
255   g_return_if_fail (GTK_IS_WIDGET (widget));
256
257   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
258   if (!information)
259     return;
260
261   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
262   if (chooser)
263       empathy_account_chooser_set_filter (chooser, filter, user_data);
264 }
265   
266 static void
267 contact_widget_destroy_cb (GtkWidget *widget,
268                            EmpathyContactWidget *information)
269 {
270   contact_widget_remove_contact (information);
271
272   if (information->widget_id_timeout != 0)
273     {
274       g_source_remove (information->widget_id_timeout);
275     }
276   if (information->manager)
277     {
278       g_object_unref (information->manager);
279     }   
280
281   g_slice_free (EmpathyContactWidget, information);
282 }
283
284 static void
285 contact_widget_remove_contact (EmpathyContactWidget *information)
286 {
287   if (information->contact)
288     {
289       g_signal_handlers_disconnect_by_func (information->contact,
290           contact_widget_name_notify_cb, information);
291       g_signal_handlers_disconnect_by_func (information->contact,
292           contact_widget_presence_notify_cb, information);
293       g_signal_handlers_disconnect_by_func (information->contact,
294           contact_widget_avatar_notify_cb, information);
295       g_signal_handlers_disconnect_by_func (information->contact,
296           contact_widget_groups_notify_cb, information);
297
298       g_object_unref (information->contact);
299       g_object_unref (information->factory);
300       information->contact = NULL;
301       information->factory = NULL;
302     }
303 }
304
305 static void
306 contact_widget_set_contact (EmpathyContactWidget *information,
307                             EmpathyContact *contact)
308 {
309   if (contact == information->contact)
310     return;
311
312   contact_widget_remove_contact (information);
313   if (contact)
314     {
315       TpConnection *connection;
316
317       connection = empathy_contact_get_connection (contact);
318       information->contact = g_object_ref (contact);
319       information->factory = empathy_tp_contact_factory_dup_singleton (connection);
320     }
321
322   /* Update information for widgets */
323   contact_widget_contact_update (information);
324   contact_widget_groups_update (information);
325   contact_widget_details_update (information);
326   contact_widget_client_update (information);
327 }
328
329 static gboolean
330 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
331 {
332   contact_widget_change_contact (self);
333   return FALSE;
334 }
335
336 static void
337 contact_widget_id_changed_cb (GtkEntry *entry,
338                               EmpathyContactWidget *self)
339 {
340   if (self->widget_id_timeout != 0)
341     {   
342       g_source_remove (self->widget_id_timeout);
343     }
344
345   self->widget_id_timeout =
346     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
347         (GSourceFunc) contact_widget_id_activate_timeout, self);
348 }
349
350 static void
351 save_avatar_menu_activate_cb (GtkWidget *widget,
352                               EmpathyContactWidget *information)
353 {
354   GtkWidget *dialog;
355   EmpathyAvatar *avatar;
356   gchar *ext = NULL, *filename;
357
358   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
359       NULL,
360       GTK_FILE_CHOOSER_ACTION_SAVE,
361       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
362       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
363       NULL);
364
365   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
366       TRUE);
367
368   /* look for the avatar extension */
369   avatar = empathy_contact_get_avatar (information->contact);
370   if (avatar->format != NULL)
371     {
372       gchar **splitted;
373
374       splitted = g_strsplit (avatar->format, "/", 2);
375       if (splitted[0] != NULL && splitted[1] != NULL)
376           ext = g_strdup (splitted[1]);
377
378       g_strfreev (splitted);
379     }
380   else
381     {
382       /* Avatar was loaded from the cache so was converted to PNG */
383       ext = g_strdup ("png");
384     }
385
386   if (ext != NULL)
387     {
388       gchar *id;
389
390       id = tp_escape_as_identifier (empathy_contact_get_id (
391             information->contact));
392
393       filename = g_strdup_printf ("%s.%s", id, ext);
394       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
395
396       g_free (id);
397       g_free (ext);
398       g_free (filename);
399     }
400
401   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
402     {
403       GError *error = NULL;
404
405       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
406
407       if (!empathy_avatar_save_to_file (avatar, filename, &error))
408         {
409           /* Save error */
410           GtkWidget *dialog;
411
412           dialog = gtk_message_dialog_new (NULL, 0,
413               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 
414               _("Unable to save avatar"));
415
416           gtk_message_dialog_format_secondary_text (
417               GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
418
419           g_signal_connect (dialog, "response",
420               G_CALLBACK (gtk_widget_destroy), NULL);
421
422           gtk_window_present (GTK_WINDOW (dialog));
423
424           g_clear_error (&error);
425         }
426
427       g_free (filename);
428     }
429
430   gtk_widget_destroy (dialog);
431 }
432
433 static void
434 popup_avatar_menu (EmpathyContactWidget *information,
435                    GtkWidget *parent,
436                    GdkEventButton *event)
437 {
438   GtkWidget *menu, *item;
439   gint button, event_time;
440
441   if (information->contact == NULL ||
442       empathy_contact_get_avatar (information->contact) == NULL)
443       return;
444
445   menu = gtk_menu_new ();
446
447   /* Add "Save as..." entry */
448   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
449   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
450   gtk_widget_show (item);
451
452   g_signal_connect (item, "activate",
453       G_CALLBACK (save_avatar_menu_activate_cb), information);
454
455   if (event)
456     {
457       button = event->button;
458       event_time = event->time;
459     }
460   else
461     {
462       button = 0;
463       event_time = gtk_get_current_event_time ();
464     }
465
466   gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
467   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
468       button, event_time);
469 }
470
471 static gboolean
472 widget_avatar_popup_menu_cb (GtkWidget *widget,
473                              EmpathyContactWidget *information)
474 {
475   popup_avatar_menu (information, widget, NULL);
476
477   return TRUE;
478 }
479
480 static gboolean
481 widget_avatar_button_press_event_cb (GtkWidget *widget,
482                                      GdkEventButton *event,
483                                      EmpathyContactWidget *information)
484 {
485   /* Ignore double-clicks and triple-clicks */
486   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
487     {
488       popup_avatar_menu (information, widget, event);
489       return TRUE;
490     }
491
492   return FALSE;
493 }
494
495 static void
496 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
497                                   EmpathyAvatarChooser *avatar_chooser)
498 {
499   TpConnection *connection;
500
501   connection = empathy_account_chooser_get_connection (account_chooser);
502   g_object_set (avatar_chooser, "connection", connection, NULL);
503 }
504
505 static void
506 contact_widget_contact_setup (EmpathyContactWidget *information)
507 {
508   /* Setup account label/chooser */
509   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
510     {
511       information->widget_account = empathy_account_chooser_new ();
512
513       g_signal_connect (information->widget_account, "changed",
514             G_CALLBACK (contact_widget_account_changed_cb),
515             information);
516     }
517   else
518     {
519       information->widget_account = gtk_label_new (NULL);
520       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
521         gtk_label_set_selectable (GTK_LABEL (information->widget_account), TRUE);
522       }
523       gtk_misc_set_alignment (GTK_MISC (information->widget_account), 0, 0.5);
524     }
525   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
526            information->widget_account,
527            1, 2, 0, 1);
528   gtk_widget_show (information->widget_account);
529
530   /* Set up avatar chooser/display */
531   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
532     {
533       information->widget_avatar = empathy_avatar_chooser_new ();
534       g_signal_connect (information->widget_avatar, "changed",
535             G_CALLBACK (contact_widget_avatar_changed_cb),
536             information);
537       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
538         {
539           g_signal_connect (information->widget_account, "changed",
540               G_CALLBACK (update_avatar_chooser_account_cb),
541               information->widget_avatar);
542           update_avatar_chooser_account_cb (
543               EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
544               EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
545         }
546     }
547   else
548     {
549       information->widget_avatar = empathy_avatar_image_new ();
550
551       g_signal_connect (information->widget_avatar, "popup-menu",
552           G_CALLBACK (widget_avatar_popup_menu_cb), information);
553       g_signal_connect (information->widget_avatar, "button-press-event",
554           G_CALLBACK (widget_avatar_button_press_event_cb), information);
555     }
556
557   gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
558           information->widget_avatar,
559           FALSE, FALSE,
560           6);
561   gtk_widget_show (information->widget_avatar);
562
563   /* Setup id label/entry */
564   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
565     {
566       information->widget_id = gtk_entry_new ();
567       g_signal_connect (information->widget_id, "focus-out-event",
568             G_CALLBACK (contact_widget_id_focus_out_cb),
569             information);
570       g_signal_connect (information->widget_id, "changed",
571             G_CALLBACK (contact_widget_id_changed_cb),
572             information);
573     }
574   else
575     {
576       information->widget_id = gtk_label_new (NULL);
577       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
578         gtk_label_set_selectable (GTK_LABEL (information->widget_id), TRUE);
579       }
580       gtk_misc_set_alignment (GTK_MISC (information->widget_id), 0, 0.5);
581     }
582   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
583            information->widget_id,
584            1, 2, 1, 2);
585   gtk_widget_show (information->widget_id);
586
587   /* Setup alias label/entry */
588   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS)
589     {
590       information->widget_alias = gtk_entry_new ();
591       g_signal_connect (information->widget_alias, "focus-out-event",
592             G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
593             information);
594       /* Make return activate the window default (the Close button) */
595       gtk_entry_set_activates_default (GTK_ENTRY (information->widget_alias),
596           TRUE);
597     }
598   else
599     {
600       information->widget_alias = gtk_label_new (NULL);
601       if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP)) {
602         gtk_label_set_selectable (GTK_LABEL (information->widget_alias), TRUE);
603       }
604       gtk_misc_set_alignment (GTK_MISC (information->widget_alias), 0, 0.5);
605     }
606   gtk_table_attach_defaults (GTK_TABLE (information->table_contact),
607            information->widget_alias,
608            1, 2, 2, 3);
609   if (information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP) {
610     gtk_label_set_selectable (GTK_LABEL (information->label_status), FALSE);
611   }
612   gtk_widget_show (information->widget_alias);
613 }
614
615 static void
616 contact_widget_contact_update (EmpathyContactWidget *information)
617 {
618   McAccount *account = NULL;
619   const gchar *id = NULL;
620
621   /* Connect and get info from new contact */
622   if (information->contact)
623     {
624       g_signal_connect_swapped (information->contact, "notify::name",
625           G_CALLBACK (contact_widget_name_notify_cb), information);
626       g_signal_connect_swapped (information->contact, "notify::presence",
627           G_CALLBACK (contact_widget_presence_notify_cb), information);
628       g_signal_connect_swapped (information->contact,
629           "notify::presence-message",
630           G_CALLBACK (contact_widget_presence_notify_cb), information);
631       g_signal_connect_swapped (information->contact, "notify::avatar",
632           G_CALLBACK (contact_widget_avatar_notify_cb), information);
633
634       account = empathy_contact_get_account (information->contact);
635       id = empathy_contact_get_id (information->contact);
636     }
637
638   /* Update account widget */
639   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
640     {
641       if (account)
642         {
643           g_signal_handlers_block_by_func (information->widget_account,
644                    contact_widget_account_changed_cb,
645                    information);
646           empathy_account_chooser_set_account (
647               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
648           g_signal_handlers_unblock_by_func (information->widget_account,
649               contact_widget_account_changed_cb, information);
650         }
651     }
652   else
653     {
654       if (account)
655         {
656           const gchar *name;
657
658           name = mc_account_get_display_name (account);
659           gtk_label_set_label (GTK_LABEL (information->widget_account), name);
660         }
661     }
662
663   /* Update id widget */
664   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
665       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
666   else
667       gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
668
669   /* Update other widgets */
670   if (information->contact)
671     {
672       contact_widget_name_notify_cb (information);
673       contact_widget_presence_notify_cb (information);
674       contact_widget_avatar_notify_cb (information);
675
676       gtk_widget_show (information->label_alias);
677       gtk_widget_show (information->widget_alias);
678       gtk_widget_show (information->hbox_presence);
679       gtk_widget_show (information->widget_avatar);
680     }
681   else
682     {
683       gtk_widget_hide (information->label_alias);
684       gtk_widget_hide (information->widget_alias);
685       gtk_widget_hide (information->hbox_presence);
686       gtk_widget_hide (information->widget_avatar);
687     }
688 }
689
690 static void
691 contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
692                                GList *contacts,
693                                gpointer user_data,
694                                GObject *weak_object)
695 {
696   EmpathyContactWidget *information = user_data;
697
698   contact_widget_set_contact (information, contacts->data);
699 }
700
701 static void
702 contact_widget_get_self_handle_cb (TpConnection *connection,
703                                    TpHandle self_handle,
704                                    const GError *error,
705                                    gpointer information,
706                                    GObject *weak_object)
707 {
708   EmpathyTpContactFactory *factory;
709
710   if (error != NULL)
711     {
712       DEBUG ("Error: %s", error->message);
713       return;
714     }
715
716   factory = empathy_tp_contact_factory_dup_singleton (connection);
717   empathy_tp_contact_factory_get_from_handles (factory, 1, &self_handle,
718       contact_widget_got_contact_cb, information, NULL,
719       weak_object);
720   g_object_unref (factory);
721 }
722
723 static void
724 contact_widget_change_contact (EmpathyContactWidget *information)
725 {
726   TpConnection *connection;
727
728   connection = empathy_account_chooser_get_connection (
729       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
730   if (!connection)
731       return;
732
733   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
734     {
735       const gchar *id;
736
737       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
738       if (!EMP_STR_EMPTY (id))
739         {
740           EmpathyTpContactFactory *factory;
741
742           factory = empathy_tp_contact_factory_dup_singleton (connection);
743           empathy_tp_contact_factory_get_from_ids (factory, 1, &id,
744               contact_widget_got_contact_cb, information, NULL,
745               G_OBJECT (information->vbox_contact_widget));
746           g_object_unref (factory);
747         }
748     }
749   else
750     {
751       /* FIXME: TpConnection should have a SelfHandle property */
752       tp_cli_connection_call_get_self_handle (connection, -1,
753           contact_widget_get_self_handle_cb, information, NULL,
754           G_OBJECT (information->vbox_contact_widget));
755     }
756 }
757
758 static void
759 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
760                                   EmpathyContactWidget *information)
761 {
762   if (information->contact && empathy_contact_is_user (information->contact))
763     {
764       McAccount *account;
765       const gchar *data;
766       gsize size;
767       const gchar *mime_type;
768
769       account = empathy_contact_get_account (information->contact);
770       empathy_avatar_chooser_get_image_data (
771           EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
772           &data, &size, &mime_type);
773       empathy_tp_contact_factory_set_avatar (information->factory,
774           data, size, mime_type);
775     }
776 }
777
778 static void
779 contact_widget_account_changed_cb (GtkComboBox *widget,
780                                    EmpathyContactWidget *information)
781 {
782   contact_widget_change_contact (information);
783 }
784
785 static gboolean
786 contact_widget_id_focus_out_cb (GtkWidget *widget,
787                                 GdkEventFocus *event,
788                                 EmpathyContactWidget *information)
789 {
790   contact_widget_change_contact (information);
791   return FALSE;
792 }
793
794 static gboolean
795 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
796                                            GdkEventFocus *event,
797                                            EmpathyContactWidget *information)
798 {
799   if (information->contact)
800     {
801       const gchar *alias;
802
803       alias = gtk_entry_get_text (GTK_ENTRY (editable));
804       empathy_tp_contact_factory_set_alias (information->factory,
805           information->contact, alias);
806     }
807
808   return FALSE;
809 }
810
811 static void
812 contact_widget_name_notify_cb (EmpathyContactWidget *information)
813 {
814   if (GTK_IS_ENTRY (information->widget_alias))
815       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
816           empathy_contact_get_name (information->contact));
817   else
818       gtk_label_set_label (GTK_LABEL (information->widget_alias),
819           empathy_contact_get_name (information->contact));
820 }
821
822 static void
823 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
824 {
825   gtk_label_set_text (GTK_LABEL (information->label_status),
826       empathy_contact_get_status (information->contact));
827   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
828       empathy_icon_name_for_contact (information->contact),
829       GTK_ICON_SIZE_BUTTON);
830 }
831
832 static void
833 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
834 {
835   EmpathyAvatar *avatar = NULL;
836
837   if (information->contact)
838       avatar = empathy_contact_get_avatar (information->contact);
839
840   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
841     {
842       g_signal_handlers_block_by_func (information->widget_avatar,
843           contact_widget_avatar_changed_cb,
844           information);
845       empathy_avatar_chooser_set (
846           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
847       g_signal_handlers_unblock_by_func (information->widget_avatar,
848           contact_widget_avatar_changed_cb, information);
849     }
850   else
851       empathy_avatar_image_set (
852           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
853 }
854
855 static void
856 contact_widget_groups_setup (EmpathyContactWidget *information)
857 {
858   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
859     {
860       information->manager = empathy_contact_manager_dup_singleton ();
861       contact_widget_model_setup (information);
862     }
863 }
864
865 static void
866 contact_widget_groups_update (EmpathyContactWidget *information)
867 {
868   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
869       information->contact)
870     {
871       g_signal_connect_swapped (information->contact, "notify::groups",
872           G_CALLBACK (contact_widget_groups_notify_cb), information);
873       contact_widget_groups_populate_data (information);
874
875       gtk_widget_show (information->vbox_groups);
876     }
877   else
878       gtk_widget_hide (information->vbox_groups);
879 }
880
881 static void
882 contact_widget_model_setup (EmpathyContactWidget *information)
883 {
884   GtkTreeView *view;
885   GtkListStore *store;
886   GtkTreeSelection *selection;
887
888   view = GTK_TREE_VIEW (information->treeview_groups);
889
890   store = gtk_list_store_new (COL_COUNT,
891       G_TYPE_STRING,   /* name */
892       G_TYPE_BOOLEAN,  /* enabled */
893       G_TYPE_BOOLEAN); /* editable */
894
895   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
896
897   selection = gtk_tree_view_get_selection (view);
898   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
899
900   contact_widget_model_populate_columns (information);
901
902   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
903       COL_NAME, GTK_SORT_ASCENDING);
904
905   g_object_unref (store);
906 }
907
908 static void
909 contact_widget_model_populate_columns (EmpathyContactWidget *information)
910 {
911   GtkTreeView *view;
912   GtkTreeModel *model;
913   GtkTreeViewColumn *column;
914   GtkCellRenderer  *renderer;
915   guint col_offset;
916
917   view = GTK_TREE_VIEW (information->treeview_groups);
918   model = gtk_tree_view_get_model (view);
919
920   renderer = gtk_cell_renderer_toggle_new ();
921   g_signal_connect (renderer, "toggled",
922       G_CALLBACK (contact_widget_cell_toggled), information);
923
924   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
925       "active", COL_ENABLED, NULL);
926
927   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
928   gtk_tree_view_column_set_fixed_width (column, 50);
929   gtk_tree_view_append_column (view, column);
930
931   renderer = gtk_cell_renderer_text_new ();
932   col_offset = gtk_tree_view_insert_column_with_attributes (view,
933       -1, _("Group"),
934       renderer,
935       "text", COL_NAME,
936       /* "editable", COL_EDITABLE, */
937       NULL);
938
939   g_object_set_data (G_OBJECT (renderer),
940       "column", GINT_TO_POINTER (COL_NAME));
941
942   column = gtk_tree_view_get_column (view, col_offset - 1);
943   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
944   gtk_tree_view_column_set_resizable (column,FALSE);
945   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
946 }
947
948 static void
949 contact_widget_groups_populate_data (EmpathyContactWidget *information)
950 {
951   GtkTreeView *view;
952   GtkListStore *store;
953   GtkTreeIter iter;
954   GList *my_groups, *l;
955   GList *all_groups;
956
957   view = GTK_TREE_VIEW (information->treeview_groups);
958   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
959   gtk_list_store_clear (store);
960
961   all_groups = empathy_contact_list_get_all_groups (
962       EMPATHY_CONTACT_LIST (information->manager));
963   my_groups = empathy_contact_list_get_groups (
964       EMPATHY_CONTACT_LIST (information->manager),
965       information->contact);
966
967   for (l = all_groups; l; l = l->next)
968     {
969       const gchar *group_str;
970       gboolean enabled;
971
972       group_str = l->data;
973
974       enabled = g_list_find_custom (my_groups,
975           group_str, (GCompareFunc) strcmp) != NULL;
976
977       gtk_list_store_append (store, &iter);
978       gtk_list_store_set (store, &iter,
979           COL_NAME, group_str,
980           COL_EDITABLE, TRUE,
981           COL_ENABLED, enabled,
982           -1);
983     }
984
985   g_list_foreach (all_groups, (GFunc) g_free, NULL);
986   g_list_foreach (my_groups, (GFunc) g_free, NULL);
987   g_list_free (all_groups);
988   g_list_free (my_groups);
989 }
990
991 static void
992 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
993 {
994   /* FIXME: not implemented */
995 }
996
997 static gboolean
998 contact_widget_model_find_name (EmpathyContactWidget *information,
999                                 const gchar *name,
1000                                 GtkTreeIter *iter)
1001 {
1002   GtkTreeView *view;
1003   GtkTreeModel *model;
1004   FindName data;
1005
1006   if (EMP_STR_EMPTY (name))
1007       return FALSE;
1008
1009   data.information = information;
1010   data.name = name;
1011   data.found = FALSE;
1012
1013   view = GTK_TREE_VIEW (information->treeview_groups);
1014   model = gtk_tree_view_get_model (view);
1015
1016   gtk_tree_model_foreach (model,
1017       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
1018       &data);
1019
1020   if (data.found == TRUE)
1021     {
1022       *iter = data.found_iter;
1023       return TRUE;
1024     }
1025
1026   return FALSE;
1027 }
1028
1029 static gboolean
1030 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1031                                         GtkTreePath *path,
1032                                         GtkTreeIter *iter,
1033                                         FindName *data)
1034 {
1035   gchar *name;
1036
1037   gtk_tree_model_get (model, iter,
1038       COL_NAME, &name,
1039       -1);
1040
1041   if (!name)
1042       return FALSE;
1043
1044   if (data->name && strcmp (data->name, name) == 0)
1045     {
1046       data->found = TRUE;
1047       data->found_iter = *iter;
1048
1049       g_free (name);
1050
1051       return TRUE;
1052     }
1053
1054   g_free (name);
1055
1056   return FALSE;
1057 }
1058
1059 static void
1060 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1061                              gchar *path_string,
1062                              EmpathyContactWidget *information)
1063 {
1064   GtkTreeView *view;
1065   GtkTreeModel *model;
1066   GtkListStore *store;
1067   GtkTreePath *path;
1068   GtkTreeIter iter;
1069   gboolean enabled;
1070   gchar *group;
1071
1072   view = GTK_TREE_VIEW (information->treeview_groups);
1073   model = gtk_tree_view_get_model (view);
1074   store = GTK_LIST_STORE (model);
1075
1076   path = gtk_tree_path_new_from_string (path_string);
1077
1078   gtk_tree_model_get_iter (model, &iter, path);
1079   gtk_tree_model_get (model, &iter,
1080       COL_ENABLED, &enabled,
1081       COL_NAME, &group,
1082       -1);
1083
1084   gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1085   gtk_tree_path_free (path);
1086
1087   if (group)
1088     {
1089       if (enabled)
1090         {
1091           empathy_contact_list_remove_from_group (
1092               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1093               group);
1094         }
1095       else
1096         {
1097           empathy_contact_list_add_to_group (
1098               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1099               group);
1100         }
1101       g_free (group);
1102     }
1103 }
1104
1105 static void
1106 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1107                                        EmpathyContactWidget *information)
1108 {
1109   GtkTreeIter iter;
1110   const gchar *group;
1111
1112   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1113
1114   if (contact_widget_model_find_name (information, group, &iter))
1115       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1116   else
1117       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1118           !EMP_STR_EMPTY (group));
1119 }
1120
1121 static void
1122 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1123                                         EmpathyContactWidget  *information)
1124 {
1125   gtk_widget_activate (GTK_WIDGET (information->button_group));
1126 }
1127
1128 static void
1129 contact_widget_button_group_clicked_cb (GtkButton *button,
1130                                         EmpathyContactWidget *information)
1131 {
1132   GtkTreeView *view;
1133   GtkListStore *store;
1134   GtkTreeIter iter;
1135   const gchar *group;
1136
1137   view = GTK_TREE_VIEW (information->treeview_groups);
1138   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1139
1140   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1141
1142   gtk_list_store_append (store, &iter);
1143   gtk_list_store_set (store, &iter,
1144       COL_NAME, group,
1145       COL_ENABLED, TRUE,
1146       -1);
1147
1148   empathy_contact_list_add_to_group (
1149       EMPATHY_CONTACT_LIST (information->manager), information->contact,
1150       group);
1151 }
1152
1153 static void
1154 contact_widget_details_setup (EmpathyContactWidget *information)
1155 {
1156   /* FIXME: Needs new telepathy spec */
1157   gtk_widget_hide (information->vbox_details);
1158 }
1159
1160 static void
1161 contact_widget_details_update (EmpathyContactWidget *information)
1162 {
1163   /* FIXME: Needs new telepathy spec */
1164 }
1165
1166 static void
1167 contact_widget_client_setup (EmpathyContactWidget *information)
1168 {
1169   /* FIXME: Needs new telepathy spec */
1170   gtk_widget_hide (information->vbox_client);
1171 }
1172
1173 static void
1174 contact_widget_client_update (EmpathyContactWidget *information)
1175 {
1176   /* FIXME: Needs new telepathy spec */
1177 }