2013-09-14 16:30:12 +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/>.
|
|
|
|
*/
|
|
|
|
|
2015-08-21 17:38:54 +01:00
|
|
|
#include "uploadjob.h"
|
|
|
|
|
2013-09-14 16:30:12 +01:00
|
|
|
#include <qalgorithms.h>
|
2015-04-04 18:05:55 +01:00
|
|
|
#include <QtGlobal>
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2013-09-24 13:13:02 +01:00
|
|
|
UploadJob::UploadJob(const QSharedPointer<QIODevice>& source): KJob()
|
2013-09-14 16:30:12 +01:00
|
|
|
{
|
|
|
|
mInput = source;
|
|
|
|
mServer = new QTcpServer(this);
|
|
|
|
mSocket = 0;
|
2015-04-04 18:05:55 +01:00
|
|
|
mPort = 0;
|
2014-03-03 03:51:36 +00:00
|
|
|
|
|
|
|
connect(mInput.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
|
|
|
|
connect(mInput.data(), SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploadJob::start()
|
|
|
|
{
|
|
|
|
mPort = 1739;
|
2015-03-22 06:11:50 +00:00
|
|
|
while (!mServer->listen(QHostAddress::Any, mPort)) {
|
2013-09-14 16:30:12 +01:00
|
|
|
mPort++;
|
|
|
|
if (mPort > 1764) { //No ports available?
|
2015-03-22 06:11:50 +00:00
|
|
|
qWarning(KDECONNECT_CORE) << "Error opening a port in range 1739-1764 for file transfer";
|
2013-09-14 16:30:12 +01:00
|
|
|
mPort = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
connect(mServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploadJob::newConnection()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mSocket || !mServer->hasPendingConnections()) return;
|
|
|
|
|
|
|
|
if (!mInput->open(QIODevice::ReadOnly)) {
|
2014-03-03 03:51:36 +00:00
|
|
|
qWarning() << "error when opening the input to upload";
|
2013-09-14 16:30:12 +01:00
|
|
|
return; //TODO: Handle error, clean up...
|
|
|
|
}
|
|
|
|
|
2014-03-03 03:51:36 +00:00
|
|
|
mSocket = mServer->nextPendingConnection();
|
|
|
|
readyRead();
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploadJob::readyRead()
|
|
|
|
{
|
2013-09-20 14:54:30 +01:00
|
|
|
//TODO: Implement payload encryption
|
|
|
|
|
2014-11-09 20:57:09 +00:00
|
|
|
while ( mInput->bytesAvailable() > 0 )
|
|
|
|
{
|
|
|
|
qint64 bytes = qMin(mInput->bytesAvailable(), (qint64)4096);
|
|
|
|
int w = mSocket->write(mInput->read(bytes));
|
|
|
|
if (w<0) {
|
|
|
|
qWarning() << "error when writing data to upload" << bytes << mInput->bytesAvailable();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while ( mSocket->flush() );
|
|
|
|
}
|
2014-03-03 03:51:36 +00:00
|
|
|
}
|
2014-11-09 20:57:09 +00:00
|
|
|
|
|
|
|
mInput->close();
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploadJob::aboutToClose()
|
|
|
|
{
|
|
|
|
mSocket->disconnectFromHost();
|
2014-11-09 20:57:09 +00:00
|
|
|
mSocket->close();
|
2013-09-14 16:30:12 +01:00
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap UploadJob::getTransferInfo()
|
|
|
|
{
|
2015-04-04 18:05:55 +01:00
|
|
|
Q_ASSERT(mPort != 0);
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2015-04-04 18:05:55 +01:00
|
|
|
QVariantMap ret;
|
2013-09-14 16:30:12 +01:00
|
|
|
ret["port"] = mPort;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|