File indexing completed on 2024-04-28 17:06:05

0001 /*
0002     SPDX-FileCopyrightText: 2005 Csaba Karai <cskarai@freemail.hu>
0003     SPDX-FileCopyrightText: 2005-2022 Krusader Krew <https://krusader.org>
0004 
0005     Based on KrRemoteEncodingMenu (© Csaba Karai and Krusader Krew)
0006     from Krusader, DolphinRecentTabsMenu (© Emmanuel Pescosta)
0007     from Dolphin and PanelManager::slotDuplicateTab() (© Shie Erlich,
0008     Rafi Yanai and Krusader Krew) from Krusader.
0009 
0010     SPDX-License-Identifier: GPL-2.0-or-later
0011 */
0012 
0013 #include "recentlyclosedtabsmenu.h"
0014 
0015 #include "../Panel/PanelView/krview.h"
0016 #include "../Panel/listpanel.h"
0017 #include "../icon.h"
0018 #include "../kractions.h"
0019 #include "../krglobal.h"
0020 #include "../krusaderview.h"
0021 #include "../panelmanager.h"
0022 #include "../tabactions.h"
0023 
0024 #include <KConfigCore/KConfig>
0025 #include <KI18n/KLocalizedString>
0026 #include <KIO/Global>
0027 #include <KXmlGui/KActionCollection>
0028 
0029 // QtWidgets
0030 #include <QMenu>
0031 
0032 RecentlyClosedTabsMenu::RecentlyClosedTabsMenu(const QString &text, const QString &iconName, KActionCollection *parent)
0033     : KActionMenu(Icon(iconName), text, parent)
0034 {
0035     parent->addAction("recently_closed_tabs", this);
0036 
0037     // Construct the Empty Recently Closed Tabs action and add it to the menu
0038     actClearTheList = new QAction(i18n("Empty Recently Closed Tabs"), this);
0039     actClearTheList->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-list")));
0040     addAction(actClearTheList);
0041     // Add a separator after that menu entry
0042     addSeparator();
0043 
0044     connect(menu(), &QMenu::triggered, this, &RecentlyClosedTabsMenu::slotTriggered);
0045 }
0046 
0047 QAction *RecentlyClosedTabsMenu::updateAfterClosingATab(const QUrl &urlClosedTab, const QByteArray &backedUpData, TabActions *argTabActions)
0048 {
0049     // Create the related action
0050     QAction *actReopenTab = new QAction(menu());
0051     actReopenTab->setText(urlClosedTab.path());
0052     actReopenTab->setData(backedUpData);
0053     const QString iconName = KIO::iconNameForUrl(urlClosedTab);
0054     actReopenTab->setIcon(QIcon::fromTheme(iconName));
0055 
0056     // Add a menu entry (related to the closed tab) after the
0057     // fixed menu entries
0058     if (menu()->actions().size() == quantFixedMenuEntries) {
0059         addAction(actReopenTab);
0060     } else {
0061         insertAction(menu()->actions().at(quantFixedMenuEntries), actReopenTab);
0062     }
0063 
0064     // Remove the last entry of the menu if the limit is exceeded
0065     if (menu()->actions().size() > quantFixedMenuEntries + maxClosedTabs) {
0066         ACTIVE_MNG->delClosedTab(menu()->actions().last());
0067     }
0068 
0069     // Enable objects
0070     setEnabled(true);
0071     tabActions = argTabActions;
0072     tabActions->actUndoCloseTab->setEnabled(true);
0073 
0074     return actReopenTab;
0075 }
0076 
0077 void RecentlyClosedTabsMenu::slotTriggered(QAction *action)
0078 {
0079     if (!action)
0080         return;
0081 
0082     if (action == actClearTheList) {
0083         ACTIVE_MNG->delAllClosedTabs();
0084     } else {
0085         ACTIVE_MNG->undoCloseTab(action);
0086 
0087         removeAction(action);
0088         delete action;
0089         action = nullptr;
0090     }
0091 
0092     if (menu()->actions().size() <= quantFixedMenuEntries) {
0093         // Disable objects
0094         tabActions->actUndoCloseTab->setEnabled(false);
0095         setEnabled(false);
0096     }
0097 }