2013-09-14 16:30:12 +01:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-09-14 16:30:12 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-09-14 16:30:12 +01:00
|
|
|
*/
|
|
|
|
|
2015-08-21 17:38:54 +01:00
|
|
|
#include "uploadjob.h"
|
|
|
|
|
2016-07-04 18:34:28 +01:00
|
|
|
#include <KLocalizedString>
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
|
|
|
#include "lanlinkprovider.h"
|
2018-11-21 18:58:44 +00:00
|
|
|
#include <daemon.h>
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
UploadJob::UploadJob(const NetworkPacket &networkPacket)
|
2016-06-22 16:49:45 +01:00
|
|
|
: KJob()
|
2018-11-28 18:56:22 +00:00
|
|
|
, m_networkPacket(networkPacket)
|
|
|
|
, m_input(networkPacket.payload())
|
2017-09-03 20:39:44 +01:00
|
|
|
, m_socket(nullptr)
|
2013-09-14 16:30:12 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void UploadJob::setSocket(QSslSocket *socket)
|
2013-09-14 16:30:12 +01:00
|
|
|
{
|
2018-11-28 18:56:22 +00:00
|
|
|
m_socket = socket;
|
|
|
|
m_socket->setParent(this);
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 18:56:22 +00:00
|
|
|
void UploadJob::start()
|
2013-09-14 16:30:12 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!m_input->open(QIODevice::ReadOnly)) {
|
2015-09-07 17:54:11 +01:00
|
|
|
qCWarning(KDECONNECT_CORE) << "error when opening the input to upload";
|
2022-09-10 22:23:52 +01:00
|
|
|
return; // TODO: Handle error, clean up...
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-28 18:56:22 +00:00
|
|
|
if (!m_socket) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "you must call setSocket() before calling start()";
|
|
|
|
return;
|
|
|
|
}
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2018-11-28 18:56:22 +00:00
|
|
|
connect(m_input.data(), &QIODevice::aboutToClose, this, &UploadJob::aboutToClose);
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-21 18:58:44 +00:00
|
|
|
bytesUploaded = 0;
|
|
|
|
setProcessedAmount(Bytes, bytesUploaded);
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-21 18:58:44 +00:00
|
|
|
connect(m_socket, &QSslSocket::encryptedBytesWritten, this, &UploadJob::encryptedBytesWritten);
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-21 18:58:44 +00:00
|
|
|
uploadNextPacket();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploadJob::uploadNextPacket()
|
|
|
|
{
|
|
|
|
qint64 bytesAvailable = m_input->bytesAvailable();
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (bytesAvailable > 0) {
|
2018-11-21 18:58:44 +00:00
|
|
|
qint64 bytesToSend = qMin(m_input->bytesAvailable(), (qint64)4096);
|
|
|
|
bytesUploading = m_socket->write(m_input->read(bytesToSend));
|
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-21 18:58:44 +00:00
|
|
|
if (bytesAvailable <= 0 || bytesUploading < 0) {
|
|
|
|
m_input->close();
|
|
|
|
disconnect(m_socket, &QSslSocket::encryptedBytesWritten, this, &UploadJob::encryptedBytesWritten);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 08:11:43 +01:00
|
|
|
void UploadJob::encryptedBytesWritten(qint64 /*bytes*/)
|
2018-11-21 18:58:44 +00:00
|
|
|
{
|
|
|
|
if (m_socket->encryptedBytesToWrite() == 0) {
|
2018-11-28 18:56:22 +00:00
|
|
|
bytesUploaded += bytesUploading;
|
2018-11-21 18:58:44 +00:00
|
|
|
setProcessedAmount(Bytes, bytesUploaded);
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-21 18:58:44 +00:00
|
|
|
uploadNextPacket();
|
2014-03-03 03:51:36 +00:00
|
|
|
}
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploadJob::aboutToClose()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
m_socket->disconnectFromHost();
|
2013-09-14 16:30:12 +01:00
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool UploadJob::stop()
|
|
|
|
{
|
2018-11-28 18:56:22 +00:00
|
|
|
m_input->close();
|
2022-09-10 22:23:52 +01:00
|
|
|
|
2018-11-28 18:56:22 +00:00
|
|
|
return true;
|
2016-06-22 16:49:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 18:56:22 +00:00
|
|
|
const NetworkPacket UploadJob::getNetworkPacket()
|
2016-06-22 16:49:45 +01:00
|
|
|
{
|
2018-11-28 18:56:22 +00:00
|
|
|
return m_networkPacket;
|
2018-11-21 18:58:44 +00:00
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_uploadjob.cpp"
|