9#include "core/filter.h"
10#include "config-messagelist.h"
11#include "core/messageitem.h"
12#include <MessageCore/StringUtil>
13#include <TextUtils/ConvertText>
16#if !FORCE_DISABLE_AKONADI_SEARCH
17#include <PIM/emailquery.h>
18#include <PIM/resultiterator.h>
25 generateRandomIdentifier();
28bool Filter::containString(
const QString &searchInString)
const
31 const QString searchInStringNormalize{TextUtils::ConvertText::normalize(searchInString)};
32 for (
const QString &str : std::as_const(mSearchList)) {
33 if (searchInStringNormalize.contains(TextUtils::ConvertText::normalize(str),
Qt::CaseInsensitive)) {
43const QString &Filter::iconName()
const
48void Filter::setIconName(
const QString &newIconName)
50 mIconName = newIconName;
55 mOptions = newOptions;
58const QString &Filter::filterName()
const
63void Filter::setFilterName(
const QString &newFilterName)
65 mFilterName = newFilterName;
68void Filter::setIdentifier(
const QString &newIdentifier)
70 mIdentifier = newIdentifier;
75 if (!mStatus.isEmpty()) {
83 if (!mSearchString.isEmpty()) {
84 if (mMatchingItemIds.contains(item->itemId())) {
88 bool searchMatches =
false;
89 bool searchEveryWhere = (mOptions & SearchMessageByButtons::SearchEveryWhere);
90 if (containString(item->
subject()) && ((mOptions & SearchMessageByButtons::SearchAgainstSubject) || searchEveryWhere)) {
92 }
else if (containString(item->
sender()) && ((mOptions & SearchMessageByButtons::SearchAgainstFrom) || searchEveryWhere)) {
94 }
else if (containString(item->
receiver()) && ((mOptions & SearchMessageByButtons::SearchAgainstTo) || searchEveryWhere)) {
102 if (!mTagId.isEmpty()) {
104 const bool tagMatches = item->
findTag(mTagId) !=
nullptr;
123bool Filter::isEmpty()
const
125 if (!mStatus.isEmpty()) {
129 if (!mSearchString.isEmpty()) {
133 if (!mTagId.isEmpty()) {
143 mSearchString.clear();
145 mMatchingItemIds.clear();
151 mCurrentFolder = folder;
156 const QString text = mSearchString;
158 if (mOptions & SearchMessageByButtons::SearchAgainstBody) {
159 SearchLineCommand::SearchLineInfo i;
160 i.type = SearchLineCommand::SearchLineType::Body;
161 i.argument = mSearchString;
163 infos.
append(std::move(i));
166 if (mOptions & SearchMessageByButtons::SearchAgainstSubject) {
167 SearchLineCommand::SearchLineInfo i;
168 i.type = SearchLineCommand::SearchLineType::Subject;
169 i.argument = mSearchString;
171 infos.
append(std::move(i));
174 if (mOptions & SearchMessageByButtons::SearchAgainstBcc) {
175 SearchLineCommand::SearchLineInfo i;
176 i.type = SearchLineCommand::SearchLineType::Bcc;
177 i.argument = mSearchString;
179 infos.
append(std::move(i));
182 if (mOptions & SearchMessageByButtons::SearchAgainstCc) {
183 SearchLineCommand::SearchLineInfo i;
184 i.type = SearchLineCommand::SearchLineType::Cc;
185 i.argument = mSearchString;
187 infos.
append(std::move(i));
192 if (
status.hasAttachment()) {
193 SearchLineCommand::SearchLineInfo i;
194 i.type = SearchLineCommand::SearchLineType::HasAttachment;
196 infos.
append(std::move(i));
198 }
else if (
status.hasInvitation()) {
199 SearchLineCommand::SearchLineInfo i;
200 i.type = SearchLineCommand::SearchLineType::HasInvitation;
202 infos.
append(std::move(i));
204 }
else if (
status.isImportant()) {
205 SearchLineCommand::SearchLineInfo i;
206 i.type = SearchLineCommand::SearchLineType::IsImportant;
208 infos.
append(std::move(i));
210 }
else if (
status.isEncrypted()) {
211 SearchLineCommand::SearchLineInfo i;
212 i.type = SearchLineCommand::SearchLineType::IsEncrypted;
214 infos.
append(std::move(i));
216 }
else if (
status.isReplied()) {
217 SearchLineCommand::SearchLineInfo i;
218 i.type = SearchLineCommand::SearchLineType::IsReplied;
220 infos.
append(std::move(i));
222 }
else if (
status.isForwarded()) {
223 SearchLineCommand::SearchLineInfo i;
224 i.type = SearchLineCommand::SearchLineType::IsForwarded;
226 infos.
append(std::move(i));
228 }
else if (
status.isRead()) {
229 SearchLineCommand::SearchLineInfo i;
230 i.type = SearchLineCommand::SearchLineType::IsRead;
232 infos.
append(std::move(i));
234 }
else if (
status.isIgnored()) {
235 SearchLineCommand::SearchLineInfo i;
236 i.type = SearchLineCommand::SearchLineType::IsIgnored;
238 infos.
append(std::move(i));
240 }
else if (
status.isSpam()) {
241 SearchLineCommand::SearchLineInfo i;
242 i.type = SearchLineCommand::SearchLineType::IsSpam;
244 infos.
append(std::move(i));
246 }
else if (
status.isHam()) {
247 SearchLineCommand::SearchLineInfo i;
248 i.type = SearchLineCommand::SearchLineType::IsHam;
250 infos.
append(std::move(i));
252 }
else if (
status.isQueued()) {
253 SearchLineCommand::SearchLineInfo i;
254 i.type = SearchLineCommand::SearchLineType::IsQueued;
256 infos.
append(std::move(i));
258 }
else if (
status.isSent()) {
259 SearchLineCommand::SearchLineInfo i;
260 i.type = SearchLineCommand::SearchLineType::IsSent;
262 infos.
append(std::move(i));
264 }
else if (
status.isDeleted()) {
265 SearchLineCommand::SearchLineInfo i;
266 i.type = SearchLineCommand::SearchLineType::IsDeleted;
268 infos.
append(std::move(i));
275 if (mContainsOutboundMessages) {
276 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstTo);
278 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstFrom);
281 SearchLineCommand command;
282 command.parseSearchLineCommand(text);
283 infos += command.searchLineInfo();
289 return mSearchString;
297void Filter::save(
const KSharedConfig::Ptr &config,
const QString &filtername,
const QString &iconName,
int numFilter)
300 int numberFilter = (numFilter == -1) ? grp.readEntry(
"NumberFilter").toInt() : numFilter;
301 KConfigGroup newGroup(config, QStringLiteral(
"Filter_%1").arg(numberFilter++));
302 newGroup.writeEntry(
"name", filtername);
304 newGroup.writeEntry(
"iconName", iconName);
306 newGroup.writeEntry(
"searchString", mSearchString);
307 newGroup.writeEntry(
"searchOptions",
static_cast<int>(mOptions));
308 newGroup.writeEntry(
"tagId", mTagId);
309 newGroup.writeEntry(
"identifier", mIdentifier);
312 for (
const auto s : std::as_const(mStatus)) {
315 newGroup.writeEntry(
"status", lst);
317 grp.writeEntry(
"NumberFilter", numberFilter);
319 config->reparseConfiguration();
322Filter *Filter::load(
const KSharedConfig::Ptr &config,
int filternumber)
325 int numberFilter = grp.readEntry(
"NumberFilter").toInt();
326 if (filternumber < numberFilter) {
327 KConfigGroup newGroup(config, QStringLiteral(
"Filter_%1").arg(filternumber));
328 return loadFromConfigGroup(newGroup);
345 for (
const auto s : std::as_const(lst)) {
348 messageStatusLst <<
status;
350 filter->setStatus(messageStatusLst);
355void Filter::setSearchString(
const SearchLineCommand &command)
357#if !FORCE_DISABLE_AKONADI_SEARCH
358 mMatchingItemIds.clear();
359 if (command.isEmpty()) {
365 for (
const auto &info : infos) {
367 case SearchLineCommand::Literal: {
370 bool needToSplitString =
false;
371 for (
const QString &text : searchListTmp) {
372 if (text.size() >= 3) {
379 needToSplitString =
true;
381 mSearchString = newStr;
382 query.matches(newStr);
383 query.setSplitSearchMatchString(needToSplitString);
386 case SearchLineCommand::Subject: {
387 mSearchString = info.argument;
388 query.subjectMatches(mSearchString);
391 case SearchLineCommand::Body: {
392 mSearchString = info.argument;
393 query.bodyMatches(mSearchString);
396 case SearchLineCommand::Unknown:
397 case SearchLineCommand::HasStateOrAttachment:
400 case SearchLineCommand::Larger:
401 case SearchLineCommand::Smaller:
402 case SearchLineCommand::OlderThan:
403 case SearchLineCommand::NewerThan:
404 case SearchLineCommand::Date:
405 case SearchLineCommand::Size:
406 case SearchLineCommand::Category:
409 case SearchLineCommand::HasAttachment: {
415 case SearchLineCommand::HasInvitation: {
421 case SearchLineCommand::IsImportant: {
427 case SearchLineCommand::IsRead: {
433 case SearchLineCommand::IsUnRead: {
440 case SearchLineCommand::IsIgnored: {
446 case SearchLineCommand::IsHam: {
452 case SearchLineCommand::IsSpam: {
458 case SearchLineCommand::IsWatched: {
464 case SearchLineCommand::IsReplied: {
470 case SearchLineCommand::IsEncrypted: {
476 case SearchLineCommand::IsQueued: {
482 case SearchLineCommand::IsDeleted: {
488 case SearchLineCommand::IsSent: {
494 case SearchLineCommand::IsForwarded: {
500 case SearchLineCommand::To: {
501 mSearchString = info.argument;
502 query.addTo(info.argument);
505 case SearchLineCommand::Bcc: {
506 mSearchString = info.argument;
507 query.addBcc(info.argument);
510 case SearchLineCommand::From: {
511 mSearchString = info.argument;
512 query.addFrom(info.argument);
515 case SearchLineCommand::Cc: {
516 mSearchString = info.argument;
517 query.addCc(info.argument);
523 setStatus(lstStatus);
525 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
526 query.addCollection(mCurrentFolder.id());
531 mMatchingItemIds << it.id();
539#if !FORCE_DISABLE_AKONADI_SEARCH
541 if ((mSearchString == trimStr) && (mOptions == options)) {
545 mSearchString = trimStr;
546 mMatchingItemIds.
clear();
548 if (mSearchString.isEmpty()) {
551 bool needToSplitString =
false;
552 QString newStr = mSearchString;
561 for (
const QString &text : searchListTmp) {
562 if (text.size() >= 3) {
570 needToSplitString =
true;
574 if (options & SearchMessageByButtons::SearchEveryWhere) {
575 query.matches(newStr);
576 query.setSplitSearchMatchString(needToSplitString);
577 }
else if (options & SearchMessageByButtons::SearchAgainstSubject) {
578 query.subjectMatches(newStr);
579 }
else if (options & SearchMessageByButtons::SearchAgainstBody) {
580 query.bodyMatches(newStr);
581 }
else if (options & SearchMessageByButtons::SearchAgainstFrom) {
582 query.setFrom(newStr);
583 }
else if (options & SearchMessageByButtons::SearchAgainstBcc) {
585 }
else if (options & SearchMessageByButtons::SearchAgainstCc) {
587 }
else if (options & SearchMessageByButtons::SearchAgainstTo) {
592 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
593 query.addCollection(mCurrentFolder.id());
598 mMatchingItemIds << it.id();
615void Filter::generateRandomIdentifier()
620QString Filter::identifier()
const
627 d <<
"filtername " << t.filterName();
628 d <<
"identifier " << t.identifier();
630 d <<
"search option " << t.currentOptions();
631 d <<
"status " << t.
status();
635#include "moc_filter.cpp"
void setQueued(bool queued=true)
void setHam(bool ham=true)
void fromQInt32(qint32 status)
void setEncrypted(bool value=true)
void setRead(bool read=true)
void setHasInvitation(bool hasInvitation=true)
void setForwarded(bool forwarded=true)
void setSpam(bool spam=true)
void setDeleted(bool deleted=true)
void setIgnored(bool ignored=true)
void setSent(bool sent=true)
void setReplied(bool replied=true)
void setHasAttachment(bool hasAttachment=true)
void setImportant(bool important=true)
void setWatched(bool watched=true)
QString readEntry(const char *key, const char *aDefault=nullptr) const
This class is responsible of matching messages that should be displayed in the View.
QList< Akonadi::MessageStatus > status() const
Returns the currently set status mask.
const QString & searchString() const
Returns the currently set search string.
const QString & receiver() const
Returns the receiver associated to this item.
const Akonadi::MessageStatus & status() const
Returns the status associated to this Item.
const QString & sender() const
Returns the sender associated to this item.
const QString & subject() const
Returns the subject associated to this Item.
const Tag * findTag(const QString &szTagId) const
Returns Tag associated to this message that has the specified id or 0 if no such tag exists.
Q_SCRIPTABLE CaptureState status()
std::optional< QSqlQuery > query(const QString &queryStatement)
KCOREADDONS_EXPORT QString randomString(int length)
The implementation independent part of the MessageList library.
void append(QList< T > &&value)
qsizetype count() const const
void reserve(qsizetype size)
bool isEmpty() const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
int toInt(bool *ok, int base) const const
QString trimmed() const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)