Payloads are now QSharedPointer<QIODevice> instead of QIODevice*

This commit is contained in:
Albert Vaca 2013-09-24 14:13:02 +02:00
parent a53ec2a9ea
commit 4bd5d2e427
9 changed files with 40 additions and 60 deletions

View file

@ -22,9 +22,10 @@
DownloadJob::DownloadJob(QHostAddress address, QVariantMap transferInfo): KJob() DownloadJob::DownloadJob(QHostAddress address, QVariantMap transferInfo): KJob()
{ {
mAddress = address;
mPort = transferInfo["port"].toInt(); mPort = transferInfo["port"].toInt();
mSocket = new QTcpSocket(); mSocket = new QTcpSocket();
mAddress = address; mOutput = QSharedPointer<QIODevice>(mSocket);
} }
void DownloadJob::start() void DownloadJob::start()
@ -40,8 +41,8 @@ void DownloadJob::disconnected()
emitResult(); emitResult();
} }
QIODevice* DownloadJob::getPayload() QSharedPointer<QIODevice> DownloadJob::getPayload()
{ {
qDebug() << "getPayload"; qDebug() << "getPayload";
return mSocket; return mOutput;
} }

View file

@ -27,6 +27,7 @@
#include <QVariantMap> #include <QVariantMap>
#include <QHostAddress> #include <QHostAddress>
#include <QTcpSocket> #include <QTcpSocket>
#include <QSharedPointer>
class DownloadJob class DownloadJob
: public KJob : public KJob
@ -35,13 +36,14 @@ class DownloadJob
public: public:
DownloadJob(QHostAddress address, QVariantMap transferInfo); DownloadJob(QHostAddress address, QVariantMap transferInfo);
virtual void start(); virtual void start();
QIODevice* getPayload(); QSharedPointer<QIODevice> getPayload();
private: private:
QIODevice* mOutput;
QTcpSocket* mSocket;
QHostAddress mAddress; QHostAddress mAddress;
qint16 mPort; qint16 mPort;
QTcpSocket* mSocket;
QSharedPointer<QIODevice> mOutput;
private Q_SLOTS: private Q_SLOTS:
void disconnected(); void disconnected();

View file

