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 return mSearchString;
164void Filter::save(
const KSharedConfig::Ptr &config,
const QString &filtername,
const QString &iconName,
int numFilter)
167 int numberFilter = (numFilter == -1) ? grp.readEntry(
"NumberFilter").toInt() : numFilter;
168 KConfigGroup newGroup(config, QStringLiteral(
"Filter_%1").arg(numberFilter++));
169 newGroup.writeEntry(
"name", filtername);
171 newGroup.writeEntry(
"iconName", iconName);
173 newGroup.writeEntry(
"searchString", mSearchString);
174 newGroup.writeEntry(
"searchOptions",
static_cast<int>(mOptions));
175 newGroup.writeEntry(
"tagId", mTagId);
176 newGroup.writeEntry(
"identifier", mIdentifier);
179 for (
const auto s : std::as_const(mStatus)) {
182 newGroup.writeEntry(
"status", lst);
184 grp.writeEntry(
"NumberFilter", numberFilter);
186 config->reparseConfiguration();
189Filter *Filter::load(
const KSharedConfig::Ptr &config,
int filternumber)
192 int numberFilter = grp.readEntry(
"NumberFilter").toInt();
193 if (filternumber < numberFilter) {
194 KConfigGroup newGroup(config, QStringLiteral(
"Filter_%1").arg(filternumber));
195 return loadFromConfigGroup(newGroup);
212 for (
const auto s : std::as_const(lst)) {
215 messageStatusLst <<
status;
217 filter->setStatus(messageStatusLst);
222void Filter::setSearchString(
const SearchLineCommand &command)
224#if !FORCE_DISABLE_AKONADI_SEARCH
225 mMatchingItemIds.clear();
226 if (command.isEmpty()) {
232 for (
const auto &info : infos) {
234 case SearchLineCommand::Literal: {
235 QString newStr = info.argument;
237 bool needToSplitString =
false;
238 for (
const QString &text : searchListTmp) {
239 if (text.size() >= 3) {
247 needToSplitString =
true;
249 mSearchString = newStr;
250 query.matches(newStr);
251 query.setSplitSearchMatchString(needToSplitString);
254 case SearchLineCommand::Subject: {
255 mSearchString = info.argument;
256 query.subjectMatches(mSearchString);
259 case SearchLineCommand::Body: {
260 mSearchString = info.argument;
261 query.bodyMatches(mSearchString);
264 case SearchLineCommand::Unknown:
265 case SearchLineCommand::HasStateOrAttachment:
268 case SearchLineCommand::Larger:
269 case SearchLineCommand::Smaller:
270 case SearchLineCommand::OlderThan:
271 case SearchLineCommand::NewerThan:
272 case SearchLineCommand::Date:
273 case SearchLineCommand::Size:
274 case SearchLineCommand::Category:
277 case SearchLineCommand::HasAttachment: {
283 case SearchLineCommand::HasInvitation: {
289 case SearchLineCommand::IsImportant: {
295 case SearchLineCommand::IsRead: {
301 case SearchLineCommand::IsUnRead: {
308 case SearchLineCommand::IsIgnored: {
314 case SearchLineCommand::IsHam: {
320 case SearchLineCommand::IsSpam: {
326 case SearchLineCommand::IsWatched: {
332 case SearchLineCommand::IsReplied: {
338 case SearchLineCommand::IsForwarded: {
344 case SearchLineCommand::To:
345 mSearchString = info.argument;
346 query.addTo(info.argument);
348 case SearchLineCommand::Bcc:
349 mSearchString = info.argument;
350 query.addBcc(info.argument);
352 case SearchLineCommand::From:
353 mSearchString = info.argument;
354 query.addFrom(info.argument);
356 case SearchLineCommand::Cc:
357 mSearchString = info.argument;
358 query.addCc(info.argument);
363 setStatus(lstStatus);
365 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
366 query.addCollection(mCurrentFolder.id());
371 mMatchingItemIds << it.id();
379#if !FORCE_DISABLE_AKONADI_SEARCH
381 if ((mSearchString == trimStr) && (mOptions == options)) {
385 mSearchString = trimStr;
386 mMatchingItemIds.
clear();
388 if (mSearchString.isEmpty()) {
391 bool needToSplitString =
false;
392 QString newStr = mSearchString;
401 for (
const QString &text : searchListTmp) {
402 if (text.size() >= 3) {
410 needToSplitString =
true;
414 if (options & SearchMessageByButtons::SearchEveryWhere) {
415 query.matches(newStr);
416 query.setSplitSearchMatchString(needToSplitString);
417 }
else if (options & SearchMessageByButtons::SearchAgainstSubject) {
418 query.subjectMatches(newStr);
419 }
else if (options & SearchMessageByButtons::SearchAgainstBody) {
420 query.bodyMatches(newStr);
421 }
else if (options & SearchMessageByButtons::SearchAgainstFrom) {
422 query.setFrom(newStr);
423 }
else if (options & SearchMessageByButtons::SearchAgainstBcc) {
425 }
else if (options & SearchMessageByButtons::SearchAgainstCc) {
427 }
else if (options & SearchMessageByButtons::SearchAgainstTo) {
432 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
433 query.addCollection(mCurrentFolder.id());
438 mMatchingItemIds << it.id();
455void Filter::generateRandomIdentifier()
460QString Filter::identifier()
const
467 d <<
"filtername " << t.filterName();
468 d <<
"identifier " << t.identifier();
470 d <<
"search option " << t.currentOptions();
471 d <<
"status " << t.
status();
475#include "moc_filter.cpp"
void setHam(bool ham=true)
void fromQInt32(qint32 status)
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 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()
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
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)