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-tp-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 EmpathyTpContactFactory *factory;
49 TpConnection *connection;
50 GtkFileChooser *chooser_dialog;
52 gulong ready_handler_id;
54 EmpathyAvatar *avatar;
55 } EmpathyAvatarChooserPriv;
57 static void avatar_chooser_finalize (GObject *object);
58 static void avatar_chooser_set_connection (EmpathyAvatarChooser *self,
59 TpConnection *connection);
60 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
61 EmpathyAvatar *avatar,
63 gboolean set_locally);
64 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
65 GdkDragContext *context,
69 EmpathyAvatarChooser *chooser);
70 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
71 GdkDragContext *context,
73 EmpathyAvatarChooser *chooser);
74 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
75 GdkDragContext *context,
79 EmpathyAvatarChooser *chooser);
80 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
81 GdkDragContext *context,
84 GtkSelectionData *selection_data,
87 EmpathyAvatarChooser *chooser);
88 static void avatar_chooser_clicked_cb (GtkWidget *button,
89 EmpathyAvatarChooser *chooser);
101 static guint signals [LAST_SIGNAL];
103 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
106 * Drag and drop stuff
108 #define URI_LIST_TYPE "text/uri-list"
111 DND_TARGET_TYPE_URI_LIST
114 static const GtkTargetEntry drop_types[] = {
115 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
119 avatar_chooser_get_property (GObject *object,
124 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
127 case PROP_CONNECTION:
128 g_value_set_object (value, priv->connection);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
137 avatar_chooser_set_property (GObject *object,
142 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
145 case PROP_CONNECTION:
146 avatar_chooser_set_connection (self, g_value_get_object (value));
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
155 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
157 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158 GParamSpec *param_spec;
160 object_class->finalize = avatar_chooser_finalize;
161 object_class->get_property = avatar_chooser_get_property;
162 object_class->set_property = avatar_chooser_set_property;
165 g_signal_new ("changed",
166 G_TYPE_FROM_CLASS (klass),
170 g_cclosure_marshal_VOID__VOID,
173 param_spec = g_param_spec_object ("connection",
175 "TpConnection whose avatar should be "
176 "shown and modified by this widget",
179 G_PARAM_STATIC_STRINGS);
180 g_object_class_install_property (object_class,
184 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
188 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
190 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
191 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
193 chooser->priv = priv;
194 gtk_drag_dest_set (GTK_WIDGET (chooser),
195 GTK_DEST_DEFAULT_ALL,
197 G_N_ELEMENTS (drop_types),
200 g_signal_connect (chooser, "drag-motion",
201 G_CALLBACK (avatar_chooser_drag_motion_cb),
203 g_signal_connect (chooser, "drag-leave",
204 G_CALLBACK (avatar_chooser_drag_leave_cb),
206 g_signal_connect (chooser, "drag-drop",
207 G_CALLBACK (avatar_chooser_drag_drop_cb),
209 g_signal_connect (chooser, "drag-data-received",
210 G_CALLBACK (avatar_chooser_drag_data_received_cb),
212 g_signal_connect (chooser, "clicked",
213 G_CALLBACK (avatar_chooser_clicked_cb),
216 empathy_avatar_chooser_set (chooser, NULL);
220 avatar_chooser_finalize (GObject *object)
222 EmpathyAvatarChooserPriv *priv;
224 priv = GET_PRIV (object);
226 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
227 g_assert (priv->connection == NULL);
228 g_assert (priv->factory == NULL);
230 if (priv->avatar != NULL) {
231 empathy_avatar_unref (priv->avatar);
234 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
238 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
239 TpConnection *connection)
241 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
243 if (priv->connection != NULL) {
244 g_object_unref (priv->connection);
245 priv->connection = NULL;
247 g_object_unref (priv->factory);
248 priv->factory = NULL;
251 if (connection != NULL) {
252 priv->connection = g_object_ref (connection);
253 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
258 avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
259 const gchar *primary_text,
260 const gchar *secondary_text)
265 parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
266 if (!GTK_IS_WINDOW (parent)) {
270 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
276 if (secondary_text != NULL) {
277 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
278 "%s", secondary_text);
281 g_signal_connect (dialog, "response",
282 G_CALLBACK (gtk_widget_destroy), NULL);
283 gtk_widget_show (dialog);
288 str_in_strv (const gchar *str,
295 while (*strv != NULL) {
296 if (g_str_equal (str, *strv)) {
304 /* The caller must free the strings stored in satisfactory_format_name and
305 * satisfactory_mime_type.
308 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
309 gchar **accepted_mime_types,
310 gchar **satisfactory_format_name,
311 gchar **satisfactory_mime_type)
313 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
316 gboolean found = FALSE;
318 *satisfactory_format_name = NULL;
319 *satisfactory_mime_type = NULL;
321 /* If there is no accepted format there is nothing we can do */
322 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
326 /* If the current mime type is good and accepted, don't change it!
327 * jpeg is compress better pictures, but png is better for logos and
328 * could have an alpha layer. */
329 if (str_in_strv (current_mime_type, good_mime_types) &&
330 str_in_strv (current_mime_type, accepted_mime_types)) {
331 *satisfactory_mime_type = g_strdup (current_mime_type);
332 *satisfactory_format_name = g_strdup (current_mime_type +
337 /* The current mime type is either not accepted or not good to use.
338 * Check if one of the good format is supported... */
339 for (i = 0; good_mime_types[i] != NULL; i++) {
340 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
341 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
342 *satisfactory_format_name = g_strdup (good_mime_types[i] +
348 /* Pick the first supported format we can write */
349 formats = gdk_pixbuf_get_formats ();
350 for (l = formats; !found && l != NULL; l = l->next) {
351 GdkPixbufFormat *format = l->data;
352 gchar **format_mime_types;
355 if (!gdk_pixbuf_format_is_writable (format)) {
359 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
360 for (iter = format_mime_types; *iter != NULL; iter++) {
361 if (str_in_strv (*iter, accepted_mime_types)) {
362 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
363 *satisfactory_mime_type = g_strdup (*iter);
368 g_strfreev (format_mime_types);
370 g_slist_free (formats);
375 static EmpathyAvatar *
376 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
378 EmpathyAvatar *avatar)
380 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
381 guint max_width = 0, max_height = 0, max_size = 0;
382 gchar **mime_types = NULL;
383 gboolean needs_conversion = FALSE;
385 gchar *new_format_name = NULL;
386 gchar *new_mime_type = NULL;
387 gdouble min_factor, max_factor;
389 gchar *converted_image_data = NULL;
390 gsize converted_image_size = 0;
392 g_object_get (priv->factory,
393 "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
394 "avatar-max-width", &max_width,
395 "avatar-max-height", &max_height,
396 "avatar-max-size", &max_size,
399 /* Smaller is the factor, smaller will be the image.
400 * 0 is an empty image, 1 is the full size. */
405 /* Check if we need to convert to another image format */
406 if (avatar_chooser_need_mime_type_conversion (avatar->format,
410 DEBUG ("Format conversion needed, we'll use mime type '%s' "
411 "and format name '%s'. Current mime type is '%s'",
412 new_mime_type, new_format_name, avatar->format);
413 needs_conversion = TRUE;
415 g_strfreev (mime_types);
417 /* If there is no format we can use, report error to the user. */
418 if (new_mime_type == NULL || new_format_name == NULL) {
419 avatar_chooser_error_show (chooser, _("Couldn't convert image"),
420 _("None of the accepted image formats is "
421 "supported on your system"));
425 /* If width or height are too big, it needs converting. */
426 width = gdk_pixbuf_get_width (pixbuf);
427 height = gdk_pixbuf_get_height (pixbuf);
428 if ((max_width > 0 && width > max_width) ||
429 (max_height > 0 && height > max_height)) {
430 gdouble h_factor, v_factor;
432 h_factor = (gdouble) max_width / width;
433 v_factor = (gdouble) max_height / height;
434 factor = max_factor = MIN (h_factor, v_factor);
436 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
437 width, height, max_width, max_height);
439 needs_conversion = TRUE;
442 /* If the data len is too big and no other conversion is needed,
443 * try with a lower factor. */
444 if (max_size > 0 && avatar->len > max_size && !needs_conversion) {
445 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
446 "(max is %u bytes), conversion needed.",
447 avatar->len, max_size);
450 needs_conversion = TRUE;
453 /* If no conversion is needed, return the avatar */
454 if (!needs_conversion) {
455 g_free (new_format_name);
456 g_free (new_mime_type);
457 return empathy_avatar_ref (avatar);
461 GdkPixbuf *pixbuf_scaled = NULL;
463 gint new_width, new_height;
464 GError *error = NULL;
466 g_free (converted_image_data);
469 new_width = width * factor;
470 new_height = height * factor;
471 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
478 pixbuf_scaled = g_object_ref (pixbuf);
481 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
482 new_width, new_height, new_format_name);
484 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
485 &converted_image_data,
486 &converted_image_size,
489 g_object_unref (pixbuf_scaled);
492 g_free (new_format_name);
493 g_free (new_mime_type);
494 avatar_chooser_error_show (chooser,
495 _("Couldn't convert image"),
496 error ? error->message : NULL);
497 g_clear_error (&error);
501 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
502 converted_image_size);
507 /* Make a binary search for the bigest factor that produce
508 * an image data size less than max_size */
509 if (converted_image_size > max_size)
511 if (converted_image_size < max_size)
513 factor = (min_factor + max_factor)/2;
515 /* We are done if either:
516 * - min_factor == max_factor. That happens if we resized to
517 * the max required dimension and the produced data size is
518 * less than max_size.
519 * - The data size is close enough to max_size. Here we accept
520 * a difference of 1k.
522 } while (min_factor != max_factor &&
523 ABS (max_size - converted_image_size) > 1024);
524 g_free (new_format_name);
526 /* Takes ownership of new_mime_type and converted_image_data */
527 avatar = empathy_avatar_new (converted_image_data,
528 converted_image_size, new_mime_type, NULL);
534 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
536 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
539 if (priv->avatar != NULL) {
540 empathy_avatar_unref (priv->avatar);
544 image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
545 gtk_button_set_image (GTK_BUTTON (chooser), image);
546 g_signal_emit (chooser, signals[CHANGED], 0);
550 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
553 gboolean set_locally)
556 EmpathyAvatar *avatar = NULL;
557 gchar *mime_type = NULL;
560 avatar_chooser_clear_image (chooser);
564 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
565 if (pixbuf == NULL) {
570 /* avatar takes ownership of data and mime_type */
571 avatar = empathy_avatar_new (data, size, mime_type, NULL);
573 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
577 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
578 EmpathyAvatar *avatar,
579 gboolean set_locally)
582 gchar *mime_type = NULL;
584 g_assert (avatar != NULL);
586 pixbuf = empathy_pixbuf_from_data_and_mime (avatar->data,
589 if (pixbuf == NULL) {
590 DEBUG ("couldn't make a pixbuf from avatar; giving up");
594 if (avatar->format == NULL) {
595 avatar->format = mime_type;
597 if (strcmp (mime_type, avatar->format)) {
598 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
599 avatar->format, mime_type);
604 empathy_avatar_ref (avatar);
606 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
610 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
611 EmpathyAvatar *avatar,
613 gboolean set_locally)
615 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
616 GdkPixbuf *pixbuf_view;
619 g_assert (avatar != NULL);
620 g_assert (pixbuf != NULL);
625 conv = avatar_chooser_maybe_convert_and_scale (chooser,
627 empathy_avatar_unref (avatar);
630 /* An error occured; don't change the avatar. */
637 if (priv->avatar != NULL) {
638 empathy_avatar_unref (priv->avatar);
640 priv->avatar = avatar;
642 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
643 image = gtk_image_new_from_pixbuf (pixbuf_view);
645 gtk_button_set_image (GTK_BUTTON (chooser), image);
646 g_signal_emit (chooser, signals[CHANGED], 0);
648 g_object_unref (pixbuf_view);
649 g_object_unref (pixbuf);
653 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
654 const gchar *filename)
656 gchar *image_data = NULL;
657 gsize image_size = 0;
658 GError *error = NULL;
660 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
661 DEBUG ("Failed to load image from '%s': %s", filename,
662 error ? error->message : "No error given");
664 g_clear_error (&error);
668 avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
672 avatar_chooser_drag_motion_cb (GtkWidget *widget,
673 GdkDragContext *context,
677 EmpathyAvatarChooser *chooser)
679 EmpathyAvatarChooserPriv *priv;
682 priv = GET_PRIV (chooser);
684 for (p = context->targets; p != NULL; p = p->next) {
685 gchar *possible_type;
687 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
689 if (!strcmp (possible_type, URI_LIST_TYPE)) {
690 g_free (possible_type);
691 gdk_drag_status (context, GDK_ACTION_COPY, time);
696 g_free (possible_type);
703 avatar_chooser_drag_leave_cb (GtkWidget *widget,
704 GdkDragContext *context,
706 EmpathyAvatarChooser *chooser)
711 avatar_chooser_drag_drop_cb (GtkWidget *widget,
712 GdkDragContext *context,
716 EmpathyAvatarChooser *chooser)
718 EmpathyAvatarChooserPriv *priv;
721 priv = GET_PRIV (chooser);
723 if (context->targets == NULL) {
727 for (p = context->targets; p != NULL; p = p->next) {
730 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
731 if (!strcmp (possible_type, URI_LIST_TYPE)) {
732 g_free (possible_type);
733 gtk_drag_get_data (widget, context,
734 GDK_POINTER_TO_ATOM (p->data),
740 g_free (possible_type);
747 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
748 GdkDragContext *context,
751 GtkSelectionData *selection_data,
754 EmpathyAvatarChooser *chooser)
757 gboolean handled = FALSE;
759 target_type = gdk_atom_name (selection_data->target);
760 if (!strcmp (target_type, URI_LIST_TYPE)) {
762 GFileInputStream *input_stream;
766 nl = strstr (selection_data->data, "\r\n");
770 uri = g_strndup (selection_data->data,
771 nl - (gchar*) selection_data->data);
773 file = g_file_new_for_uri (uri);
776 file = g_file_new_for_uri (selection_data->data);
779 input_stream = g_file_read (file, NULL, NULL);
781 if (input_stream != NULL) {
784 info = g_file_query_info (file,
785 G_FILE_ATTRIBUTE_STANDARD_SIZE,
791 size = g_file_info_get_size (info);
792 data = g_malloc (size);
794 bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
797 if (bytes_read != -1) {
798 avatar_chooser_set_image_from_data (
806 g_object_unref (info);
809 g_object_unref (input_stream);
812 g_object_unref (file);
815 gtk_drag_finish (context, handled, FALSE, time);
819 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
820 EmpathyAvatarChooser *chooser)
824 filename = gtk_file_chooser_get_preview_filename (file_chooser);
828 GdkPixbuf *pixbuf = NULL;
829 GdkPixbuf *scaled_pixbuf;
831 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
833 image = gtk_file_chooser_get_preview_widget (file_chooser);
836 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
837 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
838 g_object_unref (scaled_pixbuf);
839 g_object_unref (pixbuf);
841 gtk_image_set_from_stock (GTK_IMAGE (image),
842 "gtk-dialog-question",
843 GTK_ICON_SIZE_DIALOG);
849 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
853 avatar_chooser_response_cb (GtkWidget *widget,
855 EmpathyAvatarChooser *chooser)
857 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
859 priv->chooser_dialog = NULL;
861 if (response == GTK_RESPONSE_OK) {
865 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
866 avatar_chooser_set_image_from_file (chooser, filename);
869 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
871 empathy_conf_set_string (empathy_conf_get (),
872 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
877 else if (response == GTK_RESPONSE_NO) {
878 /* This corresponds to "No Image", not to "Cancel" */
879 avatar_chooser_clear_image (chooser);
882 gtk_widget_destroy (widget);
886 avatar_chooser_clicked_cb (GtkWidget *button,
887 EmpathyAvatarChooser *chooser)
889 GtkFileChooser *chooser_dialog;
891 gchar *saved_dir = NULL;
892 const gchar *default_dir = DEFAULT_DIR;
893 const gchar *pics_dir;
894 GtkFileFilter *filter;
895 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
897 if (priv->chooser_dialog) {
898 gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
902 priv->chooser_dialog = GTK_FILE_CHOOSER (
903 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
904 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
905 GTK_FILE_CHOOSER_ACTION_OPEN,
913 chooser_dialog = priv->chooser_dialog;
914 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
916 /* Get special dirs */
917 empathy_conf_get_string (empathy_conf_get (),
918 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
920 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
924 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
927 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
928 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
932 /* Set current dir to the last one or to DEFAULT_DIR or to home */
934 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
937 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
939 else if (default_dir) {
940 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
942 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
945 /* Add shortcuts to special dirs */
947 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
950 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
953 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
956 /* Setup preview image */
957 image = gtk_image_new ();
958 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
959 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
960 gtk_widget_show (image);
961 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
962 g_signal_connect (chooser_dialog, "update-preview",
963 G_CALLBACK (avatar_chooser_update_preview_cb),
967 filter = gtk_file_filter_new ();
968 gtk_file_filter_set_name (filter, _("Images"));
969 gtk_file_filter_add_pixbuf_formats (filter);
970 gtk_file_chooser_add_filter (chooser_dialog, filter);
971 filter = gtk_file_filter_new ();
972 gtk_file_filter_set_name (filter, _("All Files"));
973 gtk_file_filter_add_pattern(filter, "*");
974 gtk_file_chooser_add_filter (chooser_dialog, filter);
977 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
978 g_signal_connect (chooser_dialog, "response",
979 G_CALLBACK (avatar_chooser_response_cb),
982 gtk_widget_show (GTK_WIDGET (chooser_dialog));
987 empathy_avatar_chooser_new ()
989 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
993 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
994 EmpathyAvatar *avatar)
996 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
998 if (avatar != NULL) {
999 avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
1001 avatar_chooser_clear_image (chooser);
1006 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
1009 const gchar **mime_type)
1011 EmpathyAvatarChooserPriv *priv;
1013 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1015 priv = GET_PRIV (chooser);
1017 if (priv->avatar != NULL) {
1019 *data = priv->avatar->data;
1021 if (data_size != NULL) {
1022 *data_size = priv->avatar->len;
1024 if (mime_type != NULL) {
1025 *mime_type = priv->avatar->format;
1031 if (data_size != NULL) {
1034 if (mime_type != NULL) {