File indexing completed on 2024-05-19 05:02:45

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "rocketchataccountmodel.h"
0008 #include "rocketchataccount.h"
0009 #include "ruqola_debug.h"
0010 #include "ruqolaserverconfig.h"
0011 
0012 RocketChatAccountModel::RocketChatAccountModel(QObject *parent)
0013     : QAbstractListModel(parent)
0014 {
0015 }
0016 
0017 RocketChatAccountModel::~RocketChatAccountModel()
0018 {
0019     qDeleteAll(mRocketChatAccount);
0020 }
0021 
0022 void RocketChatAccountModel::clear()
0023 {
0024     setAccounts({});
0025 }
0026 
0027 void RocketChatAccountModel::setAccounts(const QVector<RocketChatAccount *> &accounts)
0028 {
0029     if (accounts == mRocketChatAccount) {
0030         return;
0031     }
0032 
0033     beginResetModel();
0034     qDeleteAll(mRocketChatAccount);
0035     mRocketChatAccount = accounts;
0036     endResetModel();
0037 
0038     Q_EMIT accountNumberChanged();
0039 }
0040 
0041 RocketChatAccount *RocketChatAccountModel::accountFromServerUrl(const QString &serverUrl) const
0042 {
0043     if (mRocketChatAccount.isEmpty()) {
0044         qCWarning(RUQOLA_LOG) << " Empty account";
0045         return nullptr;
0046     }
0047     for (int i = 0, total = mRocketChatAccount.count(); i < total; ++i) {
0048         RocketChatAccount *model = mRocketChatAccount.at(i);
0049         if (model->serverUrl().contains(serverUrl)) {
0050             return model;
0051         }
0052     }
0053     return nullptr;
0054 }
0055 
0056 RocketChatAccount *RocketChatAccountModel::account(const QString &accountName) const
0057 {
0058     if (mRocketChatAccount.isEmpty()) {
0059         qCWarning(RUQOLA_LOG) << " Empty account";
0060         return nullptr;
0061     }
0062     for (int i = 0, total = mRocketChatAccount.count(); i < total; ++i) {
0063         RocketChatAccount *model = mRocketChatAccount.at(i);
0064         if (model->accountName() == accountName) {
0065             return model;
0066         }
0067     }
0068     return nullptr;
0069 }
0070 
0071 int RocketChatAccountModel::accountNumber() const
0072 {
0073     return mRocketChatAccount.count();
0074 }
0075 
0076 QStringList RocketChatAccountModel::accountsName() const
0077 {
0078     QStringList accounts;
0079     for (int i = 0, total = mRocketChatAccount.count(); i < total; ++i) {
0080         RocketChatAccount *model = mRocketChatAccount.at(i);
0081         accounts << model->accountName();
0082     }
0083     return accounts;
0084 }
0085 
0086 RocketChatAccount *RocketChatAccountModel::account(int index) const
0087 {
0088     if (mRocketChatAccount.isEmpty() || (index > mRocketChatAccount.count() - 1)) {
0089         qCWarning(RUQOLA_LOG) << " Empty account";
0090         return nullptr;
0091     }
0092     return mRocketChatAccount.at(index);
0093 }
0094 
0095 int RocketChatAccountModel::rowCount(const QModelIndex &parent) const
0096 {
0097     Q_UNUSED(parent)
0098     return mRocketChatAccount.count();
0099 }
0100 
0101 QVariant RocketChatAccountModel::data(const QModelIndex &index, int role) const
0102 {
0103     if (!index.isValid()) {
0104         qCWarning(RUQOLA_LOG) << "ERROR: invalid index";
0105         return {};
0106     }
0107     const int idx = index.row();
0108     RocketChatAccount *account = mRocketChatAccount.at(idx);
0109     switch (role) {
0110     case Qt::DisplayRole:
0111     case Name:
0112         return account->displayName();
0113     case SiteUrl:
0114         return account->ruqolaServerConfig()->siteUrl();
0115     case UserName:
0116         return account->userName();
0117     case Account:
0118         return QVariant::fromValue(account);
0119     case AccountName:
0120         return account->accountName();
0121     }
0122     // Add icon ???
0123     return {};
0124 }
0125 
0126 void RocketChatAccountModel::insertAccount(RocketChatAccount *account)
0127 {
0128     // Verify that we have it ?
0129     const int accountCount = mRocketChatAccount.count();
0130     beginInsertRows(QModelIndex(), accountCount, accountCount);
0131     mRocketChatAccount.append(account);
0132     endInsertRows();
0133     Q_EMIT accountNumberChanged();
0134 }
0135 
0136 RocketChatAccount *RocketChatAccountModel::removeAccount(const QString &name)
0137 {
0138     // qDebug() << " void RocketChatAccountModel::removeAccount(const QString &name)"<<name;
0139     // Search account.
0140     RocketChatAccount *account = nullptr;
0141     for (int i = 0, total = mRocketChatAccount.count(); i < total; ++i) {
0142         if (mRocketChatAccount.at(i)->accountName() == name) {
0143             beginRemoveRows(QModelIndex(), i, i);
0144             account = mRocketChatAccount.takeAt(i);
0145             account->removeSettings();
0146             endRemoveRows();
0147             Q_EMIT accountNumberChanged();
0148             break;
0149         }
0150     }
0151     return account;
0152 }
0153 
0154 #include "moc_rocketchataccountmodel.cpp"