File: kpastetextaction.cpp

package info (click to toggle)
kconfigwidgets 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,220 kB
  • sloc: cpp: 8,164; perl: 43; sh: 21; makefile: 7
file content (136 lines) | stat: -rw-r--r-- 4,063 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
    This file is part of the KDE libraries
    SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
    SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
    SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
    SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
    SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
    SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
    SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
    SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
    SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
    SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "kpastetextaction.h"

#include <QApplication>
#include <QClipboard>
#include <QDBusInterface>
#include <QDBusReply>
#include <QMenu>

#if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 39)

class KPasteTextActionPrivate
{
public:
    KPasteTextActionPrivate(KPasteTextAction *parent)
        : q(parent)
    {
    }

    ~KPasteTextActionPrivate()
    {
        delete m_popup;
    }

    void menuAboutToShow();
    void slotTriggered(QAction *action);

    void init();

    KPasteTextAction *const q;
    QMenu *m_popup = nullptr;
    bool m_mixedMode;
};

KPasteTextAction::KPasteTextAction(QObject *parent)
    : QAction(parent)
    , d(new KPasteTextActionPrivate(this))
{
    d->init();
}

KPasteTextAction::KPasteTextAction(const QString &text, QObject *parent)
    : QAction(parent)
    , d(new KPasteTextActionPrivate(this))
{
    d->init();
    setText(text);
}

KPasteTextAction::KPasteTextAction(const QIcon &icon, const QString &text, QObject *parent)
    : QAction(icon, text, parent)
    , d(new KPasteTextActionPrivate(this))
{
    d->init();
}

void KPasteTextActionPrivate::init()
{
    m_popup = new QMenu;
    q->connect(m_popup, &QMenu::aboutToShow, q, [this]() {
        menuAboutToShow();
    });
    q->connect(m_popup, &QMenu::triggered, q, [this](QAction *action) {
        slotTriggered(action);
    });
    m_mixedMode = true;
}

KPasteTextAction::~KPasteTextAction() = default;

void KPasteTextAction::setMixedMode(bool mode)
{
    d->m_mixedMode = mode;
}

void KPasteTextActionPrivate::menuAboutToShow()
{
    m_popup->clear();
    QStringList list;
    QDBusInterface klipper(QStringLiteral("org.kde.klipper"), QStringLiteral("/klipper"), QStringLiteral("org.kde.klipper.klipper"));
    if (klipper.isValid()) {
        QDBusReply<QStringList> reply = klipper.call(QStringLiteral("getClipboardHistoryMenu"));
        if (reply.isValid()) {
            list = reply;
        }
    }
    QString clipboardText = qApp->clipboard()->text(QClipboard::Clipboard);
    if (list.isEmpty()) {
        list << clipboardText;
    }
    bool found = false;
    const QFontMetrics fm = m_popup->fontMetrics();
    for (const QString &string : std::as_const(list)) {
        QString text = fm.elidedText(string.simplified(), Qt::ElideMiddle, fm.maxWidth() * 20);
        text.replace(QLatin1Char('&'), QLatin1String("&&"));
        QAction *action = m_popup->addAction(text);
        if (!found && string == clipboardText) {
            action->setChecked(true);
            found = true;
        }
    }
}

void KPasteTextActionPrivate::slotTriggered(QAction *action)
{
    QDBusInterface klipper(QStringLiteral("org.kde.klipper"), QStringLiteral("/klipper"), QStringLiteral("org.kde.klipper.klipper"));
    if (klipper.isValid()) {
        QDBusReply<QString> reply = klipper.call(QStringLiteral("getClipboardHistoryItem"), m_popup->actions().indexOf(action));
        if (!reply.isValid()) {
            return;
        }
        QString clipboardText = reply;
        reply = klipper.call(QStringLiteral("setClipboardContents"), clipboardText);
        // if (reply.isValid())
        //  qCDebug(KCONFIG_WIDGETS_LOG) << "Clipboard: " << qApp->clipboard()->text(QClipboard::Clipboard);
    }
}

#include "moc_kpastetextaction.cpp"

#endif // KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE