]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
add onoff support for "inset-modify changetype xxx" in include inset
[lyx.git] / src / support / FileName.cpp
index 737bfd9f047e39cd6f1f4f46ae1fc4dd86f6c6c2..8a5584564e745b62f5c77e57775b3dfb6d3d97c2 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"
@@ -105,16 +104,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 +127,8 @@ struct FileName::Private
                        Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
        }
 
+       /// The absolute file name in UTF-8 encoding.
+       std::string name;
        ///
        QFileInfo fi;
 };
@@ -148,6 +148,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 +161,7 @@ FileName::~FileName()
 
 FileName::FileName(FileName const & rhs) : d(new Private)
 {
+       d->name = rhs.d->name;
        d->fi = rhs.d->fi;
 }
 
@@ -171,6 +174,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 +184,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(toFilesystemEncoding());
 }
 
 
 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 +222,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();
 }
 
@@ -279,37 +299,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 +362,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 +404,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 +461,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()));
 }
 
 
@@ -556,6 +585,7 @@ unsigned long FileName::checksum() const
 bool FileName::removeFile() const
 {
        bool const success = QFile::remove(d->fi.absoluteFilePath());
+       d->refresh();
        if (!success && exists())
                LYXERR0("Could not delete file " << *this);
        return success;
@@ -646,7 +676,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;
@@ -891,6 +921,10 @@ string FileName::guessFormatFromContents() const
                        format = "fits";
        }
 
+       // Dia knows also compressed form
+       if ((format == "gzip") && (!compare_ascii_no_case(extension(), "dia")))
+               format="dia";
+
        if (!format.empty()) {
                LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
                return format;
@@ -916,7 +950,13 @@ docstring const FileName::relPath(string const & path) const
 }
 
 
-bool operator==(FileName const & l, FileName const & r)
+// Note: According to Qt, QFileInfo::operator== is undefined when
+// both files do not exist (Qt4.5 gives true for all non-existent
+// files, while Qt4.4 compares the filenames).
+// see:
+// http://www.qtsoftware.com/developer/task-tracker/
+//   index_html?id=248471&method=entry.
+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:
@@ -936,10 +976,12 @@ 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.
-               return lhs.d->fi == rhs.d->fi;
+               // see note above why the extra check with fileName is needed.
+               return lhs.d->fi == rhs.d->fi
+                       && lhs.d->fi.fileName() == rhs.d->fi.fileName();
        }
 
        // FIXME: When/if QFileInfo support symlink comparison, remove this code.
@@ -949,7 +991,17 @@ bool operator==(FileName const & l, FileName const & r)
        QFileInfo fi2(rhs.d->fi);
        if (fi2.isSymLink())
                fi2 = QFileInfo(fi2.symLinkTarget());
-       return fi1 == fi2;
+       // see note above why the extra check with fileName is needed.
+       return fi1 == fi2 && fi1.fileName() == fi2.fileName();
+}
+
+
+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);
 }
 
 
@@ -1001,10 +1053,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;
 }