1 /* 2 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com> 3 * 4 * SPDX-License-Identifier: GPL-2.0-or-later 5 */ 6 7 #include "dolphinrecenttabsmenu.h" 8 9 #include <KAcceleratorManager> 10 #include <KLocalizedString> 11 #include <kio/global.h> 12 13 #include <QMenu> 14 DolphinRecentTabsMenu(QObject * parent)15DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject* parent) : 16 KActionMenu(QIcon::fromTheme(QStringLiteral("edit-undo")), i18n("Recently Closed Tabs"), parent) 17 { 18 setPopupMode(QToolButton::InstantPopup); 19 setEnabled(false); 20 21 m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this); 22 m_clearListAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-list"))); 23 addAction(m_clearListAction); 24 25 addSeparator(); 26 27 connect(menu(), &QMenu::triggered, 28 this, &DolphinRecentTabsMenu::handleAction); 29 } 30 rememberClosedTab(const QUrl & url,const QByteArray & state)31void DolphinRecentTabsMenu::rememberClosedTab(const QUrl& url, const QByteArray& state) 32 { 33 QAction* action = new QAction(menu()); 34 action->setText(url.path()); 35 action->setData(state); 36 const QString iconName = KIO::iconNameForUrl(url); 37 action->setIcon(QIcon::fromTheme(iconName)); 38 39 // Add the closed tab menu entry after the separator and 40 // "Empty Recently Closed Tabs" entry 41 if (menu()->actions().size() == 2) { 42 addAction(action); 43 } else { 44 insertAction(menu()->actions().at(2), action); 45 } 46 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2); 47 // Assure that only up to 6 closed tabs are shown in the menu. 48 // 8 because of clear action + separator + 6 closed tabs 49 if (menu()->actions().size() > 8) { 50 removeAction(menu()->actions().last()); 51 } 52 setEnabled(true); 53 KAcceleratorManager::manage(menu()); 54 } 55 undoCloseTab()56void DolphinRecentTabsMenu::undoCloseTab() 57 { 58 Q_ASSERT(menu()->actions().size() > 2); 59 handleAction(menu()->actions().at(2)); 60 } 61 handleAction(QAction * action)62void DolphinRecentTabsMenu::handleAction(QAction* action) 63 { 64 if (action == m_clearListAction) { 65 // Clear all actions except the "Empty Recently Closed Tabs" 66 // action and the separator 67 QList<QAction*> actions = menu()->actions(); 68 const int count = actions.size(); 69 for (int i = count - 1; i >= 2; i--) { 70 removeAction(actions.at(i)); 71 } 72 Q_EMIT closedTabsCountChanged(0); 73 } else { 74 const QByteArray state = action->data().toByteArray(); 75 removeAction(action); 76 delete action; 77 action = nullptr; 78 Q_EMIT restoreClosedTab(state); 79 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2); 80 } 81 82 if (menu()->actions().count() <= 2) { 83 setEnabled(false); 84 } 85 } 86