@ -23,7 +23,7 @@
#include "uploadjob.h" #include "uploadjob.h"
UploadJob::UploadJob(QIODevice* source): KJob() UploadJob::UploadJob(const QSharedPointer<QIODevice>& source): KJob()
{ {
mInput = source; mInput = source;
mServer = new QTcpServer(this); mServer = new QTcpServer(this);
@ -51,8 +51,8 @@ void UploadJob::newConnection()
mSocket = mServer->nextPendingConnection(); mSocket = mServer->nextPendingConnection();
connect(mInput, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(mInput.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(mInput, SIGNAL(aboutToClose()), this, SLOT(aboutToClose())); connect(mInput.data(), SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
if (!mInput->open(QIODevice::ReadOnly)) { if (!mInput->open(QIODevice::ReadOnly)) {
return; //TODO: Handle error, clean up... return; //TODO: Handle error, clean up...

View file

@ -27,18 +27,19 @@
#include <QVariantMap> #include <QVariantMap>
#include <QTcpServer> #include <QTcpServer>
#include <QTcpSocket> #include <QTcpSocket>
#include <QSharedPointer>
class UploadJob class UploadJob
: public KJob : public KJob
{ {
Q_OBJECT Q_OBJECT
public: public:
UploadJob(QIODevice* source); UploadJob(const QSharedPointer<QIODevice>& source);
virtual void start(); virtual void start();
QVariantMap getTransferInfo(); QVariantMap getTransferInfo();
private: private:
QIODevice* mInput; QSharedPointer<QIODevice> mInput;
QTcpServer* mServer; QTcpServer* mServer;
QTcpSocket* mSocket; QTcpSocket* mSocket;
qint16 mPort; qint16 mPort;

View file

@ -46,8 +46,7 @@ bool LoopbackDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackag
//LoopbackDeviceLink does not need deviceTransferInfo //LoopbackDeviceLink does not need deviceTransferInfo
if (input.hasPayload()) { if (input.hasPayload()) {
QIODevice* device = input.payload(); output.setPayload(input.payload(), input.payloadSize());
output.setPayload(device, input.payloadSize());
} }
Q_EMIT receivedPackage(output); Q_EMIT receivedPackage(output);
@ -62,8 +61,7 @@ bool LoopbackDeviceLink::sendPackage(NetworkPackage& input)
//LoopbackDeviceLink does not need deviceTransferInfo //LoopbackDeviceLink does not need deviceTransferInfo
if (input.hasPayload()) { if (input.hasPayload()) {
QIODevice* device = input.payload(); output.setPayload(input.payload(), input.payloadSize());
output.setPayload(device, input.payloadSize());
} }
Q_EMIT receivedPackage(output); Q_EMIT receivedPackage(output);

View file

@ -25,26 +25,20 @@
#include <QDebug> #include <QDebug>
#include <qalgorithms.h> #include <qalgorithms.h>
FileTransferJob::FileTransferJob(QIODevice* origin, int size, const KUrl& destination): KJob() FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination): KJob()
{ {
mDestination = destination; mDestination = KIO::open(destination, QIODevice::WriteOnly);
mOrigin = origin; mOrigin = origin;
mSize = size; mSize = size;
mWritten = 0; mWritten = 0;
qDebug() << "Downloading payload to" << destination;
} }
void FileTransferJob::start() void FileTransferJob::start()
{ {
//Open destination file //Open destination file
connect(mDestination, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
QTemporaryFile tmp; mDestination->start();
tmp.setAutoRemove(false);
tmp.open();
mTempDestination = KIO::open(tmp.fileName(), QIODevice::WriteOnly);
connect(mTempDestination, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
mTempDestination->start();
} }
void FileTransferJob::open(KIO::Job* job) void FileTransferJob::open(KIO::Job* job)
@ -62,8 +56,8 @@ void FileTransferJob::open(KIO::Job* job)
mOrigin->open(QIODevice::ReadOnly); mOrigin->open(QIODevice::ReadOnly);
Q_ASSERT(mOrigin->isOpen()); Q_ASSERT(mOrigin->isOpen());
connect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead())); connect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
connect(mOrigin, SIGNAL(disconnected()),this, SLOT(sourceFinished())); connect(mOrigin.data(), SIGNAL(disconnected()),this, SLOT(sourceFinished()));
if (mOrigin->bytesAvailable() > 0) readyRead(); if (mOrigin->bytesAvailable() > 0) readyRead();
} }
@ -76,7 +70,7 @@ void FileTransferJob::readyRead()
int bytes = qMin(qint64(4096), mOrigin->bytesAvailable()); int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
QByteArray data = mOrigin->read(bytes); QByteArray data = mOrigin->read(bytes);
mTempDestination->write(data); mDestination->write(data);
mWritten += bytes; mWritten += bytes;
if (mSize > -1) { if (mSize > -1) {
@ -87,7 +81,7 @@ void FileTransferJob::readyRead()
if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached
qDebug() << "No more data to read"; qDebug() << "No more data to read";
disconnect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead())); disconnect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
mOrigin->close(); mOrigin->close();
} else if (mOrigin->bytesAvailable() > 0) { } else if (mOrigin->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
@ -99,7 +93,7 @@ void FileTransferJob::sourceFinished()
qDebug() << "sourceFinished"; qDebug() << "sourceFinished";
//Make sure we do not enter this function again //Make sure we do not enter this function again
disconnect(mOrigin, SIGNAL(aboutToClose()),this, SLOT(sourceFinished())); disconnect(mOrigin.data(), SIGNAL(aboutToClose()),this, SLOT(sourceFinished()));
//TODO: MD5 check the file //TODO: MD5 check the file
if (mSize > -1 && mWritten != mSize) { if (mSize > -1 && mWritten != mSize) {
@ -107,23 +101,10 @@ void FileTransferJob::sourceFinished()
setError(1); setError(1);
setErrorText(i18n("Received incomplete file")); setErrorText(i18n("Received incomplete file"));
emitResult(); emitResult();
} } else {
qDebug() << "Finished transfer" << mDestination->url();
qDebug() << "Finished" << mTempDestination->url() << mDestination;
KIO::FileCopyJob* job = KIO::file_move(mTempDestination->url(), mDestination);
connect(job, SIGNAL(result(KJob*)), this, SLOT(moveResult(KJob*)));
job->start();
//delete mOrigin; //TODO: Use shared pointers
}
void FileTransferJob::moveResult(KJob* job)
{
//TODO: Error handling, cleanup
qDebug() << "Move finished";
if (job->error()) {
qDebug() << job->errorText();
} }
emitResult(); emitResult();
} }

View file

@ -35,21 +35,19 @@ class FileTransferJob : public KJob
Q_OBJECT Q_OBJECT
public: public:
FileTransferJob(QIODevice* origin, int size, const KUrl& destination); FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination);
virtual void start(); virtual void start();
KUrl destination() { return mDestination; } KUrl destination() { return mDestination->url(); }
public Q_SLOTS: public Q_SLOTS:
void readyRead(); void readyRead();
void moveResult(KJob*);
void open(KIO::Job*); void open(KIO::Job*);
void sourceFinished(); void sourceFinished();
private: private:
KIO::FileJob* mTempDestination; QSharedPointer<QIODevice> mOrigin;
KUrl mDestination; KIO::FileJob* mDestination;
int mSize; int mSize;
QIODevice* mOrigin;
int mWritten; int mWritten;
}; };

View file

@ -29,7 +29,6 @@
#include <QSslKey> #include <QSslKey>
#include <QDateTime> #include <QDateTime>
#include <QtCrypto> #include <QtCrypto>
#include <qjson/serializer.h> #include <qjson/serializer.h>
#include <qjson/qobjecthelper.h> #include <qjson/qobjecthelper.h>
@ -43,7 +42,7 @@ NetworkPackage::NetworkPackage(const QString& type)
mId = QString::number(QDateTime::currentMSecsSinceEpoch()); mId = QString::number(QDateTime::currentMSecsSinceEpoch());
mType = type; mType = type;
mBody = QVariantMap(); mBody = QVariantMap();
mPayload = 0; mPayload = QSharedPointer<QIODevice>();
mPayloadSize = 0; mPayloadSize = 0;
} }
@ -53,7 +52,7 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np)
QString id = config->group("myself").readEntry<QString>("id",""); QString id = config->group("myself").readEntry<QString>("id","");
np->mId = QString::number(QDateTime::currentMSecsSinceEpoch()); np->mId = QString::number(QDateTime::currentMSecsSinceEpoch());
np->mType = PACKAGE_TYPE_IDENTITY; np->mType = PACKAGE_TYPE_IDENTITY;
np->mPayload = 0; np->mPayload = QSharedPointer<QIODevice>();
np->mPayloadSize = 0; np->mPayloadSize = 0;
np->set("deviceId", id); np->set("deviceId", id);
np->set("deviceName", QHostInfo::localHostName()); np->set("deviceName", QHostInfo::localHostName());

