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>
33 #include <libempathy/empathy-tp-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>
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 EmpathyTpContactFactory
*factory
;
66 TpConnection
*connection
;
67 GtkFileChooser
*chooser_dialog
;
69 gulong ready_handler_id
;
71 EmpathyAvatar
*avatar
;
72 } EmpathyAvatarChooserPriv
;
74 static void avatar_chooser_finalize (GObject
*object
);
75 static void avatar_chooser_set_connection (EmpathyAvatarChooser
*self
,
76 TpConnection
*connection
);
77 static void avatar_chooser_set_image (EmpathyAvatarChooser
*chooser
,
78 EmpathyAvatar
*avatar
,
80 gboolean set_locally
);
81 static gboolean
avatar_chooser_drag_motion_cb (GtkWidget
*widget
,
82 GdkDragContext
*context
,
86 EmpathyAvatarChooser
*chooser
);
87 static void avatar_chooser_drag_leave_cb (GtkWidget
*widget
,
88 GdkDragContext
*context
,
90 EmpathyAvatarChooser
*chooser
);
91 static gboolean
avatar_chooser_drag_drop_cb (GtkWidget
*widget
,
92 GdkDragContext
*context
,
96 EmpathyAvatarChooser
*chooser
);
97 static void avatar_chooser_drag_data_received_cb (GtkWidget
*widget
,
98 GdkDragContext
*context
,
101 GtkSelectionData
*selection_data
,
104 EmpathyAvatarChooser
*chooser
);
105 static void avatar_chooser_clicked_cb (GtkWidget
*button
,
106 EmpathyAvatarChooser
*chooser
);
118 static guint signals
[LAST_SIGNAL
];
120 G_DEFINE_TYPE (EmpathyAvatarChooser
, empathy_avatar_chooser
, GTK_TYPE_BUTTON
);
123 * Drag and drop stuff
125 #define URI_LIST_TYPE "text/uri-list"
128 DND_TARGET_TYPE_URI_LIST
131 static const GtkTargetEntry drop_types
[] = {
132 { URI_LIST_TYPE
, 0, DND_TARGET_TYPE_URI_LIST
},
136 avatar_chooser_get_property (GObject
*object
,
141 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (object
);
144 case PROP_CONNECTION
:
145 g_value_set_object (value
, priv
->connection
);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
154 avatar_chooser_set_property (GObject
*object
,
159 EmpathyAvatarChooser
*self
= EMPATHY_AVATAR_CHOOSER (object
);
162 case PROP_CONNECTION
:
163 avatar_chooser_set_connection (self
, g_value_get_object (value
));
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
172 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass
*klass
)
174 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
175 GParamSpec
*param_spec
;
177 object_class
->finalize
= avatar_chooser_finalize
;
178 object_class
->get_property
= avatar_chooser_get_property
;
179 object_class
->set_property
= avatar_chooser_set_property
;
182 * EmpathyAvatarChooser::changed:
183 * @chooser: an #EmpathyAvatarChooser
185 * Emitted when the chosen avatar has changed.
189 g_signal_new ("changed",
190 G_TYPE_FROM_CLASS (klass
),
194 g_cclosure_marshal_VOID__VOID
,
198 * EmpathyAvatarChooser:connection:
200 * The #TpConnection whose avatar should be shown and modified by
201 * the #EmpathyAvatarChooser instance.
203 param_spec
= g_param_spec_object ("connection",
205 "TpConnection whose avatar should be "
206 "shown and modified by this widget",
209 G_PARAM_STATIC_STRINGS
);
210 g_object_class_install_property (object_class
,
214 g_type_class_add_private (object_class
, sizeof (EmpathyAvatarChooserPriv
));
218 empathy_avatar_chooser_init (EmpathyAvatarChooser
*chooser
)
220 EmpathyAvatarChooserPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (chooser
,
221 EMPATHY_TYPE_AVATAR_CHOOSER
, EmpathyAvatarChooserPriv
);
223 chooser
->priv
= priv
;
224 gtk_drag_dest_set (GTK_WIDGET (chooser
),
225 GTK_DEST_DEFAULT_ALL
,
227 G_N_ELEMENTS (drop_types
),
230 g_signal_connect (chooser
, "drag-motion",
231 G_CALLBACK (avatar_chooser_drag_motion_cb
),
233 g_signal_connect (chooser
, "drag-leave",
234 G_CALLBACK (avatar_chooser_drag_leave_cb
),
236 g_signal_connect (chooser
, "drag-drop",
237 G_CALLBACK (avatar_chooser_drag_drop_cb
),
239 g_signal_connect (chooser
, "drag-data-received",
240 G_CALLBACK (avatar_chooser_drag_data_received_cb
),
242 g_signal_connect (chooser
, "clicked",
243 G_CALLBACK (avatar_chooser_clicked_cb
),
246 empathy_avatar_chooser_set (chooser
, NULL
);
250 avatar_chooser_finalize (GObject
*object
)
252 EmpathyAvatarChooserPriv
*priv
;
254 priv
= GET_PRIV (object
);
256 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object
), NULL
);
257 g_assert (priv
->connection
== NULL
);
258 g_assert (priv
->factory
== NULL
);
260 if (priv
->avatar
!= NULL
) {
261 empathy_avatar_unref (priv
->avatar
);
264 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class
)->finalize (object
);
268 avatar_chooser_set_connection (EmpathyAvatarChooser
*self
,
269 TpConnection
*connection
)
271 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (self
);
273 if (priv
->connection
!= NULL
) {
274 g_object_unref (priv
->connection
);
275 priv
->connection
= NULL
;
277 g_object_unref (priv
->factory
);
278 priv
->factory
= NULL
;
281 if (connection
!= NULL
) {
282 priv
->connection
= g_object_ref (connection
);
283 priv
->factory
= empathy_tp_contact_factory_dup_singleton (connection
);
288 avatar_chooser_error_show (EmpathyAvatarChooser
*chooser
,
289 const gchar
*primary_text
,
290 const gchar
*secondary_text
)
295 parent
= gtk_widget_get_toplevel (GTK_WIDGET (chooser
));
296 if (!GTK_IS_WINDOW (parent
)) {
300 dialog
= gtk_message_dialog_new (parent
? GTK_WINDOW (parent
) : NULL
,
306 if (secondary_text
!= NULL
) {
307 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog
),
308 "%s", secondary_text
);
311 g_signal_connect (dialog
, "response",
312 G_CALLBACK (gtk_widget_destroy
), NULL
);
313 gtk_widget_show (dialog
);
318 str_in_strv (const gchar
*str
,
325 while (*strv
!= NULL
) {
326 if (g_str_equal (str
, *strv
)) {
334 /* The caller must free the strings stored in satisfactory_format_name and
335 * satisfactory_mime_type.
338 avatar_chooser_need_mime_type_conversion (const gchar
*current_mime_type
,
339 gchar
**accepted_mime_types
,
340 gchar
**satisfactory_format_name
,
341 gchar
**satisfactory_mime_type
)
343 gchar
*good_mime_types
[] = {"image/jpeg", "image/png", NULL
};
346 gboolean found
= FALSE
;
348 *satisfactory_format_name
= NULL
;
349 *satisfactory_mime_type
= NULL
;
351 /* If there is no accepted format there is nothing we can do */
352 if (accepted_mime_types
== NULL
|| *accepted_mime_types
== NULL
) {
356 /* If the current mime type is good and accepted, don't change it!
357 * jpeg is compress better pictures, but png is better for logos and
358 * could have an alpha layer. */
359 if (str_in_strv (current_mime_type
, good_mime_types
) &&
360 str_in_strv (current_mime_type
, accepted_mime_types
)) {
361 *satisfactory_mime_type
= g_strdup (current_mime_type
);
362 *satisfactory_format_name
= g_strdup (current_mime_type
+
367 /* The current mime type is either not accepted or not good to use.
368 * Check if one of the good format is supported... */
369 for (i
= 0; good_mime_types
[i
] != NULL
; i
++) {
370 if (str_in_strv (good_mime_types
[i
], accepted_mime_types
)) {
371 *satisfactory_mime_type
= g_strdup (good_mime_types
[i
]);
372 *satisfactory_format_name
= g_strdup (good_mime_types
[i
] +
378 /* Pick the first supported format we can write */
379 formats
= gdk_pixbuf_get_formats ();
380 for (l
= formats
; !found
&& l
!= NULL
; l
= l
->next
) {
381 GdkPixbufFormat
*format
= l
->data
;
382 gchar
**format_mime_types
;
385 if (!gdk_pixbuf_format_is_writable (format
)) {
389 format_mime_types
= gdk_pixbuf_format_get_mime_types (format
);
390 for (iter
= format_mime_types
; *iter
!= NULL
; iter
++) {
391 if (str_in_strv (*iter
, accepted_mime_types
)) {
392 *satisfactory_format_name
= gdk_pixbuf_format_get_name (format
);
393 *satisfactory_mime_type
= g_strdup (*iter
);
398 g_strfreev (format_mime_types
);
400 g_slist_free (formats
);
405 static EmpathyAvatar
*
406 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser
*chooser
,
408 EmpathyAvatar
*avatar
)
410 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (chooser
);
411 guint max_width
= 0, max_height
= 0, max_size
= 0;
412 gchar
**mime_types
= NULL
;
413 gboolean needs_conversion
= FALSE
;
415 gchar
*new_format_name
= NULL
;
416 gchar
*new_mime_type
= NULL
;
417 gdouble min_factor
, max_factor
;
419 gchar
*converted_image_data
= NULL
;
420 gsize converted_image_size
= 0;
422 g_object_get (priv
->factory
,
423 "avatar-mime-types", &mime_types
, /* Needs g_strfreev-ing */
424 "avatar-max-width", &max_width
,
425 "avatar-max-height", &max_height
,
426 "avatar-max-size", &max_size
,
429 /* Smaller is the factor, smaller will be the image.
430 * 0 is an empty image, 1 is the full size. */
435 /* Check if we need to convert to another image format */
436 if (avatar_chooser_need_mime_type_conversion (avatar
->format
,
440 DEBUG ("Format conversion needed, we'll use mime type '%s' "
441 "and format name '%s'. Current mime type is '%s'",
442 new_mime_type
, new_format_name
, avatar
->format
);
443 needs_conversion
= TRUE
;
445 g_strfreev (mime_types
);
447 /* If there is no format we can use, report error to the user. */
448 if (new_mime_type
== NULL
|| new_format_name
== NULL
) {
449 avatar_chooser_error_show (chooser
, _("Couldn't convert image"),
450 _("None of the accepted image formats are "
451 "supported on your system"));
455 /* If width or height are too big, it needs converting. */
456 width
= gdk_pixbuf_get_width (pixbuf
);
457 height
= gdk_pixbuf_get_height (pixbuf
);
458 if ((max_width
> 0 && width
> max_width
) ||
459 (max_height
> 0 && height
> max_height
)) {
460 gdouble h_factor
, v_factor
;
462 h_factor
= (gdouble
) max_width
/ width
;
463 v_factor
= (gdouble
) max_height
/ height
;
464 factor
= max_factor
= MIN (h_factor
, v_factor
);
466 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
467 width
, height
, max_width
, max_height
);
469 needs_conversion
= TRUE
;
472 /* If the data len is too big and no other conversion is needed,
473 * try with a lower factor. */
474 if (max_size
> 0 && avatar
->len
> max_size
&& !needs_conversion
) {
475 DEBUG ("Image data (%"G_GSIZE_FORMAT
" bytes) is too big "
476 "(max is %u bytes), conversion needed.",
477 avatar
->len
, max_size
);
480 needs_conversion
= TRUE
;
483 /* If no conversion is needed, return the avatar */
484 if (!needs_conversion
) {
485 g_free (new_format_name
);
486 g_free (new_mime_type
);
487 return empathy_avatar_ref (avatar
);
491 GdkPixbuf
*pixbuf_scaled
= NULL
;
493 gint new_width
, new_height
;
494 GError
*error
= NULL
;
496 g_free (converted_image_data
);
499 new_width
= width
* factor
;
500 new_height
= height
* factor
;
501 pixbuf_scaled
= gdk_pixbuf_scale_simple (pixbuf
,
508 pixbuf_scaled
= g_object_ref (pixbuf
);
511 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor
,
512 new_width
, new_height
, new_format_name
);
514 saved
= gdk_pixbuf_save_to_buffer (pixbuf_scaled
,
515 &converted_image_data
,
516 &converted_image_size
,
519 g_object_unref (pixbuf_scaled
);
522 g_free (new_format_name
);
523 g_free (new_mime_type
);
524 avatar_chooser_error_show (chooser
,
525 _("Couldn't convert image"),
526 error
? error
->message
: NULL
);
527 g_clear_error (&error
);
531 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT
" bytes.",
532 converted_image_size
);
537 /* Make a binary search for the bigest factor that produce
538 * an image data size less than max_size */
539 if (converted_image_size
> max_size
)
541 if (converted_image_size
< max_size
)
543 factor
= (min_factor
+ max_factor
)/2;
545 /* We are done if either:
546 * - min_factor == max_factor. That happens if we resized to
547 * the max required dimension and the produced data size is
548 * less than max_size.
549 * - The data size is close enough to max_size. Here we accept
550 * a difference of 1k.
552 } while (min_factor
!= max_factor
&&
553 abs (max_size
- converted_image_size
) > 1024);
554 g_free (new_format_name
);
556 /* Takes ownership of new_mime_type and converted_image_data */
557 avatar
= empathy_avatar_new ((guchar
*) converted_image_data
,
558 converted_image_size
, new_mime_type
, NULL
, NULL
);
564 avatar_chooser_clear_image (EmpathyAvatarChooser
*chooser
)
566 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (chooser
);
569 if (priv
->avatar
!= NULL
) {
570 empathy_avatar_unref (priv
->avatar
);
574 image
= gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG
);
575 gtk_button_set_image (GTK_BUTTON (chooser
), image
);
576 g_signal_emit (chooser
, signals
[CHANGED
], 0);
580 avatar_chooser_set_image_from_data (EmpathyAvatarChooser
*chooser
,
583 gboolean set_locally
)
586 EmpathyAvatar
*avatar
= NULL
;
587 gchar
*mime_type
= NULL
;
590 avatar_chooser_clear_image (chooser
);
594 pixbuf
= empathy_pixbuf_from_data_and_mime (data
, size
, &mime_type
);
595 if (pixbuf
== NULL
) {
601 /* avatar takes ownership of data and mime_type */
602 avatar
= empathy_avatar_new ((guchar
*) data
, size
, mime_type
, NULL
, NULL
);
604 avatar_chooser_set_image (chooser
, avatar
, pixbuf
, set_locally
);
608 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser
*chooser
,
609 EmpathyAvatar
*avatar
,
610 gboolean set_locally
)
613 gchar
*mime_type
= NULL
;
615 g_assert (avatar
!= NULL
);
617 pixbuf
= empathy_pixbuf_from_data_and_mime ((gchar
*) avatar
->data
,
620 if (pixbuf
== NULL
) {
621 DEBUG ("couldn't make a pixbuf from avatar; giving up");
625 if (avatar
->format
== NULL
) {
626 avatar
->format
= mime_type
;
628 if (strcmp (mime_type
, avatar
->format
)) {
629 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
630 avatar
->format
, mime_type
);
635 empathy_avatar_ref (avatar
);
637 avatar_chooser_set_image (chooser
, avatar
, pixbuf
, set_locally
);
641 avatar_chooser_set_image (EmpathyAvatarChooser
*chooser
,
642 EmpathyAvatar
*avatar
,
644 gboolean set_locally
)
646 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (chooser
);
647 GdkPixbuf
*pixbuf_view
;
650 g_assert (avatar
!= NULL
);
651 g_assert (pixbuf
!= NULL
);
656 conv
= avatar_chooser_maybe_convert_and_scale (chooser
,
658 empathy_avatar_unref (avatar
);
661 /* An error occured; don't change the avatar. */
668 if (priv
->avatar
!= NULL
) {
669 empathy_avatar_unref (priv
->avatar
);
671 priv
->avatar
= avatar
;
673 pixbuf_view
= empathy_pixbuf_scale_down_if_necessary (pixbuf
, AVATAR_SIZE_VIEW
);
674 image
= gtk_image_new_from_pixbuf (pixbuf_view
);
676 gtk_button_set_image (GTK_BUTTON (chooser
), image
);
677 g_signal_emit (chooser
, signals
[CHANGED
], 0);
679 g_object_unref (pixbuf_view
);
680 g_object_unref (pixbuf
);
684 avatar_chooser_set_image_from_file (EmpathyAvatarChooser
*chooser
,
685 const gchar
*filename
)
687 gchar
*image_data
= NULL
;
688 gsize image_size
= 0;
689 GError
*error
= NULL
;
691 if (!g_file_get_contents (filename
, &image_data
, &image_size
, &error
)) {
692 DEBUG ("Failed to load image from '%s': %s", filename
,
693 error
? error
->message
: "No error given");
695 g_clear_error (&error
);
699 avatar_chooser_set_image_from_data (chooser
, image_data
, image_size
, TRUE
);
703 avatar_chooser_drag_motion_cb (GtkWidget
*widget
,
704 GdkDragContext
*context
,
708 EmpathyAvatarChooser
*chooser
)
710 EmpathyAvatarChooserPriv
*priv
;
713 priv
= GET_PRIV (chooser
);
715 for (p
= context
->targets
; p
!= NULL
; p
= p
->next
) {
716 gchar
*possible_type
;
718 possible_type
= gdk_atom_name (GDK_POINTER_TO_ATOM (p
->data
));
720 if (!strcmp (possible_type
, URI_LIST_TYPE
)) {
721 g_free (possible_type
);
722 gdk_drag_status (context
, GDK_ACTION_COPY
, time_
);
727 g_free (possible_type
);
734 avatar_chooser_drag_leave_cb (GtkWidget
*widget
,
735 GdkDragContext
*context
,
737 EmpathyAvatarChooser
*chooser
)
742 avatar_chooser_drag_drop_cb (GtkWidget
*widget
,
743 GdkDragContext
*context
,
747 EmpathyAvatarChooser
*chooser
)
749 EmpathyAvatarChooserPriv
*priv
;
752 priv
= GET_PRIV (chooser
);
754 if (context
->targets
== NULL
) {
758 for (p
= context
->targets
; p
!= NULL
; p
= p
->next
) {
761 possible_type
= gdk_atom_name (GDK_POINTER_TO_ATOM (p
->data
));
762 if (!strcmp (possible_type
, URI_LIST_TYPE
)) {
763 g_free (possible_type
);
764 gtk_drag_get_data (widget
, context
,
765 GDK_POINTER_TO_ATOM (p
->data
),
771 g_free (possible_type
);
778 avatar_chooser_drag_data_received_cb (GtkWidget
*widget
,
779 GdkDragContext
*context
,
782 GtkSelectionData
*selection_data
,
785 EmpathyAvatarChooser
*chooser
)
788 gboolean handled
= FALSE
;
790 target_type
= gdk_atom_name (gtk_selection_data_get_target (selection_data
));
791 if (!strcmp (target_type
, URI_LIST_TYPE
)) {
797 nl
= strstr ((gchar
*) gtk_selection_data_get_data (selection_data
),
802 uri
= g_strndup ((gchar
*) gtk_selection_data_get_data (selection_data
),
803 nl
- (gchar
*) gtk_selection_data_get_data (selection_data
));
805 file
= g_file_new_for_uri (uri
);
808 file
= g_file_new_for_uri ((gchar
*) gtk_selection_data_get_data (
812 handled
= g_file_load_contents (file
, NULL
, &data
, &bytes_read
,
816 /* this in turn calls empathy_avatar_new (), which assumes
819 avatar_chooser_set_image_from_data (chooser
, data
,
824 g_object_unref (file
);
827 gtk_drag_finish (context
, handled
, FALSE
, time_
);
831 avatar_chooser_update_preview_cb (GtkFileChooser
*file_chooser
,
832 EmpathyAvatarChooser
*chooser
)
836 filename
= gtk_file_chooser_get_preview_filename (file_chooser
);
840 GdkPixbuf
*pixbuf
= NULL
;
841 GdkPixbuf
*scaled_pixbuf
;
843 pixbuf
= gdk_pixbuf_new_from_file (filename
, NULL
);
845 image
= gtk_file_chooser_get_preview_widget (file_chooser
);
848 scaled_pixbuf
= empathy_pixbuf_scale_down_if_necessary (pixbuf
, AVATAR_SIZE_SAVE
);
849 gtk_image_set_from_pixbuf (GTK_IMAGE (image
), scaled_pixbuf
);
850 g_object_unref (scaled_pixbuf
);
851 g_object_unref (pixbuf
);
853 gtk_image_set_from_stock (GTK_IMAGE (image
),
854 "gtk-dialog-question",
855 GTK_ICON_SIZE_DIALOG
);
861 gtk_file_chooser_set_preview_widget_active (file_chooser
, TRUE
);
865 avatar_chooser_response_cb (GtkWidget
*widget
,
867 EmpathyAvatarChooser
*chooser
)
869 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (chooser
);
871 priv
->chooser_dialog
= NULL
;
873 if (response
== GTK_RESPONSE_OK
) {
877 filename
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget
));
878 avatar_chooser_set_image_from_file (chooser
, filename
);
881 path
= gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget
));
883 empathy_conf_set_string (empathy_conf_get (),
884 EMPATHY_PREFS_UI_AVATAR_DIRECTORY
,
889 else if (response
== GTK_RESPONSE_NO
) {
890 /* This corresponds to "No Image", not to "Cancel" */
891 avatar_chooser_clear_image (chooser
);
894 gtk_widget_destroy (widget
);
898 avatar_chooser_clicked_cb (GtkWidget
*button
,
899 EmpathyAvatarChooser
*chooser
)
901 GtkFileChooser
*chooser_dialog
;
903 gchar
*saved_dir
= NULL
;
904 const gchar
*default_dir
= DEFAULT_DIR
;
905 const gchar
*pics_dir
;
906 GtkFileFilter
*filter
;
907 EmpathyAvatarChooserPriv
*priv
= GET_PRIV (chooser
);
909 if (priv
->chooser_dialog
) {
910 gtk_window_present (GTK_WINDOW (priv
->chooser_dialog
));
914 priv
->chooser_dialog
= GTK_FILE_CHOOSER (
915 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
916 empathy_get_toplevel_window (GTK_WIDGET (chooser
)),
917 GTK_FILE_CHOOSER_ACTION_OPEN
,
925 chooser_dialog
= priv
->chooser_dialog
;
926 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog
), TRUE
);
928 /* Get special dirs */
929 empathy_conf_get_string (empathy_conf_get (),
930 EMPATHY_PREFS_UI_AVATAR_DIRECTORY
,
932 if (saved_dir
&& !g_file_test (saved_dir
, G_FILE_TEST_IS_DIR
)) {
936 if (!g_file_test (default_dir
, G_FILE_TEST_IS_DIR
)) {
939 pics_dir
= g_get_user_special_dir (G_USER_DIRECTORY_PICTURES
);
940 if (pics_dir
&& !g_file_test (pics_dir
, G_FILE_TEST_IS_DIR
)) {
944 /* Set current dir to the last one or to DEFAULT_DIR or to home */
946 gtk_file_chooser_set_current_folder (chooser_dialog
, saved_dir
);
949 gtk_file_chooser_set_current_folder (chooser_dialog
, pics_dir
);
951 else if (default_dir
) {
952 gtk_file_chooser_set_current_folder (chooser_dialog
, default_dir
);
954 gtk_file_chooser_set_current_folder (chooser_dialog
, g_get_home_dir ());
957 /* Add shortcuts to special dirs */
959 gtk_file_chooser_add_shortcut_folder (chooser_dialog
, saved_dir
, NULL
);
962 gtk_file_chooser_add_shortcut_folder (chooser_dialog
, pics_dir
, NULL
);
965 gtk_file_chooser_add_shortcut_folder (chooser_dialog
, default_dir
, NULL
);
968 /* Setup preview image */
969 image
= gtk_image_new ();
970 gtk_file_chooser_set_preview_widget (chooser_dialog
, image
);
971 gtk_widget_set_size_request (image
, AVATAR_SIZE_SAVE
, AVATAR_SIZE_SAVE
);
972 gtk_widget_show (image
);
973 gtk_file_chooser_set_use_preview_label (chooser_dialog
, FALSE
);
974 g_signal_connect (chooser_dialog
, "update-preview",
975 G_CALLBACK (avatar_chooser_update_preview_cb
),
979 filter
= gtk_file_filter_new ();
980 gtk_file_filter_set_name (filter
, _("Images"));
981 gtk_file_filter_add_pixbuf_formats (filter
);
982 gtk_file_chooser_add_filter (chooser_dialog
, filter
);
983 filter
= gtk_file_filter_new ();
984 gtk_file_filter_set_name (filter
, _("All Files"));
985 gtk_file_filter_add_pattern (filter
, "*");
986 gtk_file_chooser_add_filter (chooser_dialog
, filter
);
989 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog
), GTK_RESPONSE_OK
);
990 g_signal_connect (chooser_dialog
, "response",
991 G_CALLBACK (avatar_chooser_response_cb
),
994 gtk_widget_show (GTK_WIDGET (chooser_dialog
));
999 * empathy_avatar_chooser_new:
1001 * Creates a new #EmpathyAvatarChooser.
1003 * Return value: a new #EmpathyAvatarChooser
1006 empathy_avatar_chooser_new ()
1008 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER
, NULL
);
1012 * empathy_avatar_chooser_set:
1013 * @chooser: an #EmpathyAvatarChooser
1014 * @avatar: a new #EmpathyAvatar
1016 * Sets the @chooser to display the avatar indicated by @avatar.
1019 empathy_avatar_chooser_set (EmpathyAvatarChooser
*chooser
,
1020 EmpathyAvatar
*avatar
)
1022 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser
));
1024 if (avatar
!= NULL
) {
1025 avatar_chooser_set_image_from_avatar (chooser
, avatar
, FALSE
);
1027 avatar_chooser_clear_image (chooser
);
1032 * empathy_avatar_chooser_get_image_data:
1033 * @chooser: an #EmpathyAvatarChooser
1034 * @data: avatar bytes
1035 * @data_size: size of @data
1036 * @mime_type: avatar mime-type
1038 * Gets image data about the currently selected avatar.
1041 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser
*chooser
,
1044 const gchar
**mime_type
)
1046 EmpathyAvatarChooserPriv
*priv
;
1048 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser
));
1050 priv
= GET_PRIV (chooser
);
1052 if (priv
->avatar
!= NULL
) {
1054 *data
= (gchar
*) priv
->avatar
->data
;
1056 if (data_size
!= NULL
) {
1057 *data_size
= priv
->avatar
->len
;
1059 if (mime_type
!= NULL
) {
1060 *mime_type
= priv
->avatar
->format
;
1066 if (data_size
!= NULL
) {
1069 if (mime_type
!= NULL
) {