[OpenDocString] kdeconnect-kde (cpp)
bluetoothuploadjob.cpp
BluetoothUploadJob::BluetoothUploadJob(const QSharedPointer &data, ConnectionMultiplexer *connection, QObject *parent)
    : QObject(parent)
    , mData(data)
    , mTransferUuid(connection->newChannel())
{
    mSocket = QSharedPointer{connection->getChannel(mTransferUuid).release()};
}
This creates a new socket and transfers the data to it. It takes the data, the connection and the parent object, and a uuid.
QVariantMap BluetoothUploadJob::transferInfo() const
{
    QVariantMap ret;
    ret[QStringLiteral("uuid")] = mTransferUuid.toString().mid(1, 36);
    return ret;
}
Returns a map of transfer uuid and data.
void BluetoothUploadJob::start()
{
    if (!mData->open(QIODevice::ReadOnly)) {
        qCWarning(KDECONNECT_CORE) << "error when opening the input to upload";
        return; // TODO: Handle error, clean up...
    }
    connect(mSocket.data(), &MultiplexChannel::bytesWritten, this, &BluetoothUploadJob::writeSome);
    connect(mSocket.data(), &MultiplexChannel::aboutToClose, this, &BluetoothUploadJob::closeConnection);
    writeSome();
}
This starts the upload job. It opens the input stream, writes the data to the socket and sets up signal/slot connections for the writeSome method to handle the writeSome method. It also connects the socket to the aboutToClose signal and the closeConnection method to handle the close.
void BluetoothUploadJob::writeSome()
{
    bool errorOccurred = false;
    while (mSocket->bytesToWrite() == 0 && mData->bytesAvailable() && mSocket->isWritable()) {
        qint64 bytes = qMin(mData->bytesAvailable(), 4096);
        int bytesWritten = mSocket->write(mData->read(bytes));

        if (bytesWritten < 0) {
            qCWarning(KDECONNECT_CORE()) << "error when writing data to bluetooth upload" << bytes << mData->bytesAvailable();
            errorOccurred = true;
            break;
        }
    }

    if (mData->atEnd() || errorOccurred) {
        mData->close();
        mSocket->close();
    }
}
This writes all available bytes from the internal data buffer, to the bluetooth upload socket, and closes the socket if it is not writable. It logs a warning if the write times out.
void BluetoothUploadJob::closeConnection()
{
    mData->close();
    deleteLater();
}
This closes the connection to the device and deletes the later.