]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Fix layout bug. Pasting text into a cell tried to set Standard layout, because
[lyx.git] / src / support / FileName.cpp
index bd1aac9aaba22c8d1185c59cd9f8f5225005b8b6..5e1ae84b90b56b573637d6eaa6478b19a8a9e392 100644 (file)
@@ -17,6 +17,7 @@
 #include "support/debug.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
+#include "support/qstring_helpers.h"
 #include "support/os.h"
 #include "support/Package.h"
 #include "support/qstring_helpers.h"
@@ -141,6 +142,12 @@ FileName::FileName(FileName const & rhs) : d(new Private)
 }
 
 
+FileName::FileName(FileName const & rhs, string const & suffix) : d(new Private)
+{
+       set(rhs, suffix);
+}
+
+
 FileName & FileName::operator=(FileName const & rhs)
 {
        d->fi = rhs.d->fi;
@@ -172,6 +179,15 @@ void FileName::set(string const & name)
 }
 
 
+void FileName::set(FileName const & rhs, string const & 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));
+}
+
+
 void FileName::erase()
 {
        d->fi = QFileInfo();
@@ -308,9 +324,15 @@ bool FileName::isWritable() const
 
 bool FileName::isDirWritable() const
 {
-       LYXERR(Debug::FILES, "isDirWriteable: " << *this);
-       FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest");
-       return !tmpfl.empty();
+       LASSERT(d->fi.isDir(), return false);
+       QFileInfo tmp(d->fi.absoluteDir(), "lyxwritetest");
+       QTemporaryFile qt_tmp(tmp.absoluteFilePath());
+       if (qt_tmp.open()) {
+               LYXERR(Debug::FILES, "Directory " << *this << " is writable");
+               return true;
+       }
+       LYXERR(Debug::FILES, "Directory " << *this << " is not writable");
+       return false;
 }
 
 
@@ -347,23 +369,31 @@ FileNameList FileName::dirList(string const & ext) const
 }
 
 
-FileName FileName::tempName(string const & mask)
+static string createTempFile(QString const & mask)
 {
-       FileName tmp_name(mask);
-       string tmpfl;
-       if (tmp_name.d->fi.isAbsolute())
-               tmpfl = mask;
-       else
-               tmpfl = package().temp_dir().absFilename() + "/" + mask;
-
-       QTemporaryFile qt_tmp(toqstr(tmpfl));
+       QTemporaryFile qt_tmp(mask);
        if (qt_tmp.open()) {
-               tmp_name.d->fi.setFile(qt_tmp.fileName());
-               LYXERR(Debug::FILES, "Temporary file `" << tmp_name << "' created.");
-               return tmp_name;
+               string const temp_file = fromqstr(qt_tmp.fileName());
+               LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
+               return temp_file;
        }
-       LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
-       return FileName();
+       LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
+               << qt_tmp.fileTemplate());
+       return string();
+}
+
+
+FileName FileName::tempName(FileName const & temp_dir, string const & mask)
+{
+       QFileInfo tmp_fi(QDir(temp_dir.d->fi.absoluteFilePath()), toqstr(mask));
+       LYXERR(Debug::FILES, "Temporary file in " << tmp_fi.absoluteFilePath());
+       return FileName(createTempFile(tmp_fi.absoluteFilePath()));
+}
+
+
+FileName FileName::tempName(string const & mask)
+{
+       return tempName(package().temp_dir(), mask);
 }
 
 
@@ -778,13 +808,39 @@ docstring const FileName::relPath(string const & path) const
 
 bool operator==(FileName const & lhs, FileName const & rhs)
 {
-       return lhs.absFilename() == rhs.absFilename();
+       // FIXME: 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.
+
+       if (lhs.empty())
+               // QFileInfo::operator==() returns false if the two QFileInfo are empty.
+               return rhs.empty();
+
+       if (rhs.empty())
+               // Avoid unnecessary checks below.
+               return false;
+
+       lhs.d->refresh();
+       rhs.d->refresh();
+       
+       if (!lhs.d->fi.isSymLink() && !rhs.d->fi.isSymLink())
+               return lhs.d->fi == rhs.d->fi;
+
+       // FIXME: When/if QFileInfo support symlink comparison, remove this code.
+       QFileInfo fi1(lhs.d->fi);
+       if (fi1.isSymLink())
+               fi1 = QFileInfo(fi1.symLinkTarget());
+       QFileInfo fi2(rhs.d->fi);
+       if (fi2.isSymLink())
+               fi2 = QFileInfo(fi2.symLinkTarget());
+       return fi1 == fi2;
 }
 
 
 bool operator!=(FileName const & lhs, FileName const & rhs)
 {
-       return lhs.absFilename() != rhs.absFilename();
+       return !(operator==(lhs, rhs));
 }
 
 
@@ -939,7 +995,8 @@ string DocFileName::unzippedFilename() const
 
 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
 {
-       return lhs.absFilename() == rhs.absFilename()
+       return static_cast<FileName const &>(lhs)
+               == static_cast<FileName const &>(rhs)
                && lhs.saveAbsPath() == rhs.saveAbsPath();
 }