Messagelib

filter.cpp
1/******************************************************************************
2 *
3 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 *******************************************************************************/
8
9#include "core/filter.h"
10#include "config-messagelist.h"
11#include "core/messageitem.h"
12#include <MessageCore/StringUtil>
13#include <TextUtils/ConvertText>
14
15#include <KRandom>
16#if !FORCE_DISABLE_AKONADI_SEARCH
17#include <PIM/emailquery.h>
18#include <PIM/resultiterator.h>
19#endif
20using namespace MessageList::Core;
21
22Filter::Filter(QObject *parent)
23 : QObject(parent)
24{
25 generateRandomIdentifier();
26}
27
28bool Filter::containString(const QString &searchInString) const
29{
30 bool found = false;
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)) {
34 found = true;
35 } else {
36 found = false;
37 break;
38 }
39 }
40 return found;
41}
42
43const QString &Filter::iconName() const
44{
45 return mIconName;
46}
47
48void Filter::setIconName(const QString &newIconName)
49{
50 mIconName = newIconName;
51}
52
53void Filter::setOptions(SearchMessageByButtons::SearchOptions newOptions)
54{
55 mOptions = newOptions;
56}
57
58const QString &Filter::filterName() const
59{
60 return mFilterName;
61}
62
63void Filter::setFilterName(const QString &newFilterName)
64{
65 mFilterName = newFilterName;
66}
67
68void Filter::setIdentifier(const QString &newIdentifier)
69{
70 mIdentifier = newIdentifier;
71}
72
73bool Filter::match(const MessageItem *item) const
74{
75 if (!mStatus.isEmpty()) {
76 for (Akonadi::MessageStatus status : std::as_const(mStatus)) {
77 if (!(status & item->status())) {
78 return false;
79 }
80 }
81 }
82
83 if (!mSearchString.isEmpty()) {
84 if (mMatchingItemIds.contains(item->itemId())) {
85 return true;
86 }
87
88 bool searchMatches = false;
89 bool searchEveryWhere = (mOptions & SearchMessageByButtons::SearchEveryWhere);
90 if (containString(item->subject()) && ((mOptions & SearchMessageByButtons::SearchAgainstSubject) || searchEveryWhere)) {
91 searchMatches = true;
92 } else if (containString(item->sender()) && ((mOptions & SearchMessageByButtons::SearchAgainstFrom) || searchEveryWhere)) {
93 searchMatches = true;
94 } else if (containString(item->receiver()) && ((mOptions & SearchMessageByButtons::SearchAgainstTo) || searchEveryWhere)) {
95 searchMatches = true;
96 }
97 if (!searchMatches) {
98 return false;
99 }
100 }
101
102 if (!mTagId.isEmpty()) {
103 // mTagId is a Akonadi::Tag::url
104 const bool tagMatches = item->findTag(mTagId) != nullptr;
105 if (!tagMatches) {
106 return false;
107 }
108 }
109
110 return true;
111}
112
113QList<Akonadi::MessageStatus> Filter::status() const
114{
115 return mStatus;
116}
117
118void Filter::setStatus(const QList<Akonadi::MessageStatus> &lstStatus)
119{
120 mStatus = lstStatus;
121}
122
123bool Filter::isEmpty() const
124{
125 if (!mStatus.isEmpty()) {
126 return false;
127 }
128
129 if (!mSearchString.isEmpty()) {
130 return false;
131 }
132
133 if (!mTagId.isEmpty()) {
134 return false;
135 }
136
137 return true;
138}
139
140void Filter::clear()
141{
142 mStatus.clear();
143 mSearchString.clear();
144 mTagId.clear();
145 mMatchingItemIds.clear();
146 mSearchList.clear();
147}
148
149void Filter::setCurrentFolder(const Akonadi::Collection &folder)
150{
151 mCurrentFolder = folder;
152}
153
154QList<SearchLineCommand::SearchLineInfo> Filter::searchLineCommands() const
155{
156 const QString text = mSearchString;
158 if (mOptions & SearchMessageByButtons::SearchAgainstBody) {
159 SearchLineCommand::SearchLineInfo i;
160 i.type = SearchLineCommand::SearchLineType::Body;
161 i.argument = mSearchString;
162 if (i.isValid()) {
163 infos.append(std::move(i));
164 }
165 }
166 if (mOptions & SearchMessageByButtons::SearchAgainstSubject) {
167 SearchLineCommand::SearchLineInfo i;
168 i.type = SearchLineCommand::SearchLineType::Subject;
169 i.argument = mSearchString;
170 if (i.isValid()) {
171 infos.append(std::move(i));
172 }
173 }
174 if (mOptions & SearchMessageByButtons::SearchAgainstBcc) {
175 SearchLineCommand::SearchLineInfo i;
176 i.type = SearchLineCommand::SearchLineType::Bcc;
177 i.argument = mSearchString;
178 if (i.isValid()) {
179 infos.append(std::move(i));
180 }
181 }
182 if (mOptions & SearchMessageByButtons::SearchAgainstCc) {
183 SearchLineCommand::SearchLineInfo i;
184 i.type = SearchLineCommand::SearchLineType::Cc;
185 i.argument = mSearchString;
186 if (i.isValid()) {
187 infos.append(std::move(i));
188 }
189 }
190
192 if (status.hasAttachment()) {
193 SearchLineCommand::SearchLineInfo i;
194 i.type = SearchLineCommand::SearchLineType::HasAttachment;
195 if (i.isValid()) {
196 infos.append(std::move(i));
197 }
198 } else if (status.hasInvitation()) {
199 SearchLineCommand::SearchLineInfo i;
200 i.type = SearchLineCommand::SearchLineType::HasInvitation;
201 if (i.isValid()) {
202 infos.append(std::move(i));
203 }
204 } else if (status.isImportant()) {
205 SearchLineCommand::SearchLineInfo i;
206 i.type = SearchLineCommand::SearchLineType::IsImportant;
207 if (i.isValid()) {
208 infos.append(std::move(i));
209 }
210 } else if (status.isEncrypted()) {
211 SearchLineCommand::SearchLineInfo i;
212 i.type = SearchLineCommand::SearchLineType::IsEncrypted;
213 if (i.isValid()) {
214 infos.append(std::move(i));
215 }
216 } else if (status.isReplied()) {
217 SearchLineCommand::SearchLineInfo i;
218 i.type = SearchLineCommand::SearchLineType::IsReplied;
219 if (i.isValid()) {
220 infos.append(std::move(i));
221 }
222 } else if (status.isForwarded()) {
223 SearchLineCommand::SearchLineInfo i;
224 i.type = SearchLineCommand::SearchLineType::IsForwarded;
225 if (i.isValid()) {
226 infos.append(std::move(i));
227 }
228 } else if (status.isRead()) {
229 SearchLineCommand::SearchLineInfo i;
230 i.type = SearchLineCommand::SearchLineType::IsRead;
231 if (i.isValid()) {
232 infos.append(std::move(i));
233 }
234 } else if (status.isIgnored()) {
235 SearchLineCommand::SearchLineInfo i;
236 i.type = SearchLineCommand::SearchLineType::IsIgnored;
237 if (i.isValid()) {
238 infos.append(std::move(i));
239 }
240 } else if (status.isSpam()) {
241 SearchLineCommand::SearchLineInfo i;
242 i.type = SearchLineCommand::SearchLineType::IsSpam;
243 if (i.isValid()) {
244 infos.append(std::move(i));
245 }
246 } else if (status.isHam()) {
247 SearchLineCommand::SearchLineInfo i;
248 i.type = SearchLineCommand::SearchLineType::IsHam;
249 if (i.isValid()) {
250 infos.append(std::move(i));
251 }
252 } else if (status.isQueued()) {
253 SearchLineCommand::SearchLineInfo i;
254 i.type = SearchLineCommand::SearchLineType::IsQueued;
255 if (i.isValid()) {
256 infos.append(std::move(i));
257 }
258 } else if (status.isSent()) {
259 SearchLineCommand::SearchLineInfo i;
260 i.type = SearchLineCommand::SearchLineType::IsSent;
261 if (i.isValid()) {
262 infos.append(std::move(i));
263 }
264 } else if (status.isDeleted()) {
265 SearchLineCommand::SearchLineInfo i;
266 i.type = SearchLineCommand::SearchLineType::IsDeleted;
267 if (i.isValid()) {
268 infos.append(std::move(i));
269 }
270 }
271 }
272
273#if 0
274
275 if (mContainsOutboundMessages) {
276 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstTo);
277 } else {
278 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstFrom);
279 }
280#endif
281 SearchLineCommand command;
282 command.parseSearchLineCommand(text);
283 infos += command.searchLineInfo();
284 return infos;
285}
286
287const QString &Filter::searchString() const
288{
289 return mSearchString;
290}
291
292SearchMessageByButtons::SearchOptions Filter::currentOptions() const
293{
294 return mOptions;
295}
296
297void Filter::save(const KSharedConfig::Ptr &config, const QString &filtername, const QString &iconName, int numFilter)
298{
299 KConfigGroup grp(config, QStringLiteral("General"));
300 int numberFilter = (numFilter == -1) ? grp.readEntry("NumberFilter").toInt() : numFilter;
301 KConfigGroup newGroup(config, QStringLiteral("Filter_%1").arg(numberFilter++));
302 newGroup.writeEntry("name", filtername);
303 if (!iconName.isEmpty()) {
304 newGroup.writeEntry("iconName", iconName);
305 }
306 newGroup.writeEntry("searchString", mSearchString);
307 newGroup.writeEntry("searchOptions", static_cast<int>(mOptions));
308 newGroup.writeEntry("tagId", mTagId);
309 newGroup.writeEntry("identifier", mIdentifier);
310 QList<qint32> lst;
311 lst.reserve(mStatus.count());
312 for (const auto s : std::as_const(mStatus)) {
313 lst << s.toQInt32();
314 }
315 newGroup.writeEntry("status", lst);
316 newGroup.sync();
317 grp.writeEntry("NumberFilter", numberFilter);
318 grp.sync();
319 config->reparseConfiguration();
320}
321
322Filter *Filter::load(const KSharedConfig::Ptr &config, int filternumber)
323{
324 KConfigGroup grp(config, QStringLiteral("General"));
325 int numberFilter = grp.readEntry("NumberFilter").toInt();
326 if (filternumber < numberFilter) {
327 KConfigGroup newGroup(config, QStringLiteral("Filter_%1").arg(filternumber));
328 return loadFromConfigGroup(newGroup);
329 }
330 return nullptr;
331}
332
333Filter *Filter::loadFromConfigGroup(const KConfigGroup &newGroup)
334{
335 auto filter = new Filter();
336 filter->setSearchString(newGroup.readEntry("searchString"),
337 static_cast<SearchMessageByButtons::SearchOptions>(newGroup.readEntry("searchOptions").toInt()));
338 filter->setTagId(newGroup.readEntry("tagId"));
339 filter->setIdentifier(newGroup.readEntry("identifier"));
340 filter->setFilterName(newGroup.readEntry("name"));
341 filter->setIconName(newGroup.readEntry("iconName"));
342 const QList<qint32> lst = newGroup.readEntry("status", QList<qint32>());
343 QList<Akonadi::MessageStatus> messageStatusLst;
344 messageStatusLst.reserve(lst.count());
345 for (const auto s : std::as_const(lst)) {
348 messageStatusLst << status;
349 }
350 filter->setStatus(messageStatusLst);
351 filter->setOptions(static_cast<SearchMessageByButtons::SearchOptions>(newGroup.readEntry("searchOptions").toInt()));
352 return filter;
353}
354
355void Filter::setSearchString(const SearchLineCommand &command)
356{
357#if !FORCE_DISABLE_AKONADI_SEARCH
358 mMatchingItemIds.clear();
359 if (command.isEmpty()) {
360 return;
361 }
362 const QList<SearchLineCommand::SearchLineInfo> infos = command.searchLineInfo();
365 for (const auto &info : infos) {
366 switch (info.type) {
367 case SearchLineCommand::Literal: {
368 QString newStr;
369 const QStringList searchListTmp = info.argument.split(QLatin1Char(' '), Qt::SkipEmptyParts);
370 bool needToSplitString = false;
371 for (const QString &text : searchListTmp) {
372 if (text.size() >= 3) {
373 if (!newStr.isEmpty()) {
374 newStr += QLatin1Char(' ');
375 }
376 newStr += text;
377 }
378 }
379 needToSplitString = true;
380
381 mSearchString = newStr;
382 query.matches(newStr);
383 query.setSplitSearchMatchString(needToSplitString);
384 break;
385 }
386 case SearchLineCommand::Subject: {
387 mSearchString = info.argument;
388 query.subjectMatches(mSearchString);
389 break;
390 }
391 case SearchLineCommand::Body: {
392 mSearchString = info.argument;
393 query.bodyMatches(mSearchString);
394 break;
395 }
396 case SearchLineCommand::Unknown:
397 case SearchLineCommand::HasStateOrAttachment:
398 // Nothing
399 break;
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:
407 // TODO implement tag support
408 break;
409 case SearchLineCommand::HasAttachment: {
412 lstStatus.append(status);
413 break;
414 }
415 case SearchLineCommand::HasInvitation: {
418 lstStatus.append(status);
419 break;
420 }
421 case SearchLineCommand::IsImportant: {
423 status.setImportant(true);
424 lstStatus.append(status);
425 break;
426 }
427 case SearchLineCommand::IsRead: {
429 status.setRead(true);
430 lstStatus.append(status);
431 break;
432 }
433 case SearchLineCommand::IsUnRead: {
434 // TODO verify
436 status.setRead(false);
437 lstStatus.append(status);
438 break;
439 }
440 case SearchLineCommand::IsIgnored: {
442 status.setIgnored(true);
443 lstStatus.append(status);
444 break;
445 }
446 case SearchLineCommand::IsHam: {
448 status.setHam(true);
449 lstStatus.append(status);
450 break;
451 }
452 case SearchLineCommand::IsSpam: {
454 status.setSpam(true);
455 lstStatus.append(status);
456 break;
457 }
458 case SearchLineCommand::IsWatched: {
460 status.setWatched(true);
461 lstStatus.append(status);
462 break;
463 }
464 case SearchLineCommand::IsReplied: {
466 status.setReplied(true);
467 lstStatus.append(status);
468 break;
469 }
470 case SearchLineCommand::IsEncrypted: {
472 status.setEncrypted(true);
473 lstStatus.append(status);
474 break;
475 }
476 case SearchLineCommand::IsQueued: {
478 status.setQueued(true);
479 lstStatus.append(status);
480 break;
481 }
482 case SearchLineCommand::IsDeleted: {
484 status.setDeleted(true);
485 lstStatus.append(status);
486 break;
487 }
488 case SearchLineCommand::IsSent: {
490 status.setSent(true);
491 lstStatus.append(status);
492 break;
493 }
494 case SearchLineCommand::IsForwarded: {
496 status.setForwarded(true);
497 lstStatus.append(status);
498 break;
499 }
500 case SearchLineCommand::To: {
501 mSearchString = info.argument;
502 query.addTo(info.argument);
503 break;
504 }
505 case SearchLineCommand::Bcc: {
506 mSearchString = info.argument;
507 query.addBcc(info.argument);
508 break;
509 }
510 case SearchLineCommand::From: {
511 mSearchString = info.argument;
512 query.addFrom(info.argument);
513 break;
514 }
515 case SearchLineCommand::Cc: {
516 mSearchString = info.argument;
517 query.addCc(info.argument);
518 break;
519 }
520 }
521 }
522
523 setStatus(lstStatus);
524 // If the collection is virtual we're probably trying to filter the search collection, so we just search globally
525 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
526 query.addCollection(mCurrentFolder.id());
527 }
528
530 while (it.next()) {
531 mMatchingItemIds << it.id();
532 }
533 Q_EMIT finished();
534#endif
535}
536
537void Filter::setSearchString(const QString &search, SearchMessageByButtons::SearchOptions options)
538{
539#if !FORCE_DISABLE_AKONADI_SEARCH
540 const QString trimStr = search.trimmed();
541 if ((mSearchString == trimStr) && (mOptions == options)) {
542 return;
543 }
544 mOptions = options;
545 mSearchString = trimStr;
546 mMatchingItemIds.clear();
547
548 if (mSearchString.isEmpty()) {
549 return;
550 }
551 bool needToSplitString = false;
552 QString newStr = mSearchString;
553 if (mSearchString.startsWith(QLatin1Char('"')) && mSearchString.startsWith(QLatin1Char('"'))) {
554 newStr.remove(0, 1);
555 newStr.remove(newStr.length() - 1, 1);
556 mSearchList = QStringList() << newStr;
557 } else {
558 const QStringList searchListTmp = mSearchString.split(QLatin1Char(' '), Qt::SkipEmptyParts);
559 mSearchList.clear();
560 newStr.clear();
561 for (const QString &text : searchListTmp) {
562 if (text.size() >= 3) {
563 mSearchList << text;
564 if (!newStr.isEmpty()) {
565 newStr += QLatin1Char(' ');
566 }
567 newStr += text;
568 }
569 }
570 needToSplitString = true;
571 }
572 if (!newStr.trimmed().isEmpty()) {
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) {
584 query.setBcc(QStringList() << newStr);
585 } else if (options & SearchMessageByButtons::SearchAgainstCc) {
586 query.setCc(QStringList() << newStr);
587 } else if (options & SearchMessageByButtons::SearchAgainstTo) {
588 query.setTo(QStringList() << newStr);
589 }
590
591 // If the collection is virtual we're probably trying to filter the search collection, so we just search globally
592 if (mCurrentFolder.isValid() && !mCurrentFolder.isVirtual()) {
593 query.addCollection(mCurrentFolder.id());
594 }
595
596 Akonadi::Search::PIM::ResultIterator it = query.exec();
597 while (it.next()) {
598 mMatchingItemIds << it.id();
599 }
600 }
601 Q_EMIT finished();
602#endif
603}
604
605const QString &Filter::tagId() const
606{
607 return mTagId;
608}
609
610void Filter::setTagId(const QString &tagId)
611{
612 mTagId = tagId;
613}
614
615void Filter::generateRandomIdentifier()
616{
617 mIdentifier = KRandom::randomString(16);
618}
619
620QString Filter::identifier() const
621{
622 return mIdentifier;
623}
624
625QDebug operator<<(QDebug d, const MessageList::Core::Filter &t)
626{
627 d << "filtername " << t.filterName();
628 d << "identifier " << t.identifier();
629 d << "search string " << t.searchString();
630 d << "search option " << t.currentOptions();
631 d << "status " << t.status();
632 return d;
633}
634
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.
Definition filter.h:34
QList< Akonadi::MessageStatus > status() const
Returns the currently set status mask.
Definition filter.cpp:113
const QString & searchString() const
Returns the currently set search string.
Definition filter.cpp:287
const QString & receiver() const
Returns the receiver associated to this item.
Definition item.cpp:504
const Akonadi::MessageStatus & status() const
Returns the status associated to this Item.
Definition item.cpp:449
const QString & sender() const
Returns the sender associated to this item.
Definition item.cpp:489
const QString & subject() const
Returns the subject associated to this Item.
Definition item.cpp:534
The MessageItem class.
Definition messageitem.h:36
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.
Definition aggregation.h:22
void append(QList< T > &&value)
void clear()
qsizetype count() const const
void reserve(qsizetype size)
void clear()
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
CaseInsensitive
SkipEmptyParts
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:55:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.