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));
269 if (mContainsOutboundMessages) {
270 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstTo);
272 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstFrom);
275 SearchLineCommand command;
276 command.parseSearchLineCommand(text);
277 infos += command.searchLineInfo();
283 return mSearchString;
291void Filter::save(
const KSharedConfig::Ptr &config,
const QString &filtername,
const QString &iconName,
int numFilter)
294 int numberFilter = (numFilter == -1) ? grp.readEntry(
"NumberFilter").toInt() : numFilter;
295 KConfigGroup newGroup(config, QStringLiteral(
"Filter_%1").arg(numberFilter++));
296 newGroup.writeEntry(
"name", filtername);
298 newGroup.writeEntry(
"iconName", iconName);
300 newGroup.writeEntry(
"searchString", mSearchString);
301 newGroup.writeEntry(
"searchOptions",
static_cast<int>(mOptions));
302 newGroup.writeEntry(
"tagId", mTagId);
303 newGroup.writeEntry(
"identifier", mIdentifier);
306 for (
const auto s : std::as_const(mStatus)) {
309 newGroup.writeEntry(
"status", lst);
311 grp.writeEntry(
"NumberFilter", numberFilter);
313 config->reparseConfiguration();
316Filter *Filter::load(
const KSharedConfig::Ptr &config,
int filternumber)
319 int numberFilter = grp.readEntry(
"NumberFilter").toInt();
320 if (filternumber < numberFilter) {
321 KConfigGroup newGroup(config, QStringLiteral(
"Filter_%1").arg(filternumber));
322 return loadFromConfigGroup(newGroup);
339 for (
const auto s : std::as_const(lst)) {
342 messageStatusLst <<
status;
344 filter->setStatus(messageStatusLst);
349void Filter::setSearchString(
const SearchLineCommand &command)
351#if !FORCE_DISABLE_AKONADI_SEARCH
352 mMatchingItemIds.clear();
353 if (command.isEmpty()) {
359 for (
const auto &info : infos) {
361 case SearchLineCommand::Literal: {
364 bool needToSplitString =
false;
365 for (
const QString &text : searchListTmp) {
366 if (text.size() >= 3) {
373 needToSplitString =
true;
375 mSearchString = newStr;
376 query.matches(newStr);
377 query.setSplitSearchMatchString(needToSplitString);
380 case SearchLineCommand::Subject: {
381 mSearchString = info.argument;
382 query.subjectMatches(mSearchString);
385 case SearchLineCommand::Body: {
386 mSearchString = info.argument;
387 query.bodyMatches(mSearchString);
390 case SearchLineCommand::Unknown:
391 case SearchLineCommand::HasStateOrAttachment:
394 case SearchLineCommand::Larger:
395 case SearchLineCommand::Smaller:
396 case SearchLineCommand::OlderThan:
397 case SearchLineCommand::NewerThan:
398 case SearchLineCommand::Date:
399 case SearchLineCommand::Size:
400 case SearchLineCommand::Category:
403 case SearchLineCommand::HasAttachment: {
409 case SearchLineCommand::HasInvitation: {
415 case SearchLineCommand::IsImportant: {
421 case SearchLineCommand::IsRead: {
427 case SearchLineCommand::IsUnRead: {
434 case SearchLineCommand::IsIgnored: {
440 case SearchLineCommand::IsHam: {
446 case SearchLineCommand::IsSpam: {
452 case SearchLineCommand::IsWatched: {
458 case SearchLineCommand::IsReplied: {
464 case SearchLineCommand::IsEncrypted: {
470 case SearchLineCommand::IsQueued: {
476 case SearchLineCommand::IsSent: {
482 case SearchLineCommand::IsForwarded: {
488 case SearchLineCommand::To:
489 mSearchString = info.argument;
490 query.addTo(info.argument);
492 case SearchLineCommand::Bcc:
493 mSearchString = info.argument;
494 query.addBcc(info.argument);
496 case SearchLineCommand::From:
497 mSearchString = info.argument;
498 query.addFrom(info.argument);
500 case SearchLineCommand::Cc:
501 mSearchString = info.argument;
502 query.addCc(info.argument);
507 setStatus(lstStatus);
509 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
510 query.addCollection(mCurrentFolder.id());
515 mMatchingItemIds << it.id();
523#if !FORCE_DISABLE_AKONADI_SEARCH
525 if ((mSearchString == trimStr) && (mOptions == options)) {
529 mSearchString = trimStr;
530 mMatchingItemIds.
clear();
532 if (mSearchString.isEmpty()) {
535 bool needToSplitString =
false;
536 QString newStr = mSearchString;
545 for (
const QString &text : searchListTmp) {
546 if (text.size() >= 3) {
554 needToSplitString =
true;
558 if (options & SearchMessageByButtons::SearchEveryWhere) {
559 query.matches(newStr);
560 query.setSplitSearchMatchString(needToSplitString);
561 }
else if (options & SearchMessageByButtons::SearchAgainstSubject) {
562 query.subjectMatches(newStr);
563 }
else if (options & SearchMessageByButtons::SearchAgainstBody) {
564 query.bodyMatches(newStr);
565 }
else if (options & SearchMessageByButtons::SearchAgainstFrom) {
566 query.setFrom(newStr);
567 }
else if (options & SearchMessageByButtons::SearchAgainstBcc) {
569 }
else if (options & SearchMessageByButtons::SearchAgainstCc) {
571 }
else if (options & SearchMessageByButtons::SearchAgainstTo) {
576 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
577 query.addCollection(mCurrentFolder.id());
582 mMatchingItemIds << it.id();
599void Filter::generateRandomIdentifier()
604QString Filter::identifier()
const
611 d <<
"filtername " << t.filterName();
612 d <<
"identifier " << t.identifier();
614 d <<
"search option " << t.currentOptions();
615 d <<
"status " << t.
status();
619#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 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)