diff --git a/kded/backends/lan/downloadjob.cpp b/kded/backends/lan/downloadjob.cpp index d86801579..58a0ac24a 100644 --- a/kded/backends/lan/downloadjob.cpp +++ b/kded/backends/lan/downloadjob.cpp @@ -22,9 +22,10 @@ DownloadJob::DownloadJob(QHostAddress address, QVariantMap transferInfo): KJob() { + mAddress = address; mPort = transferInfo["port"].toInt(); mSocket = new QTcpSocket(); - mAddress = address; + mOutput = QSharedPointer(mSocket); } void DownloadJob::start() @@ -40,8 +41,8 @@ void DownloadJob::disconnected() emitResult(); } -QIODevice* DownloadJob::getPayload() +QSharedPointer DownloadJob::getPayload() { qDebug() << "getPayload"; - return mSocket; + return mOutput; } diff --git a/kded/backends/lan/downloadjob.h b/kded/backends/lan/downloadjob.h index 4cf28792c..ff428c1d7 100644 --- a/kded/backends/lan/downloadjob.h +++ b/kded/backends/lan/downloadjob.h @@ -27,6 +27,7 @@ #include #include #include +#include class DownloadJob : public KJob @@ -35,13 +36,14 @@ class DownloadJob public: DownloadJob(QHostAddress address, QVariantMap transferInfo); virtual void start(); - QIODevice* getPayload(); + QSharedPointer getPayload(); private: - QIODevice* mOutput; - QTcpSocket* mSocket; QHostAddress mAddress; qint16 mPort; + QTcpSocket* mSocket; + QSharedPointer mOutput; + private Q_SLOTS: void disconnected(); diff --git a/kded/backends/lan/uploadjob.cpp b/kded/backends/lan/uploadjob.cpp index af8009cbb..a5c739e65 100644 --- a/kded/backends/lan/uploadjob.cpp +++ b/kded/backends/lan/uploadjob.cpp @@ -23,7 +23,7 @@ #include "uploadjob.h" -UploadJob::UploadJob(QIODevice* source): KJob() +UploadJob::UploadJob(const QSharedPointer& 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... diff --git a/kded/backends/lan/uploadjob.h b/kded/backends/lan/uploadjob.h index a0a6474f7..4b3dd4d4f 100644 --- a/kded/backends/lan/uploadjob.h +++ b/kded/backends/lan/uploadjob.h @@ -27,18 +27,19 @@ #include #include #include +#include class UploadJob : public KJob { Q_OBJECT public: - UploadJob(QIODevice* source); + UploadJob(const QSharedPointer& source); virtual void start(); QVariantMap getTransferInfo(); private: - QIODevice* mInput; + QSharedPointer mInput; QTcpServer* mServer; QTcpSocket* mSocket; qint16 mPort; diff --git a/kded/backends/loopback/loopbackdevicelink.cpp b/kded/backends/loopback/loopbackdevicelink.cpp index c16a02e19..718c5b8fd 100644 --- a/kded/backends/loopback/loopbackdevicelink.cpp +++ b/kded/backends/loopback/loopbackdevicelink.cpp @@ -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); diff --git a/kded/filetransferjob.cpp b/kded/filetransferjob.cpp index 876b81740..d8b3ad3cc 100644 --- a/kded/filetransferjob.cpp +++ b/kded/filetransferjob.cpp @@ -25,26 +25,20 @@ #include #include -FileTransferJob::FileTransferJob(QIODevice* origin, int size, const KUrl& destination): KJob() +FileTransferJob::FileTransferJob(const QSharedPointer& 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(); } + diff --git a/kded/filetransferjob.h b/kded/filetransferjob.h index d38d0feaf..8923291af 100644 --- a/kded/filetransferjob.h +++ b/kded/filetransferjob.h @@ -35,21 +35,19 @@ class FileTransferJob : public KJob Q_OBJECT public: - FileTransferJob(QIODevice* origin, int size, const KUrl& destination); + FileTransferJob(const QSharedPointer& 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 mOrigin; + KIO::FileJob* mDestination; int mSize; - QIODevice* mOrigin; int mWritten; }; diff --git a/kded/networkpackage.cpp b/kded/networkpackage.cpp index 86b691eac..7949e9313 100644 --- a/kded/networkpackage.cpp +++ b/kded/networkpackage.cpp @@ -29,7 +29,6 @@ #include #include #include - #include #include @@ -43,7 +42,7 @@ NetworkPackage::NetworkPackage(const QString& type) mId = QString::number(QDateTime::currentMSecsSinceEpoch()); mType = type; mBody = QVariantMap(); - mPayload = 0; + mPayload = QSharedPointer(); mPayloadSize = 0; } @@ -53,7 +52,7 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np) QString id = config->group("myself").readEntry("id",""); np->mId = QString::number(QDateTime::currentMSecsSinceEpoch()); np->mType = PACKAGE_TYPE_IDENTITY; - np->mPayload = 0; + np->mPayload = QSharedPointer(); np->mPayloadSize = 0; np->set("deviceId", id); np->set("deviceName", QHostInfo::localHostName()); diff --git a/kded/networkpackage.h b/kded/networkpackage.h index 37433eca7..5d6ce6774 100644 --- a/kded/networkpackage.h +++ b/kded/networkpackage.h @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -73,9 +74,8 @@ public: template 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 payload() const { return mPayload; } + void setPayload(const QSharedPointer& 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 mPayload; int mPayloadSize; QVariantMap mPayloadTransferInfo;