2013-09-10 18:01:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filetransferjob.h"
|
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2013-09-10 18:01:46 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <qalgorithms.h>
|
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
FileTransferJob::FileTransferJob(QIODevice* origin, int size, const KUrl& destination): KJob()
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
|
|
|
mDestination = destination;
|
|
|
|
mOrigin = origin;
|
2013-09-20 14:54:30 +01:00
|
|
|
mSize = size;
|
|
|
|
mWritten = 0;
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::open(KIO::Job* job)
|
|
|
|
{
|
|
|
|
Q_UNUSED(job);
|
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
qDebug() << "open";
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
if (!mOrigin) {
|
|
|
|
qDebug() << "FileTransferJob: Origin is null";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Open source file
|
|
|
|
mOrigin->open(QIODevice::ReadOnly);
|
2013-09-10 18:01:46 +01:00
|
|
|
Q_ASSERT(mOrigin->isOpen());
|
|
|
|
|
|
|
|
connect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead()));
|
2013-09-20 14:54:30 +01:00
|
|
|
connect(mOrigin, SIGNAL(disconnected()),this, SLOT(sourceFinished()));
|
2013-09-10 18:01:46 +01:00
|
|
|
if (mOrigin->bytesAvailable() > 0) readyRead();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::readyRead()
|
|
|
|
{
|
2013-09-20 14:54:30 +01:00
|
|
|
qDebug() << "readyRead";
|
|
|
|
|
2013-09-10 18:01:46 +01:00
|
|
|
//Copy a chunk of data
|
|
|
|
|
|
|
|
int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
|
|
|
|
QByteArray data = mOrigin->read(bytes);
|
|
|
|
mTempDestination->write(data);
|
2013-09-20 14:54:30 +01:00
|
|
|
mWritten += bytes;
|
|
|
|
|
|
|
|
if (mSize > -1) {
|
|
|
|
setPercent((mWritten*100)/mSize);
|
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
qDebug() << mSize << mWritten << bytes;
|
|
|
|
|
|
|
|
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()));
|
|
|
|
mOrigin->close();
|
|
|
|
} else if (mOrigin->bytesAvailable() > 0) {
|
2013-09-10 18:01:46 +01:00
|
|
|
QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
|
2013-09-20 14:54:30 +01:00
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::sourceFinished()
|
|
|
|
{
|
|
|
|
qDebug() << "sourceFinished";
|
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
//Make sure we do not enter this function again
|
|
|
|
disconnect(mOrigin, SIGNAL(aboutToClose()),this, SLOT(sourceFinished()));
|
|
|
|
|
|
|
|
//TODO: MD5 check the file
|
|
|
|
if (mSize > -1 && mWritten != mSize) {
|
|
|
|
qDebug() << "Received incomplete file";
|
|
|
|
setError(1);
|
|
|
|
setErrorText(i18n("Received incomplete file"));
|
|
|
|
emitResult();
|
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
qDebug() << "Finished" << mTempDestination->url() << mDestination;
|
2013-09-10 18:01:46 +01:00
|
|
|
KIO::FileCopyJob* job = KIO::file_move(mTempDestination->url(), mDestination);
|
|
|
|
connect(job, SIGNAL(result(KJob*)), this, SLOT(moveResult(KJob*)));
|
|
|
|
job->start();
|
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
//delete mOrigin; //TODO: Use shared pointers
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::moveResult(KJob* job)
|
|
|
|
{
|
|
|
|
//TODO: Error handling, cleanup
|
2013-09-20 14:54:30 +01:00
|
|
|
qDebug() << "Move finished";
|
|
|
|
if (job->error()) {
|
|
|
|
qDebug() << job->errorText();
|
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
|