File indexing completed on 2024-04-21 04:44:33

0001 /*
0002  * <one line to give the program's name and a brief idea of what it does.>
0003  * Copyright (C) 2019  camilo <chiguitar@unal.edu.co>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #pragma once
0020 #include <QObject>
0021 
0022 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0023 #include <MauiKit3/Core/mauilist.h>
0024 #else
0025 #include <MauiKit4/Core/mauilist.h>
0026 #endif
0027 
0028 #include "accounts_export.h"
0029 
0030 class AccountsDB;
0031 
0032 /**
0033  * @brief MauiAccounts class provides an interface to access the system registered accounts, the current active account and a bridge to add account data to the host system backend solution.
0034  * This properties and functions are exposed to all the Maui Applications that might want to use the accounts with the `Accounts` property.
0035  */
0036 class ACCOUNTS_EXPORT MauiAccounts : public MauiList
0037 {
0038     Q_OBJECT
0039     Q_PROPERTY(int currentAccountIndex READ getCurrentAccountIndex WRITE setCurrentAccountIndex NOTIFY currentAccountIndexChanged)
0040     Q_PROPERTY(QVariantMap currentAccount READ getCurrentAccount NOTIFY currentAccountChanged)
0041 
0042 public:
0043     static MauiAccounts *instance()
0044     {
0045         if (m_instance)
0046             return m_instance;
0047 
0048         m_instance = new MauiAccounts;
0049         return m_instance;
0050     }
0051 
0052     MauiAccounts(const MauiAccounts &) = delete;
0053     MauiAccounts &operator=(const MauiAccounts &) = delete;
0054     MauiAccounts(MauiAccounts &&) = delete;
0055     MauiAccounts &operator=(MauiAccounts &&) = delete;
0056 
0057     const FMH::MODEL_LIST &items() const final override;
0058 
0059     /**
0060      * @brief setCurrentAccountIndex
0061      * @param index
0062      */
0063     void setCurrentAccountIndex(const int &index);
0064 
0065     /**
0066      * @brief getCurrentAccountIndex
0067      * @return
0068      */
0069     int getCurrentAccountIndex() const;
0070 
0071     /**
0072      * @brief getCurrentAccount
0073      * @return
0074      */
0075     QVariantMap getCurrentAccount() const;
0076 
0077     /**
0078      * @brief getCount
0079      * @return
0080      */
0081     uint getCount() const;
0082 
0083     /**
0084      * @brief get
0085      * @param index
0086      * @return
0087      */
0088     QVariantMap get(const int &index) const;
0089 
0090 public Q_SLOTS:
0091     /**
0092      * @brief getCloudAccountsList
0093      * @return
0094      */
0095     QVariantList getCloudAccountsList();
0096 
0097     /**
0098      * @brief getCloudAccounts
0099      * @return
0100      */
0101     FMH::MODEL_LIST getCloudAccounts();
0102 
0103     /**
0104      * @brief registerAccount
0105      * @param account
0106      */
0107     void registerAccount(const QVariantMap &account);
0108 
0109     /**
0110      * @brief removeAccount
0111      * @param index
0112      */
0113     void removeAccount(const int &index);
0114 
0115     /**
0116      * @brief removeAccountAndFiles
0117      * @param index
0118      */
0119     void removeAccountAndFiles(const int &index);
0120 
0121     /**
0122      * @brief refresh
0123      */
0124     void refresh();
0125 
0126 private:
0127     MauiAccounts();
0128     ~MauiAccounts();
0129 
0130     static MauiAccounts *m_instance;
0131 
0132     AccountsDB *db;
0133     FMH::MODEL_LIST m_data;
0134     QVariantMap m_currentAccount;
0135 
0136     int m_currentAccountIndex = -1;
0137     void setAccounts();
0138 
0139     bool addCloudAccount(const QString &server, const QString &user, const QString &password);
0140     bool removeCloudAccount(const QString &server, const QString &user);
0141 
0142     QVariantList get(const QString &queryTxt);
0143 
0144 Q_SIGNALS:
0145     /**
0146      * @brief accountAdded
0147      * @param account
0148      */
0149     void accountAdded(QVariantMap account);
0150 
0151     /**
0152      * @brief accountRemoved
0153      * @param account
0154      */
0155     void accountRemoved(QVariantMap account);
0156 
0157     /**
0158      * @brief currentAccountChanged
0159      * @param account
0160      */
0161     void currentAccountChanged(QVariantMap account);
0162 
0163     /**
0164      * @brief currentAccountIndexChanged
0165      * @param index
0166      */
0167     void currentAccountIndexChanged(int index);
0168 };