7#include "searchlinecommand.h"
8#include <KLocalizedString>
16 {SearchLineCommand::searchLineTypeToString(Subject), SearchLineCommand::SearchLineType::Subject},
17 {SearchLineCommand::searchLineTypeToString(Body), SearchLineCommand::SearchLineType::Body},
18 {SearchLineCommand::searchLineTypeToString(To), SearchLineCommand::SearchLineType::To},
19 {SearchLineCommand::searchLineTypeToString(Cc), SearchLineCommand::SearchLineType::Cc},
20 {SearchLineCommand::searchLineTypeToString(Bcc), SearchLineCommand::SearchLineType::Bcc},
21 {SearchLineCommand::searchLineTypeToString(From), SearchLineCommand::SearchLineType::From},
22 {SearchLineCommand::searchLineTypeToString(HasAttachment), SearchLineCommand::SearchLineType::HasAttachment},
23 {SearchLineCommand::searchLineTypeToString(HasInvitation), SearchLineCommand::SearchLineType::HasInvitation},
24 {SearchLineCommand::searchLineTypeToString(IsUnRead), SearchLineCommand::SearchLineType::IsUnRead},
25 {SearchLineCommand::searchLineTypeToString(IsRead), SearchLineCommand::SearchLineType::IsRead},
26 {SearchLineCommand::searchLineTypeToString(IsImportant), SearchLineCommand::SearchLineType::IsImportant},
27 {SearchLineCommand::searchLineTypeToString(IsIgnored), SearchLineCommand::SearchLineType::IsIgnored},
28 {SearchLineCommand::searchLineTypeToString(IsHam), SearchLineCommand::SearchLineType::IsHam},
29 {SearchLineCommand::searchLineTypeToString(IsSpam), SearchLineCommand::SearchLineType::IsSpam},
30 {SearchLineCommand::searchLineTypeToString(IsWatched), SearchLineCommand::SearchLineType::IsWatched},
31 {SearchLineCommand::searchLineTypeToString(IsReplied), SearchLineCommand::SearchLineType::IsReplied},
32 {SearchLineCommand::searchLineTypeToString(IsForwarded), SearchLineCommand::SearchLineType::IsForwarded},
33 {SearchLineCommand::searchLineTypeToString(IsEncrypted), SearchLineCommand::SearchLineType::IsEncrypted},
34 {SearchLineCommand::searchLineTypeToString(IsQueued), SearchLineCommand::SearchLineType::IsQueued},
35 {SearchLineCommand::searchLineTypeToString(IsSent), SearchLineCommand::SearchLineType::IsSent},
36 {SearchLineCommand::searchLineTypeToString(IsDeleted), SearchLineCommand::SearchLineType::IsDeleted},
37 {SearchLineCommand::searchLineTypeToString(Size), SearchLineCommand::SearchLineType::Size},
38 {SearchLineCommand::searchLineTypeToString(Smaller), SearchLineCommand::SearchLineType::Larger},
39 {SearchLineCommand::searchLineTypeToString(Larger), SearchLineCommand::SearchLineType::Smaller},
40 {SearchLineCommand::searchLineTypeToString(OlderThan), SearchLineCommand::SearchLineType::OlderThan},
41 {SearchLineCommand::searchLineTypeToString(NewerThan), SearchLineCommand::SearchLineType::NewerThan},
42 {SearchLineCommand::searchLineTypeToString(Category), SearchLineCommand::SearchLineType::Category},
48SearchLineCommand::SearchLineCommand() =
default;
50SearchLineCommand::~SearchLineCommand() =
default;
52bool SearchLineCommand::hasSubType(
const QString &v)
57bool SearchLineCommand::hasSubType(SearchLineCommand::SearchLineType type)
59 return type == Date || type == Size || type == To || type == Bcc || type == Cc || type == From || type == Subject || type == Smaller || type == Larger
60 || type == OlderThan || type == NewerThan || type == Body || type == Category;
63bool SearchLineCommand::isEmpty()
const
65 return mSearchLineInfo.
isEmpty();
68QString SearchLineCommand::searchLineTypeToString(SearchLineType type)
72 case HasStateOrAttachment:
77 return QStringLiteral(
"older_than");
79 return QStringLiteral(
"newer_than");
81 return QStringLiteral(
"smaller");
83 return QStringLiteral(
"larger");
85 return QStringLiteral(
"size");
87 return QStringLiteral(
"to");
89 return QStringLiteral(
"bcc");
91 return QStringLiteral(
"cc");
93 return QStringLiteral(
"from");
95 return QStringLiteral(
"subject");
97 return QStringLiteral(
"body");
99 return QStringLiteral(
"category");
101 return QStringLiteral(
"has:attachment");
103 return QStringLiteral(
"has:invitation");
105 return QStringLiteral(
"is:important");
107 return QStringLiteral(
"is:read");
109 return QStringLiteral(
"is:unread");
111 return QStringLiteral(
"is:ignored");
113 return QStringLiteral(
"is:ham");
115 return QStringLiteral(
"is:spam");
117 return QStringLiteral(
"is:watched");
119 return QStringLiteral(
"is:replied");
121 return QStringLiteral(
"is:forwarded");
123 return QStringLiteral(
"is:encrypted");
125 return QStringLiteral(
"is:queued");
127 return QStringLiteral(
"is:sent");
129 return QStringLiteral(
"is:deleted");
134QString SearchLineCommand::convertSearchLinetypeToTranslatedString(SearchLineCommand::SearchLineType type)
const
138 case HasStateOrAttachment:
146 return i18n(
"Literal string");
148 return i18n(
"Size is smaller than");
150 return i18n(
"Size is larger than");
152 return i18n(
"Size is");
154 return i18n(
"To contains");
156 return i18n(
"BCC contains");
158 return i18n(
"CC contains");
160 return i18n(
"From contains");
162 return i18n(
"Subject contains");
164 return i18n(
"Body contains");
166 return i18n(
"Mail has tag");
168 return i18n(
"Mail has attachment");
170 return i18n(
"Mail has invitation");
172 return i18n(
"Mail is important");
174 return i18n(
"Mail is read");
176 return i18n(
"Mail is Unread");
178 return i18n(
"Mail is Ignored");
180 return i18n(
"Mail is Ham");
182 return i18n(
"Mail is Spam");
184 return i18n(
"Mail is watched");
186 return i18n(
"Mail is replied");
188 return i18n(
"Mail is forwarded");
190 return i18n(
"Mail is encrypted");
192 return i18n(
"Mail is queued");
194 return i18n(
"Mail is sent");
196 return i18n(
"Mail is deleted");
201QString SearchLineCommand::generateCommadLineStr()
const
204 for (
const auto &info : mSearchLineInfo) {
208 const QString translatedType = convertSearchLinetypeToTranslatedString(info.type);
209 if (!translatedType.
isEmpty()) {
210 result += translatedType;
212 if (!info.argument.isEmpty()) {
213 if (!translatedType.
isEmpty()) {
216 result += info.argument;
222SearchLineCommand::SearchLineInfo SearchLineCommand::isAnotherInfo(
QString tmp, SearchLineInfo searchLineInfo)
228#ifdef DEBUG_COMMAND_PARSER
229 qDebug() <<
" found has subtype " << tmp;
234 for (
const QString &key : keys) {
236#ifdef DEBUG_COMMAND_PARSER
237 qDebug() <<
" found element !!!!!! " << tmp;
241 searchLineInfo.argument = tmp;
242 if (!searchLineInfo.argument.isEmpty() && searchLineInfo.type == Unknown) {
243 searchLineInfo.type = Literal;
245 if (searchLineInfo.isValid()) {
246 appendSearchLineInfo(searchLineInfo);
248#ifdef DEBUG_COMMAND_PARSER
249 qDebug() <<
" Add searchLineInfo" << searchLineInfo << mSearchLineInfo;
253 info.type = mKeyList.
value(key);
260bool SearchLineCommand::hasOnlyOneLiteralCommand()
const
262 return mSearchLineInfo.count() == 1 && (mSearchLineInfo.at(0).type == SearchLineType::Literal);
265void SearchLineCommand::parseSearchLineCommand(
const QString &str)
267 mSearchLineInfo.clear();
271 SearchLineInfo searchLineInfo;
274 for (
int i = 0, total = str.
length(); i < total; ++i) {
277#ifdef DEBUG_COMMAND_PARSER
278 qDebug() <<
" tmp ! " << tmp;
280 const SearchLineCommand::SearchLineInfo newInfo = isAnotherInfo(tmp, searchLineInfo);
281 if (newInfo.type != Unknown) {
283 searchLineInfo = newInfo;
285#ifdef DEBUG_COMMAND_PARSER
286 qDebug() <<
" contains " << tmp;
290 }
else if (hasSubType(tmp)) {
291 searchLineInfo.type = HasStateOrAttachment;
298 const SearchLineCommand::SearchLineInfo newInfo = isAnotherInfo(tmp, searchLineInfo);
299 if (newInfo.type != Unknown) {
301 searchLineInfo = newInfo;
302 }
else if (mKeyList.
contains(tmp)) {
303 searchLineInfo.type = mKeyList.
value(tmp);
306#ifdef DEBUG_COMMAND_PARSER
307 qDebug() <<
" is space " <<
"pare" << parentheses <<
" tmp " << tmp <<
"searchLineInfo.type " << searchLineInfo.type
308 <<
" searchLineInfo.argument.isEmpty() " << searchLineInfo.argument.
isEmpty();
310 if (tmp.
isEmpty() && hasSubType(searchLineInfo.type) && parentheses == 0) {
311#ifdef DEBUG_COMMAND_PARSER
312 qDebug() <<
"clear invalid type" << searchLineInfo;
314 searchLineInfo.type = Unknown;
316 }
else if (hasSubType(searchLineInfo.type)) {
318 }
else if (searchLineInfo.type != Unknown && parentheses == 0) {
319 searchLineInfo.argument = tmp;
321#ifdef DEBUG_COMMAND_PARSER
322 qDebug() <<
"clear tmp argument " << searchLineInfo;
327 if (searchLineInfo.isValid() && parentheses == 0) {
328 appendSearchLineInfo(searchLineInfo);
329 searchLineInfo.clear();
334 if (parentheses > 1) {
337#ifdef DEBUG_COMMAND_PARSER
338 qDebug() <<
" parenthese ( equal " << parentheses;
342 if (parentheses > 0) {
345#ifdef DEBUG_COMMAND_PARSER
346 qDebug() <<
" parenthese ) equal " << parentheses;
348 if (parentheses == 0) {
349 searchLineInfo.argument = tmp;
351#ifdef DEBUG_COMMAND_PARSER
352 qDebug() <<
" new values " << searchLineInfo;
354 appendSearchLineInfo(searchLineInfo);
355 searchLineInfo.clear();
359#ifdef DEBUG_COMMAND_PARSER
360 qDebug() <<
" tmp " << tmp <<
" ch " << ch <<
"end";
364 if (searchLineInfo.type != Unknown) {
365 if (searchLineInfo.type == HasStateOrAttachment) {
366#ifdef DEBUG_COMMAND_PARSER
367 qDebug() <<
" type is HasStateOrAttachment";
370 searchLineInfo.type = mKeyList.
value(tmp);
371 appendSearchLineInfo(searchLineInfo);
375#ifdef DEBUG_COMMAND_PARSER
376 qDebug() <<
" add as original searchLineInfo" << searchLineInfo;
378 const SearchLineCommand::SearchLineInfo newInfo = isAnotherInfo(tmp, searchLineInfo);
379 if (newInfo.type != Unknown) {
380 searchLineInfo = newInfo;
382 searchLineInfo.argument = tmp;
384 appendSearchLineInfo(searchLineInfo);
389 searchLineInfo.type = Literal;
390 searchLineInfo.argument = tmp;
391 appendSearchLineInfo(searchLineInfo);
394#ifdef DEBUG_COMMAND_PARSER
395 qDebug() <<
" END " << mSearchLineInfo;
402void SearchLineCommand::appendSearchLineInfo(SearchLineInfo searchLineInfo)
404 if (searchLineInfo.mustBeUnique()) {
405 if (mSearchLineInfo.contains(searchLineInfo)) {
406#ifdef DEBUG_COMMAND_PARSER
407 qDebug() <<
" Already exist " << searchLineInfo;
412 mSearchLineInfo.
append(std::move(searchLineInfo));
417 return mSearchLineInfo;
422 mSearchLineInfo = newSearchLineInfo;
425void SearchLineCommand::SearchLineInfo::clear()
427 type = SearchLineCommand::SearchLineType::Unknown;
431bool SearchLineCommand::SearchLineInfo::isValid()
const
433 if (type == SearchLineType::Unknown || type == SearchLineCommand::HasStateOrAttachment) {
436 if (type == SearchLineType::Literal && !argument.isEmpty()) {
439 if (hasSubType(type) && !argument.isEmpty()) {
442 if (!hasSubType(type) && argument.isEmpty()) {
448bool SearchLineCommand::SearchLineInfo::operator==(
const SearchLineInfo &other)
const
450 return type == other.type && argument == other.argument;
453bool SearchLineCommand::SearchLineInfo::isValidDate()
const
455 if (argument.isEmpty()) {
461bool SearchLineCommand::SearchLineInfo::mustBeUnique()
const
463 return type == HasAttachment || type == IsImportant || type == IsRead || type == IsUnRead || type == IsIgnored || type == IsHam || type == IsSpam
464 || type == IsWatched || type == IsReplied || type == IsForwarded || type == IsEncrypted || type == IsQueued || type == IsSent || type == IsDeleted;
467qint64 SearchLineCommand::SearchLineInfo::convertArgumentAsSize()
const
475 d <<
" type " << info.type;
476 d <<
" argument " << info.argument;
480#include "moc_searchlinecommand.cpp"
QString i18n(const char *text, const TYPE &arg...)
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
The implementation independent part of the MessageList library.
bool isSpace(char32_t ucs4)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
bool isValid() const const
bool isEmpty() const const
bool contains(const Key &key) const const
QList< Key > keys() const const
T value(const Key &key, const T &defaultValue) const const
QString & append(QChar ch)
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString trimmed() const const