File indexing completed on 2024-05-05 04:41:04

0001 /*
0002     SPDX-FileCopyrightText: 2010 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "sessionsmodel.h"
0008 #include <shell/core.h>
0009 #include <shell/sessioncontroller.h>
0010 
0011 using namespace KDevelop;
0012 
0013 SessionsModel::SessionsModel(QObject* parent)
0014     : QAbstractListModel(parent)
0015     , m_sessions(KDevelop::SessionController::availableSessionInfos())
0016 {
0017     connect(Core::self()->sessionController(), &SessionController::sessionDeleted, this, &SessionsModel::sessionDeleted);
0018 }
0019 
0020 QHash< int, QByteArray > SessionsModel::roleNames() const
0021 {
0022     QHash< int, QByteArray > roles = QAbstractListModel::roleNames();
0023     roles.insert(Uuid, "uuid");
0024     roles.insert(Projects, "projects");
0025     roles.insert(ProjectNames, "projectNames");
0026     roles.insert(VisibleIdentifier, "identifier");
0027     return roles;
0028 }
0029 
0030 QVariant SessionsModel::data(const QModelIndex& index, int role) const
0031 {
0032     if(!index.isValid() || index.row()>m_sessions.count()) {
0033         return QVariant();
0034     }
0035     
0036     switch(role) {
0037         case Qt::DisplayRole:
0038             return m_sessions[index.row()].name;
0039         case Qt::ToolTip:
0040             return m_sessions[index.row()].description;
0041         case Uuid:
0042             return m_sessions[index.row()].uuid.toString();
0043         case Projects:
0044             return QVariant::fromValue(m_sessions[index.row()].projects);
0045         case VisibleIdentifier: {
0046             const KDevelop::SessionInfo& s = m_sessions[index.row()];
0047             return s.name.isEmpty() && !s.projects.isEmpty() ? s.projects.first().fileName() : s.name;
0048         }
0049         case ProjectNames: {
0050             QVariantList ret;
0051             const auto& projects = m_sessions[index.row()].projects;
0052             ret.reserve(projects.size());
0053             for (const auto& project : projects) {
0054                 ret += project.fileName();
0055             }
0056             return ret;
0057         }
0058     }
0059     return QVariant();
0060 }
0061 
0062 int SessionsModel::rowCount(const QModelIndex& parent) const
0063 {
0064     return parent.isValid() ? 0 : m_sessions.size();
0065 }
0066 
0067 void SessionsModel::loadSession(const QString& nameOrId) const
0068 {
0069     KDevelop::Core::self()->sessionController()->loadSession(nameOrId);
0070 }
0071 
0072 void SessionsModel::sessionDeleted(const QString& id)
0073 {
0074     for(int i=0; i<m_sessions.size(); i++) {
0075         if(m_sessions[i].uuid.toString()==id) {
0076             beginRemoveRows(QModelIndex(), i, i);
0077             m_sessions.removeAt(i);
0078             endRemoveRows();
0079             break;
0080         }
0081     }
0082 }
0083 
0084 #include "moc_sessionsmodel.cpp"