]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
add generic helper class for calling functions in gui thread
[lyx.git] / src / support / FileName.cpp
index cb08ba242a46e56d4b73128370b72c1406d3f786..54c6fc9fd9859c909fdb1871867176901dab0e05 100644 (file)
@@ -13,7 +13,6 @@
 #include "support/FileName.h"
 #include "support/FileNameList.h"
 
-#include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
 #include "support/lassert.h"
 #include <cerrno>
 #include <fcntl.h>
 
-#if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
-extern "C" int mkstemp(char *);
-#endif
-
-#if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
-# ifdef HAVE_IO_H
-#  include <io.h>
-# endif
-# ifdef HAVE_PROCESS_H
-#  include <process.h>
-# endif
-#endif
-
 // Three implementations of checksum(), depending on having mmap support or not.
 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
 #define SUM_WITH_MMAP
@@ -80,6 +66,7 @@ extern "C" int mkstemp(char *);
 #endif // SUM_WITH_MMAP
 
 using namespace std;
+using namespace lyx::support;
 
 // OK, this is ugly, but it is the only workaround I found to compile
 // with gcc (any version) on a system which uses a non-GNU toolchain.
@@ -105,17 +92,15 @@ struct FileName::Private
 
        Private(string const & abs_filename) : fi(toqstr(abs_filename))
        {
-               name = abs_filename;
+               name = fromqstr(fi.absoluteFilePath());
                fi.setCaching(fi.exists() ? true : false);
        }
        ///
        inline void refresh() 
        {
-// There seems to be a bug in Qt >= 4.2.0, at least, that causes problems with
+// There seems to be a bug in Qt >= 4.2.0 and < 4.5.0, that causes problems with
 // QFileInfo::refresh() on *nix. So we recreate the object in that case.
-// FIXME: When Trolltech fixes the bug, we will have to replace 0x999999 below
-// with the actual working minimum version.
-#if defined(_WIN32) || (QT_VERSION >= 0x999999)
+#if defined(_WIN32) || (QT_VERSION >= 0x040500)
                fi.refresh();
 #else
                fi = QFileInfo(fi.absoluteFilePath());
@@ -198,7 +183,7 @@ bool FileName::isAbsolute(string const & name)
 }
 
 
-string FileName::absFilename() const
+string FileName::absFileName() const
 {
        return d->name;
 }
@@ -206,14 +191,14 @@ string FileName::absFilename() const
 
 string FileName::realPath() const
 {
-       return os::real_path(toFilesystemEncoding());
+       return os::real_path(absFileName());
 }
 
 
 void FileName::set(string const & name)
 {
-       d->name = name;
        d->fi.setFile(toqstr(name));
+       d->name = fromqstr(d->fi.absoluteFilePath());
        //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
        LASSERT(empty() || isAbsolute(d->name), /**/);
 }
@@ -221,11 +206,11 @@ void FileName::set(string const & name)
 
 void FileName::set(FileName const & rhs, string const & suffix)
 {
-       d->name = rhs.d->name + suffix;
        if (!rhs.d->fi.isDir())
                d->fi.setFile(rhs.d->fi.filePath() + toqstr(suffix));
        else
                d->fi.setFile(QDir(rhs.d->fi.absoluteFilePath()), toqstr(suffix));
+       d->name = fromqstr(d->fi.absoluteFilePath());
        //LYXERR(Debug::FILES, "FileName::set(" << d->name << ')');
        LASSERT(empty() || isAbsolute(d->name), /**/);
 }
@@ -286,13 +271,22 @@ bool FileName::changePermission(unsigned long int mode) const
 
 string FileName::toFilesystemEncoding() const
 {
-       // FIXME: This doesn't work on Windows for non ascii file names with Qt < 4.4.
-       // Provided that Windows package uses Qt4.4, this isn't a problem.
+       // This doesn't work on Windows for non ascii file names.
        QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
        return string(encoded.begin(), encoded.end());
 }
 
 
