#include "support/FileName.h"
#include "support/FileNameList.h"
-#include "support/convert.h"
#include "support/debug.h"
#include "support/filetools.h"
#include "support/lassert.h"
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());
Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
}
+ /// The absolute file name in UTF-8 encoding.
+ std::string name;
///
QFileInfo fi;
};
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), /**/);
}
FileName::FileName(FileName const & rhs) : d(new Private)
{
+ d->name = rhs.d->name;
d->fi = rhs.d->fi;
}
FileName & FileName::operator=(FileName const & rhs)
{
+ if (&rhs == this)
+ return *this;
+ d->name = rhs.d->name;
d->fi = rhs.d->fi;
return *this;
}
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), /**/);
}
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();
}
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();
}
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()) {
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;
FileName FileName::getcwd()
{
- return FileName(".");
+ // return makeAbsPath("."); would create an infinite loop
+ QFileInfo fi(".");
+ return FileName(fromqstr(fi.absoluteFilePath()));
}
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;
bool FileName::createPath() const
{
- LASSERT(!empty(), /**/);
+ LASSERT(!empty(), return false);
LYXERR(Debug::FILES, "creating path '" << *this << "'.");
if (isDirectory())
return false;
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;
}
-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:
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.
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);
}
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;
}