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-11-06 21:16:55 +00:00
|
|
|
#include <qalgorithms.h>
|
2014-03-03 21:33:22 +00:00
|
|
|
#include <QFileInfo>
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2014-03-03 21:33:22 +00:00
|
|
|
#include <KIO/RenameDialog>
|
2013-09-20 14:54:30 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "kdebugnamespace.h"
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-09-24 13:13:02 +01:00
|
|
|
FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, int size, const KUrl& destination): KJob()
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2013-09-26 23:01:59 +01:00
|
|
|
Q_ASSERT(destination.isLocalFile());
|
2013-10-01 01:44:49 +01:00
|
|
|
//TODO: Make a precondition before calling this function that destination file exists
|
2013-09-10 18:01:46 +01:00
|
|
|
mOrigin = origin;
|
2013-09-20 14:54:30 +01:00
|
|
|
mSize = size;
|
|
|
|
mWritten = 0;
|
2014-03-03 20:12:06 +00:00
|
|
|
m_speedBytes = 0;
|
2014-03-03 21:33:22 +00:00
|
|
|
mDestination = destination;
|
|
|
|
mDestinationJob = 0;
|
2014-03-03 20:07:02 +00:00
|
|
|
mDeviceName = i18nc("Device name that will appear on the jobs", "KDE-Connect");
|
2013-11-06 21:16:55 +00:00
|
|
|
kDebug(kdeconnect_kded()) << "FileTransferJob Downloading payload to" << destination;
|
2013-09-26 23:01:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::openFinished(KJob* job)
|
|
|
|
{
|
2013-11-06 21:16:55 +00:00
|
|
|
kDebug(kdeconnect_kded()) << job->errorString();
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::start()
|
|
|
|
{
|
2014-03-03 21:33:22 +00:00
|
|
|
QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
|
2013-11-06 21:16:55 +00:00
|
|
|
//kDebug(kdeconnect_kded()) << "FileTransferJob start";
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::doStart()
|
|
|
|
{
|
|
|
|
description(this, i18n("Receiving file over KDE-Connect"),
|
|
|
|
QPair<QString, QString>(i18nc("File transfer origin", "From"),
|
|
|
|
QString(mDeviceName))
|
|
|
|
);
|
|
|
|
KUrl destCheck = mDestination;
|
|
|
|
if (QFile::exists(destCheck.path())) {
|
|
|
|
QFileInfo destInfo(destCheck.path());
|
|
|
|
KIO::RenameDialog *dialog = new KIO::RenameDialog(0,
|
|
|
|
i18n("Incoming file exists"),
|
|
|
|
KUrl(mDeviceName + ":/" + destCheck.fileName()),
|
|
|
|
destCheck,
|
|
|
|
KIO::M_OVERWRITE,
|
|
|
|
mSize,
|
|
|
|
destInfo.size(),
|
|
|
|
-1,
|
|
|
|
destInfo.created().toTime_t(),
|
|
|
|
-1,
|
|
|
|
destInfo.lastModified().toTime_t()
|
|
|
|
);
|
|
|
|
connect(this, SIGNAL(finished(KJob*)), dialog, SLOT(deleteLater()));
|
|
|
|
connect(dialog, SIGNAL(finished(int)), SLOT(renameDone(int)));
|
|
|
|
dialog->show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
startTransfer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::renameDone(int result)
|
|
|
|
{
|
|
|
|
KIO::RenameDialog *renameDialog = qobject_cast<KIO::RenameDialog*>(sender());
|
|
|
|
switch (result) {
|
|
|
|
case KIO::R_CANCEL:
|
|
|
|
//The user cancelled, killing the job
|
|
|
|
emitResult();
|
|
|
|
case KIO::R_RENAME:
|
|
|
|
mDestination = renameDialog->newDestUrl();
|
|
|
|
break;
|
|
|
|
case KIO::R_OVERWRITE:
|
|
|
|
{
|
|
|
|
// Delete the old file if exists
|
|
|
|
QFile oldFile(mDestination.path());
|
|
|
|
if (oldFile.exists()) {
|
|
|
|
oldFile.remove();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
kWarning() << "Unknown Error";
|
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
renameDialog->deleteLater();
|
|
|
|
startTransfer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::startTransfer()
|
|
|
|
{
|
2014-03-03 20:12:06 +00:00
|
|
|
setTotalAmount(Bytes, mSize);
|
|
|
|
setProcessedAmount(Bytes, 0);
|
|
|
|
m_time = QTime::currentTime();
|
2014-03-03 20:07:02 +00:00
|
|
|
description(this, i18n("Receiving file over KDE-Connect"),
|
|
|
|
QPair<QString, QString>(i18nc("File transfer origin", "From"),
|
|
|
|
QString(mDeviceName)),
|
2014-03-03 21:33:22 +00:00
|
|
|
QPair<QString, QString>(i18nc("File transfer destination", "To"), mDestination.path()));
|
|
|
|
|
|
|
|
QFile(mDestination.path()).open(QIODevice::WriteOnly | QIODevice::Truncate); //HACK: KIO is so dumb it can't create the file if it doesn't exist
|
|
|
|
mDestinationJob = KIO::open(mDestination, QIODevice::WriteOnly);
|
|
|
|
connect(mDestinationJob, SIGNAL(open(KIO::Job*)), this, SLOT(open(KIO::Job*)));
|
|
|
|
connect(mDestinationJob, SIGNAL(result(KJob*)), this, SLOT(openFinished(KJob*)));
|
|
|
|
|
2013-09-10 18:01:46 +01:00
|
|
|
//Open destination file
|
2014-03-03 21:33:22 +00:00
|
|
|
mDestinationJob->start();
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::open(KIO::Job* job)
|
|
|
|
{
|
|
|
|
Q_UNUSED(job);
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
//kDebug(kdeconnect_kded()) << "FileTransferJob open";
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
if (!mOrigin) {
|
2013-11-06 21:16:55 +00:00
|
|
|
kDebug(kdeconnect_kded()) << "FileTransferJob: Origin is null";
|
2013-09-20 14:54:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Open source file
|
|
|
|
mOrigin->open(QIODevice::ReadOnly);
|
2013-09-10 18:01:46 +01:00
|
|
|
Q_ASSERT(mOrigin->isOpen());
|
|
|
|
|
2013-09-24 13:13:02 +01:00
|
|
|
connect(mOrigin.data(), SIGNAL(readyRead()),this, SLOT(readyRead()));
|
|
|
|
connect(mOrigin.data(), SIGNAL(disconnected()),this, SLOT(sourceFinished()));
|
2013-09-10 18:01:46 +01:00
|
|
|
if (mOrigin->bytesAvailable() > 0) readyRead();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::readyRead()
|
|
|
|
{
|
|
|
|
int bytes = qMin(qint64(4096), mOrigin->bytesAvailable());
|
|
|
|
QByteArray data = mOrigin->read(bytes);
|
2014-03-03 21:33:22 +00:00
|
|
|
mDestinationJob->write(data);
|
2014-03-03 20:12:06 +00:00
|
|
|
mWritten += data.size();
|
|
|
|
setProcessedAmount(Bytes, mWritten);
|
2013-09-20 14:54:30 +01:00
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
//kDebug(kdeconnect_kded()) << "readyRead" << mSize << mWritten << bytes;
|
2013-09-26 23:01:59 +01:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
if (mSize > -1) {
|
2014-03-03 20:12:06 +00:00
|
|
|
//If a least 1 second has passed since last update
|
|
|
|
int secondsSinceLastTime = m_time.secsTo(QTime::currentTime());
|
|
|
|
if (secondsSinceLastTime > 0 && m_speedBytes > 0) {
|
|
|
|
float speed = (mWritten - m_speedBytes) / secondsSinceLastTime;
|
|
|
|
emitSpeed(speed);
|
|
|
|
|
|
|
|
m_time = QTime::currentTime();
|
|
|
|
m_speedBytes = mWritten;
|
|
|
|
} else if(m_speedBytes == 0) {
|
|
|
|
m_speedBytes = mWritten;
|
|
|
|
}
|
2013-09-20 14:54:30 +01:00
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached
|
|
|
|
mOrigin->close();
|
2013-10-01 01:44:49 +01:00
|
|
|
//sourceFinished();
|
2013-09-20 14:54:30 +01:00
|
|
|
} 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()
|
|
|
|
{
|
2013-09-20 14:54:30 +01:00
|
|
|
//Make sure we do not enter this function again
|
2013-09-24 13:13:02 +01:00
|
|
|
disconnect(mOrigin.data(), SIGNAL(aboutToClose()),this, SLOT(sourceFinished()));
|
2013-09-20 14:54:30 +01:00
|
|
|
|
|
|
|
//TODO: MD5 check the file
|
|
|
|
if (mSize > -1 && mWritten != mSize) {
|
2013-11-23 00:39:10 +00:00
|
|
|
kDebug(kdeconnect_kded()) << "Received incomplete file (" << mWritten << " of " << mSize << " bytes)";
|
2013-09-20 14:54:30 +01:00
|
|
|
setError(1);
|
|
|
|
setErrorText(i18n("Received incomplete file"));
|
2013-09-24 13:13:02 +01:00
|
|
|
} else {
|
2014-03-03 21:33:22 +00:00
|
|
|
kDebug(kdeconnect_kded()) << "Finished transfer" << mDestinationJob->url();
|
2013-09-20 14:54:30 +01:00
|
|
|
}
|
2014-03-03 21:33:22 +00:00
|
|
|
mDestinationJob->close();
|
|
|
|
mDestinationJob->deleteLater();
|
2013-09-10 18:01:46 +01:00
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
|