23#include <KSharedConfig>
24#include <KConfigGroup>
27#define DefaultFormat KoStore::Zip
32 if (dev->read((
char *)buf, 4) < 4)
34 if (buf[0] ==
'P' && buf[1] ==
'K' && buf[2] == 3 && buf[3] == 4)
41 if (backend ==
Auto) {
45 QFileInfo inf(fileName);
50 if (file.open(QIODevice::ReadOnly))
63 warnStore <<
"Unsupported backend requested for KoStore : " << backend;
70 if (backend ==
Auto) {
74 if (
device->open(QIODevice::ReadOnly)) {
82 errorStore <<
"Can't create a Directory store for a memory buffer!" << Qt::endl;
87 warnStore <<
"Unsupported backend requested for KoStore : " << backend;
94const char ROOTPART[] =
"root";
95const char MAINNAME[] =
"maindoc.xml";
113 d->fileName = d->toExternalNaming(_name);
115 debugStore <<
"KOStore" << _name << d->fileName;
118 warnStore <<
"Store is already opened, missing close";
122 if (d->fileName.length() > 512) {
123 errorStore <<
"KoStore: Filename " << d->fileName <<
" is too long" << Qt::endl;
127 if (d->mode ==
Write) {
128 debugStore <<
"opening for writing" << d->fileName;
129 if (d->filesList.contains(d->fileName)) {
130 warnStore <<
"KoStore: Duplicate filename" << d->fileName;
134 d->filesList.append(d->fileName);
139 }
else if (d->mode ==
Read) {
140 debugStore <<
"Opening for reading" << d->fileName;
160 warnStore <<
"You must open before closing";
175 warnStore <<
"You must open before asking for a device";
177 warnStore <<
"Can not get device from store that is opened for writing";
187 warnStore <<
"You must open before reading";
190 if (d->mode !=
Read) {
191 errorStore <<
"KoStore: Can not read from store that is opened for writing" << Qt::endl;
195 return d->stream->read(max);
200 return write(data.constData(), data.size());
207 errorStore <<
"KoStore: You must open before reading" << Qt::endl;
210 if (d->mode !=
Read) {
211 errorStore <<
"KoStore: Can not read from store that is opened for writing" << Qt::endl;
215 return d->stream->read(_buffer, _len);
221 if (_len == 0)
return 0;
224 errorStore <<
"KoStore: You must open before writing" << Qt::endl;
227 if (d->mode !=
Write) {
228 errorStore <<
"KoStore: Can not write to store that is opened for reading" << Qt::endl;
232 int nwritten = d->stream->write(_data, _len);
233 Q_ASSERT(nwritten == (
int)_len);
243 warnStore <<
"You must open before asking for a size";
244 return static_cast<qint64
>(-1);
246 if (d->mode !=
Read) {
247 warnStore <<
"Can not get size from store that is opened for writing";
248 return static_cast<qint64
>(-1);
259 QString
tmp(directory);
261 while ((
pos =
tmp.indexOf(
'/')) != -1 &&
262 (success = d->enterDirectoryInternal(
tmp.left(
pos))))
265 if (success && !
tmp.isEmpty())
266 return d->enterDirectoryInternal(
tmp);
273 if (d->currentPath.isEmpty())
276 d->currentPath.pop_back();
285 QStringList::ConstIterator it = d->currentPath.begin();
286 QStringList::ConstIterator end = d->currentPath.end();
287 for (; it != end; ++it) {
303 d->currentPath.clear();
311 QBuffer buffer(&data);
312 return d->extractFile(srcName, buffer);
317 if (!
q->
open(srcName))
320 if (!buffer.open(QIODevice::WriteOnly)) {
326 data.resize(8 * 1024);
328 for (
int block = 0; (block =
q->
read(data.data(), data.size())) > 0; total += block) {
329 buffer.write(data.data(), block);
332 if (
q->
size() !=
static_cast<qint64
>(-1))
333 Q_ASSERT(total ==
q->
size());
344 return d->stream->seek(
pos);
350 return d->stream->pos();
356 return d->stream->atEnd();
362 if (_internalNaming == ROOTPART)
366 if (_internalNaming.startsWith(
"tar:/"))
367 intern = _internalNaming.mid(5);
387 return fileExists(d->toExternalNaming(fileName));
398 Q_ASSERT(!d->finalized);
410 d->substituteThis = name;
411 d->substituteWith = substitution;
QList< QString > QStringList
static KoStore::Backend determineBackend(QIODevice *dev)
bool enterDirectoryInternal(const QString &directory)
QStringList currentPath
The "current directory" (path)
bool extractFile(const QString &sourceName, QIODevice &buffer)
QString toExternalNaming(const QString &internalNaming) const
qint64 write(const QByteArray &data)
QIODevice * device() const
virtual bool closeRead()=0
virtual bool openWrite(const QString &name)=0
virtual bool doFinalize()
void setSubstitution(const QString &name, const QString &substitution)
When reading, in the paths in the store where name occurs, substitution is used.
bool extractFile(const QString &sourceName, QByteArray &data)
virtual bool closeWrite()=0
virtual bool fileExists(const QString &absPath) const =0
virtual bool enterRelativeDirectory(const QString &dirName)=0
virtual void setCompressionEnabled(bool e)
QString currentPath() const
virtual bool openRead(const QString &name)=0
virtual QStringList directoryList() const
bool seek(qint64 pos)
See QIODevice.
virtual bool enterDirectory(const QString &directory)
static KoStore * createStore(const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
virtual bool enterAbsoluteDirectory(const QString &path)=0
KoStore(Mode mode, bool writeMimetype=true)
bool hasFile(const QString &fileName) const
bool open(const QString &name)
bool hasDirectory(const QString &directoryName)
QByteArray read(qint64 max)