1
/* This file is part of the KDE project
2
Copyright (C) 2000-2002 David Faure <faure@kde.org>
3
Copyright (C) 2010 C. Boemann <cbo@boemann.dk>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "KoTarStore.h"
22
#include "KoStore_p.h"
29
#include <StoreDebug.h>
32
#include <KoNetAccess.h>
34
KoTarStore::KoTarStore(const QString & _filename, Mode mode, const QByteArray & appIdentification,
36
: KoStore(mode, writeMimetype)
38
debugStore << "KoTarStore Constructor filename =" << _filename
39
<< " mode = " << int(mode) << endl;
42
d->localFileName = _filename;
44
m_pTar = new KTar(_filename, "application/x-gzip");
46
init(appIdentification); // open the targz file and init some vars
49
KoTarStore::KoTarStore(QIODevice *dev, Mode mode, const QByteArray & appIdentification,
51
: KoStore(mode, writeMimetype)
53
m_pTar = new KTar(dev);
55
init(appIdentification);
58
KoTarStore::KoTarStore(QWidget* window, const QUrl &_url, const QString & _filename, Mode mode,
59
const QByteArray & appIdentification, bool writeMimetype)
60
: KoStore(mode, writeMimetype)
62
debugStore << "KoTarStore Constructor url=" << _url.url(QUrl::PreferLocalFile)
63
<< " filename = " << _filename
64
<< " mode = " << int(mode) << endl;
70
if (mode == KoStore::Read) {
71
d->fileMode = KoStorePrivate::RemoteRead;
72
d->localFileName = _filename;
75
d->fileMode = KoStorePrivate::RemoteWrite;
76
d->localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
79
m_pTar = new KTar(d->localFileName, "application/x-gzip");
81
init(appIdentification); // open the targz file and init some vars
84
KoTarStore::~KoTarStore()
88
finalize(); // ### no error checking when the app forgot to call finalize itself
91
// Now we have still some job to do for remote files.
92
if (d->fileMode == KoStorePrivate::RemoteRead) {
93
KIO::NetAccess::removeTempFile(d->localFileName);
94
} else if (d->fileMode == KoStorePrivate::RemoteWrite) {
95
KIO::NetAccess::upload(d->localFileName, d->url, d->window);
96
// ### FIXME: delete temp file
100
QStringList KoTarStore::directoryList() const
103
const KArchiveDirectory *directory = m_pTar->directory();
104
foreach(const QString &name, directory->entries()) {
105
const KArchiveEntry* fileArchiveEntry = m_pTar->directory()->entry(name);
106
if (fileArchiveEntry->isDirectory()) {
113
QByteArray KoTarStore::completeMagic(const QByteArray& appMimetype)
115
debugStore << "QCString KoTarStore::completeMagic( const QCString& appMimetype )********************";
116
QByteArray res("Calligra ");
118
res += '\004'; // Two magic bytes to make the identification
119
res += '\006'; // more reliable (DF)
120
debugStore << "sssssssssssssssssssssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
121
debugStore << " return :!!!!!!!!!!!!!!! :" << res;
125
void KoTarStore::init(const QByteArray &appIdentification)
129
d->good = m_pTar->open(d->mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
134
if (d->mode == Write) {
135
debugStore << "appIdentification :" << appIdentification;
136
m_pTar->setOrigFileName(completeMagic(appIdentification));
138
d->good = m_pTar->directory() != 0;
142
bool KoTarStore::doFinalize()
144
return m_pTar->close();
147
// When reading, d->stream comes directly from KArchiveFile::device()
148
// When writing, d->stream buffers the data into m_byteArray
150
bool KoTarStore::openWrite(const QString& /*name*/)
153
// Prepare memory buffer for writing
154
m_byteArray.resize(0);
155
d->stream = new QBuffer(&m_byteArray);
156
d->stream->open(QIODevice::WriteOnly);
160
bool KoTarStore::openRead(const QString& name)
163
const KArchiveEntry * entry = m_pTar->directory()->entry(name);
167
if (entry->isDirectory()) {
168
warnStore << name << " is a directory !";
171
KArchiveFile * f = (KArchiveFile *) entry;
172
m_byteArray.resize(0);
174
d->stream = f->createDevice();
179
bool KoTarStore::closeWrite()
182
// write the whole bytearray at once into the tar file
184
debugStore << "Writing file" << d->fileName << " into TAR archive. size" << d->size;
185
m_byteArray.resize(d->size); // TODO: check if really needed
186
if (!m_pTar->writeFile(d->fileName, m_byteArray, 0100644, QLatin1String("user"), QLatin1String("group")))
187
warnStore << "Failed to write " << d->fileName;
188
m_byteArray.resize(0); // save memory
192
bool KoTarStore::enterRelativeDirectory(const QString& dirName)
195
if (d->mode == Read) {
197
m_currentDir = m_pTar->directory(); // initialize
198
Q_ASSERT(d->currentPath.isEmpty());
200
const KArchiveEntry *entry = m_currentDir->entry(dirName);
201
if (entry && entry->isDirectory()) {
202
m_currentDir = dynamic_cast<const KArchiveDirectory*>(entry);
203
return m_currentDir != 0;
206
} else // Write, no checking here
210
bool KoTarStore::enterAbsoluteDirectory(const QString& path)
213
if (path.isEmpty()) {
217
if (d->mode == Read) {
218
m_currentDir = dynamic_cast<const KArchiveDirectory*>(m_pTar->directory()->entry(path));
219
Q_ASSERT(m_currentDir);
220
return m_currentDir != 0;
225
bool KoTarStore::fileExists(const QString& absPath) const
227
return m_pTar->directory()->entry(absPath) != 0;