]> 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 35c0f136d746f39c16bf3e5bee4c72a8fbabf4cd..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,22 +104,31 @@ 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());
 #endif
        }
 
+
+       static
+       bool isFilesystemEqual(QString const & lhs, QString const & rhs)
+       {
+               return QString::compare(lhs, rhs, os::isFilesystemCaseSensitive() ?
+                       Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
+       }
+
+       /// The absolute file name in UTF-8 encoding.
+       std::string name;
        ///
        QFileInfo fi;
 };
@@ -140,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), /**/);
 }
 
 
@@ -151,6 +161,7 @@ FileName::~FileName()
 
 FileName::FileName(FileName const & rhs) : d(new Private)
 {
+       d->name = rhs.d->name;
        d->fi = rhs.d->fi;
 }
 
@@ -163,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;
 }
@@ -170,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), /**/);
 }
 
 
@@ -198,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();
 }
 
@@ -271,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();
 }
 
 
@@ -313,40 +343,49 @@ string FileName::onlyFileName() const
 
 string FileName::onlyFileNameWithoutExt() const
 {
-       return fromqstr(d->fi.baseName());
+       return fromqstr(d->fi.completeBaseName());
 }
 
 
 string FileName::extension() const
 {
-       return fromqstr(d->fi.suffix());
+       return fromqstr(d->fi.suffix());
+}
+
+
+bool FileName::hasExtension(const string & ext)
+{
+       return Private::isFilesystemEqual(d->fi.suffix(), toqstr(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);
-       QFileInfo tmp(d->fi.absoluteDir(), "lyxwritetest");
+       LASSERT(isDirectory(), return false);
+       QFileInfo tmp(QDir(d->fi.absoluteFilePath()), "lyxwritetest");
        QTemporaryFile qt_tmp(tmp.absoluteFilePath());
        if (qt_tmp.open()) {
                LYXERR(Debug::FILES, "Directory " << *this << " is writable");
@@ -365,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;
@@ -420,13 +461,15 @@ 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()));
 }
 
 
 FileName FileName::tempPath()
 {
-       return FileName(fromqstr(QDir::tempPath()));
+       return FileName(os::internal_path(fromqstr(QDir::tempPath())));
 }
 
 
@@ -542,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;
@@ -591,6 +635,7 @@ bool FileName::destroyDirectory() const
 }
 
 
+// Only used in non Win32 platforms
 static int mymkdir(char const * pathname, unsigned long int mode)
 {
        // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
@@ -619,16 +664,22 @@ static int mymkdir(char const * pathname, unsigned long int mode)
 
 bool FileName::createDirectory(int permission) const
 {
-       LASSERT(!empty(), /**/);
+       LASSERT(!empty(), return false);
+#ifdef Q_OS_WIN32
+       // FIXME: "Permissions of created directories are ignored on this system."
+       return createPath();
+#else
        return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
+#endif
 }
 
 
 bool FileName::createPath() const
 {
-       LASSERT(!empty(), /**/);
+       LASSERT(!empty(), return false);
+       LYXERR(Debug::FILES, "creating path '" << *this << "'.");
        if (isDirectory())
-               return true;
+               return false;
 
        QDir dir;
        bool success = dir.mkpath(d->fi.absoluteFilePath());
@@ -870,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;
@@ -895,12 +950,21 @@ docstring const FileName::relPath(string const & path) const
 }
 
 
-bool operator==(FileName const & lhs, FileName const & rhs)
+// 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: We need to solve this warning from Qt documentation:
+       // FIXME: In future use Qt.
+       // Qt 4.4: We need to solve this warning from Qt documentation:
        // * 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()));
 
        if (lhs.empty())
                // QFileInfo::operator==() returns false if the two QFileInfo are empty.
@@ -912,9 +976,13 @@ bool operator==(FileName const & lhs, FileName const & rhs)
 
        lhs.d->refresh();
        rhs.d->refresh();
-       
-       if (!lhs.d->fi.isSymLink() && !rhs.d->fi.isSymLink())
-               return lhs.d->fi == rhs.d->fi;
+
+       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.
+               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.
        QFileInfo fi1(lhs.d->fi);
@@ -923,7 +991,17 @@ bool operator==(FileName const & lhs, FileName const & rhs)
        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);
 }
 
 
@@ -975,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;
 }