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 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
275 tp_clear_object (&self->priv->gsettings_ui);
277 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
281 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
282 TpConnection *connection)
284 tp_clear_object (&self->priv->connection);
286 if (connection != NULL) {
287 GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
288 self->priv->connection = g_object_ref (connection);
289 tp_proxy_prepare_async (self->priv->connection, features, NULL, NULL);
294 avatar_chooser_error_show (EmpathyAvatarChooser *self,
295 const gchar *primary_text,
296 const gchar *secondary_text)
301 parent = gtk_widget_get_toplevel (GTK_WIDGET (self));
302 if (!GTK_IS_WINDOW (parent)) {
306 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
312 if (secondary_text != NULL) {
313 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
314 "%s", secondary_text);
317 g_signal_connect (dialog, "response",
318 G_CALLBACK (gtk_widget_destroy), NULL);
319 gtk_widget_show (dialog);
324 str_in_strv (const gchar *str,
331 while (*strv != NULL) {
332 if (g_str_equal (str, *strv)) {
340 /* The caller must free the strings stored in satisfactory_format_name and
341 * satisfactory_mime_type.
344 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
345 gchar **accepted_mime_types,
346 gchar **satisfactory_format_name,
347 gchar **satisfactory_mime_type)
349 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
352 gboolean found = FALSE;
354 *satisfactory_format_name = NULL;
355 *satisfactory_mime_type = NULL;
357 /* If there is no accepted format there is nothing we can do */
358 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
362 /* If the current mime type is good and accepted, don't change it!
363 * jpeg is compress better pictures, but png is better for logos and
364 * could have an alpha layer. */
365 if (str_in_strv (current_mime_type, good_mime_types) &&
366 str_in_strv (current_mime_type, accepted_mime_types)) {
367 *satisfactory_mime_type = g_strdup (current_mime_type);
368 *satisfactory_format_name = g_strdup (current_mime_type +
373 /* The current mime type is either not accepted or not good to use.
374 * Check if one of the good format is supported... */
375 for (i = 0; good_mime_types[i] != NULL; i++) {
376 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
377 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
378 *satisfactory_format_name = g_strdup (good_mime_types[i] +
384 /* Pick the first supported format we can write */
385 formats = gdk_pixbuf_get_formats ();
386 for (l = formats; !found && l != NULL; l = l->next) {
387 GdkPixbufFormat *format = l->data;
388 gchar **format_mime_types;
391 if (!gdk_pixbuf_format_is_writable (format)) {
395 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
396 for (iter = format_mime_types; *iter != NULL; iter++) {
397 if (str_in_strv (*iter, accepted_mime_types)) {
398 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
399 *satisfactory_mime_type = g_strdup (*iter);
404 g_strfreev (format_mime_types);
406 g_slist_free (formats);
411 static EmpathyAvatar *
412 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
414 EmpathyAvatar *avatar)
416 TpAvatarRequirements *req;
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 *best_image_data = NULL;
424 gsize best_image_size = 0;
427 req = tp_connection_get_avatar_requirements (self->priv->connection);
429 DEBUG ("Avatar requirements not ready");
433 /* Smaller is the factor, smaller will be the image.
434 * 0 is an empty image, 1 is the full size. */
439 /* Check if we need to convert to another image format */
440 if (avatar_chooser_need_mime_type_conversion (avatar->format,
441 req->supported_mime_types,
444 DEBUG ("Format conversion needed, we'll use mime type '%s' "
445 "and format name '%s'. Current mime type is '%s'",
446 new_mime_type, new_format_name, avatar->format);
447 needs_conversion = TRUE;
450 /* If there is no format we can use, report error to the user. */
451 if (new_mime_type == NULL || new_format_name == NULL) {
452 avatar_chooser_error_show (self, _("Couldn't convert image"),
453 _("None of the accepted image formats are "
454 "supported on your system"));
458 /* If width or height are too big, it needs converting. */
459 width = gdk_pixbuf_get_width (pixbuf);
460 height = gdk_pixbuf_get_height (pixbuf);
461 if ((req->maximum_width > 0 && width > req->maximum_width) ||
462 (req->maximum_height > 0 && height > req->maximum_height)) {
463 gdouble h_factor, v_factor;
465 h_factor = (gdouble) req->maximum_width / width;
466 v_factor = (gdouble) req->maximum_height / height;
467 factor = max_factor = MIN (h_factor, v_factor);
469 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
470 width, height, req->maximum_width, req->maximum_height);
472 needs_conversion = TRUE;
475 /* If the data len is too big and no other conversion is needed,
476 * try with a lower factor. */
477 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes && !needs_conversion) {
478 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
479 "(max is %u bytes), conversion needed.",
480 avatar->len, req->maximum_bytes);
483 needs_conversion = TRUE;
486 /* If no conversion is needed, return the avatar */
487 if (!needs_conversion) {
488 g_free (new_format_name);
489 g_free (new_mime_type);
490 return empathy_avatar_ref (avatar);
494 GdkPixbuf *pixbuf_scaled = NULL;
496 gint new_width, new_height;
497 gchar *converted_image_data;
498 gsize converted_image_size;
499 GError *error = NULL;
502 new_width = width * factor;
503 new_height = height * factor;
504 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
511 pixbuf_scaled = g_object_ref (pixbuf);
514 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
515 new_width, new_height, new_format_name);
517 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
518 &converted_image_data,
519 &converted_image_size,
522 g_object_unref (pixbuf_scaled);
525 g_free (new_format_name);
526 g_free (new_mime_type);
527 avatar_chooser_error_show (self,
528 _("Couldn't convert image"),
529 error ? error->message : NULL);
530 g_clear_error (&error);
534 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
535 converted_image_size);
537 /* If the new image satisfy the req, keep it as current best */
538 if (req->maximum_bytes == 0 ||
539 converted_image_size <= req->maximum_bytes) {
541 g_free (best_image_data);
543 best_image_data = converted_image_data;
544 best_image_size = converted_image_size;
546 /* If this image is close enough to the optimal size,
548 if (req->maximum_bytes == 0 ||
549 req->maximum_bytes - converted_image_size <= 1024)
552 g_free (converted_image_data);
555 /* Make a binary search for the bigest factor that produce
556 * an image data size less than max_size */
557 if (converted_image_size > req->maximum_bytes)
559 if (converted_image_size < req->maximum_bytes)
561 factor = (min_factor + max_factor)/2;
563 if ((int) (width * factor) == new_width ||
564 (int) (height * factor) == new_height) {
565 /* min_factor and max_factor are too close, so the new
566 * factor will produce the same image as previous
567 * iteration. No need to continue, we already found
568 * the optimal size. */
572 /* Do 10 iterations in the worst case */
573 } while (++count < 10);
575 g_free (new_format_name);
577 /* Takes ownership of new_mime_type and best_image_data */
578 avatar = empathy_avatar_new ((guchar *) best_image_data,
579 best_image_size, new_mime_type, NULL);
585 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
589 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
591 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
592 GTK_ICON_SIZE_DIALOG);
593 gtk_button_set_image (GTK_BUTTON (self), image);
594 g_signal_emit (self, signals[CHANGED], 0);
598 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
601 gboolean set_locally)
604 EmpathyAvatar *avatar = NULL;
605 gchar *mime_type = NULL;
608 avatar_chooser_clear_image (self);
612 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
613 if (pixbuf == NULL) {
619 /* avatar takes ownership of data and mime_type */
620 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
622 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
626 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *self,
627 EmpathyAvatar *avatar,
628 gboolean set_locally)
631 gchar *mime_type = NULL;
633 g_assert (avatar != NULL);
635 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
638 if (pixbuf == NULL) {
639 DEBUG ("couldn't make a pixbuf from avatar; giving up");
643 if (avatar->format == NULL) {
644 avatar->format = mime_type;
646 if (strcmp (mime_type, avatar->format)) {
647 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
648 avatar->format, mime_type);
653 empathy_avatar_ref (avatar);
655 avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
659 avatar_chooser_set_image (EmpathyAvatarChooser *self,
660 EmpathyAvatar *avatar,
662 gboolean set_locally)
664 GdkPixbuf *pixbuf_view;
667 g_assert (avatar != NULL);
668 g_assert (pixbuf != NULL);
673 conv = avatar_chooser_maybe_convert_and_scale (self,
675 empathy_avatar_unref (avatar);
678 /* An error occured; don't change the avatar. */
685 tp_clear_pointer (&self->priv->avatar, empathy_avatar_unref);
686 self->priv->avatar = avatar;
688 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
689 image = gtk_image_new_from_pixbuf (pixbuf_view);
691 gtk_button_set_image (GTK_BUTTON (self), image);
692 g_signal_emit (self, signals[CHANGED], 0);
694 g_object_unref (pixbuf_view);
695 g_object_unref (pixbuf);
699 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
700 const gchar *filename)
702 gchar *image_data = NULL;
703 gsize image_size = 0;
704 GError *error = NULL;
706 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
707 DEBUG ("Failed to load image from '%s': %s", filename,
708 error ? error->message : "No error given");
710 g_clear_error (&error);
714 avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
719 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
722 /* dup the string as empathy_avatar_new steals ownership of the it */
723 gchar *mime = g_strdup ("image/png");
726 EmpathyAvatar *avatar = NULL;
727 GError *error = NULL;
728 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL)) {
729 avatar_chooser_error_show (self,
730 _("Couldn't save pixbuf to png"),
731 error ? error->message : NULL);
732 g_clear_error (&error);
735 avatar = empathy_avatar_new ((guchar *) buf, size, mime, NULL);
736 avatar_chooser_set_image (self, avatar, pb, TRUE);
741 avatar_chooser_drag_motion_cb (GtkWidget *widget,
742 GdkDragContext *context,
746 EmpathyAvatarChooser *self)
750 for (p = gdk_drag_context_list_targets (context); p != NULL;
752 gchar *possible_type;
754 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 gdk_drag_status (context, GDK_ACTION_COPY, time_);
763 g_free (possible_type);
770 avatar_chooser_drag_leave_cb (GtkWidget *widget,
771 GdkDragContext *context,
773 EmpathyAvatarChooser *self)
778 avatar_chooser_drag_drop_cb (GtkWidget *widget,
779 GdkDragContext *context,
783 EmpathyAvatarChooser *self)
787 if (gdk_drag_context_list_targets (context) == NULL) {
791 for (p = gdk_drag_context_list_targets (context);
792 p != NULL; p = p->next) {
795 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
796 if (!strcmp (possible_type, URI_LIST_TYPE)) {
797 g_free (possible_type);
798 gtk_drag_get_data (widget, context,
799 GDK_POINTER_TO_ATOM (p->data),
805 g_free (possible_type);
812 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
813 GdkDragContext *context,
816 GtkSelectionData *selection_data,
819 EmpathyAvatarChooser *self)
822 gboolean handled = FALSE;
824 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
825 if (!strcmp (target_type, URI_LIST_TYPE)) {
831 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
836 uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
837 nl - (gchar *) gtk_selection_data_get_data (selection_data));
839 file = g_file_new_for_uri (uri);
842 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
846 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
850 /* this in turn calls empathy_avatar_new (), which assumes
853 avatar_chooser_set_image_from_data (self, data,
858 g_object_unref (file);
861 gtk_drag_finish (context, handled, FALSE, time_);
865 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
866 EmpathyAvatarChooser *self)
870 filename = gtk_file_chooser_get_preview_filename (file_chooser);
874 GdkPixbuf *pixbuf = NULL;
875 GdkPixbuf *scaled_pixbuf;
877 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
879 image = gtk_file_chooser_get_preview_widget (file_chooser);
882 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
883 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
884 g_object_unref (scaled_pixbuf);
885 g_object_unref (pixbuf);
887 gtk_image_set_from_stock (GTK_IMAGE (image),
888 "gtk-dialog-question",
889 GTK_ICON_SIZE_DIALOG);
895 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
900 destroy_chooser (GtkWidget *self)
902 gtk_widget_destroy (self);
907 webcam_response_cb (GtkDialog *dialog,
909 EmpathyAvatarChooser *self)
911 if (response == GTK_RESPONSE_ACCEPT) {
913 CheeseAvatarChooser *cheese_chooser;
915 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
916 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
917 avatar_chooser_set_avatar_from_pixbuf (self, pb);
919 if (response != GTK_RESPONSE_DELETE_EVENT &&
920 response != GTK_RESPONSE_NONE)
921 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
925 choose_avatar_from_webcam (GtkWidget *widget,
926 EmpathyAvatarChooser *self)
930 window = cheese_avatar_chooser_new ();
932 gtk_window_set_transient_for (GTK_WINDOW (window),
933 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
934 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
935 g_signal_connect (G_OBJECT (window), "response",
936 G_CALLBACK (webcam_response_cb), self);
937 gtk_widget_show (window);
939 #endif /* HAVE_CHEESE */
942 avatar_chooser_response_cb (GtkWidget *widget,
944 EmpathyAvatarChooser *self)
946 self->priv->chooser_dialog = NULL;
948 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE) {
952 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
953 avatar_chooser_set_image_from_file (self, filename);
956 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
958 g_settings_set_string (self->priv->gsettings_ui,
959 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
965 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE) {
966 /* This corresponds to "No Image", not to "Cancel" */
967 avatar_chooser_clear_image (self);
970 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM) {
971 /* This corresponds to "Camera Picture" */
972 choose_avatar_from_webcam (widget, self);
975 gtk_widget_destroy (widget);
979 avatar_chooser_clicked_cb (GtkWidget *button,
980 EmpathyAvatarChooser *self)
982 GtkFileChooser *chooser_dialog;
984 gchar *saved_dir = NULL;
985 const gchar *default_dir = DEFAULT_DIR;
986 const gchar *pics_dir;
987 GtkFileFilter *filter;
989 if (self->priv->chooser_dialog) {
990 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
994 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
995 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
996 empathy_get_toplevel_window (GTK_WIDGET (self)),
997 GTK_FILE_CHOOSER_ACTION_OPEN,
999 _("Take a picture..."),
1000 EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM,
1003 EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
1005 EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
1007 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
1009 chooser_dialog = self->priv->chooser_dialog;
1010 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
1012 /* Get special dirs */
1013 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
1014 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
1016 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
1020 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
1023 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
1024 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
1028 /* Set current dir to the last one or to DEFAULT_DIR or to home */
1030 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
1032 else if (pics_dir) {
1033 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
1035 else if (default_dir) {
1036 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
1038 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
1041 /* Add shortcuts to special dirs */
1043 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
1045 else if (pics_dir) {
1046 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
1049 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
1052 /* Setup preview image */
1053 image = gtk_image_new ();
1054 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1055 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1056 gtk_widget_show (image);
1057 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1058 g_signal_connect (chooser_dialog, "update-preview",
1059 G_CALLBACK (avatar_chooser_update_preview_cb),
1063 filter = gtk_file_filter_new ();
1064 gtk_file_filter_set_name (filter, _("Images"));
1065 gtk_file_filter_add_pixbuf_formats (filter);
1066 gtk_file_chooser_add_filter (chooser_dialog, filter);
1067 filter = gtk_file_filter_new ();
1068 gtk_file_filter_set_name (filter, _("All Files"));
1069 gtk_file_filter_add_pattern (filter, "*");
1070 gtk_file_chooser_add_filter (chooser_dialog, filter);
1072 /* Setup response */
1073 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1074 g_signal_connect (chooser_dialog, "response",
1075 G_CALLBACK (avatar_chooser_response_cb),
1078 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1084 * empathy_avatar_chooser_new:
1086 * Creates a new #EmpathyAvatarChooser.
1088 * Return value: a new #EmpathyAvatarChooser
1091 empathy_avatar_chooser_new (void)
1093 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1097 * empathy_avatar_chooser_set:
1098 * @self: an #EmpathyAvatarChooser
1099 * @avatar: a new #EmpathyAvatar
1101 * Sets the @self to display the avatar indicated by @avatar.
1104 empathy_avatar_chooser_set (EmpathyAvatarChooser *self,
1105 EmpathyAvatar *avatar)
1107 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1109 if (avatar != NULL) {
1110 avatar_chooser_set_image_from_avatar (self, avatar, FALSE);
1112 avatar_chooser_clear_image (self);
1117 * empathy_avatar_chooser_get_image_data:
1118 * @self: an #EmpathyAvatarChooser
1119 * @data: avatar bytes
1120 * @data_size: size of @data
1121 * @mime_type: avatar mime-type
1123 * Gets image data about the currently selected avatar.
1126 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *self,
1129 const gchar **mime_type)
1131 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1133 if (self->priv->avatar != NULL) {
1135 *data = (gchar *) self->priv->avatar->data;
1137 if (data_size != NULL) {
1138 *data_size = self->priv->avatar->len;
1140 if (mime_type != NULL) {
1141 *mime_type = self->priv->avatar->format;
1147 if (data_size != NULL) {
1150 if (mime_type != NULL) {
1157 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1160 g_return_if_fail (account != NULL);
1162 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1163 g_object_notify (G_OBJECT (self), "connection");