Krita Source Code Documentation
Loading...
Searching...
No Matches
KoDirectoryStore.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2002, 2006 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "KoDirectoryStore.h"
8#include "KoStore_p.h"
9
10#include <QFile>
11#include <QDir>
12#include <StoreDebug.h>
13
14// HMMM... I used QFile and QDir.... but maybe this should be made network transparent?
15
16KoDirectoryStore::KoDirectoryStore(const QString& path, Mode mode, bool writeMimetype)
17 : KoStore(mode, writeMimetype)
18 , m_basePath(path)
19{
20 //debugStore << "path:" << path
21
22
23 //debugStore << "base path:" << m_basePath;
24
25 init();
26}
27
31
33{
34 Q_D(KoStore);
35
36 if (!m_basePath.endsWith('/'))
37 m_basePath += '/';
39
40 QDir dir(m_basePath);
41 if (dir.exists()) {
42 d->good = true;
43 return;
44 }
45 // Dir doesn't exist. If reading -> error. If writing -> create.
46 if (d->mode == Write && dir.mkpath(m_basePath)) {
47 debugStore << "KoDirectoryStore::init Directory created:" << m_basePath;
48 d->good = true;
49 }
50}
51
52bool KoDirectoryStore::openReadOrWrite(const QString& name, QIODevice::OpenModeFlag iomode)
53{
54 Q_D(KoStore);
55 //debugStore <<"KoDirectoryStore::openReadOrWrite m_currentPath=" << m_currentPath <<" name=" << name;
56 int pos = name.lastIndexOf('/');
57 if (pos != -1) { // there are subdirs in the name -> maybe need to create them, when writing
58 pushDirectory(); // remember where we were
59 enterAbsoluteDirectory(QString());
60 //debugStore <<"KoDirectoryStore::openReadOrWrite entering" << name.left(pos);
61 bool ret = enterDirectory(name.left(pos));
63 if (!ret)
64 return false;
65 }
66 d->stream = new QFile(m_basePath + name);
67 if (!d->stream->open(iomode)) {
68 delete d->stream;
69 d->stream = 0;
70 return false;
71 }
72 if (iomode == QIODevice::ReadOnly)
73 d->size = d->stream->size();
74 return true;
75}
76
77bool KoDirectoryStore::enterRelativeDirectory(const QString& dirName)
78{
79 QDir origDir(m_currentPath);
80 m_currentPath += dirName;
81 if (!m_currentPath.endsWith('/'))
82 m_currentPath += '/';
83 //debugStore <<"KoDirectoryStore::enterRelativeDirectory m_currentPath now" << m_currentPath;
84 QDir newDir(m_currentPath);
85 if (newDir.exists())
86 return true;
87 // Dir doesn't exist. If reading -> error. If writing -> create.
88 if (mode() == Write && origDir.mkdir(dirName)) {
89 debugStore << "Created" << dirName << " under" << origDir.absolutePath();
90 return true;
91 }
92 return false;
93}
94
96{
98 //debugStore <<"KoDirectoryStore::enterAbsoluteDirectory" << m_currentPath;
99 QDir newDir(m_currentPath);
100 Q_ASSERT(newDir.exists()); // We've been there before, therefore it must exist.
101 return newDir.exists();
102}
103
104bool KoDirectoryStore::fileExists(const QString& absPath) const
105{
106 debugStore << "KoDirectoryStore::fileExists" << m_basePath + absPath;
107 return QFile::exists(m_basePath + absPath);
108}
#define debugStore
Definition StoreDebug.h:15
KoDirectoryStore(const QString &path, Mode _mode, bool writeMimetype)
bool openReadOrWrite(const QString &name, QIODevice::OpenModeFlag ioMode)
bool enterRelativeDirectory(const QString &dirName) override
~KoDirectoryStore() override
bool fileExists(const QString &absPath) const override
bool enterAbsoluteDirectory(const QString &path) override
@ Write
Definition KoStore.h:29
Mode mode() const
Definition KoStore.cpp:420
void pushDirectory()
Definition KoStore.cpp:294
void popDirectory()
Definition KoStore.cpp:300
virtual bool enterDirectory(const QString &directory)
Definition KoStore.cpp:253
qint64 pos() const
Definition KoStore.cpp:347