File indexing completed on 2024-12-22 03:48:44
0001 /* 0002 SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "rbrepositoriesmodel.h" 0008 #include "reviewboardjobs.h" 0009 0010 RepositoriesModel::RepositoriesModel(QObject *parent) 0011 : QAbstractListModel(parent) 0012 { 0013 refresh(); 0014 } 0015 0016 void RepositoriesModel::refresh() 0017 { 0018 if (m_server.isEmpty()) { 0019 beginResetModel(); 0020 m_values.clear(); 0021 endResetModel(); 0022 Q_EMIT repositoriesChanged(); 0023 return; 0024 } 0025 ReviewBoard::ProjectsListRequest *repo = new ReviewBoard::ProjectsListRequest(m_server, this); 0026 connect(repo, &ReviewBoard::ProjectsListRequest::finished, this, &RepositoriesModel::receivedProjects); 0027 repo->start(); 0028 } 0029 0030 QVariant RepositoriesModel::data(const QModelIndex &idx, int role) const 0031 { 0032 if (!idx.isValid() || idx.column() != 0 || idx.row() >= m_values.count()) 0033 return QVariant(); 0034 0035 switch (role) { 0036 case Qt::DisplayRole: 0037 return m_values[idx.row()].name; 0038 case Qt::ToolTipRole: 0039 return m_values[idx.row()].path; 0040 default: 0041 return QVariant(); 0042 } 0043 } 0044 0045 int RepositoriesModel::rowCount(const QModelIndex &parent) const 0046 { 0047 return parent.isValid() ? 0 : m_values.count(); 0048 } 0049 0050 void RepositoriesModel::receivedProjects(KJob *job) 0051 { 0052 if (job->error()) { 0053 qWarning() << "received error when fetching repositories:" << job->error() << job->errorString(); 0054 0055 beginResetModel(); 0056 m_values.clear(); 0057 endResetModel(); 0058 Q_EMIT repositoriesChanged(); 0059 return; 0060 } 0061 0062 ReviewBoard::ProjectsListRequest *pl = dynamic_cast<ReviewBoard::ProjectsListRequest *>(job); 0063 0064 beginResetModel(); 0065 m_values.clear(); 0066 const auto repositories = pl->repositories(); 0067 for (const QVariant &repo : repositories) { 0068 const QVariantMap repoMap = repo.toMap(); 0069 m_values += Value{repoMap[QStringLiteral("name")], repoMap[QStringLiteral("path")]}; 0070 } 0071 std::sort(m_values.begin(), m_values.end()); 0072 endResetModel(); 0073 Q_EMIT repositoriesChanged(); 0074 } 0075 0076 int RepositoriesModel::findRepository(const QString &name) 0077 { 0078 QModelIndexList idxs = match(index(0, 0), Qt::ToolTipRole, name, 1, Qt::MatchExactly); 0079 if (idxs.isEmpty()) { 0080 idxs = match(index(0, 0), Qt::DisplayRole, QUrl(name).fileName(), 1, Qt::MatchExactly); 0081 } 0082 if (!idxs.isEmpty()) 0083 return idxs.first().row(); 0084 else 0085 qWarning() << "couldn't find the repository" << name; 0086 0087 return -1; 0088 } 0089 0090 #include "moc_rbrepositoriesmodel.cpp"