File indexing completed on 2024-04-21 14:54:20

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
0004     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0005     SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
0006     SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
0007     SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
0008     SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
0009     SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
0010     SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
0011     SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
0012     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0013 
0014     SPDX-License-Identifier: LGPL-2.0-only
0015 */
0016 
0017 #include "kpastetextaction.h"
0018 
0019 #include <QApplication>
0020 #include <QClipboard>
0021 #include <QDBusInterface>
0022 #include <QDBusReply>
0023 #include <QMenu>
0024 
0025 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 39)
0026 
0027 class KPasteTextActionPrivate
0028 {
0029 public:
0030     KPasteTextActionPrivate(KPasteTextAction *parent)
0031         : q(parent)
0032     {
0033     }
0034 
0035     ~KPasteTextActionPrivate()
0036     {
0037         delete m_popup;
0038     }
0039 
0040     void menuAboutToShow();
0041     void slotTriggered(QAction *action);
0042 
0043     void init();
0044 
0045     KPasteTextAction *const q;
0046     QMenu *m_popup = nullptr;
0047     bool m_mixedMode;
0048 };
0049 
0050 KPasteTextAction::KPasteTextAction(QObject *parent)
0051     : QAction(parent)
0052     , d(new KPasteTextActionPrivate(this))
0053 {
0054     d->init();
0055 }
0056 
0057 KPasteTextAction::KPasteTextAction(const QString &text, QObject *parent)
0058     : QAction(parent)
0059     , d(new KPasteTextActionPrivate(this))
0060 {
0061     d->init();
0062     setText(text);
0063 }
0064 
0065 KPasteTextAction::KPasteTextAction(const QIcon &icon, const QString &text, QObject *parent)
0066     : QAction(icon, text, parent)
0067     , d(new KPasteTextActionPrivate(this))
0068 {
0069     d->init();
0070 }
0071 
0072 void KPasteTextActionPrivate::init()
0073 {
0074     m_popup = new QMenu;
0075     q->connect(m_popup, &QMenu::aboutToShow, q, [this]() {
0076         menuAboutToShow();
0077     });
0078     q->connect(m_popup, &QMenu::triggered, q, [this](QAction *action) {
0079         slotTriggered(action);
0080     });
0081     m_mixedMode = true;
0082 }
0083 
0084 KPasteTextAction::~KPasteTextAction() = default;
0085 
0086 void KPasteTextAction::setMixedMode(bool mode)
0087 {
0088     d->m_mixedMode = mode;
0089 }
0090 
0091 void KPasteTextActionPrivate::menuAboutToShow()
0092 {
0093     m_popup->clear();
0094     QStringList list;
0095     QDBusInterface klipper(QStringLiteral("org.kde.klipper"), QStringLiteral("/klipper"), QStringLiteral("org.kde.klipper.klipper"));
0096     if (klipper.isValid()) {
0097         QDBusReply<QStringList> reply = klipper.call(QStringLiteral("getClipboardHistoryMenu"));
0098         if (reply.isValid()) {
0099             list = reply;
0100         }
0101     }
0102     QString clipboardText = qApp->clipboard()->text(QClipboard::Clipboard);
0103     if (list.isEmpty()) {
0104         list << clipboardText;
0105     }
0106     bool found = false;
0107     const QFontMetrics fm = m_popup->fontMetrics();
0108     for (const QString &string : std::as_const(list)) {
0109         QString text = fm.elidedText(string.simplified(), Qt::ElideMiddle, fm.maxWidth() * 20);
0110         text.replace(QLatin1Char('&'), QLatin1String("&&"));
0111         QAction *action = m_popup->addAction(text);
0112         if (!found && string == clipboardText) {
0113             action->setChecked(true);
0114             found = true;
0115         }
0116     }
0117 }
0118 
0119 void KPasteTextActionPrivate::slotTriggered(QAction *action)
0120 {
0121     QDBusInterface klipper(QStringLiteral("org.kde.klipper"), QStringLiteral("/klipper"), QStringLiteral("org.kde.klipper.klipper"));
0122     if (klipper.isValid()) {
0123         QDBusReply<QString> reply = klipper.call(QStringLiteral("getClipboardHistoryItem"), m_popup->actions().indexOf(action));
0124         if (!reply.isValid()) {
0125             return;
0126         }
0127         QString clipboardText = reply;
0128         reply = klipper.call(QStringLiteral("setClipboardContents"), clipboardText);
0129         // if (reply.isValid())
0130         //  qCDebug(KCONFIG_WIDGETS_LOG) << "Clipboard: " << qApp->clipboard()->text(QClipboard::Clipboard);
0131     }
0132 }
0133 
0134 #include "moc_kpastetextaction.cpp"
0135 
0136 #endif // KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE