]> git.lyx.org Git - features.git/commitdiff
Refactor Buffer.cpp: loadLyXFile():
authorVincent van Ravesteijn <vfr@lyx.org>
Mon, 25 Oct 2010 11:57:56 +0000 (11:57 +0000)
committerVincent van Ravesteijn <vfr@lyx.org>
Mon, 25 Oct 2010 11:57:56 +0000 (11:57 +0000)
- make a new function readFromVC(),
if the file does not exist, try to extract it from the version control (RCS),

- make a new function readEmergency(),
try to read the emergency file that is associated to the file we are trying to read,

- make a new function readAutosave(),
try to read the autosave file that is associated to the file we are trying to read,

- merge loadLyXFile() and readFileHelper(),
this new function now determines which file to read.

- add more ReadStatus elements to describe failures.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35819 a592a061-630c-0410-9148-cb99ea01b6c8

src/Buffer.cpp
src/Buffer.h

index 7321bcec5261433b6aee03d120446f78f978062c..f9c7914ec38ebbc81194c5708399504fe62ceda6 100644 (file)
@@ -869,6 +869,7 @@ bool Buffer::readFile(FileName const & filename)
        if (readFile(lex, fname) != ReadSuccess)
                return false;
 
+       d->read_only = !fname.isWritable();
        return true;
 }
 
@@ -3590,68 +3591,76 @@ vector<string> Buffer::backends() const
 }
 
 
-bool Buffer::readFileHelper(FileName const & s)
+Buffer::ReadStatus Buffer::readFromVC(FileName const & fn)
 {
-       // File information about normal file
-       if (!s.exists()) {
-               docstring const file = makeDisplayPath(s.absFileName(), 50);
-               docstring text = bformat(_("The specified document\n%1$s"
-                                                    "\ncould not be read."), file);
-               Alert::error(_("Could not read document"), text);
-               return false;
-       }
+       bool const found = LyXVC::file_not_found_hook(fn);
+       if (!found)
+               return ReadFileNotFound;
+       if (!fn.isReadableFile())
+               return ReadVCError;
+       return ReadSuccess;
+}
 
-       // Check if emergency save file exists and is newer.
-       FileName const e(s.absFileName() + ".emergency");
 
-       if (e.exists() && s.exists() && e.lastModified() > s.lastModified()) {
-               docstring const file = makeDisplayPath(s.absFileName(), 20);
+Buffer::ReadStatus Buffer::readEmergency(FileName const & fn)
+{
+       FileName const emergencyFile(fn.absFileName() + ".emergency");
+       if (emergencyFile.exists() 
+                 && emergencyFile.lastModified() > fn.lastModified()) {
+               docstring const file = makeDisplayPath(fn.absFileName(), 20);
                docstring const text =
                        bformat(_("An emergency save of the document "
                                  "%1$s exists.\n\n"
-                                              "Recover emergency save?"), file);
+                                                  "Recover emergency save?"), file);
+
                switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
-                                     _("&Recover"),  _("&Load Original"),
-                                     _("&Cancel")))
+                                         _("&Recover"),  _("&Load Original"),
+                                         _("&Cancel")))
                {
                case 0: {
                        // the file is not saved if we load the emergency file.
                        markDirty();
                        docstring str;
-                       bool res;
-
-                       if (res = readFile(e))
+                       bool const res = readFile(emergencyFile);
+                       if (res)
                                str = _("Document was successfully recovered.");
                        else
                                str = _("Document was NOT successfully recovered.");
                        str += "\n\n" + bformat(_("Remove emergency file now?\n(%1$s)"),
-                                               makeDisplayPath(e.absFileName()));
+                                               makeDisplayPath(emergencyFile.absFileName()));
 
                        if (!Alert::prompt(_("Delete emergency file?"), str, 1, 1,
-                                       _("&Remove"), _("&Keep it"))) {
-                               e.removeFile();
+                                       _("&Remove"), _("&Keep"))) {
+                               emergencyFile.removeFile();
                                if (res)
                                        Alert::warning(_("Emergency file deleted"),
                                                _("Do not forget to save your file now!"), true);
                                }
-                       return res;
+                       return res ? ReadSuccess : ReadEmergencyFailure;
                }
                case 1:
                        if (!Alert::prompt(_("Delete emergency file?"),
                                        _("Remove emergency file now?"), 1, 1,
-                                       _("&Remove"), _("&Keep it")))
-                               e.removeFile();
-                       break;
+                                       _("&Remove"), _("&Keep")))
+                               emergencyFile.removeFile();
+                       return ReadOriginal;
+
                default:
-                       return false;
+                       return ReadCancel;
                }
        }
+       return ReadFileNotFound;
+}
 
