Fix QSettings problems:
authorunc0rr
Sun, 02 Dec 2012 01:25:11 +0400
changeset 8178 8bd087478b48
parent 8177 1fc36c2168c4
child 8179 a1ffcb559f99
Fix QSettings problems: - Reopen file in ReadOnly mode if it was open in ReadWrite mode and is being read. This is needed for stupid QSettings which opens file in ReadWrite mode just to call readAll() on it. - Implement setSize(0)
QTfrontend/util/FileEngine.cpp
QTfrontend/util/FileEngine.h
--- a/QTfrontend/util/FileEngine.cpp	Sat Dec 01 22:15:25 2012 +0400
+++ b/QTfrontend/util/FileEngine.cpp	Sun Dec 02 01:25:11 2012 +0400
@@ -12,6 +12,7 @@
     : m_handle(NULL)
     , m_flags(0)
     , m_bufferSet(false)
+    , m_readWrite(false)
 {
     setFileName(filename);
 }
@@ -25,7 +26,13 @@
 {
     close();
 
-    if (openMode & QIODevice::WriteOnly) {
+    if ((openMode & QIODevice::ReadWrite) == QIODevice::ReadWrite) {
+        m_handle = PHYSFS_openAppend(m_fileName.toUtf8().constData());
+        m_readWrite = true;
+        seek(0);
+    }
+
+    else if (openMode & QIODevice::WriteOnly) {
         m_handle = PHYSFS_openWrite(m_fileName.toUtf8().constData());
         m_flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType;
     }
@@ -76,9 +83,22 @@
     return PHYSFS_tell(m_handle);
 }
 
+bool FileEngine::setSize(qint64 size)
+{
+    if(size == 0)
+    {
+        m_size = 0;
+        return open(QIODevice::WriteOnly);
+    }
+    else
+        return false;
+}
+
 bool FileEngine::seek(qint64 pos)
 {
-    return PHYSFS_seek(m_handle, pos) != 0;
+    bool ok = PHYSFS_seek(m_handle, pos) != 0;
+
+    return ok;
 }
 
 bool FileEngine::isSequential() const
@@ -110,7 +130,7 @@
 
 bool FileEngine::isRelativePath() const
 {
-    return true;
+    return false;
 }
 
 QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
@@ -189,7 +209,7 @@
         m_fileName = file;
 
     PHYSFS_Stat stat;
-    if (PHYSFS_stat(m_fileName.toUtf8().constData(), &stat) != 0) {
+    if (PHYSFS_stat(m_fileName.toUtf8().constData(), &stat) != 0) {        
         m_size = stat.filesize;
         m_date = QDateTime::fromTime_t(stat.modtime);
 //        _flags |= QAbstractFileEngine::WriteUserPerm;
@@ -220,7 +240,16 @@
 
 qint64 FileEngine::read(char *data, qint64 maxlen)
 {
-    return PHYSFS_readBytes(m_handle, data, maxlen);
+    if(m_readWrite)
+    {
+        if(pos() == 0)
+            open(QIODevice::ReadOnly);
+        else
+            return -1;
+    }
+
+    qint64 len = PHYSFS_readBytes(m_handle, data, maxlen);
+    return len;
 }
 
 qint64 FileEngine::readLine(char *data, qint64 maxlen)
@@ -286,7 +315,7 @@
 QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
 {
     if (filename.startsWith(scheme))
-        return new FileEngine(filename.mid(scheme.size()));
+        return new FileEngine(filename);
     else
         return NULL;
 }
--- a/QTfrontend/util/FileEngine.h	Sat Dec 01 22:15:25 2012 +0400
+++ b/QTfrontend/util/FileEngine.h	Sun Dec 02 01:25:11 2012 +0400
@@ -22,6 +22,7 @@
         virtual bool flush();
         virtual qint64 size() const;
         virtual qint64 pos() const;
+        virtual bool setSize(qint64 size);
         virtual bool seek(qint64 pos);
         virtual bool isSequential() const;
         virtual bool remove();
@@ -55,6 +56,7 @@
         QString m_fileName;
         QDateTime m_date;
         bool m_bufferSet;
+        bool m_readWrite;
 };
 
 class FileEngineHandler : public QAbstractFileEngineHandler