File indexing completed on 2024-05-12 16:27:07

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "accountserverlistwidget.h"
0008 #include "createnewserver/createnewserverdialog.h"
0009 #include "model/rocketchataccountmodel.h"
0010 #include "rocketchataccount.h"
0011 #include "ruqola.h"
0012 #include "ruqolaglobalconfig.h"
0013 
0014 #include <QListWidgetItem>
0015 #include <QPointer>
0016 
0017 AccountServerListWidget::AccountServerListWidget(QWidget *parent)
0018     : QListWidget(parent)
0019 {
0020     setDragDropMode(QAbstractItemView::InternalMove);
0021     connect(this, &AccountServerListWidget::itemDoubleClicked, this, &AccountServerListWidget::modifyAccountConfig);
0022 }
0023 
0024 AccountServerListWidget::~AccountServerListWidget() = default;
0025 
0026 void AccountServerListWidget::load()
0027 {
0028     auto model = Ruqola::self()->accountManager()->rocketChatAccountProxyModel();
0029     const auto accountNumber = model->rowCount();
0030     for (int i = 0; i < accountNumber; ++i) {
0031         auto item = new AccountServerListWidgetItem(this);
0032         const auto index = model->index(i, 0);
0033         auto account = index.data(RocketChatAccountModel::Account).value<RocketChatAccount *>();
0034         AccountManager::AccountManagerInfo info;
0035         info.displayName = account->displayName();
0036         info.accountName = account->accountName();
0037         info.serverUrl = account->serverUrl();
0038         info.userName = account->userName();
0039         info.password = account->password();
0040         info.canResetPassword = account->allowPasswordReset() && account->allowPasswordChange();
0041         info.authenticationInfos = account->authenticationMethodInfos();
0042         item->setAccountInfo(info);
0043         item->setNewAccount(false);
0044         item->setCheckState(account->accountEnabled() ? Qt::Checked : Qt::Unchecked);
0045     }
0046 }
0047 
0048 void AccountServerListWidget::save()
0049 {
0050     // First remove account
0051     auto accountManager = Ruqola::self()->accountManager();
0052     QMapIterator<QString, bool> i(mListRemovedAccount);
0053     while (i.hasNext()) {
0054         i.next();
0055         accountManager->removeAccount(i.key(), i.value());
0056     }
0057 
0058     QStringList order;
0059     const int numberOfItems(count());
0060     order.reserve(numberOfItems);
0061     // Add account or modify it
0062     for (int i = 0; i < numberOfItems; ++i) {
0063         QListWidgetItem *it = item(i);
0064         auto serverListItem = static_cast<AccountServerListWidgetItem *>(it);
0065         AccountManager::AccountManagerInfo info = serverListItem->accountInfo();
0066 
0067         info.enabled = serverListItem->checkState() == Qt::Checked;
0068         if (serverListItem->newAccount()) {
0069             accountManager->addAccount(info);
0070         } else {
0071             accountManager->modifyAccount(info);
0072         }
0073         order << info.accountName;
0074     }
0075     Ruqola::self()->accountManager()->rocketChatAccountProxyModel()->setAccountOrder(order);
0076     RuqolaGlobalConfig::self()->setAccountOrder(order);
0077 }
0078 
0079 void AccountServerListWidget::modifyAccountConfig()
0080 {
0081     QListWidgetItem *item = currentItem();
0082     if (!item) {
0083         return;
0084     }
0085 
0086     auto serverListItem = static_cast<AccountServerListWidgetItem *>(item);
0087     QPointer<CreateNewServerDialog> dlg = new CreateNewServerDialog(this);
0088     dlg->setAccountInfo(serverListItem->accountInfo());
0089     if (dlg->exec()) {
0090         const AccountManager::AccountManagerInfo info = dlg->accountInfo();
0091         serverListItem->setAccountInfo(std::move(info));
0092     }
0093     delete dlg;
0094 }
0095 
0096 void AccountServerListWidget::deleteAccountConfig(QListWidgetItem *item, bool removeLogs)
0097 {
0098     mListRemovedAccount.insert(item->text(), removeLogs);
0099 }
0100 
0101 void AccountServerListWidget::addAccountConfig()
0102 {
0103     QPointer<CreateNewServerDialog> dlg = new CreateNewServerDialog(this);
0104     QStringList listAccounts;
0105     for (int i = 0; i < count(); ++i) {
0106         QListWidgetItem *it = item(i);
0107         listAccounts << it->text();
0108     }
0109     dlg->setExistingAccountName(listAccounts);
0110     if (dlg->exec()) {
0111         AccountManager::AccountManagerInfo info = dlg->accountInfo();
0112         QStringList accountList;
0113         accountList.reserve(count());
0114         for (int i = 0; i < count(); ++i) {
0115             QListWidgetItem *it = item(i);
0116             accountList << it->text();
0117         }
0118         QString newAccountName = info.accountName;
0119         int i = 1;
0120         while (accountList.contains(newAccountName)) {
0121             newAccountName = QStringLiteral("%1_%2").arg(newAccountName).arg(i);
0122         }
0123         info.accountName = newAccountName;
0124         auto accountServeritem = new AccountServerListWidgetItem(this);
0125         accountServeritem->setCheckState(Qt::Checked);
0126         accountServeritem->setAccountInfo(info);
0127         accountServeritem->setNewAccount(true);
0128     }
0129     delete dlg;
0130 }
0131 
0132 void AccountServerListWidget::slotMoveAccountUp()
0133 {
0134     if (!currentItem()) {
0135         return;
0136     }
0137     const int pos = row(currentItem());
0138     blockSignals(true);
0139     QListWidgetItem *item = takeItem(pos);
0140     // now selected item is at idx(idx-1), so
0141     // insert the other item at idx, ie. above(below).
0142     insertItem(pos - 1, item);
0143     blockSignals(false);
0144     setCurrentRow(pos - 1);
0145 }
0146 
0147 void AccountServerListWidget::slotMoveAccountDown()
0148 {
0149     if (!currentItem()) {
0150         return;
0151     }
0152     const int pos = row(currentItem());
0153     blockSignals(true);
0154     QListWidgetItem *item = takeItem(pos);
0155     // now selected item is at idx(idx-1), so
0156     // insert the other item at idx, ie. above(below).
0157     insertItem(pos + 1, item);
0158     blockSignals(false);
0159     setCurrentRow(pos + 1);
0160 }
0161 
0162 AccountServerListWidgetItem::AccountServerListWidgetItem(QListWidget *parent)
0163     : QListWidgetItem(parent)
0164 {
0165 }
0166 
0167 AccountServerListWidgetItem::~AccountServerListWidgetItem() = default;
0168 
0169 AccountManager::AccountManagerInfo AccountServerListWidgetItem::accountInfo() const
0170 {
0171     return mInfo;
0172 }
0173 
0174 void AccountServerListWidgetItem::setAccountInfo(const AccountManager::AccountManagerInfo &info)
0175 {
0176     mInfo = info;
0177     setText(info.displayName);
0178 }
0179 
0180 bool AccountServerListWidgetItem::newAccount() const
0181 {
0182     return mNewAccount;
0183 }
0184 
0185 void AccountServerListWidgetItem::setNewAccount(bool newAccount)
0186 {
0187     mNewAccount = newAccount;
0188 }
0189 
0190 #include "moc_accountserverlistwidget.cpp"