]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Fix Qt deprecation warns for QTime
[lyx.git] / src / support / FileName.cpp
index b485ae939524d989d585110ac5c5aed9d2e2bb76..179fef46ee92e2b419fd8f18e33d74b162a3472c 100644 (file)
 #include <QFileInfo>
 #include <QList>
 #include <QTemporaryFile>
-#include <QTime>
+#include <QElapsedTimer>
+
+#ifdef _WIN32
+#include <QThread>
+#endif
 
 #include <boost/crc.hpp>
 
@@ -80,7 +84,8 @@ struct FileName::Private
 {
        Private() {}
 
-       Private(string const & abs_filename) : fi(toqstr(handleTildeName(abs_filename)))
+       explicit Private(string const & abs_filename)
+               : fi(toqstr(handleTildeName(abs_filename)))
        {
                name = fromqstr(fi.absoluteFilePath());
                fi.setCaching(fi.exists() ? true : false);
@@ -254,6 +259,7 @@ bool FileName::renameTo(FileName const & name) const
 {
        LYXERR(Debug::FILES, "Renaming " << name << " as " << *this);
        bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
+       d->refresh();
        if (!success)
                LYXERR0("Could not rename file " << *this << " to " << name);
        return success;
@@ -263,10 +269,25 @@ bool FileName::renameTo(FileName const & name) const
 bool FileName::moveTo(FileName const & name) const
 {
        LYXERR(Debug::FILES, "Moving " << *this << " to " << name);
+#ifdef _WIN32
+       // there's a locking problem on Windows sometimes, so
+       // we will keep trying for five seconds, in the hope
+       // that clears.
+       name.refresh();
+       if (name.exists()) {
+               bool removed = name.removeFile();
+               int tries = 1;
+               while (!removed && tries < 6)   {
+                       QThread::sleep(1);
+                       removed = name.removeFile();
+                       tries++;
+               }
+       }
+#else
        QFile::remove(name.d->fi.absoluteFilePath());
+#endif
 
-       bool success = QFile::rename(d->fi.absoluteFilePath(),
-               name.d->fi.absoluteFilePath());
+       bool const success = renameTo(name);
        if (!success)
                LYXERR0("Could not move file " << *this << " to " << name);
        return success;
@@ -531,23 +552,23 @@ unsigned long checksum_ifstream_fallback(char const * file)
 
 unsigned long FileName::checksum() const
 {
-       unsigned long result = 0;
-
        if (!exists()) {
                //LYXERR0("File \"" << absFileName() << "\" does not exist!");
-               return result;
+               return 0;
        }
        // a directory may be passed here so we need to test it. (bug 3622)
        if (isDirectory()) {
                LYXERR0('"' << absFileName() << "\" is a directory!");
-               return result;
+               return 0;
        }
 
        // This is used in the debug output at the end of the method.
-       static QTime t;
+       static QElapsedTimer t;
        if (lyxerr.debugging(Debug::FILES))
                t.restart();
 
+       unsigned long result = 0;
+
 #if QT_VERSION >= 0x999999
        // First version of checksum uses Qt4.4 mmap support.
        // FIXME: This code is not ready with Qt4.4.2,
@@ -557,7 +578,7 @@ unsigned long FileName::checksum() const
        // QAbstractFileEngine::MapExtension)
        QFile qf(fi.filePath());
        if (!qf.open(QIODevice::ReadOnly))
-               return result;
+               return 0;
        qint64 size = fi.size();
        uchar * ubeg = qf.map(0, size);
        uchar * uend = ubeg + size;
@@ -577,7 +598,7 @@ unsigned long FileName::checksum() const
 
        int fd = open(file, O_RDONLY);
        if (!fd)
-               return result;
+               return 0;
 
        struct stat info;
        if (fstat(fd, &info)){
@@ -591,7 +612,7 @@ unsigned long FileName::checksum() const
        // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
        if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
                close(fd);
-               return result;
+               return 0;
        }
 
        char * beg = static_cast<char*>(mm);
@@ -949,10 +970,8 @@ string DocFileName::mangledFileName(string const & dir) const
                return (*it).second;
 
        string const name = absFileName();
-       // Now the real work
-       string mname = os::internal_path(name);
-       // Remove the extension.
-       mname = support::changeExtension(name, string());
+       // Now the real work. Remove the extension.
+       string mname = support::changeExtension(name, string());
        // The mangled name must be a valid LaTeX name.
        // The list of characters to keep is probably over-restrictive,
        // but it is not really a problem.