View file

@ -32,6 +32,7 @@
#include <QStringList> #include <QStringList>
#include <QIODevice> #include <QIODevice>
#include <QtCrypto> #include <QtCrypto>
#include <QSharedPointer>
#include <qjson/parser.h> #include <qjson/parser.h>
@ -73,9 +74,8 @@ public:
template<typename T> void set(const QString& key, const T& value) { mBody[key] = QVariant(value); } template<typename T> void set(const QString& key, const T& value) { mBody[key] = QVariant(value); }
bool has(const QString& key) const { return mBody.contains(key); } bool has(const QString& key) const { return mBody.contains(key); }
//TODO: Change to a shared pointer QSharedPointer<QIODevice> payload() const { return mPayload; }
QIODevice* payload() const { return mPayload; } void setPayload(const QSharedPointer<QIODevice>& device, int payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); }
void setPayload(QIODevice* device, int payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); }
bool hasPayload() const { return (mPayloadSize != 0); } bool hasPayload() const { return (mPayloadSize != 0); }
int payloadSize() const { return mPayloadSize; } //-1 means it is an endless stream int payloadSize() const { return mPayloadSize; } //-1 means it is an endless stream
FileTransferJob* createPayloadTransferJob(const KUrl& destination) const; FileTransferJob* createPayloadTransferJob(const KUrl& destination) const;
@ -96,7 +96,7 @@ private:
QString mType; QString mType;
QVariantMap mBody; QVariantMap mBody;
QIODevice* mPayload; QSharedPointer<QIODevice> mPayload;
int mPayloadSize; int mPayloadSize;
QVariantMap mPayloadTransferInfo; QVariantMap mPayloadTransferInfo;