Mailcommon

searchpatternedit.h
1/*
2 SPDX-FileCopyrightText: Marc Mutz <mutz@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "mailcommon_export.h"
10#include "searchpattern.h"
11
12#include <Libkdepim/KWidgetLister>
13
14#include <QByteArray>
15#include <QGroupBox>
16
17class KComboBox;
18class QPushButton;
19
20class QAbstractButton;
21class QRadioButton;
22class QStackedWidget;
23
24namespace MailCommon
25{
26class SearchPatternEdit;
27
28/**
29 * This widget is intended to be used in the filter configuration as
30 * well as in the message search dialogs. It consists of a frame,
31 * inside which there are placed two radio buttons entitled "Match
32 * {all,any} of the following", followed by a vertical stack of
33 * MailCommon::SearchRuleWidgets (initially two) and two buttons to add
34 * and remove, resp., additional KMSearchWidget 's.
35 *
36 * To set the widget according to a given KMSearchPattern, use
37 * setSearchPattern; to initialize it (e.g. for a new, virgin
38 * rule), use setSearchPattern with a 0 argument. The widget
39 * operates directly on a shallow(!) copy of the search rule. So
40 * while you actually don't really need searchPattern, because
41 * you can always store a pointer to the current pattern yourself,
42 * you must not modify the currently-worked-on pattern yourself while
43 * this widget holds a reference to it. The only exceptions are:
44 *
45 * @li If you edit a derived class, you can change aspects of the
46 * class that don't interfere with the KMSearchPattern part. An
47 * example is KMFilter, whose actions you can still edit while the
48 * KMSearchPattern part of it is being acted upon by this widget.
49 *
50 * @li You can change the name of the pattern, but only using (this
51 * widget's) setName. You cannot change the pattern's name
52 * directly, although this widget in itself doesn't let the user
53 * change it. This is because it auto-names the pattern to
54 * "<$field>:$contents" iff the pattern begins with "<".
55 *
56 * @short A widget which allows editing a set of MailCommon::SearchRule's.
57 * @author Marc Mutz <mutz@kde.org>
58 */
59class SearchRuleWidgetLister;
60class MAILCOMMON_EXPORT SearchPatternEdit : public QWidget
61{
62 Q_OBJECT
63
64public:
65 enum SearchPatternEditOption {
66 None = 0,
67 HeadersOnly = 1,
68 NotShowAbsoluteDate = 2,
69 MatchAllMessages = 4,
70 NotShowSize = 8,
71 NotShowDate = 16,
72 NotShowTags = 32
73 };
74 Q_DECLARE_FLAGS(SearchPatternEditOptions, SearchPatternEditOption)
75
76 enum SearchModeType {
77 StandardMode = 0,
78 BalooMode = 1,
79 };
80
81 /**
82 * Constructor. The parent parameter is passed to the underlying
83 * QGroupBox, as usual.
84 */
85 explicit SearchPatternEdit(QWidget *parent = nullptr,
86 SearchPatternEditOptions options = (SearchPatternEditOptions)(None),
87 SearchModeType modeType = StandardMode);
88
89 ~SearchPatternEdit() override;
90
91 void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
92
93 /**
94 * Sets the search pattern. Rules are inserted regardless of the
95 * return value of each rules' MailCommon::SearchRule::isEmpty.
96 * This widget makes a shallow copy of @p aPattern and operates
97 * directly on it.
98 */
99 void setSearchPattern(MailCommon::SearchPattern *aPattern);
100
101 /**
102 * Updates the search pattern according to the current widget values.
103 */
104 void updateSearchPattern();
105
106public Q_SLOTS:
107 /**
108 * Called when the widget should let go of the currently referenced
109 * filter and disable itself.
110 */
111 void reset();
112
113Q_SIGNALS:
114 /**
115 * This signal is emitted whenever the name of the processed
116 * search pattern may have changed.
117 */
118 void maybeNameChanged();
119
120 /**
121 * This signal is emitted wherenever the search pattern changes in some way.
122 */
123 void patternChanged();
124
125 void returnPressed();
126
127private:
128 MAILCOMMON_NO_EXPORT void slotRuleAdded(QWidget *widget);
129 MAILCOMMON_NO_EXPORT void slotAutoNameHack();
130 MAILCOMMON_NO_EXPORT void slotRadioClicked(QAbstractButton *aRBtn);
131 MAILCOMMON_NO_EXPORT void initLayout(SearchPatternEditOptions options, SearchModeType modeType);
132 MailCommon::SearchPattern *mPattern = nullptr;
133 QRadioButton *mAllRBtn = nullptr;
134 QRadioButton *mAnyRBtn = nullptr;
135 QRadioButton *mAllMessageRBtn = nullptr;
136 SearchRuleWidgetLister *mRuleLister = nullptr;
137};
138
139/**
140 * A widget to edit a single MailCommon::SearchRule.
141 * It consists of an editable KComboBox for the field,
142 * a read-only KComboBox for the function and
143 * a QLineEdit for the content or the pattern (in case of regexps).
144 * It manages the i18n itself, so field name should be in it's english form.
145 *
146 * To use, you essentially give it the reference to a MailCommon::SearchRule and
147 * it does the rest. It will never delete the rule itself, as it assumes
148 * that something outside of it manages this.
149 *
150 * @short A widget to edit a single MailCommon::SearchRule.
151 * @author Marc Mutz <mutz@kde.org>
152 */
154{
156
157public:
158 /**
159 * Constructor. You can give a MailCommon::SearchRule as parameter,
160 * which will be used to initialize the widget.
161 */
162 explicit SearchRuleWidget(QWidget *parent = nullptr,
165 SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
166
167 enum {
168 Message,
169 Body,
170 AnyHeader,
171 Recipients,
172 Size,
173 AgeInDays,
174 Status,
175 Tag,
176 Subject,
177 From,
178 To,
179 CC,
180 ReplyTo,
181 Organization,
182 Date,
183 Encryption,
184 Attachment
185 };
186
187 /**
188 * Sets the rule. The rule is accepted regardless of the return
189 * value of MailCommon::SearchRule::isEmpty. This widget makes a shallow
190 * copy of @p aRule and operates directly on it. If @p aRule is 0,
191 * resets itself, takes user input, but does essentially nothing.
192 * If you pass 0, you should probably disable it.
193 */
195
196 /**
197 * Returns a reference to the currently-worked-on MailCommon::SearchRule.
198 */
200
201 /**
202 * Resets the rule currently worked on and updates the widget accordingly.
203 */
204 void reset();
205
206 static int ruleFieldToId(const QString &i18nVal);
207
208 void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled);
209
210 void setPatternEditOptions(MailCommon::SearchPatternEdit::SearchPatternEditOptions options);
211
212public Q_SLOTS:
213 void slotFunctionChanged();
214 void slotValueChanged();
215 void slotReturnPressed();
216
218 /**
219 * This signal is emitted whenever the user alters the field.
220 * The pseudo-headers <...> are returned in their i18n form, but
221 * stored in their English form in the rule.
222 */
223 void fieldChanged(const QString &);
224
225 /**
226 * This signal is emitted whenever the user alters the contents/value
227 * of the rule.
228 */
230
231 void returnPressed();
232
233 void addWidget(QWidget *);
234 void removeWidget(QWidget *);
235
236protected:
237 /**
238 * Used internally to translate i18n-ized pseudo-headers back to English.
239 */
240 static QByteArray ruleFieldToEnglish(const QString &i18nVal);
241
242 /**
243 * Used internally to find the corresponding index into the field
244 * ComboBox. Returns the index if found or -1 if the search failed,
245 */
246 int indexOfRuleField(const QByteArray &aName) const;
247
248protected Q_SLOTS:
249 void slotRuleFieldChanged(const QString &);
250 void slotAddWidget();
251 void slotRemoveWidget();
252
253private:
254 void initWidget(SearchPatternEdit::SearchModeType modeType);
256
257 QStringList mFilterFieldList;
258 KComboBox *mRuleField = nullptr;
259 QStackedWidget *mFunctionStack = nullptr;
260 QStackedWidget *mValueStack = nullptr;
261 QPushButton *mAdd = nullptr;
262 QPushButton *mRemove = nullptr;
263};
264
265class SearchRuleWidgetLister : public KPIM::KWidgetLister
266{
268
269 friend class SearchPatternEdit;
270
271public:
272 explicit SearchRuleWidgetLister(QWidget *parent = nullptr,
274 SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
275
276 ~SearchRuleWidgetLister() override;
277
278 void setRuleList(QList<MailCommon::SearchRule::Ptr> *aList);
279
280 void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
281
282public Q_SLOTS:
283 void reset();
284 void slotAddWidget(QWidget *);
285 void slotRemoveWidget(QWidget *);
286
287protected:
288 void clearWidget(QWidget *aWidget) override;
289 QWidget *createWidget(QWidget *parent) override;
290
291private:
292 MAILCOMMON_NO_EXPORT void reconnectWidget(SearchRuleWidget *w);
293 MAILCOMMON_NO_EXPORT void updateAddRemoveButton();
294 MAILCOMMON_NO_EXPORT void regenerateRuleListFromWidgets();
295 QList<MailCommon::SearchRule::Ptr> *mRuleList = nullptr;
297 SearchPatternEdit::SearchModeType mTypeMode;
298};
299}
This class is an abstraction of a search over messages.
A widget to edit a single MailCommon::SearchRule.
void reset()
Resets the rule currently worked on and updates the widget accordingly.
SearchRuleWidget(QWidget *parent=nullptr, MailCommon::SearchRule::Ptr aRule=MailCommon::SearchRule::Ptr(), SearchPatternEdit::SearchPatternEditOptions options=(SearchPatternEdit::SearchPatternEditOptions)(SearchPatternEdit::None), SearchPatternEdit::SearchModeType modeType=SearchPatternEdit::StandardMode)
Constructor.
void contentsChanged(const QString &)
This signal is emitted whenever the user alters the contents/value of the rule.
MailCommon::SearchRule::Ptr rule() const
Returns a reference to the currently-worked-on MailCommon::SearchRule.
void fieldChanged(const QString &)
This signal is emitted whenever the user alters the field.
int indexOfRuleField(const QByteArray &aName) const
Used internally to find the corresponding index into the field ComboBox.
void setRule(MailCommon::SearchRule::Ptr aRule)
Sets the rule.
static QByteArray ruleFieldToEnglish(const QString &i18nVal)
Used internally to translate i18n-ized pseudo-headers back to English.
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
KGuiItem reset()
The filter dialog.
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.