File indexing completed on 2024-05-12 05:52:09

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Eugene Popov <popov895@ukr.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "savedsessionsmodel.h"
0008 
0009 #include <KConfig>
0010 
0011 #include <QIcon>
0012 
0013 SavedSessionsModel::SavedSessionsModel(QObject *parent)
0014     : QAbstractListModel(parent)
0015 {
0016 }
0017 
0018 QVariant SavedSessionsModel::data(const QModelIndex &index, int role) const
0019 {
0020     if (index.isValid()) {
0021         const size_t row = index.row();
0022         if (row < m_sessions.size()) {
0023             const SessionInfo &session = m_sessions.at(row);
0024             switch (role) {
0025             case Qt::DisplayRole:
0026                 return session.name;
0027             case Qt::DecorationRole:
0028                 return QIcon::fromTheme(QStringLiteral("document-save"));
0029             case Qt::ToolTipRole:
0030                 return session.lastOpened.toString();
0031             default:
0032                 break;
0033             }
0034         }
0035     }
0036 
0037     return QVariant();
0038 }
0039 
0040 int SavedSessionsModel::rowCount(const QModelIndex &parent) const
0041 {
0042     Q_UNUSED(parent);
0043 
0044     return m_sessions.size();
0045 }
0046 
0047 void SavedSessionsModel::refresh(const KateSessionList &sessionList)
0048 {
0049     std::vector<SessionInfo> sessions;
0050     sessions.reserve(sessionList.count());
0051 
0052     for (const KateSession::Ptr &session : sessionList) {
0053         sessions.push_back({session->timestamp(), session->name()});
0054     }
0055 
0056     std::sort(sessions.begin(), sessions.end());
0057 
0058     beginResetModel();
0059     m_sessions = std::move(sessions);
0060     endResetModel();
0061 }
0062 
0063 #include "moc_savedsessionsmodel.cpp"