]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Introduce FileName::changePermission() and fix ConverterCache.
[lyx.git] / src / support / FileName.cpp
index 2c8fa57b1e0fec6e986449ce3ddfc11d16d55b0f..63f13e23250247aa8a19a5edb0000946d4507d3c 100644 (file)
@@ -25,6 +25,7 @@
 #include <QFile>
 #include <QFileInfo>
 #include <QList>
+#include <QTime>
 
 #include <boost/assert.hpp>
 
 #include <cerrno>
 #include <fcntl.h>
 
-
-using std::map;
-using std::string;
-using std::ifstream;
-using std::ostringstream;
-using std::endl;
+using namespace std;
 
 namespace lyx {
 namespace support {
@@ -64,7 +60,9 @@ struct FileName::Private
        Private() {}
 
        Private(string const & abs_filename) : fi(toqstr(abs_filename))
-       {}
+       {
+               fi.setCaching(fi.exists() ? true : false);
+       }
        ///
        QFileInfo fi;
 };
@@ -143,6 +141,30 @@ bool FileName::copyTo(FileName const & name, bool overwrite) const
 }
 
 
+bool FileName::renameTo(FileName const & name) const
+{
+       bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
+       if (!success)
+               LYXERR0("Could not rename file " << *this << " to " << name);
+       return success;
+}
+
+
+bool FileName::changePermission(unsigned long int mode) const
+{
+       if (!fname.isWritable()) {
+               LYXERR0("File " << *this << " is not writable!");
+               return false;
+       }
+
+       if (!chmod(fname, mode)) {
+               LYXERR0("File " << *this << " cannot be changed to " << mode << " mode!");
+               return false;
+       }
+       return true;
+}
+
+
 string FileName::toFilesystemEncoding() const
 {
        QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
@@ -193,7 +215,7 @@ bool FileName::isReadableDirectory() const
 }
 
 
-std::string FileName::onlyFileName() const
+string FileName::onlyFileName() const
 {
        return support::onlyFilename(absFilename());
 }
@@ -231,7 +253,7 @@ bool FileName::isDirWritable() const
 }
 
 
-FileNameList FileName::dirList(std::string const & ext) const
+FileNameList FileName::dirList(string const & ext) const
 {
        FileNameList dirlist;
        if (!isDirectory()) {
@@ -264,13 +286,13 @@ FileNameList FileName::dirList(std::string const & ext) const
 }
 
 
-FileName FileName::tempName(FileName const & dir, std::string const & mask)
+FileName FileName::tempName(FileName const & dir, string const & mask)
 {
        return support::tempName(dir, mask);
 }
 
 
-std::time_t FileName::lastModified() const
+time_t FileName::lastModified() const
 {
        return d->fi.lastModified().toTime_t();
 }
@@ -292,11 +314,18 @@ unsigned long FileName::checksum() const
        }
        // a directory may be passed here so we need to test it. (bug 3622)
        if (isDirectory()) {
-               LYXERR0('\\' << absFilename() << "\" is a directory!");
+               LYXERR0('"' << absFilename() << "\" is a directory!");
                return 0;
        }
-       LYXERR0("Checksumming \"" << absFilename() << "\".");
-       return sum(absFilename().c_str());
+       if (!lyxerr.debugging(Debug::FILES))
+               return sum(absFilename().c_str());
+
+       QTime t;
+       t.start();
+       unsigned long r = sum(absFilename().c_str());
+       lyxerr << "Checksumming \"" << absFilename() << "\" lasted "
+               << t.elapsed() << " ms." << endl;
+       return r;
 }
 
 
@@ -304,8 +333,7 @@ bool FileName::removeFile() const
 {
        bool const success = QFile::remove(d->fi.absoluteFilePath());
        if (!success && exists())
-               lyxerr << "FileName::removeFile(): Could not delete file "
-                       << *this << "." << endl;
+               LYXERR0("Could not delete file " << *this);
        return success;
 }
 
