File indexing completed on 2024-04-28 16:54:26
0001 /* 0002 0003 SPDX-FileCopyrightText: Andrew Stanley-Jones <asj@cban.com> 0004 SPDX-FileCopyrightText: 2000 Carsten Pfeiffer <pfeiffer@kde.org> 0005 SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "tray.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include "history.h" 0015 #include "historyitem.h" 0016 #include "klipper.h" 0017 #include "klipperpopup.h" 0018 0019 KlipperTray::KlipperTray() 0020 : KStatusNotifierItem() 0021 { 0022 setTitle(i18n("Klipper")); 0023 const QString klipperIconName = QStringLiteral("klipper"); 0024 setIconByName(klipperIconName); 0025 setToolTip(klipperIconName, i18n("Clipboard Contents"), i18n("Clipboard is empty")); 0026 setCategory(SystemServices); 0027 setStatus(Active); 0028 setStandardActionsEnabled(false); 0029 0030 m_klipper = new Klipper(this, KSharedConfig::openConfig()); 0031 setContextMenu(m_klipper->actionsPopup()); 0032 setAssociatedWidget(m_klipper->popup()); 0033 connect(m_klipper->history(), &History::changed, this, &KlipperTray::slotSetToolTipFromHistory); 0034 slotSetToolTipFromHistory(); 0035 } 0036 0037 KlipperTray::~KlipperTray() 0038 { 0039 // Klipper abuses the KStatusNotifierItem slightly by setting both 0040 // the associated widget and the context menu to the same widget, 0041 // the KlipperPopup. This is done so that either a left click or a 0042 // right click on the icon brings up the combined menu. Unfortunately 0043 // this causes a crash in ~KStatusNotifierItem() when it first 0044 // deletes the menu and then tries to disconnect the associated widget. 0045 // Work around this by resetting the associated widget first. 0046 setAssociatedWidget(nullptr); 0047 } 0048 0049 void KlipperTray::slotSetToolTipFromHistory() 0050 { 0051 const int TOOLTIP_LENGTH_LIMIT = 200; 0052 if (m_klipper->history()->empty()) { 0053 setToolTipSubTitle(i18n("Clipboard is empty")); 0054 } else { 0055 HistoryItemConstPtr top = m_klipper->history()->first(); 0056 if (top->text().length() <= TOOLTIP_LENGTH_LIMIT) { 0057 setToolTipSubTitle(top->text()); 0058 } else { 0059 setToolTipSubTitle(top->text().left(TOOLTIP_LENGTH_LIMIT - 1) + QStringLiteral("…")); 0060 } 0061 } 0062 }