-       // Now check if autosave file is newer.
-       FileName const a(onlyPath(s.absFileName()) + '#' + onlyFileName(s.absFileName()) + '#');
 
-       if (a.exists() && s.exists() && a.lastModified() > s.lastModified()) {
-               docstring const file = makeDisplayPath(s.absFileName(), 20);
+Buffer::ReadStatus Buffer::readAutosave(FileName const & fn)
+{
+       // Now check if autosave file is newer.
+       FileName const autosaveFile(onlyPath(fn.absFileName()) 
+               + '#' + onlyFileName(fn.absFileName()) + '#');
+       if (autosaveFile.exists() 
+                 && autosaveFile.lastModified() > fn.lastModified()) {
+               docstring const file = makeDisplayPath(fn.absFileName(), 20);
                docstring const text =
                        bformat(_("The backup of the document "
                                  "%1$s is newer.\n\nLoad the "
@@ -3663,35 +3672,40 @@ bool Buffer::readFileHelper(FileName const & s)
                case 0:
                        // the file is not saved if we load the autosave file.
                        markDirty();
-                       return readFile(a);
+                       return readFile(autosaveFile) ? ReadSuccess
+                               : ReadAutosaveFailure;
                case 1:
                        // Here we delete the autosave
-                       a.removeFile();
-                       break;
+                       autosaveFile.removeFile();
+                       return ReadOriginal;
                default:
-                       return false;
+                       return ReadCancel;
                }
        }
-       return readFile(s);
+       return ReadFileNotFound;
 }
 
 
-Buffer::ReadStatus Buffer::loadLyXFile(FileName const & s)
+Buffer::ReadStatus Buffer::loadLyXFile(FileName const & fn)
 {
-       // If the file is not readable, we try to
-       // retrieve the file from version control.
-       if (!s.isReadableFile()
-                 && !LyXVC::file_not_found_hook(s))
-               return ReadFailure;
-       
-       if (s.isReadableFile()){
-               // InsetInfo needs to know if file is under VCS
-               lyxvc().file_found_hook(s);
-               if (readFileHelper(s)) {
-                       d->read_only = !s.isWritable();
-                       return ReadSuccess;
-               }
+       if (!fn.isReadableFile()) {
+               ReadStatus const ret_rvc = readFromVC(fn);
+               if (ret_rvc != ReadSuccess)
+                       return ret_rvc;
        }
+       // InsetInfo needs to know if file is under VCS
+       lyxvc().file_found_hook(fn);
+
+       ReadStatus const ret_re = readEmergency(fn);
+       if (ret_re == ReadSuccess || ret_re == ReadCancel)
+               return ret_re;
+       
+       ReadStatus const ret_ra = readAutosave(fn);
+       if (ret_ra == ReadSuccess || ret_ra == ReadCancel)
+               return ret_ra;
+
+       if (readFile(fn))
+               return ReadSuccess;
        return ReadFailure;
 }
 
index 54a719186adaf7da44af20bfa24ceaf0aa9ee050..b702a0ee4807f6e557648102c0ab743594006855 100644 (file)
@@ -97,8 +97,15 @@ public:
        enum ReadStatus {
                ReadSuccess,
                ReadCancel,
+               // failures
                ReadFailure,
-               ReadWrongVersion
+               ReadWrongVersion,
+               ReadFileNotFound,
+               ReadVCError,
+               ReadAutosaveFailure,
+               ReadEmergencyFailure,
+               // other
+               ReadOriginal
        };
 
 
@@ -186,8 +193,21 @@ public:
        /// Write file. Returns \c false if unsuccesful.
        bool writeFile(support::FileName const &) const;
 
-       /// Loads LyX file \c filename into buffer, *  and return success
+       /// Loads a LyX file \c fn into the buffer. This function
+       /// tries to extract the file from version control if it
+       /// cannot be found. If it can be found, it will try to
+       /// read an emergency save file or an autosave file.
        ReadStatus loadLyXFile(support::FileName const & s);
+       /// Try to extract the file from a version control container
+       /// before reading if the file cannot be found. This is only
+       /// implemented for RCS.
+       /// \sa LyXVC::file_not_found_hook
+       ReadStatus readFromVC(support::FileName const & fn);
+       /// Try to read an emergency file associated to \c fn. 
+       ReadStatus readEmergency(support::FileName const & fn);
+       /// Try to read an autosave file associated to \c fn.
+       ReadStatus readAutosave(support::FileName const & fn);
+
        /// Reloads the LyX file
        bool reload();
 
@@ -593,8 +613,6 @@ public:
        void checkChildBuffers();
 
 private:
-       ///
-       bool readFileHelper(support::FileName const & s);
        ///
        std::vector<std::string> backends() const;
        /** Inserts a file into a document