File indexing completed on 2024-12-15 03:45:03

0001 /*
0002     SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "feedbackconfiguicontroller.h"
0008 #include "abstractdatasource.h"
0009 
0010 #include <QGuiApplication>
0011 #include <QSet>
0012 #include <QVector>
0013 
0014 #include <algorithm>
0015 #include <vector>
0016 
0017 using namespace KUserFeedback;
0018 
0019 namespace KUserFeedback {
0020 class FeedbackConfigUiControllerPrivate {
0021 public:
0022     FeedbackConfigUiControllerPrivate();
0023 
0024     Provider *provider;
0025     std::vector<Provider::TelemetryMode> telemetryModeMap;
0026     QString m_appName;
0027 };
0028 }
0029 
0030 FeedbackConfigUiControllerPrivate::FeedbackConfigUiControllerPrivate() :
0031     provider(nullptr),
0032     m_appName(QGuiApplication::applicationDisplayName())
0033 {
0034 }
0035 
0036 FeedbackConfigUiController::FeedbackConfigUiController(QObject* parent)
0037     : QObject(parent)
0038     , d(new FeedbackConfigUiControllerPrivate)
0039 {
0040 }
0041 
0042 FeedbackConfigUiController::~FeedbackConfigUiController()
0043 {
0044 }
0045 
0046 Provider* FeedbackConfigUiController::feedbackProvider() const
0047 {
0048     return d->provider;
0049 }
0050 
0051 void FeedbackConfigUiController::setFeedbackProvider(Provider* provider)
0052 {
0053     if (d->provider == provider)
0054         return;
0055     d->provider = provider;
0056 
0057     d->telemetryModeMap.clear();
0058     d->telemetryModeMap.reserve(5);
0059     d->telemetryModeMap.push_back(Provider::NoTelemetry);
0060     d->telemetryModeMap.push_back(Provider::BasicSystemInformation);
0061     d->telemetryModeMap.push_back(Provider::BasicUsageStatistics);
0062     d->telemetryModeMap.push_back(Provider::DetailedSystemInformation);
0063     d->telemetryModeMap.push_back(Provider::DetailedUsageStatistics);
0064 
0065     QSet<Provider::TelemetryMode> supportedModes;
0066     supportedModes.reserve(d->telemetryModeMap.size());
0067     supportedModes.insert(Provider::NoTelemetry);
0068     foreach (const auto &src, provider->dataSources())
0069         supportedModes.insert(src->telemetryMode());
0070     for (auto it = d->telemetryModeMap.begin(); it != d->telemetryModeMap.end();) {
0071         if (!supportedModes.contains(*it))
0072             it = d->telemetryModeMap.erase(it);
0073         else
0074             ++it;
0075     }
0076 
0077     Q_EMIT providerChanged();
0078 }
0079 
0080 int FeedbackConfigUiController::telemetryModeCount() const
0081 {
0082     return d->telemetryModeMap.size();
0083 }
0084 
0085 int FeedbackConfigUiController::surveyModeCount() const
0086 {
0087     return 3;
0088 }
0089 
0090 Provider::TelemetryMode FeedbackConfigUiController::telemetryIndexToMode(int index) const
0091 {
0092     if (index < 0 || index >= telemetryModeCount())
0093         return Provider::NoTelemetry;
0094     return d->telemetryModeMap[index];
0095 }
0096 
0097 int FeedbackConfigUiController::telemetryModeToIndex(Provider::TelemetryMode mode) const
0098 {
0099     const auto it = std::lower_bound(d->telemetryModeMap.begin(), d->telemetryModeMap.end(), mode);
0100     if (it == d->telemetryModeMap.end())
0101         return 0;
0102     return std::distance(d->telemetryModeMap.begin(), it);
0103 }
0104 
0105 QString FeedbackConfigUiController::telemetryModeName(int telemetryIndex) const
0106 {
0107     return telemetryName(telemetryIndexToMode(telemetryIndex));
0108 }
0109 
0110 QString FeedbackConfigUiController::telemetryModeDescription(int telemetryIndex) const
0111 {
0112     return telemetryDescription(telemetryIndexToMode(telemetryIndex));
0113 }
0114 
0115 QString FeedbackConfigUiController::telemetryName(KUserFeedback::Provider::TelemetryMode mode) const
0116 {
0117     switch (mode) {
0118         case Provider::NoTelemetry:
0119             return tr("Disabled");
0120         case Provider::BasicSystemInformation:
0121             return tr("Basic system information");
0122         case Provider::BasicUsageStatistics:
0123             return tr("Basic system information and usage statistics");
0124         case Provider::DetailedSystemInformation:
0125             return tr("Detailed system information and basic usage statistics");
0126         case Provider::DetailedUsageStatistics:
0127             return tr("Detailed system information and usage statistics");
0128     }
0129 
0130     return {};
0131 }
0132 
0133 QString FeedbackConfigUiController::telemetryDescription(KUserFeedback::Provider::TelemetryMode mode) const
0134 {
0135     const auto name = applicationName();
0136     if (name.isEmpty()) {
0137         switch (mode) {
0138             case Provider::NoTelemetry:
0139                 return tr(
0140                     "Don't share anything"
0141                 );
0142             case Provider::BasicSystemInformation:
0143                 return tr(
0144                     "Share basic system information such as the version of the application and the operating system"
0145                 );
0146             case Provider::BasicUsageStatistics:
0147                 return tr(
0148                     "Share basic system information and basic statistics on how often you use the application"
0149                 );
0150             case Provider::DetailedSystemInformation:
0151                 return tr(
0152                     "Share basic statistics on how often you use the application, as well as more detailed information about your system"
0153                 );
0154             case Provider::DetailedUsageStatistics:
0155                 return tr(
0156                     "Share detailed system information and statistics on how often individual features of the application are used."
0157                 );
0158         }
0159     } else {
0160         switch (mode) {
0161             case Provider::NoTelemetry:
0162                 return tr(
0163                     "Don't share anything"
0164                 );
0165             case Provider::BasicSystemInformation:
0166                 return tr(
0167                     "Share basic system information such as the version of %1 and and the operating system"
0168                 ).arg(name);
0169             case Provider::BasicUsageStatistics:
0170                 return tr(
0171                     "Share basic system information and basic statistics on how often you use %1"
0172                 ).arg(name);
0173             case Provider::DetailedSystemInformation:
0174                 return tr(
0175                     "Share basic statistics on how often you use %1, as well as more detailed information about your system"
0176                 ).arg(name);
0177             case Provider::DetailedUsageStatistics:
0178                 return tr(
0179                     "Share detailed system information and statistics on how often individual features of %1 are used."
0180                 ).arg(name);
0181         }
0182     }
0183 
0184     return QString();
0185 }
0186 
0187 QString FeedbackConfigUiController::telemetryModeDetails(int telemetryIndex) const
0188 {
0189     if (telemetryIndex <= 0 || telemetryIndex >= telemetryModeCount())
0190         return QString();
0191 
0192     auto srcs = d->provider->dataSources();
0193     std::stable_sort(srcs.begin(), srcs.end(), [](AbstractDataSource *lhs, AbstractDataSource *rhs) {
0194         return lhs->telemetryMode() < rhs->telemetryMode();
0195     });
0196 
0197     auto detailsStr = QStringLiteral("<ul>");
0198     foreach (const auto *src, srcs) {
0199         if (telemetryIndex >= telemetryModeToIndex(src->telemetryMode()) && !src->description().isEmpty())
0200             detailsStr += QStringLiteral("<li>") + src->description() + QStringLiteral("</li>");
0201     }
0202     return detailsStr + QStringLiteral("</ul>");
0203 }
0204 
0205 int FeedbackConfigUiController::surveyIndexToInterval(int index) const
0206 {
0207     switch (index) {
0208         case 0: return -1;
0209         case 1: return 90;
0210         case 2: return 0;
0211     }
0212     return -1;
0213 }
0214 
0215 int FeedbackConfigUiController::surveyIntervalToIndex(int interval) const
0216 {
0217     if (interval < 0)
0218         return 0;
0219     else if (interval >= 90)
0220         return 1;
0221     else
0222         return 2;
0223 }
0224 
0225 QString FeedbackConfigUiController::surveyModeDescription(int surveyIndex) const
0226 {
0227     const auto name = applicationName();
0228     if (name.isEmpty()) {
0229         switch (surveyIndex) {
0230             case 0:
0231                 return tr(
0232                     "Don't participate in usability surveys"
0233                 );
0234             case 1:
0235                 return tr(
0236                     "Participate in surveys about the application not more than four times a year"
0237                 );
0238             case 2:
0239                 return tr(
0240                     "Participate in surveys about the application whenever one is available (they can be deferred or skipped)"
0241                 );
0242         }
0243     } else {
0244         switch (surveyIndex) {
0245             case 0:
0246                 return tr(
0247                     "Don't participate in usability surveys about %1"
0248                 ).arg(name);
0249             case 1:
0250                 return tr(
0251                     "Participate in surveys about %1 not more than four times a year"
0252                 ).arg(name);
0253             case 2:
0254                 return tr(
0255                     "Participate in surveys about %1 whenever one is available (they can be deferred or skipped)"
0256                 ).arg(name);
0257         }
0258     }
0259 
0260     return QString();
0261 }
0262 
0263 QString FeedbackConfigUiController::applicationName() const
0264 {
0265     return d->m_appName;
0266 }
0267 
0268 void FeedbackConfigUiController::setApplicationName(const QString& appName)
0269 {
0270     if (appName == d->m_appName)
0271         return;
0272 
0273     d->m_appName = appName;
0274     Q_EMIT applicationNameChanged(appName);
0275 }
0276 
0277 #include "moc_feedbackconfiguicontroller.cpp"