2020-08-25 17:07:38 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2014-06-19 20:04:36 +00:00
|
|
|
|
|
|
|
#include "dolphinrecenttabsmenu.h"
|
2023-10-03 05:44:55 +00:00
|
|
|
#include "search/dolphinquery.h"
|
2014-06-19 20:04:36 +00:00
|
|
|
|
|
|
|
#include <KAcceleratorManager>
|
2018-03-04 13:38:16 +00:00
|
|
|
#include <KLocalizedString>
|
2014-08-22 21:17:02 +00:00
|
|
|
#include <kio/global.h>
|
2018-03-04 13:38:16 +00:00
|
|
|
|
2014-10-10 21:36:36 +00:00
|
|
|
#include <QMenu>
|
2023-10-03 05:44:55 +00:00
|
|
|
#include <QUrlQuery>
|
2014-06-19 20:04:36 +00:00
|
|
|
|
|
|
|
DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject *parent)
|
2016-03-16 21:17:37 +00:00
|
|
|
: KActionMenu(QIcon::fromTheme(QStringLiteral("edit-undo")), i18n("Recently Closed Tabs"), parent)
|
2014-06-19 20:04:36 +00:00
|
|
|
{
|
2020-12-06 18:47:07 +00:00
|
|
|
setPopupMode(QToolButton::InstantPopup);
|
2014-06-19 20:04:36 +00:00
|
|
|
setEnabled(false);
|
|
|
|
|
|
|
|
m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this);
|
2022-06-28 05:31:06 +00:00
|
|
|
m_clearListAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-history")));
|
2014-06-19 20:04:36 +00:00
|
|
|
addAction(m_clearListAction);
|
|
|
|
|
|
|
|
addSeparator();
|
|
|
|
|
2016-03-16 21:17:37 +00:00
|
|
|
connect(menu(), &QMenu::triggered, this, &DolphinRecentTabsMenu::handleAction);
|
2014-06-19 20:04:36 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 19:18:43 +00:00
|
|
|
void DolphinRecentTabsMenu::rememberClosedTab(const QUrl &url, const QByteArray &state)
|
2014-06-19 20:04:36 +00:00
|
|
|
{
|
|
|
|
QAction *action = new QAction(menu());
|
2023-10-03 05:44:55 +00:00
|
|
|
if (DolphinQuery::supportsScheme(url.scheme())) {
|
|
|
|
const DolphinQuery query = DolphinQuery::fromSearchUrl(url);
|
|
|
|
action->setText(i18n("Search for %1 in %2", query.text(), query.includeFolder()));
|
|
|
|
} else if (url.scheme() == QLatin1String("filenamesearch")) {
|
|
|
|
const QUrlQuery query(url);
|
|
|
|
action->setText(i18n("Search for %1 in %2", query.queryItemValue(QStringLiteral("search")), query.queryItemValue(QStringLiteral("url"))));
|
|
|
|
} else {
|
|
|
|
action->setText(url.path());
|
|
|
|
}
|
2014-08-10 18:36:44 +00:00
|
|
|
action->setData(state);
|
2014-08-22 21:17:02 +00:00
|
|
|
const QString iconName = KIO::iconNameForUrl(url);
|
2014-06-29 18:26:17 +00:00
|
|
|
action->setIcon(QIcon::fromTheme(iconName));
|
2014-06-19 20:04:36 +00:00
|
|
|
|
|
|
|
// Add the closed tab menu entry after the separator and
|
|
|
|
// "Empty Recently Closed Tabs" entry
|
|
|
|
if (menu()->actions().size() == 2) {
|
|
|
|
addAction(action);
|
|
|
|
} else {
|
|
|
|
insertAction(menu()->actions().at(2), action);
|
|
|
|
}
|
2020-10-23 17:51:33 +00:00
|
|
|
Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2);
|
2014-06-19 20:04:36 +00:00
|
|
|
// Assure that only up to 6 closed tabs are shown in the menu.
|
|
|
|
// 8 because of clear action + separator + 6 closed tabs
|
|
|
|
if (menu()->actions().size() > 8) {
|
|
|
|
removeAction(menu()->actions().last());
|
|
|
|
}
|
|
|
|
setEnabled(true);
|
|
|
|
KAcceleratorManager::manage(menu());
|
|
|
|
}
|
|
|
|
|
2014-07-22 12:01:49 +00:00
|
|
|
void DolphinRecentTabsMenu::undoCloseTab()
|
|
|
|
{
|
|
|
|
Q_ASSERT(menu()->actions().size() > 2);
|
|
|
|
handleAction(menu()->actions().at(2));
|
|
|
|
}
|
|
|
|
|
2014-06-19 20:04:36 +00:00
|
|
|
void DolphinRecentTabsMenu::handleAction(QAction *action)
|
|
|
|
{
|
|
|
|
if (action == m_clearListAction) {
|
|
|
|
// Clear all actions except the "Empty Recently Closed Tabs"
|
|
|
|
// action and the separator
|
|
|
|
QList<QAction *> actions = menu()->actions();
|
|
|
|
const int count = actions.size();
|
2021-07-17 17:47:57 +00:00
|
|
|
for (int i = count - 1; i >= 2; i--) {
|
2014-06-19 20:04:36 +00:00
|
|
|
removeAction(actions.at(i));
|
|
|
|
}
|
2020-10-23 17:51:33 +00:00
|
|
|
Q_EMIT closedTabsCountChanged(0);
|
2014-06-19 20:04:36 +00:00
|
|
|
} else {
|
2016-03-16 21:17:37 +00:00
|
|
|
const QByteArray state = action->data().toByteArray();
|
2014-06-19 20:04:36 +00:00
|
|
|
removeAction(action);
|
|
|
|
delete action;
|
2017-11-21 09:52:14 +00:00
|
|
|
action = nullptr;
|
2020-10-23 17:51:33 +00:00
|
|
|
Q_EMIT restoreClosedTab(state);
|
|
|
|
Q_EMIT closedTabsCountChanged(menu()->actions().size() - 2);
|
2014-06-19 20:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (menu()->actions().count() <= 2) {
|
|
|
|
setEnabled(false);
|
|
|
|
}
|
2014-10-18 13:00:17 +00:00
|
|
|
}
|
2023-07-04 20:07:48 +00:00
|
|
|
|
|
|
|
#include "moc_dolphinrecenttabsmenu.cpp"
|