Mailcommon

kmfilteraccountlist.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "kmfilteraccountlist.h"
8#include "mailfilter.h"
9#include "util/mailutil.h"
10
11#include <Akonadi/AgentInstance>
12#include <Akonadi/AgentType>
13
14#include <KLocalizedString>
15
16#include <QHeaderView>
17
18using namespace MailCommon;
19
20KMFilterAccountList::KMFilterAccountList(QWidget *parent)
21 : QTreeWidget(parent)
22{
23 setColumnCount(2);
24 const QStringList headerNames{i18n("Account Name"), i18n("Type")};
25 setHeaderItem(new QTreeWidgetItem(headerNames));
26 setAllColumnsShowFocus(true);
27 setFrameStyle(QFrame::WinPanel + QFrame::Sunken);
28 setSortingEnabled(false);
29 setRootIsDecorated(false);
30 setSortingEnabled(true);
31 sortByColumn(0, Qt::AscendingOrder);
32 header()->setSectionsMovable(false);
33}
34
35KMFilterAccountList::~KMFilterAccountList() = default;
36
37void KMFilterAccountList::updateAccountList(MailCommon::MailFilter *filter)
38{
39 clear();
40
41 QTreeWidgetItem *top = nullptr;
42 // Block the signals here, otherwise we end up calling
43 // slotApplicableAccountsChanged(), which will read the incomplete item
44 // state and write that back to the filter
45 blockSignals(true);
46 const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
47 const int nbAccount = lst.count();
48 for (int i = 0; i < nbAccount; ++i) {
49 const Akonadi::AgentInstance agent = lst.at(i);
50 auto listItem = new QTreeWidgetItem(this, top);
51 listItem->setText(0, agent.name());
52 listItem->setText(1, agent.type().name());
53 listItem->setText(2, agent.identifier());
54 if (filter) {
55 listItem->setCheckState(0, filter->applyOnAccount(agent.identifier()) ? Qt::Checked : Qt::Unchecked);
56 }
57 top = listItem;
58 }
59 blockSignals(false);
60
61 // make sure our hidden column is really hidden (Qt tends to re-show it)
62 hideColumn(2);
65
66 top = topLevelItem(0);
67 if (top) {
68 setCurrentItem(top);
69 }
70}
71
72void KMFilterAccountList::applyOnAccount(MailCommon::MailFilter *filter)
73{
75
76 while (QTreeWidgetItem *item = *it) {
77 const QString id = item->text(2);
78 filter->setApplyOnAccount(id, item->checkState(0) == Qt::Checked);
79 ++it;
80 }
81}
82
83void KMFilterAccountList::applyOnAccount(const QStringList &lstAccount)
84{
85 clear();
86
87 QTreeWidgetItem *top = nullptr;
88 // Block the signals here, otherwise we end up calling
89 // slotApplicableAccountsChanged(), which will read the incomplete item
90 // state and write that back to the filter
91 blockSignals(true);
92 const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
93 const int nbAccount = lst.count();
94 for (int i = 0; i < nbAccount; ++i) {
95 const Akonadi::AgentInstance agent = lst.at(i);
96 auto listItem = new QTreeWidgetItem(this, top);
97 listItem->setText(0, agent.name());
98 listItem->setText(1, agent.type().name());
99 listItem->setText(2, agent.identifier());
100 listItem->setCheckState(0, lstAccount.contains(agent.identifier()) ? Qt::Checked : Qt::Unchecked);
101 top = listItem;
102 }
103 blockSignals(false);
104
105 // make sure our hidden column is really hidden (Qt tends to re-show it)
106 hideColumn(2);
109
110 top = topLevelItem(0);
111 if (top) {
112 setCurrentItem(top);
113 }
114}
115
116QStringList KMFilterAccountList::selectedAccount()
117{
118 QStringList lstAccount;
120
121 while (QTreeWidgetItem *item = *it) {
122 if (item->checkState(0) == Qt::Checked) {
123 lstAccount << item->text(2);
124 }
125 ++it;
126 }
127 return lstAccount;
128}
129
130#include "moc_kmfilteraccountlist.cpp"
QString identifier() const
AgentType type() const
QString name() const
QString name() const
The MailFilter class.
Definition mailfilter.h:29
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
const_reference at(qsizetype i) const const
qsizetype count() const const
bool blockSignals(bool block)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
AscendingOrder
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
void hideColumn(int column)
void resizeColumnToContents(int column)
void clear()
void setCurrentItem(QTreeWidgetItem *item)
QTreeWidgetItem * topLevelItem(int index) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.