Simplify code of constructors of KoStore and its subclasses

REVIEW: 120412

Thanks boud for review
parent 4c93bd4c
......@@ -44,10 +44,9 @@ public:
class KoStoreFake : public KoStore
{
public:
KoStoreFake() {
KoStoreFake() : KoStore(KoStore::Write) {
d_ptr->stream = &m_buffer;
d_ptr->isOpen = true;
d_ptr->mode = KoStore::Write;
m_buffer.open(QIODevice::ReadWrite);
}
~KoStoreFake() {
......
......@@ -26,8 +26,9 @@
// HMMM... I used QFile and QDir.... but maybe this should be made network transparent?
KoDirectoryStore::KoDirectoryStore(const QString& path, Mode _mode, bool writeMimetype)
: m_basePath(path)
KoDirectoryStore::KoDirectoryStore(const QString& path, Mode mode, bool writeMimetype)
: KoStore(mode, writeMimetype)
, m_basePath(path)
{
Q_D(KoStore);
//kDebug(30002) << "path:" << path
......@@ -35,29 +36,31 @@ KoDirectoryStore::KoDirectoryStore(const QString& path, Mode _mode, bool writeMi
//kDebug(30002) << "base path:" << m_basePath;
d->writeMimetype = writeMimetype;
d->good = init(_mode);
init();
}
KoDirectoryStore::~KoDirectoryStore()
{
}
bool KoDirectoryStore::init(Mode _mode)
void KoDirectoryStore::init()
{
KoStore::init(_mode);
Q_D(KoStore);
if (!m_basePath.endsWith('/'))
m_basePath += '/';
m_currentPath = m_basePath;
QDir dir(m_basePath);
if (dir.exists())
return true;
if (dir.exists()) {
d->good = true;
return;
}
// Dir doesn't exist. If reading -> error. If writing -> create.
if (_mode == Write && dir.mkpath(m_basePath)) {
if (d->mode == Write && dir.mkpath(m_basePath)) {
kDebug(30002) << "KoDirectoryStore::init Directory created:" << m_basePath;
return true;
d->good = true;
}
return false;
}
bool KoDirectoryStore::openReadOrWrite(const QString& name, QIODevice::OpenModeFlag iomode)
......
......@@ -30,7 +30,7 @@ public:
KoDirectoryStore(const QString& path, Mode _mode, bool writeMimetype);
~KoDirectoryStore();
protected:
virtual bool init(Mode _mode);
void init();
virtual bool openWrite(const QString &name) {
return openReadOrWrite(name, QIODevice::WriteOnly);
}
......
......@@ -71,7 +71,12 @@ const char THUMBNAIL_FILE[] = "Thumbnails/thumbnail.png";
KoEncryptedStore::KoEncryptedStore(const QString & filename, Mode mode,
const QByteArray & appIdentification, bool writeMimetype)
: m_qcaInit(QCA::Initializer()), m_password(QCA::SecureArray()), m_filename(QString(filename)), m_manifestBuffer(QByteArray()), m_tempFile(NULL), m_bPasswordUsed(false), m_bPasswordDeclined(false), m_currentDir(NULL)
: KoStore(mode, writeMimetype)
, m_filename(filename)
, m_tempFile(0)
, m_bPasswordUsed(false)
, m_bPasswordDeclined(false)
, m_currentDir(0)
{
Q_D(KoStore);
......@@ -79,27 +84,34 @@ KoEncryptedStore::KoEncryptedStore(const QString & filename, Mode mode,
d->good = true;
d->localFileName = filename;
d->writeMimetype = writeMimetype;
init(mode, appIdentification);
init(appIdentification);
}
KoEncryptedStore::KoEncryptedStore(QIODevice *dev, Mode mode, const QByteArray & appIdentification,
bool writeMimetype)
: m_qcaInit(QCA::Initializer()), m_password(QCA::SecureArray()), m_filename(QString()), m_manifestBuffer(QByteArray()), m_tempFile(NULL), m_bPasswordUsed(false), m_bPasswordDeclined(false), m_currentDir(NULL)
: KoStore(mode, writeMimetype)
, m_tempFile(0)
, m_bPasswordUsed(false)
, m_bPasswordDeclined(false)
, m_currentDir(0)
{
Q_D(KoStore);
m_pZip = new KZip(dev);
d->good = true;
d->writeMimetype = writeMimetype;
init(mode, appIdentification);
init(appIdentification);
}
KoEncryptedStore::KoEncryptedStore(QWidget* window, const KUrl& url, const QString & filename,
Mode mode,
const QByteArray & appIdentification, bool writeMimetype)
: m_qcaInit(QCA::Initializer()), m_password(QCA::SecureArray()), m_filename(QString(url.url())), m_manifestBuffer(QByteArray()), m_tempFile(NULL), m_bPasswordUsed(false), m_bPasswordDeclined(false), m_currentDir(NULL)
: KoStore(mode, writeMimetype)
, m_filename(url.url())
, m_tempFile(0)
, m_bPasswordUsed(false)
, m_bPasswordDeclined(false)
, m_currentDir(0)
{
Q_D(KoStore);
......@@ -122,33 +134,26 @@ KoEncryptedStore::KoEncryptedStore(QWidget* window, const KUrl& url, const QStri
}
d->url = url;
d->writeMimetype = writeMimetype;
init(mode, appIdentification);
init(appIdentification);
}
bool KoEncryptedStore::init(Mode mode, const QByteArray & appIdentification)
void KoEncryptedStore::init(const QByteArray & appIdentification)
{
Q_D(KoStore);
bool checksumErrorShown = false;
bool unreadableErrorShown = false;
if (!KoStore::init(mode) || !d->good) {
// This Store is already bad
d->good = false;
return false;
}
d->mode = mode;
if (mode == Write) {
if (d->mode == Write) {
d->good = KoEncryptionChecker::isEncryptionSupported();
if (d->good) {
if (!m_pZip->open(QIODevice::WriteOnly)) {
d->good = false;
return false;
return;
}
m_pZip->setExtraField(KZip::NoExtraField);
// Write identification
if (d->writeMimetype) {
m_pZip->setCompression(KZip::NoCompression);
(void)m_pZip->writeFile("mimetype", "", "",
(void)m_pZip->writeFile(QLatin1String("mimetype"), QString(), QString(),
appIdentification.data(), appIdentification.length());
}
// FIXME: Hmm, seems to be a bug here since this is
......@@ -160,14 +165,14 @@ bool KoEncryptedStore::init(Mode mode, const QByteArray & appIdentification)
d->good = m_pZip->open(QIODevice::ReadOnly);
d->good &= m_pZip->directory() != 0;
if (!d->good) {
return false;
return;
}
// Read the manifest-file, so we can get the data we'll need to decrypt the other files in the store
const KArchiveEntry* manifestArchiveEntry = m_pZip->directory()->entry(MANIFEST_FILE);
if (!manifestArchiveEntry || !manifestArchiveEntry->isFile()) {
// No manifest file? OK, *I* won't complain
return true;
return;
}
QIODevice *dev = (static_cast< const KArchiveFile* >(manifestArchiveEntry))->createDevice();
......@@ -179,7 +184,7 @@ bool KoEncryptedStore::init(Mode mode, const QByteArray & appIdentification)
delete dev;
m_pZip->close();
d->good = false;
return false;
return;
}
KoXmlElement xmlroot = xmldoc.documentElement();
if (xmlroot.hasChildNodes()) {
......@@ -308,7 +313,6 @@ bool KoEncryptedStore::init(Mode mode, const QByteArray & appIdentification)
KMessage::message(KMessage::Error, i18n("QCA has currently no support for SHA1 or PBKDF2 using SHA1. The document can not be opened."));
}
}
return d->good;
}
bool KoEncryptedStore::doFinalize()
......@@ -433,7 +437,8 @@ bool KoEncryptedStore::doFinalize()
}
m_manifestBuffer = document.toByteArray();
m_pZip->setCompression(KZip::DeflateCompression);
if (!m_pZip->writeFile(MANIFEST_FILE, "", "", m_manifestBuffer.data(), m_manifestBuffer.size())) {
if (!m_pZip->writeFile(QLatin1String(MANIFEST_FILE), QString(), QString(),
m_manifestBuffer.constData(), m_manifestBuffer.size())) {
KMessage::message(KMessage::Error, i18n("The manifest file cannot be written. The document will remain unreadable. Please try and save the document again to prevent losing your work."));
m_pZip->close();
return false;
......
......@@ -67,9 +67,7 @@ public:
protected:
using KoStore::init;
virtual bool init(Mode mode, const QByteArray &appIdentification);
void init(const QByteArray &appIdentification);
virtual bool doFinalize();
virtual bool openWrite(const QString &name);
virtual bool openRead(const QString &name);
......
......@@ -190,21 +190,9 @@ const char* const ROOTPART = "root";
const char* const MAINNAME = "maindoc.xml";
}
KoStore::KoStore(bool writeMimetype)
: d_ptr(new KoStorePrivate(this))
{
d_ptr->writeMimetype = writeMimetype;
}
bool KoStore::init(Mode mode)
{
Q_D(KoStore);
d->isOpen = false;
d->mode = mode;
d->stream = 0;
d->finalized = false;
return true;
}
KoStore::KoStore(Mode mode, bool writeMimetype)
: d_ptr(new KoStorePrivate(this, mode, writeMimetype))
{}
KoStore::~KoStore()
{
......
......@@ -304,15 +304,9 @@ public:
* ZIP backend.
*/
virtual void setCompressionEnabled(bool e);
protected:
KoStore(bool writeMimetype = true);
/**
* Init store - called by constructor.
* @return true on success
*/
virtual bool init(Mode mode);
protected:
KoStore(Mode mode, bool writeMimetype = true);
/**
* Finalize store - called by finalize.
......
......@@ -33,16 +33,17 @@ class QWidget;
class KoStorePrivate
{
public:
explicit KoStorePrivate(KoStore *qq)
explicit KoStorePrivate(KoStore *qq, KoStore::Mode _mode, bool _writeMimetype)
: q(qq),
fileMode(Local),
window(0),
mode(_mode),
size(0),
stream(0),
isOpen(false),
good(false),
finalized(false),
writeMimetype(true)
writeMimetype(_writeMimetype)
{
}
......
......@@ -31,49 +31,43 @@
#include <kio/netaccess.h>
KoTarStore::KoTarStore(const QString & _filename, Mode _mode, const QByteArray & appIdentification,
KoTarStore::KoTarStore(const QString & _filename, Mode mode, const QByteArray & appIdentification,
bool writeMimetype)
: KoStore(mode, writeMimetype)
{
kDebug(30002) << "KoTarStore Constructor filename =" << _filename
<< " mode = " << int(_mode) << endl;
<< " mode = " << int(mode) << endl;
Q_D(KoStore);
d->localFileName = _filename;
m_pTar = new KTar(_filename, "application/x-gzip");
d->writeMimetype = writeMimetype;
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));
init(appIdentification); // open the targz file and init some vars
}
KoTarStore::KoTarStore(QIODevice *dev, Mode mode, const QByteArray & appIdentification,
bool writeMimetype)
: KoStore(mode, writeMimetype)
{
Q_D(KoStore);
m_pTar = new KTar(dev);
d->writeMimetype = writeMimetype;
d->good = init(mode);
if (d->good && mode == Write)
m_pTar->setOrigFileName(completeMagic(appIdentification));
init(appIdentification);
}
KoTarStore::KoTarStore(QWidget* window, const KUrl& _url, const QString & _filename, Mode _mode,
KoTarStore::KoTarStore(QWidget* window, const KUrl& _url, const QString & _filename, Mode mode,
const QByteArray & appIdentification, bool writeMimetype)
: KoStore(mode, writeMimetype)
{
kDebug(30002) << "KoTarStore Constructor url=" << _url.pathOrUrl()
<< " filename = " << _filename
<< " mode = " << int(_mode) << endl;
<< " mode = " << int(mode) << endl;
Q_D(KoStore);
d->url = _url;
d->window = window;
if (_mode == KoStore::Read) {
if (mode == KoStore::Read) {
d->fileMode = KoStorePrivate::RemoteRead;
d->localFileName = _filename;
......@@ -84,11 +78,7 @@ KoTarStore::KoTarStore(QWidget* window, const KUrl& _url, const QString & _filen
m_pTar = new KTar(d->localFileName, "application/x-gzip");
d->writeMimetype = writeMimetype;
d->good = init(_mode); // open the targz file and init some vars
if (d->good && _mode == Write)
m_pTar->setOrigFileName(completeMagic(appIdentification));
init(appIdentification); // open the targz file and init some vars
}
KoTarStore::~KoTarStore()
......@@ -119,15 +109,21 @@ QByteArray KoTarStore::completeMagic(const QByteArray& appMimetype)
return res;
}
bool KoTarStore::init(Mode _mode)
void KoTarStore::init(const QByteArray &appIdentification)
{
KoStore::init(_mode);
Q_D(KoStore);
m_currentDir = 0;
bool good = m_pTar->open(_mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
d->good = m_pTar->open(d->mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
if (!d->good)
return;
if (good && _mode == Read)
good = m_pTar->directory() != 0;
return good;
if (d->mode == Write) {
kDebug(30002) << "appIdentification :" << appIdentification;
m_pTar->setOrigFileName(completeMagic(appIdentification));
} else {
d->good = m_pTar->directory() != 0;
}
}
bool KoTarStore::doFinalize()
......
......@@ -43,7 +43,7 @@ public:
const QByteArray & appIdentification, bool writeMimetype = true);
~KoTarStore();
protected:
virtual bool init(Mode _mode);
void init(const QByteArray &appIdentification);
virtual bool doFinalize();
virtual bool openWrite(const QString& name);
virtual bool openRead(const QString& name);
......
......@@ -30,11 +30,12 @@
#include <kurl.h>
#include <kio/netaccess.h>
KoZipStore::KoZipStore(const QString & _filename, Mode _mode, const QByteArray & appIdentification,
KoZipStore::KoZipStore(const QString & _filename, Mode mode, const QByteArray & appIdentification,
bool writeMimetype)
: KoStore(mode, writeMimetype)
{
kDebug(30002) << "KoZipStore Constructor filename =" << _filename
<< " mode = " << int(_mode)
<< " mode = " << int(mode)
<< " mimetype = " << appIdentification << endl;
Q_D(KoStore);
......@@ -42,43 +43,41 @@ KoZipStore::KoZipStore(const QString & _filename, Mode _mode, const QByteArray &
m_pZip = new KZip(_filename);
d->writeMimetype = writeMimetype;
d->good = init(_mode, appIdentification); // open the zip file and init some vars
init(appIdentification); // open the zip file and init some vars
}
KoZipStore::KoZipStore(QIODevice *dev, Mode mode, const QByteArray & appIdentification,
bool writeMimetype)
: KoStore(mode, writeMimetype)
{
Q_D(KoStore);
m_pZip = new KZip(dev);
d->writeMimetype = writeMimetype;
d->good = init(mode, appIdentification);
init(appIdentification);
}
KoZipStore::KoZipStore(QWidget* window, const KUrl & _url, const QString & _filename, Mode _mode,
KoZipStore::KoZipStore(QWidget* window, const KUrl & _url, const QString & _filename, Mode mode,
const QByteArray & appIdentification, bool writeMimetype)
: KoStore(mode, writeMimetype)
{
kDebug(30002) << "KoZipStore Constructor url" << _url.pathOrUrl()
<< " filename = " << _filename
<< " mode = " << int(_mode)
<< " mode = " << int(mode)
<< " mimetype = " << appIdentification << endl;
Q_D(KoStore);
d->url = _url;
d->window = window;
if (_mode == KoStore::Read) {
if (mode == KoStore::Read) {
d->fileMode = KoStorePrivate::RemoteRead;
d->localFileName = _filename;
} else {
d->fileMode = KoStorePrivate::RemoteWrite;
d->localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
d->localFileName = QLatin1String("/tmp/kozip"); // ### FIXME with KTempFile
}
m_pZip = new KZip(d->localFileName);
d->writeMimetype = writeMimetype;
d->good = init(_mode, appIdentification); // open the zip file and init some vars
init(appIdentification); // open the zip file and init some vars
}
KoZipStore::~KoZipStore()
......@@ -98,17 +97,17 @@ KoZipStore::~KoZipStore()
}
}
bool KoZipStore::init(Mode _mode, const QByteArray& appIdentification)
void KoZipStore::init(const QByteArray& appIdentification)
{
Q_D(KoStore);
KoStore::init(_mode);
m_currentDir = 0;
bool good = m_pZip->open(_mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
d->good = m_pZip->open(d->mode == Write ? QIODevice::WriteOnly : QIODevice::ReadOnly);
if (!d->good)
return;
if (good && _mode == Read)
good = m_pZip->directory() != 0;
else if (good && _mode == Write) {
if (d->mode == Write) {
//kDebug(30002) <<"KoZipStore::init writing mimetype" << appIdentification;
m_pZip->setCompression(KZip::NoCompression);
......@@ -116,14 +115,15 @@ bool KoZipStore::init(Mode _mode, const QByteArray& appIdentification)
// Write identification
if (d->writeMimetype) {
(void)m_pZip->writeFile("mimetype", "", "",
(void)m_pZip->writeFile(QLatin1String("mimetype"), QString(), QString(),
appIdentification.data() , appIdentification.length());
}
m_pZip->setCompression(KZip::DeflateCompression);
// We don't need the extra field in Calligra - so we leave it as "no extra field".
} else {
d->good = m_pZip->directory() != 0;
}
return good;
}
void KoZipStore::setCompressionEnabled(bool e)
......
......@@ -44,9 +44,7 @@ public:
virtual void setCompressionEnabled(bool e);
virtual qint64 write(const char* _data, qint64 _len);
protected:
using KoStore::init;
virtual bool init(Mode _mode, const QByteArray& appIdentification);
void init(const QByteArray& appIdentification);
virtual bool doFinalize();
virtual bool openWrite(const QString& name);
virtual bool openRead(const QString& name);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment