File indexing completed on 2024-12-15 03:45:03
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include <QtCore/QSettings> 0008 0009 #include "abstractdatasource.h" 0010 #include "abstractdatasource_p.h" 0011 0012 using namespace KUserFeedback; 0013 0014 static bool defaultActiveState() { return true; } 0015 static QString activeStateKey() { return QStringLiteral("activeState"); } 0016 0017 static QString commonSettingsGroupName() { return QStringLiteral("dataSourceCommonSettings"); } 0018 0019 AbstractDataSourcePrivate::AbstractDataSourcePrivate() 0020 : mode(Provider::DetailedUsageStatistics) 0021 , active(defaultActiveState()) 0022 { 0023 } 0024 0025 AbstractDataSourcePrivate::~AbstractDataSourcePrivate() 0026 { 0027 } 0028 0029 void AbstractDataSourcePrivate::storeCommonSettings(QSettings *settings) 0030 { 0031 settings->beginGroup(commonSettingsGroupName()); 0032 0033 settings->setValue(activeStateKey(), active); 0034 0035 settings->endGroup(); 0036 } 0037 0038 void AbstractDataSourcePrivate::loadCommonSettings(QSettings *settings) 0039 { 0040 settings->beginGroup(commonSettingsGroupName()); 0041 0042 active = settings->value(activeStateKey(), defaultActiveState()).toBool(); 0043 0044 settings->endGroup(); 0045 } 0046 0047 // Please note that this function is supposed to be invoked in between 0048 // data submissions, so be careful to don't reset flags that might be 0049 // changed by a user via UI (e.g., active state) 0050 void AbstractDataSourcePrivate::resetCommonSettings(QSettings *settings) 0051 { 0052 Q_UNUSED(settings); 0053 } 0054 0055 AbstractDataSource::AbstractDataSource(const QString &id, Provider::TelemetryMode mode) 0056 : AbstractDataSource(id, mode, nullptr) 0057 { 0058 } 0059 0060 AbstractDataSource::AbstractDataSource(const QString &id, Provider::TelemetryMode mode, 0061 AbstractDataSourcePrivate* dd) 0062 : d_ptr(dd ? dd : new AbstractDataSourcePrivate) 0063 { 0064 d_ptr->id = id; 0065 d_ptr->mode = mode; 0066 } 0067 0068 AbstractDataSource::~AbstractDataSource() 0069 { 0070 delete d_ptr; 0071 } 0072 0073 QString AbstractDataSource::id() const 0074 { 0075 return d_ptr->id; 0076 } 0077 0078 QString AbstractDataSource::name() const 0079 { 0080 return {}; 0081 } 0082 0083 void AbstractDataSource::setId(const QString& id) 0084 { 0085 d_ptr->id = id; 0086 } 0087 0088 void AbstractDataSource::loadImpl(QSettings *settings) 0089 { 0090 Q_UNUSED(settings); 0091 } 0092 0093 void AbstractDataSource::storeImpl(QSettings *settings) 0094 { 0095 Q_UNUSED(settings); 0096 } 0097 0098 void AbstractDataSource::resetImpl(QSettings *settings) 0099 { 0100 Q_UNUSED(settings); 0101 } 0102 0103 void AbstractDataSource::load(QSettings *settings) 0104 { 0105 Q_D(AbstractDataSource); 0106 d->loadCommonSettings(settings); 0107 0108 loadImpl(settings); 0109 } 0110 0111 void AbstractDataSource::store(QSettings *settings) 0112 { 0113 Q_D(AbstractDataSource); 0114 d->storeCommonSettings(settings); 0115 0116 storeImpl(settings); 0117 } 0118 0119 void AbstractDataSource::reset(QSettings *settings) 0120 { 0121 Q_D(AbstractDataSource); 0122 d->resetCommonSettings(settings); 0123 0124 resetImpl(settings); 0125 } 0126 0127 Provider::TelemetryMode AbstractDataSource::telemetryMode() const 0128 { 0129 Q_D(const AbstractDataSource); 0130 0131 Q_ASSERT(d->mode != Provider::NoTelemetry); 0132 if (d->mode == Provider::NoTelemetry) 0133 return Provider::DetailedUsageStatistics; 0134 return d->mode; 0135 } 0136 0137 void AbstractDataSource::setTelemetryMode(Provider::TelemetryMode mode) 0138 { 0139 Q_D(AbstractDataSource); 0140 Q_ASSERT(mode != Provider::NoTelemetry); 0141 d->mode = mode; 0142 } 0143 0144 bool AbstractDataSource::isActive() const 0145 { 0146 Q_D(const AbstractDataSource); 0147 return d->active; 0148 } 0149 0150 void AbstractDataSource::setActive(bool active) 0151 { 0152 Q_D(AbstractDataSource); 0153 d->active = active; 0154 }