surveymodel.cpp 3.9 KB
Newer Older
Volker Krause's avatar
Volker Krause committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    Copyright (C) 2016 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "surveymodel.h"

Volker Krause's avatar
Volker Krause committed
20
#include <rest/restapi.h>
21
#include <rest/restclient.h>
Volker Krause's avatar
Volker Krause committed
22 23 24 25 26

#include <QDebug>
#include <QNetworkReply>
#include <QUrl>

27
using namespace KUserFeedback::Console;
Volker Krause's avatar
Volker Krause committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

SurveyModel::SurveyModel(QObject *parent) : QAbstractTableModel(parent)
{
}

SurveyModel::~SurveyModel() = default;

void SurveyModel::setRESTClient(RESTClient* client)
{
    Q_ASSERT(client);
    m_restClient = client;
    connect(client, &RESTClient::clientConnected, this, &SurveyModel::reload);
    reload();
}

Volker Krause's avatar
Volker Krause committed
43
void SurveyModel::setProduct(const Product& product)
Volker Krause's avatar
Volker Krause committed
44
{
Volker Krause's avatar
Volker Krause committed
45
    m_product = product;
Volker Krause's avatar
Volker Krause committed
46 47 48 49 50
    reload();
}

void SurveyModel::reload()
{
Volker Krause's avatar
Volker Krause committed
51
    if (!m_restClient || !m_restClient->isConnected() || !m_product.isValid())
Volker Krause's avatar
Volker Krause committed
52 53
        return;

Volker Krause's avatar
Volker Krause committed
54
    auto reply = RESTApi::listSurveys(m_restClient, m_product);
Volker Krause's avatar
Volker Krause committed
55 56 57 58 59 60 61
    connect(reply, &QNetworkReply::finished, this, [this, reply]() {
        if (reply->error() == QNetworkReply::NoError) {
            beginResetModel();
            const auto data = reply->readAll();
            m_surveys = Survey::fromJson(data);
            endResetModel();
        }
62
        reply->deleteLater();
Volker Krause's avatar
Volker Krause committed
63 64 65 66 67 68
    });
}

int SurveyModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent);
69
    return 4;
Volker Krause's avatar
Volker Krause committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

int SurveyModel::rowCount(const QModelIndex& parent) const
{
    if (parent.isValid())
        return 0;
    return m_surveys.size();
}

QVariant SurveyModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
        return {};

    if (role == Qt::DisplayRole) {
        const auto survey = m_surveys.at(index.row());
        switch (index.column()) {
            case 0: return survey.name();
            case 1: return survey.url().toString();
89
            case 3: return survey.target();
Volker Krause's avatar
Volker Krause committed
90
        }
91 92 93
    } else if (role == Qt::CheckStateRole) {
        if (index.column() == 2)
            return  m_surveys.at(index.row()).isActive() ? Qt::Checked : Qt::Unchecked;
94 95
    } else if (role == SurveyRole) {
        return QVariant::fromValue(m_surveys.at(index.row()));
Volker Krause's avatar
Volker Krause committed
96 97 98 99
    }
    return {};
}

100 101 102 103 104
bool SurveyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.column() == 2 && role == Qt::CheckStateRole) {
        auto &survey = m_surveys[index.row()];
        survey.setActive(value.toInt() == Qt::Checked);
Volker Krause's avatar
Volker Krause committed
105
        auto reply = RESTApi::updateSurvey(m_restClient, survey);
106 107 108 109 110
        connect(reply, &QNetworkReply::finished, this, [this, reply]() {
            qDebug() << reply->readAll();
            reload();
        });
        emit dataChanged(index, index);
111
        reply->deleteLater();
112 113 114 115 116
        return true;
    }
    return false;
}

Volker Krause's avatar
Volker Krause committed
117 118 119 120 121 122
QVariant SurveyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 0: return tr("Name");
            case 1: return tr("URL");
123
            case 2: return tr("Active");
124
            case 3: return tr("Target");
Volker Krause's avatar
Volker Krause committed
125 126 127 128
        }
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}
129 130 131 132 133 134 135 136

Qt::ItemFlags SurveyModel::flags(const QModelIndex &index) const
{
    auto f = QAbstractTableModel::flags(index);
    if (index.column() == 2)
        return f | Qt::ItemIsUserCheckable;
    return f;
}