Mailcommon

filteractionaddtag.cpp
1 /*
2  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <[email protected]>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  */
7 
8 #include "filteractionaddtag.h"
9 #include "filter/dialog/filteractionmissingtagdialog.h"
10 #include "filter/filtermanager.h"
11 
12 #include <QComboBox>
13 
14 #include <Akonadi/Tag>
15 
16 using namespace MailCommon;
17 
18 FilterAction *FilterActionAddTag::newAction()
19 {
20  return new FilterActionAddTag;
21 }
22 
23 FilterActionAddTag::FilterActionAddTag(QObject *parent)
24  : FilterAction(QStringLiteral("add tag"), i18n("Add Tag"), parent)
25 {
26  mList = FilterManager::instance()->tagList();
27  connect(FilterManager::instance(), &FilterManager::tagListingFinished, this, &FilterActionAddTag::slotTagListingFinished);
28 }
29 
30 QWidget *FilterActionAddTag::createParamWidget(QWidget *parent) const
31 {
32  mComboBox = new QComboBox(parent);
33  mComboBox->setMinimumWidth(50);
34  mComboBox->setEditable(false);
36  while (i.hasNext()) {
37  i.next();
38  mComboBox->addItem(i.value(), i.key());
39  }
40 
41  setParamWidgetValue(mComboBox);
42  connect(mComboBox, &QComboBox::currentIndexChanged, this, &FilterActionAddTag::filterActionModified);
43 
44  return mComboBox;
45 }
46 
47 void FilterActionAddTag::applyParamWidgetValue(QWidget *paramWidget)
48 {
49  auto combo = static_cast<QComboBox *>(paramWidget);
50  mParameter = combo->itemData(combo->currentIndex()).toString();
51 }
52 
53 void FilterActionAddTag::setParamWidgetValue(QWidget *paramWidget) const
54 {
55  const int index = static_cast<QComboBox *>(paramWidget)->findData(mParameter);
56 
57  static_cast<QComboBox *>(paramWidget)->setCurrentIndex(index < 0 ? 0 : index);
58 }
59 
60 void FilterActionAddTag::clearParamWidget(QWidget *paramWidget) const
61 {
62  static_cast<QComboBox *>(paramWidget)->setCurrentIndex(0);
63 }
64 
65 bool FilterActionAddTag::isEmpty() const
66 {
67  return mParameter.isEmpty();
68 }
69 
70 void FilterActionAddTag::slotTagListingFinished()
71 {
72  if (mComboBox) {
73  mList = FilterManager::instance()->tagList();
74  mComboBox->clear();
75  fillComboBox();
76  }
77 }
78 
79 void FilterActionAddTag::fillComboBox()
80 {
82  while (i.hasNext()) {
83  i.next();
84  mComboBox->addItem(i.value(), i.key());
85  }
86 }
87 
88 bool FilterActionAddTag::argsFromStringInteractive(const QString &argsStr, const QString &filterName)
89 {
90  bool needUpdate = false;
91  argsFromString(argsStr);
92  if (mList.isEmpty()) {
93  return needUpdate;
94  }
95  const bool index = mList.contains(QUrl(mParameter));
96  if (!index) {
97  QPointer<MailCommon::FilterActionMissingTagDialog> dlg = new MailCommon::FilterActionMissingTagDialog(mList, filterName, argsStr);
98  if (dlg->exec()) {
99  mParameter = dlg->selectedTag();
100  needUpdate = true;
101  }
102  delete dlg;
103  }
104  return needUpdate;
105 }
106 
107 FilterAction::ReturnCode FilterActionAddTag::process(ItemContext &context, bool) const
108 {
109  if (!mList.contains(QUrl(mParameter))) {
110  return ErrorButGoOn;
111  }
112  context.item().setTag(Akonadi::Tag::fromUrl(QUrl(mParameter)));
113  context.setNeedsFlagStore();
114 
115  return GoOn;
116 }
117 
118 SearchRule::RequiredPart FilterActionAddTag::requiredPart() const
119 {
120  return SearchRule::Envelope;
121 }
122 
123 void FilterActionAddTag::argsFromString(const QString &argsStr)
124 {
125  if (mList.isEmpty()) {
126  mParameter = argsStr;
127  return;
128  }
129  if (mList.contains(QUrl(argsStr))) {
130  mParameter = argsStr;
131  return;
132  }
133  if (!mList.isEmpty()) {
134  mParameter = mList.cbegin().value();
135  }
136 }
137 
138 QString FilterActionAddTag::argsAsString() const
139 {
140  return mParameter;
141 }
142 
143 QString FilterActionAddTag::displayString() const
144 {
145  return label() + QLatin1String(" \"") + argsAsString().toHtmlEscaped() + QLatin1String("\"");
146 }
147 
148 QString FilterActionAddTag::informationAboutNotValidAction() const
149 {
150  const QString info = name() + QLatin1Char('\n') + i18n("No tag selected.");
151  return info;
152 }
void clear()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Abstract base class for mail filter actions.
Definition: filteraction.h:38
QString i18n(const char *text, const TYPE &arg...)
void setNeedsFlagStore()
Marks that the item's flags has been changed and needs to be written back.
Definition: itemcontext.cpp:43
RequiredPart
Possible required parts.
Definition: searchrule.h:68
static FilterManager * instance()
Returns the global filter manager object.
A helper class for the filtering process.
Definition: itemcontext.h:26
QString label(StandardShortcut id)
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
QVariant itemData(int index, int role) const const
QString name(StandardShortcut id)
QString::const_iterator cbegin() const const
void currentIndexChanged(int index)
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
char * toString(const EngineQuery &query)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Jun 6 2023 03:57:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.