File indexing completed on 2024-05-12 15:59:59
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2002, 2006 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "KoDirectoryStore.h" 0008 #include "KoStore_p.h" 0009 0010 #include <QFile> 0011 #include <QDir> 0012 #include <StoreDebug.h> 0013 0014 // HMMM... I used QFile and QDir.... but maybe this should be made network transparent? 0015 0016 KoDirectoryStore::KoDirectoryStore(const QString& path, Mode mode, bool writeMimetype) 0017 : KoStore(mode, writeMimetype) 0018 , m_basePath(path) 0019 { 0020 //debugStore << "path:" << path 0021 0022 0023 //debugStore << "base path:" << m_basePath; 0024 0025 init(); 0026 } 0027 0028 KoDirectoryStore::~KoDirectoryStore() 0029 { 0030 } 0031 0032 void KoDirectoryStore::init() 0033 { 0034 Q_D(KoStore); 0035 0036 if (!m_basePath.endsWith('/')) 0037 m_basePath += '/'; 0038 m_currentPath = m_basePath; 0039 0040 QDir dir(m_basePath); 0041 if (dir.exists()) { 0042 d->good = true; 0043 return; 0044 } 0045 // Dir doesn't exist. If reading -> error. If writing -> create. 0046 if (d->mode == Write && dir.mkpath(m_basePath)) { 0047 debugStore << "KoDirectoryStore::init Directory created:" << m_basePath; 0048 d->good = true; 0049 } 0050 } 0051 0052 bool KoDirectoryStore::openReadOrWrite(const QString& name, QIODevice::OpenModeFlag iomode) 0053 { 0054 Q_D(KoStore); 0055 //debugStore <<"KoDirectoryStore::openReadOrWrite m_currentPath=" << m_currentPath <<" name=" << name; 0056 int pos = name.lastIndexOf('/'); 0057 if (pos != -1) { // there are subdirs in the name -> maybe need to create them, when writing 0058 pushDirectory(); // remember where we were 0059 enterAbsoluteDirectory(QString()); 0060 //debugStore <<"KoDirectoryStore::openReadOrWrite entering" << name.left(pos); 0061 bool ret = enterDirectory(name.left(pos)); 0062 popDirectory(); 0063 if (!ret) 0064 return false; 0065 } 0066 d->stream = new QFile(m_basePath + name); 0067 if (!d->stream->open(iomode)) { 0068 delete d->stream; 0069 d->stream = 0; 0070 return false; 0071 } 0072 if (iomode == QIODevice::ReadOnly) 0073 d->size = d->stream->size(); 0074 return true; 0075 } 0076 0077 bool KoDirectoryStore::enterRelativeDirectory(const QString& dirName) 0078 { 0079 QDir origDir(m_currentPath); 0080 m_currentPath += dirName; 0081 if (!m_currentPath.endsWith('/')) 0082 m_currentPath += '/'; 0083 //debugStore <<"KoDirectoryStore::enterRelativeDirectory m_currentPath now" << m_currentPath; 0084 QDir newDir(m_currentPath); 0085 if (newDir.exists()) 0086 return true; 0087 // Dir doesn't exist. If reading -> error. If writing -> create. 0088 if (mode() == Write && origDir.mkdir(dirName)) { 0089 debugStore << "Created" << dirName << " under" << origDir.absolutePath(); 0090 return true; 0091 } 0092 return false; 0093 } 0094 0095 bool KoDirectoryStore::enterAbsoluteDirectory(const QString& path) 0096 { 0097 m_currentPath = m_basePath + path; 0098 //debugStore <<"KoDirectoryStore::enterAbsoluteDirectory" << m_currentPath; 0099 QDir newDir(m_currentPath); 0100 Q_ASSERT(newDir.exists()); // We've been there before, therefore it must exist. 0101 return newDir.exists(); 0102 } 0103 0104 bool KoDirectoryStore::fileExists(const QString& absPath) const 0105 { 0106 debugStore << "KoDirectoryStore::fileExists" << m_basePath + absPath; 0107 return QFile::exists(m_basePath + absPath); 0108 }