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-gsettings.h>
32 #include <libempathy/empathy-utils.h>
34 #include "empathy-avatar-chooser.h"
35 #include "empathy-images.h"
36 #include "empathy-ui-utils.h"
39 #include <cheese-avatar-chooser.h>
40 #endif /* HAVE_CHEESE */
43 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
44 #include <libempathy/empathy-debug.h>
47 * SECTION:empathy-avatar-chooser
48 * @title: EmpathyAvatarChooser
49 * @short_description: A widget used to change avatar
50 * @include: libempathy-gtk/empathy-avatar-chooser.h
52 * #EmpathyAvatarChooser is a widget which extends #GtkButton to
53 * provide a way of changing avatar.
57 * EmpathyAvatarChooser:
58 * @parent: parent object
60 * Widget which extends #GtkButton to provide a way of changing avatar.
63 #define AVATAR_SIZE_SAVE 96
64 #define AVATAR_SIZE_VIEW 64
65 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
69 * A custom GtkResponseType used when the user presses the
70 * "Camera Picture" button. Any positive value would be sufficient.
72 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM 10
74 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE GTK_RESPONSE_NO
75 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL GTK_RESPONSE_CANCEL
76 #define EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE GTK_RESPONSE_OK
78 struct _EmpathyAvatarChooserPrivate
80 TpConnection *connection;
81 GtkFileChooser *chooser_dialog;
83 gulong ready_handler_id;
85 EmpathyAvatar *avatar;
86 GSettings *gsettings_ui;
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"
112 DND_TARGET_TYPE_URI_LIST
115 static const GtkTargetEntry drop_types[] =
117 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
121 avatar_chooser_get_property (GObject *object,
126 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
130 case PROP_CONNECTION:
131 g_value_set_object (value, self->priv->connection);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
140 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
141 TpConnection *connection)
143 tp_clear_object (&self->priv->connection);
145 if (connection != NULL)
146 self->priv->connection = g_object_ref (connection);
150 avatar_chooser_set_property (GObject *object,
155 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
159 case PROP_CONNECTION:
160 avatar_chooser_set_connection (self, g_value_get_object (value));
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
169 avatar_chooser_dispose (GObject *object)
171 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
173 tp_clear_object (&self->priv->connection);
174 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
175 tp_clear_object (&self->priv->gsettings_ui);
177 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->dispose (object);
181 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
183 GObjectClass *object_class = G_OBJECT_CLASS (klass);
184 GParamSpec *param_spec;
186 object_class->dispose = avatar_chooser_dispose;
187 object_class->get_property = avatar_chooser_get_property;
188 object_class->set_property = avatar_chooser_set_property;
191 * EmpathyAvatarChooser::changed:
192 * @self: an #EmpathyAvatarChooser
194 * Emitted when the chosen avatar has changed.
198 g_signal_new ("changed",
199 G_TYPE_FROM_CLASS (klass),
203 g_cclosure_marshal_VOID__VOID,
207 * EmpathyAvatarChooser:connection:
209 * The #TpConnection whose avatar should be shown and modified by
210 * the #EmpathyAvatarChooser instance.
212 param_spec = g_param_spec_object ("connection",
214 "TpConnection whose avatar should be "
215 "shown and modified by this widget",
218 G_PARAM_STATIC_STRINGS);
219 g_object_class_install_property (object_class,
223 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPrivate));
227 avatar_chooser_drag_motion_cb (GtkWidget *widget,
228 GdkDragContext *context,
232 EmpathyAvatarChooser *self)
236 for (p = gdk_drag_context_list_targets (context); p != NULL;
239 gchar *possible_type;
241 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
243 if (!strcmp (possible_type, URI_LIST_TYPE))
245 g_free (possible_type);
246 gdk_drag_status (context, GDK_ACTION_COPY, time_);
251 g_free (possible_type);
258 avatar_chooser_drag_drop_cb (GtkWidget *widget,
259 GdkDragContext *context,
263 EmpathyAvatarChooser *self)
267 if (gdk_drag_context_list_targets (context) == NULL)
270 for (p = gdk_drag_context_list_targets (context);
271 p != NULL; p = p->next)
275 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
276 if (!strcmp (possible_type, URI_LIST_TYPE))
278 g_free (possible_type);
279 gtk_drag_get_data (widget, context,
280 GDK_POINTER_TO_ATOM (p->data),
286 g_free (possible_type);
293 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
297 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
299 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
300 GTK_ICON_SIZE_DIALOG);
301 gtk_button_set_image (GTK_BUTTON (self), image);
302 g_signal_emit (self, signals[CHANGED], 0);
306 str_in_strv (const gchar *str,
312 while (*strv != NULL)
314 if (g_str_equal (str, *strv))
323 /* The caller must free the strings stored in satisfactory_format_name and
324 * satisfactory_mime_type.
327 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
328 gchar **accepted_mime_types,
329 gchar **satisfactory_format_name,
330 gchar **satisfactory_mime_type)
332 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
335 gboolean found = FALSE;
337 *satisfactory_format_name = NULL;
338 *satisfactory_mime_type = NULL;
340 /* If there is no accepted format there is nothing we can do */
341 if (accepted_mime_types == NULL || *accepted_mime_types == NULL)
344 /* If the current mime type is good and accepted, don't change it!
345 * jpeg is compress better pictures, but png is better for logos and
346 * could have an alpha layer. */
347 if (str_in_strv (current_mime_type, good_mime_types) &&
348 str_in_strv (current_mime_type, accepted_mime_types))
350 *satisfactory_mime_type = g_strdup (current_mime_type);
351 *satisfactory_format_name = g_strdup (current_mime_type +
356 /* The current mime type is either not accepted or not good to use.
357 * Check if one of the good format is supported... */
358 for (i = 0; good_mime_types[i] != NULL; i++)
360 if (str_in_strv (good_mime_types[i], accepted_mime_types))
362 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
363 *satisfactory_format_name = g_strdup (good_mime_types[i] +
369 /* Pick the first supported format we can write */
370 formats = gdk_pixbuf_get_formats ();
371 for (l = formats; !found && l != NULL; l = l->next)
373 GdkPixbufFormat *format = l->data;
374 gchar **format_mime_types;
377 if (!gdk_pixbuf_format_is_writable (format))
380 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
381 for (iter = format_mime_types; *iter != NULL; iter++)
383 if (str_in_strv (*iter, accepted_mime_types))
385 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
386 *satisfactory_mime_type = g_strdup (*iter);
391 g_strfreev (format_mime_types);
393 g_slist_free (formats);
399 avatar_chooser_error_show (EmpathyAvatarChooser *self,
400 const gchar *primary_text,
401 const gchar *secondary_text)
406 parent = gtk_widget_get_toplevel (GTK_WIDGET (self));
407 if (!GTK_IS_WINDOW (parent))
410 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
416 if (secondary_text != NULL)
418 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
419 "%s", secondary_text);
422 g_signal_connect (dialog, "response",
423 G_CALLBACK (gtk_widget_destroy), NULL);
424 gtk_widget_show (dialog);
428 static EmpathyAvatar *
429 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
431 EmpathyAvatar *avatar)
433 TpAvatarRequirements *req;
434 gboolean needs_conversion = FALSE;
436 gchar *new_format_name = NULL;
437 gchar *new_mime_type = NULL;
438 gdouble min_factor, max_factor;
440 gchar *best_image_data = NULL;
441 gsize best_image_size = 0;
444 req = tp_connection_get_avatar_requirements (self->priv->connection);
447 DEBUG ("Avatar requirements not ready");
451 /* Smaller is the factor, smaller will be the image.
452 * 0 is an empty image, 1 is the full size. */
457 /* Check if we need to convert to another image format */
458 if (avatar_chooser_need_mime_type_conversion (avatar->format,
459 req->supported_mime_types, &new_format_name, &new_mime_type))
461 DEBUG ("Format conversion needed, we'll use mime type '%s' "
462 "and format name '%s'. Current mime type is '%s'",
463 new_mime_type, new_format_name, avatar->format);
464 needs_conversion = TRUE;
467 /* If there is no format we can use, report error to the user. */
468 if (new_mime_type == NULL || new_format_name == NULL)
470 avatar_chooser_error_show (self, _("Couldn't convert image"),
471 _("None of the accepted image formats are "
472 "supported on your system"));
476 /* If width or height are too big, it needs converting. */
477 width = gdk_pixbuf_get_width (pixbuf);
478 height = gdk_pixbuf_get_height (pixbuf);
479 if ((req->maximum_width > 0 && width > req->maximum_width) ||
480 (req->maximum_height > 0 && height > req->maximum_height))
482 gdouble h_factor, v_factor;
484 h_factor = (gdouble) req->maximum_width / width;
485 v_factor = (gdouble) req->maximum_height / height;
486 factor = max_factor = MIN (h_factor, v_factor);
488 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
489 width, height, req->maximum_width, req->maximum_height);
491 needs_conversion = TRUE;
494 /* If the data len is too big and no other conversion is needed,
495 * try with a lower factor. */
496 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes &&
499 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
500 "(max is %u bytes), conversion needed.",
501 avatar->len, req->maximum_bytes);
504 needs_conversion = TRUE;
507 /* If no conversion is needed, return the avatar */
508 if (!needs_conversion)
510 g_free (new_format_name);
511 g_free (new_mime_type);
512 return empathy_avatar_ref (avatar);
517 GdkPixbuf *pixbuf_scaled = NULL;
519 gint new_width, new_height;
520 gchar *converted_image_data;
521 gsize converted_image_size;
522 GError *error = NULL;
526 new_width = width * factor;
527 new_height = height * factor;
528 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
537 pixbuf_scaled = g_object_ref (pixbuf);
540 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
541 new_width, new_height, new_format_name);
543 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
544 &converted_image_data,
545 &converted_image_size,
548 g_object_unref (pixbuf_scaled);
552 g_free (new_format_name);
553 g_free (new_mime_type);
554 avatar_chooser_error_show (self,
555 _("Couldn't convert image"),
556 error ? error->message : NULL);
557 g_clear_error (&error);
561 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
562 converted_image_size);
564 /* If the new image satisfy the req, keep it as current best */
565 if (req->maximum_bytes == 0 || converted_image_size <= req->maximum_bytes)
567 g_free (best_image_data);
569 best_image_data = converted_image_data;
570 best_image_size = converted_image_size;
572 /* If this image is close enough to the optimal size,
574 if (req->maximum_bytes == 0 ||
575 req->maximum_bytes - converted_image_size <= 1024)
580 g_free (converted_image_data);
583 /* Make a binary search for the bigest factor that produce
584 * an image data size less than max_size */
585 if (converted_image_size > req->maximum_bytes)
587 if (converted_image_size < req->maximum_bytes)
589 factor = (min_factor + max_factor)/2;
591 if ((int) (width * factor) == new_width ||
592 (int) (height * factor) == new_height)
594 /* min_factor and max_factor are too close, so the new
595 * factor will produce the same image as previous
596 * iteration. No need to continue, we already found
597 * the optimal size. */
601 /* Do 10 iterations in the worst case */
602 } while (++count < 10);
604 g_free (new_format_name);
606 avatar = empathy_avatar_new ((guchar *) best_image_data,
607 best_image_size, new_mime_type, NULL);
609 g_free (best_image_data);
610 g_free (new_mime_type);
616 avatar_chooser_set_image (EmpathyAvatarChooser *self,
617 EmpathyAvatar *avatar,
619 gboolean set_locally)
621 GdkPixbuf *pixbuf_view;
624 g_assert (avatar != NULL);
625 g_assert (pixbuf != NULL);
631 conv = avatar_chooser_maybe_convert_and_scale (self,
633 empathy_avatar_unref (avatar);
636 /* An error occured; don't change the avatar. */
642 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
643 self->priv->avatar = avatar;
645 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf,
647 image = gtk_image_new_from_pixbuf (pixbuf_view);
649 gtk_button_set_image (GTK_BUTTON (self), image);
650 g_signal_emit (self, signals[CHANGED], 0);
652 g_object_unref (pixbuf_view);
653 g_object_unref (pixbuf);
656 /* takes ownership of @data */
658 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
661 gboolean set_locally)
664 EmpathyAvatar *avatar = NULL;
665 gchar *mime_type = NULL;
669 avatar_chooser_clear_image (self);
673 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
681 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
683 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
690 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
691 GdkDragContext *context,
694 GtkSelectionData *selection_data,
697 EmpathyAvatarChooser *self)
700 gboolean handled = FALSE;
702 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
703 if (!strcmp (target_type, URI_LIST_TYPE))
710 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
717 (gchar *) gtk_selection_data_get_data (selection_data),
718 nl - (gchar *) gtk_selection_data_get_data (selection_data));
720 file = g_file_new_for_uri (uri);
725 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
729 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
734 /* pass data to the avatar_chooser_set_image_from_data */
735 avatar_chooser_set_image_from_data (self, data, bytes_read, TRUE);
738 g_object_unref (file);
741 gtk_drag_finish (context, handled, FALSE, time_);
745 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
746 EmpathyAvatarChooser *self)
750 filename = gtk_file_chooser_get_preview_filename (file_chooser);
752 if (filename != NULL)
755 GdkPixbuf *pixbuf = NULL;
756 GdkPixbuf *scaled_pixbuf;
758 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
760 image = gtk_file_chooser_get_preview_widget (file_chooser);
764 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf,
767 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
768 g_object_unref (scaled_pixbuf);
769 g_object_unref (pixbuf);
773 gtk_image_set_from_stock (GTK_IMAGE (image),
774 "gtk-dialog-question",
775 GTK_ICON_SIZE_DIALOG);
781 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
785 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
786 const gchar *filename)
788 gchar *image_data = NULL;
789 gsize image_size = 0;
790 GError *error = NULL;
792 if (!g_file_get_contents (filename, &image_data, &image_size, &error))
794 DEBUG ("Failed to load image from '%s': %s", filename,
795 error ? error->message : "No error given");
797 g_clear_error (&error);
801 /* pass image_data to the avatar_chooser_set_image_from_data */
802 avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
807 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
812 EmpathyAvatar *avatar = NULL;
813 GError *error = NULL;
815 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL))
817 avatar_chooser_error_show (self,
818 _("Couldn't save picture to file"),
819 error ? error->message : NULL);
820 g_clear_error (&error);
824 avatar = empathy_avatar_new ((guchar *) buf, size, "image/png", NULL);
825 avatar_chooser_set_image (self, avatar, pb, TRUE);
831 destroy_chooser (GtkWidget *self)
833 gtk_widget_destroy (self);
838 webcam_response_cb (GtkDialog *dialog,
840 EmpathyAvatarChooser *self)
842 if (response == GTK_RESPONSE_ACCEPT)
845 CheeseAvatarChooser *cheese_chooser;
847 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
848 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
849 avatar_chooser_set_avatar_from_pixbuf (self, pb);
852 if (response != GTK_RESPONSE_DELETE_EVENT &&
853 response != GTK_RESPONSE_NONE)
854 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
858 choose_avatar_from_webcam (GtkWidget *widget,
859 EmpathyAvatarChooser *self)
863 window = cheese_avatar_chooser_new ();
865 gtk_window_set_transient_for (GTK_WINDOW (window),
866 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
867 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
868 g_signal_connect (G_OBJECT (window), "response",
869 G_CALLBACK (webcam_response_cb), self);
870 gtk_widget_show (window);
872 #endif /* HAVE_CHEESE */
875 avatar_chooser_response_cb (GtkWidget *widget,
877 EmpathyAvatarChooser *self)
879 self->priv->chooser_dialog = NULL;
881 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE)
886 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
887 avatar_chooser_set_image_from_file (self, filename);
890 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
893 g_settings_set_string (self->priv->gsettings_ui,
894 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
900 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE)
902 /* This corresponds to "No Image", not to "Cancel" */
903 avatar_chooser_clear_image (self);
906 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM)
908 /* This corresponds to "Camera Picture" */
909 choose_avatar_from_webcam (widget, self);
913 gtk_widget_destroy (widget);
917 avatar_chooser_clicked_cb (GtkWidget *button,
918 EmpathyAvatarChooser *self)
920 GtkFileChooser *chooser_dialog;
922 gchar *saved_dir = NULL;
923 const gchar *default_dir = DEFAULT_DIR;
924 const gchar *pics_dir;
925 GtkFileFilter *filter;
927 if (self->priv->chooser_dialog != NULL)
929 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
933 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
934 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
935 empathy_get_toplevel_window (GTK_WIDGET (self)),
936 GTK_FILE_CHOOSER_ACTION_OPEN,
938 _("Take a picture..."),
939 EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM,
942 EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
944 EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
946 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
949 chooser_dialog = self->priv->chooser_dialog;
950 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
952 /* Get special dirs */
953 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
954 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
956 if (saved_dir != NULL &&
957 !g_file_test (saved_dir, G_FILE_TEST_IS_DIR))
963 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR))
966 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
967 if (pics_dir != NULL && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR))
970 /* Set current dir to the last one or to DEFAULT_DIR or to home */
971 if (saved_dir != NULL)
972 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
973 else if (pics_dir != NULL)
974 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
975 else if (default_dir != NULL)
976 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
978 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
980 /* Add shortcuts to special dirs */
982 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
984 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
986 if (default_dir != NULL)
987 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
989 /* Setup preview image */
990 image = gtk_image_new ();
991 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
992 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
993 gtk_widget_show (image);
994 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
995 g_signal_connect (chooser_dialog, "update-preview",
996 G_CALLBACK (avatar_chooser_update_preview_cb),
1000 filter = gtk_file_filter_new ();
1001 gtk_file_filter_set_name (filter, _("Images"));
1002 gtk_file_filter_add_pixbuf_formats (filter);
1003 gtk_file_chooser_add_filter (chooser_dialog, filter);
1004 filter = gtk_file_filter_new ();
1005 gtk_file_filter_set_name (filter, _("All Files"));
1006 gtk_file_filter_add_pattern (filter, "*");
1007 gtk_file_chooser_add_filter (chooser_dialog, filter);
1009 /* Setup response */
1010 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog),
1011 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1013 g_signal_connect (chooser_dialog, "response",
1014 G_CALLBACK (avatar_chooser_response_cb),
1017 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1023 empathy_avatar_chooser_init (EmpathyAvatarChooser *self)
1025 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1026 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPrivate);
1028 gtk_drag_dest_set (GTK_WIDGET (self),
1029 GTK_DEST_DEFAULT_ALL,
1031 G_N_ELEMENTS (drop_types),
1034 self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1036 g_signal_connect (self, "drag-motion",
1037 G_CALLBACK (avatar_chooser_drag_motion_cb),
1039 g_signal_connect (self, "drag-drop",
1040 G_CALLBACK (avatar_chooser_drag_drop_cb),
1042 g_signal_connect (self, "drag-data-received",
1043 G_CALLBACK (avatar_chooser_drag_data_received_cb),
1045 g_signal_connect (self, "clicked",
1046 G_CALLBACK (avatar_chooser_clicked_cb),
1049 empathy_avatar_chooser_set (self, NULL);
1053 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *self,
1054 EmpathyAvatar *avatar,
1055 gboolean set_locally)
1058 gchar *mime_type = NULL;
1060 g_assert (avatar != NULL);
1062 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
1063 avatar->len, &mime_type);
1067 DEBUG ("couldn't make a pixbuf from avatar; giving up");
1071 if (avatar->format == NULL)
1073 avatar->format = mime_type;
1077 if (strcmp (mime_type, avatar->format))
1078 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
1079 avatar->format, mime_type);
1084 empathy_avatar_ref (avatar);
1086 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
1090 * empathy_avatar_chooser_new:
1092 * Creates a new #EmpathyAvatarChooser.
1094 * Return value: a new #EmpathyAvatarChooser
1097 empathy_avatar_chooser_new (void)
1099 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1103 * empathy_avatar_chooser_set:
1104 * @self: an #EmpathyAvatarChooser
1105 * @avatar: a new #EmpathyAvatar
1107 * Sets the @self to display the avatar indicated by @avatar.
1110 empathy_avatar_chooser_set (EmpathyAvatarChooser *self,
1111 EmpathyAvatar *avatar)
1113 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1116 avatar_chooser_set_image_from_avatar (self, avatar, FALSE);
1118 avatar_chooser_clear_image (self);
1122 * empathy_avatar_chooser_get_image_data:
1123 * @self: an #EmpathyAvatarChooser
1124 * @data: avatar bytes
1125 * @data_size: size of @data
1126 * @mime_type: avatar mime-type
1128 * Gets image data about the currently selected avatar.
1131 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *self,
1134 const gchar **mime_type)
1136 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1138 if (self->priv->avatar != NULL)
1141 *data = (gchar *) self->priv->avatar->data;
1143 if (data_size != NULL)
1144 *data_size = self->priv->avatar->len;
1146 if (mime_type != NULL)
1147 *mime_type = self->priv->avatar->format;
1154 if (data_size != NULL)
1157 if (mime_type != NULL)
1163 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1166 g_return_if_fail (account != NULL);
1168 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1169 g_object_notify (G_OBJECT (self), "connection");