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