File indexing completed on 2024-04-28 05:35:28

0001 /*
0002     SPDX-FileCopyrightText: Andrew Stanley-Jones <asj@cban.com>
0003     SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk>
0004     SPDX-FileCopyrightText: 2008 Dmitry Suzdalev <dimsuz@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 #pragma once
0009 
0010 #include "config-klipper.h"
0011 
0012 #include <QClipboard>
0013 #include <QElapsedTimer>
0014 #include <QPointer>
0015 #include <QTimer>
0016 
0017 #include <KTextEdit>
0018 
0019 #include "urlgrabber.h"
0020 
0021 class KToggleAction;
0022 class KActionCollection;
0023 class KlipperPopup;
0024 class URLGrabber;
0025 class QTime;
0026 class History;
0027 class QAction;
0028 class QMenu;
0029 class QMimeData;
0030 class HistoryItem;
0031 class KNotification;
0032 class KSystemClipboard;
0033 
0034 namespace KWayland
0035 {
0036 namespace Client
0037 {
0038 class PlasmaShell;
0039 }
0040 }
0041 
0042 class Klipper : public QObject
0043 {
0044     Q_OBJECT
0045     Q_CLASSINFO("D-Bus Interface", "org.kde.klipper.klipper")
0046 
0047 public Q_SLOTS:
0048     Q_SCRIPTABLE QString getClipboardContents();
0049     Q_SCRIPTABLE void setClipboardContents(const QString &s);
0050     Q_SCRIPTABLE void clearClipboardContents();
0051     Q_SCRIPTABLE void clearClipboardHistory();
0052     Q_SCRIPTABLE void saveClipboardHistory();
0053     Q_SCRIPTABLE QStringList getClipboardHistoryMenu();
0054     Q_SCRIPTABLE QString getClipboardHistoryItem(int i);
0055     Q_SCRIPTABLE void showKlipperPopupMenu();
0056     Q_SCRIPTABLE void showKlipperManuallyInvokeActionMenu();
0057 
0058 public:
0059     Klipper(QObject *parent, const KSharedConfigPtr &config);
0060     ~Klipper() override;
0061 
0062     bool eventFilter(QObject *object, QEvent *event) override;
0063 
0064     /**
0065      * Get clipboard history (the "document")
0066      */
0067     History *history()
0068     {
0069         return m_history;
0070     }
0071 
0072     URLGrabber *urlGrabber() const
0073     {
0074         return m_myURLGrabber;
0075     }
0076 
0077     void saveSettings() const;
0078 
0079     QMenu *actionsPopup() const
0080     {
0081         return m_actionsPopup;
0082     }
0083 
0084     KlipperPopup *popup()
0085     {
0086         return m_popup;
0087     }
0088 
0089     void showBarcode(std::shared_ptr<const HistoryItem> item);
0090 
0091 public Q_SLOTS:
0092     void saveSession();
0093     void slotHistoryTopChanged();
0094     void slotConfigure();
0095     void slotCycleNext();
0096     void slotCyclePrev();
0097 
0098 protected:
0099     /**
0100      * The selection modes
0101      *
0102      * Don't use 1, as I use that as a guard against passing
0103      * a boolean true as a mode.
0104      */
0105     enum SelectionMode {
0106         Clipboard = 2,
0107         Selection = 4,
0108     };
0109     enum class ClipboardUpdateReason {
0110         UpdateClipboard,
0111         PreventEmptyClipboard,
0112     };
0113 
0114     /**
0115      * Loads history from disk.
0116      */
0117     bool loadHistory();
0118 
0119     /**
0120      * Save history to disk
0121      * @param empty save empty history instead of actual history
0122      * @return whether saving was successful
0123      */
0124     bool saveHistory(bool empty = false);
0125 
0126     /**
0127      * Check data in clipboard, and if it passes these checks,
0128      * store the data in the clipboard history.
0129      */
0130     void checkClipData(bool selectionMode);
0131 
0132     /**
0133      * Enter clipboard data in the history.
0134      */
0135     std::shared_ptr<HistoryItem> applyClipChanges(const QMimeData *data, bool selectionMode);
0136 
0137     void setClipboard(const HistoryItem &item, int mode, ClipboardUpdateReason updateReason = ClipboardUpdateReason::UpdateClipboard);
0138     bool ignoreClipboardChanges() const;
0139 
0140     KSharedConfigPtr config() const
0141     {
0142         return m_config;
0143     }
0144 
0145 Q_SIGNALS:
0146     void passivePopup(const QString &caption, const QString &text);
0147     void editFinished(std::shared_ptr<const HistoryItem> item, int result);
0148     Q_SCRIPTABLE void clipboardHistoryUpdated();
0149 
0150 public Q_SLOTS:
0151     void slotPopupMenu();
0152     void slotAskClearHistory();
0153     void setURLGrabberEnabled(bool);
0154 
0155 protected Q_SLOTS:
0156     void showPopupMenu(QMenu *);
0157     void slotRepeatAction();
0158     void disableURLGrabber();
0159 
0160 private Q_SLOTS:
0161     void newClipData(QClipboard::Mode);
0162     void slotClearClipboard();
0163 
0164     void slotHistoryChanged();
0165 
0166     void slotQuit();
0167     void slotStartShowTimer();
0168 
0169     void slotClearOverflow();
0170     void slotCheckPending();
0171 
0172     void loadSettings();
0173 
0174 private:
0175     static void updateTimestamp();
0176 
0177     KSystemClipboard *m_clip;
0178 
0179     QElapsedTimer m_showTimer;
0180 
0181     History *m_history;
0182     KlipperPopup *m_popup;
0183     int m_overflowCounter;
0184 
0185     KToggleAction *m_toggleURLGrabAction;
0186     QAction *m_clearHistoryAction;
0187     QAction *m_repeatAction;
0188     QAction *m_showBarcodeAction;
0189     QAction *m_configureAction;
0190     QAction *m_quitAction;
0191     QAction *m_cycleNextAction;
0192     QAction *m_cyclePrevAction;
0193     QAction *m_showOnMousePos;
0194 
0195     bool m_bKeepContents : 1;
0196     bool m_bURLGrabber : 1;
0197     bool m_bReplayActionInHistory : 1;
0198     bool m_bUseGUIRegExpEditor : 1;
0199     bool m_bNoNullClipboard : 1;
0200     bool m_bIgnoreSelection : 1;
0201     bool m_bSynchronize : 1;
0202     bool m_bSelectionTextOnly : 1;
0203     bool m_bIgnoreImages : 1;
0204 
0205     /**
0206      * Avoid reacting to our own changes, using this
0207      * lock.
0208      * Don't manupulate this object directly... use the Ignore struct
0209      * instead
0210      */
0211     int m_selectionLocklevel;
0212     int m_clipboardLocklevel;
0213 
0214     URLGrabber *m_myURLGrabber;
0215     QString m_lastURLGrabberTextSelection;
0216     QString m_lastURLGrabberTextClipboard;
0217     KSharedConfigPtr m_config;
0218     QTimer m_overflowClearTimer;
0219     QTimer m_pendingCheckTimer;
0220     bool m_pendingContentsCheck;
0221 
0222     bool blockFetchingNewData();
0223     QString cycleText() const;
0224     KActionCollection *m_collection;
0225     QMenu *m_actionsPopup;
0226     QTimer *m_saveFileTimer;
0227     QPointer<KNotification> m_notification;
0228     KWayland::Client::PlasmaShell *m_plasmashell;
0229 };