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
80 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
82 TpConnection *connection;
83 GtkFileChooser *chooser_dialog;
85 gulong ready_handler_id;
87 EmpathyAvatar *avatar;
88 GSettings *gsettings_ui;
89 } EmpathyAvatarChooserPriv;
91 static void avatar_chooser_finalize (GObject *object);
92 static void avatar_chooser_set_connection (EmpathyAvatarChooser *self,
93 TpConnection *connection);
94 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
95 EmpathyAvatar *avatar,
97 gboolean set_locally);
98 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
99 GdkDragContext *context,
103 EmpathyAvatarChooser *chooser);
104 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
105 GdkDragContext *context,
107 EmpathyAvatarChooser *chooser);
108 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
109 GdkDragContext *context,
113 EmpathyAvatarChooser *chooser);
114 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
115 GdkDragContext *context,
118 GtkSelectionData *selection_data,
121 EmpathyAvatarChooser *chooser);
122 static void avatar_chooser_clicked_cb (GtkWidget *button,
123 EmpathyAvatarChooser *chooser);
135 static guint signals [LAST_SIGNAL];
137 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
140 * Drag and drop stuff
142 #define URI_LIST_TYPE "text/uri-list"
145 DND_TARGET_TYPE_URI_LIST
148 static const GtkTargetEntry drop_types[] = {
149 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
153 avatar_chooser_get_property (GObject *object,
158 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
161 case PROP_CONNECTION:
162 g_value_set_object (value, priv->connection);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
171 avatar_chooser_set_property (GObject *object,
176 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
179 case PROP_CONNECTION:
180 avatar_chooser_set_connection (self, g_value_get_object (value));
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
189 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
191 GObjectClass *object_class = G_OBJECT_CLASS (klass);
192 GParamSpec *param_spec;
194 object_class->finalize = avatar_chooser_finalize;
195 object_class->get_property = avatar_chooser_get_property;
196 object_class->set_property = avatar_chooser_set_property;
199 * EmpathyAvatarChooser::changed:
200 * @chooser: an #EmpathyAvatarChooser
202 * Emitted when the chosen avatar has changed.
206 g_signal_new ("changed",
207 G_TYPE_FROM_CLASS (klass),
211 g_cclosure_marshal_VOID__VOID,
215 * EmpathyAvatarChooser:connection:
217 * The #TpConnection whose avatar should be shown and modified by
218 * the #EmpathyAvatarChooser instance.
220 param_spec = g_param_spec_object ("connection",
222 "TpConnection whose avatar should be "
223 "shown and modified by this widget",
226 G_PARAM_STATIC_STRINGS);
227 g_object_class_install_property (object_class,
231 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
235 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
237 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
238 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
240 chooser->priv = priv;
241 gtk_drag_dest_set (GTK_WIDGET (chooser),
242 GTK_DEST_DEFAULT_ALL,
244 G_N_ELEMENTS (drop_types),
247 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
249 g_signal_connect (chooser, "drag-motion",
250 G_CALLBACK (avatar_chooser_drag_motion_cb),
252 g_signal_connect (chooser, "drag-leave",
253 G_CALLBACK (avatar_chooser_drag_leave_cb),
255 g_signal_connect (chooser, "drag-drop",
256 G_CALLBACK (avatar_chooser_drag_drop_cb),
258 g_signal_connect (chooser, "drag-data-received",
259 G_CALLBACK (avatar_chooser_drag_data_received_cb),
261 g_signal_connect (chooser, "clicked",
262 G_CALLBACK (avatar_chooser_clicked_cb),
265 empathy_avatar_chooser_set (chooser, NULL);
269 avatar_chooser_finalize (GObject *object)
271 EmpathyAvatarChooserPriv *priv;
273 priv = GET_PRIV (object);
275 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
276 g_assert (priv->connection == NULL);
278 if (priv->avatar != NULL) {
279 empathy_avatar_unref (priv->avatar);
282 g_object_unref (priv->gsettings_ui);
284 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
288 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
289 TpConnection *connection)
291 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
293 if (priv->connection != NULL) {
294 g_object_unref (priv->connection);
295 priv->connection = NULL;
298 if (connection != NULL) {
299 GQuark features[] = { TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS, 0 };
300 priv->connection = g_object_ref (connection);
301 tp_proxy_prepare_async (priv->connection, features, NULL, NULL);
306 avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
307 const gchar *primary_text,
308 const gchar *secondary_text)
313 parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
314 if (!GTK_IS_WINDOW (parent)) {
318 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
324 if (secondary_text != NULL) {
325 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
326 "%s", secondary_text);
329 g_signal_connect (dialog, "response",
330 G_CALLBACK (gtk_widget_destroy), NULL);
331 gtk_widget_show (dialog);
336 str_in_strv (const gchar *str,
343 while (*strv != NULL) {
344 if (g_str_equal (str, *strv)) {
352 /* The caller must free the strings stored in satisfactory_format_name and
353 * satisfactory_mime_type.
356 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
357 gchar **accepted_mime_types,
358 gchar **satisfactory_format_name,
359 gchar **satisfactory_mime_type)
361 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
364 gboolean found = FALSE;
366 *satisfactory_format_name = NULL;
367 *satisfactory_mime_type = NULL;
369 /* If there is no accepted format there is nothing we can do */
370 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
374 /* If the current mime type is good and accepted, don't change it!
375 * jpeg is compress better pictures, but png is better for logos and
376 * could have an alpha layer. */
377 if (str_in_strv (current_mime_type, good_mime_types) &&
378 str_in_strv (current_mime_type, accepted_mime_types)) {
379 *satisfactory_mime_type = g_strdup (current_mime_type);
380 *satisfactory_format_name = g_strdup (current_mime_type +
385 /* The current mime type is either not accepted or not good to use.
386 * Check if one of the good format is supported... */
387 for (i = 0; good_mime_types[i] != NULL; i++) {
388 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
389 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
390 *satisfactory_format_name = g_strdup (good_mime_types[i] +
396 /* Pick the first supported format we can write */
397 formats = gdk_pixbuf_get_formats ();
398 for (l = formats; !found && l != NULL; l = l->next) {
399 GdkPixbufFormat *format = l->data;
400 gchar **format_mime_types;
403 if (!gdk_pixbuf_format_is_writable (format)) {
407 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
408 for (iter = format_mime_types; *iter != NULL; iter++) {
409 if (str_in_strv (*iter, accepted_mime_types)) {
410 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
411 *satisfactory_mime_type = g_strdup (*iter);
416 g_strfreev (format_mime_types);
418 g_slist_free (formats);
423 static EmpathyAvatar *
424 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
426 EmpathyAvatar *avatar)
428 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
429 TpAvatarRequirements *req;
430 gboolean needs_conversion = FALSE;
432 gchar *new_format_name = NULL;
433 gchar *new_mime_type = NULL;
434 gdouble min_factor, max_factor;
436 gchar *best_image_data = NULL;
437 gsize best_image_size = 0;
440 req = tp_connection_get_avatar_requirements (priv->connection);
442 DEBUG ("Avatar requirements not ready");
446 /* Smaller is the factor, smaller will be the image.
447 * 0 is an empty image, 1 is the full size. */
452 /* Check if we need to convert to another image format */
453 if (avatar_chooser_need_mime_type_conversion (avatar->format,
454 req->supported_mime_types,
457 DEBUG ("Format conversion needed, we'll use mime type '%s' "
458 "and format name '%s'. Current mime type is '%s'",
459 new_mime_type, new_format_name, avatar->format);
460 needs_conversion = TRUE;
463 /* If there is no format we can use, report error to the user. */
464 if (new_mime_type == NULL || new_format_name == NULL) {
465 avatar_chooser_error_show (chooser, _("Couldn't convert image"),
466 _("None of the accepted image formats are "
467 "supported on your system"));
471 /* If width or height are too big, it needs converting. */
472 width = gdk_pixbuf_get_width (pixbuf);
473 height = gdk_pixbuf_get_height (pixbuf);
474 if ((req->maximum_width > 0 && width > req->maximum_width) ||
475 (req->maximum_height > 0 && height > req->maximum_height)) {
476 gdouble h_factor, v_factor;
478 h_factor = (gdouble) req->maximum_width / width;
479 v_factor = (gdouble) req->maximum_height / height;
480 factor = max_factor = MIN (h_factor, v_factor);
482 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
483 width, height, req->maximum_width, req->maximum_height);
485 needs_conversion = TRUE;
488 /* If the data len is too big and no other conversion is needed,
489 * try with a lower factor. */
490 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes && !needs_conversion) {
491 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
492 "(max is %u bytes), conversion needed.",
493 avatar->len, req->maximum_bytes);
496 needs_conversion = TRUE;
499 /* If no conversion is needed, return the avatar */
500 if (!needs_conversion) {
501 g_free (new_format_name);
502 g_free (new_mime_type);
503 return empathy_avatar_ref (avatar);
507 GdkPixbuf *pixbuf_scaled = NULL;
509 gint new_width, new_height;
510 gchar *converted_image_data;
511 gsize converted_image_size;
512 GError *error = NULL;
515 new_width = width * factor;
516 new_height = height * factor;
517 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
524 pixbuf_scaled = g_object_ref (pixbuf);
527 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
528 new_width, new_height, new_format_name);
530 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
531 &converted_image_data,
532 &converted_image_size,
535 g_object_unref (pixbuf_scaled);
538 g_free (new_format_name);
539 g_free (new_mime_type);
540 avatar_chooser_error_show (chooser,
541 _("Couldn't convert image"),
542 error ? error->message : NULL);
543 g_clear_error (&error);
547 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
548 converted_image_size);
550 /* If the new image satisfy the req, keep it as current best */
551 if (req->maximum_bytes == 0 ||
552 converted_image_size <= req->maximum_bytes) {
554 g_free (best_image_data);
556 best_image_data = converted_image_data;
557 best_image_size = converted_image_size;
559 /* If this image is close enough to the optimal size,
561 if (req->maximum_bytes == 0 ||
562 req->maximum_bytes - converted_image_size <= 1024)
565 g_free (converted_image_data);
568 /* Make a binary search for the bigest factor that produce
569 * an image data size less than max_size */
570 if (converted_image_size > req->maximum_bytes)
572 if (converted_image_size < req->maximum_bytes)
574 factor = (min_factor + max_factor)/2;
576 if ((int) (width * factor) == new_width ||
577 (int) (height * factor) == new_height) {
578 /* min_factor and max_factor are too close, so the new
579 * factor will produce the same image as previous
580 * iteration. No need to continue, we already found
581 * the optimal size. */
585 /* Do 10 iterations in the worst case */
586 } while (++count < 10);
588 g_free (new_format_name);
590 /* Takes ownership of new_mime_type and best_image_data */
591 avatar = empathy_avatar_new ((guchar *) best_image_data,
592 best_image_size, new_mime_type, NULL);
598 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
600 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
603 if (priv->avatar != NULL) {
604 empathy_avatar_unref (priv->avatar);
608 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
609 GTK_ICON_SIZE_DIALOG);
610 gtk_button_set_image (GTK_BUTTON (chooser), image);
611 g_signal_emit (chooser, signals[CHANGED], 0);
615 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
618 gboolean set_locally)
621 EmpathyAvatar *avatar = NULL;
622 gchar *mime_type = NULL;
625 avatar_chooser_clear_image (chooser);
629 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
630 if (pixbuf == NULL) {
636 /* avatar takes ownership of data and mime_type */
637 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
639 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
643 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
644 EmpathyAvatar *avatar,
645 gboolean set_locally)
648 gchar *mime_type = NULL;
650 g_assert (avatar != NULL);
652 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
655 if (pixbuf == NULL) {
656 DEBUG ("couldn't make a pixbuf from avatar; giving up");
660 if (avatar->format == NULL) {
661 avatar->format = mime_type;
663 if (strcmp (mime_type, avatar->format)) {
664 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
665 avatar->format, mime_type);
670 empathy_avatar_ref (avatar);
672 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
676 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
677 EmpathyAvatar *avatar,
679 gboolean set_locally)
681 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
682 GdkPixbuf *pixbuf_view;
685 g_assert (avatar != NULL);
686 g_assert (pixbuf != NULL);
691 conv = avatar_chooser_maybe_convert_and_scale (chooser,
693 empathy_avatar_unref (avatar);
696 /* An error occured; don't change the avatar. */
703 if (priv->avatar != NULL) {
704 empathy_avatar_unref (priv->avatar);
706 priv->avatar = avatar;
708 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
709 image = gtk_image_new_from_pixbuf (pixbuf_view);
711 gtk_button_set_image (GTK_BUTTON (chooser), image);
712 g_signal_emit (chooser, signals[CHANGED], 0);
714 g_object_unref (pixbuf_view);
715 g_object_unref (pixbuf);
719 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
720 const gchar *filename)
722 gchar *image_data = NULL;
723 gsize image_size = 0;
724 GError *error = NULL;
726 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
727 DEBUG ("Failed to load image from '%s': %s", filename,
728 error ? error->message : "No error given");
730 g_clear_error (&error);
734 avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
739 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *chooser,
742 /* dup the string as empathy_avatar_new steals ownership of the it */
743 gchar *mime = g_strdup ("image/png");
746 EmpathyAvatar *avatar = NULL;
747 GError *error = NULL;
748 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL)) {
749 avatar_chooser_error_show (chooser,
750 _("Couldn't save pixbuf to png"),
751 error ? error->message : NULL);
752 g_clear_error (&error);
755 avatar = empathy_avatar_new ((guchar *) buf, size, mime, NULL);
756 avatar_chooser_set_image (chooser, avatar, pb, TRUE);
761 avatar_chooser_drag_motion_cb (GtkWidget *widget,
762 GdkDragContext *context,
766 EmpathyAvatarChooser *chooser)
770 for (p = gdk_drag_context_list_targets (context); p != NULL;
772 gchar *possible_type;
774 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
776 if (!strcmp (possible_type, URI_LIST_TYPE)) {
777 g_free (possible_type);
778 gdk_drag_status (context, GDK_ACTION_COPY, time_);
783 g_free (possible_type);
790 avatar_chooser_drag_leave_cb (GtkWidget *widget,
791 GdkDragContext *context,
793 EmpathyAvatarChooser *chooser)
798 avatar_chooser_drag_drop_cb (GtkWidget *widget,
799 GdkDragContext *context,
803 EmpathyAvatarChooser *chooser)
807 if (gdk_drag_context_list_targets (context) == NULL) {
811 for (p = gdk_drag_context_list_targets (context);
812 p != NULL; p = p->next) {
815 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
816 if (!strcmp (possible_type, URI_LIST_TYPE)) {
817 g_free (possible_type);
818 gtk_drag_get_data (widget, context,
819 GDK_POINTER_TO_ATOM (p->data),
825 g_free (possible_type);
832 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
833 GdkDragContext *context,
836 GtkSelectionData *selection_data,
839 EmpathyAvatarChooser *chooser)
842 gboolean handled = FALSE;
844 target_type = gdk_atom_name (gtk_selection_data_get_target (selection_data));
845 if (!strcmp (target_type, URI_LIST_TYPE)) {
851 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
856 uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
857 nl - (gchar *) gtk_selection_data_get_data (selection_data));
859 file = g_file_new_for_uri (uri);
862 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
866 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
870 /* this in turn calls empathy_avatar_new (), which assumes
873 avatar_chooser_set_image_from_data (chooser, data,
878 g_object_unref (file);
881 gtk_drag_finish (context, handled, FALSE, time_);
885 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
886 EmpathyAvatarChooser *chooser)
890 filename = gtk_file_chooser_get_preview_filename (file_chooser);
894 GdkPixbuf *pixbuf = NULL;
895 GdkPixbuf *scaled_pixbuf;
897 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
899 image = gtk_file_chooser_get_preview_widget (file_chooser);
902 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
903 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
904 g_object_unref (scaled_pixbuf);
905 g_object_unref (pixbuf);
907 gtk_image_set_from_stock (GTK_IMAGE (image),
908 "gtk-dialog-question",
909 GTK_ICON_SIZE_DIALOG);
915 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
920 destroy_chooser (GtkWidget *chooser)
922 gtk_widget_destroy (chooser);
927 webcam_response_cb (GtkDialog *dialog,
929 EmpathyAvatarChooser *chooser)
931 if (response == GTK_RESPONSE_ACCEPT) {
933 CheeseAvatarChooser *cheese_chooser;
935 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
936 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
937 avatar_chooser_set_avatar_from_pixbuf (chooser, pb);
939 if (response != GTK_RESPONSE_DELETE_EVENT &&
940 response != GTK_RESPONSE_NONE)
941 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
945 choose_avatar_from_webcam (GtkWidget *widget,
946 EmpathyAvatarChooser *chooser)
950 window = cheese_avatar_chooser_new ();
952 gtk_window_set_transient_for (GTK_WINDOW (window),
953 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (chooser))));
954 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
955 g_signal_connect (G_OBJECT (window), "response",
956 G_CALLBACK (webcam_response_cb), chooser);
957 gtk_widget_show (window);
959 #endif /* HAVE_CHEESE */
962 avatar_chooser_response_cb (GtkWidget *widget,
964 EmpathyAvatarChooser *chooser)
966 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
968 priv->chooser_dialog = NULL;
970 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE) {
974 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
975 avatar_chooser_set_image_from_file (chooser, filename);
978 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
980 g_settings_set_string (priv->gsettings_ui,
981 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
987 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE) {
988 /* This corresponds to "No Image", not to "Cancel" */
989 avatar_chooser_clear_image (chooser);
992 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM) {
993 /* This corresponds to "Camera Picture" */
994 choose_avatar_from_webcam (widget, chooser);
997 gtk_widget_destroy (widget);
1001 avatar_chooser_clicked_cb (GtkWidget *button,
1002 EmpathyAvatarChooser *chooser)
1004 GtkFileChooser *chooser_dialog;
1006 gchar *saved_dir = NULL;
1007 const gchar *default_dir = DEFAULT_DIR;
1008 const gchar *pics_dir;
1009 GtkFileFilter *filter;
1010 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
1012 if (priv->chooser_dialog) {
1013 gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
1017 priv->chooser_dialog = GTK_FILE_CHOOSER (
1018 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
1019 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
1020 GTK_FILE_CHOOSER_ACTION_OPEN,
1022 _("Take a picture..."),
1023 EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM,
1026 EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
1028 EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
1030 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
1032 chooser_dialog = priv->chooser_dialog;
1033 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
1035 /* Get special dirs */
1036 saved_dir = g_settings_get_string (priv->gsettings_ui,
1037 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
1039 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
1043 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
1046 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
1047 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
1051 /* Set current dir to the last one or to DEFAULT_DIR or to home */
1053 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
1055 else if (pics_dir) {
1056 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
1058 else if (default_dir) {
1059 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
1061 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
1064 /* Add shortcuts to special dirs */
1066 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
1068 else if (pics_dir) {
1069 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
1072 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
1075 /* Setup preview image */
1076 image = gtk_image_new ();
1077 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1078 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1079 gtk_widget_show (image);
1080 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1081 g_signal_connect (chooser_dialog, "update-preview",
1082 G_CALLBACK (avatar_chooser_update_preview_cb),
1086 filter = gtk_file_filter_new ();
1087 gtk_file_filter_set_name (filter, _("Images"));
1088 gtk_file_filter_add_pixbuf_formats (filter);
1089 gtk_file_chooser_add_filter (chooser_dialog, filter);
1090 filter = gtk_file_filter_new ();
1091 gtk_file_filter_set_name (filter, _("All Files"));
1092 gtk_file_filter_add_pattern (filter, "*");
1093 gtk_file_chooser_add_filter (chooser_dialog, filter);
1095 /* Setup response */
1096 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1097 g_signal_connect (chooser_dialog, "response",
1098 G_CALLBACK (avatar_chooser_response_cb),
1101 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1107 * empathy_avatar_chooser_new:
1109 * Creates a new #EmpathyAvatarChooser.
1111 * Return value: a new #EmpathyAvatarChooser
1114 empathy_avatar_chooser_new (void)
1116 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1120 * empathy_avatar_chooser_set:
1121 * @chooser: an #EmpathyAvatarChooser
1122 * @avatar: a new #EmpathyAvatar
1124 * Sets the @chooser to display the avatar indicated by @avatar.
1127 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
1128 EmpathyAvatar *avatar)
1130 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1132 if (avatar != NULL) {
1133 avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
1135 avatar_chooser_clear_image (chooser);
1140 * empathy_avatar_chooser_get_image_data:
1141 * @chooser: an #EmpathyAvatarChooser
1142 * @data: avatar bytes
1143 * @data_size: size of @data
1144 * @mime_type: avatar mime-type
1146 * Gets image data about the currently selected avatar.
1149 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
1152 const gchar **mime_type)
1154 EmpathyAvatarChooserPriv *priv;
1156 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1158 priv = GET_PRIV (chooser);
1160 if (priv->avatar != NULL) {
1162 *data = (gchar *) priv->avatar->data;
1164 if (data_size != NULL) {
1165 *data_size = priv->avatar->len;
1167 if (mime_type != NULL) {
1168 *mime_type = priv->avatar->format;
1174 if (data_size != NULL) {
1177 if (mime_type != NULL) {
1184 empathy_avatar_chooser_set_account (EmpathyAvatarChooser *self,
1187 g_return_if_fail (account != NULL);
1189 avatar_chooser_set_connection (self, tp_account_get_connection (account));
1190 g_object_notify (G_OBJECT (self), "connection");