[goffice] Image: add auto-by-extension for format selection.



commit e998e98c6a00e63f8c2d458e7682999a88386a13
Author: Morten Welinder <terra gnome org>
Date:   Mon Mar 9 20:15:47 2020 -0400

    Image: add auto-by-extension for format selection.

 ChangeLog                 | 11 ++++++++---
 NEWS                      |  1 +
 goffice/gtk/goffice-gtk.c | 28 +++++++++++++++++++++++-----
 goffice/utils/go-file.c   | 10 +++++-----
 4 files changed, 37 insertions(+), 13 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 08ffccd1..74130b65 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2020-03-09  Morten Welinder  <terra gnome org>
+
+       * goffice/utils/go-file.c (go_url_check_extension): Make new_uri
+       optional.
+
 2020-01-11  Jean Brefort  <jean brefort normalesup org>
 
        * plugins/plot_surface/gog-contour.c (gog_contour_view_render): fix a
@@ -10,9 +15,9 @@
 
 2019-12-13  Jean Brefort  <jean brefort normalesup org>
 
-       * goffice/graph/gog-series-labels.c (gog_series_labels_update): fix warnings
-       when the vector length is nil. See #426.
-       * plugins/plot_xy/gog-xy.c (gog_xy_view_render): do not process labels 
+       * goffice/graph/gog-series-labels.c (gog_series_labels_update):
+       fix warnings when the vector length is nil. See #426.
+       * plugins/plot_xy/gog-xy.c (gog_xy_view_render): do not process labels
        if the series does not contain any valid data. Fix #426.
 
 2019-11-06  Morten Welinder <terra gnome org>
diff --git a/NEWS b/NEWS
index a38dc68d..86f3a5f0 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ Jean:
 
 Morten:
        * Fix library namespace issue.
+       * Implement auto-by-extension for image files.
 
 --------------------------------------------------------------------------
 goffice 0.10.46:
diff --git a/goffice/gtk/goffice-gtk.c b/goffice/gtk/goffice-gtk.c
index 72953596..dc6f80c5 100644
--- a/goffice/gtk/goffice-gtk.c
+++ b/goffice/gtk/goffice-gtk.c
@@ -982,7 +982,7 @@ cb_format_combo_changed (GtkComboBox *combo, GtkWidget *expander)
  * @ret_format: default file format
  * @resolution: export resolution
  *
- * Opens a file chooser and let user choose file URI and format in a list of
+ * Opens a file chooser and lets user choose file URI and format in a list of
  * supported ones.
  *
  * Returns: file URI string, file #GOImageFormat stored in @ret_format, and
@@ -1003,6 +1003,7 @@ go_gui_get_image_save_info (GtkWindow *toplevel, GSList *supported_formats,
        SaveInfoState *state;
        char const *key = "go_gui_get_image_save_info";
        char *uri = NULL;
+       gboolean by_ext = FALSE;
 
        state = g_object_get_data (G_OBJECT (toplevel), key);
        if (state == NULL) {
@@ -1028,6 +1029,10 @@ go_gui_get_image_save_info (GtkWindow *toplevel, GSList *supported_formats,
                        int i;
                        GSList *l;
                        format_combo = go_gtk_builder_combo_box_init_text (gui, "format_combo");
+
+                       by_ext = TRUE;
+                       go_gtk_combo_box_append_text (format_combo, _("Auto by extension"));
+
                        for (l = supported_formats, i = 0; l != NULL; l = l->next, i++) {
                                format = GPOINTER_TO_UINT (l->data);
                                format_info = go_image_get_format_info (format);
@@ -1083,9 +1088,23 @@ go_gui_get_image_save_info (GtkWindow *toplevel, GSList *supported_formats,
                char *new_uri = NULL;
                int index = gtk_combo_box_get_active (format_combo);
 
-               if (index >= 0) {
+               format = GO_IMAGE_FORMAT_UNKNOWN;
+               if (index < 0)
+                       ; // That's it.
+               else if (by_ext && index == 0) {
+                       GSList *l;
+
+                       for (l = supported_formats; l; l = l->next) {
+                               GOImageFormat f = GPOINTER_TO_UINT (l->data);
+                               GOImageFormatInfo const *format_info = go_image_get_format_info (f);
+                               if (go_url_check_extension (uri, format_info->ext, NULL))
+                                       format = f;
+                       }
+                       if (format == GO_IMAGE_FORMAT_UNKNOWN)
+                               goto loop;
+               } else {
                        format = GPOINTER_TO_UINT (g_slist_nth_data
-                               (supported_formats, index));
+                               (supported_formats, index - by_ext));
                        format_info = go_image_get_format_info (format);
                        if (!go_url_check_extension (uri, format_info->ext, &new_uri) &&
                            !go_gtk_query_yes_no (GTK_WINDOW (fsel), TRUE,
@@ -1099,8 +1118,7 @@ go_gui_get_image_save_info (GtkWindow *toplevel, GSList *supported_formats,
                        }
                        g_free (uri);
                        uri = new_uri;
-               } else
-                       format = GO_IMAGE_FORMAT_UNKNOWN;
+               }
                *ret_format = format;
        }
        if (!go_gtk_url_is_writeable (GTK_WINDOW (fsel), uri, TRUE)) {
diff --git a/goffice/utils/go-file.c b/goffice/utils/go-file.c
index dad3c721..384dbbe0 100644
--- a/goffice/utils/go-file.c
+++ b/goffice/utils/go-file.c
@@ -1037,7 +1037,7 @@ go_url_encode (gchar const *uri_fragment, int type)
  * go_url_check_extension:
  * @uri: Uri
  * @std_ext: Standard extension for the content type
- * @new_uri: New uri
+ * @new_uri: (optional) (nullable): New uri
  *
  * Modifies given @uri by adding the extension @std_ext if needed.
  * If no @std_ext is given or @uri already has some extension,
@@ -1046,7 +1046,7 @@ go_url_encode (gchar const *uri_fragment, int type)
  * Value in new_uri:  newly allocated string which you should free after
  *                    use, containing (optionally) modified uri.
  *
- * Return Value:  FALSE if the uri has an extension not matching @std_ext
+ * Return Value: %FALSE if the uri has an extension not matching @std_ext
  */
 gboolean
 go_url_check_extension (gchar const *uri,
@@ -1058,17 +1058,17 @@ go_url_check_extension (gchar const *uri,
        gboolean res;
 
        g_return_val_if_fail (uri != NULL, FALSE);
-       g_return_val_if_fail (new_uri != NULL, FALSE);
 
        res      = TRUE;
        base     = g_path_get_basename (uri);
        user_ext = strrchr (base, '.');
-       if (std_ext != NULL && strlen (std_ext) > 0 && user_ext == NULL)
+       if (std_ext != NULL && strlen (std_ext) > 0 && !user_ext && new_uri)
                *new_uri = g_strconcat (uri, ".", std_ext, NULL);
        else {
                if (user_ext != NULL && std_ext != NULL)
                        res = !go_utf8_collate_casefold (user_ext + 1, std_ext);
-               *new_uri = g_strdup (uri);
+               if (new_uri)
+                       *new_uri = g_strdup (uri);
        }
        g_free (base);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]