File indexing completed on 2024-04-21 05:48:31

0001 /***********************************************************************
0002  * SPDX-FileCopyrightText: 2003-2004 Max Howell <max.howell@methylblue.com>
0003  * SPDX-FileCopyrightText: 2008-2009 Martin Sandsmark <martin.sandsmark@kde.org>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  ***********************************************************************/
0007 
0008 #include "historyAction.h"
0009 #include "filelight_debug.h"
0010 #include <KActionCollection>
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <KStandardShortcut>
0015 
0016 #include <QGuiApplication>
0017 #include <QIcon>
0018 
0019 inline HistoryAction::HistoryAction(const QIcon &icon, const QString &text, KActionCollection *ac)
0020     : QAction(icon, text, ac)
0021     , m_text(text)
0022 {
0023     // .ui files make this false, but we can't rely on UI file as it isn't compiled in :(
0024     setEnabled(false);
0025 }
0026 
0027 void HistoryAction::setHelpText(const QUrl &url)
0028 {
0029     QString text = url.path();
0030     setStatusTip(text);
0031     setToolTip(text);
0032     setWhatsThis(text);
0033 }
0034 
0035 void HistoryAction::push(const QUrl &path)
0036 {
0037     if (path.isEmpty()) {
0038         return;
0039     }
0040 
0041     if (m_list.isEmpty() || (!m_list.isEmpty() && (m_list.last() != path))) {
0042         m_list.append(path);
0043     }
0044 
0045     setHelpText(path);
0046     setEnabled(true);
0047 }
0048 
0049 QUrl HistoryAction::pop()
0050 {
0051     const QUrl s = m_list.takeLast();
0052     if (!m_list.isEmpty()) {
0053         setHelpText(m_list.last());
0054     }
0055 
0056     setEnabled();
0057     return s;
0058 }
0059 
0060 HistoryCollection::HistoryCollection(KActionCollection *ac, QObject *parent)
0061     : QObject(parent)
0062     , m_b(new HistoryAction(
0063           QIcon::fromTheme(QGuiApplication::layoutDirection() == Qt::LeftToRight ? QStringLiteral("go-previous") : QStringLiteral("go-previous-symbolic-rtl")),
0064           i18nc("Go to the last path viewed", "Back"),
0065           ac))
0066     , m_f(new HistoryAction(
0067           QIcon::fromTheme(QGuiApplication::layoutDirection() == Qt::LeftToRight ? QStringLiteral("go-next") : QStringLiteral("go-next-symbolic-rtl")),
0068           i18nc("Go to forward in the history of paths viewed", "Forward"),
0069           ac))
0070     , m_receiver(nullptr)
0071 {
0072     ac->addAction(QStringLiteral("go_back"), m_b);
0073     ac->addAction(QStringLiteral("go_forward"), m_f);
0074     connect(m_b, &QAction::triggered, this, &HistoryCollection::pop);
0075     connect(m_f, &QAction::triggered, this, &HistoryCollection::pop);
0076 }
0077 
0078 void HistoryCollection::push(const QUrl &url) // slot
0079 {
0080     if (!url.isEmpty()) {
0081         if (!m_receiver) {
0082             m_f->clear();
0083             m_receiver = m_b;
0084         }
0085 
0086         m_receiver->push(url);
0087     }
0088     m_receiver = nullptr;
0089 }
0090 
0091 void HistoryCollection::pop() // slot
0092 {
0093     QUrl url = ((HistoryAction *)sender())->pop();
0094 
0095     m_receiver = (sender() == m_b) ? m_f : m_b;
0096 
0097     Q_EMIT activated(url);
0098 }
0099 
0100 void HistoryCollection::save(KConfigGroup &configgroup)
0101 {
0102     configgroup.writePathEntry("backHistory", QUrl::toStringList(m_b->m_list));
0103     configgroup.writePathEntry("forwardHistory", QUrl::toStringList(m_f->m_list));
0104 }
0105 
0106 void HistoryCollection::restore(const KConfigGroup &configgroup)
0107 {
0108     if (!m_b || !m_f) {
0109         qCWarning(FILELIGHT_LOG) << "what the actual fuck";
0110         return;
0111     }
0112 
0113     m_b->m_list = QUrl::fromStringList(configgroup.readPathEntry("backHistory", QStringList()));
0114     m_f->m_list = QUrl::fromStringList(configgroup.readPathEntry("forwardHistory", QStringList()));
0115     // TODO texts are not updated - no matter
0116 }