File indexing completed on 2025-01-26 03:44:34
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "surveyeditor.h" 0008 #include "ui_surveyeditor.h" 0009 0010 #include "surveydialog.h" 0011 0012 #include <model/surveymodel.h> 0013 #include <rest/restapi.h> 0014 0015 #include <QMessageBox> 0016 #include <QNetworkReply> 0017 #include <QUuid> 0018 0019 using namespace KUserFeedback::Console; 0020 0021 SurveyEditor::SurveyEditor(QWidget* parent) : 0022 QWidget(parent), 0023 ui(new Ui::SurveyEditor), 0024 m_surveyModel(new SurveyModel(this)) 0025 { 0026 ui->setupUi(this); 0027 ui->surveyView->setModel(m_surveyModel); 0028 ui->surveyView->addActions({ ui->actionAddSurvey, ui->actionEditSurvey, ui->actionDeleteSurvey }); 0029 connect(ui->surveyView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SurveyEditor::updateActions); 0030 connect(ui->surveyView, &QAbstractItemView::doubleClicked, this, &SurveyEditor::editSurvey); 0031 connect(m_surveyModel, &QAbstractItemModel::modelReset, this, &SurveyEditor::updateActions); 0032 0033 connect(ui->actionAddSurvey, &QAction::triggered, this, &SurveyEditor::createSurvey); 0034 connect(ui->actionEditSurvey, &QAction::triggered, this, &SurveyEditor::editSurvey); 0035 connect(ui->actionDeleteSurvey, &QAction::triggered, this, &SurveyEditor::deleteSurvey); 0036 0037 addActions({ ui->actionAddSurvey, ui->actionEditSurvey, ui->actionDeleteSurvey }); 0038 updateActions(); 0039 } 0040 0041 SurveyEditor::~SurveyEditor() = default; 0042 0043 void SurveyEditor::setRESTClient(RESTClient* client) 0044 { 0045 m_restClient = client; 0046 m_surveyModel->setRESTClient(client); 0047 } 0048 0049 void SurveyEditor::setProduct(const Product& product) 0050 { 0051 m_product = product; 0052 m_surveyModel->setProduct(product); 0053 updateActions(); 0054 } 0055 0056 void SurveyEditor::createSurvey() 0057 { 0058 if (!m_product.isValid()) 0059 return; 0060 SurveyDialog dlg(this); 0061 if (!dlg.exec()) 0062 return; 0063 auto reply = RESTApi::createSurvey(m_restClient, m_product, dlg.survey()); 0064 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0065 if (reply->error() == QNetworkReply::NoError) { 0066 Q_EMIT logMessage(QString::fromUtf8(reply->readAll())); 0067 } 0068 m_surveyModel->reload(); 0069 reply->deleteLater(); 0070 }); 0071 } 0072 0073 void SurveyEditor::editSurvey() 0074 { 0075 if (!m_product.isValid()) 0076 return; 0077 const auto selection = ui->surveyView->selectionModel()->selectedRows(); 0078 if (selection.isEmpty()) 0079 return; 0080 const auto survey = selection.first().data(SurveyModel::SurveyRole).value<Survey>(); 0081 0082 SurveyDialog dlg; 0083 dlg.setSurvey(survey); 0084 if (dlg.exec() != QDialog::Accepted) 0085 return; 0086 0087 auto reply = RESTApi::updateSurvey(m_restClient, dlg.survey()); 0088 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0089 if (reply->error() != QNetworkReply::NoError) 0090 return; 0091 Q_EMIT logMessage(QString::fromUtf8(reply->readAll())); 0092 m_surveyModel->reload(); 0093 reply->deleteLater(); 0094 }); 0095 } 0096 0097 void SurveyEditor::deleteSurvey() 0098 { 0099 if (!m_product.isValid()) 0100 return; 0101 const auto selection = ui->surveyView->selectionModel()->selectedRows(); 0102 if (selection.isEmpty()) 0103 return; 0104 0105 const auto r = QMessageBox::critical(this, tr("Delete Survey"), 0106 tr("Do you really want to delete the selected survey?"), 0107 QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel); 0108 if (r != QMessageBox::Discard) 0109 return; 0110 0111 const auto survey = selection.first().data(SurveyModel::SurveyRole).value<Survey>(); 0112 if (survey.uuid().isNull()) 0113 return; 0114 auto reply = RESTApi::deleteSurvey(m_restClient, survey); 0115 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0116 if (reply->error() != QNetworkReply::NoError) 0117 return; 0118 Q_EMIT logMessage(QString::fromUtf8(reply->readAll())); 0119 m_surveyModel->reload(); 0120 reply->deleteLater(); 0121 }); 0122 } 0123 0124 void SurveyEditor::updateActions() 0125 { 0126 ui->actionAddSurvey->setEnabled(m_product.isValid()); 0127 const auto hasSelection = !ui->surveyView->selectionModel()->selectedRows().isEmpty(); 0128 ui->actionEditSurvey->setEnabled(hasSelection); 0129 ui->actionDeleteSurvey->setEnabled(hasSelection); 0130 } 0131 0132 #include "moc_surveyeditor.cpp"