Commit 83af709c authored by Volker Krause's avatar Volker Krause
Browse files

Implement contribution encouragement.

parent bb947d44
......@@ -165,6 +165,7 @@ MainWindow::MainWindow() :
m_feedbackProvider->setFeedbackServer(QUrl(QStringLiteral("https://feedback.volkerkrause.eu")));
m_feedbackProvider->setSubmissionInterval(1);
m_feedbackProvider->setApplicationUsageTimeUntilEncouragement(10);
}
MainWindow::~MainWindow()
......
......@@ -55,8 +55,12 @@ public:
QByteArray jsonData() const;
void scheduleNextSubmission();
void submitFinished();
void selectSurvey(const SurveyInfo &survey) const;
void scheduleEncouragement();
void emitShowEncouragementMessage();
Provider *q;
QString productId;
......@@ -75,6 +79,12 @@ public:
QTime startTime;
int startCount;
int usageTime;
QTimer encouragementTimer;
int encouragementStarts;
int encouragementTime;
int encouragementDelay;
bool encouragementDisplayed;
};
}
......@@ -86,6 +96,10 @@ ProviderPrivate::ProviderPrivate(Provider *qq)
, surveyInterval(-1)
, startCount(0)
, usageTime(0)
, encouragementStarts(-1)
, encouragementTime(-1)
, encouragementDelay(300)
, encouragementDisplayed(false)
{
auto domain = QCoreApplication::organizationDomain().split(QLatin1Char('.'));
std::reverse(domain.begin(), domain.end());
......@@ -95,6 +109,9 @@ ProviderPrivate::ProviderPrivate(Provider *qq)
QObject::connect(&submissionTimer, SIGNAL(timeout()), q, SLOT(submit()));
startTime.start();
encouragementTimer.setSingleShot(true);
QObject::connect(&encouragementTimer, SIGNAL(timeout()), q, SLOT(emitShowEncouragementMessage()));
}
void ProviderPrivate::reset()
......@@ -123,6 +140,8 @@ void ProviderPrivate::load()
startCount = std::max(settings.value(QStringLiteral("ApplicationStartCount"), 0).toInt() + 1, 1);
usageTime = std::max(settings.value(QStringLiteral("ApplicationTime"), 0).toInt(), 0);
encouragementDisplayed = settings.value(QStringLiteral("EncouragementDisplayed"), false).toBool();
}
void ProviderPrivate::store()
......@@ -138,6 +157,8 @@ void ProviderPrivate::store()
settings.setValue(QStringLiteral("ApplicationStartCount"), startCount);
settings.setValue(QStringLiteral("ApplicationTime"), currentApplicationTime());
settings.setValue(QStringLiteral("EncouragementDisplayed"), encouragementDisplayed);
}
void ProviderPrivate::aboutToQuit()
......@@ -211,6 +232,31 @@ void ProviderPrivate::selectSurvey(const SurveyInfo &survey) const
emit q->surveyAvailable(survey);
}
void ProviderPrivate::scheduleEncouragement()
{
encouragementTimer.stop();
if (encouragementStarts < 0 && encouragementTime < 0) // encouragement disabled
return;
if (encouragementStarts > startCount) // we need more starts
return;
if (statisticsMode == Provider::AllStatistics && surveyInterval == 0) // already everything enabled
return;
Q_ASSERT(encouragementDelay >= 0);
int timeToEncouragement = encouragementDelay;
if (encouragementTime > 0)
timeToEncouragement = std::max(timeToEncouragement, (encouragementTime * 60) - currentApplicationTime());
encouragementTimer.start(timeToEncouragement);
}
void ProviderPrivate::emitShowEncouragementMessage()
{
encouragementDisplayed = true; // TODO make this explicit, in case the host application decides to delay?
emit q->showEncouragementMessage();
}
Provider::Provider(QObject *parent) :
QObject(parent),
......@@ -253,6 +299,7 @@ void Provider::setStatisticsCollectionMode(StatisticsCollectionMode mode)
{
d->statisticsMode = mode;
d->scheduleNextSubmission();
d->scheduleEncouragement();
}
int Provider::surveyInterval() const
......@@ -264,6 +311,25 @@ void Provider::setSurveyInterval(int days)
{
d->surveyInterval = days;
d->scheduleNextSubmission();
d->scheduleEncouragement();
}
void Provider::setApplicationStartsUntilEncouragement(int starts)
{
d->encouragementStarts = starts;
d->scheduleEncouragement();
}
void Provider::setApplicationUsageTimeUntilEncouragement(int minutes)
{
d->encouragementTime = minutes;
d->scheduleEncouragement();
}
void Provider::setEncouragementDelay(int secs)
{
d->encouragementDelay = std::max(0, secs);
d->scheduleEncouragement();
}
void Provider::setSurveyCompleted(const SurveyInfo &info)
......
......@@ -74,6 +74,15 @@ public:
*/
void setSurveyCompleted(const SurveyInfo &info);
/** Set the amount of application starts until the encouragement message should be shown. */
void setApplicationStartsUntilEncouragement(int starts);
/** Set the amount of usage time until the encouragement message should be shown. */
void setApplicationUsageTimeUntilEncouragement(int minutes);
/** Set the delay after application start for the earliest display of the encouragement message. */
void setEncouragementDelay(int secs);
public slots:
/** Manually submit currently recorded data. */
void submit();
......@@ -84,11 +93,15 @@ signals:
*/
void surveyAvailable(const UserFeedback::SurveyInfo &survey);
/** Indicate that the encouragement notice should be shown. */
void showEncouragementMessage();
private:
friend class ProviderPrivate;
ProviderPrivate * const d;
Q_PRIVATE_SLOT(d, void aboutToQuit())
Q_PRIVATE_SLOT(d, void submitFinished())
Q_PRIVATE_SLOT(d, void emitShowEncouragementMessage())
// for testing
Q_PRIVATE_SLOT(d, void load())
Q_PRIVATE_SLOT(d, void store())
......
......@@ -16,6 +16,7 @@
*/
#include "notificationwidget.h"
#include "feedbackconfigdialog.h"
#include <provider/core/provider.h>
#include <provider/core/surveyinfo.h>
......@@ -38,6 +39,7 @@ public:
void clearActions();
void surveyAvailable(const SurveyInfo &survey);
void showEncouragement();
NotificationWidget *q;
Provider *provider;
......@@ -69,6 +71,26 @@ void NotificationWidgetPrivate::surveyAvailable(const SurveyInfo &survey)
q->animatedShow();
}
void NotificationWidgetPrivate::showEncouragement()
{
Q_ASSERT(provider);
clearActions();
q->setText(NotificationWidget::tr("Contribute!")); // TODO
auto configAction = new QAction(q);
configAction->setText(NotificationWidget::tr("Contribute..."));
QObject::connect(configAction, &QAction::triggered, q, [this]() {
FeedbackConfigDialog dlg(q);
dlg.setFeedbackProvider(provider);
dlg.exec();
q->animatedHide();
});
q->addAction(configAction);
q->animatedShow();
}
NotificationWidget::NotificationWidget(QWidget *parent) :
KMessageWidget(parent),
......@@ -90,4 +112,5 @@ void NotificationWidget::setFeedbackProvider(Provider* provider)
connect(provider, &Provider::surveyAvailable, this, [this](const SurveyInfo &survey) {
d->surveyAvailable(survey);
});
connect(provider, &Provider::showEncouragementMessage, this, [this]() { d->showEncouragement(); });
}
......@@ -94,6 +94,8 @@ int main(int argc, char** argv)
provider.reset(new UserFeedback::Provider);
provider->setFeedbackServer(QUrl(QStringLiteral("https://feedback.volkerkrause.eu")));
provider->setSubmissionInterval(1);
provider->setApplicationStartsUntilEncouragement(5);
provider->setEncouragementDelay(10);
Orwell mainWindow;
mainWindow.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