add empathy-auto-salut-account-helper.c to POTFILES.in
[empathy-mirror.git] / libempathy-gtk / empathy-avatar-chooser.c
blob9c79eb852750e547be32dffedddb450f1fec5661
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
24 #include "config.h"
26 #include <string.h>
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30 #include <gio/gio.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
35 #include "empathy-avatar-chooser.h"
36 #include "empathy-conf.h"
37 #include "empathy-ui-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
42 /**
43 * SECTION:empathy-avatar-chooser
44 * @title: EmpathyAvatarChooser
45 * @short_description: A widget used to change avatar
46 * @include: libempathy-gtk/empathy-avatar-chooser.h
48 * #EmpathyAvatarChooser is a widget which extends #GtkButton to
49 * provide a way of changing avatar.
52 /**
53 * EmpathyAvatarChooser:
54 * @parent: parent object
56 * Widget which extends #GtkButton to provide a way of changing avatar.
59 #define AVATAR_SIZE_SAVE 96
60 #define AVATAR_SIZE_VIEW 64
61 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
64 typedef struct {
65 EmpathyTpContactFactory *factory;
66 TpConnection *connection;
67 GtkFileChooser *chooser_dialog;
69 gulong ready_handler_id;
71 EmpathyAvatar *avatar;
72 } EmpathyAvatarChooserPriv;
74 static void avatar_chooser_finalize (GObject *object);
75 static void avatar_chooser_set_connection (EmpathyAvatarChooser *self,
76 TpConnection *connection);
77 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
78 EmpathyAvatar *avatar,
79 GdkPixbuf *pixbuf,
80 gboolean set_locally);
81 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
82 GdkDragContext *context,
83 gint x,
84 gint y,
85 guint time,
86 EmpathyAvatarChooser *chooser);
87 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
88 GdkDragContext *context,
89 guint time,
90 EmpathyAvatarChooser *chooser);
91 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
92 GdkDragContext *context,
93 gint x,
94 gint y,
95 guint time,
96 EmpathyAvatarChooser *chooser);
97 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
98 GdkDragContext *context,
99 gint x,
100 gint y,
101 GtkSelectionData *selection_data,
102 guint info,
103 guint time,
104 EmpathyAvatarChooser *chooser);
105 static void avatar_chooser_clicked_cb (GtkWidget *button,
106 EmpathyAvatarChooser *chooser);
108 enum {
109 CHANGED,
110 LAST_SIGNAL
113 enum {
114 PROP_0,
115 PROP_CONNECTION
118 static guint signals [LAST_SIGNAL];
120 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
123 * Drag and drop stuff
125 #define URI_LIST_TYPE "text/uri-list"
127 enum DndTargetType {
128 DND_TARGET_TYPE_URI_LIST
131 static const GtkTargetEntry drop_types[] = {
132 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
135 static void
136 avatar_chooser_get_property (GObject *object,
137 guint param_id,
138 GValue *value,
139 GParamSpec *pspec)
141 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
143 switch (param_id) {
144 case PROP_CONNECTION:
145 g_value_set_object (value, priv->connection);
146 break;
147 default:
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
149 break;
153 static void
154 avatar_chooser_set_property (GObject *object,
155 guint param_id,
156 const GValue *value,
157 GParamSpec *pspec)
159 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
161 switch (param_id) {
162 case PROP_CONNECTION:
163 avatar_chooser_set_connection (self, g_value_get_object (value));
164 break;
165 default:
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
167 break;
171 static void
172 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
174 GObjectClass *object_class = G_OBJECT_CLASS (klass);
175 GParamSpec *param_spec;
177 object_class->finalize = avatar_chooser_finalize;
178 object_class->get_property = avatar_chooser_get_property;
179 object_class->set_property = avatar_chooser_set_property;
182 * EmpathyAvatarChooser::changed:
183 * @chooser: an #EmpathyAvatarChooser
185 * Emitted when the chosen avatar has changed.
188 signals[CHANGED] =
189 g_signal_new ("changed",
190 G_TYPE_FROM_CLASS (klass),
191 G_SIGNAL_RUN_LAST,
193 NULL, NULL,
194 g_cclosure_marshal_VOID__VOID,
195 G_TYPE_NONE, 0);
198 * EmpathyAvatarChooser:connection:
200 * The #TpConnection whose avatar should be shown and modified by
201 * the #EmpathyAvatarChooser instance.
203 param_spec = g_param_spec_object ("connection",
204 "TpConnection",
205 "TpConnection whose avatar should be "
206 "shown and modified by this widget",
207 TP_TYPE_CONNECTION,
208 G_PARAM_READWRITE |
209 G_PARAM_STATIC_STRINGS);
210 g_object_class_install_property (object_class,
211 PROP_CONNECTION,
212 param_spec);
214 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
217 static void
218 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
220 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
221 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
223 chooser->priv = priv;
224 gtk_drag_dest_set (GTK_WIDGET (chooser),
225 GTK_DEST_DEFAULT_ALL,
226 drop_types,
227 G_N_ELEMENTS (drop_types),
228 GDK_ACTION_COPY);
230 g_signal_connect (chooser, "drag-motion",
231 G_CALLBACK (avatar_chooser_drag_motion_cb),
232 chooser);
233 g_signal_connect (chooser, "drag-leave",
234 G_CALLBACK (avatar_chooser_drag_leave_cb),
235 chooser);
236 g_signal_connect (chooser, "drag-drop",
237 G_CALLBACK (avatar_chooser_drag_drop_cb),
238 chooser);
239 g_signal_connect (chooser, "drag-data-received",
240 G_CALLBACK (avatar_chooser_drag_data_received_cb),
241 chooser);
242 g_signal_connect (chooser, "clicked",
243 G_CALLBACK (avatar_chooser_clicked_cb),
244 chooser);
246 empathy_avatar_chooser_set (chooser, NULL);
249 static void
250 avatar_chooser_finalize (GObject *object)
252 EmpathyAvatarChooserPriv *priv;
254 priv = GET_PRIV (object);
256 avatar_chooser_set_connection (EMPATHY_AVATAR_CHOOSER (object), NULL);
257 g_assert (priv->connection == NULL);
258 g_assert (priv->factory == NULL);
260 if (priv->avatar != NULL) {
261 empathy_avatar_unref (priv->avatar);
264 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
267 static void
268 avatar_chooser_set_connection (EmpathyAvatarChooser *self,
269 TpConnection *connection)
271 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
273 if (priv->connection != NULL) {
274 g_object_unref (priv->connection);
275 priv->connection = NULL;
277 g_object_unref (priv->factory);
278 priv->factory = NULL;
281 if (connection != NULL) {
282 priv->connection = g_object_ref (connection);
283 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
287 static void
288 avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
289 const gchar *primary_text,
290 const gchar *secondary_text)
292 GtkWidget *parent;
293 GtkWidget *dialog;
295 parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
296 if (!GTK_IS_WINDOW (parent)) {
297 parent = NULL;
300 dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
301 GTK_DIALOG_MODAL,
302 GTK_MESSAGE_WARNING,
303 GTK_BUTTONS_CLOSE,
304 "%s", primary_text);
306 if (secondary_text != NULL) {
307 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
308 "%s", secondary_text);
311 g_signal_connect (dialog, "response",
312 G_CALLBACK (gtk_widget_destroy), NULL);
313 gtk_widget_show (dialog);
317 static gboolean
318 str_in_strv (const gchar *str,
319 gchar **strv)
321 if (strv == NULL) {
322 return FALSE;
325 while (*strv != NULL) {
326 if (g_str_equal (str, *strv)) {
327 return TRUE;
329 strv++;
331 return FALSE;
334 /* The caller must free the strings stored in satisfactory_format_name and
335 * satisfactory_mime_type.
337 static gboolean
338 avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
339 gchar **accepted_mime_types,
340 gchar **satisfactory_format_name,
341 gchar **satisfactory_mime_type)
343 gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
344 guint i;
345 GSList *formats, *l;
346 gboolean found = FALSE;
348 *satisfactory_format_name = NULL;
349 *satisfactory_mime_type = NULL;
351 /* If there is no accepted format there is nothing we can do */
352 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
353 return TRUE;
356 /* If the current mime type is good and accepted, don't change it!
357 * jpeg is compress better pictures, but png is better for logos and
358 * could have an alpha layer. */
359 if (str_in_strv (current_mime_type, good_mime_types) &&
360 str_in_strv (current_mime_type, accepted_mime_types)) {
361 *satisfactory_mime_type = g_strdup (current_mime_type);
362 *satisfactory_format_name = g_strdup (current_mime_type +
363 strlen ("image/"));
364 return FALSE;
367 /* The current mime type is either not accepted or not good to use.
368 * Check if one of the good format is supported... */
369 for (i = 0; good_mime_types[i] != NULL; i++) {
370 if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
371 *satisfactory_mime_type = g_strdup (good_mime_types[i]);
372 *satisfactory_format_name = g_strdup (good_mime_types[i] +
373 strlen ("image/"));
374 return TRUE;
378 /* Pick the first supported format we can write */
379 formats = gdk_pixbuf_get_formats ();
380 for (l = formats; !found && l != NULL; l = l->next) {
381 GdkPixbufFormat *format = l->data;
382 gchar **format_mime_types;
383 gchar **iter;
385 if (!gdk_pixbuf_format_is_writable (format)) {
386 continue;
389 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
390 for (iter = format_mime_types; *iter != NULL; iter++) {
391 if (str_in_strv (*iter, accepted_mime_types)) {
392 *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
393 *satisfactory_mime_type = g_strdup (*iter);
394 found = TRUE;
395 break;
398 g_strfreev (format_mime_types);
400 g_slist_free (formats);
402 return TRUE;
405 static EmpathyAvatar *
406 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
407 GdkPixbuf *pixbuf,
408 EmpathyAvatar *avatar)
410 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
411 guint max_width = 0, max_height = 0, max_size = 0;
412 gchar **mime_types = NULL;
413 gboolean needs_conversion = FALSE;
414 guint width, height;
415 gchar *new_format_name = NULL;
416 gchar *new_mime_type = NULL;
417 gdouble min_factor, max_factor;
418 gdouble factor;
419 gchar *converted_image_data = NULL;
420 gsize converted_image_size = 0;
422 g_object_get (priv->factory,
423 "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
424 "avatar-max-width", &max_width,
425 "avatar-max-height", &max_height,
426 "avatar-max-size", &max_size,
427 NULL);
429 /* Smaller is the factor, smaller will be the image.
430 * 0 is an empty image, 1 is the full size. */
431 min_factor = 0;
432 max_factor = 1;
433 factor = 1;
435 /* Check if we need to convert to another image format */
436 if (avatar_chooser_need_mime_type_conversion (avatar->format,
437 mime_types,
438 &new_format_name,
439 &new_mime_type)) {
440 DEBUG ("Format conversion needed, we'll use mime type '%s' "
441 "and format name '%s'. Current mime type is '%s'",
442 new_mime_type, new_format_name, avatar->format);
443 needs_conversion = TRUE;
445 g_strfreev (mime_types);
447 /* If there is no format we can use, report error to the user. */
448 if (new_mime_type == NULL || new_format_name == NULL) {
449 avatar_chooser_error_show (chooser, _("Couldn't convert image"),
450 _("None of the accepted image formats are "
451 "supported on your system"));
452 return NULL;
455 /* If width or height are too big, it needs converting. */
456 width = gdk_pixbuf_get_width (pixbuf);
457 height = gdk_pixbuf_get_height (pixbuf);
458 if ((max_width > 0 && width > max_width) ||
459 (max_height > 0 && height > max_height)) {
460 gdouble h_factor, v_factor;
462 h_factor = (gdouble) max_width / width;
463 v_factor = (gdouble) max_height / height;
464 factor = max_factor = MIN (h_factor, v_factor);
466 DEBUG ("Image dimensions (%dx%d) are too big. Max is %dx%d.",
467 width, height, max_width, max_height);
469 needs_conversion = TRUE;
472 /* If the data len is too big and no other conversion is needed,
473 * try with a lower factor. */
474 if (max_size > 0 && avatar->len > max_size && !needs_conversion) {
475 DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
476 "(max is %u bytes), conversion needed.",
477 avatar->len, max_size);
479 factor = 0.5;
480 needs_conversion = TRUE;
483 /* If no conversion is needed, return the avatar */
484 if (!needs_conversion) {
485 g_free (new_format_name);
486 g_free (new_mime_type);
487 return empathy_avatar_ref (avatar);
490 do {
491 GdkPixbuf *pixbuf_scaled = NULL;
492 gboolean saved;
493 gint new_width, new_height;
494 GError *error = NULL;
496 g_free (converted_image_data);
498 if (factor != 1) {
499 new_width = width * factor;
500 new_height = height * factor;
501 pixbuf_scaled = gdk_pixbuf_scale_simple (pixbuf,
502 new_width,
503 new_height,
504 GDK_INTERP_HYPER);
505 } else {
506 new_width = width;
507 new_height = height;
508 pixbuf_scaled = g_object_ref (pixbuf);
511 DEBUG ("Trying with factor %f (%dx%d) and format %s...", factor,
512 new_width, new_height, new_format_name);
514 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled,
515 &converted_image_data,
516 &converted_image_size,
517 new_format_name,
518 &error, NULL);
519 g_object_unref (pixbuf_scaled);
521 if (!saved) {
522 g_free (new_format_name);
523 g_free (new_mime_type);
524 avatar_chooser_error_show (chooser,
525 _("Couldn't convert image"),
526 error ? error->message : NULL);
527 g_clear_error (&error);
528 return NULL;
531 DEBUG ("Produced an image data of %"G_GSIZE_FORMAT" bytes.",
532 converted_image_size);
534 if (max_size == 0)
535 break;
537 /* Make a binary search for the bigest factor that produce
538 * an image data size less than max_size */
539 if (converted_image_size > max_size)
540 max_factor = factor;
541 if (converted_image_size < max_size)
542 min_factor = factor;
543 factor = (min_factor + max_factor)/2;
545 /* We are done if either:
546 * - min_factor == max_factor. That happens if we resized to
547 * the max required dimension and the produced data size is
548 * less than max_size.
549 * - The data size is close enough to max_size. Here we accept
550 * a difference of 1k.
552 } while (min_factor != max_factor &&
553 abs (max_size - converted_image_size) > 1024);
554 g_free (new_format_name);
556 /* Takes ownership of new_mime_type and converted_image_data */
557 avatar = empathy_avatar_new ((guchar *) converted_image_data,
558 converted_image_size, new_mime_type, NULL, NULL);
560 return avatar;
563 static void
564 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
566 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
567 GtkWidget *image;
569 if (priv->avatar != NULL) {
570 empathy_avatar_unref (priv->avatar);
571 priv->avatar = NULL;
574 image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
575 gtk_button_set_image (GTK_BUTTON (chooser), image);
576 g_signal_emit (chooser, signals[CHANGED], 0);
579 static void
580 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser,
581 gchar *data,
582 gsize size,
583 gboolean set_locally)
585 GdkPixbuf *pixbuf;
586 EmpathyAvatar *avatar = NULL;
587 gchar *mime_type = NULL;
589 if (data == NULL) {
590 avatar_chooser_clear_image (chooser);
591 return;
594 pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type);
595 if (pixbuf == NULL) {
596 g_free (data);
597 data = NULL;
598 return;
601 /* avatar takes ownership of data and mime_type */
602 avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL, NULL);
604 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
607 static void
608 avatar_chooser_set_image_from_avatar (EmpathyAvatarChooser *chooser,
609 EmpathyAvatar *avatar,
610 gboolean set_locally)
612 GdkPixbuf *pixbuf;
613 gchar *mime_type = NULL;
615 g_assert (avatar != NULL);
617 pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data,
618 avatar->len,
619 &mime_type);
620 if (pixbuf == NULL) {
621 DEBUG ("couldn't make a pixbuf from avatar; giving up");
622 return;
625 if (avatar->format == NULL) {
626 avatar->format = mime_type;
627 } else {
628 if (strcmp (mime_type, avatar->format)) {
629 DEBUG ("avatar->format is %s; gdkpixbuf yields %s!",
630 avatar->format, mime_type);
632 g_free (mime_type);
635 empathy_avatar_ref (avatar);
637 avatar_chooser_set_image (chooser, avatar, pixbuf, set_locally);
640 static void
641 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
642 EmpathyAvatar *avatar,
643 GdkPixbuf *pixbuf,
644 gboolean set_locally)
646 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
647 GdkPixbuf *pixbuf_view;
648 GtkWidget *image;
650 g_assert (avatar != NULL);
651 g_assert (pixbuf != NULL);
653 if (set_locally) {
654 EmpathyAvatar *conv;
656 conv = avatar_chooser_maybe_convert_and_scale (chooser,
657 pixbuf, avatar);
658 empathy_avatar_unref (avatar);
660 if (conv == NULL) {
661 /* An error occured; don't change the avatar. */
662 return;
665 avatar = conv;
668 if (priv->avatar != NULL) {
669 empathy_avatar_unref (priv->avatar);
671 priv->avatar = avatar;
673 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
674 image = gtk_image_new_from_pixbuf (pixbuf_view);
676 gtk_button_set_image (GTK_BUTTON (chooser), image);
677 g_signal_emit (chooser, signals[CHANGED], 0);
679 g_object_unref (pixbuf_view);
680 g_object_unref (pixbuf);
683 static void
684 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
685 const gchar *filename)
687 gchar *image_data = NULL;
688 gsize image_size = 0;
689 GError *error = NULL;
691 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
692 DEBUG ("Failed to load image from '%s': %s", filename,
693 error ? error->message : "No error given");
695 g_clear_error (&error);
696 return;
699 avatar_chooser_set_image_from_data (chooser, image_data, image_size, TRUE);
702 static gboolean
703 avatar_chooser_drag_motion_cb (GtkWidget *widget,
704 GdkDragContext *context,
705 gint x,
706 gint y,
707 guint time_,
708 EmpathyAvatarChooser *chooser)
710 EmpathyAvatarChooserPriv *priv;
711 GList *p;
713 priv = GET_PRIV (chooser);
715 for (p = context->targets; p != NULL; p = p->next) {
716 gchar *possible_type;
718 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
720 if (!strcmp (possible_type, URI_LIST_TYPE)) {
721 g_free (possible_type);
722 gdk_drag_status (context, GDK_ACTION_COPY, time_);
724 return TRUE;
727 g_free (possible_type);
730 return FALSE;
733 static void
734 avatar_chooser_drag_leave_cb (GtkWidget *widget,
735 GdkDragContext *context,
736 guint time_,
737 EmpathyAvatarChooser *chooser)
741 static gboolean
742 avatar_chooser_drag_drop_cb (GtkWidget *widget,
743 GdkDragContext *context,
744 gint x,
745 gint y,
746 guint time_,
747 EmpathyAvatarChooser *chooser)
749 EmpathyAvatarChooserPriv *priv;
750 GList *p;
752 priv = GET_PRIV (chooser);
754 if (context->targets == NULL) {
755 return FALSE;
758 for (p = context->targets; p != NULL; p = p->next) {
759 char *possible_type;
761 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
762 if (!strcmp (possible_type, URI_LIST_TYPE)) {
763 g_free (possible_type);
764 gtk_drag_get_data (widget, context,
765 GDK_POINTER_TO_ATOM (p->data),
766 time_);
768 return TRUE;
771 g_free (possible_type);
774 return FALSE;
777 static void
778 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
779 GdkDragContext *context,
780 gint x,
781 gint y,
782 GtkSelectionData *selection_data,
783 guint info,
784 guint time_,
785 EmpathyAvatarChooser *chooser)
787 gchar *target_type;
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)) {
792 GFile *file;
793 gchar *nl;
794 gchar *data = NULL;
795 gsize bytes_read;
797 nl = strstr ((gchar *) gtk_selection_data_get_data (selection_data),
798 "\r\n");
799 if (nl) {
800 gchar *uri;
802 uri = g_strndup ((gchar *) gtk_selection_data_get_data (selection_data),
803 nl - (gchar *) gtk_selection_data_get_data (selection_data));
805 file = g_file_new_for_uri (uri);
806 g_free (uri);
807 } else {
808 file = g_file_new_for_uri ((gchar *) gtk_selection_data_get_data (
809 selection_data));
812 handled = g_file_load_contents (file, NULL, &data, &bytes_read,
813 NULL, NULL);
815 if (handled) {
816 /* this in turn calls empathy_avatar_new (), which assumes
817 * ownership of data.
819 avatar_chooser_set_image_from_data (chooser, data,
820 bytes_read,
821 TRUE);
824 g_object_unref (file);
827 gtk_drag_finish (context, handled, FALSE, time_);
830 static void
831 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
832 EmpathyAvatarChooser *chooser)
834 gchar *filename;
836 filename = gtk_file_chooser_get_preview_filename (file_chooser);
838 if (filename) {
839 GtkWidget *image;
840 GdkPixbuf *pixbuf = NULL;
841 GdkPixbuf *scaled_pixbuf;
843 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
845 image = gtk_file_chooser_get_preview_widget (file_chooser);
847 if (pixbuf) {
848 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
849 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
850 g_object_unref (scaled_pixbuf);
851 g_object_unref (pixbuf);
852 } else {
853 gtk_image_set_from_stock (GTK_IMAGE (image),
854 "gtk-dialog-question",
855 GTK_ICON_SIZE_DIALOG);
858 g_free (filename);
861 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
864 static void
865 avatar_chooser_response_cb (GtkWidget *widget,
866 gint response,
867 EmpathyAvatarChooser *chooser)
869 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
871 priv->chooser_dialog = NULL;
873 if (response == GTK_RESPONSE_OK) {
874 gchar *filename;
875 gchar *path;
877 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
878 avatar_chooser_set_image_from_file (chooser, filename);
879 g_free (filename);
881 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
882 if (path) {
883 empathy_conf_set_string (empathy_conf_get (),
884 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
885 path);
886 g_free (path);
889 else if (response == GTK_RESPONSE_NO) {
890 /* This corresponds to "No Image", not to "Cancel" */
891 avatar_chooser_clear_image (chooser);
894 gtk_widget_destroy (widget);
897 static void
898 avatar_chooser_clicked_cb (GtkWidget *button,
899 EmpathyAvatarChooser *chooser)
901 GtkFileChooser *chooser_dialog;
902 GtkWidget *image;
903 gchar *saved_dir = NULL;
904 const gchar *default_dir = DEFAULT_DIR;
905 const gchar *pics_dir;
906 GtkFileFilter *filter;
907 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
909 if (priv->chooser_dialog) {
910 gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
911 return;
914 priv->chooser_dialog = GTK_FILE_CHOOSER (
915 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
916 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
917 GTK_FILE_CHOOSER_ACTION_OPEN,
918 _("No Image"),
919 GTK_RESPONSE_NO,
920 GTK_STOCK_CANCEL,
921 GTK_RESPONSE_CANCEL,
922 GTK_STOCK_OPEN,
923 GTK_RESPONSE_OK,
924 NULL));
925 chooser_dialog = priv->chooser_dialog;
926 gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
928 /* Get special dirs */
929 empathy_conf_get_string (empathy_conf_get (),
930 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
931 &saved_dir);
932 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
933 g_free (saved_dir);
934 saved_dir = NULL;
936 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
937 default_dir = NULL;
939 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
940 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
941 pics_dir = NULL;
944 /* Set current dir to the last one or to DEFAULT_DIR or to home */
945 if (saved_dir) {
946 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
948 else if (pics_dir) {
949 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
951 else if (default_dir) {
952 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
953 } else {
954 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
957 /* Add shortcuts to special dirs */
958 if (saved_dir) {
959 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
961 else if (pics_dir) {
962 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
964 if (default_dir) {
965 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
968 /* Setup preview image */
969 image = gtk_image_new ();
970 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
971 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
972 gtk_widget_show (image);
973 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
974 g_signal_connect (chooser_dialog, "update-preview",
975 G_CALLBACK (avatar_chooser_update_preview_cb),
976 chooser);
978 /* Setup filers */
979 filter = gtk_file_filter_new ();
980 gtk_file_filter_set_name (filter, _("Images"));
981 gtk_file_filter_add_pixbuf_formats (filter);
982 gtk_file_chooser_add_filter (chooser_dialog, filter);
983 filter = gtk_file_filter_new ();
984 gtk_file_filter_set_name (filter, _("All Files"));
985 gtk_file_filter_add_pattern (filter, "*");
986 gtk_file_chooser_add_filter (chooser_dialog, filter);
988 /* Setup response */
989 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
990 g_signal_connect (chooser_dialog, "response",
991 G_CALLBACK (avatar_chooser_response_cb),
992 chooser);
994 gtk_widget_show (GTK_WIDGET (chooser_dialog));
995 g_free (saved_dir);
999 * empathy_avatar_chooser_new:
1001 * Creates a new #EmpathyAvatarChooser.
1003 * Return value: a new #EmpathyAvatarChooser
1005 GtkWidget *
1006 empathy_avatar_chooser_new ()
1008 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
1012 * empathy_avatar_chooser_set:
1013 * @chooser: an #EmpathyAvatarChooser
1014 * @avatar: a new #EmpathyAvatar
1016 * Sets the @chooser to display the avatar indicated by @avatar.
1018 void
1019 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
1020 EmpathyAvatar *avatar)
1022 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1024 if (avatar != NULL) {
1025 avatar_chooser_set_image_from_avatar (chooser, avatar, FALSE);
1026 } else {
1027 avatar_chooser_clear_image (chooser);
1032 * empathy_avatar_chooser_get_image_data:
1033 * @chooser: an #EmpathyAvatarChooser
1034 * @data: avatar bytes
1035 * @data_size: size of @data
1036 * @mime_type: avatar mime-type
1038 * Gets image data about the currently selected avatar.
1040 void
1041 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
1042 const gchar **data,
1043 gsize *data_size,
1044 const gchar **mime_type)
1046 EmpathyAvatarChooserPriv *priv;
1048 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
1050 priv = GET_PRIV (chooser);
1052 if (priv->avatar != NULL) {
1053 if (data != NULL) {
1054 *data = (gchar *) priv->avatar->data;
1056 if (data_size != NULL) {
1057 *data_size = priv->avatar->len;
1059 if (mime_type != NULL) {
1060 *mime_type = priv->avatar->format;
1062 } else {
1063 if (data != NULL) {
1064 *data = NULL;
1066 if (data_size != NULL) {
1067 *data_size = 0;
1069 if (mime_type != NULL) {
1070 *mime_type = NULL;