[OpenDocString] kdeconnect-kde (cpp)
clipboardlistener.cpp
QString ClipboardListener::currentContent()
{
    return m_currentContent;
}
Returns the current content stored in the clipboard.
ClipboardListener::ClipboardContentType ClipboardListener::currentContentType()
{
    return m_currentContentType;
}
Returns the content type of the current clipboard.
qint64 ClipboardListener::updateTimestamp()
{
    return m_updateTimestamp;
}
Returns the timestamp of the last update.
ClipboardListener *ClipboardListener::instance()
{
    static ClipboardListener *me = nullptr;
    if (!me) {
        me = new ClipboardListener();
    }
    return me;
}
This returns the singleton object of the class ClipboardListener. It makes it public and returns a pointer to it.
void ClipboardListener::refreshContent(const QString &content, ClipboardListener::ClipboardContentType contentType)
{
    m_updateTimestamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
    m_currentContent = content;
    m_currentContentType = contentType;
}
This updates the content and content type of the clipboard by storing its timestamp in the m_currentContent and m_currentContentType variables. Finally, it updates the timestamp based on the current time since the epoch.
ClipboardListener::ClipboardListener()
    : clipboard(KSystemClipboard::instance())
{
#ifdef Q_OS_MAC
    connect(&m_clipboardMonitorTimer, &QTimer::timeout, this, [this]() {
        updateClipboard(QClipboard::Clipboard);
    });
    m_clipboardMonitorTimer.start(1000); // Refresh 1s
#endif
    connect(clipboard, &KSystemClipboard::changed, this, &ClipboardListener::updateClipboard);
}
This constructor builds a clipboard listener object and sets up signal/slot connections for the clock and updates the clipboard after a QTimer expires.
void ClipboardListener::updateClipboard(QClipboard::Mode mode)
{
    if (mode != QClipboard::Clipboard) {
        return;
    }

    ClipboardListener::ClipboardContentType contentType = ClipboardListener::ClipboardContentTypeUnknown;
    if (clipboard->mimeData(mode) && clipboard->mimeData(mode)->data(QStringLiteral("x-kde-passwordManagerHint")) == QByteArrayLiteral("secret")) {
        contentType = ClipboardListener::ClipboardContentTypePassword;
    }

    const QString content = clipboard->text(QClipboard::Clipboard);
    if (content == m_currentContent && contentType == m_currentContentType) {
        return;
    }
    refreshContent(content, contentType);
    Q_EMIT clipboardChanged(content, contentType);
}
This updates the clipboard content and its content type by reading it from the internal buffer. It checks if the mime data of the clipboard contains a secret data. If it is the first time it gets the content of the password manager hint, it gets the content of the password manager hint, and it gets the content of the password manager hint, and emits the signal 'clipboardChanged'.
void ClipboardListener::setText(const QString &content)
{
    refreshContent(content, ClipboardListener::ClipboardContentTypeUnknown);
    auto mime = new QMimeData;
    mime->setText(content);
    clipboard->setMimeData(mime, QClipboard::Clipboard);
}
Sets the clipboard content to the given string. It refreshes the content, creates a new mime data object and sets it on the clipboard.