File indexing completed on 2024-12-22 05:01:09
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 SPDX-License-Identifier: GPL-2.0-only 0004 */ 0005 0006 #include "historyclosedreadermenu.h" 0007 #include "historyclosedreadermanager.h" 0008 #include "kmail_debug.h" 0009 #include <KLocalizedString> 0010 #include <QMenu> 0011 0012 HistoryClosedReaderMenu::HistoryClosedReaderMenu(QObject *parent) 0013 : KActionMenu{parent} 0014 { 0015 setText(i18nc("List of message viewer closed", "Closed Reader")); 0016 delete menu(); 0017 auto subMenu = new QMenu; 0018 setMenu(subMenu); 0019 connect(HistoryClosedReaderManager::self(), &HistoryClosedReaderManager::historyClosedReaderChanged, this, &HistoryClosedReaderMenu::updateMenu); 0020 } 0021 0022 HistoryClosedReaderMenu::~HistoryClosedReaderMenu() = default; 0023 0024 void HistoryClosedReaderMenu::slotClear() 0025 { 0026 HistoryClosedReaderManager::self()->clear(); 0027 } 0028 0029 void HistoryClosedReaderMenu::updateMenu() 0030 { 0031 if (mReopenAction) { 0032 menu()->removeAction(mReopenAction); 0033 } 0034 if (mSeparatorAction) { 0035 menu()->removeAction(mSeparatorAction); 0036 } 0037 menu()->clear(); 0038 const QList<HistoryClosedReaderInfo> list = HistoryClosedReaderManager::self()->closedReaderInfos(); 0039 if (!list.isEmpty()) { 0040 addReOpenClosedAction(); 0041 for (const auto &info : list) { 0042 QString subject = info.subject(); 0043 const QString originalSubject{subject}; 0044 if (subject.length() > 61) { 0045 subject = subject.first(60) + QStringLiteral("..."); 0046 } 0047 auto action = new QAction(subject, menu()); 0048 action->setToolTip(originalSubject); 0049 connect(action, &QAction::triggered, this, [this, info]() { 0050 const auto identifier = info.item(); 0051 Q_EMIT openMessage(identifier); 0052 HistoryClosedReaderManager::self()->removeItem(identifier); 0053 }); 0054 menu()->addAction(action); 0055 } 0056 menu()->addSeparator(); 0057 auto clearAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear-history")), i18n("Clear History"), menu()); 0058 connect(clearAction, &QAction::triggered, this, &HistoryClosedReaderMenu::slotClear); 0059 menu()->addAction(clearAction); 0060 } 0061 } 0062 0063 void HistoryClosedReaderMenu::slotReopenLastClosedViewer() 0064 { 0065 const QList<HistoryClosedReaderInfo> list = HistoryClosedReaderManager::self()->closedReaderInfos(); 0066 if (!list.isEmpty()) { 0067 const auto identifier = list.constFirst().item(); 0068 Q_EMIT openMessage(identifier); 0069 HistoryClosedReaderManager::self()->removeItem(identifier); 0070 } 0071 } 0072 0073 void HistoryClosedReaderMenu::createReOpenClosedAction() 0074 { 0075 if (!mReopenAction) { 0076 mReopenAction = new QAction(i18n("Reopen Closed Viewer"), this); 0077 menu()->addAction(mReopenAction); 0078 connect(mReopenAction, &QAction::triggered, this, &HistoryClosedReaderMenu::slotReopenLastClosedViewer); 0079 0080 mSeparatorAction = new QAction(this); 0081 mSeparatorAction->setSeparator(true); 0082 } 0083 } 0084 0085 void HistoryClosedReaderMenu::addReOpenClosedAction() 0086 { 0087 if (!mReopenAction) { 0088 qCWarning(KMAIL_LOG) << "mReopenAction was not created ! It's a bug"; 0089 return; 0090 } 0091 menu()->addAction(mReopenAction); 0092 menu()->addAction(mSeparatorAction); 0093 } 0094 0095 QAction *HistoryClosedReaderMenu::reopenAction() const 0096 { 0097 return mReopenAction; 0098 } 0099 0100 #include "moc_historyclosedreadermenu.cpp"