]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-widget.c
Don't forget to show the contact widget in some places
[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 gboolean contact_widget_id_focus_out_cb (GtkWidget *widget,
110     GdkEventFocus *event, EmpathyContactWidget *information);
111 static gboolean contact_widget_entry_alias_focus_event_cb (
112     GtkEditable *editable, GdkEventFocus *event,
113     EmpathyContactWidget *information);
114 static void contact_widget_name_notify_cb (EmpathyContactWidget *information);
115 static void contact_widget_presence_notify_cb (
116     EmpathyContactWidget *information);
117 static void contact_widget_avatar_notify_cb (
118     EmpathyContactWidget *information);
119 static void contact_widget_groups_setup (
120     EmpathyContactWidget *information);
121 static void contact_widget_groups_update (EmpathyContactWidget *information);
122 static void contact_widget_model_setup (EmpathyContactWidget *information);
123 static void contact_widget_model_populate_columns (
124     EmpathyContactWidget *information);
125 static void contact_widget_groups_populate_data (
126     EmpathyContactWidget *information);
127 static void contact_widget_groups_notify_cb (
128     EmpathyContactWidget *information);
129 static gboolean contact_widget_model_find_name (
130     EmpathyContactWidget *information,const gchar *name, GtkTreeIter *iter);
131 static gboolean contact_widget_model_find_name_foreach (GtkTreeModel *model,
132     GtkTreePath *path, GtkTreeIter *iter, FindName *data);
133 static void contact_widget_cell_toggled (GtkCellRendererToggle *cell,
134     gchar *path_string, EmpathyContactWidget *information);
135 static void contact_widget_entry_group_changed_cb (GtkEditable *editable,
136     EmpathyContactWidget *information);
137 static void contact_widget_entry_group_activate_cb (GtkEntry *entry,
138     EmpathyContactWidget *information);
139 static void contact_widget_button_group_clicked_cb (GtkButton *button,
140     EmpathyContactWidget *information);
141 static void contact_widget_details_setup (EmpathyContactWidget *information);
142 static void contact_widget_details_update (EmpathyContactWidget *information);
143 static void contact_widget_client_setup (EmpathyContactWidget *information);
144 static void contact_widget_client_update (EmpathyContactWidget *information);
145
146 enum
147 {
148   COL_NAME,
149   COL_ENABLED,
150   COL_EDITABLE,
151   COL_COUNT
152 };
153
154 GtkWidget *
155 empathy_contact_widget_new (EmpathyContact *contact,
156                             EmpathyContactWidgetFlags flags)
157 {
158   EmpathyContactWidget *information;
159   GtkBuilder *gui;
160   gchar *filename;
161
162   g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
163
164   information = g_slice_new0 (EmpathyContactWidget);
165   information->flags = flags;
166
167   filename = empathy_file_lookup ("empathy-contact-widget.ui",
168       "libempathy-gtk");
169   gui = empathy_builder_get_file (filename,
170        "vbox_contact_widget", &information->vbox_contact_widget,
171        "vbox_contact", &information->vbox_contact,
172        "hbox_presence", &information->hbox_presence,
173        "label_alias", &information->label_alias,
174        "image_state", &information->image_state,
175        "label_status", &information->label_status,
176        "table_contact", &information->table_contact,
177        "vbox_avatar", &information->vbox_avatar,
178        "vbox_groups", &information->vbox_groups,
179        "entry_group", &information->entry_group,
180        "button_group", &information->button_group,
181        "treeview_groups", &information->treeview_groups,
182        "vbox_details", &information->vbox_details,
183        "table_details", &information->table_details,
184        "hbox_details_requested", &information->hbox_details_requested,
185        "vbox_client", &information->vbox_client,
186        "table_client", &information->table_client,
187        "hbox_client_requested", &information->hbox_client_requested,
188        NULL);
189   g_free (filename);
190
191   empathy_builder_connect (gui, information,
192       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
193       "entry_group", "changed", contact_widget_entry_group_changed_cb,
194       "entry_group", "activate", contact_widget_entry_group_activate_cb,
195       "button_group", "clicked", contact_widget_button_group_clicked_cb,
196       NULL);
197
198   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
199       "EmpathyContactWidget",
200       information);
201
202   /* Create widgets */
203   contact_widget_contact_setup (information);
204   contact_widget_groups_setup (information);
205   contact_widget_details_setup (information);
206   contact_widget_client_setup (information);
207
208   if (contact != NULL)
209       contact_widget_set_contact (information, contact);
210
211   return empathy_builder_unref_and_keep_widget (gui,
212     information->vbox_contact_widget);
213 }
214
215 EmpathyContact *
216 empathy_contact_widget_get_contact (GtkWidget *widget)
217 {
218   EmpathyContactWidget *information;
219
220   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
221
222   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
223   if (!information)
224       return NULL;
225
226   return information->contact;
227 }
228
229 void
230 empathy_contact_widget_set_contact (GtkWidget *widget,
231                                     EmpathyContact *contact)
232 {
233   EmpathyContactWidget *information;
234
235   g_return_if_fail (GTK_IS_WIDGET (widget));
236   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
237
238   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
239   if (!information)
240     return;
241
242   contact_widget_set_contact (information, contact);
243 }
244
245 void
246 empathy_contact_widget_set_account_filter (
247     GtkWidget *widget,
248     EmpathyAccountChooserFilterFunc filter,
249     gpointer user_data)
250 {
251   EmpathyContactWidget *information;
252   EmpathyAccountChooser *chooser;
253
254   g_return_if_fail (GTK_IS_WIDGET (widget));
255
256   information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
257   if (!information)
258     return;
259
260   chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
261   if (chooser)
262       empathy_account_chooser_set_filter (chooser, filter, user_data);
263 }
264   
265 static void
266 contact_widget_destroy_cb (GtkWidget *widget,
267                            EmpathyContactWidget *information)
268 {
269   contact_widget_remove_contact (information);
270
271   if (information->widget_id_timeout != 0)
272     {
273       g_source_remove (information->widget_id_timeout);
274     }
275   if (information->manager)
276     {
277       g_object_unref (information->manager);
278     }   
279
280   g_slice_free (EmpathyContactWidget, information);
281 }
282
283 static void
284 contact_widget_remove_contact (EmpathyContactWidget *information)
285 {
286   if (information->contact)
287     {
288       g_signal_handlers_disconnect_by_func (information->contact,
289           contact_widget_name_notify_cb, information);
290       g_signal_handlers_disconnect_by_func (information->contact,
291           contact_widget_presence_notify_cb, information);
292       g_signal_handlers_disconnect_by_func (information->contact,
293           contact_widget_avatar_notify_cb, information);
294       g_signal_handlers_disconnect_by_func (information->contact,
295           contact_widget_groups_notify_cb, information);
296
297       g_object_unref (information->contact);
298       g_object_unref (information->factory);
299       information->contact = NULL;
300       information->factory = NULL;
301     }
302 }
303
304 static void
305 contact_widget_set_contact (EmpathyContactWidget *information,
306                             EmpathyContact *contact)
307 {
308   if (contact == information->contact)
309     return;
310
311   contact_widget_remove_contact (information);
312   if (contact)
313     {
314       TpConnection *connection;
315
316       connection = empathy_contact_get_connection (contact);
317       information->contact = g_object_ref (contact);
318       information->factory = empathy_tp_contact_factory_dup_singleton (connection);
319     }
320
321   /* Update information for widgets */
322   contact_widget_contact_update (information);
323   contact_widget_groups_update (information);
324   contact_widget_details_update (information);
325   contact_widget_client_update (information);
326 }
327
328 static gboolean
329 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
330 {
331   contact_widget_change_contact (self);
332   return FALSE;
333 }
334
335 static void
336 contact_widget_id_changed_cb (GtkEntry *entry,
337                               EmpathyContactWidget *self)
338 {
339   if (self->widget_id_timeout != 0)
340     {   
341       g_source_remove (self->widget_id_timeout);
342     }
343
344   self->widget_id_timeout =
345     g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
346         (GSourceFunc) contact_widget_id_activate_timeout, self);
347 }
348
349 static void
350 save_avatar_menu_activate_cb (GtkWidget *widget,
351                               EmpathyContactWidget *information)
352 {
353   GtkWidget *dialog;
354   EmpathyAvatar *avatar;
355   gchar *ext = NULL, *filename;
356
357   dialog = gtk_file_chooser_dialog_new (_("Save Avatar"),
358       NULL,
359       GTK_FILE_CHOOSER_ACTION_SAVE,
360       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
361       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
362       NULL);
363
364   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
365       TRUE);
366
367   /* look for the avatar extension */
368   avatar = empathy_contact_get_avatar (information->contact);
369   if (avatar->format != NULL)
370     {
371       gchar **splitted;
372
373       splitted = g_strsplit (avatar->format, "/", 2);
374       if (splitted[0] != NULL && splitted[1] != NULL)
375           ext = g_strdup (splitted[1]);
376
377       g_strfreev (splitted);
378     }
379   else
380     {
381       /* Avatar was loaded from the cache so was converted to PNG */
382       ext = g_strdup ("png");
383     }
384
385   if (ext != NULL)
386     {
387       gchar *id;
388
389       id = tp_escape_as_identifier (empathy_contact_get_id (
390             information->contact));
391
392       filename = g_strdup_printf ("%s.%s", id, ext);
393       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);
394
395       g_free (id);
396       g_free (ext);
397       g_free (filename);
398     }
399
400   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
401     {
402       GError *error = NULL;
403
404       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
405
406       if (!empathy_avatar_save_to_file (avatar, filename, &error))
407         {
408           /* Save error */
409           GtkWidget *dialog;
410
411           dialog = gtk_message_dialog_new (NULL, 0,
412               GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 
413               _("Unable to save avatar"));
414
415           gtk_message_dialog_format_secondary_text (
416               GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
417
418           g_signal_connect (dialog, "response",
419               G_CALLBACK (gtk_widget_destroy), NULL);
420
421           gtk_window_present (GTK_WINDOW (dialog));
422
423           g_clear_error (&error);
424         }
425
426       g_free (filename);
427     }
428
429   gtk_widget_destroy (dialog);
430 }
431
432 static void
433 popup_avatar_menu (EmpathyContactWidget *information,
434                    GtkWidget *parent,
435                    GdkEventButton *event)
436 {
437   GtkWidget *menu, *item;
438   gint button, event_time;
439
440   if (information->contact == NULL ||
441       empathy_contact_get_avatar (information->contact) == NULL)
442       return;
443
444   menu = gtk_menu_new ();
445
446   /* Add "Save as..." entry */
447   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
448   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
449   gtk_widget_show (item);
450
451   g_signal_connect (item, "activate",
452       G_CALLBACK (save_avatar_menu_activate_cb), information);
453
454   if (event)
455     {
456       button = event->button;
457       event_time = event->time;
458     }
459   else
460     {
461       button = 0;
462       event_time = gtk_get_current_event_time ();
463     }
464
465   gtk_menu_attach_to_widget (GTK_MENU (menu), parent, NULL);
466   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
467       button, event_time);
468 }
469
470 static gboolean
471 widget_avatar_popup_menu_cb (GtkWidget *widget,
472                              EmpathyContactWidget *information)
473 {
474   popup_avatar_menu (information, widget, NULL);
475
476   return TRUE;
477 }
478
479 static gboolean
480 widget_avatar_button_press_event_cb (GtkWidget *widget,
481                                      GdkEventButton *event,
482                                      EmpathyContactWidget *information)
483 {
484   /* Ignore double-clicks and triple-clicks */
485   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
486     {
487       popup_avatar_menu (information, widget, event);
488       return TRUE;
489     }
490
491   return FALSE;
492 }
493
494 static void
495 update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
496                                   EmpathyAvatarChooser *avatar_chooser)
497 {
498   TpConnection *connection;
499
500   connection = empathy_account_chooser_get_connection (account_chooser);
501   g_object_set (avatar_chooser, "connection", connection, NULL);
502 }
503
504 static void
505 contact_widget_contact_setup (EmpathyContactWidget *information)
506 {
507   /* Setup account label/chooser */
508   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
509     {
510       information->widget_account = empathy_account_chooser_new ();
511
512       contact_widget_change_contact (information);
513       g_signal_connect_swapped (information->widget_account, "changed",
514             G_CALLBACK (contact_widget_change_contact),
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_change_contact,
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_change_contact, 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                                EmpathyContact *contact,
693                                const GError *error,
694                                gpointer user_data,
695                                GObject *weak_object)
696 {
697   EmpathyContactWidget *information = user_data;
698
699   if (error != NULL)
700     {
701       DEBUG ("Error: %s", error->message);
702       return;
703     }
704
705   contact_widget_set_contact (information, contact);
706 }
707
708 static void
709 contact_widget_change_contact (EmpathyContactWidget *information)
710 {
711   EmpathyTpContactFactory *factory;
712   TpConnection *connection;
713
714   connection = empathy_account_chooser_get_connection (
715       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
716   if (!connection)
717       return;
718
719   factory = empathy_tp_contact_factory_dup_singleton (connection);
720   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
721     {
722       const gchar *id;
723
724       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
725       if (!EMP_STR_EMPTY (id))
726         {
727           empathy_tp_contact_factory_get_from_id (factory, id,
728               contact_widget_got_contact_cb, information, NULL,
729               G_OBJECT (information->vbox_contact_widget));
730         }
731     }
732   else
733     {
734       empathy_tp_contact_factory_get_from_handle (factory,
735           tp_connection_get_self_handle (connection),
736           contact_widget_got_contact_cb, information, NULL,
737           G_OBJECT (information->vbox_contact_widget));
738     }
739
740   g_object_unref (factory);
741 }
742
743 static void
744 contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
745                                   EmpathyContactWidget *information)
746 {
747   const gchar *data;
748   gsize size;
749   const gchar *mime_type;
750
751   empathy_avatar_chooser_get_image_data (
752       EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
753       &data, &size, &mime_type);
754   empathy_tp_contact_factory_set_avatar (information->factory,
755       data, size, mime_type);
756 }
757
758 static gboolean
759 contact_widget_id_focus_out_cb (GtkWidget *widget,
760                                 GdkEventFocus *event,
761                                 EmpathyContactWidget *information)
762 {
763   contact_widget_change_contact (information);
764   return FALSE;
765 }
766
767 static gboolean
768 contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
769                                            GdkEventFocus *event,
770                                            EmpathyContactWidget *information)
771 {
772   if (information->contact)
773     {
774       const gchar *alias;
775
776       alias = gtk_entry_get_text (GTK_ENTRY (editable));
777       empathy_tp_contact_factory_set_alias (information->factory,
778           information->contact, alias);
779     }
780
781   return FALSE;
782 }
783
784 static void
785 contact_widget_name_notify_cb (EmpathyContactWidget *information)
786 {
787   if (GTK_IS_ENTRY (information->widget_alias))
788       gtk_entry_set_text (GTK_ENTRY (information->widget_alias),
789           empathy_contact_get_name (information->contact));
790   else
791       gtk_label_set_label (GTK_LABEL (information->widget_alias),
792           empathy_contact_get_name (information->contact));
793 }
794
795 static void
796 contact_widget_presence_notify_cb (EmpathyContactWidget *information)
797 {
798   gtk_label_set_text (GTK_LABEL (information->label_status),
799       empathy_contact_get_status (information->contact));
800   gtk_image_set_from_icon_name (GTK_IMAGE (information->image_state),
801       empathy_icon_name_for_contact (information->contact),
802       GTK_ICON_SIZE_BUTTON);
803 }
804
805 static void
806 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
807 {
808   EmpathyAvatar *avatar = NULL;
809
810   if (information->contact)
811       avatar = empathy_contact_get_avatar (information->contact);
812
813   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
814     {
815       g_signal_handlers_block_by_func (information->widget_avatar,
816           contact_widget_avatar_changed_cb,
817           information);
818       empathy_avatar_chooser_set (
819           EMPATHY_AVATAR_CHOOSER (information->widget_avatar), avatar);
820       g_signal_handlers_unblock_by_func (information->widget_avatar,
821           contact_widget_avatar_changed_cb, information);
822     }
823   else
824       empathy_avatar_image_set (
825           EMPATHY_AVATAR_IMAGE (information->widget_avatar), avatar);
826 }
827
828 static void
829 contact_widget_groups_setup (EmpathyContactWidget *information)
830 {
831   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
832     {
833       information->manager = empathy_contact_manager_dup_singleton ();
834       contact_widget_model_setup (information);
835     }
836 }
837
838 static void
839 contact_widget_groups_update (EmpathyContactWidget *information)
840 {
841   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
842       information->contact)
843     {
844       g_signal_connect_swapped (information->contact, "notify::groups",
845           G_CALLBACK (contact_widget_groups_notify_cb), information);
846       contact_widget_groups_populate_data (information);
847
848       gtk_widget_show (information->vbox_groups);
849     }
850   else
851       gtk_widget_hide (information->vbox_groups);
852 }
853
854 static void
855 contact_widget_model_setup (EmpathyContactWidget *information)
856 {
857   GtkTreeView *view;
858   GtkListStore *store;
859   GtkTreeSelection *selection;
860
861   view = GTK_TREE_VIEW (information->treeview_groups);
862
863   store = gtk_list_store_new (COL_COUNT,
864       G_TYPE_STRING,   /* name */
865       G_TYPE_BOOLEAN,  /* enabled */
866       G_TYPE_BOOLEAN); /* editable */
867
868   gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
869
870   selection = gtk_tree_view_get_selection (view);
871   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
872
873   contact_widget_model_populate_columns (information);
874
875   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
876       COL_NAME, GTK_SORT_ASCENDING);
877
878   g_object_unref (store);
879 }
880
881 static void
882 contact_widget_model_populate_columns (EmpathyContactWidget *information)
883 {
884   GtkTreeView *view;
885   GtkTreeModel *model;
886   GtkTreeViewColumn *column;
887   GtkCellRenderer  *renderer;
888   guint col_offset;
889
890   view = GTK_TREE_VIEW (information->treeview_groups);
891   model = gtk_tree_view_get_model (view);
892
893   renderer = gtk_cell_renderer_toggle_new ();
894   g_signal_connect (renderer, "toggled",
895       G_CALLBACK (contact_widget_cell_toggled), information);
896
897   column = gtk_tree_view_column_new_with_attributes (_("Select"), renderer,
898       "active", COL_ENABLED, NULL);
899
900   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
901   gtk_tree_view_column_set_fixed_width (column, 50);
902   gtk_tree_view_append_column (view, column);
903
904   renderer = gtk_cell_renderer_text_new ();
905   col_offset = gtk_tree_view_insert_column_with_attributes (view,
906       -1, _("Group"),
907       renderer,
908       "text", COL_NAME,
909       /* "editable", COL_EDITABLE, */
910       NULL);
911
912   g_object_set_data (G_OBJECT (renderer),
913       "column", GINT_TO_POINTER (COL_NAME));
914
915   column = gtk_tree_view_get_column (view, col_offset - 1);
916   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
917   gtk_tree_view_column_set_resizable (column,FALSE);
918   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
919 }
920
921 static void
922 contact_widget_groups_populate_data (EmpathyContactWidget *information)
923 {
924   GtkTreeView *view;
925   GtkListStore *store;
926   GtkTreeIter iter;
927   GList *my_groups, *l;
928   GList *all_groups;
929
930   view = GTK_TREE_VIEW (information->treeview_groups);
931   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
932   gtk_list_store_clear (store);
933
934   all_groups = empathy_contact_list_get_all_groups (
935       EMPATHY_CONTACT_LIST (information->manager));
936   my_groups = empathy_contact_list_get_groups (
937       EMPATHY_CONTACT_LIST (information->manager),
938       information->contact);
939
940   for (l = all_groups; l; l = l->next)
941     {
942       const gchar *group_str;
943       gboolean enabled;
944
945       group_str = l->data;
946
947       enabled = g_list_find_custom (my_groups,
948           group_str, (GCompareFunc) strcmp) != NULL;
949
950       gtk_list_store_append (store, &iter);
951       gtk_list_store_set (store, &iter,
952           COL_NAME, group_str,
953           COL_EDITABLE, TRUE,
954           COL_ENABLED, enabled,
955           -1);
956     }
957
958   g_list_foreach (all_groups, (GFunc) g_free, NULL);
959   g_list_foreach (my_groups, (GFunc) g_free, NULL);
960   g_list_free (all_groups);
961   g_list_free (my_groups);
962 }
963
964 static void
965 contact_widget_groups_notify_cb (EmpathyContactWidget *information)
966 {
967   /* FIXME: not implemented */
968 }
969
970 static gboolean
971 contact_widget_model_find_name (EmpathyContactWidget *information,
972                                 const gchar *name,
973                                 GtkTreeIter *iter)
974 {
975   GtkTreeView *view;
976   GtkTreeModel *model;
977   FindName data;
978
979   if (EMP_STR_EMPTY (name))
980       return FALSE;
981
982   data.information = information;
983   data.name = name;
984   data.found = FALSE;
985
986   view = GTK_TREE_VIEW (information->treeview_groups);
987   model = gtk_tree_view_get_model (view);
988
989   gtk_tree_model_foreach (model,
990       (GtkTreeModelForeachFunc) contact_widget_model_find_name_foreach,
991       &data);
992
993   if (data.found == TRUE)
994     {
995       *iter = data.found_iter;
996       return TRUE;
997     }
998
999   return FALSE;
1000 }
1001
1002 static gboolean
1003 contact_widget_model_find_name_foreach (GtkTreeModel *model,
1004                                         GtkTreePath *path,
1005                                         GtkTreeIter *iter,
1006                                         FindName *data)
1007 {
1008   gchar *name;
1009
1010   gtk_tree_model_get (model, iter,
1011       COL_NAME, &name,
1012       -1);
1013
1014   if (!name)
1015       return FALSE;
1016
1017   if (data->name && strcmp (data->name, name) == 0)
1018     {
1019       data->found = TRUE;
1020       data->found_iter = *iter;
1021
1022       g_free (name);
1023
1024       return TRUE;
1025     }
1026
1027   g_free (name);
1028
1029   return FALSE;
1030 }
1031
1032 static void
1033 contact_widget_cell_toggled (GtkCellRendererToggle *cell,
1034                              gchar *path_string,
1035                              EmpathyContactWidget *information)
1036 {
1037   GtkTreeView *view;
1038   GtkTreeModel *model;
1039   GtkListStore *store;
1040   GtkTreePath *path;
1041   GtkTreeIter iter;
1042   gboolean enabled;
1043   gchar *group;
1044
1045   view = GTK_TREE_VIEW (information->treeview_groups);
1046   model = gtk_tree_view_get_model (view);
1047   store = GTK_LIST_STORE (model);
1048
1049   path = gtk_tree_path_new_from_string (path_string);
1050
1051   gtk_tree_model_get_iter (model, &iter, path);
1052   gtk_tree_model_get (model, &iter,
1053       COL_ENABLED, &enabled,
1054       COL_NAME, &group,
1055       -1);
1056
1057   gtk_list_store_set (store, &iter, COL_ENABLED, !enabled, -1);
1058   gtk_tree_path_free (path);
1059
1060   if (group)
1061     {
1062       if (enabled)
1063         {
1064           empathy_contact_list_remove_from_group (
1065               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1066               group);
1067         }
1068       else
1069         {
1070           empathy_contact_list_add_to_group (
1071               EMPATHY_CONTACT_LIST (information->manager), information->contact,
1072               group);
1073         }
1074       g_free (group);
1075     }
1076 }
1077
1078 static void
1079 contact_widget_entry_group_changed_cb (GtkEditable *editable,
1080                                        EmpathyContactWidget *information)
1081 {
1082   GtkTreeIter iter;
1083   const gchar *group;
1084
1085   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1086
1087   if (contact_widget_model_find_name (information, group, &iter))
1088       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
1089   else
1090       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
1091           !EMP_STR_EMPTY (group));
1092 }
1093
1094 static void
1095 contact_widget_entry_group_activate_cb (GtkEntry *entry,
1096                                         EmpathyContactWidget  *information)
1097 {
1098   gtk_widget_activate (GTK_WIDGET (information->button_group));
1099 }
1100
1101 static void
1102 contact_widget_button_group_clicked_cb (GtkButton *button,
1103                                         EmpathyContactWidget *information)
1104 {
1105   GtkTreeView *view;
1106   GtkListStore *store;
1107   GtkTreeIter iter;
1108   const gchar *group;
1109
1110   view = GTK_TREE_VIEW (information->treeview_groups);
1111   store = GTK_LIST_STORE (gtk_tree_view_get_model (view));
1112
1113   group = gtk_entry_get_text (GTK_ENTRY (information->entry_group));
1114
1115   gtk_list_store_append (store, &iter);
1116   gtk_list_store_set (store, &iter,
1117       COL_NAME, group,
1118       COL_ENABLED, TRUE,
1119       -1);
1120
1121   empathy_contact_list_add_to_group (
1122       EMPATHY_CONTACT_LIST (information->manager), information->contact,
1123       group);
1124 }
1125
1126 static void
1127 contact_widget_details_setup (EmpathyContactWidget *information)
1128 {
1129   /* FIXME: Needs new telepathy spec */
1130   gtk_widget_hide (information->vbox_details);
1131 }
1132
1133 static void
1134 contact_widget_details_update (EmpathyContactWidget *information)
1135 {
1136   /* FIXME: Needs new telepathy spec */
1137 }
1138
1139 static void
1140 contact_widget_client_setup (EmpathyContactWidget *information)
1141 {
1142   /* FIXME: Needs new telepathy spec */
1143   gtk_widget_hide (information->vbox_client);
1144 }
1145
1146 static void
1147 contact_widget_client_update (EmpathyContactWidget *information)
1148 {
1149   /* FIXME: Needs new telepathy spec */
1150 }