]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-avatar-chooser.c
Coding style: cuddle a brace
[empathy.git] / libempathy-gtk / empathy-avatar-chooser.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB.
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Based on Novell's e-image-chooser.
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gio/gio.h>
31
32 #include <libempathy/empathy-utils.h>
33 #include "empathy-avatar-chooser.h"
34 #include "empathy-conf.h"
35 #include "empathy-ui-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39
40 #define AVATAR_SIZE_SAVE 96
41 #define AVATAR_SIZE_VIEW 64
42 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
43
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
45 typedef struct {
46         EmpathyContactFactory   *contact_factory;
47         McAccount               *account;
48         EmpathyTpContactFactory *tp_contact_factory;
49
50         gulong ready_handler_id;
51
52         EmpathyAvatar *avatar;
53 } EmpathyAvatarChooserPriv;
54
55 static void       avatar_chooser_finalize              (GObject              *object);
56 static void       avatar_chooser_set_account           (EmpathyAvatarChooser *self,
57                                                         McAccount            *account);
58 static void       avatar_chooser_set_image             (EmpathyAvatarChooser *chooser,
59                                                         gchar                *data,
60                                                         gsize                 size,
61                                                         gboolean              set_locally);
62 static gboolean   avatar_chooser_drag_motion_cb        (GtkWidget            *widget,
63                                                         GdkDragContext       *context,
64                                                         gint                  x,
65                                                         gint                  y,
66                                                         guint                 time,
67                                                         EmpathyAvatarChooser *chooser);
68 static void       avatar_chooser_drag_leave_cb         (GtkWidget            *widget,
69                                                         GdkDragContext       *context,
70                                                         guint                 time,
71                                                         EmpathyAvatarChooser *chooser);
72 static gboolean   avatar_chooser_drag_drop_cb          (GtkWidget            *widget,
73                                                         GdkDragContext       *context,
74                                                         gint                  x,
75                                                         gint                  y,
76                                                         guint                 time,
77                                                         EmpathyAvatarChooser *chooser);
78 static void       avatar_chooser_drag_data_received_cb (GtkWidget            *widget,
79                                                         GdkDragContext       *context,
80                                                         gint                  x,
81                                                         gint                  y,
82                                                         GtkSelectionData     *selection_data,
83                                                         guint                 info,
84                                                         guint                 time,
85                                                         EmpathyAvatarChooser *chooser);
86 static void       avatar_chooser_clicked_cb            (GtkWidget            *button,
87                                                         EmpathyAvatarChooser *chooser);
88
89 enum {
90         CHANGED,
91         LAST_SIGNAL
92 };
93
94 enum {
95         PROP_0,
96         PROP_ACCOUNT
97 };
98
99 static guint signals [LAST_SIGNAL];
100
101 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
102
103 /*
104  * Drag and drop stuff
105  */
106 #define URI_LIST_TYPE "text/uri-list"
107
108 enum DndTargetType {
109         DND_TARGET_TYPE_URI_LIST
110 };
111
112 static const GtkTargetEntry drop_types[] = {
113         { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
114 };
115
116 static void
117 avatar_chooser_get_property (GObject    *object,
118                              guint       param_id,
119                              GValue     *value,
120                              GParamSpec *pspec)
121 {
122         EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
123
124         switch (param_id) {
125         case PROP_ACCOUNT:
126                 g_value_set_object (value, priv->account);
127                 break;
128         default:
129                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
130                 break;
131         }
132 }
133
134 static void
135 avatar_chooser_set_property (GObject      *object,
136                              guint         param_id,
137                              const GValue *value,
138                              GParamSpec   *pspec)
139 {
140         EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
141
142         switch (param_id) {
143         case PROP_ACCOUNT:
144                 avatar_chooser_set_account (self, g_value_get_object (value));
145                 break;
146         default:
147                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
148                 break;
149         }
150 }
151
152 static void
153 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
154 {
155         GObjectClass *object_class = G_OBJECT_CLASS (klass);
156         GParamSpec *param_spec;
157
158         object_class->finalize = avatar_chooser_finalize;
159         object_class->get_property = avatar_chooser_get_property;
160         object_class->set_property = avatar_chooser_set_property;
161
162         signals[CHANGED] =
163                 g_signal_new ("changed",
164                               G_TYPE_FROM_CLASS (klass),
165                               G_SIGNAL_RUN_LAST,
166                               0,
167                               NULL, NULL,
168                               g_cclosure_marshal_VOID__VOID,
169                               G_TYPE_NONE, 0);
170
171         param_spec = g_param_spec_object ("account",
172                                           "McAccount",
173                                           "McAccount whose avatar should be "
174                                           "shown and modified by this widget",
175                                           MC_TYPE_ACCOUNT,
176                                           G_PARAM_READWRITE |
177                                           G_PARAM_STATIC_STRINGS);
178         g_object_class_install_property (object_class,
179                                          PROP_ACCOUNT,
180                                          param_spec);
181
182         g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
183 }
184
185 static void
186 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
187 {
188         EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
189                 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
190
191         chooser->priv = priv;
192         gtk_drag_dest_set (GTK_WIDGET (chooser),
193                            GTK_DEST_DEFAULT_ALL,
194                            drop_types,
195                            G_N_ELEMENTS (drop_types),
196                            GDK_ACTION_COPY);
197
198         g_signal_connect (chooser, "drag-motion",
199                           G_CALLBACK (avatar_chooser_drag_motion_cb),
200                           chooser);
201         g_signal_connect (chooser, "drag-leave",
202                           G_CALLBACK (avatar_chooser_drag_leave_cb),
203                           chooser);
204         g_signal_connect (chooser, "drag-drop",
205                           G_CALLBACK (avatar_chooser_drag_drop_cb),
206                           chooser);
207         g_signal_connect (chooser, "drag-data-received",
208                           G_CALLBACK (avatar_chooser_drag_data_received_cb),
209                           chooser);
210         g_signal_connect (chooser, "clicked",
211                           G_CALLBACK (avatar_chooser_clicked_cb),
212                           chooser);
213
214         priv->contact_factory = empathy_contact_factory_new ();
215
216         empathy_avatar_chooser_set (chooser, NULL);
217 }
218
219 static void
220 avatar_chooser_finalize (GObject *object)
221 {
222         EmpathyAvatarChooserPriv *priv;
223
224         priv = GET_PRIV (object);
225
226         avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
227         g_assert (priv->account == NULL && priv->tp_contact_factory == NULL);
228
229         g_object_unref (priv->contact_factory);
230
231         if (priv->avatar != NULL) {
232                 empathy_avatar_unref (priv->avatar);
233         }
234
235         G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
236 }
237
238 static void
239 avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
240                                GParamSpec              *unused,
241                                EmpathyAvatarChooser    *self)
242 {
243         EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
244         gboolean ready;
245
246         /* sanity check that we're listening on the right ETpCF */
247         g_assert (priv->tp_contact_factory == tp_cf);
248
249         ready = empathy_tp_contact_factory_is_ready (tp_cf);
250         gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
251 }
252
253 static void
254 avatar_chooser_set_account (EmpathyAvatarChooser *self,
255                             McAccount            *account)
256 {
257         EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
258
259         if (priv->account != NULL) {
260                 g_object_unref (priv->account);
261                 priv->account = NULL;
262
263                 g_assert (priv->tp_contact_factory != NULL);
264
265                 g_signal_handler_disconnect (priv->tp_contact_factory,
266                         priv->ready_handler_id);
267                 priv->ready_handler_id = 0;
268
269                 g_object_unref (priv->tp_contact_factory);
270                 priv->tp_contact_factory = NULL;
271         }
272
273         if (account != NULL) {
274                 priv->account = g_object_ref (account);
275                 priv->tp_contact_factory = g_object_ref (
276                         empathy_contact_factory_get_tp_factory (
277                                 priv->contact_factory, priv->account));
278
279                 priv->ready_handler_id = g_signal_connect (
280                         priv->tp_contact_factory, "notify::ready",
281                         G_CALLBACK (avatar_chooser_tp_cf_ready_cb), self);
282                 avatar_chooser_tp_cf_ready_cb (priv->tp_contact_factory, NULL,
283                         self);
284         }
285 }
286
287 static gboolean
288 str_in_strv (gchar  *str,
289              gchar **strv)
290 {
291         if (strv == NULL) {
292                 return FALSE;
293         }
294
295         while (*strv != NULL) {
296                 if (g_str_equal (str, *strv)) {
297                         return TRUE;
298                 }
299                 strv++;
300         }
301         return FALSE;
302 }
303
304 /* The caller must free the strings stored in satisfactory_format_name and
305  * satisfactory_mime_type.
306  */
307 static gboolean
308 can_satisfy_mime_type_requirements (gchar **accepted_mime_types,
309                                     gchar **satisfactory_format_name,
310                                     gchar **satisfactory_mime_type)
311 {
312         GSList *formats;
313         GSList *i;
314         gchar **j;
315         gboolean done = FALSE;
316
317         if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
318                 return FALSE;
319         }
320
321         g_assert (satisfactory_format_name != NULL);
322         g_assert (satisfactory_mime_type != NULL);
323
324         formats = gdk_pixbuf_get_formats ();
325
326         for (i = formats; !done && i != NULL; i = i->next) {
327                 GdkPixbufFormat *format = i->data;
328                 gchar **format_mime_types;
329
330                 if (!gdk_pixbuf_format_is_writable (format)) {
331                         continue;
332                 }
333
334                 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
335                 for (j = accepted_mime_types; *j != NULL; j++) {
336                         if (str_in_strv (*j, format_mime_types)) {
337                                 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
338                                 *satisfactory_mime_type = g_strdup (*j);
339                                 done = TRUE;
340                                 break;
341                         }
342                 }
343                 g_strfreev (format_mime_types);
344         }
345
346         g_slist_free (formats);
347
348         return done;
349 }
350
351 static EmpathyAvatar *
352 avatar_chooser_convert (EmpathyAvatarChooser *chooser,
353                         GdkPixbuf            *pixbuf_scaled,
354                         gchar               **mime_types,
355                         gsize                 max_size)
356 {
357         gchar         *format_name = NULL, *new_mime_type = NULL;
358         gchar         *converted_image_data = NULL;
359         gsize          converted_image_size = 0;
360         EmpathyAvatar *converted_avatar = NULL;
361         gboolean       saved;
362         GError        *error = NULL;
363
364         if (!can_satisfy_mime_type_requirements (mime_types, &format_name,
365                                                  &new_mime_type)) {
366                 DEBUG ("Mon dieu! Can't convert to any acceptable format!");
367                 return NULL;
368         }
369
370         saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled, &converted_image_data,
371                 &converted_image_size, format_name, &error, NULL);
372         g_free (format_name);
373
374         if (!saved) {
375                 DEBUG ("Couldn't convert image: %s", error->message);
376                 g_error_free (error);
377
378                 g_free (new_mime_type);
379                 return NULL;
380         }
381
382         /* Takes ownership of new_mime_type */
383         converted_avatar = empathy_avatar_new (converted_image_data,
384                 converted_image_size, new_mime_type, NULL);
385
386         if (max_size > 0 && converted_avatar->len > max_size) {
387                 /* FIXME: We could try converting to a different format; in
388                  *        particular, try converting to jpeg with increasingly
389                  *        high compression (if jpeg is supported). Not sure how
390                  *        much we care.
391                  */
392                 DEBUG ("Converted the image, but the new filesize is too big");
393
394                 empathy_avatar_unref (converted_avatar);
395                 converted_avatar = NULL;
396         }
397
398         return converted_avatar;
399 }
400
401 static EmpathyAvatar *
402 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
403                                         GdkPixbuf            *pixbuf,
404                                         EmpathyAvatar        *avatar)
405 {
406         EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
407         EmpathyTpContactFactory  *tp_cf = priv->tp_contact_factory;
408         gint                      max_width = 0, max_height = 0, max_size = 0;
409         gchar                   **mime_types = NULL;
410         gboolean                  needs_conversion = FALSE;
411         GdkPixbuf                *pixbuf_scaled = NULL;
412
413         /* This should only be called if the user is setting a new avatar,
414          * which should only be allowed once the avatar requirements have been
415          * discovered.
416          */
417         g_return_val_if_fail (tp_cf != NULL, NULL);
418         g_return_val_if_fail (empathy_tp_contact_factory_is_ready (tp_cf),
419                 NULL);
420
421         g_object_get (tp_cf,
422                 "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
423                 "avatar-max-width", &max_width,
424                 "avatar-max-height", &max_height,
425                 "avatar-max-size", &max_size,
426                 NULL);
427
428         /* If the avatar's not already the right type, it needs converting. */
429         if (!str_in_strv (avatar->format, mime_types)) {
430                 needs_conversion = TRUE;
431         }
432
433         /* If scaling down the pixbuf to fit the dimensions yields a new
434          * pixbuf, then it needed scaling. if it's the same pixbuf, it did not.
435          */
436         pixbuf_scaled = empathy_pixbuf_scale_down_if_necessary (pixbuf,
437                 MIN(max_width, max_height));
438         if (pixbuf_scaled != pixbuf) {
439                 needs_conversion = TRUE;
440         }
441
442         if (max_size > 0 && avatar->len > max_size) {
443                 needs_conversion = TRUE;
444         }
445
446         if (needs_conversion) {
447                 avatar = avatar_chooser_convert (chooser, pixbuf_scaled,
448                         mime_types, max_size);
449         } else {
450                 /* Just return another reference to the avatar passed in. */
451                 avatar = empathy_avatar_ref (avatar);
452         }
453
454         g_object_unref (pixbuf_scaled);
455         g_strfreev (mime_types);
456         return avatar;
457 }
458
459 static void
460 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
461 {
462         EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
463         GtkWidget *image;
464
465         if (priv->avatar == NULL) {
466                 return;
467         }
468
469         empathy_avatar_unref (priv->avatar);
470         priv->avatar = NULL;
471
472         image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
473         gtk_button_set_image (GTK_BUTTON (chooser), image);
474         g_signal_emit (chooser, signals[CHANGED], 0);
475 }
476
477 static void
478 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
479                           gchar                *data,
480                           gsize                 size,
481                           gboolean              set_locally)
482 {
483         EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
484
485         GdkPixbuf     *pixbuf, *pixbuf_view;
486         GtkWidget     *image;
487         EmpathyAvatar *avatar = NULL;
488         gchar         *mime_type = NULL;
489
490         if (data == NULL) {
491                 avatar_chooser_clear_image (chooser);
492                 g_free (data);
493                 return;
494         }
495
496         pixbuf = empathy_pixbuf_from_data (data, size, &mime_type);
497         if (pixbuf == NULL) {
498                 g_free (data);
499                 return;
500         }
501
502         /* avatar takes ownership of data and mime_type */
503         avatar = empathy_avatar_new (data, size, mime_type, NULL);
504
505         if (set_locally) {
506                 EmpathyAvatar *conv = avatar_chooser_maybe_convert_and_scale (
507                         chooser, pixbuf, avatar);
508                 empathy_avatar_unref (avatar);
509                 avatar = conv;
510         }
511
512         if (avatar == NULL) {
513                 /* An error occured; don't change the avatar. */
514                 return;
515         }
516
517         if (priv->avatar != NULL) {
518                 empathy_avatar_unref (priv->avatar);
519         }
520         priv->avatar = avatar;
521
522         pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
523         image = gtk_image_new_from_pixbuf (pixbuf_view);
524
525         gtk_button_set_image (GTK_BUTTON (chooser), image);
526         g_signal_emit (chooser, signals[CHANGED], 0);
527
528         g_object_unref (pixbuf_view);
529         g_object_unref (pixbuf);
530 }
531
532 static void
533 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
534                                     const gchar          *filename)
535 {
536         gchar  *image_data = NULL;
537         gsize   image_size = 0;
538         GError *error = NULL;
539
540         if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
541                 DEBUG ("Failed to load image from '%s': %s", filename,
542                         error ? error->message : "No error given");
543
544                 g_clear_error (&error);
545                 return;
546         }
547
548         avatar_chooser_set_image (chooser, image_data, image_size, TRUE);
549 }
550
551 static gboolean
552 avatar_chooser_drag_motion_cb (GtkWidget          *widget,
553                               GdkDragContext     *context,
554                               gint                x,
555                               gint                y,
556                               guint               time,
557                               EmpathyAvatarChooser *chooser)
558 {
559         EmpathyAvatarChooserPriv *priv;
560         GList                  *p;
561
562         priv = GET_PRIV (chooser);
563
564         for (p = context->targets; p != NULL; p = p->next) {
565                 gchar *possible_type;
566
567                 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
568
569                 if (!strcmp (possible_type, URI_LIST_TYPE)) {
570                         g_free (possible_type);
571                         gdk_drag_status (context, GDK_ACTION_COPY, time);
572
573                         return TRUE;
574                 }
575
576                 g_free (possible_type);
577         }
578
579         return FALSE;
580 }
581
582 static void
583 avatar_chooser_drag_leave_cb (GtkWidget          *widget,
584                              GdkDragContext     *context,
585                              guint               time,
586                              EmpathyAvatarChooser *chooser)
587 {
588 }
589
590 static gboolean
591 avatar_chooser_drag_drop_cb (GtkWidget          *widget,
592                             GdkDragContext     *context,
593                             gint                x,
594                             gint                y,
595                             guint               time,
596                             EmpathyAvatarChooser *chooser)
597 {
598         EmpathyAvatarChooserPriv *priv;
599         GList                  *p;
600
601         priv = GET_PRIV (chooser);
602
603         if (context->targets == NULL) {
604                 return FALSE;
605         }
606
607         for (p = context->targets; p != NULL; p = p->next) {
608                 char *possible_type;
609
610                 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
611                 if (!strcmp (possible_type, URI_LIST_TYPE)) {
612                         g_free (possible_type);
613                         gtk_drag_get_data (widget, context,
614                                            GDK_POINTER_TO_ATOM (p->data),
615                                            time);
616
617                         return TRUE;
618                 }
619
620                 g_free (possible_type);
621         }
622
623         return FALSE;
624 }
625
626 static void
627 avatar_chooser_drag_data_received_cb (GtkWidget          *widget,
628                                      GdkDragContext     *context,
629                                      gint                x,
630                                      gint                y,
631                                      GtkSelectionData   *selection_data,
632                                      guint               info,
633                                      guint               time,
634                                      EmpathyAvatarChooser *chooser)
635 {
636         gchar    *target_type;
637         gboolean  handled = FALSE;
638
639         target_type = gdk_atom_name (selection_data->target);
640         if (!strcmp (target_type, URI_LIST_TYPE)) {
641                 GFile            *file;
642                 GFileInputStream *input_stream;
643                 gchar            *nl;
644                 gchar            *data = NULL;
645
646                 nl = strstr (selection_data->data, "\r\n");
647                 if (nl) {
648                         gchar *uri;
649
650                         uri = g_strndup (selection_data->data,
651                                          nl - (gchar*) selection_data->data);
652
653                         file = g_file_new_for_uri (uri);
654                         g_free (uri);
655                 } else {
656                         file = g_file_new_for_uri (selection_data->data);
657                 }
658
659                 input_stream = g_file_read (file, NULL, NULL);
660
661                 if (input_stream != NULL) {
662                         GFileInfo *info;
663                         
664                         info = g_file_query_info (file,
665                                                   G_FILE_ATTRIBUTE_STANDARD_SIZE,
666                                                   0, NULL, NULL);
667                         if (info != NULL) {
668                                 goffset size;
669                                 gssize bytes_read;
670                                 
671                                 size = g_file_info_get_size (info);
672                                 data = g_malloc (size);
673
674                                 bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
675                                                                   data, size,
676                                                                   NULL, NULL);
677                                 if (bytes_read != -1) {
678                                         avatar_chooser_set_image (chooser,
679                                                                   data,
680                                                                   (gsize) bytes_read,
681                                                                   TRUE);
682                                         handled = TRUE;
683                                 }
684
685                                 g_free (data);
686                                 g_object_unref (info);
687                         }
688
689                         g_object_unref (input_stream);
690                 }
691                 
692                 g_object_unref (file);
693         }
694
695         gtk_drag_finish (context, handled, FALSE, time);
696 }
697
698 static void
699 avatar_chooser_update_preview_cb (GtkFileChooser       *file_chooser,
700                                   EmpathyAvatarChooser *chooser)
701 {
702         gchar *filename;
703
704         filename = gtk_file_chooser_get_preview_filename (file_chooser);
705
706         if (filename) {
707                 GtkWidget *image;
708                 GdkPixbuf *pixbuf = NULL;
709                 GdkPixbuf *scaled_pixbuf;
710
711                 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
712
713                 image = gtk_file_chooser_get_preview_widget (file_chooser);
714
715                 if (pixbuf) {
716                         scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
717                         gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
718                         g_object_unref (scaled_pixbuf);
719                         g_object_unref (pixbuf);
720                 } else {
721                         gtk_image_set_from_stock (GTK_IMAGE (image),
722                                                   "gtk-dialog-question",
723                                                   GTK_ICON_SIZE_DIALOG);
724                 }
725         }
726
727         gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
728 }
729
730 static void
731 avatar_chooser_response_cb (GtkWidget            *widget,
732                             gint                  response,
733                             EmpathyAvatarChooser *chooser)
734 {
735         EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
736
737         if (response == GTK_RESPONSE_CANCEL) {
738                 goto out;
739         }
740
741         /* Check if we went non-ready since displaying the dialog. */
742         if (!empathy_tp_contact_factory_is_ready (priv->tp_contact_factory)) {
743                 DEBUG ("Can't set avatar when contact factory isn't ready.");
744                 goto out;
745         }
746
747         if (response == GTK_RESPONSE_OK) {
748                 gchar *filename;
749                 gchar *path;
750
751                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
752                 avatar_chooser_set_image_from_file (chooser, filename);
753                 g_free (filename);
754
755                 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
756                 if (path) {
757                         empathy_conf_set_string (empathy_conf_get (),
758                                                  EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
759                                                  path);
760                         g_free (path);
761                 }
762         }
763         else if (response == GTK_RESPONSE_NO) {
764                 /* This corresponds to "No Image", not to "Cancel" */
765                 avatar_chooser_clear_image (chooser);
766         }
767
768 out:
769         gtk_widget_destroy (widget);
770 }
771
772 static void
773 avatar_chooser_clicked_cb (GtkWidget            *button,
774                            EmpathyAvatarChooser *chooser)
775 {
776         GtkFileChooser *chooser_dialog;
777         GtkWidget      *image;
778         gchar          *saved_dir = NULL;
779         const gchar    *default_dir = DEFAULT_DIR;
780         const gchar    *pics_dir;
781         GtkFileFilter  *filter;
782
783         chooser_dialog = GTK_FILE_CHOOSER (
784                 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
785                                              empathy_get_toplevel_window (GTK_WIDGET (chooser)),
786                                              GTK_FILE_CHOOSER_ACTION_OPEN,
787                                              _("No Image"),
788                                              GTK_RESPONSE_NO,
789                                              GTK_STOCK_CANCEL,
790                                              GTK_RESPONSE_CANCEL,
791                                              GTK_STOCK_OPEN,
792                                              GTK_RESPONSE_OK,
793                                              NULL));
794
795         /* Get special dirs */
796         empathy_conf_get_string (empathy_conf_get (),
797                                  EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
798                                  &saved_dir);
799         if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
800                 g_free (saved_dir);
801                 saved_dir = NULL;
802         }
803         if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
804                 default_dir = NULL;
805         }
806         pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
807         if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
808                 pics_dir = NULL;
809         }
810
811         /* Set current dir to the last one or to DEFAULT_DIR or to home */
812         if (saved_dir) {
813                 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
814         }
815         else if (pics_dir) {
816                 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
817         }
818         else if (default_dir) {
819                 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
820         } else {
821                 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
822         }
823
824         /* Add shortcuts to special dirs */
825         if (saved_dir) {
826                 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
827         }
828         else if (pics_dir) {
829                 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
830         }
831         if (default_dir) {
832                 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
833         }
834
835         /* Setup preview image */
836         image = gtk_image_new ();
837         gtk_file_chooser_set_preview_widget (chooser_dialog, image);
838         gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
839         gtk_widget_show (image);
840         gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
841         g_signal_connect (chooser_dialog, "update-preview",
842                           G_CALLBACK (avatar_chooser_update_preview_cb),
843                           chooser);
844
845         /* Setup filers */
846         filter = gtk_file_filter_new ();
847         gtk_file_filter_set_name (filter, _("Images"));
848         gtk_file_filter_add_pixbuf_formats (filter);
849         gtk_file_chooser_add_filter (chooser_dialog, filter);
850         filter = gtk_file_filter_new ();
851         gtk_file_filter_set_name (filter, _("All Files"));
852         gtk_file_filter_add_pattern(filter, "*");
853         gtk_file_chooser_add_filter (chooser_dialog, filter);
854
855         /* Setup response */
856         gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
857         g_signal_connect (chooser_dialog, "response",
858                           G_CALLBACK (avatar_chooser_response_cb),
859                           chooser);
860
861         gtk_widget_show (GTK_WIDGET (chooser_dialog));
862         g_free (saved_dir);
863 }
864
865 GtkWidget *
866 empathy_avatar_chooser_new ()
867 {
868         return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
869 }
870
871 /* FIXME: when the avatar passed to this function actually can be relied upon to
872  * contain a mime type, we can probably just ref it and store it.
873  */
874 void
875 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
876                             EmpathyAvatar        *avatar)
877 {
878         g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
879
880         if (avatar != NULL) {
881                 gchar *data = g_memdup (avatar->data, avatar->len);
882                 avatar_chooser_set_image (chooser, data, avatar->len, FALSE);
883         } else {
884                 avatar_chooser_clear_image (chooser);
885         }
886 }
887
888 void
889 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser  *chooser,
890                                        const gchar          **data,
891                                        gsize                 *data_size,
892                                        const gchar          **mime_type)
893 {
894         EmpathyAvatarChooserPriv *priv;
895
896         g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
897
898         priv = GET_PRIV (chooser);
899
900         if (priv->avatar != NULL) {
901                 if (data != NULL) {
902                         *data = priv->avatar->data;
903                 }
904                 if (data_size != NULL) {
905                         *data_size = priv->avatar->len;
906                 }
907                 if (mime_type != NULL) {
908                         *mime_type = priv->avatar->format;
909                 }
910         } else {
911                 if (data != NULL) {
912                         *data = NULL;
913                 }
914                 if (data_size != NULL) {
915                         *data_size = 0;
916                 }
917                 if (mime_type != NULL) {
918                         *mime_type = NULL;
919                 }
920         }
921 }
922