Purpose

rbrepositoriesmodel.cpp
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()) {
20 m_values.clear();
22 Q_EMIT repositoriesChanged();
23 return;
24 }
25 ReviewBoard::ProjectsListRequest *repo = new ReviewBoard::ProjectsListRequest(m_server, this);
26 connect(repo, &ReviewBoard::ProjectsListRequest::finished, this, &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
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
46int RepositoriesModel::rowCount(const QModelIndex &parent) const
47{
48 return parent.isValid() ? 0 : m_values.count();
49}
50
51void RepositoriesModel::receivedProjects(KJob *job)
52{
53 if (job->error()) {
54 qWarning() << "received error when fetching repositories:" << job->error() << job->errorString();
55
57 m_values.clear();
59 Q_EMIT repositoriesChanged();
60 return;
61 }
62
63 ReviewBoard::ProjectsListRequest *pl = dynamic_cast<ReviewBoard::ProjectsListRequest *>(job);
64
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{repoMap[QStringLiteral("name")], repoMap[QStringLiteral("path")]};
71 }
72 std::sort(m_values.begin(), m_values.end());
74 Q_EMIT repositoriesChanged();
75}
76
77int RepositoriesModel::findRepository(const QString &name)
78{
79 QModelIndexList idxs = match(index(0, 0), Qt::ToolTipRole, name, 1, Qt::MatchExactly);
80 if (idxs.isEmpty()) {
81 idxs = match(index(0, 0), Qt::DisplayRole, QUrl(name).fileName(), 1, 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"
virtual QString errorString() const
int error() const
void finished(KJob *job)
QString name(StandardAction id)
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
iterator begin()
void clear()
qsizetype count() const const
iterator end()
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
DisplayRole
MatchExactly
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.