Pimcommon

kmailcompletion.cpp
1/*
2 This file is part of libkdepim.
3
4 SPDX-FileCopyrightText: 2006 Christian Schaarschmidt <schaarsc@gmx.de>
5 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "kmailcompletion.h"
11#include <QRegularExpression>
12#include <QSet>
13
14using namespace PimCommon;
15
16KMailCompletion::KMailCompletion()
17{
18 setIgnoreCase(true);
19}
20
22{
23 m_keyMap.clear();
25}
26
28{
30
31 static const QRegularExpression emailRegularExpression{QRegularExpression(QStringLiteral("(@)|(<.*>)"))};
32 // this should be in postProcessMatch, but postProcessMatch is const and will not allow nextMatch
33 if (!match.isEmpty()) {
34 const QString firstMatch(match);
35 while (match.indexOf(emailRegularExpression) == -1) {
36 /* local email do not require @domain part, if match is an address we'll
37 * find last+first <match> in m_keyMap and we'll know that match is
38 * already a valid email.
39 *
40 * Distribution list do not have last+first <match> entry, they will be
41 * in mailAddr
42 */
43 const QStringList &mailAddr = m_keyMap[match]; // get all mailAddr for this keyword
44 bool isEmail = false;
45 for (QStringList::ConstIterator sit(mailAddr.begin()), sEnd(mailAddr.end()); sit != sEnd; ++sit) {
46 if ((*sit).indexOf(QLatin1Char('<') + match + QLatin1Char('>')) != -1 || (*sit) == match) {
47 isEmail = true;
48 break;
49 }
50 }
51
52 if (!isEmail) {
53 // match is a keyword, skip it and try to find match <email@domain>
54 match = nextMatch();
55 if (firstMatch == match) {
56 match.clear();
57 break;
58 }
59 } else {
60 break;
61 }
62 }
63 }
64 return match;
65}
66
67void KMailCompletion::addItemWithKeys(const QString &email, int weight, const QStringList *keyWords)
68{
69 Q_ASSERT(keyWords != nullptr);
70 QStringList::ConstIterator end = keyWords->constEnd();
71 for (QStringList::ConstIterator it(keyWords->constBegin()); it != end; ++it) {
72 QStringList &emailList = m_keyMap[(*it)]; // lookup email-list for given keyword
73 if (!emailList.contains(email)) { // add email if not there
74 emailList.append(email);
75 }
76 addItem((*it), weight); // inform KCompletion about keyword
77 }
78}
79
81{
82 Q_ASSERT(pMatches != nullptr);
83 if (pMatches->isEmpty()) {
84 return;
85 }
86
87 // KCompletion has found the keywords for us, we can now map them to mail-addr
88 QSet<QString> mailAddrDistinct;
89 for (QStringList::ConstIterator sit2(pMatches->begin()), sEnd2(pMatches->end()); sit2 != sEnd2; ++sit2) {
90 const QStringList &mailAddr = m_keyMap[(*sit2)]; // get all mailAddr for this keyword
91 if (mailAddr.isEmpty()) {
92 mailAddrDistinct.insert(*sit2);
93 } else {
94 for (QStringList::ConstIterator sit(mailAddr.begin()), sEnd(mailAddr.end()); sit != sEnd; ++sit) {
95 mailAddrDistinct.insert(*sit); // store mailAddr, QSet will make them unique
96 }
97 }
98 }
99 pMatches->clear(); // delete keywords
100 (*pMatches) += mailAddrDistinct.values(); // add emailAddr
101}
102
103#include "moc_kmailcompletion.cpp"
QString nextMatch()
virtual QString makeCompletion(const QString &string)
virtual void setIgnoreCase(bool ignoreCase)
virtual void clear()
void match(const QString &item)
void addItem(const QString &item)
QString makeCompletion(const QString &string) override
uses KCompletion::makeCompletion to find email addresses which starts with string.
void postProcessMatches(QStringList *pMatches) const override
use internal map to replace all keywords in pMatches with corresponding email addresses.
void addItemWithKeys(const QString &email, int weight, const QStringList *keyWords)
specify keywords for email.
void clear() override
clears internal keyword map and calls KCompletion::clear.
folderdialogacltab.h
typedef ConstIterator
void append(QList< T > &&value)
iterator begin()
void clear()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
bool isEmpty() const const
void clear()
iterator insert(const T &value)
QList< T > values() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:23 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.