]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinrecenttabsmenu.cpp
Merge remote-tracking branch 'fork/work/zakharafoniam/useful-groups'
[dolphin.git] / src / dolphinrecenttabsmenu.cpp
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 #include "search/dolphinquery.h"
9
10 #include <KAcceleratorManager>
11 #include <KLocalizedString>
12 #include <kio/global.h>
13
14 #include <QMenu>
15 #include <QUrlQuery>
16
17 DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject *parent)
18 : KActionMenu(QIcon::fromTheme(QStringLiteral("edit-undo")), i18n("Recently Closed Tabs"), parent)
19 {
20 setPopupMode(QToolButton::InstantPopup);
21 setEnabled(false);
22
23 m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this);
24 m_clearListAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-history")));
25 addAction(m_clearListAction);
26
27 addSeparator();
28
29 connect(menu(), &QMenu::triggered, this, &DolphinRecentTabsMenu::handleAction);
30 }
31
32 void DolphinRecentTabsMenu::rememberClosedTab(const QUrl &url, const QByteArray &state)
33 {
34 QAction *action = new QAction(menu());
35 if (Search::isSupportedSearchScheme(url.scheme())) {
36 action->setText(Search::DolphinQuery{url, QUrl{}}.title());
37 } else {
38 action->setText(url.path());
39 }
40 action->setData(state);
41 const QString iconName = KIO::iconNameForUrl(url);
42 action->setIcon(QIcon::fromTheme(iconName));
43
44 // Add the closed tab menu entry after the separator and
45 // "Empty Recently Closed Tabs" entry
46 if (menu()->actions().size() == 2) {
47 addAction(action);
48 } else {
49 insertAction(menu()->actions().at(2), action);
50 }
51 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2);
52 // Assure that only up to 6 closed tabs are shown in the menu.
53 // 8 because of clear action + separator + 6 closed tabs
54 if (menu()->actions().size() > 8) {
55 removeAction(menu()->actions().last());
56 }
57 setEnabled(true);
58 KAcceleratorManager::manage(menu());
59 }
60
61 void DolphinRecentTabsMenu::undoCloseTab()
62 {
63 Q_ASSERT(menu()->actions().size() > 2);
64 handleAction(menu()->actions().at(2));
65 }
66
67 void DolphinRecentTabsMenu::handleAction(QAction *action)
68 {
69 if (action == m_clearListAction) {
70 // Clear all actions except the "Empty Recently Closed Tabs"
71 // action and the separator
72 QList<QAction *> actions = menu()->actions();
73 const int count = actions.size();
74 for (int i = count - 1; i >= 2; i--) {
75 removeAction(actions.at(i));
76 }
77 Q_EMIT closedTabsCountChanged(0);
78 } else {
79 const QByteArray state = action->data().toByteArray();
80 removeAction(action);
81 delete action;
82 action = nullptr;
83 Q_EMIT restoreClosedTab(state);
84 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2);
85 }
86
87 if (menu()->actions().count() <= 2) {
88 setEnabled(false);
89 }
90 }
91
92 #include "moc_dolphinrecenttabsmenu.cpp"