2016-11-11 14:55:16 +00:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2016 Saikrishna Arcot <saiarcot895@gmail.com>
|
|
|
|
* SPDX-FileCopyrightText: 2018 Matthijs TIjink <matthijstijink@gmail.com>
|
2016-11-11 14:55:16 +00: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
|
2016-11-11 14:55:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bluetoothuploadjob.h"
|
2019-03-08 18:18:01 +00:00
|
|
|
#include "connectionmultiplexer.h"
|
|
|
|
#include "multiplexchannel.h"
|
2016-11-11 14:55:16 +00:00
|
|
|
|
|
|
|
#include "core_debug.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QBluetoothSocket>
|
2016-11-11 14:55:16 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
BluetoothUploadJob::BluetoothUploadJob(const QSharedPointer<QIODevice> &data, ConnectionMultiplexer *connection, QObject *parent)
|
2016-11-11 14:55:16 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, mData(data)
|
2019-03-08 18:18:01 +00:00
|
|
|
, mTransferUuid(connection->newChannel())
|
2016-11-11 14:55:16 +00:00
|
|
|
{
|
2019-03-08 18:18:01 +00:00
|
|
|
mSocket = QSharedPointer<MultiplexChannel>{connection->getChannel(mTransferUuid).release()};
|
2016-11-11 14:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap BluetoothUploadJob::transferInfo() const
|
|
|
|
{
|
|
|
|
QVariantMap ret;
|
2019-06-10 15:40:28 +01:00
|
|
|
ret[QStringLiteral("uuid")] = mTransferUuid.toString().mid(1, 36);
|
2016-11-11 14:55:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothUploadJob::start()
|
|
|
|
{
|
2019-03-08 18:18:01 +00:00
|
|
|
if (!mData->open(QIODevice::ReadOnly)) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "error when opening the input to upload";
|
2022-09-10 22:23:52 +01:00
|
|
|
return; // TODO: Handle error, clean up...
|
2016-11-11 14:55:16 +00:00
|
|
|
}
|
2019-03-08 18:18:01 +00:00
|
|
|
connect(mSocket.data(), &MultiplexChannel::bytesWritten, this, &BluetoothUploadJob::writeSome);
|
|
|
|
connect(mSocket.data(), &MultiplexChannel::aboutToClose, this, &BluetoothUploadJob::closeConnection);
|
2018-04-16 20:57:59 +01:00
|
|
|
writeSome();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void BluetoothUploadJob::writeSome()
|
|
|
|
{
|
2018-04-16 20:57:59 +01:00
|
|
|
bool errorOccurred = false;
|
2019-03-08 18:18:01 +00:00
|
|
|
while (mSocket->bytesToWrite() == 0 && mData->bytesAvailable() && mSocket->isWritable()) {
|
2018-04-16 20:57:59 +01:00
|
|
|
qint64 bytes = qMin<qint64>(mData->bytesAvailable(), 4096);
|
2019-03-08 18:18:01 +00:00
|
|
|
int bytesWritten = mSocket->write(mData->read(bytes));
|
2018-04-16 20:57:59 +01:00
|
|
|
|
|
|
|
if (bytesWritten < 0) {
|
2023-08-04 16:04:15 +01:00
|
|
|
qCWarning(KDECONNECT_CORE) << "error when writing data to bluetooth upload" << bytes << mData->bytesAvailable();
|
2018-04-16 20:57:59 +01:00
|
|
|
errorOccurred = true;
|
2016-11-11 14:55:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:57:59 +01:00
|
|
|
if (mData->atEnd() || errorOccurred) {
|
|
|
|
mData->close();
|
2019-03-08 18:18:01 +00:00
|
|
|
mSocket->close();
|
2018-04-16 20:57:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void BluetoothUploadJob::closeConnection()
|
|
|
|
{
|
2016-11-11 14:55:16 +00:00
|
|
|
mData->close();
|
|
|
|
deleteLater();
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_bluetoothuploadjob.cpp"
|