Mailcommon

filteractionaddtag.cpp
1/*
2 * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
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
16using namespace MailCommon;
17
18FilterAction *FilterActionAddTag::newAction()
19{
20 return new FilterActionAddTag;
21}
22
23FilterActionAddTag::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
30QWidget *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);
43
44 return mComboBox;
45}
46
47void FilterActionAddTag::applyParamWidgetValue(QWidget *paramWidget)
48{
49 auto combo = static_cast<QComboBox *>(paramWidget);
50 mParameter = combo->itemData(combo->currentIndex()).toString();
51}
52
53void 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
60void FilterActionAddTag::clearParamWidget(QWidget *paramWidget) const
61{
62 static_cast<QComboBox *>(paramWidget)->setCurrentIndex(0);
63}
64
65bool FilterActionAddTag::isEmpty() const
66{
67 return mParameter.isEmpty();
68}
69
70void FilterActionAddTag::slotTagListingFinished()
71{
72 if (mComboBox) {
73 mList = FilterManager::instance()->tagList();
74 mComboBox->clear();
75 fillComboBox();
76 }
77}
78
79void FilterActionAddTag::fillComboBox()
80{
82 while (i.hasNext()) {
83 i.next();
84 mComboBox->addItem(i.value(), i.key());
85 }
86}
87
88bool 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
107FilterAction::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
118SearchRule::RequiredPart FilterActionAddTag::requiredPart() const
119{
121}
122
123void 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
138QString FilterActionAddTag::argsAsString() const
139{
140 return mParameter;
141}
142
143QString FilterActionAddTag::displayString() const
144{
145 return label() + QLatin1StringView(" \"") + argsAsString().toHtmlEscaped() + QLatin1StringView("\"");
146}
147
148QString FilterActionAddTag::informationAboutNotValidAction() const
149{
150 const QString info = name() + QLatin1Char('\n') + i18n("No tag selected.");
151 return info;
152}
153
154#include "moc_filteractionaddtag.cpp"
Abstract base class for mail filter actions.
QString name() const
Returns identifier name, ie.
ReturnCode
Describes the possible return codes of filter processing:
@ ErrorButGoOn
A non-critical error occurred.
@ GoOn
Go on with applying filter actions.
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
QString label() const
Returns i18n'd label, ie.
static FilterManager * instance()
Returns the global filter manager object.
A helper class for the filtering process.
Definition itemcontext.h:27
Akonadi::Item & item()
Returns the item of the context.
void setNeedsFlagStore()
Marks that the item's flags has been changed and needs to be written back.
RequiredPart
Possible required parts.
Definition searchrule.h:68
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
void currentIndexChanged(int index)
const_iterator cbegin() const const
bool contains(const Key &key) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isEmpty() const const
QString toHtmlEscaped() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:02:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.