File indexing completed on 2024-12-15 03:45:01

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "surveymodel.h"
0008 
0009 #include <rest/restapi.h>
0010 #include <rest/restclient.h>
0011 
0012 #include <QDebug>
0013 #include <QNetworkReply>
0014 #include <QUrl>
0015 
0016 using namespace KUserFeedback::Console;
0017 
0018 SurveyModel::SurveyModel(QObject *parent) : QAbstractTableModel(parent)
0019 {
0020 }
0021 
0022 SurveyModel::~SurveyModel() = default;
0023 
0024 void SurveyModel::setRESTClient(RESTClient* client)
0025 {
0026     Q_ASSERT(client);
0027     m_restClient = client;
0028     connect(client, &RESTClient::clientConnected, this, &SurveyModel::reload);
0029     reload();
0030 }
0031 
0032 void SurveyModel::setProduct(const Product& product)
0033 {
0034     m_product = product;
0035     reload();
0036 }
0037 
0038 void SurveyModel::reload()
0039 {
0040     if (!m_restClient || !m_restClient->isConnected() || !m_product.isValid())
0041         return;
0042 
0043     auto reply = RESTApi::listSurveys(m_restClient, m_product);
0044     connect(reply, &QNetworkReply::finished, this, [this, reply]() {
0045         if (reply->error() == QNetworkReply::NoError) {
0046             beginResetModel();
0047             const auto data = reply->readAll();
0048             m_surveys = Survey::fromJson(data);
0049             endResetModel();
0050         }
0051         reply->deleteLater();
0052     });
0053 }
0054 
0055 int SurveyModel::columnCount(const QModelIndex& parent) const
0056 {
0057     Q_UNUSED(parent);
0058     return 4;
0059 }
0060 
0061 int SurveyModel::rowCount(const QModelIndex& parent) const
0062 {
0063     if (parent.isValid())
0064         return 0;
0065     return m_surveys.size();
0066 }
0067 
0068 QVariant SurveyModel::data(const QModelIndex& index, int role) const
0069 {
0070     if (!index.isValid())
0071         return {};
0072 
0073     if (role == Qt::DisplayRole) {
0074         const auto survey = m_surveys.at(index.row());
0075         switch (index.column()) {
0076             case 0: return survey.name();
0077             case 1: return survey.url().toString();
0078             case 3: return survey.target();
0079         }
0080     } else if (role == Qt::CheckStateRole) {
0081         if (index.column() == 2)
0082             return  m_surveys.at(index.row()).isActive() ? Qt::Checked : Qt::Unchecked;
0083     } else if (role == SurveyRole) {
0084         return QVariant::fromValue(m_surveys.at(index.row()));
0085     }
0086     return {};
0087 }
0088 
0089 bool SurveyModel::setData(const QModelIndex &index, const QVariant &value, int role)
0090 {
0091     if (index.column() == 2 && role == Qt::CheckStateRole) {
0092         auto &survey = m_surveys[index.row()];
0093         survey.setActive(value.toInt() == Qt::Checked);
0094         auto reply = RESTApi::updateSurvey(m_restClient, survey);
0095         connect(reply, &QNetworkReply::finished, this, [this, reply]() {
0096             qDebug() << reply->readAll();
0097             reload();
0098         });
0099         Q_EMIT dataChanged(index, index);
0100         reply->deleteLater();
0101         return true;
0102     }
0103     return false;
0104 }
0105 
0106 QVariant SurveyModel::headerData(int section, Qt::Orientation orientation, int role) const
0107 {
0108     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
0109         switch (section) {
0110             case 0: return tr("Name");
0111             case 1: return tr("URL");
0112             case 2: return tr("Active");
0113             case 3: return tr("Target");
0114         }
0115     }
0116     return QAbstractTableModel::headerData(section, orientation, role);
0117 }
0118 
0119 Qt::ItemFlags SurveyModel::flags(const QModelIndex &index) const
0120 {
0121     auto f = QAbstractTableModel::flags(index);
0122     if (index.column() == 2)
0123         return f | Qt::ItemIsUserCheckable;
0124     return f;
0125 }
0126 
0127 #include "moc_surveymodel.cpp"