Mailcommon

kmfilteraccountlist.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2022 Laurent Montel <[email protected]>
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 
18 using namespace MailCommon;
19 
20 KMFilterAccountList::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 
35 KMFilterAccountList::~KMFilterAccountList() = default;
36 
37 void 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);
63  resizeColumnToContents(0);
64  resizeColumnToContents(1);
65 
66  top = topLevelItem(0);
67  if (top) {
68  setCurrentItem(top);
69  }
70 }
71 
72 void KMFilterAccountList::applyOnAccount(MailCommon::MailFilter *filter)
73 {
74  QTreeWidgetItemIterator it(this);
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 
83 void 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);
107  resizeColumnToContents(0);
108  resizeColumnToContents(1);
109 
110  top = topLevelItem(0);
111  if (top) {
112  setCurrentItem(top);
113  }
114 }
115 
116 QStringList KMFilterAccountList::selectedAccount()
117 {
118  QStringList lstAccount;
119  QTreeWidgetItemIterator it(this);
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 }
QString identifier() const
AgentType type() const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
AscendingOrder
The MailFilter class.
Definition: mailfilter.h:30
QString i18n(const char *text, const TYPE &arg...)
KGuiItem clear()
const T & at(int i) const const
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString name() const
int count(const T &value) const const
The filter dialog.
QString name() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.