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 <config-userfeedback.h>
0008 
0009 #include "surveydialog.h"
0010 #include "ui_surveydialog.h"
0011 
0012 
0013 #include <common/surveytargetexpressionparser.h>
0014 
0015 #include <QIcon>
0016 #include <QPushButton>
0017 #include <QUrl>
0018 #include <QUuid>
0019 
0020 using namespace KUserFeedback::Console;
0021 
0022 SurveyDialog::SurveyDialog(QWidget *parent) :
0023     QDialog(parent),
0024     ui(new Ui::SurveyDialog)
0025 {
0026     ui->setupUi(this);
0027     setWindowIcon(QIcon::fromTheme(QStringLiteral("dialog-question")));
0028 
0029     connect(ui->buttonBox->button(QDialogButtonBox::Discard), &QPushButton::clicked, this, &QDialog::reject);
0030 
0031     connect(ui->name, &QLineEdit::textChanged, this, &SurveyDialog::updateState);
0032     connect(ui->url, &QLineEdit::textChanged, this, &SurveyDialog::updateState);
0033     connect(ui->targetExpression, &QPlainTextEdit::textChanged, this, &SurveyDialog::updateState);
0034     updateState();
0035 }
0036 
0037 SurveyDialog::~SurveyDialog() = default;
0038 
0039 Survey SurveyDialog::survey() const
0040 {
0041     Survey s = m_survey;
0042     s.setName(ui->name->text());
0043     s.setUrl(QUrl(ui->url->text()));
0044     s.setTarget(ui->targetExpression->toPlainText());
0045     if (s.uuid().isNull())
0046         s.setUuid(QUuid::createUuid());
0047     return s;
0048 }
0049 
0050 void SurveyDialog::setSurvey(const Survey& survey)
0051 {
0052     m_survey = survey;
0053     ui->name->setText(survey.name());
0054     ui->url->setText(survey.url().toString());
0055     ui->targetExpression->setPlainText(survey.target());
0056     updateState();
0057 }
0058 
0059 void SurveyDialog::updateState()
0060 {
0061     bool valid = true;
0062     valid &= !ui->name->text().isEmpty();
0063     const auto url = QUrl(ui->url->text());
0064     valid &= url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty();
0065 
0066 #ifdef HAVE_SURVEY_TARGET_EXPRESSIONS
0067     if (!ui->targetExpression->toPlainText().isEmpty()) {
0068         SurveyTargetExpressionParser p;
0069         valid &= p.parse(ui->targetExpression->toPlainText());
0070     }
0071 #endif
0072 
0073     ui->buttonBox->button(QDialogButtonBox::Save)->setEnabled(valid);
0074 }
0075 
0076 #include "moc_surveydialog.cpp"