]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinrecenttabsmenu.cpp
make CTRL+SHIFT+T reopen last closed tab
[dolphin.git] / src / dolphinrecenttabsmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphinrecenttabsmenu.h"
21
22 #include <KLocalizedString>
23 #include <KAcceleratorManager>
24 #include <KMimeType>
25 #include <KMenu>
26
27 DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject* parent) :
28 KActionMenu(KIcon("edit-undo"), i18n("Recently Closed Tabs"), parent)
29 {
30 setDelayed(false);
31 setEnabled(false);
32
33 m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this);
34 m_clearListAction->setIcon(KIcon("edit-clear-list"));
35 addAction(m_clearListAction);
36
37 addSeparator();
38
39 connect(menu(), SIGNAL(triggered(QAction*)),
40 this, SLOT(handleAction(QAction*)));
41 }
42
43 void DolphinRecentTabsMenu::rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
44 {
45 QAction* action = new QAction(menu());
46 action->setText(primaryUrl.path());
47
48 const QString iconName = KMimeType::iconNameForUrl(primaryUrl);
49 action->setIcon(KIcon(iconName));
50
51 KUrl::List urls;
52 urls << primaryUrl;
53 urls << secondaryUrl;
54 action->setData(QVariant::fromValue(urls));
55
56 // Add the closed tab menu entry after the separator and
57 // "Empty Recently Closed Tabs" entry
58 if (menu()->actions().size() == 2) {
59 addAction(action);
60 } else {
61 insertAction(menu()->actions().at(2), action);
62 }
63 emit closedTabsCountChanged(menu()->actions().size() - 2);
64 // Assure that only up to 6 closed tabs are shown in the menu.
65 // 8 because of clear action + separator + 6 closed tabs
66 if (menu()->actions().size() > 8) {
67 removeAction(menu()->actions().last());
68 }
69 setEnabled(true);
70 KAcceleratorManager::manage(menu());
71 }
72
73 void DolphinRecentTabsMenu::undoCloseTab()
74 {
75 Q_ASSERT(menu()->actions().size() > 2);
76 handleAction(menu()->actions().at(2));
77 }
78
79 void DolphinRecentTabsMenu::handleAction(QAction* action)
80 {
81 if (action == m_clearListAction) {
82 // Clear all actions except the "Empty Recently Closed Tabs"
83 // action and the separator
84 QList<QAction*> actions = menu()->actions();
85 const int count = actions.size();
86 for (int i = 2; i < count; ++i) {
87 removeAction(actions.at(i));
88 }
89 emit closedTabsCountChanged(0);
90 } else {
91 const KUrl::List urls = action->data().value<KUrl::List>();
92 removeAction(action);
93 delete action;
94 action = 0;
95 if (urls.count() == 2) {
96 emit restoreClosedTab(urls.first(), urls.last());
97 }
98 emit closedTabsCountChanged(menu()->actions().size() - 2);
99 }
100
101 if (menu()->actions().count() <= 2) {
102 setEnabled(false);
103 }
104 }