1/*
2 SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "rbrepositoriesmodel.h"
8#include "reviewboardjobs.h"
9
10RepositoriesModel::RepositoriesModel(QObject *parent)
11 : QAbstractListModel(parent)
12{
13 refresh();
14}
15
16void RepositoriesModel::refresh()
17{
18 if (m_server.isEmpty()) {
19 beginResetModel();
20 m_values.clear();
21 endResetModel();
22 Q_EMIT repositoriesChanged();
23 return;
24 }
25 ReviewBoard::ProjectsListRequest *repo = new ReviewBoard::ProjectsListRequest(m_server, this);
26 connect(sender: repo, signal: &ReviewBoard::ProjectsListRequest::finished, context: this, slot: &RepositoriesModel::receivedProjects);
27 repo->start();
28}
29
30QVariant RepositoriesModel::data(const QModelIndex &idx, int role) const
31{
32 if (!idx.isValid() || idx.column() != 0 || idx.row() >= m_values.count())
33 return QVariant();
34
35 switch (role) {
36 case Qt::DisplayRole:
37 return m_values[idx.row()].name;
38 case Qt::ToolTipRole:
39 return m_values[idx.row()].path;
40 default:
41 return QVariant();
42 }
43}
44
45int RepositoriesModel::rowCount(const QModelIndex &parent) const
46{
47 return parent.isValid() ? 0 : m_values.count();
48}
49
50void RepositoriesModel::receivedProjects(KJob *job)
51{
52 if (job->error()) {
53 qWarning() << "received error when fetching repositories:" << job->error() << job->errorString();
54
55 beginResetModel();
56 m_values.clear();
57 endResetModel();
58 Q_EMIT repositoriesChanged();
59 return;
60 }
61
62 ReviewBoard::ProjectsListRequest *pl = dynamic_cast<ReviewBoard::ProjectsListRequest *>(job);
63
64 beginResetModel();
65 m_values.clear();
66 const auto repositories = pl->repositories();
67 for (const QVariant &repo : repositories) {
68 const QVariantMap repoMap = repo.toMap();
69 m_values += Value{.name: repoMap[QStringLiteral("name")], .path: repoMap[QStringLiteral("path")]};
70 }
71 std::sort(first: m_values.begin(), last: m_values.end());
72 endResetModel();
73 Q_EMIT repositoriesChanged();
74}
75
76int RepositoriesModel::findRepository(const QString &name)
77{
78 QModelIndexList idxs = match(start: index(row: 0, column: 0), role: Qt::ToolTipRole, value: name, hits: 1, flags: Qt::MatchExactly);
79 if (idxs.isEmpty()) {
80 idxs = match(start: index(row: 0, column: 0), role: Qt::DisplayRole, value: QUrl(name).fileName(), hits: 1, flags: Qt::MatchExactly);
81 }
82 if (!idxs.isEmpty())
83 return idxs.first().row();
84 else
85 qWarning() << "couldn't find the repository" << name;
86
87 return -1;
88}
89
90#include "moc_rbrepositoriesmodel.cpp"
91

source code of purpose/src/plugins/reviewboard/quick/rbrepositoriesmodel.cpp