8#include "blacklistakonadisearchemailcompletionwidget.h"
10#include "blacklistakonadisearchemaillist.h"
11#include "blacklistakonadisearchemailsearchjob.h"
12#include "blacklistakonadisearchemailutil.h"
13#include "blacklistakonadisearchemailwarning.h"
14#include <KLineEditEventHandler>
16#include "pimcommonakonadi_debug.h"
17#include <KConfigGroup>
18#include <KListWidgetSearchLine>
19#include <KLocalizedString>
20#include <KSharedConfig>
30BlackListAkonadiSearchEmailCompletionWidget::BlackListAkonadiSearchEmailCompletionWidget(
QWidget *parent)
32 , mNumberOfEmailsFound(new
QLabel(this))
34 , mExcludeDomainLineEdit(new
QLineEdit(this))
35 , mEmailList(new BlackListAkonadiSearchEmailList(this))
36 , mSearchButton(new
QPushButton(
QIcon::fromTheme(QStringLiteral(
"edit-find")),
i18n(
"Search"), this))
38 , mUnselectButton(new
QPushButton(
i18nc(
"@action:button",
"&Unselect"), this))
39 , mShowAllBlackListedEmails(new
QPushButton(
i18nc(
"@action:button",
"Show Blacklisted Emails"), this))
40 , mMoreResult(new
QLabel(
i18nc(
"@label:textbox",
"<qt><a href=\"more_result\">More result...</a></qt>"), this))
41 , mBlackListWarning(new BlackListAkonadiSearchEmailWarning(this))
42 , mExcludeEmailFromRegularExpressionLineEdit(new
QLineEdit(this))
49 auto lab =
new QLabel(
i18nc(
"@label:textbox",
"Search email:"),
this);
50 lab->setObjectName(
"search_label"_L1);
51 searchLayout->addWidget(lab);
53 mSearchLineEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Research is done from 3 characters"));
54 mSearchLineEdit->setFocus();
55 mSearchLineEdit->setClearButtonEnabled(
true);
57 mSearchLineEdit->setObjectName(
"search_lineedit"_L1);
59 searchLayout->addWidget(mSearchLineEdit);
61 mSearchButton->setObjectName(
"search_button"_L1);
63 mSearchButton->setEnabled(
false);
64 searchLayout->addWidget(mSearchButton);
66 mShowAllBlackListedEmails->setObjectName(
"show_blacklisted_email_button"_L1);
68 searchLayout->addWidget(mShowAllBlackListedEmails);
70 mEmailList->setObjectName(
"email_list"_L1);
71 mainLayout->addWidget(mEmailList);
73 mBlackListWarning->setObjectName(
"backlistwarning"_L1);
74 connect(mBlackListWarning, &BlackListAkonadiSearchEmailWarning::newSearch,
this, &BlackListAkonadiSearchEmailCompletionWidget::slotSearch);
75 connect(mBlackListWarning, &BlackListAkonadiSearchEmailWarning::saveChanges,
this, &BlackListAkonadiSearchEmailCompletionWidget::slotSaveChanges);
76 mainLayout->addWidget(mBlackListWarning);
82 searchLineLayout->
addLayout(selectElementLayout);
84 mSelectButton->setObjectName(
"select_email"_L1);
86 selectElementLayout->addWidget(mSelectButton);
88 mUnselectButton->setObjectName(
"unselect_email"_L1);
90 selectElementLayout->addWidget(mUnselectButton);
92 mMoreResult->setObjectName(
"moreresultlabel"_L1);
93 selectElementLayout->addWidget(mMoreResult);
97 mMoreResult->setVisible(
false);
98 selectElementLayout->addStretch(1);
103 mSearchInResultLineEdit->setObjectName(
"searchinresultlineedit"_L1);
104 mSearchInResultLineEdit->setClearButtonEnabled(
true);
105 mSearchInResultLineEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Search in result..."));
108 searchLineLayout->addStretch(0);
109 mNumberOfEmailsFound->setObjectName(
"numberofemailsfound"_L1);
111 searchLineLayout->addWidget(mNumberOfEmailsFound, 1);
112 searchLineLayout->addWidget(mSearchInResultLineEdit);
116 mainLayout->addLayout(excludeDomainLayout);
118 auto excludeDomainLabel =
new QLabel(
i18nc(
"@label:textbox",
"Exclude domain names:"),
this);
119 excludeDomainLabel->setObjectName(
"domain_label"_L1);
120 excludeDomainLayout->addWidget(excludeDomainLabel);
122 excludeDomainLayout->addWidget(mExcludeDomainLineEdit);
123 mExcludeDomainLineEdit->setObjectName(
"domain_lineedit"_L1);
124 mExcludeDomainLineEdit->setClearButtonEnabled(
true);
126 mExcludeDomainLineEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Separate domain with \'%1\'",
QLatin1Char(
',')));
128 auto excludeEmailRegularExpressionLayout =
new QHBoxLayout;
130 mainLayout->addLayout(excludeEmailRegularExpressionLayout);
132 auto excludeEmailRegularExpressionLabel =
new QLabel(
i18nc(
"@label:textbox",
"Exclude email with Regular Expression:"),
this);
133 excludeEmailRegularExpressionLabel->setObjectName(
"email_regularexpression_label"_L1);
134 excludeEmailRegularExpressionLayout->addWidget(excludeEmailRegularExpressionLabel);
136 excludeEmailRegularExpressionLayout->addWidget(mExcludeEmailFromRegularExpressionLineEdit);
137 mExcludeEmailFromRegularExpressionLineEdit->setObjectName(
"exclude_email_lineedit"_L1);
138 mExcludeEmailFromRegularExpressionLineEdit->setClearButtonEnabled(
true);
140 mExcludeEmailFromRegularExpressionLineEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Separate regular expression with \'%1\'",
QLatin1Char(
',')));
143 slotSelectionChanged();
147BlackListAkonadiSearchEmailCompletionWidget::~BlackListAkonadiSearchEmailCompletionWidget() =
default;
149void BlackListAkonadiSearchEmailCompletionWidget::slotSelectionChanged()
151 const bool selectionIsNotEmpty = !mEmailList->selectedItems().isEmpty();
152 mSelectButton->setEnabled(selectionIsNotEmpty);
153 mUnselectButton->setEnabled(selectionIsNotEmpty);
156void BlackListAkonadiSearchEmailCompletionWidget::slotCustomContextMenuRequested(
const QPoint &pos)
158 const bool selectionIsNotEmpty = !mEmailList->selectedItems().isEmpty();
159 if (selectionIsNotEmpty) {
161 menu.addAction(
i18n(
"Select"),
this, &BlackListAkonadiSearchEmailCompletionWidget::slotSelectEmails);
163 menu.addAction(
i18n(
"Unselect"),
this, &BlackListAkonadiSearchEmailCompletionWidget::slotUnselectEmails);
168void BlackListAkonadiSearchEmailCompletionWidget::load()
171 KConfigGroup group(config, QStringLiteral(
"AddressLineEdit"));
173 const QStringList lstExcludeEmailsRegularExpressions = group.readEntry(
"ExcludeEmailsRegexp", QStringList());
174 mEmailList->setExcludeEmailsRegularExpressions(lstExcludeEmailsRegularExpressions);
175 mExcludeEmailFromRegularExpressionLineEdit->setText(lstExcludeEmailsRegularExpressions.
join(QLatin1Char(
',')));
176 mOriginalExcludeEmailRegexp = lstExcludeEmailsRegularExpressions;
178 const QStringList lstExcludeDomains = group.readEntry(
"ExcludeDomain", QStringList());
179 mEmailList->setExcludeDomains(lstExcludeDomains);
180 mExcludeDomainLineEdit->setText(lstExcludeDomains.
join(QLatin1Char(
',')));
181 mOriginalExcludeDomain = lstExcludeDomains;
182 slotSelectionChanged();
185void BlackListAkonadiSearchEmailCompletionWidget::save()
187 const QString excludeEmailsRegexp = mExcludeEmailFromRegularExpressionLineEdit->text().remove(QLatin1Char(
' '));
189 bool needToSave = (mOriginalExcludeEmailRegexp != newExcludeEmailsRegExp);
191 const QString excludeDomains = mExcludeDomainLineEdit->text().
remove(QLatin1Char(
' '));
193 needToSave |= (mOriginalExcludeDomain != newExcludeDomain);
196 KConfigGroup group(config, QStringLiteral(
"AddressLineEdit"));
197 const QHash<QString, bool> result = mEmailList->blackListItemChanged();
200 QStringList blackList = group.readEntry(
"BalooBackList", QStringList());
201 PimCommon::BlackListAkonadiSearchEmailUtil util;
202 util.initialBlackList(blackList);
203 util.setNewBlackList(result);
204 blackList = util.createNewBlackList();
205 group.writeEntry(
"BalooBackList", blackList);
208 group.writeEntry(
"ExcludeEmailsRegexp", newExcludeEmailsRegExp);
209 mEmailList->setExcludeEmailsRegularExpressions(newExcludeEmailsRegExp);
210 mOriginalExcludeEmailRegexp = newExcludeEmailsRegExp;
212 group.writeEntry(
"ExcludeDomain", newExcludeDomain);
213 mEmailList->setExcludeDomains(newExcludeDomain);
214 mOriginalExcludeDomain = newExcludeDomain;
217 config->reparseConfiguration();
220void BlackListAkonadiSearchEmailCompletionWidget::slotSaveChanges()
223 const QHash<QString, bool> result = mEmailList->blackListItemChanged();
225 KConfigGroup group(config, QStringLiteral(
"AddressLineEdit"));
226 QStringList blackList = group.readEntry(
"BalooBackList", QStringList());
227 PimCommon::BlackListAkonadiSearchEmailUtil util;
228 util.initialBlackList(blackList);
229 util.setNewBlackList(result);
230 blackList = util.createNewBlackList();
231 group.writeEntry(
"BalooBackList", blackList);
233 mEmailList->setEmailBlackList(blackList);
237void BlackListAkonadiSearchEmailCompletionWidget::slotUnselectEmails()
239 const QList<QListWidgetItem *> lstSelected = mEmailList->selectedItems();
240 for (QListWidgetItem *item : lstSelected) {
245void BlackListAkonadiSearchEmailCompletionWidget::slotSelectEmails()
247 const QList<QListWidgetItem *> lstSelected = mEmailList->selectedItems();
248 for (QListWidgetItem *item : lstSelected) {
253void BlackListAkonadiSearchEmailCompletionWidget::slotSearchLineEditChanged(
const QString &text)
256 hideMoreResultAndChangeLimit();
259void BlackListAkonadiSearchEmailCompletionWidget::hideMoreResultAndChangeLimit()
261 mMoreResult->setVisible(
false);
265void BlackListAkonadiSearchEmailCompletionWidget::slotSearch()
267 const QString searchEmail = mSearchLineEdit->text().trimmed();
268 if (searchEmail.
length() > 2) {
269 mSearchInResultLineEdit->clear();
270 auto job =
new PimCommon::BlackListAkonadiSearchEmailSearchJob(
this);
271 job->setSearchEmail(searchEmail);
272 job->setLimit(mLimit);
273 connect(job, &BlackListAkonadiSearchEmailSearchJob::emailsFound,
this, &BlackListAkonadiSearchEmailCompletionWidget::slotEmailFound);
275 qCWarning(PIMCOMMONAKONADI_LOG) <<
"Impossible to start search job";
280void BlackListAkonadiSearchEmailCompletionWidget::slotEmailFound(
const QStringList &list)
282 const int numberOfEmails = mEmailList->setEmailFound(list);
283 mMoreResult->setVisible(numberOfEmails == mLimit);
284 mEmailList->scrollToBottom();
285 if (numberOfEmails == 0) {
286 mNumberOfEmailsFound->setText(
i18n(
"No email found."));
288 mNumberOfEmailsFound->setText(
i18np(
"1 email found",
"%1 emails found", numberOfEmails));
292void BlackListAkonadiSearchEmailCompletionWidget::setEmailBlackList(
const QStringList &list)
294 mEmailList->setEmailBlackList(list);
297void BlackListAkonadiSearchEmailCompletionWidget::slotCheckIfUpdateBlackListIsNeeded()
299 const QHash<QString, bool> result = mEmailList->blackListItemChanged();
303 mBlackListWarning->animatedShow();
307void BlackListAkonadiSearchEmailCompletionWidget::slotLinkClicked(
const QString &link)
309 if (link ==
"more_result"_L1) {
315void BlackListAkonadiSearchEmailCompletionWidget::slotShowAllBlacklistedEmail()
318 KConfigGroup group(config, QStringLiteral(
"AddressLineEdit"));
319 const QStringList balooBlackList = group.readEntry(
"BalooBackList", QStringList());
320 slotEmailFound(balooBlackList);
323#include "moc_blacklistakonadisearchemailcompletionwidget.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void addLayout(QLayout *layout, int stretch)
bool isEmpty() const const
void linkActivated(const QString &link)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
void remove(qsizetype i, qsizetype n)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
qsizetype length() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QString join(QChar separator) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)