File indexing completed on 2024-05-12 05:52:08
0001 /* 0002 SPDX-FileCopyrightText: 2005 Christoph Cullmann <cullmann@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "katesessionsaction.h" 0008 #include "katesessionmanager.h" 0009 0010 #include <KConfig> 0011 #include <QActionGroup> 0012 #include <QMenu> 0013 #include <algorithm> 0014 0015 KateSessionsAction::KateSessionsAction(const QString &text, QObject *parent, KateSessionManager *manager, bool allSessions) 0016 : KActionMenu(text, parent) 0017 , m_manager(manager) 0018 , m_allSessions(allSessions) 0019 { 0020 connect(menu(), &QMenu::aboutToShow, this, &KateSessionsAction::slotAboutToShow); 0021 setPopupMode(QToolButton::InstantPopup); 0022 0023 sessionsGroup = new QActionGroup(menu()); 0024 0025 // reason for Qt::QueuedConnection: when switching session with N mainwindows 0026 // to e.g. 1 mainwindow, the last N - 1 mainwindows are deleted. Invoking 0027 // a session switch without queued connection deletes a mainwindow in which 0028 // the current code path is executed ---> crash. See bug #227008. 0029 connect(sessionsGroup, &QActionGroup::triggered, this, &KateSessionsAction::openSession, Qt::QueuedConnection); 0030 0031 connect(m_manager, &KateSessionManager::sessionListChanged, this, &KateSessionsAction::slotSessionChanged); 0032 0033 setDisabled(m_manager->sessionList().empty()); 0034 } 0035 0036 void KateSessionsAction::slotAboutToShow() 0037 { 0038 qDeleteAll(sessionsGroup->actions()); 0039 0040 KateSessionList slist = m_manager->sessionList(); 0041 0042 // only show first 10 sessions for recent session menu? 0043 if (!m_allSessions) { 0044 std::sort(slist.begin(), slist.end(), KateSession::compareByTimeDesc); 0045 slist = slist.mid(0, 10); // take first 10 0046 } 0047 0048 // sort the reduced list alphabetically (#364089) 0049 std::sort(slist.begin(), slist.end(), KateSession::compareByName); 0050 0051 for (const KateSession::Ptr &session : qAsConst(slist)) { 0052 QString sessionName = session->name(); 0053 sessionName.replace(QStringLiteral("&"), QStringLiteral("&&")); 0054 QAction *action = new QAction(sessionName, sessionsGroup); 0055 action->setData(QVariant(session->name())); 0056 action->setCheckable(true); 0057 action->setChecked(session == m_manager->activeSession()); 0058 menu()->addAction(action); 0059 } 0060 } 0061 0062 void KateSessionsAction::openSession(QAction *action) 0063 { 0064 const QString name = action->data().toString(); 0065 m_manager->activateSession(name); 0066 } 0067 0068 void KateSessionsAction::slotSessionChanged() 0069 { 0070 setDisabled(m_manager->sessionList().empty()); 0071 } 0072 0073 #include "moc_katesessionsaction.cpp"