Payloads are now QSharedPointer<QIODevice> instead of QIODevice*
This commit is contained in:
parent
a53ec2a9ea
commit
4bd5d2e427
9 changed files with 40 additions and 60 deletions
|
@ -22,9 +22,10 @@
|
|||
|
||||
DownloadJob::DownloadJob(QHostAddress address, QVariantMap transferInfo): KJob()
|
||||
{
|
||||
mAddress = address;
|
||||
mPort = transferInfo["port"].toInt();
|
||||
mSocket = new QTcpSocket();
|
||||
mAddress = address;
|
||||
mOutput = QSharedPointer<QIODevice>(mSocket);
|
||||
}
|
||||
|
||||
void DownloadJob::start()
|
||||
|
@ -40,8 +41,8 @@ void DownloadJob::disconnected()
|
|||
emitResult();
|
||||
}
|
||||
|
||||
QIODevice* DownloadJob::getPayload()
|
||||
QSharedPointer<QIODevice> DownloadJob::getPayload()
|
||||
{
|
||||
qDebug() << "getPayload";
|
||||
return mSocket;
|
||||
return mOutput;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include <QVariantMap>
|
||||
#include <QHostAddress>
|
||||
#include <QTcpSocket>
|
||||
#include <QSharedPointer>
|
||||
|
||||
class DownloadJob
|
||||
: public KJob
|
||||
|
@ -35,13 +36,14 @@ class DownloadJob
|
|||
public:
|
||||
DownloadJob(QHostAddress address, QVariantMap transferInfo);
|
||||
virtual void start();
|
||||
QIODevice* getPayload();
|
||||
QSharedPointer<QIODevice> getPayload();
|
||||
|
||||
private:
|
||||
QIODevice* mOutput;
|
||||
QTcpSocket* mSocket;
|
||||
QHostAddress mAddress;
|
||||
qint16 mPort;
|
||||
QTcpSocket* mSocket;
|
||||
QSharedPointer<QIODevice> mOutput;
|
||||
|
||||
|
||||
private Q_SLOTS:
|
||||
void disconnected();
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
#include "uploadjob.h"
|
||||
|
||||
UploadJob::UploadJob(QIODevice* source): KJob()
|
||||
UploadJob::UploadJob(const QSharedPointer<QIODevice>& source): KJob()
|
||||
{
|
||||
mInput = source;
|
||||
mServer = new QTcpServer(this);
|
||||
|
@ -51,8 +51,8 @@ void UploadJob::newConnection()
|
|||
|
||||
mSocket = mServer->nextPendingConnection();
|
||||
|
||||
connect(mInput, SIGNAL(readyRead()), this, SLOT(readyRead()));
|
||||
connect(mInput, SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
|
||||
connect(mInput.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
|
||||
connect(mInput.data(), SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
|
||||
|
||||
if (!mInput->open(QIODevice::ReadOnly)) {
|
||||
return; //TODO: Handle error, clean up...
|
||||
|
|
|
@ -27,18 +27,19 @@
|
|||
#include <QVariantMap>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QSharedPointer>
|
||||
|
||||
class UploadJob
|
||||
: public KJob
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
UploadJob(QIODevice* source);
|
||||
UploadJob(const QSharedPointer<QIODevice>& source);
|
||||
virtual void start();
|
||||
QVariantMap getTransferInfo();
|
||||
|
||||
private:
|
||||
QIODevice* mInput;
|
||||
QSharedPointer<QIODevice> mInput;
|
||||
QTcpServer* mServer;
|
||||
QTcpSocket* mSocket;
|
||||
qint16 mPort;
|
||||
|
|
|
@ -46,8 +46,7 @@ bool LoopbackDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackag
|
|||
|
||||
//LoopbackDeviceLink does not need deviceTransferInfo
|
||||
if (input.hasPayload()) {
|
||||
QIODevice* device = input.payload();
|
||||
output.setPayload(device, input.payloadSize());
|
||||
output.setPayload(input.payload(), input.payloadSize());
|
||||
}
|
||||
|
||||
Q_EMIT receivedPackage(output);
|
||||
|
@ -62,8 +61,7 @@ bool LoopbackDeviceLink::sendPackage(NetworkPackage& input)
|
|||
|
||||
//LoopbackDeviceLink does not need deviceTransferInfo
|
||||
if (input.hasPayload()) {
|
||||
QIODevice* device = input.payload();
|
||||
output.setPayload(device, input.payloadSize());
|
||||
output.setPayload(input.payload(), input.payloadSize());
|
||||
}
|
||||
|
||||
Q_EMIT receivedPackage(output);
|
||||
|
|
|
@ -25,26 +25,20 @@
|
|||
#include <QDebug>
|
||||
#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;
|
||||
mSize = size;
|
||||
mWritten = 0;
|
||||
qDebug() << "Downloading payload to" << destination;
|
||||
}
|
||||
|
||||
void FileTransferJob::start()
|
||||
{
|
||||
//Open destination file
|
||||
|
||||
QTemporaryFile tmp;
|
||||
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();
|
||||
|
||||
connect(mDestination, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
|
||||
mDestination->start();
|
||||
}
|
||||
|
||||
void FileTransferJob::open(KIO::Job* job)
|
||||
|
@ -62,8 +56,8 @@ void FileTransferJob::open(KIO::Job* job)
|
|||
mOrigin->open(QIODevice::ReadOnly);
|
||||
Q_ASSERT(mOrigin->isOpen());
|
||||
|
||||
connect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead()));
|
||||
connect(mOrigin, SIGNAL(disconnected()),this, SLOT(sourceFinished()));
|
||||
connect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
|
||||
connect(mOrigin.data(), SIGNAL(disconnected()),this, SLOT(sourceFinished()));
|
||||
if (mOrigin->bytesAvailable() > 0) readyRead();
|
||||
|
||||
}
|
||||
|
@ -76,7 +70,7 @@ void FileTransferJob::readyRead()
|
|||
|
||||
int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
|
||||
QByteArray data = mOrigin->read(bytes);
|
||||
mTempDestination->write(data);
|
||||
mDestination->write(data);
|
||||
mWritten += bytes;
|
||||
|
||||
if (mSize > -1) {
|
||||
|
@ -87,7 +81,7 @@ void FileTransferJob::readyRead()
|
|||
|
||||
if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached
|
||||
qDebug() << "No more data to read";
|
||||
disconnect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead()));
|
||||
disconnect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
|
||||
mOrigin->close();
|
||||
} else if (mOrigin->bytesAvailable() > 0) {
|
||||
QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
|
||||
|
@ -99,7 +93,7 @@ void FileTransferJob::sourceFinished()
|
|||
qDebug() << "sourceFinished";
|
||||
|
||||
//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
|
||||
if (mSize > -1 && mWritten != mSize) {
|
||||
|
@ -107,23 +101,10 @@ void FileTransferJob::sourceFinished()
|
|||
setError(1);
|
||||
setErrorText(i18n("Received incomplete file"));
|
||||
emitResult();
|
||||
}
|
||||
|
||||
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();
|
||||
} else {
|
||||
qDebug() << "Finished transfer" << mDestination->url();
|
||||
}
|
||||
emitResult();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -35,21 +35,19 @@ class FileTransferJob : public KJob
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FileTransferJob(QIODevice* origin, int size, const KUrl& destination);
|
||||
FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination);
|
||||
virtual void start();
|
||||
KUrl destination() { return mDestination; }
|
||||
KUrl destination() { return mDestination->url(); }
|
||||
|
||||
public Q_SLOTS:
|
||||
void readyRead();
|
||||
void moveResult(KJob*);
|
||||
void open(KIO::Job*);
|
||||
void sourceFinished();
|
||||
|
||||
private:
|
||||
KIO::FileJob* mTempDestination;
|
||||
KUrl mDestination;
|
||||
QSharedPointer<QIODevice> mOrigin;
|
||||
KIO::FileJob* mDestination;
|
||||
int mSize;
|
||||
QIODevice* mOrigin;
|
||||
int mWritten;
|
||||
|
||||
};
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
#include <QSslKey>
|
||||
#include <QDateTime>
|
||||
#include <QtCrypto>
|
||||
|
||||
#include <qjson/serializer.h>
|
||||
#include <qjson/qobjecthelper.h>
|
||||
|
||||
|
@ -43,7 +42,7 @@ NetworkPackage::NetworkPackage(const QString& type)
|
|||
mId = QString::number(QDateTime::currentMSecsSinceEpoch());
|
||||
mType = type;
|
||||
mBody = QVariantMap();
|
||||
mPayload = 0;
|
||||
mPayload = QSharedPointer<QIODevice>();
|
||||
mPayloadSize = 0;
|
||||
}
|
||||
|
||||
|
@ -53,7 +52,7 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np)
|
|||
QString id = config->group("myself").readEntry<QString>("id","");
|
||||
np->mId = QString::number(QDateTime::currentMSecsSinceEpoch());
|
||||
np->mType = PACKAGE_TYPE_IDENTITY;
|
||||
np->mPayload = 0;
|
||||
np->mPayload = QSharedPointer<QIODevice>();
|
||||
np->mPayloadSize = 0;
|
||||
np->set("deviceId", id);
|
||||
np->set("deviceName", QHostInfo::localHostName());
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <QStringList>
|
||||
#include <QIODevice>
|
||||
#include <QtCrypto>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <qjson/parser.h>
|
||||
|
||||
|
@ -73,9 +74,8 @@ public:
|
|||
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); }
|
||||
|
||||
//TODO: Change to a shared pointer
|
||||
QIODevice* payload() const { return mPayload; }
|
||||
void setPayload(QIODevice* device, int payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); }
|
||||
QSharedPointer<QIODevice> payload() const { return mPayload; }
|
||||
void setPayload(const QSharedPointer<QIODevice>& device, int payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); }
|
||||
bool hasPayload() const { return (mPayloadSize != 0); }
|
||||
int payloadSize() const { return mPayloadSize; } //-1 means it is an endless stream
|
||||
FileTransferJob* createPayloadTransferJob(const KUrl& destination) const;
|
||||
|
@ -96,7 +96,7 @@ private:
|
|||
QString mType;
|
||||
QVariantMap mBody;
|
||||
|
||||
QIODevice* mPayload;
|
||||
QSharedPointer<QIODevice> mPayload;
|
||||
int mPayloadSize;
|
||||
QVariantMap mPayloadTransferInfo;
|
||||
|
||||
|
|
Loading…
Reference in a new issue