+string FileName::toSafeFilesystemEncoding(os::file_access how) const
+{
+       // This will work on Windows for non ascii file names.
+       QString const safe_path =
+               toqstr(os::safe_internal_path(absFileName(), how));
+       QByteArray const encoded = QFile::encodeName(safe_path);
+       return string(encoded.begin(), encoded.end());
+}
+
+
 FileName FileName::fromFilesystemEncoding(string const & name)
 {
        QByteArray const encoded(name.c_str(), name.length());
@@ -407,7 +401,9 @@ FileNameList FileName::dirList(string const & ext) const
                return dirlist;
        }
 
-       QDir dir = d->fi.absoluteDir();
+       // If the directory is specified without a trailing '/', absoluteDir()
+       // would return the parent dir, so we must use absoluteFilePath() here.
+       QDir dir = d->fi.absoluteFilePath();
 
        if (!ext.empty()) {
                QString filter;
@@ -474,6 +470,12 @@ FileName FileName::tempPath()
 }
 
 
+void FileName::refresh() const
+{
+       d->refresh();
+}
+
+
 time_t FileName::lastModified() const
 {
        // QFileInfo caches information about the file. So, in case this file has
@@ -495,12 +497,12 @@ unsigned long FileName::checksum() const
        unsigned long result = 0;
 
        if (!exists()) {
-               //LYXERR0("File \"" << absFilename() << "\" does not exist!");
+               //LYXERR0("File \"" << absFileName() << "\" does not exist!");
                return result;
        }
        // a directory may be passed here so we need to test it. (bug 3622)
        if (isDirectory()) {
-               LYXERR0('"' << absFilename() << "\" is a directory!");
+               LYXERR0('"' << absFileName() << "\" is a directory!");
                return result;
        }
 
@@ -512,7 +514,7 @@ unsigned long FileName::checksum() const
 #if QT_VERSION >= 0x999999
        // First version of checksum uses Qt4.4 mmap support.
        // FIXME: This code is not ready with Qt4.4.2,
-       // see http://bugzilla.lyx.org/show_bug.cgi?id=5293
+       // see http://www.lyx.org/trac/ticket/5293
        // FIXME: should we check if the MapExtension extension is supported?
        // see QAbstractFileEngine::supportsExtension() and 
        // QAbstractFileEngine::MapExtension)
@@ -530,7 +532,7 @@ unsigned long FileName::checksum() const
 
 #else // QT_VERSION
 
-       string const encoded = toFilesystemEncoding();
+       string const encoded = toSafeFilesystemEncoding();
        char const * file = encoded.c_str();
 
  #ifdef SUM_WITH_MMAP
@@ -577,7 +579,7 @@ unsigned long FileName::checksum() const
  #endif // SUM_WITH_MMAP
 #endif // QT_VERSION
 
-       LYXERR(Debug::FILES, "Checksumming \"" << absFilename() << "\" "
+       LYXERR(Debug::FILES, "Checksumming \"" << absFileName() << "\" "
                << result << " lasted " << t.elapsed() << " ms.");
        return result;
 }
@@ -698,7 +700,7 @@ docstring const FileName::absoluteFilePath() const
 
 docstring FileName::displayName(int threshold) const
 {
-       return makeDisplayPath(absFilename(), threshold);
+       return makeDisplayPath(absFileName(), threshold);
 }
 
 
@@ -741,7 +743,7 @@ docstring FileName::fileContents(string const & encoding) const
 void FileName::changeExtension(string const & extension)
 {
        // FIXME: use Qt native methods...
-       string const oldname = absFilename();
+       string const oldname = absFileName();
        string::size_type const last_slash = oldname.rfind('/');
        string::size_type last_dot = oldname.rfind('.');
        if (last_dot < last_slash && last_slash != string::npos)
@@ -964,8 +966,8 @@ bool equivalent(FileName const & l, FileName const & r)
        // * Long and short file names that refer to the same file on Windows are
        //   treated as if they referred to different files.
        // This is supposed to be fixed for Qt5.
-       FileName const lhs(os::internal_path(l.absFilename()));
-       FileName const rhs(os::internal_path(r.absFilename()));
+       FileName const lhs(os::internal_path(l.absFileName()));
+       FileName const rhs(os::internal_path(r.absFileName()));
 
        if (lhs.empty())
                // QFileInfo::operator==() returns false if the two QFileInfo are empty.
@@ -999,7 +1001,10 @@ bool equivalent(FileName const & l, FileName const & r)
 
 bool operator==(FileName const & lhs, FileName const & rhs)
 {
-       return lhs.absFilename() == rhs.absFilename();
+       return os::isFilesystemCaseSensitive()
+               ? lhs.absFileName() == rhs.absFileName()
+               : !QString::compare(toqstr(lhs.absFileName()),
+                               toqstr(rhs.absFileName()), Qt::CaseInsensitive);
 }
 
 
@@ -1011,19 +1016,19 @@ bool operator!=(FileName const & lhs, FileName const & rhs)
 
 bool operator<(FileName const & lhs, FileName const & rhs)
 {
-       return lhs.absFilename() < rhs.absFilename();
+       return lhs.absFileName() < rhs.absFileName();
 }
 
 
 bool operator>(FileName const & lhs, FileName const & rhs)
 {
-       return lhs.absFilename() > rhs.absFilename();
+       return lhs.absFileName() > rhs.absFileName();
 }
 
 
 ostream & operator<<(ostream & os, FileName const & filename)
 {
-       return os << filename.absFilename();
+       return os << filename.absFileName();
 }
 
 
@@ -1055,7 +1060,7 @@ void DocFileName::set(string const & name, string const & buffer_path)
        if (save_abs_path_)
                FileName::set(name);
        else
-               FileName::set(makeAbsPath(name, buffer_path).absFilename());
+               FileName::set(makeAbsPath(name, buffer_path).absFileName());
        zipped_valid_ = false;
 }
 
@@ -1067,30 +1072,30 @@ void DocFileName::erase()
 }
 
 
