File indexing completed on 2024-05-12 05:04:25

0001 // SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "initialsetupflow.h"
0005 #include "config.h"
0006 
0007 #include <qt6keychain/keychain.h>
0008 
0009 #include <KNotificationPermission>
0010 
0011 class SetupStep : public QObject
0012 {
0013 public:
0014     explicit SetupStep(QObject *parent)
0015         : QObject(parent)
0016     {
0017     }
0018 
0019     virtual bool isSetupNeeded() = 0;
0020     virtual QString moduleName() = 0;
0021 };
0022 
0023 class SetupPasswordStep : public SetupStep
0024 {
0025 public:
0026     explicit SetupPasswordStep(QObject *parent)
0027         : SetupStep(parent)
0028     {
0029     }
0030 
0031     bool isSetupNeeded() override
0032     {
0033         // Sailfish doesn't have a secure backend yet, so always skip this setup
0034 #ifdef SAILFISHOS
0035         return false;
0036 #else
0037         return !QKeychain::isAvailable();
0038 #endif
0039     }
0040 
0041     QString moduleName() override
0042     {
0043         return QStringLiteral("SetupPassword");
0044     }
0045 };
0046 
0047 class SetupNotificationsStep : public SetupStep
0048 {
0049 public:
0050     explicit SetupNotificationsStep(QObject *parent)
0051         : SetupStep(parent)
0052     {
0053     }
0054 
0055     bool isSetupNeeded() override
0056     {
0057         // Don't show the notification permission prompt if the user asked not to
0058         auto config = Config::self();
0059         return !config->promptedNotificationPermission();
0060     }
0061 
0062     QString moduleName() override
0063     {
0064         return QStringLiteral("SetupNotifications");
0065     }
0066 };
0067 
0068 InitialSetupFlow::InitialSetupFlow(QObject *parent)
0069     : QObject(parent)
0070 {
0071     m_steps = {new SetupPasswordStep(this), new SetupNotificationsStep(this)};
0072 }
0073 
0074 InitialSetupFlow::~InitialSetupFlow() = default;
0075 
0076 bool InitialSetupFlow::isSetupNeeded() const
0077 {
0078     return std::any_of(m_steps.cbegin(), m_steps.cend(), [](SetupStep *step) {
0079         return step->isSetupNeeded();
0080     });
0081 }
0082 
0083 QString InitialSetupFlow::getNextStep()
0084 {
0085     while (true) {
0086         if (m_currentStep > m_steps.size()) {
0087             break;
0088         }
0089 
0090         if (m_steps[m_currentStep]->isSetupNeeded()) {
0091             return m_steps[m_currentStep]->moduleName();
0092         } else {
0093             m_currentStep++;
0094         }
0095     }
0096 
0097     return QString();
0098 }
0099 
0100 #include "moc_initialsetupflow.cpp"