File indexing completed on 2024-04-21 16:30:15
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "dolphinrecenttabsmenu.h" 0008 #include "search/dolphinquery.h" 0009 0010 #include <KAcceleratorManager> 0011 #include <KLocalizedString> 0012 #include <kio/global.h> 0013 0014 #include <QMenu> 0015 #include <QUrlQuery> 0016 0017 DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject *parent) 0018 : KActionMenu(QIcon::fromTheme(QStringLiteral("edit-undo")), i18n("Recently Closed Tabs"), parent) 0019 { 0020 setPopupMode(QToolButton::InstantPopup); 0021 setEnabled(false); 0022 0023 m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this); 0024 m_clearListAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-history"))); 0025 addAction(m_clearListAction); 0026 0027 addSeparator(); 0028 0029 connect(menu(), &QMenu::triggered, this, &DolphinRecentTabsMenu::handleAction); 0030 } 0031 0032 void DolphinRecentTabsMenu::rememberClosedTab(const QUrl &url, const QByteArray &state) 0033 { 0034 QAction *action = new QAction(menu()); 0035 if (DolphinQuery::supportsScheme(url.scheme())) { 0036 const DolphinQuery query = DolphinQuery::fromSearchUrl(url); 0037 action->setText(i18n("Search for %1 in %2", query.text(), query.includeFolder())); 0038 } else if (url.scheme() == QLatin1String("filenamesearch")) { 0039 const QUrlQuery query(url); 0040 action->setText(i18n("Search for %1 in %2", query.queryItemValue(QStringLiteral("search")), query.queryItemValue(QStringLiteral("url")))); 0041 } else { 0042 action->setText(url.path()); 0043 } 0044 action->setData(state); 0045 const QString iconName = KIO::iconNameForUrl(url); 0046 action->setIcon(QIcon::fromTheme(iconName)); 0047 0048 // Add the closed tab menu entry after the separator and 0049 // "Empty Recently Closed Tabs" entry 0050 if (menu()->actions().size() == 2) { 0051 addAction(action); 0052 } else { 0053 insertAction(menu()->actions().at(2), action); 0054 } 0055 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2); 0056 // Assure that only up to 6 closed tabs are shown in the menu. 0057 // 8 because of clear action + separator + 6 closed tabs 0058 if (menu()->actions().size() > 8) { 0059 removeAction(menu()->actions().last()); 0060 } 0061 setEnabled(true); 0062 KAcceleratorManager::manage(menu()); 0063 } 0064 0065 void DolphinRecentTabsMenu::undoCloseTab() 0066 { 0067 Q_ASSERT(menu()->actions().size() > 2); 0068 handleAction(menu()->actions().at(2)); 0069 } 0070 0071 void DolphinRecentTabsMenu::handleAction(QAction *action) 0072 { 0073 if (action == m_clearListAction) { 0074 // Clear all actions except the "Empty Recently Closed Tabs" 0075 // action and the separator 0076 QList<QAction *> actions = menu()->actions(); 0077 const int count = actions.size(); 0078 for (int i = count - 1; i >= 2; i--) { 0079 removeAction(actions.at(i)); 0080 } 0081 Q_EMIT closedTabsCountChanged(0); 0082 } else { 0083 const QByteArray state = action->data().toByteArray(); 0084 removeAction(action); 0085 delete action; 0086 action = nullptr; 0087 Q_EMIT restoreClosedTab(state); 0088 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2); 0089 } 0090 0091 if (menu()->actions().count() <= 2) { 0092 setEnabled(false); 0093 } 0094 } 0095 0096 #include "moc_dolphinrecenttabsmenu.cpp"