[gimp: 20/27] app: Support default types for save and export



commit d8f3cd1b2603a59987443ea4692e81ad956759ad
Author: Martin Nordholts <martinn src gnome org>
Date:   Tue May 5 03:00:53 2009 +0200

    app: Support default types for save and export
---
 app/file/file-utils.c        |   55 ++++++++++++++++++++++++++++++++++++++---
 app/file/file-utils.h        |    2 +
 app/widgets/gimpfiledialog.c |   40 +++++++++++++++++++++++++++++-
 3 files changed, 92 insertions(+), 5 deletions(-)

diff --git a/app/file/file-utils.c b/app/file/file-utils.c
index 3df0484..cc06182 100644
--- a/app/file/file-utils.c
+++ b/app/file/file-utils.c
@@ -43,10 +43,12 @@
 #include "gimp-intl.h"
 
 
-static gchar * file_utils_unescape_uri (const gchar  *escaped,
-                                        gint          len,
-                                        const gchar  *illegal_escaped_characters,
-                                        gboolean      ascii_must_not_be_escaped);
+static gchar *      file_utils_unescape_uri  (const gchar *escaped,
+                                              gint         len,
+                                              const gchar *illegal_escaped_characters,
+                                              gboolean     ascii_must_not_be_escaped);
+static const gchar *file_utils_get_ext_start (const gchar *uri);
+
 
 
 gboolean
@@ -232,6 +234,51 @@ file_utils_filename_from_uri (const gchar *uri)
 }
 
 gchar *
+file_utils_uri_with_new_ext (const gchar *uri,
+                             const gchar *ext_uri)
+{
+  const gchar *uri_ext      = file_utils_get_ext_start (uri);
+  const gchar *ext_uri_ext  = file_utils_get_ext_start (ext_uri);
+  gchar *uri_without_ext    = g_strndup (uri, uri_ext - uri);
+  gchar *ret                = g_strconcat (uri_without_ext, ext_uri_ext, NULL);
+  g_free (uri_without_ext);
+  return ret;
+}
+
+
+/**
+ * file_utils_get_ext_start:
+ * @uri:
+ *
+ * Returns the position of the extension (after the .) for an URI. If
+ * there is no extension the returned position is right after the
+ * string, at the terminating NULL character.
+ *
+ * Returns:
+ **/
+static const gchar *
+file_utils_get_ext_start (const gchar *uri)
+{
+  const gchar *ext        = NULL;
+  int          uri_len    = strlen (uri);
+  int          search_len = 0;
+
+  if (g_strrstr (uri, ".gz"))
+    search_len = uri_len - 3;
+  else if (g_strrstr (uri, ".bz2"))
+    search_len = uri_len - 4;
+  else
+    search_len = uri_len;
+
+  ext = g_strrstr_len (uri, search_len, ".");
+
+  if (! ext)
+    ext = uri + uri_len;
+
+  return ext;
+}
+
+gchar *
 file_utils_uri_to_utf8_filename (const gchar *uri)
 {
   g_return_val_if_fail (uri != NULL, NULL);
diff --git a/app/file/file-utils.h b/app/file/file-utils.h
index a07dfe7..3ecd6f8 100644
--- a/app/file/file-utils.h
+++ b/app/file/file-utils.h
@@ -33,6 +33,8 @@ gchar     * file_utils_any_to_uri           (Gimp          *gimp,
                                              const gchar   *filename_or_uri,
                                              GError       **error);
 gchar     * file_utils_filename_from_uri    (const gchar   *uri);
+gchar     * file_utils_uri_with_new_ext     (const gchar   *uri,
+                                             const gchar   *uri_with_ext);
 
 gchar     * file_utils_uri_to_utf8_filename (const gchar   *uri);
 
diff --git a/app/widgets/gimpfiledialog.c b/app/widgets/gimpfiledialog.c
index 5b72e09..0334850 100644
--- a/app/widgets/gimpfiledialog.c
+++ b/app/widgets/gimpfiledialog.c
@@ -470,6 +470,7 @@ gimp_file_dialog_set_save_image (GimpFileDialog *dialog,
 {
   const gchar *dir_uri  = NULL;
   const gchar *name_uri = NULL;
+  const gchar *ext_uri  = NULL;
   gchar       *docs_uri = NULL;
   gchar       *dirname  = NULL;
   gchar       *basename = NULL;
@@ -537,6 +538,17 @@ gimp_file_dialog_set_save_image (GimpFileDialog *dialog,
 
       if (! name_uri)
         name_uri = gimp_image_get_uri (image); /* Untitled */
+
+
+      /* Priority of default type/extension for Save:
+       *
+       *   1. Type of last Save
+       *   2. .xcf (which we don't explicitly append)
+       */
+      ext_uri = gimp_object_get_name (GIMP_OBJECT (image));
+
+      if (! ext_uri)
+        ext_uri = "file:///we/only/care/about/extension.xcf";
     }
   else /* if (export) */
     {
@@ -593,10 +605,36 @@ gimp_file_dialog_set_save_image (GimpFileDialog *dialog,
 
       if (! name_uri)
         name_uri = gimp_image_get_uri (image); /* Untitled */
+
+
+      /* Priority of default type/extension for Export:
+       *
+       *   1. Type of last Export
+       *   2. Type of import source
+       *   3. Type of latest Export of any document
+       *   2. .png
+       */
+      ext_uri = g_object_get_data (G_OBJECT (image), GIMP_FILE_EXPORT_URI_KEY);
+
+      if (! ext_uri)
+        ext_uri = g_object_get_data (G_OBJECT (gimp), GIMP_FILE_EXPORT_LAST_URI_KEY);
+
+      if (! ext_uri)
+        ext_uri = "file:///we/only/care/about/extension.png";
     }
 
   dirname = gimp_file_dialog_get_dirname_from_uri (dir_uri);
-  basename = file_utils_uri_display_basename (name_uri);
+  if (ext_uri)
+    {
+      gchar *uri_new_ext = file_utils_uri_with_new_ext (name_uri,
+                                                        ext_uri);
+      basename = file_utils_uri_display_basename (uri_new_ext);
+      g_free (uri_new_ext);
+    }
+  else
+    {
+      basename = file_utils_uri_display_basename (name_uri);
+    }
 
   gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog), dirname);
   gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), basename);



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