commit 61d68d05bd0f2172ab9406f23dc2640e6b9ac56b
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Sun Jul 17 20:10:53 2022 +0200

    Add proper extension as needed in Save as and Export
    
    Introduce the new FileName method ensureExtension, which does the following:
    * if the extension is already correct (in a case-insensitive way), do 
nothing.
    * if it is not correct, add the extension to the file name.
    
    This is different from changeExtension that will fail in a case where
    the file contains dots, but not a real extension, like newfile2.1.
    
    Use this new method in renameBuffer() and exportBufferAs().
    
    Fixes bug #11008.
---
 src/frontends/qt/GuiView.cpp |    8 ++++----
 src/support/FileName.cpp     |   13 +++++++++++++
 src/support/FileName.h       |    3 +++
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index 24969c2..8f6bf07 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -3080,8 +3080,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & 
newname, RenameKind kin
                dlg.setButton1(qt_("D&ocuments"), toqstr(lyxrc.document_path));
                dlg.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path));
 
-               if (!isLyXFileName(fname.absFileName()))
-                       fname.changeExtension(".lyx");
+               fname.ensureExtension(".lyx");
 
                string const path = as_template ?
                                        getTemplatesPath(b)
@@ -3099,8 +3098,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & 
newname, RenameKind kin
                if (fname.empty())
                        return false;
 
-               if (!isLyXFileName(fname.absFileName()))
-                       fname.changeExtension(".lyx");
+               fname.ensureExtension(".lyx");
        }
 
        // fname is now the new Buffer location.
@@ -3248,6 +3246,8 @@ bool GuiView::exportBufferAs(Buffer & b, docstring const 
& iformat)
        if (fmt_name.empty() || fname.empty())
                return false;
 
+       fname.ensureExtension(theFormats().extension(fmt_name));
+
        // fname is now the new Buffer location.
        if (fname.exists()) {
                docstring const file = makeDisplayPath(fname.absFileName(), 30);
diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp
index 8ad7e00..5b494a5 100644
--- a/src/support/FileName.cpp
+++ b/src/support/FileName.cpp
@@ -812,6 +812,19 @@ void FileName::changeExtension(string const & extension)
 }
 
 
+void FileName::ensureExtension(string const & extension)
+{
+       string ext;
+       // Make sure the extension starts with a dot
+       if (!extension.empty() && extension[0] != '.')
+               ext= '.' + extension;
+       else
+               ext = extension;
+       if (!suffixIs(ascii_lowercase(absFileName()), ext))
+               set(absFileName() + ext);
+}
+
+
 docstring const FileName::relPath(string const & path) const
 {
        // FIXME UNICODE
diff --git a/src/support/FileName.h b/src/support/FileName.h
index 1cf1e73..95940a9 100644
--- a/src/support/FileName.h
+++ b/src/support/FileName.h
@@ -177,6 +177,9 @@ public:
        */
        void changeExtension(std::string const & extension);
 
+       /// Add extension to the file name if it is not already there
+       void ensureExtension(std::string const & extension);
+
        static FileName fromFilesystemEncoding(std::string const & name);
 
        /// get the current working directory
-- 
lyx-cvs mailing list
[email protected]
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to