Commit b0c5a0a7 authored by Volker Krause's avatar Volker Krause
Browse files

Add survey dialog, and ability to delete surveys.

parent e8b1e7c0
......@@ -8,6 +8,7 @@ set(analyzer_srcs
restclient.cpp
serverinfo.cpp
survey.cpp
surveydialog.cpp
surveymodel.cpp
)
......
......@@ -23,6 +23,7 @@
#include "productmodel.h"
#include "restclient.h"
#include "serverinfo.h"
#include "surveydialog.h"
#include "surveymodel.h"
#include <QApplication>
......@@ -108,13 +109,10 @@ MainWindow::MainWindow() :
const auto product = selectedProduct();
if (product.isEmpty())
return;
const auto surveyUrl = QInputDialog::getText(this, tr("Add New Survey"), tr("Survey URL:"));
if (surveyUrl.isEmpty())
SurveyDialog dlg(this);
if (!dlg.exec())
return;
Survey survey;
survey.setName(surveyUrl); // TODO
survey.setUrl(QUrl(surveyUrl));
auto reply = m_restClient->post(QStringLiteral("surveys/") + product, survey.toJson());
auto reply = m_restClient->post(QStringLiteral("surveys/") + product, dlg.survey().toJson());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
logMessage(QString::fromUtf8(reply->readAll()));
......@@ -128,7 +126,20 @@ MainWindow::MainWindow() :
const auto product = selectedProduct();
if (product.isEmpty())
return;
// TODO
// TODO safety check
const auto selection = ui->surveyView->selectionModel()->selectedRows();
if (selection.isEmpty())
return;
const auto survey = selection.first().data(SurveyModel::SurveyRole).value<Survey>();
if (survey.id() < 0)
return;
auto reply = m_restClient->deleteResource(QStringLiteral("surveys/") + QString::number(survey.id()));
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
return;
logMessage(QString::fromUtf8(reply->readAll()));
m_surveyModel->reload();
});
});
ui->actionQuit->setShortcut(QKeySequence::Quit);
......
......@@ -17,6 +17,7 @@
#include "survey.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
......@@ -34,6 +35,7 @@ class SurveyData : public QSharedData
public:
QString name;
QUrl url;
int id = -1;
};
}
......@@ -44,6 +46,16 @@ Survey::Survey(const Survey&) = default;
Survey::~Survey() = default;
Survey& Survey::operator=(const Survey&) = default;
int Survey::id() const
{
return d->id;
}
void Survey::setId(int id)
{
d->id = id;
}
QString Survey::name() const
{
return d->name;
......@@ -67,6 +79,7 @@ void Survey::setUrl(const QUrl& url)
QByteArray Survey::toJson() const
{
QJsonObject obj;
obj.insert(QStringLiteral("id"), id());
obj.insert(QStringLiteral("name"), name());
obj.insert(QStringLiteral("url"), url().toString());
return QJsonDocument(obj).toJson();
......@@ -78,6 +91,13 @@ QVector<Survey> Survey::fromJson(const QByteArray &data)
foreach (const auto &v, QJsonDocument::fromJson(data).array()) {
const auto obj = v.toObject();
Survey survey;
const auto id = obj.value(QStringLiteral("id"));
// TODO move this to a helper function
if (id.isDouble())
survey.setId(id.toInt(-1));
else if (id.isString())
survey.setId(id.toString().toInt());
survey.setName(obj.value(QStringLiteral("name")).toString());
survey.setUrl(QUrl(obj.value(QStringLiteral("url")).toString()));
surveys.push_back(survey);
......
......@@ -18,6 +18,7 @@
#ifndef USERFEEDBACK_ANALYZER_SURVEY_H
#define USERFEEDBACK_ANALYZER_SURVEY_H
#include <QMetaType>
#include <QSharedDataPointer>
#include <QVector>
......@@ -38,6 +39,9 @@ public:
~Survey();
Survey& operator=(const Survey&);
int id() const;
void setId(int id);
QString name() const;
void setName(const QString& name);
......@@ -54,5 +58,6 @@ private:
}
Q_DECLARE_TYPEINFO(UserFeedback::Analyzer::Survey, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(UserFeedback::Analyzer::Survey)
#endif // USERFEEDBACK_ANALYZER_SURVEY_H
/*
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 "surveydialog.h"
#include "ui_surveydialog.h"
#include "survey.h"
#include <QIcon>
#include <QUrl>
using namespace UserFeedback::Analyzer;
SurveyDialog::SurveyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SurveyDialog)
{
ui->setupUi(this);
setWindowIcon(QIcon::fromTheme(QStringLiteral("dialog-question")));
}
SurveyDialog::~SurveyDialog() = default;
Survey SurveyDialog::survey() const
{
Survey s;
s.setName(ui->name->text());
s.setUrl(QUrl(ui->url->text()));
return s;
}
void SurveyDialog::setSurvey(const Survey& survey)
{
ui->name->setText(survey.name());
ui->url->setText(survey.url().toString());
}
#ifndef USERFEEDBACK_ANALYZER_SURVEYDIALOG_H
#define USERFEEDBACK_ANALYZER_SURVEYDIALOG_H
#include <QDialog>
#include <memory>
class QUrl;
namespace UserFeedback {
namespace Analyzer {
namespace Ui
{
class SurveyDialog;
}
class Survey;
class SurveyDialog : public QDialog
{
Q_OBJECT
public:
explicit SurveyDialog(QWidget *parent = nullptr);
~SurveyDialog();
Survey survey() const;
void setSurvey(const Survey &survey);
private:
std::unique_ptr<Ui::SurveyDialog> ui;
};
}
}
#endif // USERFEEDBACK_ANALYZER_SURVEYDIALOG_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UserFeedback::Analyzer::SurveyDialog</class>
<widget class="QDialog" name="UserFeedback::Analyzer::SurveyDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Survey</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="buddy">
<cstring>name</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="name"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;URL:</string>
</property>
<property name="buddy">
<cstring>url</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="url"/>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>UserFeedback::Analyzer::SurveyDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>UserFeedback::Analyzer::SurveyDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
......@@ -85,6 +85,8 @@ QVariant SurveyModel::data(const QModelIndex& index, int role) const
case 0: return survey.name();
case 1: return survey.url().toString();
}
} else if (role == SurveyRole) {
return QVariant::fromValue(m_surveys.at(index.row()));
}
return {};
}
......
......@@ -31,6 +31,10 @@ class SurveyModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Role {
SurveyRole = Qt::UserRole + 1
};
explicit SurveyModel(QObject *parent = nullptr);
~SurveyModel();
......
......@@ -101,9 +101,11 @@ public function post_surveys($product)
}
/** Delete survey. */
public function delete_surveys($product, $survey)
public function delete_surveys($survey)
{
echo("TODO: DELETE surveys for $product/$survey.");
$db = new DataStore();
$db->deleteSurvey($survey);
echo("Survey deleted.");
}
}
......
......@@ -183,6 +183,14 @@ public function addSurvey($productId, $survey)
$this->fatalDbError();
}
/** Delete a survey given its internal id. */
public function deleteSurvey($surveyId)
{
$res = $this->db->exec('DELETE FROM surveys WHERE id = ' . $this->db->quote($surveyId));
if ($res === FALSE)
$this->fatalDbError();
}
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment