1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB.
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Based on Novell's e-image-chooser.
21 * Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-contact-factory.h>
35 #include "empathy-avatar-chooser.h"
36 #include "empathy-conf.h"
37 #include "empathy-ui-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
42 #define AVATAR_SIZE_SAVE 96
43 #define AVATAR_SIZE_VIEW 64
44 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
48 EmpathyContactFactory *contact_factory;
50 EmpathyTpContactFactory *tp_contact_factory;
52 gulong ready_handler_id;
54 EmpathyAvatar *avatar;
55 } EmpathyAvatarChooserPriv;
57 static void avatar_chooser_finalize (GObject *object);
58 static void avatar_chooser_set_account (EmpathyAvatarChooser *self,
60 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
61 EmpathyAvatar *avatar,
63 gboolean set_locally);
64 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
65 GdkDragContext *context,
69 EmpathyAvatarChooser *chooser);
70 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
71 GdkDragContext *context,
73 EmpathyAvatarChooser *chooser);
74 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
75 GdkDragContext *context,
79 EmpathyAvatarChooser *chooser);
80 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
81 GdkDragContext *context,
84 GtkSelectionData *selection_data,
87 EmpathyAvatarChooser *chooser);
88 static void avatar_chooser_clicked_cb (GtkWidget *button,
89 EmpathyAvatarChooser *chooser);
101 static guint signals [LAST_SIGNAL];
103 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
106 * Drag and drop stuff
108 #define URI_LIST_TYPE "text/uri-list"
111 DND_TARGET_TYPE_URI_LIST
114 static const GtkTargetEntry drop_types[] = {
115 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
119 avatar_chooser_get_property (GObject *object,
124 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
128 g_value_set_object (value, priv->account);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
137 avatar_chooser_set_property (GObject *object,
142 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
146 avatar_chooser_set_account (self, g_value_get_object (value));
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
155 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
157 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158 GParamSpec *param_spec;
160 object_class->finalize = avatar_chooser_finalize;
161 object_class->get_property = avatar_chooser_get_property;
162 object_class->set_property = avatar_chooser_set_property;
165 g_signal_new ("changed",
166 G_TYPE_FROM_CLASS (klass),
170 g_cclosure_marshal_VOID__VOID,
173 param_spec = g_param_spec_object ("account",
175 "McAccount whose avatar should be "
176 "shown and modified by this widget",
179 G_PARAM_STATIC_STRINGS);
180 g_object_class_install_property (object_class,
184 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
188 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
190 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
191 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
193 chooser->priv = priv;
194 gtk_drag_dest_set (GTK_WIDGET (chooser),
195 GTK_DEST_DEFAULT_ALL,
197 G_N_ELEMENTS (drop_types),
200 g_signal_connect (chooser, "drag-motion",
201 G_CALLBACK (avatar_chooser_drag_motion_cb),
203 g_signal_connect (chooser, "drag-leave",
204 G_CALLBACK (avatar_chooser_drag_leave_cb),
206 g_signal_connect (chooser, "drag-drop",
207 G_CALLBACK (avatar_chooser_drag_drop_cb),
209 g_signal_connect (chooser, "drag-data-received",
210 G_CALLBACK (avatar_chooser_drag_data_received_cb),
212 g_signal_connect (chooser, "clicked",
213 G_CALLBACK (avatar_chooser_clicked_cb),
216 priv->contact_factory = empathy_contact_factory_dup_singleton ();
218 empathy_avatar_chooser_set (chooser, NULL);
222 avatar_chooser_finalize (GObject *object)
224 EmpathyAvatarChooserPriv *priv;
226 priv = GET_PRIV (object);
228 avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
229 g_assert (priv->account == NULL);
230 g_assert (priv->tp_contact_factory == NULL);
232 g_object_unref (priv->contact_factory);
234 if (priv->avatar != NULL) {
235 empathy_avatar_unref (priv->avatar);
238 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
242 avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
244 EmpathyAvatarChooser *self)
246 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
249 /* sanity check that we're listening on the right ETpCF */
250 g_assert (priv->tp_contact_factory == tp_cf);
252 ready = empathy_tp_contact_factory_is_ready (tp_cf);
253 gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
257 avatar_chooser_set_account (EmpathyAvatarChooser *self,
260 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
262 if (priv->account != NULL) {
263 g_object_unref (priv->account);
264 priv->account = NULL;
266 g_assert (priv->tp_contact_factory != NULL);
268 g_signal_handler_disconnect (priv->tp_contact_factory,
269 priv->ready_handler_id);
270 priv->ready_handler_id = 0;
272 g_object_unref (priv->tp_contact_factory);
273 priv->tp_contact_factory = NULL;
276 if (account != NULL) {
277 priv->account = g_object_ref (account);
278 priv->tp_contact_factory = g_object_ref (
279 empathy_contact_factory_get_tp_factory (
280 priv->contact_factory, priv->account));
282 priv->ready_handler_id = g_signal_connect (
283 priv->tp_contact_factory, "notify::ready",
284 G_CALLBACK (avatar_chooser_tp_cf_ready_cb), self);
285 avatar_chooser_tp_cf_ready_cb (priv->tp_contact_factory, NULL,
291 avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
292 const gchar *primary_text,
293 const gchar *secondary_text)
298 parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
299 if (!GTK_IS_WINDOW (parent)) {
303 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
309 if (secondary_text != NULL) {
310 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
311 "%s", secondary_text);
314 g_signal_connect (dialog, "response",
315 G_CALLBACK (gtk_widget_destroy), NULL);
316 gtk_widget_show (dialog);
321 str_in_strv (const gchar *str,
328 while (*strv != NULL) {
329 if (g_str_equal (str, *strv)) {
337 /* The caller must free the strings stored in satisfactory_format_name and
338 * satisfactory_mime_type.
341 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
342 gchar **accepted_mime_types,
343 gchar **satisfactory_format_name,
344 gchar **satisfactory_mime_type)
346 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
349 gboolean found = FALSE;
351 *satisfactory_format_name = NULL;
352 *satisfactory_mime_type = NULL;
354 /* If there is no accepted format there is nothing we can do */
355 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
359 /* If the current mime type is good and accepted, don't change it!
360 * jpeg is compress better pictures, but png is better for logos and
361 * could have an alpha layer. */
362 if (str_in_strv (current_mime_type, good_mime_types) &&
363 str_in_strv (current_mime_type, accepted_mime_types)) {
364 *satisfactory_mime_type = g_strdup (current_mime_type);
365 *satisfactory_format_name = g_strdup (current_mime_type +
370 /* The current mime type is either not accepted or not good to use.
371 * Check if one of the good format is supported... */
372 for (i = 0; good_mime_types[i] != NULL; i++) {
373 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
374 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
375 *satisfactory_format_name = g_strdup (good_mime_types[i] +
381 /* Pick the first supported format we can write */
382 formats = gdk_pixbuf_get_formats ();
383 for (l = formats; !found && l != NULL; l = l->next) {
384 GdkPixbufFormat *format = l->data;
385 gchar **format_mime_types;
388 if (!gdk_pixbuf_format_is_writable (format)) {
392 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
393 for (iter = format_mime_types; *iter != NULL; iter++) {
394 if (str_in_strv (*iter, accepted_mime_types)) {
395 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
396 *satisfactory_mime_type = g_strdup (*iter);
401 g_strfreev (format_mime_types);
403 g_slist_free (formats);
408 static EmpathyAvatar *
409 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
411 EmpathyAvatar *avatar)
413 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
414 EmpathyTpContactFactory *tp_cf = priv->tp_contact_factory;
415 guint max_width = 0, max_height = 0, max_size = 0;
416 gchar **mime_types = NULL;
417 gboolean needs_conversion = FALSE;
419 gchar *new_format_name = NULL;
420 gchar *new_mime_type = NULL;
421 gdouble min_factor, max_factor;
423 gchar *converted_image_data = NULL;
424 gsize converted_image_size = 0;
426 /* This should only be called if the user is setting a new avatar,
427 * which should only be allowed once the avatar requirements have been
430 g_return_val_if_fail (tp_cf != NULL, NULL);
431 g_return_val_if_fail (empathy_tp_contact_factory_is_ready (tp_cf),
435 "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
436 "avatar-max-width", &max_width,
437 "avatar-max-height", &max_height,
438 "avatar-max-size", &max_size,
441 /* Smaller is the factor, smaller will be the image.
442 * 0 is an empty image, 1 is the full size. */
447 /* Check if we need to convert to another image format */
448 if (avatar_chooser_need_mime_type_conversion (avatar->format,
452 DEBUG ("Format conversion needed, we'll use mime type '%s' "
453 "and format name '%s'. Current mime type is '%s'",
454 new_mime_type, new_format_name, avatar->format);
455 needs_conversion = TRUE;
457 g_strfreev (mime_types);
459 /* If there is no format we can use, report error to the user. */
460 if (new_mime_type == NULL || new_format_name == NULL) {
461 avatar_chooser_error_show (chooser, _("Couldn't convert image"),
462 _("None of the accepted image formats is "
463 "supported on your system"));
467 /* If width or height are too big, it needs converting. */
468 width = gdk_pixbuf_get_width (pixbuf);
469 height = gdk_pixbuf_get_height (pixbuf);
470 if ((max_width > 0 && width > max_width) ||
471 (max_height > 0 && height > max_height)) {
472 gdouble h_factor, v_factor;
474 h_factor = (gdouble) max_width / width;
475 v_factor = (gdouble) max_height / height;
476 factor = max_factor = MIN (h_factor, v_factor);
478 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
479 width, height, max_width, max_height);
481 needs_conversion = TRUE;
484 /* If the data len is too big and no other conversion is needed,
485 * try with a lower factor. */
486 if (max_size > 0 && avatar->len > max_size && !needs_conversion) {
487 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
488 "(max is %u bytes), conversion needed.",
489 avatar->len, max_size);
492 needs_conversion = TRUE;
495 /* If no conversion is needed, return the avatar */
496 if (!needs_conversion) {
497 g_free (new_format_name);
498 g_free (new_mime_type);
499 return empathy_avatar_ref (avatar);
503 GdkPixbuf *pixbuf_scaled = NULL;
505 gint new_width, new_height;
506 GError *error = NULL;
508 g_free (converted_image_data);
511 new_width = width * factor;
512 new_height = height * factor;
513 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
520 pixbuf_scaled = g_object_ref (pixbuf);
523 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
524 new_width, new_height, new_format_name);
526 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
527 &converted_image_data,
528 &converted_image_size,
533 g_free (new_format_name);
534 g_free (new_mime_type);
535 avatar_chooser_error_show (chooser,
536 _("Couldn't convert image"),
537 error ? error->message : NULL);
538 g_clear_error (&error);
542 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
543 converted_image_size);
548 /* Make a binary search for the bigest factor that produce
549 * an image data size less than max_size */
550 if (converted_image_size > max_size)
552 if (converted_image_size < max_size)
554 factor = (min_factor + max_factor)/2;
556 /* We are done if either:
557 * - min_factor == max_factor. That happens if we resized to
558 * the max required dimension and the produced data size is
559 * less than max_size.
560 * - The data size is close enough to max_size. Here we accept
561 * a difference of 1k.
563 } while (min_factor != max_factor &&
564 ABS (max_size - converted_image_size) > 1024);
565 g_free (new_format_name);
567 /* Takes ownership of new_mime_type and converted_image_data */
568 avatar = empathy_avatar_new (converted_image_data,
569 converted_image_size, new_mime_type, NULL);
575 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
577 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
580 if (priv->avatar == NULL) {
584 empathy_avatar_unref (priv->avatar);
587 image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
588 gtk_button_set_image (GTK_BUTTON (chooser), image);
589 g_signal_emit (chooser, signals[CHANGED], 0);
593 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
596 gboolean set_locally)
599 EmpathyAvatar *avatar = NULL;
600 gchar *mime_type = NULL;
603 avatar_chooser_clear_image (chooser);
607 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
608 if (pixbuf == NULL) {
613 /* avatar takes ownership of data and mime_type */
614 avatar = empathy_avatar_new (data, size, mime_type, NULL);
616 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
620 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
621 EmpathyAvatar *avatar,
622 gboolean set_locally)
625 gchar *mime_type = NULL;
627 g_assert (avatar != NULL);
629 pixbuf = empathy_pixbuf_from_data_and_mime (avatar->data,
632 if (pixbuf == NULL) {
633 DEBUG ("couldn't make a pixbuf from avatar; giving up");
637 if (avatar->format == NULL) {
638 avatar->format = mime_type;
640 if (strcmp (mime_type, avatar->format)) {
641 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
642 avatar->format, mime_type);
647 empathy_avatar_ref (avatar);
649 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
653 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
654 EmpathyAvatar *avatar,
656 gboolean set_locally)
658 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
659 GdkPixbuf *pixbuf_view;
662 g_assert (avatar != NULL);
663 g_assert (pixbuf != NULL);
668 conv = avatar_chooser_maybe_convert_and_scale (chooser,
670 empathy_avatar_unref (avatar);
673 /* An error occured; don't change the avatar. */
680 if (priv->avatar != NULL) {
681 empathy_avatar_unref (priv->avatar);
683 priv->avatar = avatar;
685 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
686 image = gtk_image_new_from_pixbuf (pixbuf_view);
688 gtk_button_set_image (GTK_BUTTON (chooser), image);
689 g_signal_emit (chooser, signals[CHANGED], 0);
691 g_object_unref (pixbuf_view);
692 g_object_unref (pixbuf);
696 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
697 const gchar *filename)
699 gchar *image_data = NULL;
700 gsize image_size = 0;
701 GError *error = NULL;
703 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
704 DEBUG ("Failed to load image from '%s': %s", filename,
705 error ? error->message : "No error given");
707 g_clear_error (&error);
711 avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
715 avatar_chooser_drag_motion_cb (GtkWidget *widget,
716 GdkDragContext *context,
720 EmpathyAvatarChooser *chooser)
722 EmpathyAvatarChooserPriv *priv;
725 priv = GET_PRIV (chooser);
727 for (p = context->targets; p != NULL; p = p->next) {
728 gchar *possible_type;
730 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
732 if (!strcmp (possible_type, URI_LIST_TYPE)) {
733 g_free (possible_type);
734 gdk_drag_status (context, GDK_ACTION_COPY, time);
739 g_free (possible_type);
746 avatar_chooser_drag_leave_cb (GtkWidget *widget,
747 GdkDragContext *context,
749 EmpathyAvatarChooser *chooser)
754 avatar_chooser_drag_drop_cb (GtkWidget *widget,
755 GdkDragContext *context,
759 EmpathyAvatarChooser *chooser)
761 EmpathyAvatarChooserPriv *priv;
764 priv = GET_PRIV (chooser);
766 if (context->targets == NULL) {
770 for (p = context->targets; p != NULL; p = p->next) {
773 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
774 if (!strcmp (possible_type, URI_LIST_TYPE)) {
775 g_free (possible_type);
776 gtk_drag_get_data (widget, context,
777 GDK_POINTER_TO_ATOM (p->data),
783 g_free (possible_type);
790 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
791 GdkDragContext *context,
794 GtkSelectionData *selection_data,
797 EmpathyAvatarChooser *chooser)
800 gboolean handled = FALSE;
802 target_type = gdk_atom_name (selection_data->target);
803 if (!strcmp (target_type, URI_LIST_TYPE)) {
805 GFileInputStream *input_stream;
809 nl = strstr (selection_data->data, "\r\n");
813 uri = g_strndup (selection_data->data,
814 nl - (gchar*) selection_data->data);
816 file = g_file_new_for_uri (uri);
819 file = g_file_new_for_uri (selection_data->data);
822 input_stream = g_file_read (file, NULL, NULL);
824 if (input_stream != NULL) {
827 info = g_file_query_info (file,
828 G_FILE_ATTRIBUTE_STANDARD_SIZE,
834 size = g_file_info_get_size (info);
835 data = g_malloc (size);
837 bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
840 if (bytes_read != -1) {
841 avatar_chooser_set_image_from_data (
849 g_object_unref (info);
852 g_object_unref (input_stream);
855 g_object_unref (file);
858 gtk_drag_finish (context, handled, FALSE, time);
862 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
863 EmpathyAvatarChooser *chooser)
867 filename = gtk_file_chooser_get_preview_filename (file_chooser);
871 GdkPixbuf *pixbuf = NULL;
872 GdkPixbuf *scaled_pixbuf;
874 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
876 image = gtk_file_chooser_get_preview_widget (file_chooser);
879 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
880 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
881 g_object_unref (scaled_pixbuf);
882 g_object_unref (pixbuf);
884 gtk_image_set_from_stock (GTK_IMAGE (image),
885 "gtk-dialog-question",
886 GTK_ICON_SIZE_DIALOG);
890 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
894 avatar_chooser_response_cb (GtkWidget *widget,
896 EmpathyAvatarChooser *chooser)
898 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
900 if (response == GTK_RESPONSE_CANCEL) {
904 /* Check if we went non-ready since displaying the dialog. */
905 if (!empathy_tp_contact_factory_is_ready (priv->tp_contact_factory)) {
906 DEBUG ("Can't set avatar when contact factory isn't ready.");
910 if (response == GTK_RESPONSE_OK) {
914 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
915 avatar_chooser_set_image_from_file (chooser, filename);
918 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
920 empathy_conf_set_string (empathy_conf_get (),
921 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
926 else if (response == GTK_RESPONSE_NO) {
927 /* This corresponds to "No Image", not to "Cancel" */
928 avatar_chooser_clear_image (chooser);
932 gtk_widget_destroy (widget);
936 avatar_chooser_clicked_cb (GtkWidget *button,
937 EmpathyAvatarChooser *chooser)
939 GtkFileChooser *chooser_dialog;
941 gchar *saved_dir = NULL;
942 const gchar *default_dir = DEFAULT_DIR;
943 const gchar *pics_dir;
944 GtkFileFilter *filter;
946 chooser_dialog = GTK_FILE_CHOOSER (
947 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
948 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
949 GTK_FILE_CHOOSER_ACTION_OPEN,
958 /* Get special dirs */
959 empathy_conf_get_string (empathy_conf_get (),
960 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
962 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
966 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
969 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
970 if (pics_dir && !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 */
976 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
979 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
981 else if (default_dir) {
982 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
984 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
987 /* Add shortcuts to special dirs */
989 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
992 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
995 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
998 /* Setup preview image */
999 image = gtk_image_new ();
1000 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1001 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1002 gtk_widget_show (image);
1003 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1004 g_signal_connect (chooser_dialog, "update-preview",
1005 G_CALLBACK (avatar_chooser_update_preview_cb),
1009 filter = gtk_file_filter_new ();
1010 gtk_file_filter_set_name (filter, _("Images"));
1011 gtk_file_filter_add_pixbuf_formats (filter);
1012 gtk_file_chooser_add_filter (chooser_dialog, filter);
1013 filter = gtk_file_filter_new ();
1014 gtk_file_filter_set_name (filter, _("All Files"));
1015 gtk_file_filter_add_pattern(filter, "*");
1016 gtk_file_chooser_add_filter (chooser_dialog, filter);
1018 /* Setup response */
1019 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
1020 g_signal_connect (chooser_dialog, "response",
1021 G_CALLBACK (avatar_chooser_response_cb),
1024 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1029 empathy_avatar_chooser_new ()
1031 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1035 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
1036 EmpathyAvatar *avatar)
1038 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1040 if (avatar != NULL) {
1041 avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
1043 avatar_chooser_clear_image (chooser);
1048 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
1051 const gchar **mime_type)
1053 EmpathyAvatarChooserPriv *priv;
1055 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1057 priv = GET_PRIV (chooser);
1059 if (priv->avatar != NULL) {
1061 *data = priv->avatar->data;
1063 if (data_size != NULL) {
1064 *data_size = priv->avatar->len;
1066 if (mime_type != NULL) {
1067 *mime_type = priv->avatar->format;
1073 if (data_size != NULL) {
1076 if (mime_type != NULL) {