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)
147 GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
148 self->priv->connection = g_object_ref (connection);
149 tp_proxy_prepare_async (self->priv->connection, features, NULL, NULL);
154 avatar_chooser_set_property (GObject *object,
159 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
163 case PROP_CONNECTION:
164 avatar_chooser_set_connection (self, g_value_get_object (value));
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
173 avatar_chooser_dispose (GObject *object)
175 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
177 tp_clear_object (&self->priv->connection);
178 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
179 tp_clear_object (&self->priv->gsettings_ui);
181 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->dispose (object);
185 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
187 GObjectClass *object_class = G_OBJECT_CLASS (klass);
188 GParamSpec *param_spec;
190 object_class->dispose = avatar_chooser_dispose;
191 object_class->get_property = avatar_chooser_get_property;
192 object_class->set_property = avatar_chooser_set_property;
195 * EmpathyAvatarChooser::changed:
196 * @self: an #EmpathyAvatarChooser
198 * Emitted when the chosen avatar has changed.
202 g_signal_new ("changed",
203 G_TYPE_FROM_CLASS (klass),
207 g_cclosure_marshal_VOID__VOID,
211 * EmpathyAvatarChooser:connection:
213 * The #TpConnection whose avatar should be shown and modified by
214 * the #EmpathyAvatarChooser instance.
216 param_spec = g_param_spec_object ("connection",
218 "TpConnection whose avatar should be "
219 "shown and modified by this widget",
222 G_PARAM_STATIC_STRINGS);
223 g_object_class_install_property (object_class,
227 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPrivate));
231 avatar_chooser_drag_motion_cb (GtkWidget *widget,
232 GdkDragContext *context,
236 EmpathyAvatarChooser *self)
240 for (p = gdk_drag_context_list_targets (context); p != NULL;
243 gchar *possible_type;
245 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
247 if (!strcmp (possible_type, URI_LIST_TYPE))
249 g_free (possible_type);
250 gdk_drag_status (context, GDK_ACTION_COPY, time_);
255 g_free (possible_type);
262 avatar_chooser_drag_drop_cb (GtkWidget *widget,
263 GdkDragContext *context,
267 EmpathyAvatarChooser *self)
271 if (gdk_drag_context_list_targets (context) == NULL)
274 for (p = gdk_drag_context_list_targets (context);
275 p != NULL; p = p->next)
279 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
280 if (!strcmp (possible_type, URI_LIST_TYPE))
282 g_free (possible_type);
283 gtk_drag_get_data (widget, context,
284 GDK_POINTER_TO_ATOM (p->data),
290 g_free (possible_type);
297 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
301 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
303 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
304 GTK_ICON_SIZE_DIALOG);
305 gtk_button_set_image (GTK_BUTTON (self), image);
306 g_signal_emit (self, signals[CHANGED], 0);
310 str_in_strv (const gchar *str,
316 while (*strv != NULL)
318 if (g_str_equal (str, *strv))
327 /* The caller must free the strings stored in satisfactory_format_name and
328 * satisfactory_mime_type.
331 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
332 gchar **accepted_mime_types,
333 gchar **satisfactory_format_name,
334 gchar **satisfactory_mime_type)
336 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
339 gboolean found = FALSE;
341 *satisfactory_format_name = NULL;
342 *satisfactory_mime_type = NULL;
344 /* If there is no accepted format there is nothing we can do */
345 if (accepted_mime_types == NULL || *accepted_mime_types == NULL)
348 /* If the current mime type is good and accepted, don't change it!
349 * jpeg is compress better pictures, but png is better for logos and
350 * could have an alpha layer. */
351 if (str_in_strv (current_mime_type, good_mime_types) &&
352 str_in_strv (current_mime_type, accepted_mime_types))
354 *satisfactory_mime_type = g_strdup (current_mime_type);
355 *satisfactory_format_name = g_strdup (current_mime_type +
360 /* The current mime type is either not accepted or not good to use.
361 * Check if one of the good format is supported... */
362 for (i = 0; good_mime_types[i] != NULL; i++)
364 if (str_in_strv (good_mime_types[i], accepted_mime_types))
366 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
367 *satisfactory_format_name = g_strdup (good_mime_types[i] +
373 /* Pick the first supported format we can write */
374 formats = gdk_pixbuf_get_formats ();
375 for (l = formats; !found && l != NULL; l = l->next)
377 GdkPixbufFormat *format = l->data;
378 gchar **format_mime_types;
381 if (!gdk_pixbuf_format_is_writable (format))
384 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
385 for (iter = format_mime_types; *iter != NULL; iter++)
387 if (str_in_strv (*iter, accepted_mime_types))
389 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
390 *satisfactory_mime_type = g_strdup (*iter);
395 g_strfreev (format_mime_types);
397 g_slist_free (formats);
403 avatar_chooser_error_show (EmpathyAvatarChooser *self,
404 const gchar *primary_text,
405 const gchar *secondary_text)
410 parent = gtk_widget_get_toplevel (GTK_WIDGET (self));
411 if (!GTK_IS_WINDOW (parent))
414 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
420 if (secondary_text != NULL)
422 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
423 "%s", secondary_text);
426 g_signal_connect (dialog, "response",
427 G_CALLBACK (gtk_widget_destroy), NULL);
428 gtk_widget_show (dialog);
432 static EmpathyAvatar *
433 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
435 EmpathyAvatar *avatar)
437 TpAvatarRequirements *req;
438 gboolean needs_conversion = FALSE;
440 gchar *new_format_name = NULL;
441 gchar *new_mime_type = NULL;
442 gdouble min_factor, max_factor;
444 gchar *best_image_data = NULL;
445 gsize best_image_size = 0;
448 req = tp_connection_get_avatar_requirements (self->priv->connection);
451 DEBUG ("Avatar requirements not ready");
455 /* Smaller is the factor, smaller will be the image.
456 * 0 is an empty image, 1 is the full size. */
461 /* Check if we need to convert to another image format */
462 if (avatar_chooser_need_mime_type_conversion (avatar->format,
463 req->supported_mime_types, &new_format_name, &new_mime_type))
465 DEBUG ("Format conversion needed, we'll use mime type '%s' "
466 "and format name '%s'. Current mime type is '%s'",
467 new_mime_type, new_format_name, avatar->format);
468 needs_conversion = TRUE;
471 /* If there is no format we can use, report error to the user. */
472 if (new_mime_type == NULL || new_format_name == NULL)
474 avatar_chooser_error_show (self, _("Couldn't convert image"),
475 _("None of the accepted image formats are "
476 "supported on your system"));
480 /* If width or height are too big, it needs converting. */
481 width = gdk_pixbuf_get_width (pixbuf);
482 height = gdk_pixbuf_get_height (pixbuf);
483 if ((req->maximum_width > 0 && width > req->maximum_width) ||
484 (req->maximum_height > 0 && height > req->maximum_height))
486 gdouble h_factor, v_factor;
488 h_factor = (gdouble) req->maximum_width / width;
489 v_factor = (gdouble) req->maximum_height / height;
490 factor = max_factor = MIN (h_factor, v_factor);
492 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
493 width, height, req->maximum_width, req->maximum_height);
495 needs_conversion = TRUE;
498 /* If the data len is too big and no other conversion is needed,
499 * try with a lower factor. */
500 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes &&
503 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
504 "(max is %u bytes), conversion needed.",
505 avatar->len, req->maximum_bytes);
508 needs_conversion = TRUE;
511 /* If no conversion is needed, return the avatar */
512 if (!needs_conversion)
514 g_free (new_format_name);
515 g_free (new_mime_type);
516 return empathy_avatar_ref (avatar);
521 GdkPixbuf *pixbuf_scaled = NULL;
523 gint new_width, new_height;
524 gchar *converted_image_data;
525 gsize converted_image_size;
526 GError *error = NULL;
530 new_width = width * factor;
531 new_height = height * factor;
532 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
541 pixbuf_scaled = g_object_ref (pixbuf);
544 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
545 new_width, new_height, new_format_name);
547 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
548 &converted_image_data,
549 &converted_image_size,
552 g_object_unref (pixbuf_scaled);
556 g_free (new_format_name);
557 g_free (new_mime_type);
558 avatar_chooser_error_show (self,
559 _("Couldn't convert image"),
560 error ? error->message : NULL);
561 g_clear_error (&error);
565 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
566 converted_image_size);
568 /* If the new image satisfy the req, keep it as current best */
569 if (req->maximum_bytes == 0 || converted_image_size <= req->maximum_bytes)
571 g_free (best_image_data);
573 best_image_data = converted_image_data;
574 best_image_size = converted_image_size;
576 /* If this image is close enough to the optimal size,
578 if (req->maximum_bytes == 0 ||
579 req->maximum_bytes - converted_image_size <= 1024)
584 g_free (converted_image_data);
587 /* Make a binary search for the bigest factor that produce
588 * an image data size less than max_size */
589 if (converted_image_size > req->maximum_bytes)
591 if (converted_image_size < req->maximum_bytes)
593 factor = (min_factor + max_factor)/2;
595 if ((int) (width * factor) == new_width ||
596 (int) (height * factor) == new_height)
598 /* min_factor and max_factor are too close, so the new
599 * factor will produce the same image as previous
600 * iteration. No need to continue, we already found
601 * the optimal size. */
605 /* Do 10 iterations in the worst case */
606 } while (++count < 10);
608 g_free (new_format_name);
610 avatar = empathy_avatar_new ((guchar *) best_image_data,
611 best_image_size, new_mime_type, NULL);
613 g_free (best_image_data);
614 g_free (new_mime_type);
620 avatar_chooser_set_image (EmpathyAvatarChooser *self,
621 EmpathyAvatar *avatar,
623 gboolean set_locally)
625 GdkPixbuf *pixbuf_view;
628 g_assert (avatar != NULL);
629 g_assert (pixbuf != NULL);
635 conv = avatar_chooser_maybe_convert_and_scale (self,
637 empathy_avatar_unref (avatar);
640 /* An error occured; don't change the avatar. */
646 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
647 self->priv->avatar = avatar;
649 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf,
651 image = gtk_image_new_from_pixbuf (pixbuf_view);
653 gtk_button_set_image (GTK_BUTTON (self), image);
654 g_signal_emit (self, signals[CHANGED], 0);
656 g_object_unref (pixbuf_view);
657 g_object_unref (pixbuf);
660 /* takes ownership of @data */
662 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
665 gboolean set_locally)
668 EmpathyAvatar *avatar = NULL;
669 gchar *mime_type = NULL;
673 avatar_chooser_clear_image (self);
677 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
685 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
687 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
694 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
695 GdkDragContext *context,
698 GtkSelectionData *selection_data,
701 EmpathyAvatarChooser *self)
704 gboolean handled = FALSE;
706 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
707 if (!strcmp (target_type, URI_LIST_TYPE))
714 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
721 (gchar *) gtk_selection_data_get_data (selection_data),
722 nl - (gchar *) gtk_selection_data_get_data (selection_data));
724 file = g_file_new_for_uri (uri);
729 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
733 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
738 /* pass data to the avatar_chooser_set_image_from_data */
739 avatar_chooser_set_image_from_data (self, data, bytes_read, TRUE);
742 g_object_unref (file);
745 gtk_drag_finish (context, handled, FALSE, time_);
749 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
750 EmpathyAvatarChooser *self)
754 filename = gtk_file_chooser_get_preview_filename (file_chooser);
756 if (filename != NULL)
759 GdkPixbuf *pixbuf = NULL;
760 GdkPixbuf *scaled_pixbuf;
762 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
764 image = gtk_file_chooser_get_preview_widget (file_chooser);
768 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf,
771 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
772 g_object_unref (scaled_pixbuf);
773 g_object_unref (pixbuf);
777 gtk_image_set_from_stock (GTK_IMAGE (image),
778 "gtk-dialog-question",
779 GTK_ICON_SIZE_DIALOG);
785 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
789 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
790 const gchar *filename)
792 gchar *image_data = NULL;
793 gsize image_size = 0;
794 GError *error = NULL;
796 if (!g_file_get_contents (filename, &image_data, &image_size, &error))
798 DEBUG ("Failed to load image from '%s': %s", filename,
799 error ? error->message : "No error given");
801 g_clear_error (&error);
805 /* pass image_data to the avatar_chooser_set_image_from_data */
806 avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
811 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
816 EmpathyAvatar *avatar = NULL;
817 GError *error = NULL;
819 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL))
821 avatar_chooser_error_show (self,
822 _("Couldn't save pixbuf to png"),
823 error ? error->message : NULL);
824 g_clear_error (&error);
828 avatar = empathy_avatar_new ((guchar *) buf, size, "image/png", NULL);
829 avatar_chooser_set_image (self, avatar, pb, TRUE);
835 destroy_chooser (GtkWidget *self)
837 gtk_widget_destroy (self);
842 webcam_response_cb (GtkDialog *dialog,
844 EmpathyAvatarChooser *self)
846 if (response == GTK_RESPONSE_ACCEPT)
849 CheeseAvatarChooser *cheese_chooser;
851 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
852 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
853 avatar_chooser_set_avatar_from_pixbuf (self, pb);
856 if (response != GTK_RESPONSE_DELETE_EVENT &&
857 response != GTK_RESPONSE_NONE)
858 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
862 choose_avatar_from_webcam (GtkWidget *widget,
863 EmpathyAvatarChooser *self)
867 window = cheese_avatar_chooser_new ();
869 gtk_window_set_transient_for (GTK_WINDOW (window),
870 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
871 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
872 g_signal_connect (G_OBJECT (window), "response",
873 G_CALLBACK (webcam_response_cb), self);
874 gtk_widget_show (window);
876 #endif /* HAVE_CHEESE */
879 avatar_chooser_response_cb (GtkWidget *widget,
881 EmpathyAvatarChooser *self)
883 self->priv->chooser_dialog = NULL;
885 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE)
890 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
891 avatar_chooser_set_image_from_file (self, filename);
894 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
897 g_settings_set_string (self->priv->gsettings_ui,
898 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
904 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE)
906 /* This corresponds to "No Image", not to "Cancel" */
907 avatar_chooser_clear_image (self);
910 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM)
912 /* This corresponds to "Camera Picture" */
913 choose_avatar_from_webcam (widget, self);
917 gtk_widget_destroy (widget);
921 avatar_chooser_clicked_cb (GtkWidget *button,
922 EmpathyAvatarChooser *self)
924 GtkFileChooser *chooser_dialog;
926 gchar *saved_dir = NULL;
927 const gchar *default_dir = DEFAULT_DIR;
928 const gchar *pics_dir;
929 GtkFileFilter *filter;
931 if (self->priv->chooser_dialog != NULL)
933 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
937 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
938 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
939 empathy_get_toplevel_window (GTK_WIDGET (self)),
940 GTK_FILE_CHOOSER_ACTION_OPEN,
942 _("Take a picture..."),
943 EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM,
946 EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
948 EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
950 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
953 chooser_dialog = self->priv->chooser_dialog;
954 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
956 /* Get special dirs */
957 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
958 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
960 if (saved_dir != NULL &&
961 !g_file_test (saved_dir, G_FILE_TEST_IS_DIR))
967 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR))
970 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
971 if (pics_dir != NULL && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR))
974 /* Set current dir to the last one or to DEFAULT_DIR or to home */
975 if (saved_dir != NULL)
976 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
977 else if (pics_dir != NULL)
978 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
979 else if (default_dir != NULL)
980 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
982 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
984 /* Add shortcuts to special dirs */
986 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
988 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
990 if (default_dir != NULL)
991 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
993 /* Setup preview image */
994 image = gtk_image_new ();
995 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
996 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
997 gtk_widget_show (image);
998 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
999 g_signal_connect (chooser_dialog, "update-preview",
1000 G_CALLBACK (avatar_chooser_update_preview_cb),
1004 filter = gtk_file_filter_new ();
1005 gtk_file_filter_set_name (filter, _("Images"));
1006 gtk_file_filter_add_pixbuf_formats (filter);
1007 gtk_file_chooser_add_filter (chooser_dialog, filter);
1008 filter = gtk_file_filter_new ();
1009 gtk_file_filter_set_name (filter, _("All Files"));
1010 gtk_file_filter_add_pattern (filter, "*");
1011 gtk_file_chooser_add_filter (chooser_dialog, filter);
1013 /* Setup response */
1014 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog),
1015 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1017 g_signal_connect (chooser_dialog, "response",
1018 G_CALLBACK (avatar_chooser_response_cb),
1021 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1027 empathy_avatar_chooser_init (EmpathyAvatarChooser *self)
1029 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1030 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPrivate);
1032 gtk_drag_dest_set (GTK_WIDGET (self),
1033 GTK_DEST_DEFAULT_ALL,
1035 G_N_ELEMENTS (drop_types),
1038 self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1040 g_signal_connect (self, "drag-motion",
1041 G_CALLBACK (avatar_chooser_drag_motion_cb),
1043 g_signal_connect (self, "drag-drop",
1044 G_CALLBACK (avatar_chooser_drag_drop_cb),
1046 g_signal_connect (self, "drag-data-received",
1047 G_CALLBACK (avatar_chooser_drag_data_received_cb),
1049 g_signal_connect (self, "clicked",
1050 G_CALLBACK (avatar_chooser_clicked_cb),
1053 empathy_avatar_chooser_set (self, NULL);
1057 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *self,
1058 EmpathyAvatar *avatar,
1059 gboolean set_locally)
1062 gchar *mime_type = NULL;
1064 g_assert (avatar != NULL);
1066 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
1067 avatar->len, &mime_type);
1071 DEBUG ("couldn't make a pixbuf from avatar; giving up");
1075 if (avatar->format == NULL)
1077 avatar->format = mime_type;
1081 if (strcmp (mime_type, avatar->format))
1082 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
1083 avatar->format, mime_type);
1088 empathy_avatar_ref (avatar);
1090 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
1094 * empathy_avatar_chooser_new:
1096 * Creates a new #EmpathyAvatarChooser.
1098 * Return value: a new #EmpathyAvatarChooser
1101 empathy_avatar_chooser_new (void)
1103 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1107 * empathy_avatar_chooser_set:
1108 * @self: an #EmpathyAvatarChooser
1109 * @avatar: a new #EmpathyAvatar
1111 * Sets the @self to display the avatar indicated by @avatar.
1114 empathy_avatar_chooser_set (EmpathyAvatarChooser *self,
1115 EmpathyAvatar *avatar)
1117 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1120 avatar_chooser_set_image_from_avatar (self, avatar, FALSE);
1122 avatar_chooser_clear_image (self);
1126 * empathy_avatar_chooser_get_image_data:
1127 * @self: an #EmpathyAvatarChooser
1128 * @data: avatar bytes
1129 * @data_size: size of @data
1130 * @mime_type: avatar mime-type
1132 * Gets image data about the currently selected avatar.
1135 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *self,
1138 const gchar **mime_type)
1140 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1142 if (self->priv->avatar != NULL)
1145 *data = (gchar *) self->priv->avatar->data;
1147 if (data_size != NULL)
1148 *data_size = self->priv->avatar->len;
1150 if (mime_type != NULL)
1151 *mime_type = self->priv->avatar->format;
1158 if (data_size != NULL)
1161 if (mime_type != NULL)
1167 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1170 g_return_if_fail (account != NULL);
1172 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1173 g_object_notify (G_OBJECT (self), "connection");