1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB.
4 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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.
20 * Authors: Based on Novell's e-image-chooser.
21 * Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-contact-factory.h>
35 #include "empathy-avatar-chooser.h"
36 #include "empathy-conf.h"
37 #include "empathy-ui-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
42 #define AVATAR_SIZE_SAVE 96
43 #define AVATAR_SIZE_VIEW 64
44 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
48 EmpathyContactFactory *contact_factory;
50 EmpathyTpContactFactory *tp_contact_factory;
51 GtkFileChooser *chooser_dialog;
53 gulong ready_handler_id;
55 EmpathyAvatar *avatar;
56 } EmpathyAvatarChooserPriv;
58 static void avatar_chooser_finalize (GObject *object);
59 static void avatar_chooser_set_account (EmpathyAvatarChooser *self,
61 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
62 EmpathyAvatar *avatar,
64 gboolean set_locally);
65 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
66 GdkDragContext *context,
70 EmpathyAvatarChooser *chooser);
71 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
72 GdkDragContext *context,
74 EmpathyAvatarChooser *chooser);
75 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
76 GdkDragContext *context,
80 EmpathyAvatarChooser *chooser);
81 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
82 GdkDragContext *context,
85 GtkSelectionData *selection_data,
88 EmpathyAvatarChooser *chooser);
89 static void avatar_chooser_clicked_cb (GtkWidget *button,
90 EmpathyAvatarChooser *chooser);
102 static guint signals [LAST_SIGNAL];
104 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
107 * Drag and drop stuff
109 #define URI_LIST_TYPE "text/uri-list"
112 DND_TARGET_TYPE_URI_LIST
115 static const GtkTargetEntry drop_types[] = {
116 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
120 avatar_chooser_get_property (GObject *object,
125 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
129 g_value_set_object (value, priv->account);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
138 avatar_chooser_set_property (GObject *object,
143 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
147 avatar_chooser_set_account (self, g_value_get_object (value));
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
156 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
158 GObjectClass *object_class = G_OBJECT_CLASS (klass);
159 GParamSpec *param_spec;
161 object_class->finalize = avatar_chooser_finalize;
162 object_class->get_property = avatar_chooser_get_property;
163 object_class->set_property = avatar_chooser_set_property;
166 g_signal_new ("changed",
167 G_TYPE_FROM_CLASS (klass),
171 g_cclosure_marshal_VOID__VOID,
174 param_spec = g_param_spec_object ("account",
176 "McAccount whose avatar should be "
177 "shown and modified by this widget",
180 G_PARAM_STATIC_STRINGS);
181 g_object_class_install_property (object_class,
185 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
189 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
191 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
192 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
194 chooser->priv = priv;
195 gtk_drag_dest_set (GTK_WIDGET (chooser),
196 GTK_DEST_DEFAULT_ALL,
198 G_N_ELEMENTS (drop_types),
201 g_signal_connect (chooser, "drag-motion",
202 G_CALLBACK (avatar_chooser_drag_motion_cb),
204 g_signal_connect (chooser, "drag-leave",
205 G_CALLBACK (avatar_chooser_drag_leave_cb),
207 g_signal_connect (chooser, "drag-drop",
208 G_CALLBACK (avatar_chooser_drag_drop_cb),
210 g_signal_connect (chooser, "drag-data-received",
211 G_CALLBACK (avatar_chooser_drag_data_received_cb),
213 g_signal_connect (chooser, "clicked",
214 G_CALLBACK (avatar_chooser_clicked_cb),
217 priv->contact_factory = empathy_contact_factory_dup_singleton ();
219 empathy_avatar_chooser_set (chooser, NULL);
223 avatar_chooser_finalize (GObject *object)
225 EmpathyAvatarChooserPriv *priv;
227 priv = GET_PRIV (object);
229 avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
230 g_assert (priv->account == NULL);
231 g_assert (priv->tp_contact_factory == NULL);
233 g_object_unref (priv->contact_factory);
235 if (priv->avatar != NULL) {
236 empathy_avatar_unref (priv->avatar);
239 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
243 avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
245 EmpathyAvatarChooser *self)
247 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
250 /* sanity check that we're listening on the right ETpCF */
251 g_assert (priv->tp_contact_factory == tp_cf);
253 ready = empathy_tp_contact_factory_is_ready (tp_cf);
254 gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
258 avatar_chooser_set_account (EmpathyAvatarChooser *self,
261 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
263 if (priv->account != NULL) {
264 g_object_unref (priv->account);
265 priv->account = NULL;
267 g_assert (priv->tp_contact_factory != NULL);
269 g_signal_handler_disconnect (priv->tp_contact_factory,
270 priv->ready_handler_id);
271 priv->ready_handler_id = 0;
273 g_object_unref (priv->tp_contact_factory);
274 priv->tp_contact_factory = NULL;
277 if (account != NULL) {
278 priv->account = g_object_ref (account);
279 priv->tp_contact_factory = g_object_ref (
280 empathy_contact_factory_get_tp_factory (
281 priv->contact_factory, priv->account));
283 priv->ready_handler_id = g_signal_connect (
284 priv->tp_contact_factory, "notify::ready",
285 G_CALLBACK (avatar_chooser_tp_cf_ready_cb), self);
286 avatar_chooser_tp_cf_ready_cb (priv->tp_contact_factory, NULL,
292 avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
293 const gchar *primary_text,
294 const gchar *secondary_text)
299 parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
300 if (!GTK_IS_WINDOW (parent)) {
304 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
310 if (secondary_text != NULL) {
311 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
312 "%s", secondary_text);
315 g_signal_connect (dialog, "response",
316 G_CALLBACK (gtk_widget_destroy), NULL);
317 gtk_widget_show (dialog);
322 str_in_strv (const gchar *str,
329 while (*strv != NULL) {
330 if (g_str_equal (str, *strv)) {
338 /* The caller must free the strings stored in satisfactory_format_name and
339 * satisfactory_mime_type.
342 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
343 gchar **accepted_mime_types,
344 gchar **satisfactory_format_name,
345 gchar **satisfactory_mime_type)
347 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
350 gboolean found = FALSE;
352 *satisfactory_format_name = NULL;
353 *satisfactory_mime_type = NULL;
355 /* If there is no accepted format there is nothing we can do */
356 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
360 /* If the current mime type is good and accepted, don't change it!
361 * jpeg is compress better pictures, but png is better for logos and
362 * could have an alpha layer. */
363 if (str_in_strv (current_mime_type, good_mime_types) &&
364 str_in_strv (current_mime_type, accepted_mime_types)) {
365 *satisfactory_mime_type = g_strdup (current_mime_type);
366 *satisfactory_format_name = g_strdup (current_mime_type +
371 /* The current mime type is either not accepted or not good to use.
372 * Check if one of the good format is supported... */
373 for (i = 0; good_mime_types[i] != NULL; i++) {
374 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
375 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
376 *satisfactory_format_name = g_strdup (good_mime_types[i] +
382 /* Pick the first supported format we can write */
383 formats = gdk_pixbuf_get_formats ();
384 for (l = formats; !found && l != NULL; l = l->next) {
385 GdkPixbufFormat *format = l->data;
386 gchar **format_mime_types;
389 if (!gdk_pixbuf_format_is_writable (format)) {
393 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
394 for (iter = format_mime_types; *iter != NULL; iter++) {
395 if (str_in_strv (*iter, accepted_mime_types)) {
396 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
397 *satisfactory_mime_type = g_strdup (*iter);
402 g_strfreev (format_mime_types);
404 g_slist_free (formats);
409 static EmpathyAvatar *
410 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
412 EmpathyAvatar *avatar)
414 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
415 EmpathyTpContactFactory *tp_cf = priv->tp_contact_factory;
416 guint max_width = 0, max_height = 0, max_size = 0;
417 gchar **mime_types = NULL;
418 gboolean needs_conversion = FALSE;
420 gchar *new_format_name = NULL;
421 gchar *new_mime_type = NULL;
422 gdouble min_factor, max_factor;
424 gchar *converted_image_data = NULL;
425 gsize converted_image_size = 0;
427 /* This should only be called if the user is setting a new avatar,
428 * which should only be allowed once the avatar requirements have been
431 g_return_val_if_fail (tp_cf != NULL, NULL);
432 g_return_val_if_fail (empathy_tp_contact_factory_is_ready (tp_cf),
436 "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
437 "avatar-max-width", &max_width,
438 "avatar-max-height", &max_height,
439 "avatar-max-size", &max_size,
442 /* Smaller is the factor, smaller will be the image.
443 * 0 is an empty image, 1 is the full size. */
448 /* Check if we need to convert to another image format */
449 if (avatar_chooser_need_mime_type_conversion (avatar->format,
453 DEBUG ("Format conversion needed, we'll use mime type '%s' "
454 "and format name '%s'. Current mime type is '%s'",
455 new_mime_type, new_format_name, avatar->format);
456 needs_conversion = TRUE;
458 g_strfreev (mime_types);
460 /* If there is no format we can use, report error to the user. */
461 if (new_mime_type == NULL || new_format_name == NULL) {
462 avatar_chooser_error_show (chooser, _("Couldn't convert image"),
463 _("None of the accepted image formats is "
464 "supported on your system"));
468 /* If width or height are too big, it needs converting. */
469 width = gdk_pixbuf_get_width (pixbuf);
470 height = gdk_pixbuf_get_height (pixbuf);
471 if ((max_width > 0 && width > max_width) ||
472 (max_height > 0 && height > max_height)) {
473 gdouble h_factor, v_factor;
475 h_factor = (gdouble) max_width / width;
476 v_factor = (gdouble) max_height / height;
477 factor = max_factor = MIN (h_factor, v_factor);
479 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
480 width, height, max_width, max_height);
482 needs_conversion = TRUE;
485 /* If the data len is too big and no other conversion is needed,
486 * try with a lower factor. */
487 if (max_size > 0 && avatar->len > max_size && !needs_conversion) {
488 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
489 "(max is %u bytes), conversion needed.",
490 avatar->len, max_size);
493 needs_conversion = TRUE;
496 /* If no conversion is needed, return the avatar */
497 if (!needs_conversion) {
498 g_free (new_format_name);
499 g_free (new_mime_type);
500 return empathy_avatar_ref (avatar);
504 GdkPixbuf *pixbuf_scaled = NULL;
506 gint new_width, new_height;
507 GError *error = NULL;
509 g_free (converted_image_data);
512 new_width = width * factor;
513 new_height = height * factor;
514 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
521 pixbuf_scaled = g_object_ref (pixbuf);
524 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
525 new_width, new_height, new_format_name);
527 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
528 &converted_image_data,
529 &converted_image_size,
534 g_free (new_format_name);
535 g_free (new_mime_type);
536 avatar_chooser_error_show (chooser,
537 _("Couldn't convert image"),
538 error ? error->message : NULL);
539 g_clear_error (&error);
543 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
544 converted_image_size);
549 /* Make a binary search for the bigest factor that produce
550 * an image data size less than max_size */
551 if (converted_image_size > max_size)
553 if (converted_image_size < max_size)
555 factor = (min_factor + max_factor)/2;
557 /* We are done if either:
558 * - min_factor == max_factor. That happens if we resized to
559 * the max required dimension and the produced data size is
560 * less than max_size.
561 * - The data size is close enough to max_size. Here we accept
562 * a difference of 1k.
564 } while (min_factor != max_factor &&
565 ABS (max_size - converted_image_size) > 1024);
566 g_free (new_format_name);
568 /* Takes ownership of new_mime_type and converted_image_data */
569 avatar = empathy_avatar_new (converted_image_data,
570 converted_image_size, new_mime_type, NULL);
576 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
578 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
581 if (priv->avatar == NULL) {
585 empathy_avatar_unref (priv->avatar);
588 image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
589 gtk_button_set_image (GTK_BUTTON (chooser), image);
590 g_signal_emit (chooser, signals[CHANGED], 0);
594 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
597 gboolean set_locally)
600 EmpathyAvatar *avatar = NULL;
601 gchar *mime_type = NULL;
604 avatar_chooser_clear_image (chooser);
608 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
609 if (pixbuf == NULL) {
614 /* avatar takes ownership of data and mime_type */
615 avatar = empathy_avatar_new (data, size, mime_type, NULL);
617 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
621 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
622 EmpathyAvatar *avatar,
623 gboolean set_locally)
626 gchar *mime_type = NULL;
628 g_assert (avatar != NULL);
630 pixbuf = empathy_pixbuf_from_data_and_mime (avatar->data,
633 if (pixbuf == NULL) {
634 DEBUG ("couldn't make a pixbuf from avatar; giving up");
638 if (avatar->format == NULL) {
639 avatar->format = mime_type;
641 if (strcmp (mime_type, avatar->format)) {
642 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
643 avatar->format, mime_type);
648 empathy_avatar_ref (avatar);
650 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
654 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
655 EmpathyAvatar *avatar,
657 gboolean set_locally)
659 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
660 GdkPixbuf *pixbuf_view;
663 g_assert (avatar != NULL);
664 g_assert (pixbuf != NULL);
669 conv = avatar_chooser_maybe_convert_and_scale (chooser,
671 empathy_avatar_unref (avatar);
674 /* An error occured; don't change the avatar. */
681 if (priv->avatar != NULL) {
682 empathy_avatar_unref (priv->avatar);
684 priv->avatar = avatar;
686 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
687 image = gtk_image_new_from_pixbuf (pixbuf_view);
689 gtk_button_set_image (GTK_BUTTON (chooser), image);
690 g_signal_emit (chooser, signals[CHANGED], 0);
692 g_object_unref (pixbuf_view);
693 g_object_unref (pixbuf);
697 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
698 const gchar *filename)
700 gchar *image_data = NULL;
701 gsize image_size = 0;
702 GError *error = NULL;
704 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
705 DEBUG ("Failed to load image from '%s': %s", filename,
706 error ? error->message : "No error given");
708 g_clear_error (&error);
712 avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
716 avatar_chooser_drag_motion_cb (GtkWidget *widget,
717 GdkDragContext *context,
721 EmpathyAvatarChooser *chooser)
723 EmpathyAvatarChooserPriv *priv;
726 priv = GET_PRIV (chooser);
728 for (p = context->targets; p != NULL; p = p->next) {
729 gchar *possible_type;
731 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
733 if (!strcmp (possible_type, URI_LIST_TYPE)) {
734 g_free (possible_type);
735 gdk_drag_status (context, GDK_ACTION_COPY, time);
740 g_free (possible_type);
747 avatar_chooser_drag_leave_cb (GtkWidget *widget,
748 GdkDragContext *context,
750 EmpathyAvatarChooser *chooser)
755 avatar_chooser_drag_drop_cb (GtkWidget *widget,
756 GdkDragContext *context,
760 EmpathyAvatarChooser *chooser)
762 EmpathyAvatarChooserPriv *priv;
765 priv = GET_PRIV (chooser);
767 if (context->targets == NULL) {
771 for (p = context->targets; p != NULL; p = p->next) {
774 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
775 if (!strcmp (possible_type, URI_LIST_TYPE)) {
776 g_free (possible_type);
777 gtk_drag_get_data (widget, context,
778 GDK_POINTER_TO_ATOM (p->data),
784 g_free (possible_type);
791 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
792 GdkDragContext *context,
795 GtkSelectionData *selection_data,
798 EmpathyAvatarChooser *chooser)
801 gboolean handled = FALSE;
803 target_type = gdk_atom_name (selection_data->target);
804 if (!strcmp (target_type, URI_LIST_TYPE)) {
806 GFileInputStream *input_stream;
810 nl = strstr (selection_data->data, "\r\n");
814 uri = g_strndup (selection_data->data,
815 nl - (gchar*) selection_data->data);
817 file = g_file_new_for_uri (uri);
820 file = g_file_new_for_uri (selection_data->data);
823 input_stream = g_file_read (file, NULL, NULL);
825 if (input_stream != NULL) {
828 info = g_file_query_info (file,
829 G_FILE_ATTRIBUTE_STANDARD_SIZE,
835 size = g_file_info_get_size (info);
836 data = g_malloc (size);
838 bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
841 if (bytes_read != -1) {
842 avatar_chooser_set_image_from_data (
850 g_object_unref (info);
853 g_object_unref (input_stream);
856 g_object_unref (file);
859 gtk_drag_finish (context, handled, FALSE, time);
863 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
864 EmpathyAvatarChooser *chooser)
868 filename = gtk_file_chooser_get_preview_filename (file_chooser);
872 GdkPixbuf *pixbuf = NULL;
873 GdkPixbuf *scaled_pixbuf;
875 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
877 image = gtk_file_chooser_get_preview_widget (file_chooser);
880 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
881 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
882 g_object_unref (scaled_pixbuf);
883 g_object_unref (pixbuf);
885 gtk_image_set_from_stock (GTK_IMAGE (image),
886 "gtk-dialog-question",
887 GTK_ICON_SIZE_DIALOG);
891 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
895 avatar_chooser_response_cb (GtkWidget *widget,
897 EmpathyAvatarChooser *chooser)
899 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
901 priv->chooser_dialog = NULL;
903 if (response == GTK_RESPONSE_CANCEL) {
907 /* Check if we went non-ready since displaying the dialog. */
908 if (!empathy_tp_contact_factory_is_ready (priv->tp_contact_factory)) {
909 DEBUG ("Can't set avatar when contact factory isn't ready.");
913 if (response == GTK_RESPONSE_OK) {
917 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
918 avatar_chooser_set_image_from_file (chooser, filename);
921 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
923 empathy_conf_set_string (empathy_conf_get (),
924 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
929 else if (response == GTK_RESPONSE_NO) {
930 /* This corresponds to "No Image", not to "Cancel" */
931 avatar_chooser_clear_image (chooser);
935 gtk_widget_destroy (widget);
939 avatar_chooser_clicked_cb (GtkWidget *button,
940 EmpathyAvatarChooser *chooser)
942 GtkFileChooser *chooser_dialog;
944 gchar *saved_dir = NULL;
945 const gchar *default_dir = DEFAULT_DIR;
946 const gchar *pics_dir;
947 GtkFileFilter *filter;
948 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
950 if (priv->chooser_dialog) {
951 gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
955 priv->chooser_dialog = GTK_FILE_CHOOSER (
956 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
957 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
958 GTK_FILE_CHOOSER_ACTION_OPEN,
966 chooser_dialog = priv->chooser_dialog;
967 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
969 /* Get special dirs */
970 empathy_conf_get_string (empathy_conf_get (),
971 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
973 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
977 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
980 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
981 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
985 /* Set current dir to the last one or to DEFAULT_DIR or to home */
987 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
990 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
992 else if (default_dir) {
993 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
995 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
998 /* Add shortcuts to special dirs */
1000 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
1002 else if (pics_dir) {
1003 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
1006 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
1009 /* Setup preview image */
1010 image = gtk_image_new ();
1011 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1012 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1013 gtk_widget_show (image);
1014 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1015 g_signal_connect (chooser_dialog, "update-preview",
1016 G_CALLBACK (avatar_chooser_update_preview_cb),
1020 filter = gtk_file_filter_new ();
1021 gtk_file_filter_set_name (filter, _("Images"));
1022 gtk_file_filter_add_pixbuf_formats (filter);
1023 gtk_file_chooser_add_filter (chooser_dialog, filter);
1024 filter = gtk_file_filter_new ();
1025 gtk_file_filter_set_name (filter, _("All Files"));
1026 gtk_file_filter_add_pattern(filter, "*");
1027 gtk_file_chooser_add_filter (chooser_dialog, filter);
1029 /* Setup response */
1030 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
1031 g_signal_connect (chooser_dialog, "response",
1032 G_CALLBACK (avatar_chooser_response_cb),
1035 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1040 empathy_avatar_chooser_new ()
1042 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1046 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
1047 EmpathyAvatar *avatar)
1049 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1051 if (avatar != NULL) {
1052 avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
1054 avatar_chooser_clear_image (chooser);
1059 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
1062 const gchar **mime_type)
1064 EmpathyAvatarChooserPriv *priv;
1066 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1068 priv = GET_PRIV (chooser);
1070 if (priv->avatar != NULL) {
1072 *data = priv->avatar->data;
1074 if (data_size != NULL) {
1075 *data_size = priv->avatar->len;
1077 if (mime_type != NULL) {
1078 *mime_type = priv->avatar->format;
1084 if (data_size != NULL) {
1087 if (mime_type != NULL) {