Warning, file /office/calligra/libs/store/KoDirectoryStore.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2002, 2006 David Faure <faure@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoDirectoryStore.h"
0021 #include "KoStore_p.h"
0022 
0023 #include <QFile>
0024 #include <QDir>
0025 #include <StoreDebug.h>
0026 
0027 // HMMM... I used QFile and QDir.... but maybe this should be made network transparent?
0028 
0029 KoDirectoryStore::KoDirectoryStore(const QString& path, Mode mode, bool writeMimetype)
0030     : KoStore(mode, writeMimetype)
0031     , m_basePath(path)
0032 {
0033     //debugStore << "path:" << path
0034 
0035 
0036     //debugStore << "base path:" << m_basePath;
0037 
0038     init();
0039 }
0040 
0041 KoDirectoryStore::~KoDirectoryStore()
0042 {
0043 }
0044 
0045 void KoDirectoryStore::init()
0046 {
0047     Q_D(KoStore);
0048 
0049     if (!m_basePath.endsWith('/'))
0050         m_basePath += '/';
0051     m_currentPath = m_basePath;
0052 
0053     QDir dir(m_basePath);
0054     if (dir.exists()) {
0055         d->good = true;
0056         return;
0057     }
0058     // Dir doesn't exist. If reading -> error. If writing -> create.
0059     if (d->mode == Write && dir.mkpath(m_basePath)) {
0060         debugStore << "KoDirectoryStore::init Directory created:" << m_basePath;
0061         d->good = true;
0062     }
0063 }
0064 
0065 bool KoDirectoryStore::openReadOrWrite(const QString& name, QIODevice::OpenModeFlag iomode)
0066 {
0067     Q_D(KoStore);
0068     //debugStore <<"KoDirectoryStore::openReadOrWrite m_currentPath=" << m_currentPath <<" name=" << name;
0069     int pos = name.lastIndexOf('/');
0070     if (pos != -1) { // there are subdirs in the name -> maybe need to create them, when writing
0071         pushDirectory(); // remember where we were
0072         enterAbsoluteDirectory(QString());
0073         //debugStore <<"KoDirectoryStore::openReadOrWrite entering" << name.left(pos);
0074         bool ret = enterDirectory(name.left(pos));
0075         popDirectory();
0076         if (!ret)
0077             return false;
0078     }
0079     d->stream = new QFile(m_basePath + name);
0080     if (!d->stream->open(iomode)) {
0081         delete d->stream;
0082         d->stream = 0;
0083         return false;
0084     }
0085     if (iomode == QIODevice::ReadOnly)
0086         d->size = d->stream->size();
0087     return true;
0088 }
0089 
0090 bool KoDirectoryStore::enterRelativeDirectory(const QString& dirName)
0091 {
0092     QDir origDir(m_currentPath);
0093     m_currentPath += dirName;
0094     if (!m_currentPath.endsWith('/'))
0095         m_currentPath += '/';
0096     //debugStore <<"KoDirectoryStore::enterRelativeDirectory m_currentPath now" << m_currentPath;
0097     QDir newDir(m_currentPath);
0098     if (newDir.exists())
0099         return true;
0100     // Dir doesn't exist. If reading -> error. If writing -> create.
0101     if (mode() == Write && origDir.mkdir(dirName)) {
0102         debugStore << "Created" << dirName << " under" << origDir.absolutePath();
0103         return true;
0104     }
0105     return false;
0106 }
0107 
0108 bool KoDirectoryStore::enterAbsoluteDirectory(const QString& path)
0109 {
0110     m_currentPath = m_basePath + path;
0111     //debugStore <<"KoDirectoryStore::enterAbsoluteDirectory" << m_currentPath;
0112     QDir newDir(m_currentPath);
0113     Q_ASSERT(newDir.exists());   // We've been there before, therefore it must exist.
0114     return newDir.exists();
0115 }
0116 
0117 bool KoDirectoryStore::fileExists(const QString& absPath) const
0118 {
0119     debugStore << "KoDirectoryStore::fileExists" << m_basePath + absPath;
0120     return QFile::exists(m_basePath + absPath);
0121 }