@@ -314,32 +342,32 @@ static bool rmdir(QFileInfo const & fi)
 {
        QDir dir(fi.absoluteFilePath());
        QFileInfoList list = dir.entryInfoList();
-       bool global_success = true;
+       bool success = true;
        for (int i = 0; i != list.size(); ++i) {
                if (list.at(i).fileName() == ".")
                        continue;
                if (list.at(i).fileName() == "..")
                        continue;
-               bool success;
+               bool removed;
                if (list.at(i).isDir()) {
-                       LYXERR(Debug::FILES, "Erasing dir " 
+                       LYXERR(Debug::FILES, "Removing dir " 
                                << fromqstr(list.at(i).absoluteFilePath()));
-                       success = rmdir(list.at(i));
+                       removed = rmdir(list.at(i));
                }
                else {
-                       LYXERR(Debug::FILES, "Erasing file " 
+                       LYXERR(Debug::FILES, "Removing file " 
                                << fromqstr(list.at(i).absoluteFilePath()));
-                       success = dir.remove(list.at(i).fileName());
+                       removed = dir.remove(list.at(i).fileName());
                }
-               if (!success) {
-                       global_success = false;
-                       lyxerr << "Could not delete "
-                               << fromqstr(list.at(i).absoluteFilePath()) << "." << endl;
+               if (!removed) {
+                       success = false;
+                       LYXERR0("Could not delete "
+                               << fromqstr(list.at(i).absoluteFilePath()));
                }
        } 
        QDir parent = fi.absolutePath();
-       global_success |= parent.rmdir(fi.fileName());
-       return global_success;
+       success &= parent.rmdir(fi.fileName());
+       return success;
 }
 
 
@@ -347,7 +375,7 @@ bool FileName::destroyDirectory() const
 {
        bool const success = rmdir(d->fi);
        if (!success)
-               lyxerr << "Could not delete " << *this << "." << endl;
+               LYXERR0("Could not delete " << *this);
 
        return success;
 }
@@ -375,8 +403,7 @@ docstring FileName::displayName(int threshold) const
 docstring FileName::fileContents(string const & encoding) const
 {
        if (!isReadableFile()) {
-               LYXERR0("File '" << *this
-                       << "' is not redable!");
+               LYXERR0("File '" << *this << "' is not redable!");
                return docstring();
        }
 
@@ -409,7 +436,7 @@ docstring FileName::fileContents(string const & encoding) const
 }
 
 
-void FileName::changeExtension(std::string const & extension)
+void FileName::changeExtension(string const & extension)
 {
        // FIXME: use Qt native methods...
        string const oldname = absFilename();
@@ -457,8 +484,8 @@ string FileName::guessFormatFromContents() const
        // GZIP \037\213        http://www.ietf.org/rfc/rfc1952.txt
        // ZIP  PK...                   http://www.halyava.ru/document/ind_arch.htm
        // Z    \037\235                UNIX compress
-       // paranoia check
 
+       // paranoia check
        if (empty() || !isReadableFile())
                return string();
 
@@ -642,7 +669,7 @@ bool operator>(FileName const & lhs, FileName const & rhs)
 }
 
 
-std::ostream & operator<<(std::ostream & os, FileName const & filename)
+ostream & operator<<(ostream & os, FileName const & filename)
 {
        return os << filename.absFilename();
 }
@@ -698,7 +725,7 @@ string DocFileName::outputFilename(string const & path) const
 }
 
 
-string DocFileName::mangledFilename(std::string const & dir) const
+string DocFileName::mangledFilename(string const & dir) const
 {
        // We need to make sure that every DocFileName instance for a given
        // filename returns the same mangled name.
@@ -732,7 +759,7 @@ string DocFileName::mangledFilename(std::string const & dir) const
        // Prepend a counter to the filename. This is necessary to make
        // the mangled name unique.
        static int counter = 0;
-       std::ostringstream s;
+       ostringstream s;
        s << counter++ << mname;
        mname = s.str();
 
@@ -742,7 +769,7 @@ string DocFileName::mangledFilename(std::string const & dir) const
        // If dir.size() > max length, all bets are off for YAP. We truncate
        // the filename nevertheless, keeping a minimum of 10 chars.
 
-       string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
+       string::size_type max_length = max(100 - ((int)dir.size() + 1), 10);
 
        // If the mangled file name is too long, hack it to fit.
        // We know we're guaranteed to have a unique file name because