[OpenDocString] kdeconnect-kde (cpp)
devicelinereader.cpp
DeviceLineReader::DeviceLineReader(QIODevice *device, QObject *parent)
    : QObject(parent)
    , m_device(device)
{
    connect(m_device, &QIODevice::readyRead, this, &DeviceLineReader::dataReceived);
    connect(m_device, &QIODevice::aboutToClose, this, &DeviceLineReader::disconnected);
}
This constructor builds a device object from an existing device object, and its duplex connections. It connects the readyRead and aboutToClose methods to the device object and sets up signal/slot connections for data received and aboutToClose.
void DeviceLineReader::dataReceived()
{
    while (m_device->canReadLine()) {
        const QByteArray line = m_device->readLine();
        if (line.length() > 1) {
            m_packets.enqueue(line); // we don't want single \n
        }
    }

    // If we still have things to read from the device, call dataReceived again
    // We do this manually because we do not trust readyRead to be emitted again
    // So we call this method again just in case.
    if (m_device->bytesAvailable() > 0) {
        QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
        return;
    }

    // If we have any packets, tell it to the world.
    if (!m_packets.isEmpty()) {
        Q_EMIT readyRead();
    }
}
This reads from the device until it finds a line of data, adds it to the internal list of packets, and emits a readyRead signal if it doesn't have any packets.