File indexing completed on 2024-04-28 05:50:07

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2021 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 #include "vms.h"
0006 #include "state_p.h"
0007 #include "flows_p.h"
0008 
0009 namespace app
0010 {
0011     AddAccountViewModel::AddAccountViewModel(model::AccountInput *input, model::SimpleAccountListModel *accounts,
0012                                              bool quitEnabled, bool validateAvailability, QObject *parent) :
0013         QObject(parent), m_input(input), m_accounts(accounts), m_quitEnabled(quitEnabled),
0014         m_validateAvailability(validateAvailability)
0015     {
0016     }
0017 
0018     model::AccountInput * AddAccountViewModel::input(void) const
0019     {
0020         return m_input;
0021     }
0022 
0023     model::SimpleAccountListModel * AddAccountViewModel::accounts(void) const
0024     {
0025         return m_accounts;
0026     }
0027 
0028     bool AddAccountViewModel::validateAvailability(void) const
0029     {
0030         return m_validateAvailability;
0031     }
0032 
0033     bool AddAccountViewModel::quitEnabled(void) const
0034     {
0035         return m_quitEnabled;
0036     }
0037 
0038     RenameAccountViewModel::RenameAccountViewModel(model::AccountInput *input, model::SimpleAccountListModel *accounts,
0039                                                    QObject *parent) :
0040         QObject(parent), m_input(input), m_accounts(accounts)
0041     {
0042     }
0043 
0044     model::AccountInput * RenameAccountViewModel::input(void) const
0045     {
0046         return m_input;
0047     }
0048 
0049     model::SimpleAccountListModel * RenameAccountViewModel::accounts(void) const
0050     {
0051         return m_accounts;
0052     }
0053 
0054     ErrorViewModel::ErrorViewModel(const QString &errorTitle, const QString &errorText, bool quitEnabled,
0055                                    QObject *parent) :
0056         QObject(parent), m_title(errorTitle), m_error(errorText), m_quitEnabled(quitEnabled)
0057     {
0058     }
0059 
0060     QString ErrorViewModel::error(void) const
0061     {
0062         return m_error;
0063     }
0064 
0065     QString ErrorViewModel::title(void) const
0066     {
0067         return m_title;
0068     }
0069 
0070     bool ErrorViewModel::quitEnabled(void) const
0071     {
0072         return m_quitEnabled;
0073     }
0074 
0075     OverviewState * overviewStateOf(Keysmith *app)
0076     {
0077         auto overview = app->store().overview();
0078         Q_ASSERT_X(overview, Q_FUNC_INFO, "should have a valid overview state object");
0079         return overview;
0080     }
0081 
0082     FlowState * flowStateOf(Keysmith *app)
0083     {
0084         auto flows = app->store().flows();
0085         Q_ASSERT_X(flows, Q_FUNC_INFO, "should have a valid flow state object");
0086         return flows;
0087     }
0088 
0089     model::SimpleAccountListModel * accountListOf(Keysmith *app)
0090     {
0091         Q_ASSERT_X(app, Q_FUNC_INFO, "should have a Keysmith application instance");
0092 
0093         auto list = app->store().accountList();
0094 
0095         Q_ASSERT_X(list, Q_FUNC_INFO, "should have an SimpleAccountListModel instance");
0096         return list;
0097     }
0098 
0099     AccountsOverviewViewModel::AccountsOverviewViewModel(Keysmith *app) :
0100         QObject(), m_app(app), m_accounts(accountListOf(app))
0101     {
0102         QObject::connect(overviewStateOf(m_app), &OverviewState::actionsEnabledChanged,
0103                          this, &AccountsOverviewViewModel::actionsEnabledChanged);
0104     }
0105 
0106     model::SimpleAccountListModel * AccountsOverviewViewModel::accounts(void) const
0107     {
0108         return m_accounts;
0109     }
0110 
0111     bool AccountsOverviewViewModel::actionsEnabled(void) const
0112     {
0113         return overviewStateOf(m_app)->actionsEnabled();
0114     }
0115 
0116     void AccountsOverviewViewModel::addNewAccount(void)
0117     {
0118         auto flow = new ManualAddAccountFlow(m_app);
0119         flow->run();
0120     }
0121 
0122     PasswordViewModel::PasswordViewModel(model::PasswordRequest *request, QObject *parent) :
0123         QObject(parent), m_failed(false), m_request(request)
0124     {
0125         Q_ASSERT_X(request, Q_FUNC_INFO, "should have a valid password request object");
0126         QObject::connect(m_request, &model::PasswordRequest::passwordRejected, this, &PasswordViewModel::rejected);
0127         QObject::connect(m_request, &model::PasswordRequest::passwordStateChanged,
0128                          this, &PasswordViewModel::busyChanged);
0129     }
0130 
0131     PasswordViewModel::~PasswordViewModel()
0132     {
0133     }
0134 
0135     bool PasswordViewModel::busy(void) const
0136     {
0137         return m_request->passwordProvided();
0138     }
0139 
0140     bool PasswordViewModel::failed(void) const
0141     {
0142         return m_failed;
0143     }
0144 
0145     void PasswordViewModel::rejected(void)
0146     {
0147         if (!m_failed) {
0148             m_failed = true;
0149             Q_EMIT failedChanged();
0150         }
0151     }
0152 
0153     SetupPasswordViewModel::SetupPasswordViewModel(model::PasswordRequest *request, QObject *parent) :
0154         PasswordViewModel(request, parent)
0155     {
0156     }
0157 
0158     void SetupPasswordViewModel::setup(const QString &password, const QString &confirmedPassword)
0159     {
0160         bool result = !m_request->provideBothPasswords(password, confirmedPassword);
0161         if (result != m_failed) {
0162             m_failed = result;
0163             Q_EMIT failedChanged();
0164         }
0165     }
0166 
0167     UnlockAccountsViewModel::UnlockAccountsViewModel(model::PasswordRequest *request, QObject *parent) :
0168         PasswordViewModel(request, parent)
0169     {
0170     }
0171 
0172     void UnlockAccountsViewModel::unlock(const QString &password)
0173     {
0174         bool result = !m_request->providePassword(password);
0175         if (result != m_failed) {
0176             m_failed = result;
0177             Q_EMIT failedChanged();
0178         }
0179     }
0180 }