2 * Copyright (C) 2006-2007 Imendio AB.
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
19 * Authors: Based on Novell's e-image-chooser.
20 * Xavier Claessens <xclaesse@gmail.com>
27 #include <glib/gi18n-lib.h>
31 #include <libempathy/empathy-camera-monitor.h>
32 #include <libempathy/empathy-gsettings.h>
33 #include <libempathy/empathy-utils.h>
35 #include "empathy-avatar-chooser.h"
36 #include "empathy-images.h"
37 #include "empathy-ui-utils.h"
40 #include <cheese-avatar-chooser.h>
41 #endif /* HAVE_CHEESE */
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include <libempathy/empathy-debug.h>
48 * SECTION:empathy-avatar-chooser
49 * @title: EmpathyAvatarChooser
50 * @short_description: A widget used to change avatar
51 * @include: libempathy-gtk/empathy-avatar-chooser.h
53 * #EmpathyAvatarChooser is a widget which extends #GtkButton to
54 * provide a way of changing avatar.
58 * EmpathyAvatarChooser:
59 * @parent: parent object
61 * Widget which extends #GtkButton to provide a way of changing avatar.
64 #define AVATAR_SIZE_SAVE 96
65 #define AVATAR_SIZE_VIEW 64
66 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
70 * A custom GtkResponseType used when the user presses the
71 * "Camera Picture" button. Any positive value would be sufficient.
73 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM 10
75 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE GTK_RESPONSE_NO
76 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL GTK_RESPONSE_CANCEL
77 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE GTK_RESPONSE_OK
79 struct _EmpathyAvatarChooserPrivate
81 TpConnection *connection;
82 GtkFileChooser *chooser_dialog;
84 gulong ready_handler_id;
86 EmpathyAvatar *avatar;
87 GSettings *gsettings_ui;
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"
113 DND_TARGET_TYPE_URI_LIST
116 static const GtkTargetEntry drop_types[] =
118 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
122 avatar_chooser_get_property (GObject *object,
127 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
131 case PROP_CONNECTION:
132 g_value_set_object (value, self->priv->connection);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
141 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
142 TpConnection *connection)
144 tp_clear_object (&self->priv->connection);
146 if (connection != NULL)
147 self->priv->connection = g_object_ref (connection);
151 avatar_chooser_set_property (GObject *object,
156 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
160 case PROP_CONNECTION:
161 avatar_chooser_set_connection (self, g_value_get_object (value));
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
170 avatar_chooser_dispose (GObject *object)
172 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
174 tp_clear_object (&self->priv->connection);
175 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
176 tp_clear_object (&self->priv->gsettings_ui);
178 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->dispose (object);
182 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
184 GObjectClass *object_class = G_OBJECT_CLASS (klass);
185 GParamSpec *param_spec;
187 object_class->dispose = avatar_chooser_dispose;
188 object_class->get_property = avatar_chooser_get_property;
189 object_class->set_property = avatar_chooser_set_property;
192 * EmpathyAvatarChooser::changed:
193 * @self: an #EmpathyAvatarChooser
195 * Emitted when the chosen avatar has changed.
199 g_signal_new ("changed",
200 G_TYPE_FROM_CLASS (klass),
204 g_cclosure_marshal_generic,
208 * EmpathyAvatarChooser:connection:
210 * The #TpConnection whose avatar should be shown and modified by
211 * the #EmpathyAvatarChooser instance.
213 param_spec = g_param_spec_object ("connection",
215 "TpConnection whose avatar should be "
216 "shown and modified by this widget",
219 G_PARAM_STATIC_STRINGS);
220 g_object_class_install_property (object_class,
224 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPrivate));
228 avatar_chooser_drag_motion_cb (GtkWidget *widget,
229 GdkDragContext *context,
233 EmpathyAvatarChooser *self)
237 for (p = gdk_drag_context_list_targets (context); p != NULL;
240 gchar *possible_type;
242 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
244 if (!strcmp (possible_type, URI_LIST_TYPE))
246 g_free (possible_type);
247 gdk_drag_status (context, GDK_ACTION_COPY, time_);
252 g_free (possible_type);
259 avatar_chooser_drag_drop_cb (GtkWidget *widget,
260 GdkDragContext *context,
264 EmpathyAvatarChooser *self)
268 if (gdk_drag_context_list_targets (context) == NULL)
271 for (p = gdk_drag_context_list_targets (context);
272 p != NULL; p = p->next)
276 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
277 if (!strcmp (possible_type, URI_LIST_TYPE))
279 g_free (possible_type);
280 gtk_drag_get_data (widget, context,
281 GDK_POINTER_TO_ATOM (p->data),
287 g_free (possible_type);
294 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
298 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
300 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
301 GTK_ICON_SIZE_DIALOG);
302 gtk_button_set_image (GTK_BUTTON (self), image);
303 g_signal_emit (self, signals[CHANGED], 0);
307 str_in_strv (const gchar *str,
313 while (*strv != NULL)
315 if (g_str_equal (str, *strv))
324 /* The caller must free the strings stored in satisfactory_format_name and
325 * satisfactory_mime_type.
328 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
329 gchar **accepted_mime_types,
330 gchar **satisfactory_format_name,
331 gchar **satisfactory_mime_type)
333 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
336 gboolean found = FALSE;
338 *satisfactory_format_name = NULL;
339 *satisfactory_mime_type = NULL;
341 /* If there is no accepted format there is nothing we can do */
342 if (accepted_mime_types == NULL || *accepted_mime_types == NULL)
345 /* If the current mime type is good and accepted, don't change it!
346 * jpeg is compress better pictures, but png is better for logos and
347 * could have an alpha layer. */
348 if (str_in_strv (current_mime_type, good_mime_types) &&
349 str_in_strv (current_mime_type, accepted_mime_types))
351 *satisfactory_mime_type = g_strdup (current_mime_type);
352 *satisfactory_format_name = g_strdup (current_mime_type +
357 /* The current mime type is either not accepted or not good to use.
358 * Check if one of the good format is supported... */
359 for (i = 0; good_mime_types[i] != NULL; i++)
361 if (str_in_strv (good_mime_types[i], accepted_mime_types))
363 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
364 *satisfactory_format_name = g_strdup (good_mime_types[i] +
370 /* Pick the first supported format we can write */
371 formats = gdk_pixbuf_get_formats ();
372 for (l = formats; !found && l != NULL; l = l->next)
374 GdkPixbufFormat *format = l->data;
375 gchar **format_mime_types;
378 if (!gdk_pixbuf_format_is_writable (format))
381 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
382 for (iter = format_mime_types; *iter != NULL; iter++)
384 if (str_in_strv (*iter, accepted_mime_types))
386 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
387 *satisfactory_mime_type = g_strdup (*iter);
392 g_strfreev (format_mime_types);
394 g_slist_free (formats);
400 avatar_chooser_error_show (EmpathyAvatarChooser *self,
401 const gchar *primary_text,
402 const gchar *secondary_text)
407 parent = gtk_widget_get_toplevel (GTK_WIDGET (self));
408 if (!GTK_IS_WINDOW (parent))
411 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
417 if (secondary_text != NULL)
419 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
420 "%s", secondary_text);
423 g_signal_connect (dialog, "response",
424 G_CALLBACK (gtk_widget_destroy), NULL);
425 gtk_widget_show (dialog);
429 static EmpathyAvatar *
430 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
432 EmpathyAvatar *avatar)
434 TpAvatarRequirements *req;
435 gboolean needs_conversion = FALSE;
437 gchar *new_format_name = NULL;
438 gchar *new_mime_type = NULL;
439 gdouble min_factor, max_factor;
441 gchar *best_image_data = NULL;
442 gsize best_image_size = 0;
445 req = tp_connection_get_avatar_requirements (self->priv->connection);
448 DEBUG ("Avatar requirements not ready");
452 /* Smaller is the factor, smaller will be the image.
453 * 0 is an empty image, 1 is the full size. */
458 /* Check if we need to convert to another image format */
459 if (avatar_chooser_need_mime_type_conversion (avatar->format,
460 req->supported_mime_types, &new_format_name, &new_mime_type))
462 DEBUG ("Format conversion needed, we'll use mime type '%s' "
463 "and format name '%s'. Current mime type is '%s'",
464 new_mime_type, new_format_name, avatar->format);
465 needs_conversion = TRUE;
468 /* If there is no format we can use, report error to the user. */
469 if (new_mime_type == NULL || new_format_name == NULL)
471 avatar_chooser_error_show (self, _("Couldn't convert image"),
472 _("None of the accepted image formats are "
473 "supported on your system"));
477 /* If width or height are too big, it needs converting. */
478 width = gdk_pixbuf_get_width (pixbuf);
479 height = gdk_pixbuf_get_height (pixbuf);
480 if ((req->maximum_width > 0 && width > req->maximum_width) ||
481 (req->maximum_height > 0 && height > req->maximum_height))
483 gdouble h_factor, v_factor;
485 h_factor = (gdouble) req->maximum_width / width;
486 v_factor = (gdouble) req->maximum_height / height;
487 factor = max_factor = MIN (h_factor, v_factor);
489 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
490 width, height, req->maximum_width, req->maximum_height);
492 needs_conversion = TRUE;
495 /* If the data len is too big and no other conversion is needed,
496 * try with a lower factor. */
497 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes &&
500 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
501 "(max is %u bytes), conversion needed.",
502 avatar->len, req->maximum_bytes);
505 needs_conversion = TRUE;
508 /* If no conversion is needed, return the avatar */
509 if (!needs_conversion)
511 g_free (new_format_name);
512 g_free (new_mime_type);
513 return empathy_avatar_ref (avatar);
518 GdkPixbuf *pixbuf_scaled = NULL;
520 gint new_width, new_height;
521 gchar *converted_image_data;
522 gsize converted_image_size;
523 GError *error = NULL;
527 new_width = width * factor;
528 new_height = height * factor;
529 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
538 pixbuf_scaled = g_object_ref (pixbuf);
541 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
542 new_width, new_height, new_format_name);
544 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
545 &converted_image_data,
546 &converted_image_size,
549 g_object_unref (pixbuf_scaled);
553 g_free (new_format_name);
554 g_free (new_mime_type);
555 avatar_chooser_error_show (self,
556 _("Couldn't convert image"),
557 error ? error->message : NULL);
558 g_clear_error (&error);
562 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
563 converted_image_size);
565 /* If the new image satisfy the req, keep it as current best */
566 if (req->maximum_bytes == 0 || converted_image_size <= req->maximum_bytes)
568 g_free (best_image_data);
570 best_image_data = converted_image_data;
571 best_image_size = converted_image_size;
573 /* If this image is close enough to the optimal size,
575 if (req->maximum_bytes == 0 ||
576 req->maximum_bytes - converted_image_size <= 1024)
581 g_free (converted_image_data);
584 /* Make a binary search for the bigest factor that produce
585 * an image data size less than max_size */
586 if (converted_image_size > req->maximum_bytes)
588 if (converted_image_size < req->maximum_bytes)
590 factor = (min_factor + max_factor)/2;
592 if ((int) (width * factor) == new_width ||
593 (int) (height * factor) == new_height)
595 /* min_factor and max_factor are too close, so the new
596 * factor will produce the same image as previous
597 * iteration. No need to continue, we already found
598 * the optimal size. */
602 /* Do 10 iterations in the worst case */
603 } while (++count < 10);
605 g_free (new_format_name);
607 avatar = empathy_avatar_new ((guchar *) best_image_data,
608 best_image_size, new_mime_type, NULL);
610 g_free (best_image_data);
611 g_free (new_mime_type);
617 avatar_chooser_set_image (EmpathyAvatarChooser *self,
618 EmpathyAvatar *avatar,
620 gboolean set_locally)
622 GdkPixbuf *pixbuf_view;
625 g_assert (avatar != NULL);
626 g_assert (pixbuf != NULL);
632 conv = avatar_chooser_maybe_convert_and_scale (self,
634 empathy_avatar_unref (avatar);
637 /* An error occured; don't change the avatar. */
643 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
644 self->priv->avatar = avatar;
646 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf,
648 image = gtk_image_new_from_pixbuf (pixbuf_view);
650 gtk_button_set_image (GTK_BUTTON (self), image);
651 g_signal_emit (self, signals[CHANGED], 0);
653 g_object_unref (pixbuf_view);
654 g_object_unref (pixbuf);
657 /* takes ownership of @data */
659 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
662 gboolean set_locally)
665 EmpathyAvatar *avatar = NULL;
666 gchar *mime_type = NULL;
670 avatar_chooser_clear_image (self);
674 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
682 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
684 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
691 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
692 GdkDragContext *context,
695 GtkSelectionData *selection_data,
698 EmpathyAvatarChooser *self)
701 gboolean handled = FALSE;
703 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
704 if (!strcmp (target_type, URI_LIST_TYPE))
711 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
718 (gchar *) gtk_selection_data_get_data (selection_data),
719 nl - (gchar *) gtk_selection_data_get_data (selection_data));
721 file = g_file_new_for_uri (uri);
726 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
730 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
735 /* pass data to the avatar_chooser_set_image_from_data */
736 avatar_chooser_set_image_from_data (self, data, bytes_read, TRUE);
739 g_object_unref (file);
742 gtk_drag_finish (context, handled, FALSE, time_);
746 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
747 EmpathyAvatarChooser *self)
751 filename = gtk_file_chooser_get_preview_filename (file_chooser);
753 if (filename != NULL)
756 GdkPixbuf *pixbuf = NULL;
757 GdkPixbuf *scaled_pixbuf;
759 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
761 image = gtk_file_chooser_get_preview_widget (file_chooser);
765 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf,
768 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
769 g_object_unref (scaled_pixbuf);
770 g_object_unref (pixbuf);
774 gtk_image_set_from_stock (GTK_IMAGE (image),
775 "gtk-dialog-question",
776 GTK_ICON_SIZE_DIALOG);
782 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
786 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
787 const gchar *filename)
789 gchar *image_data = NULL;
790 gsize image_size = 0;
791 GError *error = NULL;
793 if (!g_file_get_contents (filename, &image_data, &image_size, &error))
795 DEBUG ("Failed to load image from '%s': %s", filename,
796 error ? error->message : "No error given");
798 g_clear_error (&error);
802 /* pass image_data to the avatar_chooser_set_image_from_data */
803 avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
808 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
813 EmpathyAvatar *avatar = NULL;
814 GError *error = NULL;
816 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL))
818 avatar_chooser_error_show (self,
819 _("Couldn't save picture to file"),
820 error ? error->message : NULL);
821 g_clear_error (&error);
825 avatar = empathy_avatar_new ((guchar *) buf, size, "image/png", NULL);
826 avatar_chooser_set_image (self, avatar, pb, TRUE);
832 destroy_chooser (GtkWidget *self)
834 gtk_widget_destroy (self);
839 webcam_response_cb (GtkDialog *dialog,
841 EmpathyAvatarChooser *self)
843 if (response == GTK_RESPONSE_ACCEPT)
846 CheeseAvatarChooser *cheese_chooser;
848 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
849 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
850 avatar_chooser_set_avatar_from_pixbuf (self, pb);
853 if (response != GTK_RESPONSE_DELETE_EVENT &&
854 response != GTK_RESPONSE_NONE)
855 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
859 choose_avatar_from_webcam (GtkWidget *widget,
860 EmpathyAvatarChooser *self)
864 window = cheese_avatar_chooser_new ();
866 gtk_window_set_transient_for (GTK_WINDOW (window),
867 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
868 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
869 g_signal_connect (G_OBJECT (window), "response",
870 G_CALLBACK (webcam_response_cb), self);
871 gtk_widget_show (window);
873 #endif /* HAVE_CHEESE */
876 avatar_chooser_response_cb (GtkWidget *widget,
878 EmpathyAvatarChooser *self)
880 self->priv->chooser_dialog = NULL;
882 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE)
887 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
888 avatar_chooser_set_image_from_file (self, filename);
891 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
894 g_settings_set_string (self->priv->gsettings_ui,
895 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
901 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE)
903 /* This corresponds to "No Image", not to "Cancel" */
904 avatar_chooser_clear_image (self);
907 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM)
909 /* This corresponds to "Camera Picture" */
910 choose_avatar_from_webcam (widget, self);
914 gtk_widget_destroy (widget);
918 avatar_chooser_clicked_cb (GtkWidget *button,
919 EmpathyAvatarChooser *self)
921 GtkFileChooser *chooser_dialog;
923 gchar *saved_dir = NULL;
924 const gchar *default_dir = DEFAULT_DIR;
925 const gchar *pics_dir;
926 GtkFileFilter *filter;
928 GtkWidget *picture_button;
929 EmpathyCameraMonitor *monitor;
932 if (self->priv->chooser_dialog != NULL)
934 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
938 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
939 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
940 empathy_get_toplevel_window (GTK_WIDGET (self)),
941 GTK_FILE_CHOOSER_ACTION_OPEN,
945 picture_button = gtk_dialog_add_button (
946 GTK_DIALOG (self->priv->chooser_dialog),
947 _("Take a picture..."), EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM);
949 /* Button is sensitive only if there is one camera connected */
950 monitor = empathy_camera_monitor_dup_singleton ();
952 g_object_set_data_full (G_OBJECT (picture_button),
953 "monitor", monitor, g_object_unref);
955 g_object_bind_property (monitor, "available", picture_button, "sensitive",
956 G_BINDING_SYNC_CREATE);
959 gtk_dialog_add_buttons (GTK_DIALOG (self->priv->chooser_dialog),
960 _("No Image"), EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
961 GTK_STOCK_CANCEL, EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
962 GTK_STOCK_OPEN, EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
965 chooser_dialog = self->priv->chooser_dialog;
966 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
968 /* Get special dirs */
969 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
970 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
972 if (saved_dir != NULL &&
973 !g_file_test (saved_dir, G_FILE_TEST_IS_DIR))
979 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR))
982 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
983 if (pics_dir != NULL && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR))
986 /* Set current dir to the last one or to DEFAULT_DIR or to home */
987 if (saved_dir != NULL)
988 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
989 else if (pics_dir != NULL)
990 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
991 else if (default_dir != NULL)
992 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
994 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
996 /* Add shortcuts to special dirs */
998 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
1000 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
1002 if (default_dir != NULL)
1003 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
1005 /* Setup preview image */
1006 image = gtk_image_new ();
1007 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1008 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1009 gtk_widget_show (image);
1010 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1011 g_signal_connect (chooser_dialog, "update-preview",
1012 G_CALLBACK (avatar_chooser_update_preview_cb),
1016 filter = gtk_file_filter_new ();
1017 gtk_file_filter_set_name (filter, _("Images"));
1018 gtk_file_filter_add_pixbuf_formats (filter);
1019 gtk_file_chooser_add_filter (chooser_dialog, filter);
1020 filter = gtk_file_filter_new ();
1021 gtk_file_filter_set_name (filter, _("All Files"));
1022 gtk_file_filter_add_pattern (filter, "*");
1023 gtk_file_chooser_add_filter (chooser_dialog, filter);
1025 /* Setup response */
1026 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog),
1027 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1029 g_signal_connect (chooser_dialog, "response",
1030 G_CALLBACK (avatar_chooser_response_cb),
1033 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1039 empathy_avatar_chooser_init (EmpathyAvatarChooser *self)
1041 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1042 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPrivate);
1044 gtk_drag_dest_set (GTK_WIDGET (self),
1045 GTK_DEST_DEFAULT_ALL,
1047 G_N_ELEMENTS (drop_types),
1050 self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1052 g_signal_connect (self, "drag-motion",
1053 G_CALLBACK (avatar_chooser_drag_motion_cb),
1055 g_signal_connect (self, "drag-drop",
1056 G_CALLBACK (avatar_chooser_drag_drop_cb),
1058 g_signal_connect (self, "drag-data-received",
1059 G_CALLBACK (avatar_chooser_drag_data_received_cb),
1061 g_signal_connect (self, "clicked",
1062 G_CALLBACK (avatar_chooser_clicked_cb),
1065 empathy_avatar_chooser_set (self, NULL);
1069 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *self,
1070 EmpathyAvatar *avatar,
1071 gboolean set_locally)
1074 gchar *mime_type = NULL;
1076 g_assert (avatar != NULL);
1078 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
1079 avatar->len, &mime_type);
1083 DEBUG ("couldn't make a pixbuf from avatar; giving up");
1087 if (avatar->format == NULL)
1089 avatar->format = mime_type;
1093 if (strcmp (mime_type, avatar->format))
1094 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
1095 avatar->format, mime_type);
1100 empathy_avatar_ref (avatar);
1102 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
1106 * empathy_avatar_chooser_new:
1108 * Creates a new #EmpathyAvatarChooser.
1110 * Return value: a new #EmpathyAvatarChooser
1113 empathy_avatar_chooser_new (void)
1115 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1119 * empathy_avatar_chooser_set:
1120 * @self: an #EmpathyAvatarChooser
1121 * @avatar: a new #EmpathyAvatar
1123 * Sets the @self to display the avatar indicated by @avatar.
1126 empathy_avatar_chooser_set (EmpathyAvatarChooser *self,
1127 EmpathyAvatar *avatar)
1129 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1132 avatar_chooser_set_image_from_avatar (self, avatar, FALSE);
1134 avatar_chooser_clear_image (self);
1138 * empathy_avatar_chooser_get_image_data:
1139 * @self: an #EmpathyAvatarChooser
1140 * @data: avatar bytes
1141 * @data_size: size of @data
1142 * @mime_type: avatar mime-type
1144 * Gets image data about the currently selected avatar.
1147 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *self,
1150 const gchar **mime_type)
1152 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1154 if (self->priv->avatar != NULL)
1157 *data = (gchar *) self->priv->avatar->data;
1159 if (data_size != NULL)
1160 *data_size = self->priv->avatar->len;
1162 if (mime_type != NULL)
1163 *mime_type = self->priv->avatar->format;
1170 if (data_size != NULL)
1173 if (mime_type != NULL)
1179 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1182 g_return_if_fail (account != NULL);
1184 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1185 g_object_notify (G_OBJECT (self), "connection");