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

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020-2021 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 #include "keysmith.h"
0006 #include "../logging_p.h"
0007 
0008 #include "state_p.h"
0009 
0010 #include <QClipboard>
0011 #include <QGuiApplication>
0012 
0013 KEYSMITH_LOGGER(logger, ".app.keysmith")
0014 
0015 namespace app
0016 {
0017 
0018     static QMetaEnum pagesEnum = QMetaEnum::fromType<Navigation::Page>();
0019 
0020     Navigation::Navigation(QQmlEngine *engine) :
0021         QObject(engine), m_engine(engine)
0022     {
0023         Q_ASSERT_X(m_engine, Q_FUNC_INFO, "must have an engine to work with");
0024     }
0025 
0026     QString Navigation::name(Navigation::Page page) const
0027     {
0028         const char *cname = pagesEnum.valueToKey(page);
0029         Q_ASSERT_X(cname, Q_FUNC_INFO, "must be able to lookup pages enum constant's name");
0030         QString result;
0031         result.append(QLatin1String(cname));
0032         return result;
0033     }
0034 
0035     void Navigation::navigate(Navigation::Page page, QObject *modelToTransfer)
0036     {
0037         const QString route = name(page);
0038         if (modelToTransfer) {
0039             m_engine->setObjectOwnership(modelToTransfer, QQmlEngine::JavaScriptOwnership);
0040         }
0041 
0042         qCDebug(logger) << "Requesting switch to route:" << route << "using (view) model:" << modelToTransfer;
0043         Q_EMIT routed(page, modelToTransfer);
0044     }
0045 
0046     void Navigation::push(Navigation::Page page, QObject *modelToTransfer)
0047     {
0048         const QString route = name(page);
0049         if (modelToTransfer) {
0050             m_engine->setObjectOwnership(modelToTransfer, QQmlEngine::JavaScriptOwnership);
0051         }
0052 
0053         qCDebug(logger) << "Requesting to push route:" << route << "using (view) model:" << modelToTransfer;
0054         Q_EMIT pushed(page, modelToTransfer);
0055     }
0056 
0057     static accounts::AccountStorage * openStorage(void)
0058     {
0059         qCDebug(logger) << "Initialising Keysmith account storage...";
0060         const accounts::SettingsProvider settings([](const accounts::PersistenceAction &action) -> void
0061         {
0062             QSettings data(QStringLiteral("org.kde.keysmith"), QStringLiteral("Keysmith"));
0063             action(data);
0064         });
0065         return accounts::AccountStorage::open(settings);
0066     }
0067 
0068     Store::Store(void) :
0069         m_flows(QSharedPointer<FlowState>(new FlowState(), &QObject::deleteLater)),
0070         m_overview(QSharedPointer<OverviewState>(new OverviewState(), &QObject::deleteLater)),
0071         m_accounts(QSharedPointer<accounts::AccountStorage>(openStorage(), &accounts::AccountStorage::dispose)),
0072         m_accountList(QSharedPointer<model::SimpleAccountListModel>(nullptr, &QObject::deleteLater))
0073     {
0074         m_accountList.reset(new model::SimpleAccountListModel(m_accounts.data()));
0075     }
0076 
0077     FlowState * Store::flows(void) const
0078     {
0079         return m_flows.data();
0080     }
0081 
0082     OverviewState * Store::overview(void) const
0083     {
0084         return m_overview.data();
0085     }
0086 
0087     accounts::AccountStorage * Store::accounts(void) const
0088     {
0089         return m_accounts.data();
0090     }
0091 
0092     model::SimpleAccountListModel * Store::accountList(void) const
0093     {
0094         return m_accountList.data();
0095     }
0096 
0097     Keysmith::Keysmith(Navigation *navigation, QObject *parent): QObject(parent), m_store(), m_navigation(navigation)
0098     {
0099     }
0100 
0101     Keysmith::~Keysmith()
0102     {
0103         qCDebug(logger) << "Tearing down Keysmith application; disposal of account storage should be requested";
0104     }
0105 
0106     Navigation * Keysmith::navigation(void) const
0107     {
0108         return m_navigation;
0109     }
0110 
0111     const Store & Keysmith::store(void) const
0112     {
0113         return m_store;
0114     };
0115 
0116     void Keysmith::copyToClipboard(const QString &text)
0117     {
0118         QClipboard * clipboard = QGuiApplication::clipboard();
0119         if (!clipboard) {
0120             qCWarning(logger) << "Unable to copy text: clipboard not available";
0121             return;
0122         }
0123 
0124         clipboard->setText(text);
0125     }
0126 
0127     model::SimpleAccountListModel * Keysmith::accountListModel(void)
0128     {
0129         return new model::SimpleAccountListModel(m_store.accounts(), this);
0130     }
0131 
0132     model::PasswordRequest * Keysmith::passwordRequest(void)
0133     {
0134         return new model::PasswordRequest(m_store.accounts()->secret(), this);
0135     }
0136 }