-string DocFileName::relFilename(string const & path) const
+string DocFileName::relFileName(string const & path) const
 {
        // FIXME UNICODE
        return to_utf8(relPath(path));
 }
 
 
-string DocFileName::outputFilename(string const & path) const
+string DocFileName::outputFileName(string const & path) const
 {
-       return save_abs_path_ ? absFilename() : relFilename(path);
+       return save_abs_path_ ? absFileName() : relFileName(path);
 }
 
 
-string DocFileName::mangledFilename(string const & dir) const
+string DocFileName::mangledFileName(string const & dir) const
 {
        // We need to make sure that every DocFileName instance for a given
        // filename returns the same mangled name.
        typedef map<string, string> MangledMap;
        static MangledMap mangledNames;
-       MangledMap::const_iterator const it = mangledNames.find(absFilename());
+       MangledMap::const_iterator const it = mangledNames.find(absFileName());
        if (it != mangledNames.end())
                return (*it).second;
 
-       string const name = absFilename();
+       string const name = absFileName();
        // Now the real work
        string mname = os::internal_path(name);
        // Remove the extension.
@@ -1104,7 +1109,7 @@ string DocFileName::mangledFilename(string const & dir) const
        // in the name.
        static string const keep = "abcdefghijklmnopqrstuvwxyz"
                                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-                                  "+,-0123456789;=";
+                                  "+-0123456789;=";
        string::size_type pos = 0;
        while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
                mname[pos++] = '_';
@@ -1137,7 +1142,7 @@ string DocFileName::mangledFilename(string const & dir) const
                }
        }
 
-       mangledNames[absFilename()] = mname;
+       mangledNames[absFileName()] = mname;
        return mname;
 }
 
@@ -1152,9 +1157,9 @@ bool DocFileName::isZipped() const
 }
 
 
-string DocFileName::unzippedFilename() const
+string DocFileName::unzippedFileName() const
 {
-       return unzippedFileName(absFilename());
+       return support::unzippedFileName(absFileName());
 }