#include <string.h>
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
EmpathyContactFactory *contact_factory;
McAccount *account;
EmpathyTpContactFactory *tp_contact_factory;
+ GtkFileChooser *chooser_dialog;
gulong ready_handler_id;
G_CALLBACK (avatar_chooser_clicked_cb),
chooser);
- priv->contact_factory = empathy_contact_factory_new ();
+ priv->contact_factory = empathy_contact_factory_dup_singleton ();
empathy_avatar_chooser_set (chooser, NULL);
}
}
}
+static void
+avatar_chooser_error_show (EmpathyAvatarChooser *chooser,
+ const gchar *primary_text,
+ const gchar *secondary_text)
+{
+ GtkWidget *parent;
+ GtkWidget *dialog;
+
+ parent = gtk_widget_get_toplevel (GTK_WIDGET (chooser));
+ if (!GTK_IS_WINDOW (parent)) {
+ parent = NULL;
+ }
+
+ dialog = gtk_message_dialog_new (parent ? GTK_WINDOW (parent) : NULL,
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_CLOSE,
+ "%s", primary_text);
+
+ if (secondary_text != NULL) {
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+ "%s", secondary_text);
+ }
+
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (gtk_widget_destroy), NULL);
+ gtk_widget_show (dialog);
+
+}
+
static gboolean
-str_in_strv (gchar *str,
+str_in_strv (const gchar *str,
gchar **strv)
{
if (strv == NULL) {
* satisfactory_mime_type.
*/
static gboolean
-can_satisfy_mime_type_requirements (gchar **accepted_mime_types,
- gchar **satisfactory_format_name,
- gchar **satisfactory_mime_type)
+avatar_chooser_need_mime_type_conversion (const gchar *current_mime_type,
+ gchar **accepted_mime_types,
+ gchar **satisfactory_format_name,
+ gchar **satisfactory_mime_type)
{
- gchar *name = NULL;
- gchar *type = NULL;
+ gchar *good_mime_types[] = {"image/jpeg", "image/png", NULL};
+ guint i;
+ GSList *formats, *l;
+ gboolean found = FALSE;
+ *satisfactory_format_name = NULL;
+ *satisfactory_mime_type = NULL;
+
+ /* If there is no accepted format there is nothing we can do */
if (accepted_mime_types == NULL || *accepted_mime_types == NULL) {
+ return TRUE;
+ }
+
+ /* If the current mime type is good and accepted, don't change it!
+ * jpeg is compress better pictures, but png is better for logos and
+ * could have an alpha layer. */
+ if (str_in_strv (current_mime_type, good_mime_types) &&
+ str_in_strv (current_mime_type, accepted_mime_types)) {
+ *satisfactory_mime_type = g_strdup (current_mime_type);
+ *satisfactory_format_name = g_strdup (current_mime_type +
+ strlen ("image/"));
return FALSE;
}
- /* Special-case png and jpeg to avoid accidentally saving to something
- * uncompressed like bmp. This assumes that we can write image/png and
- * image/jpeg; if this isn't true then something's really wrong with
- * GdkPixbuf.
- */
- if (str_in_strv ("image/jpeg", accepted_mime_types)) {
- name = g_strdup ("jpeg");
- type = g_strdup ("image/jpeg");
- } else if (str_in_strv ("image/png", accepted_mime_types)) {
- name = g_strdup ("png");
- type = g_strdup ("image/png");
- } else {
- GSList *formats, *l;
- gboolean found = FALSE;
+ /* The current mime type is either not accepted or not good to use.
+ * Check if one of the good format is supported... */
+ for (i = 0; good_mime_types[i] != NULL; i++) {
+ if (str_in_strv (good_mime_types[i], accepted_mime_types)) {
+ *satisfactory_mime_type = g_strdup (good_mime_types[i]);
+ *satisfactory_format_name = g_strdup (good_mime_types[i] +
+ strlen ("image/"));
+ return TRUE;
+ }
+ }
- formats = gdk_pixbuf_get_formats ();
- for (l = formats; !found && l != NULL; l = l->next) {
- GdkPixbufFormat *format = l->data;
- gchar **format_mime_types;
- gchar **iter;
+ /* Pick the first supported format we can write */
+ formats = gdk_pixbuf_get_formats ();
+ for (l = formats; !found && l != NULL; l = l->next) {
+ GdkPixbufFormat *format = l->data;
+ gchar **format_mime_types;
+ gchar **iter;
- if (!gdk_pixbuf_format_is_writable (format)) {
- continue;
- }
+ if (!gdk_pixbuf_format_is_writable (format)) {
+ continue;
+ }
- format_mime_types = gdk_pixbuf_format_get_mime_types (format);
- for (iter = format_mime_types; *iter != NULL; iter++) {
- if (str_in_strv (*iter, accepted_mime_types)) {
- name = gdk_pixbuf_format_get_name (format);
- type = g_strdup (*iter);
- found = TRUE;
- break;
- }
+ format_mime_types = gdk_pixbuf_format_get_mime_types (format);
+ for (iter = format_mime_types; *iter != NULL; iter++) {
+ if (str_in_strv (*iter, accepted_mime_types)) {
+ *satisfactory_format_name = gdk_pixbuf_format_get_name (format);
+ *satisfactory_mime_type = g_strdup (*iter);
+ found = TRUE;
+ break;
}
- g_strfreev (format_mime_types);
}
-
- g_slist_free (formats);
+ g_strfreev (format_mime_types);
}
+ g_slist_free (formats);
- *satisfactory_format_name = name;
- *satisfactory_mime_type = type;
-
- return name != NULL;
+ return TRUE;
}
static EmpathyAvatar *
{
EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
EmpathyTpContactFactory *tp_cf = priv->tp_contact_factory;
- gint max_width = 0, max_height = 0, max_size = 0;
+ guint max_width = 0, max_height = 0, max_size = 0;
gchar **mime_types = NULL;
gboolean needs_conversion = FALSE;
gint width, height;
max_factor = 1;
factor = 1;
- if (!can_satisfy_mime_type_requirements (mime_types, &new_format_name,
- &new_mime_type)) {
- /* FIXME: That should be reported to the user */
- DEBUG ("Mon dieu! Can't convert to any acceptable format!");
- g_strfreev (mime_types);
- return NULL;
- }
-
- /* If the avatar is not already in the right type, it needs converting. */
- if (!str_in_strv (avatar->format, mime_types)) {
- DEBUG ("Image format %s not supported by the protocol",
- avatar->format);
-
+ /* Check if we need to convert to another image format */
+ if (avatar_chooser_need_mime_type_conversion (avatar->format,
+ mime_types,
+ &new_format_name,
+ &new_mime_type)) {
+ DEBUG ("Format conversion needed, we'll use mime type '%s' "
+ "and format name '%s'. Current mime type is '%s'",
+ new_mime_type, new_format_name, avatar->format);
needs_conversion = TRUE;
}
g_strfreev (mime_types);
+ /* If there is no format we can use, report error to the user. */
+ if (new_mime_type == NULL || new_format_name == NULL) {
+ avatar_chooser_error_show (chooser, _("Couldn't convert image"),
+ _("None of the accepted image formats is "
+ "supported on your system"));
+ return NULL;
+ }
+
/* If width or height are too big, it needs converting. */
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
* try with a lower factor. */
if (max_size > 0 && avatar->len > max_size && !needs_conversion) {
DEBUG ("Image data (%"G_GSIZE_FORMAT" bytes) is too big "
- "(max is %"G_GSIZE_FORMAT" bytes), conversion needed.",
+ "(max is %u bytes), conversion needed.",
avatar->len, max_size);
factor = 0.5;
&error, NULL);
if (!saved) {
- DEBUG ("Couldn't convert image: %s", error->message);
- g_clear_error (&error);
g_free (new_format_name);
g_free (new_mime_type);
+ avatar_chooser_error_show (chooser,
+ _("Couldn't convert image"),
+ error ? error->message : NULL);
+ g_clear_error (&error);
return NULL;
}
max_factor = factor;
if (converted_image_size < max_size)
min_factor = factor;
- factor = min_factor + (max_factor - min_factor)/2;
+ factor = (min_factor + max_factor)/2;
/* We are done if either:
* - min_factor == max_factor. That happens if we resized to
EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
GtkWidget *image;
- if (priv->avatar == NULL) {
- return;
+ if (priv->avatar != NULL) {
+ empathy_avatar_unref (priv->avatar);
+ priv->avatar = NULL;
}
- empathy_avatar_unref (priv->avatar);
- priv->avatar = NULL;
-
image = gtk_image_new_from_icon_name ("stock_person", GTK_ICON_SIZE_DIALOG);
gtk_button_set_image (GTK_BUTTON (chooser), image);
g_signal_emit (chooser, signals[CHANGED], 0);
"gtk-dialog-question",
GTK_ICON_SIZE_DIALOG);
}
+
+ g_free (filename);
}
gtk_file_chooser_set_preview_widget_active (file_chooser, TRUE);
{
EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+ priv->chooser_dialog = NULL;
+
if (response == GTK_RESPONSE_CANCEL) {
goto out;
}
const gchar *default_dir = DEFAULT_DIR;
const gchar *pics_dir;
GtkFileFilter *filter;
+ EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser);
+
+ if (priv->chooser_dialog) {
+ gtk_window_present (GTK_WINDOW (priv->chooser_dialog));
+ return;
+ }
- chooser_dialog = GTK_FILE_CHOOSER (
+ priv->chooser_dialog = GTK_FILE_CHOOSER (
gtk_file_chooser_dialog_new (_("Select Your Avatar Image"),
empathy_get_toplevel_window (GTK_WIDGET (chooser)),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_OPEN,
GTK_RESPONSE_OK,
NULL));
+ chooser_dialog = priv->chooser_dialog;
+ gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser_dialog), TRUE);
/* Get special dirs */
empathy_conf_get_string (empathy_conf_get (),