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 "rbreviewslistmodel.h" |
8 | #include "reviewboardjobs.h" |
9 | |
10 | ReviewsListModel::ReviewsListModel(QObject *parent) |
11 | : QAbstractListModel(parent) |
12 | { |
13 | refresh(); |
14 | } |
15 | |
16 | void ReviewsListModel::refresh() |
17 | { |
18 | if (m_server.isEmpty() || m_repository.isEmpty()) { |
19 | beginResetModel(); |
20 | m_values.clear(); |
21 | endResetModel(); |
22 | return; |
23 | } |
24 | |
25 | ReviewBoard::ReviewListRequest *repo = new ReviewBoard::ReviewListRequest(m_server, m_username, m_status, this); |
26 | connect(sender: repo, signal: &ReviewBoard::ReviewListRequest::finished, context: this, slot: &ReviewsListModel::receivedReviews); |
27 | repo->start(); |
28 | } |
29 | |
30 | void ReviewsListModel::receivedReviews(KJob *job) |
31 | { |
32 | if (job->error() != 0) { |
33 | qWarning() << "error review list" << job->errorString(); |
34 | |
35 | beginResetModel(); |
36 | m_values.clear(); |
37 | endResetModel(); |
38 | return; |
39 | } |
40 | |
41 | const QVariantList revs = dynamic_cast<ReviewBoard::ReviewListRequest *>(job)->reviews(); |
42 | beginResetModel(); |
43 | m_values.clear(); |
44 | for (const QVariant &review : revs) { |
45 | QVariantMap reviewMap = review.toMap(); |
46 | QVariantMap repoMap = reviewMap[QStringLiteral("links" )].toMap()[QStringLiteral("repository" )].toMap(); |
47 | if (repoMap[QStringLiteral("title" )].toString() == m_repository) { |
48 | m_values += Value{.summary: reviewMap[QStringLiteral("summary" )], .id: reviewMap[QStringLiteral("id" )]}; |
49 | } |
50 | } |
51 | endResetModel(); |
52 | } |
53 | |
54 | QVariant ReviewsListModel::data(const QModelIndex &idx, int role) const |
55 | { |
56 | if (!idx.isValid() || idx.column() != 0 || idx.row() >= m_values.size()) |
57 | return QVariant(); |
58 | |
59 | switch (role) { |
60 | case Qt::DisplayRole: |
61 | return m_values[idx.row()].summary; |
62 | case Qt::ToolTipRole: |
63 | return m_values[idx.row()].id; |
64 | } |
65 | return QVariant(); |
66 | } |
67 | |
68 | int ReviewsListModel::rowCount(const QModelIndex &parent) const |
69 | { |
70 | return parent.isValid() ? 0 : m_values.count(); |
71 | } |
72 | |
73 | QVariant ReviewsListModel::get(int row, const QByteArray &role) |
74 | { |
75 | return index(row, column: 0).data(arole: roleNames().key(value: role)); |
76 | } |
77 | |
78 | #include "moc_rbreviewslistmodel.cpp" |
79 | |