2 * Copyright (C) 2006-2007 Imendio AB.
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
19 * Authors: Based on Novell's e-image-chooser.
20 * Xavier Claessens <xclaesse@gmail.com>
27 #include <glib/gi18n-lib.h>
31 #include <libempathy/empathy-camera-monitor.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
87 GtkFileChooser *chooser_dialog;
88 GSettings *gsettings_ui;
97 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
100 * Drag and drop stuff
102 #define URI_LIST_TYPE "text/uri-list"
106 DND_TARGET_TYPE_URI_LIST
109 static const GtkTargetEntry drop_types[] =
111 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
114 static void avatar_chooser_set_image (EmpathyAvatarChooser *self,
118 gboolean maybe_convert);
119 static void avatar_chooser_clear_image (EmpathyAvatarChooser *self);
122 get_avatar_cb (GObject *source,
123 GAsyncResult *result,
126 TpWeakRef *wr = user_data;
127 EmpathyAvatarChooser *self = tp_weak_ref_dup_object (wr);
128 const GArray *avatar;
131 GError *error = NULL;
135 tp_weak_ref_destroy (wr);
139 avatar = tp_account_get_avatar_finish (self->priv->account, result, &error);
142 DEBUG ("Error getting account's avatar: %s", error->message);
143 g_clear_error (&error);
147 if (avatar->len == 0)
149 avatar_chooser_clear_image (self);
153 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
154 avatar->len, &mime_type);
157 DEBUG ("couldn't make a pixbuf from avatar; giving up");
161 avatar_chooser_set_image (self, (GArray *) avatar, mime_type, pixbuf, FALSE);
164 self->priv->changed = FALSE;
167 tp_weak_ref_destroy (wr);
168 g_object_unref (self);
172 avatar_changed_cb (TpAccount *account,
174 GObject *weak_object)
176 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) weak_object;
178 tp_account_get_avatar_async (self->priv->account,
179 get_avatar_cb, tp_weak_ref_new (self, NULL, NULL));
183 avatar_chooser_constructed (GObject *object)
185 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
187 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->constructed (object);
189 tp_account_get_avatar_async (self->priv->account,
190 get_avatar_cb, tp_weak_ref_new (self, NULL, NULL));
192 /* FIXME: no signal on TpAccount, yet.
193 * See https://bugs.freedesktop.org/show_bug.cgi?id=52938 */
194 tp_cli_account_interface_avatar_connect_to_avatar_changed (
195 self->priv->account, avatar_changed_cb, NULL, NULL, (GObject *) self,
200 avatar_chooser_get_property (GObject *object,
205 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
210 g_value_set_object (value, self->priv->account);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
219 avatar_chooser_set_property (GObject *object,
224 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
229 g_assert (self->priv->account == NULL); /* construct-only */
230 self->priv->account = g_value_dup_object (value);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
239 avatar_chooser_dispose (GObject *object)
241 EmpathyAvatarChooser *self = (EmpathyAvatarChooser *) object;
243 tp_clear_object (&self->priv->account);
244 tp_clear_pointer (&self->priv->avatar, g_array_unref);
245 tp_clear_pointer (&self->priv->mime_type, g_free);
246 tp_clear_object (&self->priv->gsettings_ui);
248 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->dispose (object);
252 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
254 GObjectClass *object_class = G_OBJECT_CLASS (klass);
255 GParamSpec *param_spec;
257 object_class->constructed = avatar_chooser_constructed;
258 object_class->dispose = avatar_chooser_dispose;
259 object_class->get_property = avatar_chooser_get_property;
260 object_class->set_property = avatar_chooser_set_property;
263 * EmpathyAvatarChooser:account:
265 * The #TpAccount whose avatar should be shown and modified by
266 * the #EmpathyAvatarChooser instance.
268 param_spec = g_param_spec_object ("account",
270 "TpAccount whose avatar should be "
271 "shown and modified by this widget",
274 G_PARAM_CONSTRUCT_ONLY |
275 G_PARAM_STATIC_STRINGS);
276 g_object_class_install_property (object_class,
280 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPrivate));
284 avatar_chooser_drag_motion_cb (GtkWidget *widget,
285 GdkDragContext *context,
289 EmpathyAvatarChooser *self)
293 for (p = gdk_drag_context_list_targets (context); p != NULL;
296 gchar *possible_type;
298 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
300 if (!strcmp (possible_type, URI_LIST_TYPE))
302 g_free (possible_type);
303 gdk_drag_status (context, GDK_ACTION_COPY, time_);
308 g_free (possible_type);
315 avatar_chooser_drag_drop_cb (GtkWidget *widget,
316 GdkDragContext *context,
320 EmpathyAvatarChooser *self)
324 if (gdk_drag_context_list_targets (context) == NULL)
327 for (p = gdk_drag_context_list_targets (context);
328 p != NULL; p = p->next)
332 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
333 if (!strcmp (possible_type, URI_LIST_TYPE))
335 g_free (possible_type);
336 gtk_drag_get_data (widget, context,
337 GDK_POINTER_TO_ATOM (p->data),
343 g_free (possible_type);
350 avatar_chooser_clear_image (EmpathyAvatarChooser *self)
354 tp_clear_pointer (&self->priv->avatar, g_array_unref);
355 tp_clear_pointer (&self->priv->mime_type, g_free);
356 self->priv->changed = TRUE;
358 image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_AVATAR_DEFAULT,
359 GTK_ICON_SIZE_DIALOG);
360 gtk_button_set_image (GTK_BUTTON (self), image);
364 str_in_strv (const gchar *str,
370 while (*strv != NULL)
372 if (g_str_equal (str, *strv))
381 /* The caller must free the strings stored in satisfactory_format_name and
382 * satisfactory_mime_type.
385 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
386 gchar **accepted_mime_types,
387 gchar **satisfactory_format_name,
388 gchar **satisfactory_mime_type)
390 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
393 gboolean found = FALSE;
395 *satisfactory_format_name = NULL;
396 *satisfactory_mime_type = NULL;
398 /* If there is no accepted format there is nothing we can do */
399 if (accepted_mime_types == NULL || *accepted_mime_types == NULL)
402 /* If the current mime type is good and accepted, don't change it!
403 * jpeg is compress better pictures, but png is better for logos and
404 * could have an alpha layer. */
405 if (str_in_strv (current_mime_type, good_mime_types) &&
406 str_in_strv (current_mime_type, accepted_mime_types))
408 *satisfactory_mime_type = g_strdup (current_mime_type);
409 *satisfactory_format_name = g_strdup (current_mime_type +
414 /* The current mime type is either not accepted or not good to use.
415 * Check if one of the good format is supported... */
416 for (i = 0; good_mime_types[i] != NULL; i++)
418 if (str_in_strv (good_mime_types[i], accepted_mime_types))
420 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
421 *satisfactory_format_name = g_strdup (good_mime_types[i] +
427 /* Pick the first supported format we can write */
428 formats = gdk_pixbuf_get_formats ();
429 for (l = formats; !found && l != NULL; l = l->next)
431 GdkPixbufFormat *format = l->data;
432 gchar **format_mime_types;
435 if (!gdk_pixbuf_format_is_writable (format))
438 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
439 for (iter = format_mime_types; *iter != NULL; iter++)
441 if (str_in_strv (*iter, accepted_mime_types))
443 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
444 *satisfactory_mime_type = g_strdup (*iter);
449 g_strfreev (format_mime_types);
451 g_slist_free (formats);
457 avatar_chooser_error_show (EmpathyAvatarChooser *self,
458 const gchar *primary_text,
459 const gchar *secondary_text)
464 parent = gtk_widget_get_toplevel (GTK_WIDGET (self));
465 if (!GTK_IS_WINDOW (parent))
468 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
474 if (secondary_text != NULL)
476 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
477 "%s", secondary_text);
480 g_signal_connect (dialog, "response",
481 G_CALLBACK (gtk_widget_destroy), NULL);
482 gtk_widget_show (dialog);
486 static TpAvatarRequirements *
487 get_requirements (EmpathyAvatarChooser *self)
489 TpConnection *connection;
491 /* FIXME: Should get on TpProtocol if account is offline */
492 connection = tp_account_get_connection (self->priv->account);
493 return tp_connection_get_avatar_requirements (connection);
498 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
503 gchar **ret_mime_type)
505 TpAvatarRequirements *req;
506 gboolean needs_conversion = FALSE;
508 gchar *new_format_name = NULL;
509 gchar *new_mime_type = NULL;
510 gdouble min_factor, max_factor;
512 gchar *best_image_data = NULL;
513 gsize best_image_size = 0;
516 g_assert (ret_avatar != NULL);
517 g_assert (ret_mime_type != NULL);
519 req = get_requirements (self);
522 DEBUG ("Avatar requirements not ready");
526 /* Smaller is the factor, smaller will be the image.
527 * 0 is an empty image, 1 is the full size. */
532 /* Check if we need to convert to another image format */
533 if (avatar_chooser_need_mime_type_conversion (mime_type,
534 req->supported_mime_types, &new_format_name, &new_mime_type))
536 DEBUG ("Format conversion needed, we'll use mime type '%s' "
537 "and format name '%s'. Current mime type is '%s'",
538 new_mime_type, new_format_name, mime_type);
539 needs_conversion = TRUE;
542 /* If there is no format we can use, report error to the user. */
543 if (new_mime_type == NULL || new_format_name == NULL)
545 avatar_chooser_error_show (self, _("Couldn't convert image"),
546 _("None of the accepted image formats are "
547 "supported on your system"));
551 /* If width or height are too big, it needs converting. */
552 width = gdk_pixbuf_get_width (pixbuf);
553 height = gdk_pixbuf_get_height (pixbuf);
554 if ((req->maximum_width > 0 && width > req->maximum_width) ||
555 (req->maximum_height > 0 && height > req->maximum_height))
557 gdouble h_factor, v_factor;
559 h_factor = (gdouble) req->maximum_width / width;
560 v_factor = (gdouble) req->maximum_height / height;
561 factor = max_factor = MIN (h_factor, v_factor);
563 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
564 width, height, req->maximum_width, req->maximum_height);
566 needs_conversion = TRUE;
569 /* If the data len is too big and no other conversion is needed,
570 * try with a lower factor. */
571 if (req->maximum_bytes > 0 && avatar->len > req->maximum_bytes &&
574 DEBUG ("Image data (%u bytes) is too big "
575 "(max is %u bytes), conversion needed.",
576 avatar->len, req->maximum_bytes);
579 needs_conversion = TRUE;
582 /* If no conversion is needed, return the avatar */
583 if (!needs_conversion)
585 *ret_avatar = g_array_ref (avatar);
586 *ret_mime_type = g_strdup (mime_type);
592 GdkPixbuf *pixbuf_scaled = NULL;
594 gint new_width, new_height;
595 gchar *converted_image_data;
596 gsize converted_image_size;
597 GError *error = NULL;
601 new_width = width * factor;
602 new_height = height * factor;
603 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
612 pixbuf_scaled = g_object_ref (pixbuf);
615 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
616 new_width, new_height, new_format_name);
618 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
619 &converted_image_data,
620 &converted_image_size,
623 g_object_unref (pixbuf_scaled);
627 g_free (new_format_name);
628 g_free (new_mime_type);
629 avatar_chooser_error_show (self,
630 _("Couldn't convert image"),
631 error ? error->message : NULL);
632 g_clear_error (&error);
636 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
637 converted_image_size);
639 /* If the new image satisfy the req, keep it as current best */
640 if (req->maximum_bytes == 0 || converted_image_size <= req->maximum_bytes)
642 g_free (best_image_data);
644 best_image_data = converted_image_data;
645 best_image_size = converted_image_size;
647 /* If this image is close enough to the optimal size,
649 if (req->maximum_bytes == 0 ||
650 req->maximum_bytes - converted_image_size <= 1024)
655 g_free (converted_image_data);
658 /* Make a binary search for the bigest factor that produce
659 * an image data size less than max_size */
660 if (converted_image_size > req->maximum_bytes)
662 if (converted_image_size < req->maximum_bytes)
664 factor = (min_factor + max_factor)/2;
666 if ((int) (width * factor) == new_width ||
667 (int) (height * factor) == new_height)
669 /* min_factor and max_factor are too close, so the new
670 * factor will produce the same image as previous
671 * iteration. No need to continue, we already found
672 * the optimal size. */
676 /* Do 10 iterations in the worst case */
677 } while (++count < 10);
679 g_free (new_format_name);
681 /* FIXME: there is no way to create a GArray with zero copy? */
682 *ret_avatar = g_array_sized_new (FALSE, FALSE, sizeof (gchar),
684 g_array_append_vals (*ret_avatar, best_image_data, best_image_size);
685 g_free (best_image_data);
687 *ret_mime_type = new_mime_type;
692 /* Take ownership of @pixbuf */
694 avatar_chooser_set_image (EmpathyAvatarChooser *self,
698 gboolean maybe_convert)
700 GdkPixbuf *pixbuf_view;
703 g_assert (avatar != NULL);
704 g_assert (pixbuf != NULL);
708 GArray *conv_avatar = NULL;
709 gchar *conv_mime_type = NULL;
711 if (!avatar_chooser_maybe_convert_and_scale (self,
712 pixbuf, avatar, mime_type, &conv_avatar, &conv_mime_type))
715 /* Transfer ownership */
716 tp_clear_pointer (&self->priv->avatar, g_array_unref);
717 self->priv->avatar = conv_avatar;
719 g_free (self->priv->mime_type);
720 self->priv->mime_type = conv_mime_type;
724 tp_clear_pointer (&self->priv->avatar, g_array_unref);
725 self->priv->avatar = g_array_ref (avatar);
727 g_free (self->priv->mime_type);
728 self->priv->mime_type = g_strdup (mime_type);
731 self->priv->changed = TRUE;
733 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf,
735 image = gtk_image_new_from_pixbuf (pixbuf_view);
737 gtk_button_set_image (GTK_BUTTON (self), image);
739 g_object_unref (pixbuf_view);
740 g_object_unref (pixbuf);
743 /* takes ownership of @data */
745 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
751 gchar *mime_type = NULL;
755 avatar_chooser_clear_image (self);
759 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
766 /* FIXME: there is no way to create a GArray with zero copy? */
767 avatar = g_array_sized_new (FALSE, FALSE, sizeof (gchar), size);
768 g_array_append_vals (avatar, data, size);
770 avatar_chooser_set_image (self, avatar, mime_type, pixbuf, TRUE);
773 g_array_unref (avatar);
778 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
779 GdkDragContext *context,
782 GtkSelectionData *selection_data,
785 EmpathyAvatarChooser *self)
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))
798 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
805 (gchar *) gtk_selection_data_get_data (selection_data),
806 nl - (gchar *) gtk_selection_data_get_data (selection_data));
808 file = g_file_new_for_uri (uri);
813 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
817 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
822 /* pass data to the avatar_chooser_set_image_from_data */
823 avatar_chooser_set_image_from_data (self, data, bytes_read);
826 g_object_unref (file);
829 gtk_drag_finish (context, handled, FALSE, time_);
833 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
834 EmpathyAvatarChooser *self)
838 filename = gtk_file_chooser_get_preview_filename (file_chooser);
840 if (filename != NULL)
843 GdkPixbuf *pixbuf = NULL;
844 GdkPixbuf *scaled_pixbuf;
846 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
848 image = gtk_file_chooser_get_preview_widget (file_chooser);
852 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf,
855 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
856 g_object_unref (scaled_pixbuf);
857 g_object_unref (pixbuf);
861 gtk_image_set_from_stock (GTK_IMAGE (image),
862 "gtk-dialog-question",
863 GTK_ICON_SIZE_DIALOG);
869 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
873 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
874 const gchar *filename)
876 gchar *image_data = NULL;
877 gsize image_size = 0;
878 GError *error = NULL;
880 if (!g_file_get_contents (filename, &image_data, &image_size, &error))
882 DEBUG ("Failed to load image from '%s': %s", filename,
883 error ? error->message : "No error given");
885 g_clear_error (&error);
889 /* pass image_data to the avatar_chooser_set_image_from_data */
890 avatar_chooser_set_image_from_data (self, image_data, image_size);
895 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
901 GError *error = NULL;
903 if (!gdk_pixbuf_save_to_buffer (pb, &buf, &size, "png", &error, NULL))
905 avatar_chooser_error_show (self,
906 _("Couldn't save picture to file"),
907 error ? error->message : NULL);
908 g_clear_error (&error);
912 /* FIXME: there is no way to create a GArray with zero copy? */
913 avatar = g_array_sized_new (FALSE, FALSE, sizeof (gchar), size);
914 g_array_append_vals (avatar, buf, size);
916 avatar_chooser_set_image (self, avatar, "image/png", pb, TRUE);
919 g_array_unref (avatar);
923 destroy_chooser (GtkWidget *self)
925 gtk_widget_destroy (self);
930 webcam_response_cb (GtkDialog *dialog,
932 EmpathyAvatarChooser *self)
934 if (response == GTK_RESPONSE_ACCEPT)
937 CheeseAvatarChooser *cheese_chooser;
939 cheese_chooser = CHEESE_AVATAR_CHOOSER (dialog);
940 pb = cheese_avatar_chooser_get_picture (cheese_chooser);
941 avatar_chooser_set_avatar_from_pixbuf (self, pb);
944 if (response != GTK_RESPONSE_DELETE_EVENT &&
945 response != GTK_RESPONSE_NONE)
946 g_idle_add ((GSourceFunc) destroy_chooser, dialog);
950 choose_avatar_from_webcam (GtkWidget *widget,
951 EmpathyAvatarChooser *self)
955 window = cheese_avatar_chooser_new ();
957 gtk_window_set_transient_for (GTK_WINDOW (window),
958 GTK_WINDOW (empathy_get_toplevel_window (GTK_WIDGET (self))));
959 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
960 g_signal_connect (G_OBJECT (window), "response",
961 G_CALLBACK (webcam_response_cb), self);
962 gtk_widget_show (window);
964 #endif /* HAVE_CHEESE */
967 avatar_chooser_response_cb (GtkWidget *widget,
969 EmpathyAvatarChooser *self)
971 self->priv->chooser_dialog = NULL;
973 if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE)
978 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
979 avatar_chooser_set_image_from_file (self, filename);
982 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
985 g_settings_set_string (self->priv->gsettings_ui,
986 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
992 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE)
994 /* This corresponds to "No Image", not to "Cancel" */
995 avatar_chooser_clear_image (self);
998 else if (response == EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM)
1000 /* This corresponds to "Camera Picture" */
1001 choose_avatar_from_webcam (widget, self);
1005 gtk_widget_destroy (widget);
1009 avatar_chooser_clicked_cb (GtkWidget *button,
1010 EmpathyAvatarChooser *self)
1012 GtkFileChooser *chooser_dialog;
1014 gchar *saved_dir = NULL;
1015 const gchar *default_dir = DEFAULT_DIR;
1016 const gchar *pics_dir;
1017 GtkFileFilter *filter;
1019 GtkWidget *picture_button;
1020 EmpathyCameraMonitor *monitor;
1023 if (self->priv->chooser_dialog != NULL)
1025 gtk_window_present (GTK_WINDOW (self->priv->chooser_dialog));
1029 self->priv->chooser_dialog = GTK_FILE_CHOOSER (
1030 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
1031 empathy_get_toplevel_window (GTK_WIDGET (self)),
1032 GTK_FILE_CHOOSER_ACTION_OPEN,
1036 picture_button = gtk_dialog_add_button (
1037 GTK_DIALOG (self->priv->chooser_dialog),
1038 _("Take a picture..."), EMPATHY_AVATAR_CHOOSER_RESPONSE_WEBCAM);
1040 /* Button is sensitive only if there is one camera connected */
1041 monitor = empathy_camera_monitor_dup_singleton ();
1043 g_object_set_data_full (G_OBJECT (picture_button),
1044 "monitor", monitor, g_object_unref);
1046 g_object_bind_property (monitor, "available", picture_button, "sensitive",
1047 G_BINDING_SYNC_CREATE);
1050 gtk_dialog_add_buttons (GTK_DIALOG (self->priv->chooser_dialog),
1051 _("No Image"), EMPATHY_AVATAR_CHOOSER_RESPONSE_NO_IMAGE,
1052 GTK_STOCK_CANCEL, EMPATHY_AVATAR_CHOOSER_RESPONSE_CANCEL,
1053 GTK_STOCK_OPEN, EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE,
1056 chooser_dialog = self->priv->chooser_dialog;
1057 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
1059 /* Get special dirs */
1060 saved_dir = g_settings_get_string (self->priv->gsettings_ui,
1061 EMPATHY_PREFS_UI_AVATAR_DIRECTORY);
1063 if (saved_dir != NULL &&
1064 !g_file_test (saved_dir, G_FILE_TEST_IS_DIR))
1070 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR))
1073 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
1074 if (pics_dir != NULL && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR))
1077 /* Set current dir to the last one or to DEFAULT_DIR or to home */
1078 if (saved_dir != NULL)
1079 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
1080 else if (pics_dir != NULL)
1081 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
1082 else if (default_dir != NULL)
1083 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
1085 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
1087 /* Add shortcuts to special dirs */
1089 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
1091 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
1093 if (default_dir != NULL)
1094 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
1096 /* Setup preview image */
1097 image = gtk_image_new ();
1098 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
1099 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
1100 gtk_widget_show (image);
1101 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
1102 g_signal_connect (chooser_dialog, "update-preview",
1103 G_CALLBACK (avatar_chooser_update_preview_cb),
1107 filter = gtk_file_filter_new ();
1108 gtk_file_filter_set_name (filter, _("Images"));
1109 gtk_file_filter_add_pixbuf_formats (filter);
1110 gtk_file_chooser_add_filter (chooser_dialog, filter);
1111 filter = gtk_file_filter_new ();
1112 gtk_file_filter_set_name (filter, _("All Files"));
1113 gtk_file_filter_add_pattern (filter, "*");
1114 gtk_file_chooser_add_filter (chooser_dialog, filter);
1116 /* Setup response */
1117 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog),
1118 EMPATHY_AVATAR_CHOOSER_RESPONSE_FILE);
1120 g_signal_connect (chooser_dialog, "response",
1121 G_CALLBACK (avatar_chooser_response_cb),
1124 gtk_widget_show (GTK_WIDGET (chooser_dialog));
1130 empathy_avatar_chooser_init (EmpathyAvatarChooser *self)
1132 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1133 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPrivate);
1135 gtk_drag_dest_set (GTK_WIDGET (self),
1136 GTK_DEST_DEFAULT_ALL,
1138 G_N_ELEMENTS (drop_types),
1141 self->priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
1143 g_signal_connect (self, "drag-motion",
1144 G_CALLBACK (avatar_chooser_drag_motion_cb),
1146 g_signal_connect (self, "drag-drop",
1147 G_CALLBACK (avatar_chooser_drag_drop_cb),
1149 g_signal_connect (self, "drag-data-received",
1150 G_CALLBACK (avatar_chooser_drag_data_received_cb),
1152 g_signal_connect (self, "clicked",
1153 G_CALLBACK (avatar_chooser_clicked_cb),
1156 avatar_chooser_clear_image (self);
1160 * empathy_avatar_chooser_new:
1161 * @account: a #TpAccount
1163 * Creates a new #EmpathyAvatarChooser.
1165 * Return value: a new #EmpathyAvatarChooser
1168 empathy_avatar_chooser_new (TpAccount *account)
1170 g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
1172 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER,
1178 set_avatar_cb (GObject *source,
1179 GAsyncResult *result,
1182 GSimpleAsyncResult *my_result = user_data;
1183 GError *error = NULL;
1185 if (!tp_account_set_avatar_finish (TP_ACCOUNT (source), result, &error))
1186 g_simple_async_result_take_error (my_result, error);
1188 g_simple_async_result_complete (my_result);
1189 g_object_unref (my_result);
1193 empathy_avatar_chooser_apply_async (EmpathyAvatarChooser *self,
1194 GAsyncReadyCallback callback,
1197 GSimpleAsyncResult *result;
1199 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (self));
1201 result = g_simple_async_result_new ((GObject *) self, callback, user_data,
1202 empathy_avatar_chooser_apply_async);
1204 if (!self->priv->changed)
1206 g_simple_async_result_complete_in_idle (result);
1207 g_object_unref (result);
1211 self->priv->changed = FALSE;
1213 DEBUG ("%s Account.Avatar on %s", self->priv->avatar != NULL ? "Set": "Clear",
1214 tp_proxy_get_object_path (self->priv->account));
1216 tp_account_set_avatar_async (self->priv->account,
1217 self->priv->avatar != NULL ? (guchar *) self->priv->avatar->data : NULL,
1218 self->priv->avatar != NULL ? self->priv->avatar->len : 0,
1219 self->priv->mime_type, set_avatar_cb, result);
1223 empathy_avatar_chooser_apply_finish (EmpathyAvatarChooser *self,
1224 GAsyncResult *result,
1227 empathy_implement_finish_void (self, empathy_avatar_chooser_apply_async);