Mailcommon

searchpatternedit.h
1 /*
2  SPDX-FileCopyrightText: Marc Mutz <[email protected]>
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 
17 class KComboBox;
18 class QPushButton;
19 
20 class QAbstractButton;
21 class QRadioButton;
22 class QStackedWidget;
23 
24 namespace MailCommon
25 {
26 class 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 <[email protected]>
58  */
59 class SearchRuleWidgetLister;
60 class MAILCOMMON_EXPORT SearchPatternEdit : public QWidget
61 {
62  Q_OBJECT
63 
64 public:
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 { StandardMode = 0, BalooMode = 1 };
77 
78  /**
79  * Constructor. The parent parameter is passed to the underlying
80  * QGroupBox, as usual.
81  */
82  explicit SearchPatternEdit(QWidget *parent = nullptr,
83  SearchPatternEditOptions options = (SearchPatternEditOptions)(None),
84  SearchModeType modeType = StandardMode);
85 
86  ~SearchPatternEdit() override;
87 
88  void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
89 
90  /**
91  * Sets the search pattern. Rules are inserted regardless of the
92  * return value of each rules' MailCommon::SearchRule::isEmpty.
93  * This widget makes a shallow copy of @p aPattern and operates
94  * directly on it.
95  */
96  void setSearchPattern(MailCommon::SearchPattern *aPattern);
97 
98  /**
99  * Updates the search pattern according to the current widget values.
100  */
101  void updateSearchPattern();
102 
103 public Q_SLOTS:
104  /**
105  * Called when the widget should let go of the currently referenced
106  * filter and disable itself.
107  */
108  void reset();
109 
110 Q_SIGNALS:
111  /**
112  * This signal is emitted whenever the name of the processed
113  * search pattern may have changed.
114  */
115  void maybeNameChanged();
116 
117  /**
118  * This signal is emitted wherenever the search pattern changes in some way.
119  */
120  void patternChanged();
121 
122  void returnPressed();
123 
124 private Q_SLOTS:
125  void slotRadioClicked(QAbstractButton *aRBtn);
126  void slotAutoNameHack();
127  void slotRuleAdded(QWidget *widget);
128 
129 private:
130  void initLayout(SearchPatternEditOptions options, SearchModeType modeType);
131  MailCommon::SearchPattern *mPattern = nullptr;
132  QRadioButton *mAllRBtn = nullptr;
133  QRadioButton *mAnyRBtn = nullptr;
134  QRadioButton *mAllMessageRBtn = nullptr;
135  SearchRuleWidgetLister *mRuleLister = nullptr;
136 };
137 
138 /**
139  * A widget to edit a single MailCommon::SearchRule.
140  * It consists of an editable KComboBox for the field,
141  * a read-only KComboBox for the function and
142  * a QLineEdit for the content or the pattern (in case of regexps).
143  * It manages the i18n itself, so field name should be in it's english form.
144  *
145  * To use, you essentially give it the reference to a MailCommon::SearchRule and
146  * it does the rest. It will never delete the rule itself, as it assumes
147  * that something outside of it manages this.
148  *
149  * @short A widget to edit a single MailCommon::SearchRule.
150  * @author Marc Mutz <[email protected]>
151  */
152 class SearchRuleWidget : public QWidget
153 {
154  Q_OBJECT
155 
156 public:
157  /**
158  * Constructor. You can give a MailCommon::SearchRule as parameter,
159  * which will be used to initialize the widget.
160  */
161  explicit SearchRuleWidget(QWidget *parent = nullptr,
164  SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
165 
166  enum { Message, Body, AnyHeader, Recipients, Size, AgeInDays, Status, Tag, Subject, From, To, CC, ReplyTo, Organization, Date, Encryption };
167 
168  /**
169  * Sets the rule. The rule is accepted regardless of the return
170  * value of MailCommon::SearchRule::isEmpty. This widget makes a shallow
171  * copy of @p aRule and operates directly on it. If @p aRule is 0,
172  * resets itself, takes user input, but does essentially nothing.
173  * If you pass 0, you should probably disable it.
174  */
176 
177  /**
178  * Returns a reference to the currently-worked-on MailCommon::SearchRule.
179  */
181 
182  /**
183  * Resets the rule currently worked on and updates the widget accordingly.
184  */
185  void reset();
186 
187  static int ruleFieldToId(const QString &i18nVal);
188 
189  void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled);
190 
191  void setPatternEditOptions(MailCommon::SearchPatternEdit::SearchPatternEditOptions options);
192 
193 public Q_SLOTS:
194  void slotFunctionChanged();
195  void slotValueChanged();
196  void slotReturnPressed();
197 
198 Q_SIGNALS:
199  /**
200  * This signal is emitted whenever the user alters the field.
201  * The pseudo-headers <...> are returned in their i18n form, but
202  * stored in their English form in the rule.
203  */
204  void fieldChanged(const QString &);
205 
206  /**
207  * This signal is emitted whenever the user alters the contents/value
208  * of the rule.
209  */
210  void contentsChanged(const QString &);
211 
212  void returnPressed();
213 
214  void addWidget(QWidget *);
215  void removeWidget(QWidget *);
216 
217 protected:
218  /**
219  * Used internally to translate i18n-ized pseudo-headers back to English.
220  */
221  static QByteArray ruleFieldToEnglish(const QString &i18nVal);
222 
223  /**
224  * Used internally to find the corresponding index into the field
225  * ComboBox. Returns the index if found or -1 if the search failed,
226  */
227  int indexOfRuleField(const QByteArray &aName) const;
228 
229 protected Q_SLOTS:
230  void slotRuleFieldChanged(const QString &);
231  void slotAddWidget();
232  void slotRemoveWidget();
233 
234 private:
235  void initWidget(SearchPatternEdit::SearchModeType modeType);
237 
238  QStringList mFilterFieldList;
239  KComboBox *mRuleField = nullptr;
240  QStackedWidget *mFunctionStack = nullptr;
241  QStackedWidget *mValueStack = nullptr;
242  QPushButton *mAdd = nullptr;
243  QPushButton *mRemove = nullptr;
244 };
245 
246 class SearchRuleWidgetLister : public KPIM::KWidgetLister
247 {
248  Q_OBJECT
249 
250  friend class SearchPatternEdit;
251 
252 public:
253  explicit SearchRuleWidgetLister(QWidget *parent = nullptr,
255  SearchPatternEdit::SearchModeType modeType = SearchPatternEdit::StandardMode);
256 
257  ~SearchRuleWidgetLister() override;
258 
259  void setRuleList(QList<MailCommon::SearchRule::Ptr> *aList);
260 
261  void setPatternEditOptions(SearchPatternEdit::SearchPatternEditOptions options);
262 
263 public Q_SLOTS:
264  void reset();
265  void slotAddWidget(QWidget *);
266  void slotRemoveWidget(QWidget *);
267 
268 protected:
269  void clearWidget(QWidget *aWidget) override;
270  QWidget *createWidget(QWidget *parent) override;
271 
272 private:
273  void reconnectWidget(SearchRuleWidget *w);
274  void updateAddRemoveButton();
275  void regenerateRuleListFromWidgets();
276  QList<MailCommon::SearchRule::Ptr> *mRuleList = nullptr;
278  SearchPatternEdit::SearchModeType mTypeMode;
279 };
280 }
Q_OBJECTQ_OBJECT
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
Q_SLOTSQ_SLOTS
static QByteArray ruleFieldToEnglish(const QString &i18nVal)
Used internally to translate i18n-ized pseudo-headers back to English.
void contentsChanged(const QString &)
This signal is emitted whenever the user alters the contents/value of the rule.
Q_SIGNALSQ_SIGNALS
void reset()
Resets the rule currently worked on and updates the widget accordingly.
This class is an abstraction of a search over messages.
Definition: searchpattern.h:58
void setRule(MailCommon::SearchRule::Ptr aRule)
Sets the rule.
void fieldChanged(const QString &)
This signal is emitted whenever the user alters the field.
SearchRuleWidget(QWidget *parent=nullptr, MailCommon::SearchRule::Ptr aRule=MailCommon::SearchRule::Ptr(), SearchPatternEdit::SearchPatternEditOptions options=(SearchPatternEdit::SearchPatternEditOptions)(SearchPatternEdit::None), SearchPatternEdit::SearchModeType modeType=SearchPatternEdit::StandardMode)
Constructor.
KGuiItem reset()
MailCommon::SearchRule::Ptr rule() const
Returns a reference to the currently-worked-on MailCommon::SearchRule.
int indexOfRuleField(const QByteArray &aName) const
Used internally to find the corresponding index into the field ComboBox.
A widget to edit a single MailCommon::SearchRule.
QObject * parent() const const
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.