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 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
56 m_values.clear();
58 Q_EMIT repositoriesChanged();
59 return;
60 }
61
62 ReviewBoard::ProjectsListRequest *pl = dynamic_cast<ReviewBoard::ProjectsListRequest *>(job);
63
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{repoMap[QStringLiteral("name")], repoMap[QStringLiteral("path")]};
70 }
71 std::sort(m_values.begin(), m_values.end());
73 Q_EMIT repositoriesChanged();
74}
75
76int RepositoriesModel::findRepository(const QString &name)
77{
78 QModelIndexList idxs = match(index(0, 0), Qt::ToolTipRole, name, 1, Qt::MatchExactly);
79 if (idxs.isEmpty()) {
80 idxs = match(index(0, 0), Qt::DisplayRole, QUrl(name).fileName(), 1, 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"
virtual QString errorString() const
int error() const
void finished(KJob *job)
QString name(StandardShortcut 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 Tue Mar 26 2024 11:14:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.