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;
89 static void avatar_chooser_finalize (GObject *object);
90 static void avatar_chooser_set_connection (EmpathyAvatarChooser *self,
91 TpConnection *connection);
92 static void avatar_chooser_set_image (EmpathyAvatarChooser *self,
93 EmpathyAvatar *avatar,
95 gboolean set_locally);
96 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
97 GdkDragContext *context,
101 EmpathyAvatarChooser *self);
102 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
103 GdkDragContext *context,
105 EmpathyAvatarChooser *self);
106 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
107 GdkDragContext *context,
111 EmpathyAvatarChooser *self);
112 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
113 GdkDragContext *context,
116 GtkSelectionData *selection_data,
119 EmpathyAvatarChooser *self);
120 static void avatar_chooser_clicked_cb (GtkWidget *button,
121 EmpathyAvatarChooser *self);
133 static guint signals [LAST_SIGNAL];
135 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
138 * Drag and drop stuff
140 #define URI_LIST_TYPE "text/uri-list"
143 DND_TARGET_TYPE_URI_LIST
146 static const GtkTargetEntry drop_types[] = {
147 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
151 avatar_chooser_get_property (GObject *object,
156 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
159 case PROP_CONNECTION:
160 g_value_set_object (value, self->priv->connection);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
169 avatar_chooser_set_property (GObject *object,
174 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
177 case PROP_CONNECTION:
178 avatar_chooser_set_connection (self, g_value_get_object (value));
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
187 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
189 GObjectClass *object_class = G_OBJECT_CLASS (klass);
190 GParamSpec *param_spec;
192 object_class->finalize = avatar_chooser_finalize;
193 object_class->get_property = avatar_chooser_get_property;
194 object_class->set_property = avatar_chooser_set_property;
197 * EmpathyAvatarChooser::changed:
198 * @self: an #EmpathyAvatarChooser
200 * Emitted when the chosen avatar has changed.
204 g_signal_new ("changed",
205 G_TYPE_FROM_CLASS (klass),
209 g_cclosure_marshal_VOID__VOID,
213 * EmpathyAvatarChooser:connection:
215 * The #TpConnection whose avatar should be shown and modified by
216 * the #EmpathyAvatarChooser instance.
218 param_spec = g_param_spec_object ("connection",
220 "TpConnection whose avatar should be "
221 "shown and modified by this widget",
224 G_PARAM_STATIC_STRINGS);
225 g_object_class_install_property (object_class,
229 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPrivate));
233 empathy_avatar_chooser_init (EmpathyAvatarChooser *self)
235 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
236 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPrivate);
238 gtk_drag_dest_set (GTK_WIDGET (self),
239 GTK_DEST_DEFAULT_ALL,
241 G_N_ELEMENTS (drop_types),
244 self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
246 g_signal_connect (self, "drag-motion",
247 G_CALLBACK (avatar_chooser_drag_motion_cb),
249 g_signal_connect (self, "drag-leave",
250 G_CALLBACK (avatar_chooser_drag_leave_cb),
252 g_signal_connect (self, "drag-drop",
253 G_CALLBACK (avatar_chooser_drag_drop_cb),
255 g_signal_connect (self, "drag-data-received",
256 G_CALLBACK (avatar_chooser_drag_data_received_cb),
258 g_signal_connect (self, "clicked",
259 G_CALLBACK (avatar_chooser_clicked_cb),
262 empathy_avatar_chooser_set (self, NULL);
266 avatar_chooser_finalize (GObject *object)
268 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
270 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
271 g_assert (self->priv->connection == NULL);
273 if (self->priv->avatar != NULL) {
274 empathy_avatar_unref (self->priv->avatar);
277 g_object_unref (self->priv->gsettings_ui);
279 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
283 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
284 TpConnection *connection)
286 if (self->priv->connection != NULL) {
287 g_object_unref (self->priv->connection);
288 self->priv->connection = NULL;
291 if (connection != NULL) {
292 GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
293 self->priv->connection = g_object_ref (connection);
294 tp_proxy_prepare_async (self->priv->connection, features, NULL, NULL);
299 avatar_chooser_error_show (EmpathyAvatarChooser *self,
300 const gchar *primary_text,
301 const gchar *secondary_text)
306 parent = gtk_widget_get_toplevel (GTK_WIDGET (self));
307 if (!GTK_IS_WINDOW (parent)) {
311 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
317 if (secondary_text != NULL) {
318 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
319 "%s", secondary_text);
322 g_signal_connect (dialog, "response",
323 G_CALLBACK (gtk_widget_destroy), NULL);
324 gtk_widget_show (dialog);
329 str_in_strv (const gchar *str,
336 while (*strv != NULL) {
337 if (g_str_equal (str, *strv)) {
345 /* The caller must free the strings stored in satisfactory_format_name and
346 * satisfactory_mime_type.
349 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
350 gchar **accepted_mime_types,
351 gchar **satisfactory_format_name,
352 gchar **satisfactory_mime_type)
354 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
357 gboolean found = FALSE;
359 *satisfactory_format_name = NULL;
360 *satisfactory_mime_type = NULL;
362 /* If there is no accepted format there is nothing we can do */
363 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
367 /* If the current mime type is good and accepted, don't change it!
368 * jpeg is compress better pictures, but png is better for logos and
369 * could have an alpha layer. */
370 if (str_in_strv (current_mime_type, good_mime_types) &&
371 str_in_strv (current_mime_type, accepted_mime_types)) {
372 *satisfactory_mime_type = g_strdup (current_mime_type);
373 *satisfactory_format_name = g_strdup (current_mime_type +
378 /* The current mime type is either not accepted or not good to use.
379 * Check if one of the good format is supported... */
380 for (i = 0; good_mime_types[i] != NULL; i++) {
381 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
382 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
383 *satisfactory_format_name = g_strdup (good_mime_types[i] +
389 /* Pick the first supported format we can write */
390 formats = gdk_pixbuf_get_formats ();
391 for (l = formats; !found && l != NULL; l = l->next) {
392 GdkPixbufFormat *format = l->data;
393 gchar **format_mime_types;
396 if (!gdk_pixbuf_format_is_writable (format)) {
400 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
401 for (iter = format_mime_types; *iter != NULL; iter++) {
402 if (str_in_strv (*iter, accepted_mime_types)) {
403 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
404 *satisfactory_mime_type = g_strdup (*iter);
409 g_strfreev (format_mime_types);
411 g_slist_free (formats);
416 static EmpathyAvatar *
417 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
419 EmpathyAvatar *avatar)
421 TpAvatarRequirements *req;
422 gboolean needs_conversion = FALSE;
424 gchar *new_format_name = NULL;
425 gchar *new_mime_type = NULL;
426 gdouble min_factor, max_factor;
428 gchar *best_image_data = NULL;
429 gsize best_image_size = 0;
432 req = tp_connection_get_avatar_requirements (self->priv->connection);
434 DEBUG ("Avatar requirements not ready");
438 /* Smaller is the factor, smaller will be the image.
439 * 0 is an empty image, 1 is the full size. */
444 /* Check if we need to convert to another image format */
445 if (avatar_chooser_need_mime_type_conversion (avatar->format,
446 req->supported_mime_types,
449 DEBUG ("Format conversion needed, we'll use mime type '%s' "
450 "and format name '%s'. Current mime type is '%s'",
451 new_mime_type, new_format_name, avatar->format);
452 needs_conversion = TRUE;
455 /* If there is no format we can use, report error to the user. */
456 if (new_mime_type == NULL || new_format_name == NULL) {
457 avatar_chooser_error_show (self, _("Couldn't convert image"),
458 _("None of the accepted image formats are "
459 "supported on your system"));
463 /* If width or height are too big, it needs converting. */
464 width = gdk_pixbuf_get_width (pixbuf);
465 height = gdk_pixbuf_get_height (pixbuf);
466 if ((req->maximum_width > 0 && width > req->maximum_width) ||
467 (req->maximum_height > 0 && height > req->maximum_height)) {
468 gdouble h_factor, v_factor;
470 h_factor = (gdouble) req->maximum_width / width;
471 v_factor = (gdouble) req->maximum_height / height;
472 factor = max_factor = MIN (h_factor, v_factor);
474 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
475 width, height, req->maximum_width, req->maximum_height);
477 needs_conversion = TRUE;
480 /* If the data len is too big and no other conversion is needed,
481 * try with a lower factor. */
482 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes && !needs_conversion) {
483 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
484 "(max is %u bytes), conversion needed.",
485 avatar->len, req->maximum_bytes);
488 needs_conversion = TRUE;
491 /* If no conversion is needed, return the avatar */
492 if (!needs_conversion) {
493 g_free (new_format_name);
494 g_free (new_mime_type);
495 return empathy_avatar_ref (avatar);
499 GdkPixbuf *pixbuf_scaled = NULL;
501 gint new_width, new_height;
502 gchar *converted_image_data;
503 gsize converted_image_size;
504 GError *error = NULL;
507 new_width = width * factor;
508 new_height = height * factor;
509 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
516 pixbuf_scaled = g_object_ref (pixbuf);
519 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
520 new_width, new_height, new_format_name);
522 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
523 &converted_image_data,
524 &converted_image_size,
527 g_object_unref (pixbuf_scaled);
530 g_free (new_format_name);
531 g_free (new_mime_type);
532 avatar_chooser_error_show (self,
533 _("Couldn't convert image"),
534 error ? error->message : NULL);
535 g_clear_error (&error);
539 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
540 converted_image_size);
542 /* If the new image satisfy the req, keep it as current best */
543 if (req->maximum_bytes == 0 ||
544 converted_image_size <= req->maximum_bytes) {
546 g_free (best_image_data);
548 best_image_data = converted_image_data;
549 best_image_size = converted_image_size;
551 /* If this image is close enough to the optimal size,
553 if (req->maximum_bytes == 0 ||
554 req->maximum_bytes - converted_image_size <= 1024)
557 g_free (converted_image_data);
560 /* Make a binary search for the bigest factor that produce
561 * an image data size less than max_size */
562 if (converted_image_size > req->maximum_bytes)
564 if (converted_image_size < req->maximum_bytes)
566 factor = (min_factor + max_factor)/2;
568 if ((int) (width * factor) == new_width ||
569 (int) (height * factor) == new_height) {
570 /* min_factor and max_factor are too close, so the new
571 * factor will produce the same image as previous
572 * iteration. No need to continue, we already found
573 * the optimal size. */
577 /* Do 10 iterations in the worst case */
578 } while (++count < 10);
580 g_free (new_format_name);
582 /* Takes ownership of new_mime_type and best_image_data */
583 avatar = empathy_avatar_new ((guchar *) best_image_data,
584 best_image_size, new_mime_type, NULL);
590 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
594 if (self->priv->avatar != NULL) {
595 empathy_avatar_unref (self->priv->avatar);
596 self->priv->avatar = NULL;
599 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
600 GTK_ICON_SIZE_DIALOG);
601 gtk_button_set_image (GTK_BUTTON (self), image);
602 g_signal_emit (self, signals[CHANGED], 0);
606 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
609 gboolean set_locally)
612 EmpathyAvatar *avatar = NULL;
613 gchar *mime_type = NULL;
616 avatar_chooser_clear_image (self);
620 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
621 if (pixbuf == NULL) {
627 /* avatar takes ownership of data and mime_type */
628 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
630 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
634 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *self,
635 EmpathyAvatar *avatar,
636 gboolean set_locally)
639 gchar *mime_type = NULL;
641 g_assert (avatar != NULL);
643 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
646 if (pixbuf == NULL) {
647 DEBUG ("couldn't make a pixbuf from avatar; giving up");
651 if (avatar->format == NULL) {
652 avatar->format = mime_type;
654 if (strcmp (mime_type, avatar->format)) {
655 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
656 avatar->format, mime_type);
661 empathy_avatar_ref (avatar);
663 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
667 avatar_chooser_set_image (EmpathyAvatarChooser *self,
668 EmpathyAvatar *avatar,
670 gboolean set_locally)
672 GdkPixbuf *pixbuf_view;
675 g_assert (avatar != NULL);
676 g_assert (pixbuf != NULL);
681 conv = avatar_chooser_maybe_convert_and_scale (self,
683 empathy_avatar_unref (avatar);
686 /* An error occured; don't change the avatar. */
693 if (self->priv->avatar != NULL) {
694 empathy_avatar_unref (self->priv->avatar);
696 self->priv->avatar = avatar;
698 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
699 image = gtk_image_new_from_pixbuf (pixbuf_view);
701 gtk_button_set_image (GTK_BUTTON (self), image);
702 g_signal_emit (self, signals[CHANGED], 0);
704 g_object_unref (pixbuf_view);
705 g_object_unref (pixbuf);
709 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
710 const gchar *filename)
712 gchar *image_data = NULL;
713 gsize image_size = 0;
714 GError *error = NULL;
716 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
717 DEBUG ("Failed to load image from '%s': %s", filename,
718 error ? error->message : "No error given");
720 g_clear_error (&error);
724 avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
729 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
732 /* dup the string as empathy_avatar_new steals ownership of the it */
733 gchar *mime = g_strdup ("image/png");
736 EmpathyAvatar *avatar = NULL;
737 GError *error = NULL;
738 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL)) {
739 avatar_chooser_error_show (self,
740 _("Couldn't save pixbuf to png"),
741 error ? error->message : NULL);
742 g_clear_error (&error);
745 avatar = empathy_avatar_new ((guchar *) buf, size, mime, NULL);
746 avatar_chooser_set_image (self, avatar, pb, TRUE);
751 avatar_chooser_drag_motion_cb (GtkWidget *widget,
752 GdkDragContext *context,
756 EmpathyAvatarChooser *self)
760 for (p = gdk_drag_context_list_targets (context); p != NULL;
762 gchar *possible_type;
764 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
766 if (!strcmp (possible_type, URI_LIST_TYPE)) {
767 g_free (possible_type);
768 gdk_drag_status (context, GDK_ACTION_COPY, time_);
773 g_free (possible_type);
780 avatar_chooser_drag_leave_cb (GtkWidget *widget,
781 GdkDragContext *context,
783 EmpathyAvatarChooser *self)
788 avatar_chooser_drag_drop_cb (GtkWidget *widget,
789 GdkDragContext *context,
793 EmpathyAvatarChooser *self)
797 if (gdk_drag_context_list_targets (context) == NULL) {
801 for (p = gdk_drag_context_list_targets (context);
802 p != NULL; p = p->next) {
805 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
806 if (!strcmp (possible_type, URI_LIST_TYPE)) {
807 g_free (possible_type);
808 gtk_drag_get_data (widget, context,
809 GDK_POINTER_TO_ATOM (p->data),
815 g_free (possible_type);
822 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
823 GdkDragContext *context,
826 GtkSelectionData *selection_data,
829 EmpathyAvatarChooser *self)
832 gboolean handled = FALSE;
834 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
835 if (!strcmp (target_type, URI_LIST_TYPE)) {
841 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
846 uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
847 nl - (gchar *) gtk_selection_data_get_data (selection_data));
849 file = g_file_new_for_uri (uri);
852 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
856 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
860 /* this in turn calls empathy_avatar_new (), which assumes
863 avatar_chooser_set_image_from_data (self, data,
868 g_object_unref (file);
871 gtk_drag_finish (context, handled, FALSE, time_);
875 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
876 EmpathyAvatarChooser *self)
880 filename = gtk_file_chooser_get_preview_filename (file_chooser);
884 GdkPixbuf *pixbuf = NULL;
885 GdkPixbuf *scaled_pixbuf;
887 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
889 image = gtk_file_chooser_get_preview_widget (file_chooser);
892 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
893 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
894 g_object_unref (scaled_pixbuf);
895 g_object_unref (pixbuf);
897 gtk_image_set_from_stock (GTK_IMAGE (image),
898 "gtk-dialog-question",
899 GTK_ICON_SIZE_DIALOG);
905 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
910 destroy_chooser (GtkWidget *self)
912 gtk_widget_destroy (self);
917 webcam_response_cb (GtkDialog *dialog,
919 EmpathyAvatarChooser *self)
921 if (response == GTK_RESPONSE_ACCEPT) {
923 CheeseAvatarChooser *cheese_chooser;
925 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
926 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
927 avatar_chooser_set_avatar_from_pixbuf (self, pb);
929 if (response != GTK_RESPONSE_DELETE_EVENT &&
930 response != GTK_RESPONSE_NONE)
931 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
935 choose_avatar_from_webcam (GtkWidget *widget,
936 EmpathyAvatarChooser *self)
940 window = cheese_avatar_chooser_new ();
942 gtk_window_set_transient_for (GTK_WINDOW (window),
943 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
944 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
945 g_signal_connect (G_OBJECT (window), "response",
946 G_CALLBACK (webcam_response_cb), self);
947 gtk_widget_show (window);
949 #endif /* HAVE_CHEESE */
952 avatar_chooser_response_cb (GtkWidget *widget,
954 EmpathyAvatarChooser *self)
956 self->priv->chooser_dialog = NULL;
958 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE) {
962 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
963 avatar_chooser_set_image_from_file (self, filename);
966 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
968 g_settings_set_string (self->priv->gsettings_ui,
969 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
975 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE) {
976 /* This corresponds to "No Image", not to "Cancel" */
977 avatar_chooser_clear_image (self);
980 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM) {
981 /* This corresponds to "Camera Picture" */
982 choose_avatar_from_webcam (widget, self);
985 gtk_widget_destroy (widget);
989 avatar_chooser_clicked_cb (GtkWidget *button,
990 EmpathyAvatarChooser *self)
992 GtkFileChooser *chooser_dialog;
994 gchar *saved_dir = NULL;
995 const gchar *default_dir = DEFAULT_DIR;
996 const gchar *pics_dir;
997 GtkFileFilter *filter;
999 if (self->priv->chooser_dialog) {
1000 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
1004 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
1005 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
1006 empathy_get_toplevel_window (GTK_WIDGET (self)),
1007 GTK_FILE_CHOOSER_ACTION_OPEN,
1009 _("Take a picture..."),
1010 EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM,
1013 EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
1015 EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
1017 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
1019 chooser_dialog = self->priv->chooser_dialog;
1020 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
1022 /* Get special dirs */
1023 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
1024 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
1026 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
1030 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
1033 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
1034 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
1038 /* Set current dir to the last one or to DEFAULT_DIR or to home */
1040 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
1042 else if (pics_dir) {
1043 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
1045 else if (default_dir) {
1046 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
1048 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
1051 /* Add shortcuts to special dirs */
1053 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
1055 else if (pics_dir) {
1056 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
1059 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
1062 /* Setup preview image */
1063 image = gtk_image_new ();
1064 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1065 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1066 gtk_widget_show (image);
1067 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1068 g_signal_connect (chooser_dialog, "update-preview",
1069 G_CALLBACK (avatar_chooser_update_preview_cb),
1073 filter = gtk_file_filter_new ();
1074 gtk_file_filter_set_name (filter, _("Images"));
1075 gtk_file_filter_add_pixbuf_formats (filter);
1076 gtk_file_chooser_add_filter (chooser_dialog, filter);
1077 filter = gtk_file_filter_new ();
1078 gtk_file_filter_set_name (filter, _("All Files"));
1079 gtk_file_filter_add_pattern (filter, "*");
1080 gtk_file_chooser_add_filter (chooser_dialog, filter);
1082 /* Setup response */
1083 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1084 g_signal_connect (chooser_dialog, "response",
1085 G_CALLBACK (avatar_chooser_response_cb),
1088 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1094 * empathy_avatar_chooser_new:
1096 * Creates a new #EmpathyAvatarChooser.
1098 * Return value: a new #EmpathyAvatarChooser
1101 empathy_avatar_chooser_new (void)
1103 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1107 * empathy_avatar_chooser_set:
1108 * @self: an #EmpathyAvatarChooser
1109 * @avatar: a new #EmpathyAvatar
1111 * Sets the @self to display the avatar indicated by @avatar.
1114 empathy_avatar_chooser_set (EmpathyAvatarChooser *self,
1115 EmpathyAvatar *avatar)
1117 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1119 if (avatar != NULL) {
1120 avatar_chooser_set_image_from_avatar (self, avatar, FALSE);
1122 avatar_chooser_clear_image (self);
1127 * empathy_avatar_chooser_get_image_data:
1128 * @self: an #EmpathyAvatarChooser
1129 * @data: avatar bytes
1130 * @data_size: size of @data
1131 * @mime_type: avatar mime-type
1133 * Gets image data about the currently selected avatar.
1136 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *self,
1139 const gchar **mime_type)
1141 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1143 if (self->priv->avatar != NULL) {
1145 *data = (gchar *) self->priv->avatar->data;
1147 if (data_size != NULL) {
1148 *data_size = self->priv->avatar->len;
1150 if (mime_type != NULL) {
1151 *mime_type = self->priv->avatar->format;
1157 if (data_size != NULL) {
1160 if (mime_type != NULL) {
1167 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1170 g_return_if_fail (account != NULL);
1172 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1173 g_object_notify (G_OBJECT (self), "connection");