1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/* This file is part of the KDE project
Copyright (C) 2000-2002 David Faure <faure@kde.org>
Copyright (C) 2010 Casper Boemann <cbo@boemann.dk>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoTarStore.h"
#include "KoStore_p.h"
#include <QBuffer>
//Added by qt3to4:
#include <QByteArray>
#include <ktar.h>
#include <kdebug.h>
#include <kurl.h>
#include <kio/netaccess.h>
KoTarStore::KoTarStore(const QString & _filename, Mode _mode, const QByteArray & appIdentification)
{
kDebug(30002) << "KoTarStore Constructor filename =" << _filename
<< " mode = " << int(_mode) << endl;
Q_D(KoStore);
d->localFileName = _filename;
m_pTar = new KTar(_filename, "application/x-gzip");
d->good = init(_mode); // open the targz file and init some vars
kDebug(30002) << "appIdentification :" << appIdentification;
if (d->good && _mode == Write)
m_pTar->setOrigFileName(completeMagic(appIdentification));
}
KoTarStore::KoTarStore(QIODevice *dev, Mode mode, const QByteArray & appIdentification)
{
Q_D(KoStore);
m_pTar = new KTar(dev);
d->good = init(mode);
if (d->good && mode == Write)
m_pTar->setOrigFileName(completeMagic(appIdentification));
}
KoTarStore::KoTarStore(QWidget* window, const KUrl& _url, const QString & _filename, Mode _mode, const QByteArray & appIdentification)
{
kDebug(30002) << "KoTarStore Constructor url=" << _url.pathOrUrl()
<< " filename = " << _filename
<< " mode = " << int(_mode) << endl;
Q_D(KoStore);
d->url = _url;
d->window = window;
if (_mode == KoStore::Read) {
d->fileMode = KoStorePrivate::RemoteRead;
d->localFileName = _filename;
} else {
d->fileMode = KoStorePrivate::RemoteWrite;
d->localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
}
m_pTar = new KTar(d->localFileName, "application/x-gzip");
d->good = init(_mode); // open the targz file and init some vars
if (d->good && _mode == Write)
m_pTar->setOrigFileName(completeMagic(appIdentification));
}
KoTarStore::~KoTarStore()
{
Q_D(KoStore);
if (!d->finalized)
finalize(); // ### no error checking when the app forgot to call finalize itself
delete m_pTar;
// Now we have still some job to do for remote files.
if (d->fileMode == KoStorePrivate::RemoteRead) {
KIO::NetAccess::removeTempFile(d->localFileName);
} else if (d->fileMode == KoStorePrivate::RemoteWrite) {
KIO::NetAccess::upload(d->localFileName, d->url, d->window);
// ### FIXME: delete temp file
}
}
QByteArray KoTarStore::completeMagic(const QByteArray& appMimetype)
{
kDebug(30002) << "QCString KoTarStore::completeMagic( const QCString& appMimetype )********************";
QByteArray res("KOffice ");
res += appMimetype;
res += '\004'; // Two magic bytes to make the identification
res += '\006'; // more reliable (DF)
kDebug(30002) << "sssssssssssssssssssssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
kDebug(30002) << " return :!!!!!!!!!!!!!!! :" << res;
return res;
}
bool KoTarStore::init(Mode _mode)
{
KoStore::init(_mode);
m_currentDir = 0;
bool good = m_pTar->open(_mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
if (good && _mode == Read)
good = m_pTar->directory() != 0;
return good;
}
bool KoTarStore::doFinalize()
{
return m_pTar->close();
}
// When reading, d->stream comes directly from KArchiveFile::device()
// When writing, d->stream buffers the data into m_byteArray
bool KoTarStore::openWrite(const QString& /*name*/)
{
Q_D(KoStore);
// Prepare memory buffer for writing
m_byteArray.resize(0);
d->stream = new QBuffer(&m_byteArray);
d->stream->open(QIODevice::WriteOnly);
return true;
}
bool KoTarStore::openRead(const QString& name)
{
Q_D(KoStore);
const KArchiveEntry * entry = m_pTar->directory()->entry(name);
if (entry == 0) {
//kWarning(30002) << "Unknown filename " << name;
//return KIO::ERR_DOES_NOT_EXIST;
return false;
}
if (entry->isDirectory()) {
kWarning(30002) << name << " is a directory !";
//return KIO::ERR_IS_DIRECTORY;
return false;
}
KArchiveFile * f = (KArchiveFile *) entry;
m_byteArray.resize(0);
delete d->stream;
d->stream = f->createDevice();
d->size = f->size();
return true;
}
bool KoTarStore::closeWrite()
{
Q_D(KoStore);
// write the whole bytearray at once into the tar file
kDebug(30002) << "Writing file" << d->fileName << " into TAR archive. size" << d->size;
if (!m_pTar->writeFile(d->fileName , "user", "group", m_byteArray.data(), d->size))
kWarning(30002) << "Failed to write " << d->fileName;
m_byteArray.resize(0); // save memory
return true;
}
bool KoTarStore::enterRelativeDirectory(const QString& dirName)
{
Q_D(KoStore);
if (d->mode == Read) {
if (!m_currentDir) {
m_currentDir = m_pTar->directory(); // initialize
Q_ASSERT(d->currentPath.isEmpty());
}
const KArchiveEntry *entry = m_currentDir->entry(dirName);
if (entry && entry->isDirectory()) {
m_currentDir = dynamic_cast<const KArchiveDirectory*>(entry);
return m_currentDir != 0;
}
return false;
} else // Write, no checking here
return true;
}
bool KoTarStore::enterAbsoluteDirectory(const QString& path)
{
Q_D(KoStore);
if (path.isEmpty()) {
m_currentDir = 0;
return true;
}
if (d->mode == Read) {
m_currentDir = dynamic_cast<const KArchiveDirectory*>(m_pTar->directory()->entry(path));
Q_ASSERT(m_currentDir);
return m_currentDir != 0;
} else
return true;
}
bool KoTarStore::fileExists(const QString& absPath) const
{
return m_pTar->directory()->entry(absPath) != 0;
}
|