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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Based on Novell's e-image-chooser.
21 * Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n.h>
32 #include <libempathy/empathy-utils.h>
33 #include "empathy-avatar-chooser.h"
34 #include "empathy-conf.h"
35 #include "empathy-ui-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
40 #define AVATAR_SIZE_SAVE 96
41 #define AVATAR_SIZE_VIEW 64
42 #define DEFAULT_DIR DATADIR"/pixmaps/faces"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarChooser)
46 EmpathyContactFactory *contact_factory;
48 EmpathyTpContactFactory *tp_contact_factory;
50 gulong ready_handler_id;
52 EmpathyAvatar *avatar;
53 } EmpathyAvatarChooserPriv;
55 static void avatar_chooser_finalize (GObject *object);
56 static void avatar_chooser_set_account (EmpathyAvatarChooser *self,
58 static void avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
61 gboolean set_locally);
62 static gboolean avatar_chooser_drag_motion_cb (GtkWidget *widget,
63 GdkDragContext *context,
67 EmpathyAvatarChooser *chooser);
68 static void avatar_chooser_drag_leave_cb (GtkWidget *widget,
69 GdkDragContext *context,
71 EmpathyAvatarChooser *chooser);
72 static gboolean avatar_chooser_drag_drop_cb (GtkWidget *widget,
73 GdkDragContext *context,
77 EmpathyAvatarChooser *chooser);
78 static void avatar_chooser_drag_data_received_cb (GtkWidget *widget,
79 GdkDragContext *context,
82 GtkSelectionData *selection_data,
85 EmpathyAvatarChooser *chooser);
86 static void avatar_chooser_clicked_cb (GtkWidget *button,
87 EmpathyAvatarChooser *chooser);
99 static guint signals [LAST_SIGNAL];
101 G_DEFINE_TYPE (EmpathyAvatarChooser, empathy_avatar_chooser, GTK_TYPE_BUTTON);
104 * Drag and drop stuff
106 #define URI_LIST_TYPE "text/uri-list"
109 DND_TARGET_TYPE_URI_LIST
112 static const GtkTargetEntry drop_types[] = {
113 { URI_LIST_TYPE, 0, DND_TARGET_TYPE_URI_LIST },
117 avatar_chooser_get_property (GObject *object,
122 EmpathyAvatarChooserPriv *priv = GET_PRIV (object);
126 g_value_set_object (value, priv->account);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
135 avatar_chooser_set_property (GObject *object,
140 EmpathyAvatarChooser *self = EMPATHY_AVATAR_CHOOSER (object);
144 avatar_chooser_set_account (self, g_value_get_object (value));
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
153 empathy_avatar_chooser_class_init (EmpathyAvatarChooserClass *klass)
155 GObjectClass *object_class = G_OBJECT_CLASS (klass);
156 GParamSpec *param_spec;
158 object_class->finalize = avatar_chooser_finalize;
159 object_class->get_property = avatar_chooser_get_property;
160 object_class->set_property = avatar_chooser_set_property;
163 g_signal_new ("changed",
164 G_TYPE_FROM_CLASS (klass),
168 g_cclosure_marshal_VOID__VOID,
171 param_spec = g_param_spec_object ("account",
173 "McAccount whose avatar should be "
174 "shown and modified by this widget",
177 G_PARAM_STATIC_STRINGS);
178 g_object_class_install_property (object_class,
182 g_type_class_add_private (object_class, sizeof (EmpathyAvatarChooserPriv));
186 empathy_avatar_chooser_init (EmpathyAvatarChooser *chooser)
188 EmpathyAvatarChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
189 EMPATHY_TYPE_AVATAR_CHOOSER, EmpathyAvatarChooserPriv);
191 chooser->priv = priv;
192 gtk_drag_dest_set (GTK_WIDGET (chooser),
193 GTK_DEST_DEFAULT_ALL,
195 G_N_ELEMENTS (drop_types),
198 g_signal_connect (chooser, "drag-motion",
199 G_CALLBACK (avatar_chooser_drag_motion_cb),
201 g_signal_connect (chooser, "drag-leave",
202 G_CALLBACK (avatar_chooser_drag_leave_cb),
204 g_signal_connect (chooser, "drag-drop",
205 G_CALLBACK (avatar_chooser_drag_drop_cb),
207 g_signal_connect (chooser, "drag-data-received",
208 G_CALLBACK (avatar_chooser_drag_data_received_cb),
210 g_signal_connect (chooser, "clicked",
211 G_CALLBACK (avatar_chooser_clicked_cb),
214 priv->contact_factory = empathy_contact_factory_new ();
216 empathy_avatar_chooser_set (chooser, NULL);
220 avatar_chooser_finalize (GObject *object)
222 EmpathyAvatarChooserPriv *priv;
224 priv = GET_PRIV (object);
226 avatar_chooser_set_account (EMPATHY_AVATAR_CHOOSER (object), NULL);
227 g_assert (priv->account == NULL && priv->tp_contact_factory == NULL);
229 g_object_unref (priv->contact_factory);
231 if (priv->avatar != NULL) {
232 empathy_avatar_unref (priv->avatar);
235 G_OBJECT_CLASS (empathy_avatar_chooser_parent_class)->finalize (object);
239 avatar_chooser_tp_cf_ready_cb (EmpathyTpContactFactory *tp_cf,
241 EmpathyAvatarChooser *self)
243 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
246 /* sanity check that we're listening on the right ETpCF */
247 g_assert (priv->tp_contact_factory == tp_cf);
249 ready = empathy_tp_contact_factory_is_ready (tp_cf);
250 gtk_widget_set_sensitive (GTK_WIDGET (self), ready);
254 avatar_chooser_set_account (EmpathyAvatarChooser *self,
257 EmpathyAvatarChooserPriv *priv = GET_PRIV (self);
259 if (priv->account != NULL) {
260 g_object_unref (priv->account);
261 priv->account = NULL;
263 g_assert (priv->tp_contact_factory != NULL);
265 g_signal_handler_disconnect (priv->tp_contact_factory,
266 priv->ready_handler_id);
267 priv->ready_handler_id = 0;
269 g_object_unref (priv->tp_contact_factory);
270 priv->tp_contact_factory = NULL;
273 if (account != NULL) {
274 priv->account = g_object_ref (account);
275 priv->tp_contact_factory = g_object_ref (
276 empathy_contact_factory_get_tp_factory (
277 priv->contact_factory, priv->account));
279 priv->ready_handler_id = g_signal_connect (
280 priv->tp_contact_factory, "notify::ready",
281 G_CALLBACK (avatar_chooser_tp_cf_ready_cb), self);
282 avatar_chooser_tp_cf_ready_cb (priv->tp_contact_factory, NULL,
288 str_in_strv (gchar *str,
295 while (*strv != NULL) {
296 if (g_str_equal (str, *strv)) {
304 /* The caller must free the strings stored in satisfactory_format_name and
305 * satisfactory_mime_type.
308 can_satisfy_mime_type_requirements (gchar **accepted_mime_types,
309 gchar **satisfactory_format_name,
310 gchar **satisfactory_mime_type)
317 gboolean done = FALSE;
319 if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
323 g_assert (satisfactory_format_name != NULL);
324 g_assert (satisfactory_mime_type != NULL);
326 formats = gdk_pixbuf_get_formats ();
328 for (l = formats; !done && l != NULL; l = l->next) {
329 GdkPixbufFormat *format = l->data;
330 gchar **format_mime_types;
332 if (!gdk_pixbuf_format_is_writable (format)) {
336 format_mime_types = gdk_pixbuf_format_get_mime_types (format);
337 for (strv = format_mime_types; *strv != NULL; strv++) {
338 if (str_in_strv (*strv, accepted_mime_types)) {
339 name = gdk_pixbuf_format_get_name (format);
340 type = g_strdup (*strv);
345 g_strfreev (format_mime_types);
348 g_slist_free (formats);
351 if (satisfactory_format_name != NULL) {
352 *satisfactory_format_name = name;
356 if (satisfactory_mime_type != NULL) {
357 *satisfactory_mime_type = type;
363 /* check we're not leaking. */
364 g_assert (name == NULL && type == NULL);
369 static EmpathyAvatar *
370 avatar_chooser_convert (EmpathyAvatarChooser *chooser,
371 GdkPixbuf *pixbuf_scaled,
375 gchar *format_name = NULL, *new_mime_type = NULL;
376 gchar *converted_image_data = NULL;
377 gsize converted_image_size = 0;
378 EmpathyAvatar *converted_avatar = NULL;
380 GError *error = NULL;
382 if (!can_satisfy_mime_type_requirements (mime_types, &format_name,
384 DEBUG ("Mon dieu! Can't convert to any acceptable format!");
388 saved = gdk_pixbuf_save_to_buffer (pixbuf_scaled, &converted_image_data,
389 &converted_image_size, format_name, &error, NULL);
390 g_free (format_name);
393 DEBUG ("Couldn't convert image: %s", error->message);
394 g_error_free (error);
396 g_free (new_mime_type);
400 /* Takes ownership of new_mime_type */
401 converted_avatar = empathy_avatar_new (converted_image_data,
402 converted_image_size, new_mime_type, NULL);
404 if (max_size > 0 && converted_avatar->len > max_size) {
405 /* FIXME: We could try converting to a different format; in
406 * particular, try converting to jpeg with increasingly
407 * high compression (if jpeg is supported). Not sure how
410 DEBUG ("Converted the image, but the new filesize is too big");
412 empathy_avatar_unref (converted_avatar);
413 converted_avatar = NULL;
416 return converted_avatar;
419 static EmpathyAvatar *
420 avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *chooser,
422 EmpathyAvatar *avatar)
424 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
425 EmpathyTpContactFactory *tp_cf = priv->tp_contact_factory;
426 gint max_width = 0, max_height = 0, max_size = 0;
427 gchar **mime_types = NULL;
428 gboolean needs_conversion = FALSE;
429 GdkPixbuf *pixbuf_scaled = NULL;
431 /* This should only be called if the user is setting a new avatar,
432 * which should only be allowed once the avatar requirements have been
435 g_return_val_if_fail (tp_cf != NULL, NULL);
436 g_return_val_if_fail (empathy_tp_contact_factory_is_ready (tp_cf),
440 "avatar-mime-types", &mime_types, /* Needs g_strfreev-ing */
441 "avatar-max-width", &max_width,
442 "avatar-max-height", &max_height,
443 "avatar-max-size", &max_size,
446 /* If the avatar's not already the right type, it needs converting. */
447 if (!str_in_strv (avatar->format, mime_types)) {
448 needs_conversion = TRUE;
451 /* If scaling down the pixbuf to fit the dimensions yields a new
452 * pixbuf, then it needed scaling. if it's the same pixbuf, it did not.
454 pixbuf_scaled = empathy_pixbuf_scale_down_if_necessary (pixbuf,
455 MIN(max_width, max_height));
456 if (pixbuf_scaled != pixbuf) {
457 needs_conversion = TRUE;
460 if (max_size > 0 && avatar->len > max_size) {
461 needs_conversion = TRUE;
464 if (needs_conversion) {
465 avatar = avatar_chooser_convert (chooser, pixbuf_scaled,
466 mime_types, max_size);
468 /* Just return another reference to the avatar passed in. */
469 avatar = empathy_avatar_ref (avatar);
472 g_object_unref (pixbuf_scaled);
473 g_strfreev (mime_types);
478 avatar_chooser_clear_image (EmpathyAvatarChooser *chooser)
480 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
483 if (priv->avatar == NULL) {
487 empathy_avatar_unref (priv->avatar);
490 image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
491 gtk_button_set_image (GTK_BUTTON (chooser), image);
492 g_signal_emit (chooser, signals[CHANGED], 0);
496 avatar_chooser_set_image (EmpathyAvatarChooser *chooser,
499 gboolean set_locally)
501 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
503 GdkPixbuf *pixbuf, *pixbuf_view;
505 EmpathyAvatar *avatar = NULL;
506 gchar *mime_type = NULL;
509 avatar_chooser_clear_image (chooser);
514 pixbuf = empathy_pixbuf_from_data (data, size, &mime_type);
515 if (pixbuf == NULL) {
520 /* avatar takes ownership of data and mime_type */
521 avatar = empathy_avatar_new (data, size, mime_type, NULL);
524 EmpathyAvatar *conv = avatar_chooser_maybe_convert_and_scale (
525 chooser, pixbuf, avatar);
526 empathy_avatar_unref (avatar);
530 if (avatar == NULL) {
531 /* An error occured; don't change the avatar. */
535 if (priv->avatar != NULL) {
536 empathy_avatar_unref (priv->avatar);
538 priv->avatar = avatar;
540 pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW);
541 image = gtk_image_new_from_pixbuf (pixbuf_view);
543 gtk_button_set_image (GTK_BUTTON (chooser), image);
544 g_signal_emit (chooser, signals[CHANGED], 0);
546 g_object_unref (pixbuf_view);
547 g_object_unref (pixbuf);
551 avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser,
552 const gchar *filename)
554 gchar *image_data = NULL;
555 gsize image_size = 0;
556 GError *error = NULL;
558 if (!g_file_get_contents (filename, &image_data, &image_size, &error)) {
559 DEBUG ("Failed to load image from '%s': %s", filename,
560 error ? error->message : "No error given");
562 g_clear_error (&error);
566 avatar_chooser_set_image (chooser, image_data, image_size, TRUE);
570 avatar_chooser_drag_motion_cb (GtkWidget *widget,
571 GdkDragContext *context,
575 EmpathyAvatarChooser *chooser)
577 EmpathyAvatarChooserPriv *priv;
580 priv = GET_PRIV (chooser);
582 for (p = context->targets; p != NULL; p = p->next) {
583 gchar *possible_type;
585 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
587 if (!strcmp (possible_type, URI_LIST_TYPE)) {
588 g_free (possible_type);
589 gdk_drag_status (context, GDK_ACTION_COPY, time);
594 g_free (possible_type);
601 avatar_chooser_drag_leave_cb (GtkWidget *widget,
602 GdkDragContext *context,
604 EmpathyAvatarChooser *chooser)
609 avatar_chooser_drag_drop_cb (GtkWidget *widget,
610 GdkDragContext *context,
614 EmpathyAvatarChooser *chooser)
616 EmpathyAvatarChooserPriv *priv;
619 priv = GET_PRIV (chooser);
621 if (context->targets == NULL) {
625 for (p = context->targets; p != NULL; p = p->next) {
628 possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
629 if (!strcmp (possible_type, URI_LIST_TYPE)) {
630 g_free (possible_type);
631 gtk_drag_get_data (widget, context,
632 GDK_POINTER_TO_ATOM (p->data),
638 g_free (possible_type);
645 avatar_chooser_drag_data_received_cb (GtkWidget *widget,
646 GdkDragContext *context,
649 GtkSelectionData *selection_data,
652 EmpathyAvatarChooser *chooser)
655 gboolean handled = FALSE;
657 target_type = gdk_atom_name (selection_data->target);
658 if (!strcmp (target_type, URI_LIST_TYPE)) {
660 GFileInputStream *input_stream;
664 nl = strstr (selection_data->data, "\r\n");
668 uri = g_strndup (selection_data->data,
669 nl - (gchar*) selection_data->data);
671 file = g_file_new_for_uri (uri);
674 file = g_file_new_for_uri (selection_data->data);
677 input_stream = g_file_read (file, NULL, NULL);
679 if (input_stream != NULL) {
682 info = g_file_query_info (file,
683 G_FILE_ATTRIBUTE_STANDARD_SIZE,
689 size = g_file_info_get_size (info);
690 data = g_malloc (size);
692 bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
695 if (bytes_read != -1) {
696 avatar_chooser_set_image (chooser,
704 g_object_unref (info);
707 g_object_unref (input_stream);
710 g_object_unref (file);
713 gtk_drag_finish (context, handled, FALSE, time);
717 avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser,
718 EmpathyAvatarChooser *chooser)
722 filename = gtk_file_chooser_get_preview_filename (file_chooser);
726 GdkPixbuf *pixbuf = NULL;
727 GdkPixbuf *scaled_pixbuf;
729 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
731 image = gtk_file_chooser_get_preview_widget (file_chooser);
734 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE);
735 gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf);
736 g_object_unref (scaled_pixbuf);
737 g_object_unref (pixbuf);
739 gtk_image_set_from_stock (GTK_IMAGE (image),
740 "gtk-dialog-question",
741 GTK_ICON_SIZE_DIALOG);
745 gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
749 avatar_chooser_response_cb (GtkWidget *widget,
751 EmpathyAvatarChooser *chooser)
753 EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
755 if (response == GTK_RESPONSE_CANCEL) {
759 /* Check if we went non-ready since displaying the dialog. */
760 if (!empathy_tp_contact_factory_is_ready (priv->tp_contact_factory)) {
761 DEBUG ("Can't set avatar when contact factory isn't ready.");
765 if (response == GTK_RESPONSE_OK) {
769 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
770 avatar_chooser_set_image_from_file (chooser, filename);
773 path = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget));
775 empathy_conf_set_string (empathy_conf_get (),
776 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
781 else if (response == GTK_RESPONSE_NO) {
782 /* This corresponds to "No Image", not to "Cancel" */
783 avatar_chooser_clear_image (chooser);
787 gtk_widget_destroy (widget);
791 avatar_chooser_clicked_cb (GtkWidget *button,
792 EmpathyAvatarChooser *chooser)
794 GtkFileChooser *chooser_dialog;
796 gchar *saved_dir = NULL;
797 const gchar *default_dir = DEFAULT_DIR;
798 const gchar *pics_dir;
799 GtkFileFilter *filter;
801 chooser_dialog = GTK_FILE_CHOOSER (
802 gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
803 empathy_get_toplevel_window (GTK_WIDGET (chooser)),
804 GTK_FILE_CHOOSER_ACTION_OPEN,
813 /* Get special dirs */
814 empathy_conf_get_string (empathy_conf_get (),
815 EMPATHY_PREFS_UI_AVATAR_DIRECTORY,
817 if (saved_dir && !g_file_test (saved_dir, G_FILE_TEST_IS_DIR)) {
821 if (!g_file_test (default_dir, G_FILE_TEST_IS_DIR)) {
824 pics_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
825 if (pics_dir && !g_file_test (pics_dir, G_FILE_TEST_IS_DIR)) {
829 /* Set current dir to the last one or to DEFAULT_DIR or to home */
831 gtk_file_chooser_set_current_folder (chooser_dialog, saved_dir);
834 gtk_file_chooser_set_current_folder (chooser_dialog, pics_dir);
836 else if (default_dir) {
837 gtk_file_chooser_set_current_folder (chooser_dialog, default_dir);
839 gtk_file_chooser_set_current_folder (chooser_dialog, g_get_home_dir ());
842 /* Add shortcuts to special dirs */
844 gtk_file_chooser_add_shortcut_folder (chooser_dialog, saved_dir, NULL);
847 gtk_file_chooser_add_shortcut_folder (chooser_dialog, pics_dir, NULL);
850 gtk_file_chooser_add_shortcut_folder (chooser_dialog, default_dir, NULL);
853 /* Setup preview image */
854 image = gtk_image_new ();
855 gtk_file_chooser_set_preview_widget (chooser_dialog, image);
856 gtk_widget_set_size_request (image, AVATAR_SIZE_SAVE, AVATAR_SIZE_SAVE);
857 gtk_widget_show (image);
858 gtk_file_chooser_set_use_preview_label (chooser_dialog, FALSE);
859 g_signal_connect (chooser_dialog, "update-preview",
860 G_CALLBACK (avatar_chooser_update_preview_cb),
864 filter = gtk_file_filter_new ();
865 gtk_file_filter_set_name (filter, _("Images"));
866 gtk_file_filter_add_pixbuf_formats (filter);
867 gtk_file_chooser_add_filter (chooser_dialog, filter);
868 filter = gtk_file_filter_new ();
869 gtk_file_filter_set_name (filter, _("All Files"));
870 gtk_file_filter_add_pattern(filter, "*");
871 gtk_file_chooser_add_filter (chooser_dialog, filter);
874 gtk_dialog_set_default_response (GTK_DIALOG (chooser_dialog), GTK_RESPONSE_OK);
875 g_signal_connect (chooser_dialog, "response",
876 G_CALLBACK (avatar_chooser_response_cb),
879 gtk_widget_show (GTK_WIDGET (chooser_dialog));
884 empathy_avatar_chooser_new ()
886 return g_object_new (EMPATHY_TYPE_AVATAR_CHOOSER, NULL);
889 /* FIXME: when the avatar passed to this function actually can be relied upon to
890 * contain a mime type, we can probably just ref it and store it.
893 empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser,
894 EmpathyAvatar *avatar)
896 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
898 if (avatar != NULL) {
899 gchar *data = g_memdup (avatar->data, avatar->len);
900 avatar_chooser_set_image (chooser, data, avatar->len, FALSE);
902 avatar_chooser_clear_image (chooser);
907 empathy_avatar_chooser_get_image_data (EmpathyAvatarChooser *chooser,
910 const gchar **mime_type)
912 EmpathyAvatarChooserPriv *priv;
914 g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser));
916 priv = GET_PRIV (chooser);
918 if (priv->avatar != NULL) {
920 *data = priv->avatar->data;
922 if (data_size != NULL) {
923 *data_size = priv->avatar->len;
925 if (mime_type != NULL) {
926 *mime_type = priv->avatar->format;
932 if (data_size != NULL) {
935 if (mime_type != NULL) {