]> 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 1b4a545f3f088e7d01974dfc737feef9862efd41..5e1ae84b90b56b573637d6eaa6478b19a8a9e392 100644 (file)
 #include "support/FileName.h"
 #include "support/FileNameList.h"
 
+#include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
-#include "support/lyxlib.h"
+#include "support/qstring_helpers.h"
 #include "support/os.h"
+#include "support/Package.h"
 #include "support/qstring_helpers.h"
 
 #include <QDateTime>
 #include <QFile>
 #include <QFileInfo>
 #include <QList>
+#include <QTemporaryFile>
 #include <QTime>
 
-#include <boost/assert.hpp>
+#include "support/lassert.h"
+#include <boost/scoped_array.hpp>
 
 #include <map>
 #include <sstream>
 # include <windows.h>
 #endif
 
-#include <fcntl.h>
 #include <cerrno>
+#include <fcntl.h>
+
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
+extern "C" int mkstemp(char *);
+#endif
+
+#if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
+# ifdef HAVE_IO_H
+#  include <io.h>
+# endif
+# ifdef HAVE_PROCESS_H
+#  include <process.h>
+# endif
+#endif
 
 using namespace std;
 
@@ -74,6 +96,19 @@ struct FileName::Private
                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
+// 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)
+               fi.refresh();
+#else
+               fi = QFileInfo(fi.absoluteFilePath());
+#endif
+       }
+       ///
        QFileInfo fi;
 };
 
@@ -107,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;
@@ -120,6 +161,12 @@ bool FileName::empty() const
 }
 
 
+bool FileName::isAbsolute() const
+{
+       return d->fi.isAbsolute();
+}
+
+
 string FileName::absFilename() const
 {
        return fromqstr(d->fi.absoluteFilePath());
@@ -129,7 +176,15 @@ string FileName::absFilename() const
 void FileName::set(string const & name)
 {
        d->fi.setFile(toqstr(name));
-       BOOST_ASSERT(d->fi.isAbsolute());
+}
+
+
+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));
 }
 
 
@@ -141,11 +196,12 @@ void FileName::erase()
 
 bool FileName::copyTo(FileName const & name) const
 {
+       LYXERR(Debug::FILES, "Copying " << name);
        QFile::remove(name.d->fi.absoluteFilePath());
        bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
        if (!success)
-               lyxerr << "FileName::copyTo(): Could not copy file "
-                       << *this << " to " << name << endl;
+               LYXERR0("FileName::copyTo(): Could not copy file "
+                       << *this << " to " << name);
        return success;
 }
 
@@ -173,11 +229,6 @@ bool FileName::moveTo(FileName const & name) const
 
 bool FileName::changePermission(unsigned long int mode) const
 {
-       if (!isWritable()) {
-               LYXERR0("File " << *this << " is not writable!");
-               return false;
-       }
-
 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
        if (::chmod(toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
                LYXERR0("File " << *this << ": cannot change permission to "
@@ -241,13 +292,21 @@ bool FileName::isReadableDirectory() const
 
 string FileName::onlyFileName() const
 {
-       return support::onlyFilename(absFilename());
+       return fromqstr(d->fi.fileName());
+}
+
+
+string FileName::onlyFileNameWithoutExt() const
+{
+       return fromqstr(d->fi.baseName());
 }
 
 
 FileName FileName::onlyPath() const
 {
-       return FileName(support::onlyPath(absFilename()));
+       FileName path;
+       path.d->fi.setFile(d->fi.path());
+       return path;
 }
 
 
@@ -265,15 +324,15 @@ bool FileName::isWritable() const
 
 bool FileName::isDirWritable() const
 {
-       LYXERR(Debug::FILES, "isDirWriteable: " << *this);
-
-       FileName const tmpfl(tempName(*this, "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;
 }
 
 
@@ -310,14 +369,52 @@ FileNameList FileName::dirList(string const & ext) const
 }
 
 
-FileName FileName::tempName(FileName const & dir, string const & mask)
+static string createTempFile(QString const & mask)
+{
+       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(FileName const & temp_dir, string const & mask)
 {
-       return support::tempName(dir, 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);
+}
+
+
+FileName FileName::getcwd()
+{
+       return FileName(".");
+}
+
+
+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
+       // been touched between the object creation and now, we refresh the file
+       // information.
+       d->refresh();
        return d->fi.lastModified().toTime_t();
 }
 
@@ -333,7 +430,7 @@ extern unsigned long sum(char const * file);
 unsigned long FileName::checksum() const
 {
        if (!exists()) {
-               LYXERR0("File \"" << absFilename() << "\" does not exist!");
+               //LYXERR0("File \"" << absFilename() << "\" does not exist!");
                return 0;
        }
        // a directory may be passed here so we need to test it. (bug 3622)
@@ -433,14 +530,14 @@ static int mymkdir(char const * pathname, unsigned long int mode)
 
 bool FileName::createDirectory(int permission) const
 {
-       BOOST_ASSERT(!empty());
+       LASSERT(!empty(), /**/);
        return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
 }
 
 
 bool FileName::createPath() const
 {
-       BOOST_ASSERT(!empty());
+       LASSERT(!empty(), /**/);
        if (isDirectory())
                return true;
 
@@ -711,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));
 }
 
 
@@ -763,8 +886,11 @@ DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
 
 void DocFileName::set(string const & name, string const & buffer_path)
 {
-       save_abs_path_ = absolutePath(name);
-       FileName::set(save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename());
+       FileName::set(name);
+       bool const nameIsAbsolute = isAbsolute();
+       save_abs_path_ = nameIsAbsolute;
+       if (!nameIsAbsolute)
+               FileName::set(makeAbsPath(name, buffer_path).absFilename());
        zipped_valid_ = false;
 }
 
@@ -869,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();
 }