Commit 2846b80c authored by Volker Krause's avatar Volker Krause
Browse files

Expand survey handling on the client side.

We now have API for getting notification about a survey, and we can check
if a survey has been completed already.
parent 95dddef2
/*
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/>.
*/
#ifndef USERFEEDBACK_ANALYZER_SURVEYDIALOG_H
#define USERFEEDBACK_ANALYZER_SURVEYDIALOG_H
......
set(userfeedback_core_srcs
provider.cpp
surveyinfo.cpp
)
add_library(UserFeedbackCore SHARED ${userfeedback_core_srcs})
......
......@@ -16,6 +16,7 @@
*/
#include "provider.h"
#include "surveyinfo.h"
#include <QCoreApplication>
#include <QDateTime>
......@@ -26,6 +27,7 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QSettings>
#include <QStringList>
#include <QTime>
#include <QUrl>
......@@ -59,6 +61,8 @@ public:
QUrl serverUrl;
QDateTime lastSubmitTime;
QStringList completedSurveys;
QTime startTime;
int startCount;
int usageTime;
......@@ -93,6 +97,7 @@ void ProviderPrivate::load()
lastSubmitTime = settings.value(QStringLiteral("LastSubmission")).toDateTime();
startCount = std::max(settings.value(QStringLiteral("ApplicationStartCount"), 0).toInt() + 1, 1);
usageTime = std::max(settings.value(QStringLiteral("ApplicationTime"), 0).toInt(), 0);
completedSurveys = settings.value(QStringLiteral("CompletedSurveys"), QStringList()).toStringList();
}
void ProviderPrivate::store()
......@@ -102,6 +107,7 @@ void ProviderPrivate::store()
settings.setValue(QStringLiteral("LastSubmission"), lastSubmitTime);
settings.setValue(QStringLiteral("ApplicationStartCount"), startCount);
settings.setValue(QStringLiteral("ApplicationTime"), currentApplicationTime());
settings.setValue(QStringLiteral("CompletedSurveys"), completedSurveys);
}
void ProviderPrivate::aboutToQuit()
......@@ -136,7 +142,11 @@ void ProviderPrivate::submitFinished()
const auto obj = QJsonDocument::fromJson(reply->readAll()).object();
if (obj.contains(QStringLiteral("survey"))) {
const auto surveyObj = obj.value(QStringLiteral("survey")).toObject();
qDebug() << "got survey: " << surveyObj.value(QStringLiteral("url"));
const auto survey = SurveyInfo::fromJson(surveyObj);
qDebug() << Q_FUNC_INFO << "got survey:" << survey.url();
if (!survey.isValid() || completedSurveys.contains(QString::number(survey.id())))
return;
emit q->surveyAvailable(survey);
}
}
......@@ -162,6 +172,12 @@ void Provider::setFeedbackServer(const QUrl &url)
d->serverUrl = url;
}
void Provider::setSurveyCompleted(const SurveyInfo &info)
{
d->completedSurveys.push_back(QString::number(info.id()));
d->store();
}
void Provider::submit()
{
if (!d->networkAccessManager)
......
......@@ -27,6 +27,7 @@
namespace UserFeedback {
class ProviderPrivate;
class SurveyInfo;
/** The central object managing data sources and transmitting feedback to the server. */
class USERFEEDBACKCORE_EXPORT Provider : public QObject
......@@ -43,10 +44,21 @@ public:
/** Set the feedback server URL. */
void setFeedbackServer(const QUrl &url);
/** Marks the given survey as completed. This avoids getting further notification
* about the same survey.
*/
void setSurveyCompleted(const SurveyInfo &info);
public slots:
/** Manually submit currently recorded data. */
void submit();
signals:
/** Emitted whenever there is a new survey available that can be presented
* to the user.
*/
void surveyAvailable(const SurveyInfo &survey);
private:
friend class ProviderPrivate;
ProviderPrivate * const d;
......
/*
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 "surveyinfo.h"
#include <QJsonObject>
#include <QSharedData>
#include <QUrl>
using namespace UserFeedback;
class UserFeedback::SurveyInfoData : public QSharedData
{
public:
int id;
QUrl url;
};
SurveyInfo::SurveyInfo() : d (new SurveyInfoData)
{
}
SurveyInfo::SurveyInfo(const SurveyInfo&) = default;
SurveyInfo::~SurveyInfo() = default;
SurveyInfo & SurveyInfo::operator=(const SurveyInfo&) = default;
bool SurveyInfo::isValid() const
{
return d->id >= 0 && d->url.isValid();
}
int SurveyInfo::id() const
{
return d->id;
}
void SurveyInfo::setId(int id)
{
d->id = id;
}
QUrl SurveyInfo::url() const
{
return d->url;
}
void SurveyInfo::setUrl(const QUrl& url)
{
d->url = url;
}
SurveyInfo SurveyInfo::fromJson(const QJsonObject& obj)
{
SurveyInfo s;
s.setId(obj.value(QStringLiteral("id")).toInt());
s.setUrl(QUrl(obj.value(QStringLiteral("url")).toString()));
return s;
}
/*
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/>.
*/
#ifndef USERFEEDBACK_SURVEYINFO_H
#define USERFEEDBACK_SURVEYINFO_H
#include "userfeedbackcore_export.h"
#include <QSharedDataPointer>
class QJsonObject;
class QUrl;
namespace UserFeedback {
class SurveyInfoData;
/** Information about a survey request. */
class USERFEEDBACKCORE_EXPORT SurveyInfo
{
public:
SurveyInfo();
SurveyInfo(const SurveyInfo&);
~SurveyInfo();
SurveyInfo& operator=(const SurveyInfo&);
/** Returns @c true if this survey has all necessary information to actually execute it. */
bool isValid() const;
/** Internal unique id of the survey.
* Used to locally check if a user has completed the survey already.
*/
int id() const;
void setId(int id);
/** The URL to the survey website. */
QUrl url() const;
void setUrl(const QUrl &url);
/** @internal */
static SurveyInfo fromJson(const QJsonObject &obj);
private:
QSharedDataPointer<SurveyInfoData> d;
};
}
#endif // USERFEEDBACK_SURVEYINFO_H
......@@ -52,8 +52,10 @@ function post_submit()
$responseData = array();
$surveys = $db->activeSurveysForProduct($product);
if (sizeof($surveys) > 0) {
// TODO pick one somehow, and only send relevant data fields
$responseData['survey'] = $surveys[0];
$surveyInfo = array();
$surveyInfo['id'] = intval($surveys[0]['id']);
$surveyInfo['url'] = $surveys[0]['url'];
$responseData['survey'] = $surveyInfo;
}
echo(json_encode($responseData));
......
......@@ -16,8 +16,10 @@
*/
#include <provider/core/provider.h>
#include <provider/core/surveyinfo.h>
#include <QApplication>
#include <QDesktopServices>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
......@@ -35,6 +37,11 @@ int main(int argc, char** argv)
provider.setProductIdentifier(QStringLiteral("org.kde.orwell"));
provider.setFeedbackServer(QUrl(QStringLiteral("https://feedback.volkerkrause.eu")));
QObject::connect(&provider, &UserFeedback::Provider::surveyAvailable, &app, [&provider](const UserFeedback::SurveyInfo &info) {
QDesktopServices::openUrl(info.url());
provider.setSurveyCompleted(info);
});
// TODO
QWidget top;
top.show();
......
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