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