]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
tiger support on mac snow leopard, include Qt4 frameworks, smart build script with...
[lyx.git] / src / support / FileName.cpp
index 65c682c2c6c978fce5819abbfc00509eb24979fa..96aa4da93feaca12e28cc7a2943ed8b377c9886e 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,16 +92,15 @@ struct FileName::Private
 
        Private(string const & abs_filename) : fi(toqstr(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());
@@ -129,6 +115,8 @@ struct FileName::Private
                        Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
        }
 
+       /// The absolute file name in UTF-8 encoding.
+       std::string name;
        ///
        QFileInfo fi;
 };
@@ -148,6 +136,8 @@ FileName::FileName() : d(new Private)
 FileName::FileName(string const & abs_filename)
        : d(abs_filename.empty() ? new Private : new Private(abs_filename))
 {
+       //LYXERR(Debug::FILES, "FileName(" << abs_filename << ')');
+       LASSERT(empty() || isAbsolute(d->name), /**/);
 }
 
 
@@ -159,6 +149,7 @@ FileName::~FileName()
 
 FileName::FileName(FileName const & rhs) : d(new Private)
 {
+       d->name = rhs.d->name;
        d->fi = rhs.d->fi;
 }
 
@@ -171,6 +162,9 @@ FileName::FileName(FileName const & rhs, string const & suffix) : d(new Private)
 
 FileName & FileName::operator=(FileName const & rhs)
 {
+       if (&rhs == this)
+               return *this;
+       d->name = rhs.d->name;
        d->fi = rhs.d->fi;
        return *this;
 }
@@ -178,25 +172,35 @@ FileName & FileName::operator=(FileName const & rhs)
 
 bool FileName::empty() const
 {
-       return d->fi.absoluteFilePath().isEmpty();
+       return d->name.empty();
 }
 
 
-bool FileName::isAbsolute() const
+bool FileName::isAbsolute(string const & name)
 {
-       return d->fi.isAbsolute();
+       QFileInfo fi(toqstr(name));
+       return fi.isAbsolute();
 }
 
 
 string FileName::absFilename() const
 {
-       return fromqstr(d->fi.absoluteFilePath());
+       return d->name;
+}
+
+
+string FileName::realPath() const
+{
+       return os::real_path(absFilename());
 }
 
 
 void FileName::set(string const & name)
 {
        d->fi.setFile(toqstr(name));
+       d->name = fromqstr(d->fi.absoluteFilePath());
+       //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
+       LASSERT(empty() || isAbsolute(d->name), /**/);
 }
 
 
@@ -206,11 +210,15 @@ void FileName::set(FileName const & rhs, string const & suffix)
                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), /**/);
 }
 
 
 void FileName::erase()
 {
+       d->name.clear();
        d->fi = QFileInfo();
 }
 
@@ -263,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());
@@ -279,37 +296,39 @@ FileName FileName::fromFilesystemEncoding(string const & name)
 
 bool FileName::exists() const
 {
-       return d->fi.exists();
+       return !empty() && d->fi.exists();
 }
 
 
 bool FileName::isSymLink() const
 {
-       return d->fi.isSymLink();
+       return !empty() && d->fi.isSymLink();
 }
 
 
 bool FileName::isFileEmpty() const
 {
+       LASSERT(!empty(), return true);
        return d->fi.size() == 0;
 }
 
 
 bool FileName::isDirectory() const
 {
-       return d->fi.isDir();
+       return !empty() && d->fi.isDir();
 }
 
 
 bool FileName::isReadOnly() const
 {
+       LASSERT(!empty(), return true);
        return d->fi.isReadable() && !d->fi.isWritable();
 }
 
 
 bool FileName::isReadableDirectory() const
 {
-       return d->fi.isDir() && d->fi.isReadable();
+       return isDirectory() && d->fi.isReadable();
 }
 
 
@@ -340,26 +359,29 @@ bool FileName::hasExtension(const string & ext)
 FileName FileName::onlyPath() const
 {
        FileName path;
+       if (empty())
+               return path;
        path.d->fi.setFile(d->fi.path());
+       path.d->name = fromqstr(path.d->fi.absoluteFilePath());
        return path;
 }
 
 
 bool FileName::isReadableFile() const
 {
-       return d->fi.isFile() && d->fi.isReadable();
+       return !empty() && d->fi.isFile() && d->fi.isReadable();
 }
 
 
 bool FileName::isWritable() const
 {
-       return d->fi.isWritable();
+       return !empty() && d->fi.isWritable();
 }
 
 
 bool FileName::isDirWritable() const
 {
-       LASSERT(d->fi.isDir(), return false);
+       LASSERT(isDirectory(), return false);
        QFileInfo tmp(QDir(d->fi.absoluteFilePath()), "lyxwritetest");
        QTemporaryFile qt_tmp(tmp.absoluteFilePath());
        if (qt_tmp.open()) {
@@ -379,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;
@@ -434,7 +458,9 @@ FileName FileName::tempName(string const & mask)
 
 FileName FileName::getcwd()
 {
-       return FileName(".");
+       // return makeAbsPath("."); would create an infinite loop
+       QFileInfo fi(".");
+       return FileName(fromqstr(fi.absoluteFilePath()));
 }
 
 
@@ -444,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
@@ -482,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)
@@ -500,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
@@ -647,7 +679,7 @@ bool FileName::createDirectory(int permission) const
 
 bool FileName::createPath() const
 {
-       LASSERT(!empty(), /**/);
+       LASSERT(!empty(), return false);
        LYXERR(Debug::FILES, "creating path '" << *this << "'.");
        if (isDirectory())
                return false;
@@ -927,7 +959,7 @@ docstring const FileName::relPath(string const & path) const
 // see:
 // http://www.qtsoftware.com/developer/task-tracker/
 //   index_html?id=248471&method=entry.
-bool operator==(FileName const & l, FileName const & r)
+bool equivalent(FileName const & l, FileName const & r)
 {
        // FIXME: In future use Qt.
        // Qt 4.4: We need to solve this warning from Qt documentation:
@@ -947,7 +979,7 @@ bool operator==(FileName const & l, FileName const & r)
 
        lhs.d->refresh();
        rhs.d->refresh();
-       
+
        if (!lhs.d->fi.isSymLink() && !rhs.d->fi.isSymLink()) {
                // Qt already checks if the filesystem is case sensitive or not.
                // see note above why the extra check with fileName is needed.
@@ -967,6 +999,15 @@ bool operator==(FileName const & l, FileName const & r)
 }
 
 
+bool operator==(FileName const & lhs, FileName const & rhs)
+{
+       return os::isFilesystemCaseSensitive()
+               ? lhs.absFilename() == rhs.absFilename()
+               : !QString::compare(toqstr(lhs.absFilename()),
+                               toqstr(rhs.absFilename()), Qt::CaseInsensitive);
+}
+
+
 bool operator!=(FileName const & lhs, FileName const & rhs)
 {
        return !(operator==(lhs, rhs));
@@ -1015,10 +1056,10 @@ DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
 
 void DocFileName::set(string const & name, string const & buffer_path)
 {
-       FileName::set(name);
-       bool const nameIsAbsolute = isAbsolute();
-       save_abs_path_ = nameIsAbsolute;
-       if (!nameIsAbsolute)
+       save_abs_path_ = isAbsolute(name);
+       if (save_abs_path_)
+               FileName::set(name);
+       else
                FileName::set(makeAbsPath(name, buffer_path).absFilename());
        zipped_valid_ = false;
 }