[OpenDocString] kdeconnect-kde (cpp)
uploadjob.cpp
UploadJob::UploadJob(const NetworkPacket &networkPacket)
    : KJob()
    , m_networkPacket(networkPacket)
    , m_input(networkPacket.payload())
    , m_socket(nullptr)
{
}
This creates a upload job object from a given network packet. It takes the payload of the network packet and processes it.
void UploadJob::setSocket(QSslSocket *socket)
{
    m_socket = socket;
    m_socket->setParent(this);
}
Sets the socket object within this upload job. The socket object is created only once and sets its parent to this upload job.
void UploadJob::start()
{
    if (!m_input->open(QIODevice::ReadOnly)) {
        qCWarning(KDECONNECT_CORE) << "error when opening the input to upload";
        return; // TODO: Handle error, clean up...
    }

    if (!m_socket) {
        qCWarning(KDECONNECT_CORE) << "you must call setSocket() before calling start()";
        return;
    }

    connect(m_input.data(), &QIODevice::aboutToClose, this, &UploadJob::aboutToClose);

    bytesUploaded = 0;
    setProcessedAmount(Bytes, bytesUploaded);

    connect(m_socket, &QSslSocket::encryptedBytesWritten, this, &UploadJob::encryptedBytesWritten);

    uploadNextPacket();
}
This starts the upload job. It first opens the input device, sets up connections for the socket and starts the upload process. It also connects the encrypted bytes written to the socket to the upload job's data channel. It also connects the socket to the input data channel and sets the processed amount to the processed amount.
void UploadJob::uploadNextPacket()
{
    qint64 bytesAvailable = m_input->bytesAvailable();

    if (bytesAvailable > 0) {
        qint64 bytesToSend = qMin(m_input->bytesAvailable(), (qint64)4096);
        bytesUploading = m_socket->write(m_input->read(bytesToSend));
    }

    if (bytesAvailable <= 0 || bytesUploading < 0) {
        m_input->close();
        disconnect(m_socket, &QSslSocket::encryptedBytesWritten, this, &UploadJob::encryptedBytesWritten);
    }
}
This sends the next packet from the input buffer to the socket if it is available. It closes the socket when the socket is not available or the number of bytes are not enough. It also disconnects the socket when the socket is not available.
void UploadJob::encryptedBytesWritten(qint64 bytes)
{
    Q_UNUSED(bytes);

    if (m_socket->encryptedBytesToWrite() == 0) {
        bytesUploaded += bytesUploading;
        setProcessedAmount(Bytes, bytesUploaded);

        uploadNextPacket();
    }
}
This adds the number of bytes written to the socket and updates the bytes processed count. It also updates the bytesUploaded value in the processedAmount member variable.
void UploadJob::aboutToClose()
{
    m_socket->disconnectFromHost();
    emitResult();
}
This disconnects the socket from the host and emits the result.
bool UploadJob::stop()
{
    m_input->close();

    return true;
}
This closes the input stream, and stops the upload job. It returns true on the first successful attempt.
const NetworkPacket UploadJob::getNetworkPacket()
{
    return m_networkPacket;
}
Returns a copy of the network packet object, which uses implicit sharing for its data.