]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinrecenttabsmenu.cpp
refactored renameTab
[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 (DolphinQuery::supportsScheme(url.scheme())) {
36 const DolphinQuery query = DolphinQuery::fromSearchUrl(url);
37 action->setText(i18n("Search for %1 in %2", query.text(), query.includeFolder()));
38 } else if (url.scheme() == QLatin1String("filenamesearch")) {
39 const QUrlQuery query(url);
40 action->setText(i18n("Search for %1 in %2", query.queryItemValue(QStringLiteral("search")), query.queryItemValue(QStringLiteral("url"))));
41 } else {
42 action->setText(url.path());
43 }
44 action->setData(state);
45 const QString iconName = KIO::iconNameForUrl(url);
46 action->setIcon(QIcon::fromTheme(iconName));
47
48 // Add the closed tab menu entry after the separator and
49 // "Empty Recently Closed Tabs" entry
50 if (menu()->actions().size() == 2) {
51 addAction(action);
52 } else {
53 insertAction(menu()->actions().at(2), action);
54 }
55 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2);
56 // Assure that only up to 6 closed tabs are shown in the menu.
57 // 8 because of clear action + separator + 6 closed tabs
58 if (menu()->actions().size() > 8) {
59 removeAction(menu()->actions().last());
60 }
61 setEnabled(true);
62 KAcceleratorManager::manage(menu());
63 }
64
65 void DolphinRecentTabsMenu::undoCloseTab()
66 {
67 Q_ASSERT(menu()->actions().size() > 2);
68 handleAction(menu()->actions().at(2));
69 }
70
71 void DolphinRecentTabsMenu::handleAction(QAction *action)
72 {
73 if (action == m_clearListAction) {
74 // Clear all actions except the "Empty Recently Closed Tabs"
75 // action and the separator
76 QList<QAction *> actions = menu()->actions();
77 const int count = actions.size();
78 for (int i = count - 1; i >= 2; i--) {
79 removeAction(actions.at(i));
80 }
81 Q_EMIT closedTabsCountChanged(0);
82 } else {
83 const QByteArray state = action->data().toByteArray();
84 removeAction(action);
85 delete action;
86 action = nullptr;
87 Q_EMIT restoreClosedTab(state);
88 Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2);
89 }
90
91 if (menu()->actions().count() <= 2) {
92 setEnabled(false);
93 }
94 }
95
96 #include "moc_dolphinrecenttabsmenu.cpp"