Commit 037e1a08 authored by Vitaly Fanaskov's avatar Vitaly Fanaskov Committed by Volker Krause

Add method to check whether a data source is active or not

Summary:
"Active" state means that all data collected by the data source will be
sent to a server.
This change aims to give a user more control of what data they want to
send. Without this functionality the user might decide to reduce
telemetry level because of one small part of it. This means that we can
collect less statistic without a really good reason.

Reviewers: vkrause

Differential Revision: https://phabricator.kde.org/D19757
parent 0858b9de
......@@ -254,6 +254,38 @@ private slots:
QVERIFY(m.contains(QLatin1String("name")));
QVERIFY(!m.value(QLatin1String("name")).toString().isEmpty());
}
void testActiveState()
{
CpuInfoSource src;
QVERIFY(src.isActive());
src.setActive(false);
QVERIFY(!src.isActive());
}
void testCommonActiveStateSetting()
{
CpuInfoSource src;
src.setActive(false);
QSettings s;
src.store(&s);
src.setActive(true);
src.load(&s);
QVERIFY(!src.isActive());
// NOTE: reset shouldn't change global settings that might be changed by a user via UI
src.reset(&s);
QVERIFY(!src.isActive());
}
};
QTEST_MAIN(DataSourceTest)
......
......@@ -15,13 +15,21 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtCore/QSettings>
#include "abstractdatasource.h"
#include "abstractdatasource_p.h"
using namespace KUserFeedback;
static bool defaultActiveState() { return true; }
static QString activeStateKey() { return QStringLiteral("activeState"); }
static QString commonSettingsGroupName() { return QStringLiteral("dataSourceCommonSettings"); }
AbstractDataSourcePrivate::AbstractDataSourcePrivate()
: mode(Provider::DetailedUsageStatistics)
, active(defaultActiveState())
{
}
......@@ -29,6 +37,32 @@ AbstractDataSourcePrivate::~AbstractDataSourcePrivate()
{
}
void AbstractDataSourcePrivate::storeCommonSettings(QSettings *settings)
{
settings->beginGroup(commonSettingsGroupName());
settings->setValue(activeStateKey(), active);
settings->endGroup();
}
void AbstractDataSourcePrivate::loadCommonSettings(QSettings *settings)
{
settings->beginGroup(commonSettingsGroupName());
active = settings->value(activeStateKey(), defaultActiveState()).toBool();
settings->endGroup();
}
// Please note that this function is supposed to be invoked in between
// data submissions, so be careful to don't reset flags that might be
// changed by a user via UI (e.g., active state)
void AbstractDataSourcePrivate::resetCommonSettings(QSettings *settings)
{
Q_UNUSED(settings);
}
AbstractDataSource::AbstractDataSource(const QString &id, Provider::TelemetryMode mode,
AbstractDataSourcePrivate* dd)
: d_ptr(dd ? dd : new AbstractDataSourcePrivate)
......@@ -57,21 +91,45 @@ void AbstractDataSource::setId(const QString& id)
d_ptr->id = id;
}
void AbstractDataSource::load(QSettings *settings)
void AbstractDataSource::loadImpl(QSettings *settings)
{
Q_UNUSED(settings);
}
void AbstractDataSource::store(QSettings *settings)
void AbstractDataSource::storeImpl(QSettings *settings)
{
Q_UNUSED(settings);
}
void AbstractDataSource::reset(QSettings *settings)
void AbstractDataSource::resetImpl(QSettings *settings)
{
Q_UNUSED(settings);
}
void AbstractDataSource::load(QSettings *settings)
{
Q_D(AbstractDataSource);
d->loadCommonSettings(settings);
loadImpl(settings);
}
void AbstractDataSource::store(QSettings *settings)
{
Q_D(AbstractDataSource);
d->storeCommonSettings(settings);
storeImpl(settings);
}
void AbstractDataSource::reset(QSettings *settings)
{
Q_D(AbstractDataSource);
d->resetCommonSettings(settings);
resetImpl(settings);
}
Provider::TelemetryMode AbstractDataSource::telemetryMode() const
{
Q_D(const AbstractDataSource);
......@@ -88,3 +146,15 @@ void AbstractDataSource::setTelemetryMode(Provider::TelemetryMode mode)
Q_ASSERT(mode != Provider::NoTelemetry);
d->mode = mode;
}
bool AbstractDataSource::isActive() const
{
Q_D(const AbstractDataSource);
return d->active;
}
void AbstractDataSource::setActive(bool active)
{
Q_D(AbstractDataSource);
d->active = active;
}
......@@ -83,13 +83,13 @@ public:
* @param settings A QSettings object opened in a dedicated group for loading
* persistent data.
*/
virtual void load(QSettings *settings);
void load(QSettings *settings);
/*! Store persistent state for this data source.
* @param settings A QSettings object opened in a dedicated group for storing
* persistent data.
*/
virtual void store(QSettings *settings);
void store(QSettings *settings);
/*! Reset the persistent state of this data source.
* This is called after a successful submission of data, and can be used
......@@ -98,7 +98,7 @@ public:
* @param settings A QSettings object opened in the dedicated group of this
* data source.
*/
virtual void reset(QSettings *settings);
void reset(QSettings *settings);
/*! Returns which telemetry colleciton mode this data source belongs to.
* @return The telemetry collection category this source belongs to.
......@@ -111,6 +111,20 @@ public:
*/
void setTelemetryMode(Provider::TelemetryMode mode);
/*! Checks whether this data source is active or not
* If the data source is not active, then collected
* data neither will be sent to a server nor appeared
* in the audit log.
* Data source is active by default.
* @return true if active, false otherwise
*/
bool isActive() const;
/*! Changes active state of the data source
* @param active The new active state for this data source
*/
void setActive(bool active);
protected:
/*! Create a new data source named @p name.
* The name of the data source must match the corresponding
......@@ -129,6 +143,27 @@ protected:
*/
void setId(const QString &id);
/*! Invoked by @p load() in order to load individual settings of this data source.
* @see load() description for further details.
* @param settings A QSettings object opened in a dedicated group for loading
* persistent data.
*/
virtual void loadImpl(QSettings *settings);
/*! Invoked by @p store() in order to store individual settings of this data source.
* @see store() description for further details.
* @param settings A QSettings object opened in a dedicated group for loading
* persistent data.
*/
virtual void storeImpl(QSettings *settings);
/*! Invoked by @p reset() in order to reset individual settings of this data source.
* @see reset() description for further details.
* @param settings A QSettings object opened in a dedicated group for loading
* persistent data.
*/
virtual void resetImpl(QSettings *settings);
///@cond internal
class AbstractDataSourcePrivate* const d_ptr;
///@endcond
......
......@@ -20,6 +20,10 @@
#include "provider.h"
QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace KUserFeedback {
class AbstractDataSourcePrivate
......@@ -30,6 +34,11 @@ public:
QString id;
Provider::TelemetryMode mode;
bool active;
void storeCommonSettings(QSettings *settings);
void loadCommonSettings(QSettings *settings);
void resetCommonSettings(QSettings *settings);
private:
Q_DISABLE_COPY(AbstractDataSourcePrivate)
......
......@@ -210,7 +210,7 @@ QVariant PropertyRatioSource::data()
return m;
}
void PropertyRatioSource::load(QSettings *settings)
void PropertyRatioSource::loadImpl(QSettings *settings)
{
Q_D(PropertyRatioSource);
foreach (const auto &value, settings->childKeys()) {
......@@ -221,7 +221,7 @@ void PropertyRatioSource::load(QSettings *settings)
}
}
void PropertyRatioSource::store(QSettings *settings)
void PropertyRatioSource::storeImpl(QSettings *settings)
{
Q_D(PropertyRatioSource);
d->propertyChanged();
......@@ -238,7 +238,7 @@ void PropertyRatioSource::store(QSettings *settings)
}
}
void PropertyRatioSource::reset(QSettings* settings)
void PropertyRatioSource::resetImpl(QSettings* settings)
{
Q_D(PropertyRatioSource);
d->baseRatioSet.clear();
......
......@@ -73,9 +73,10 @@ public:
void setDescription(const QString &desc);
QVariant data() override;
void load(QSettings *settings) override;
void store(QSettings *settings) override;
void reset(QSettings *settings) override;
void loadImpl(QSettings *settings) override;
void storeImpl(QSettings *settings) override;
void resetImpl(QSettings *settings) override;
using AbstractDataSource::setId;
private:
......
......@@ -222,7 +222,7 @@ QByteArray ProviderPrivate::jsonData(Provider::TelemetryMode mode) const
QJsonObject obj;
if (mode != Provider::NoTelemetry) {
foreach (auto source, dataSources) {
if (!isValidSource(source))
if (!isValidSource(source) || !source->isActive())
continue;
if (mode < source->telemetryMode())
continue;
......@@ -682,7 +682,7 @@ void ProviderPrivate::writeAuditLog(const QDateTime &dt)
QJsonObject docObj;
foreach (auto source, dataSources) {
if (!isValidSource(source) || telemetryMode < source->telemetryMode())
if (!isValidSource(source) || !source->isActive() || telemetryMode < source->telemetryMode())
continue;
QJsonObject obj;
const auto data = source->data();
......
......@@ -138,7 +138,7 @@ QVariant SelectionRatioSource::data()
return m;
}
void SelectionRatioSource::load(QSettings *settings)
void SelectionRatioSource::loadImpl(QSettings *settings)
{
Q_D(SelectionRatioSource);
foreach (const auto &value, settings->childKeys()) {
......@@ -149,7 +149,7 @@ void SelectionRatioSource::load(QSettings *settings)
}
}
void SelectionRatioSource::store(QSettings *settings)
void SelectionRatioSource::storeImpl(QSettings *settings)
{
Q_D(SelectionRatioSource);
d->selectionChanged();
......@@ -166,7 +166,7 @@ void SelectionRatioSource::store(QSettings *settings)
}
}
void SelectionRatioSource::reset(QSettings* settings)
void SelectionRatioSource::resetImpl(QSettings* settings)
{
Q_D(SelectionRatioSource);
d->baseRatioSet.clear();
......
......@@ -61,9 +61,9 @@ public:
void setDescription(const QString &desc);
QVariant data() override;
void load(QSettings *settings) override;
void store(QSettings *settings) override;
void reset(QSettings *settings) override;
void loadImpl(QSettings *settings) override;
void storeImpl(QSettings *settings) override;
void resetImpl(QSettings *settings) override;
private:
Q_DECLARE_PRIVATE(SelectionRatioSource)
......
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