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 | |
10 | RepositoriesModel::RepositoriesModel(QObject *parent) |
11 | : QAbstractListModel(parent) |
12 | { |
13 | refresh(); |
14 | } |
15 | |
16 | void 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 | |
30 | QVariant 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 | |
36 | switch (role) { |
37 | case Qt::DisplayRole: |
38 | return m_values[idx.row()].name; |
39 | case Qt::ToolTipRole: |
40 | return m_values[idx.row()].path; |
41 | default: |
42 | return QVariant(); |
43 | } |
44 | } |
45 | |
46 | int RepositoriesModel::rowCount(const QModelIndex &parent) const |
47 | { |
48 | return parent.isValid() ? 0 : m_values.count(); |
49 | } |
50 | |
51 | void RepositoriesModel::receivedProjects(KJob *job) |
52 | { |
53 | if (job->error()) { |
54 | qWarning() << "received error when fetching repositories:" << job->error() << job->errorString(); |
55 | |
56 | beginResetModel(); |
57 | m_values.clear(); |
58 | endResetModel(); |
59 | Q_EMIT repositoriesChanged(); |
60 | return; |
61 | } |
62 | |
63 | ReviewBoard::ProjectsListRequest *pl = dynamic_cast<ReviewBoard::ProjectsListRequest *>(job); |
64 | |
65 | beginResetModel(); |
66 | m_values.clear(); |
67 | const auto repositories = pl->repositories(); |
68 | for (const QVariant &repo : repositories) { |
69 | const QVariantMap repoMap = repo.toMap(); |
70 | m_values += Value{.name: repoMap[QStringLiteral("name" )], .path: repoMap[QStringLiteral("path" )]}; |
71 | } |
72 | std::sort(first: m_values.begin(), last: m_values.end()); |
73 | endResetModel(); |
74 | Q_EMIT repositoriesChanged(); |
75 | } |
76 | |
77 | int RepositoriesModel::findRepository(const QString &name) |
78 | { |
79 | QModelIndexList idxs = match(start: index(row: 0, column: 0), role: Qt::ToolTipRole, value: name, hits: 1, flags: Qt::MatchExactly); |
80 | if (idxs.isEmpty()) { |
81 | idxs = match(start: index(row: 0, column: 0), role: Qt::DisplayRole, value: QUrl(name).fileName(), hits: 1, flags: Qt::MatchExactly); |
82 | } |
83 | if (!idxs.isEmpty()) { |
84 | return idxs.first().row(); |
85 | } else { |
86 | qWarning() << "couldn't find the repository" << name; |
87 | } |
88 | |
89 | return -1; |
90 | } |
91 | |
92 | #include "moc_rbrepositoriesmodel.cpp" |
93 | |