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-utils.h>
34 #include "empathy-avatar-chooser.h"
35 #include "empathy-conf.h"
36 #include "empathy-images.h"
37 #include "empathy-ui-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
43 * SECTION:empathy-avatar-chooser
44 * @title: EmpathyAvatarChooser
45 * @short_description: A widget used to change avatar
46 * @include: libempathy-gtk/empathy-avatar-chooser.h
48 * #EmpathyAvatarChooser is a widget which extends #GtkButton to
49 * provide a way of changing avatar.
53 * EmpathyAvatarChooser:
54 * @parent: parent object
56 * Widget which extends #GtkButton to provide a way of changing avatar.
59 #define AVATAR_SIZE_SAVE 96
60 #define AVATAR_SIZE_VIEW 64
61 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
65 TpConnection *connection;
66 GtkFileChooser *chooser_dialog;
68 gulong ready_handler_id;
70 EmpathyAvatar *avatar;
71 } EmpathyAvatarChooserPriv;
73 static void avatar_chooser_finalize (GObject *object);
74 static void avatar_chooser_set_connection (EmpathyAvatarChooser *self,
75 TpConnection *connection);
76 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
77 EmpathyAvatar *avatar,
79 gboolean set_locally);
80 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
81 GdkDragContext *context,
85 EmpathyAvatarChooser *chooser);
86 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
87 GdkDragContext *context,
89 EmpathyAvatarChooser *chooser);
90 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
91 GdkDragContext *context,
95 EmpathyAvatarChooser *chooser);
96 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
97 GdkDragContext *context,
100 GtkSelectionData *selection_data,
103 EmpathyAvatarChooser *chooser);
104 static void avatar_chooser_clicked_cb (GtkWidget *button,
105 EmpathyAvatarChooser *chooser);
117 static guint signals [LAST_SIGNAL];
119 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
122 * Drag and drop stuff
124 #define URI_LIST_TYPE "text/uri-list"
127 DND_TARGET_TYPE_URI_LIST
130 static const GtkTargetEntry drop_types[] = {
131 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
135 avatar_chooser_get_property (GObject *object,
140 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
143 case PROP_CONNECTION:
144 g_value_set_object (value, priv->connection);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
153 avatar_chooser_set_property (GObject *object,
158 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
161 case PROP_CONNECTION:
162 avatar_chooser_set_connection (self, g_value_get_object (value));
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
171 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
173 GObjectClass *object_class = G_OBJECT_CLASS (klass);
174 GParamSpec *param_spec;
176 object_class->finalize = avatar_chooser_finalize;
177 object_class->get_property = avatar_chooser_get_property;
178 object_class->set_property = avatar_chooser_set_property;
181 * EmpathyAvatarChooser::changed:
182 * @chooser: an #EmpathyAvatarChooser
184 * Emitted when the chosen avatar has changed.
188 g_signal_new ("changed",
189 G_TYPE_FROM_CLASS (klass),
193 g_cclosure_marshal_VOID__VOID,
197 * EmpathyAvatarChooser:connection:
199 * The #TpConnection whose avatar should be shown and modified by
200 * the #EmpathyAvatarChooser instance.
202 param_spec = g_param_spec_object ("connection",
204 "TpConnection whose avatar should be "
205 "shown and modified by this widget",
208 G_PARAM_STATIC_STRINGS);
209 g_object_class_install_property (object_class,
213 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
217 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
219 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
220 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
222 chooser->priv = priv;
223 gtk_drag_dest_set (GTK_WIDGET (chooser),
224 GTK_DEST_DEFAULT_ALL,
226 G_N_ELEMENTS (drop_types),
229 g_signal_connect (chooser, "drag-motion",
230 G_CALLBACK (avatar_chooser_drag_motion_cb),
232 g_signal_connect (chooser, "drag-leave",
233 G_CALLBACK (avatar_chooser_drag_leave_cb),
235 g_signal_connect (chooser, "drag-drop",
236 G_CALLBACK (avatar_chooser_drag_drop_cb),
238 g_signal_connect (chooser, "drag-data-received",
239 G_CALLBACK (avatar_chooser_drag_data_received_cb),
241 g_signal_connect (chooser, "clicked",
242 G_CALLBACK (avatar_chooser_clicked_cb),
245 empathy_avatar_chooser_set (chooser, NULL);
249 avatar_chooser_finalize (GObject *object)
251 EmpathyAvatarChooserPriv *priv;
253 priv = GET_PRIV (object);
255 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
256 g_assert (priv->connection == NULL);
258 if (priv->avatar != NULL) {
259 empathy_avatar_unref (priv->avatar);
262 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
266 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
267 TpConnection *connection)
269 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
271 if (priv->connection != NULL) {
272 g_object_unref (priv->connection);
273 priv->connection = NULL;
276 if (connection != NULL) {
277 GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
278 priv->connection = g_object_ref (connection);
279 tp_proxy_prepare_async (priv->connection, features, NULL, NULL);
284 avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
285 const gchar *primary_text,
286 const gchar *secondary_text)
291 parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
292 if (!GTK_IS_WINDOW (parent)) {
296 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
302 if (secondary_text != NULL) {
303 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
304 "%s", secondary_text);
307 g_signal_connect (dialog, "response",
308 G_CALLBACK (gtk_widget_destroy), NULL);
309 gtk_widget_show (dialog);
314 str_in_strv (const gchar *str,
321 while (*strv != NULL) {
322 if (g_str_equal (str, *strv)) {
330 /* The caller must free the strings stored in satisfactory_format_name and
331 * satisfactory_mime_type.
334 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
335 gchar **accepted_mime_types,
336 gchar **satisfactory_format_name,
337 gchar **satisfactory_mime_type)
339 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
342 gboolean found = FALSE;
344 *satisfactory_format_name = NULL;
345 *satisfactory_mime_type = NULL;
347 /* If there is no accepted format there is nothing we can do */
348 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
352 /* If the current mime type is good and accepted, don't change it!
353 * jpeg is compress better pictures, but png is better for logos and
354 * could have an alpha layer. */
355 if (str_in_strv (current_mime_type, good_mime_types) &&
356 str_in_strv (current_mime_type, accepted_mime_types)) {
357 *satisfactory_mime_type = g_strdup (current_mime_type);
358 *satisfactory_format_name = g_strdup (current_mime_type +
363 /* The current mime type is either not accepted or not good to use.
364 * Check if one of the good format is supported... */
365 for (i = 0; good_mime_types[i] != NULL; i++) {
366 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
367 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
368 *satisfactory_format_name = g_strdup (good_mime_types[i] +
374 /* Pick the first supported format we can write */
375 formats = gdk_pixbuf_get_formats ();
376 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)) {
385 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
386 for (iter = format_mime_types; *iter != NULL; iter++) {
387 if (str_in_strv (*iter, accepted_mime_types)) {
388 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
389 *satisfactory_mime_type = g_strdup (*iter);
394 g_strfreev (format_mime_types);
396 g_slist_free (formats);
401 static EmpathyAvatar *
402 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
404 EmpathyAvatar *avatar)
406 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
407 TpAvatarRequirements *req;
408 gboolean needs_conversion = FALSE;
410 gchar *new_format_name = NULL;
411 gchar *new_mime_type = NULL;
412 gdouble min_factor, max_factor;
414 gchar *converted_image_data = NULL;
415 gsize converted_image_size = 0;
417 req = tp_connection_get_avatar_requirements (priv->connection);
419 DEBUG ("Avatar requirements not ready");
423 /* Smaller is the factor, smaller will be the image.
424 * 0 is an empty image, 1 is the full size. */
429 /* Check if we need to convert to another image format */
430 if (avatar_chooser_need_mime_type_conversion (avatar->format,
431 req->supported_mime_types,
434 DEBUG ("Format conversion needed, we'll use mime type '%s' "
435 "and format name '%s'. Current mime type is '%s'",
436 new_mime_type, new_format_name, avatar->format);
437 needs_conversion = TRUE;
440 /* If there is no format we can use, report error to the user. */
441 if (new_mime_type == NULL || new_format_name == NULL) {
442 avatar_chooser_error_show (chooser, _("Couldn't convert image"),
443 _("None of the accepted image formats are "
444 "supported on your system"));
448 /* If width or height are too big, it needs converting. */
449 width = gdk_pixbuf_get_width (pixbuf);
450 height = gdk_pixbuf_get_height (pixbuf);
451 if ((req->maximum_width > 0 && width > req->maximum_width) ||
452 (req->maximum_height > 0 && height > req->maximum_height)) {
453 gdouble h_factor, v_factor;
455 h_factor = (gdouble) req->maximum_width / width;
456 v_factor = (gdouble) req->maximum_height / height;
457 factor = max_factor = MIN (h_factor, v_factor);
459 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
460 width, height, req->maximum_width, req->maximum_height);
462 needs_conversion = TRUE;
465 /* If the data len is too big and no other conversion is needed,
466 * try with a lower factor. */
467 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes && !needs_conversion) {
468 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
469 "(max is %u bytes), conversion needed.",
470 avatar->len, req->maximum_bytes);
473 needs_conversion = TRUE;
476 /* If no conversion is needed, return the avatar */
477 if (!needs_conversion) {
478 g_free (new_format_name);
479 g_free (new_mime_type);
480 return empathy_avatar_ref (avatar);
484 GdkPixbuf *pixbuf_scaled = NULL;
486 gint new_width, new_height;
487 GError *error = NULL;
489 g_free (converted_image_data);
492 new_width = width * factor;
493 new_height = height * factor;
494 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
501 pixbuf_scaled = g_object_ref (pixbuf);
504 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
505 new_width, new_height, new_format_name);
507 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
508 &converted_image_data,
509 &converted_image_size,
512 g_object_unref (pixbuf_scaled);
515 g_free (new_format_name);
516 g_free (new_mime_type);
517 avatar_chooser_error_show (chooser,
518 _("Couldn't convert image"),
519 error ? error->message : NULL);
520 g_clear_error (&error);
524 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
525 converted_image_size);
527 if (req->maximum_bytes == 0)
530 /* Make a binary search for the bigest factor that produce
531 * an image data size less than max_size */
532 if (converted_image_size > req->maximum_bytes)
534 if (converted_image_size < req->maximum_bytes)
536 factor = (min_factor + max_factor)/2;
538 /* We are done if either:
539 * - min_factor == max_factor. That happens if we resized to
540 * the max required dimension and the produced data size is
541 * less than max_size.
542 * - The data size is close enough to max_size. Here we accept
543 * a difference of 1k.
545 } while (min_factor != max_factor &&
546 abs (req->maximum_bytes - converted_image_size) > 1024);
547 g_free (new_format_name);
549 /* Takes ownership of new_mime_type and converted_image_data */
550 avatar = empathy_avatar_new ((guchar *) converted_image_data,
551 converted_image_size, new_mime_type, NULL, NULL);
557 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
559 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
562 if (priv->avatar != NULL) {
563 empathy_avatar_unref (priv->avatar);
567 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
568 GTK_ICON_SIZE_DIALOG);
569 gtk_button_set_image (GTK_BUTTON (chooser), image);
570 g_signal_emit (chooser, signals[CHANGED], 0);
574 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
577 gboolean set_locally)
580 EmpathyAvatar *avatar = NULL;
581 gchar *mime_type = NULL;
584 avatar_chooser_clear_image (chooser);
588 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
589 if (pixbuf == NULL) {
595 /* avatar takes ownership of data and mime_type */
596 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL, NULL);
598 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
602 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
603 EmpathyAvatar *avatar,
604 gboolean set_locally)
607 gchar *mime_type = NULL;
609 g_assert (avatar != NULL);
611 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
614 if (pixbuf == NULL) {
615 DEBUG ("couldn't make a pixbuf from avatar; giving up");
619 if (avatar->format == NULL) {
620 avatar->format = mime_type;
622 if (strcmp (mime_type, avatar->format)) {
623 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
624 avatar->format, mime_type);
629 empathy_avatar_ref (avatar);
631 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
635 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
636 EmpathyAvatar *avatar,
638 gboolean set_locally)
640 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
641 GdkPixbuf *pixbuf_view;
644 g_assert (avatar != NULL);
645 g_assert (pixbuf != NULL);
650 conv = avatar_chooser_maybe_convert_and_scale (chooser,
652 empathy_avatar_unref (avatar);
655 /* An error occured; don't change the avatar. */
662 if (priv->avatar != NULL) {
663 empathy_avatar_unref (priv->avatar);
665 priv->avatar = avatar;
667 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
668 image = gtk_image_new_from_pixbuf (pixbuf_view);
670 gtk_button_set_image (GTK_BUTTON (chooser), image);
671 g_signal_emit (chooser, signals[CHANGED], 0);
673 g_object_unref (pixbuf_view);
674 g_object_unref (pixbuf);
678 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
679 const gchar *filename)
681 gchar *image_data = NULL;
682 gsize image_size = 0;
683 GError *error = NULL;
685 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
686 DEBUG ("Failed to load image from '%s': %s", filename,
687 error ? error->message : "No error given");
689 g_clear_error (&error);
693 avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
697 avatar_chooser_drag_motion_cb (GtkWidget *widget,
698 GdkDragContext *context,
702 EmpathyAvatarChooser *chooser)
704 EmpathyAvatarChooserPriv *priv;
707 priv = GET_PRIV (chooser);
709 for (p = context->targets; p != NULL; p = p->next) {
710 gchar *possible_type;
712 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
714 if (!strcmp (possible_type, URI_LIST_TYPE)) {
715 g_free (possible_type);
716 gdk_drag_status (context, GDK_ACTION_COPY, time_);
721 g_free (possible_type);
728 avatar_chooser_drag_leave_cb (GtkWidget *widget,
729 GdkDragContext *context,
731 EmpathyAvatarChooser *chooser)
736 avatar_chooser_drag_drop_cb (GtkWidget *widget,
737 GdkDragContext *context,
741 EmpathyAvatarChooser *chooser)
743 EmpathyAvatarChooserPriv *priv;
746 priv = GET_PRIV (chooser);
748 if (context->targets == NULL) {
752 for (p = context->targets; p != NULL; p = p->next) {
755 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
756 if (!strcmp (possible_type, URI_LIST_TYPE)) {
757 g_free (possible_type);
758 gtk_drag_get_data (widget, context,
759 GDK_POINTER_TO_ATOM (p->data),
765 g_free (possible_type);
772 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
773 GdkDragContext *context,
776 GtkSelectionData *selection_data,
779 EmpathyAvatarChooser *chooser)
782 gboolean handled = FALSE;
784 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
785 if (!strcmp (target_type, URI_LIST_TYPE)) {
791 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
796 uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
797 nl - (gchar *) gtk_selection_data_get_data (selection_data));
799 file = g_file_new_for_uri (uri);
802 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
806 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
810 /* this in turn calls empathy_avatar_new (), which assumes
813 avatar_chooser_set_image_from_data (chooser, data,
818 g_object_unref (file);
821 gtk_drag_finish (context, handled, FALSE, time_);
825 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
826 EmpathyAvatarChooser *chooser)
830 filename = gtk_file_chooser_get_preview_filename (file_chooser);
834 GdkPixbuf *pixbuf = NULL;
835 GdkPixbuf *scaled_pixbuf;
837 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
839 image = gtk_file_chooser_get_preview_widget (file_chooser);
842 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
843 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
844 g_object_unref (scaled_pixbuf);
845 g_object_unref (pixbuf);
847 gtk_image_set_from_stock (GTK_IMAGE (image),
848 "gtk-dialog-question",
849 GTK_ICON_SIZE_DIALOG);
855 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
859 avatar_chooser_response_cb (GtkWidget *widget,
861 EmpathyAvatarChooser *chooser)
863 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
865 priv->chooser_dialog = NULL;
867 if (response == GTK_RESPONSE_OK) {
871 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
872 avatar_chooser_set_image_from_file (chooser, filename);
875 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
877 empathy_conf_set_string (empathy_conf_get (),
878 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
883 else if (response == GTK_RESPONSE_NO) {
884 /* This corresponds to "No Image", not to "Cancel" */
885 avatar_chooser_clear_image (chooser);
888 gtk_widget_destroy (widget);
892 avatar_chooser_clicked_cb (GtkWidget *button,
893 EmpathyAvatarChooser *chooser)
895 GtkFileChooser *chooser_dialog;
897 gchar *saved_dir = NULL;
898 const gchar *default_dir = DEFAULT_DIR;
899 const gchar *pics_dir;
900 GtkFileFilter *filter;
901 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
903 if (priv->chooser_dialog) {
904 gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
908 priv->chooser_dialog = GTK_FILE_CHOOSER (
909 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
910 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
911 GTK_FILE_CHOOSER_ACTION_OPEN,
919 chooser_dialog = priv->chooser_dialog;
920 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
922 /* Get special dirs */
923 empathy_conf_get_string (empathy_conf_get (),
924 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
926 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
930 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
933 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
934 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
938 /* Set current dir to the last one or to DEFAULT_DIR or to home */
940 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
943 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
945 else if (default_dir) {
946 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
948 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
951 /* Add shortcuts to special dirs */
953 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
956 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
959 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
962 /* Setup preview image */
963 image = gtk_image_new ();
964 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
965 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
966 gtk_widget_show (image);
967 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
968 g_signal_connect (chooser_dialog, "update-preview",
969 G_CALLBACK (avatar_chooser_update_preview_cb),
973 filter = gtk_file_filter_new ();
974 gtk_file_filter_set_name (filter, _("Images"));
975 gtk_file_filter_add_pixbuf_formats (filter);
976 gtk_file_chooser_add_filter (chooser_dialog, filter);
977 filter = gtk_file_filter_new ();
978 gtk_file_filter_set_name (filter, _("All Files"));
979 gtk_file_filter_add_pattern (filter, "*");
980 gtk_file_chooser_add_filter (chooser_dialog, filter);
983 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
984 g_signal_connect (chooser_dialog, "response",
985 G_CALLBACK (avatar_chooser_response_cb),
988 gtk_widget_show (GTK_WIDGET (chooser_dialog));
993 * empathy_avatar_chooser_new:
995 * Creates a new #EmpathyAvatarChooser.
997 * Return value: a new #EmpathyAvatarChooser
1000 empathy_avatar_chooser_new ()
1002 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1006 * empathy_avatar_chooser_set:
1007 * @chooser: an #EmpathyAvatarChooser
1008 * @avatar: a new #EmpathyAvatar
1010 * Sets the @chooser to display the avatar indicated by @avatar.
1013 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
1014 EmpathyAvatar *avatar)
1016 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1018 if (avatar != NULL) {
1019 avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
1021 avatar_chooser_clear_image (chooser);
1026 * empathy_avatar_chooser_get_image_data:
1027 * @chooser: an #EmpathyAvatarChooser
1028 * @data: avatar bytes
1029 * @data_size: size of @data
1030 * @mime_type: avatar mime-type
1032 * Gets image data about the currently selected avatar.
1035 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
1038 const gchar **mime_type)
1040 EmpathyAvatarChooserPriv *priv;
1042 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1044 priv = GET_PRIV (chooser);
1046 if (priv->avatar != NULL) {
1048 *data = (gchar *) priv->avatar->data;
1050 if (data_size != NULL) {
1051 *data_size = priv->avatar->len;
1053 if (mime_type != NULL) {
1054 *mime_type = priv->avatar->format;
1060 if (data_size != NULL) {
1063 if (mime_type != NULL) {
1070 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1073 g_return_if_fail (account != NULL);
1075 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1076 g_object_notify (G_OBJECT (self), "connection");