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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 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-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 {
80 TpConnection *connection;
81 GtkFileChooser *chooser_dialog;
83 gulong ready_handler_id;
85 EmpathyAvatar *avatar;
86 GSettings *gsettings_ui;
99 static guint signals [LAST_SIGNAL];
101 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
104 * Drag and drop stuff
106 #define URI_LIST_TYPE "text/uri-list"
109 DND_TARGET_TYPE_URI_LIST
112 static const GtkTargetEntry drop_types[] = {
113 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
117 avatar_chooser_get_property (GObject *object,
122 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
125 case PROP_CONNECTION:
126 g_value_set_object (value, self->priv->connection);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
135 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
136 TpConnection *connection)
138 tp_clear_object (&self->priv->connection);
140 if (connection != NULL) {
141 GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
142 self->priv->connection = g_object_ref (connection);
143 tp_proxy_prepare_async (self->priv->connection, features, NULL, NULL);
148 avatar_chooser_set_property (GObject *object,
153 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
156 case PROP_CONNECTION:
157 avatar_chooser_set_connection (self, g_value_get_object (value));
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
166 avatar_chooser_finalize (GObject *object)
168 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
170 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
171 g_assert (self->priv->connection == NULL);
173 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)->finalize (object);
181 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
183 GObjectClass *object_class = G_OBJECT_CLASS (klass);
184 GParamSpec *param_spec;
186 object_class->finalize = avatar_chooser_finalize;
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;
238 gchar *possible_type;
240 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
242 if (!strcmp (possible_type, URI_LIST_TYPE)) {
243 g_free (possible_type);
244 gdk_drag_status (context, GDK_ACTION_COPY, time_);
249 g_free (possible_type);
256 avatar_chooser_drag_leave_cb (GtkWidget *widget,
257 GdkDragContext *context,
259 EmpathyAvatarChooser *self)
264 avatar_chooser_drag_drop_cb (GtkWidget *widget,
265 GdkDragContext *context,
269 EmpathyAvatarChooser *self)
273 if (gdk_drag_context_list_targets (context) == NULL) {
277 for (p = gdk_drag_context_list_targets (context);
278 p != NULL; p = p->next) {
281 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
282 if (!strcmp (possible_type, URI_LIST_TYPE)) {
283 g_free (possible_type);
284 gtk_drag_get_data (widget, context,
285 GDK_POINTER_TO_ATOM (p->data),
291 g_free (possible_type);
298 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
302 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
304 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
305 GTK_ICON_SIZE_DIALOG);
306 gtk_button_set_image (GTK_BUTTON (self), image);
307 g_signal_emit (self, signals[CHANGED], 0);
311 str_in_strv (const gchar *str,
318 while (*strv != NULL) {
319 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) {
349 /* If the current mime type is good and accepted, don't change it!
350 * jpeg is compress better pictures, but png is better for logos and
351 * could have an alpha layer. */
352 if (str_in_strv (current_mime_type, good_mime_types) &&
353 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++) {
363 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
364 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
365 *satisfactory_format_name = g_strdup (good_mime_types[i] +
371 /* Pick the first supported format we can write */
372 formats = gdk_pixbuf_get_formats ();
373 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)) {
382 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
383 for (iter = format_mime_types; *iter != NULL; iter++) {
384 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)) {
411 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
417 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);
446 DEBUG ("Avatar requirements not ready");
450 /* Smaller is the factor, smaller will be the image.
451 * 0 is an empty image, 1 is the full size. */
456 /* Check if we need to convert to another image format */
457 if (avatar_chooser_need_mime_type_conversion (avatar->format,
458 req->supported_mime_types,
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) {
469 avatar_chooser_error_show (self, _("Couldn't convert image"),
470 _("None of the accepted image formats are "
471 "supported on your system"));
475 /* If width or height are too big, it needs converting. */
476 width = gdk_pixbuf_get_width (pixbuf);
477 height = gdk_pixbuf_get_height (pixbuf);
478 if ((req->maximum_width > 0 && width > req->maximum_width) ||
479 (req->maximum_height > 0 && height > req->maximum_height)) {
480 gdouble h_factor, v_factor;
482 h_factor = (gdouble) req->maximum_width / width;
483 v_factor = (gdouble) req->maximum_height / height;
484 factor = max_factor = MIN (h_factor, v_factor);
486 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
487 width, height, req->maximum_width, req->maximum_height);
489 needs_conversion = TRUE;
492 /* If the data len is too big and no other conversion is needed,
493 * try with a lower factor. */
494 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes && !needs_conversion) {
495 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
496 "(max is %u bytes), conversion needed.",
497 avatar->len, req->maximum_bytes);
500 needs_conversion = TRUE;
503 /* If no conversion is needed, return the avatar */
504 if (!needs_conversion) {
505 g_free (new_format_name);
506 g_free (new_mime_type);
507 return empathy_avatar_ref (avatar);
511 GdkPixbuf *pixbuf_scaled = NULL;
513 gint new_width, new_height;
514 gchar *converted_image_data;
515 gsize converted_image_size;
516 GError *error = NULL;
519 new_width = width * factor;
520 new_height = height * factor;
521 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
528 pixbuf_scaled = g_object_ref (pixbuf);
531 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
532 new_width, new_height, new_format_name);
534 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
535 &converted_image_data,
536 &converted_image_size,
539 g_object_unref (pixbuf_scaled);
542 g_free (new_format_name);
543 g_free (new_mime_type);
544 avatar_chooser_error_show (self,
545 _("Couldn't convert image"),
546 error ? error->message : NULL);
547 g_clear_error (&error);
551 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
552 converted_image_size);
554 /* If the new image satisfy the req, keep it as current best */
555 if (req->maximum_bytes == 0 ||
556 converted_image_size <= req->maximum_bytes) {
558 g_free (best_image_data);
560 best_image_data = converted_image_data;
561 best_image_size = converted_image_size;
563 /* If this image is close enough to the optimal size,
565 if (req->maximum_bytes == 0 ||
566 req->maximum_bytes - converted_image_size <= 1024)
569 g_free (converted_image_data);
572 /* Make a binary search for the bigest factor that produce
573 * an image data size less than max_size */
574 if (converted_image_size > req->maximum_bytes)
576 if (converted_image_size < req->maximum_bytes)
578 factor = (min_factor + max_factor)/2;
580 if ((int) (width * factor) == new_width ||
581 (int) (height * factor) == new_height) {
582 /* min_factor and max_factor are too close, so the new
583 * factor will produce the same image as previous
584 * iteration. No need to continue, we already found
585 * the optimal size. */
589 /* Do 10 iterations in the worst case */
590 } while (++count < 10);
592 g_free (new_format_name);
594 /* Takes ownership of new_mime_type and best_image_data */
595 avatar = empathy_avatar_new ((guchar *) best_image_data,
596 best_image_size, new_mime_type, NULL);
602 avatar_chooser_set_image (EmpathyAvatarChooser *self,
603 EmpathyAvatar *avatar,
605 gboolean set_locally)
607 GdkPixbuf *pixbuf_view;
610 g_assert (avatar != NULL);
611 g_assert (pixbuf != NULL);
616 conv = avatar_chooser_maybe_convert_and_scale (self,
618 empathy_avatar_unref (avatar);
621 /* An error occured; don't change the avatar. */
628 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
629 self->priv->avatar = avatar;
631 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
632 image = gtk_image_new_from_pixbuf (pixbuf_view);
634 gtk_button_set_image (GTK_BUTTON (self), image);
635 g_signal_emit (self, signals[CHANGED], 0);
637 g_object_unref (pixbuf_view);
638 g_object_unref (pixbuf);
642 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
645 gboolean set_locally)
648 EmpathyAvatar *avatar = NULL;
649 gchar *mime_type = NULL;
652 avatar_chooser_clear_image (self);
656 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
657 if (pixbuf == NULL) {
663 /* avatar takes ownership of data and mime_type */
664 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
666 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
670 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
671 GdkDragContext *context,
674 GtkSelectionData *selection_data,
677 EmpathyAvatarChooser *self)
680 gboolean handled = FALSE;
682 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
683 if (!strcmp (target_type, URI_LIST_TYPE)) {
689 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
694 uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
695 nl - (gchar *) gtk_selection_data_get_data (selection_data));
697 file = g_file_new_for_uri (uri);
700 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
704 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
708 /* this in turn calls empathy_avatar_new (), which assumes
711 avatar_chooser_set_image_from_data (self, data,
716 g_object_unref (file);
719 gtk_drag_finish (context, handled, FALSE, time_);
723 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
724 EmpathyAvatarChooser *self)
728 filename = gtk_file_chooser_get_preview_filename (file_chooser);
732 GdkPixbuf *pixbuf = NULL;
733 GdkPixbuf *scaled_pixbuf;
735 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
737 image = gtk_file_chooser_get_preview_widget (file_chooser);
740 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
741 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
742 g_object_unref (scaled_pixbuf);
743 g_object_unref (pixbuf);
745 gtk_image_set_from_stock (GTK_IMAGE (image),
746 "gtk-dialog-question",
747 GTK_ICON_SIZE_DIALOG);
753 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
757 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
758 const gchar *filename)
760 gchar *image_data = NULL;
761 gsize image_size = 0;
762 GError *error = NULL;
764 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
765 DEBUG ("Failed to load image from '%s': %s", filename,
766 error ? error->message : "No error given");
768 g_clear_error (&error);
772 avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
777 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
780 /* dup the string as empathy_avatar_new steals ownership of the it */
781 gchar *mime = g_strdup ("image/png");
784 EmpathyAvatar *avatar = NULL;
785 GError *error = NULL;
786 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL)) {
787 avatar_chooser_error_show (self,
788 _("Couldn't save pixbuf to png"),
789 error ? error->message : NULL);
790 g_clear_error (&error);
793 avatar = empathy_avatar_new ((guchar *) buf, size, mime, NULL);
794 avatar_chooser_set_image (self, avatar, pb, TRUE);
798 destroy_chooser (GtkWidget *self)
800 gtk_widget_destroy (self);
805 webcam_response_cb (GtkDialog *dialog,
807 EmpathyAvatarChooser *self)
809 if (response == GTK_RESPONSE_ACCEPT) {
811 CheeseAvatarChooser *cheese_chooser;
813 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
814 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
815 avatar_chooser_set_avatar_from_pixbuf (self, pb);
817 if (response != GTK_RESPONSE_DELETE_EVENT &&
818 response != GTK_RESPONSE_NONE)
819 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
823 choose_avatar_from_webcam (GtkWidget *widget,
824 EmpathyAvatarChooser *self)
828 window = cheese_avatar_chooser_new ();
830 gtk_window_set_transient_for (GTK_WINDOW (window),
831 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
832 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
833 g_signal_connect (G_OBJECT (window), "response",
834 G_CALLBACK (webcam_response_cb), self);
835 gtk_widget_show (window);
837 #endif /* HAVE_CHEESE */
840 avatar_chooser_response_cb (GtkWidget *widget,
842 EmpathyAvatarChooser *self)
844 self->priv->chooser_dialog = NULL;
846 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE) {
850 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
851 avatar_chooser_set_image_from_file (self, filename);
854 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
856 g_settings_set_string (self->priv->gsettings_ui,
857 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
863 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE) {
864 /* This corresponds to "No Image", not to "Cancel" */
865 avatar_chooser_clear_image (self);
868 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM) {
869 /* This corresponds to "Camera Picture" */
870 choose_avatar_from_webcam (widget, self);
873 gtk_widget_destroy (widget);
877 avatar_chooser_clicked_cb (GtkWidget *button,
878 EmpathyAvatarChooser *self)
880 GtkFileChooser *chooser_dialog;
882 gchar *saved_dir = NULL;
883 const gchar *default_dir = DEFAULT_DIR;
884 const gchar *pics_dir;
885 GtkFileFilter *filter;
887 if (self->priv->chooser_dialog) {
888 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
892 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
893 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
894 empathy_get_toplevel_window (GTK_WIDGET (self)),
895 GTK_FILE_CHOOSER_ACTION_OPEN,
897 _("Take a picture..."),
898 EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM,
901 EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
903 EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
905 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
907 chooser_dialog = self->priv->chooser_dialog;
908 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
910 /* Get special dirs */
911 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
912 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
914 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
918 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
921 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
922 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
926 /* Set current dir to the last one or to DEFAULT_DIR or to home */
928 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
931 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
933 else if (default_dir) {
934 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
936 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
939 /* Add shortcuts to special dirs */
941 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
944 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
947 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
950 /* Setup preview image */
951 image = gtk_image_new ();
952 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
953 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
954 gtk_widget_show (image);
955 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
956 g_signal_connect (chooser_dialog, "update-preview",
957 G_CALLBACK (avatar_chooser_update_preview_cb),
961 filter = gtk_file_filter_new ();
962 gtk_file_filter_set_name (filter, _("Images"));
963 gtk_file_filter_add_pixbuf_formats (filter);
964 gtk_file_chooser_add_filter (chooser_dialog, filter);
965 filter = gtk_file_filter_new ();
966 gtk_file_filter_set_name (filter, _("All Files"));
967 gtk_file_filter_add_pattern (filter, "*");
968 gtk_file_chooser_add_filter (chooser_dialog, filter);
971 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
972 g_signal_connect (chooser_dialog, "response",
973 G_CALLBACK (avatar_chooser_response_cb),
976 gtk_widget_show (GTK_WIDGET (chooser_dialog));
982 empathy_avatar_chooser_init (EmpathyAvatarChooser *self)
984 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
985 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPrivate);
987 gtk_drag_dest_set (GTK_WIDGET (self),
988 GTK_DEST_DEFAULT_ALL,
990 G_N_ELEMENTS (drop_types),
993 self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
995 g_signal_connect (self, "drag-motion",
996 G_CALLBACK (avatar_chooser_drag_motion_cb),
998 g_signal_connect (self, "drag-leave",
999 G_CALLBACK (avatar_chooser_drag_leave_cb),
1001 g_signal_connect (self, "drag-drop",
1002 G_CALLBACK (avatar_chooser_drag_drop_cb),
1004 g_signal_connect (self, "drag-data-received",
1005 G_CALLBACK (avatar_chooser_drag_data_received_cb),
1007 g_signal_connect (self, "clicked",
1008 G_CALLBACK (avatar_chooser_clicked_cb),
1011 empathy_avatar_chooser_set (self, NULL);
1015 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *self,
1016 EmpathyAvatar *avatar,
1017 gboolean set_locally)
1020 gchar *mime_type = NULL;
1022 g_assert (avatar != NULL);
1024 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
1027 if (pixbuf == NULL) {
1028 DEBUG ("couldn't make a pixbuf from avatar; giving up");
1032 if (avatar->format == NULL) {
1033 avatar->format = mime_type;
1035 if (strcmp (mime_type, avatar->format)) {
1036 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
1037 avatar->format, mime_type);
1042 empathy_avatar_ref (avatar);
1044 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
1048 * empathy_avatar_chooser_new:
1050 * Creates a new #EmpathyAvatarChooser.
1052 * Return value: a new #EmpathyAvatarChooser
1055 empathy_avatar_chooser_new (void)
1057 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1061 * empathy_avatar_chooser_set:
1062 * @self: an #EmpathyAvatarChooser
1063 * @avatar: a new #EmpathyAvatar
1065 * Sets the @self to display the avatar indicated by @avatar.
1068 empathy_avatar_chooser_set (EmpathyAvatarChooser *self,
1069 EmpathyAvatar *avatar)
1071 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1073 if (avatar != NULL) {
1074 avatar_chooser_set_image_from_avatar (self, avatar, FALSE);
1076 avatar_chooser_clear_image (self);
1081 * empathy_avatar_chooser_get_image_data:
1082 * @self: an #EmpathyAvatarChooser
1083 * @data: avatar bytes
1084 * @data_size: size of @data
1085 * @mime_type: avatar mime-type
1087 * Gets image data about the currently selected avatar.
1090 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *self,
1093 const gchar **mime_type)
1095 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1097 if (self->priv->avatar != NULL) {
1099 *data = (gchar *) self->priv->avatar->data;
1101 if (data_size != NULL) {
1102 *data_size = self->priv->avatar->len;
1104 if (mime_type != NULL) {
1105 *mime_type = self->priv->avatar->format;
1111 if (data_size != NULL) {
1114 if (mime_type != NULL) {
1121 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1124 g_return_if_fail (account != NULL);
1126 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1127 g_object_notify (G_OBJECT (self), "connection");