File indexing completed on 2024-11-10 04:50:08

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kmfilteraccountlist.h"
0008 #include "mailfilter.h"
0009 #include "util/mailutil.h"
0010 
0011 #include <Akonadi/AgentInstance>
0012 #include <Akonadi/AgentType>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QHeaderView>
0017 
0018 using namespace MailCommon;
0019 
0020 KMFilterAccountList::KMFilterAccountList(QWidget *parent)
0021     : QTreeWidget(parent)
0022 {
0023     setColumnCount(2);
0024     const QStringList headerNames{i18n("Account Name"), i18n("Type")};
0025     setHeaderItem(new QTreeWidgetItem(headerNames));
0026     setAllColumnsShowFocus(true);
0027     setFrameStyle(QFrame::WinPanel + QFrame::Sunken);
0028     setSortingEnabled(false);
0029     setRootIsDecorated(false);
0030     setSortingEnabled(true);
0031     sortByColumn(0, Qt::AscendingOrder);
0032     header()->setSectionsMovable(false);
0033 }
0034 
0035 KMFilterAccountList::~KMFilterAccountList() = default;
0036 
0037 void KMFilterAccountList::updateAccountList(MailCommon::MailFilter *filter)
0038 {
0039     clear();
0040 
0041     QTreeWidgetItem *top = nullptr;
0042     // Block the signals here, otherwise we end up calling
0043     // slotApplicableAccountsChanged(), which will read the incomplete item
0044     // state and write that back to the filter
0045     blockSignals(true);
0046     const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
0047     const int nbAccount = lst.count();
0048     for (int i = 0; i < nbAccount; ++i) {
0049         const Akonadi::AgentInstance agent = lst.at(i);
0050         auto listItem = new QTreeWidgetItem(this, top);
0051         listItem->setText(0, agent.name());
0052         listItem->setText(1, agent.type().name());
0053         listItem->setText(2, agent.identifier());
0054         if (filter) {
0055             listItem->setCheckState(0, filter->applyOnAccount(agent.identifier()) ? Qt::Checked : Qt::Unchecked);
0056         }
0057         top = listItem;
0058     }
0059     blockSignals(false);
0060 
0061     // make sure our hidden column is really hidden (Qt tends to re-show it)
0062     hideColumn(2);
0063     resizeColumnToContents(0);
0064     resizeColumnToContents(1);
0065 
0066     top = topLevelItem(0);
0067     if (top) {
0068         setCurrentItem(top);
0069     }
0070 }
0071 
0072 void KMFilterAccountList::applyOnAccount(MailCommon::MailFilter *filter)
0073 {
0074     QTreeWidgetItemIterator it(this);
0075 
0076     while (QTreeWidgetItem *item = *it) {
0077         const QString id = item->text(2);
0078         filter->setApplyOnAccount(id, item->checkState(0) == Qt::Checked);
0079         ++it;
0080     }
0081 }
0082 
0083 void KMFilterAccountList::applyOnAccount(const QStringList &lstAccount)
0084 {
0085     clear();
0086 
0087     QTreeWidgetItem *top = nullptr;
0088     // Block the signals here, otherwise we end up calling
0089     // slotApplicableAccountsChanged(), which will read the incomplete item
0090     // state and write that back to the filter
0091     blockSignals(true);
0092     const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
0093     const int nbAccount = lst.count();
0094     for (int i = 0; i < nbAccount; ++i) {
0095         const Akonadi::AgentInstance agent = lst.at(i);
0096         auto listItem = new QTreeWidgetItem(this, top);
0097         listItem->setText(0, agent.name());
0098         listItem->setText(1, agent.type().name());
0099         listItem->setText(2, agent.identifier());
0100         listItem->setCheckState(0, lstAccount.contains(agent.identifier()) ? Qt::Checked : Qt::Unchecked);
0101         top = listItem;
0102     }
0103     blockSignals(false);
0104 
0105     // make sure our hidden column is really hidden (Qt tends to re-show it)
0106     hideColumn(2);
0107     resizeColumnToContents(0);
0108     resizeColumnToContents(1);
0109 
0110     top = topLevelItem(0);
0111     if (top) {
0112         setCurrentItem(top);
0113     }
0114 }
0115 
0116 QStringList KMFilterAccountList::selectedAccount()
0117 {
0118     QStringList lstAccount;
0119     QTreeWidgetItemIterator it(this);
0120 
0121     while (QTreeWidgetItem *item = *it) {
0122         if (item->checkState(0) == Qt::Checked) {
0123             lstAccount << item->text(2);
0124         }
0125         ++it;
0126     }
0127     return lstAccount;
0128 }
0129 
0130 #include "moc_kmfilteraccountlist.cpp"