]> 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 b11a7865c1e13f9d33d391403cab5dc9a2ddc753..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"
@@ -26,6 +27,7 @@
 #include <QFile>
 #include <QFileInfo>
 #include <QList>
+#include <QTemporaryFile>
 #include <QTime>
 
 #include "support/lassert.h"
@@ -140,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;
@@ -171,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();
@@ -307,15 +324,15 @@ bool FileName::isWritable() const
 
 bool FileName::isDirWritable() const
 {
-       LYXERR(Debug::FILES, "isDirWriteable: " << *this);
-
-       FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest");
-
-       if (tmpfl.empty())
-               return false;
-
-       tmpfl.removeFile();
-       return true;
+       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;
 }
 
 
@@ -352,71 +369,31 @@ FileNameList FileName::dirList(string const & ext) const
 }
 
 
-static int make_tempfile(char * templ)
+static string createTempFile(QString const & mask)
 {
-#if defined(HAVE_MKSTEMP)
-       return ::mkstemp(templ);
-#elif defined(HAVE_MKTEMP)
-       // This probably just barely works...
-       ::mktemp(templ);
-# if defined (HAVE_OPEN)
-# if (!defined S_IRUSR)
-#   define S_IRUSR S_IREAD
-#   define S_IWUSR S_IWRITE
-# endif
-       return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
-# elif defined (HAVE__OPEN)
-       return ::_open(templ,
-                      _O_RDWR | _O_CREAT | _O_EXCL,
-                      _S_IREAD | _S_IWRITE);
-# else
-#  error No open() function.
-# endif
-#else
-#error FIX FIX FIX
-#endif
+       QTemporaryFile qt_tmp(mask);
+       if (qt_tmp.open()) {
+               string const temp_file = fromqstr(qt_tmp.fileName());
+               LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
+               return temp_file;
+       }
+       LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
+               << qt_tmp.fileTemplate());
+       return string();
 }
 
 
-FileName FileName::tempName(string const & mask)
+FileName FileName::tempName(FileName const & temp_dir, string const & mask)
 {
-       FileName tmp_name(mask);
-       string tmpfl;
-       if (tmp_name.d->fi.isAbsolute())
-               tmpfl = mask;
-       else
-               tmpfl = package().temp_dir().absFilename() + "/" + 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()));
+}
 
-#if defined (HAVE_GETPID)
-       tmpfl += convert<string>(getpid());
-#elif defined (HAVE__GETPID)
-       tmpfl += convert<string>(_getpid());
-#else
-# error No getpid() function
-#endif
-       tmpfl += "XXXXXX";
-
-       // The supposedly safe mkstemp version
-       // FIXME: why not using std::string directly?
-       boost::scoped_array<char> tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0'
-       tmpfl.copy(tmpl.get(), string::npos);
-       tmpl[tmpfl.length()] = '\0'; // terminator
-
-       int const tmpf = make_tempfile(tmpl.get());
-       if (tmpf != -1) {
-               string const t(to_utf8(from_filesystem8bit(tmpl.get())));
-#if defined (HAVE_CLOSE)
-               ::close(tmpf);
-#elif defined (HAVE__CLOSE)
-               ::_close(tmpf);
-#else
-# error No x() function.
-#endif
-               LYXERR(Debug::FILES, "Temporary file `" << t << "' created.");
-               return FileName(t);
-       }
-       LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
-       return FileName();
+
+FileName FileName::tempName(string const & mask)
+{
+       return tempName(package().temp_dir(), mask);
 }
 
 
@@ -426,6 +403,12 @@ FileName FileName::getcwd()
 }
 
 
+FileName FileName::tempPath()
+{
+       return FileName(fromqstr(QDir::tempPath()));
+}
+
+
 time_t FileName::lastModified() const
 {
        // QFileInfo caches information about the file. So, in case this file has
@@ -825,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));
 }
 
 
@@ -986,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();
 }