Libksieve

sievetemplatewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "sievetemplatewidget.h"
8#include "sievedefaulttemplate.h"
9#include "sievetemplateeditdialog.h"
10#include <PimCommon/TemplateManager>
11
12#include <KLocalizedString>
13
14#include <QLabel>
15#include <QMimeData>
16#include <QPointer>
17#include <QVBoxLayout>
18
19using namespace KSieveUi;
20SieveTemplateListWidget::SieveTemplateListWidget(const QString &configName, QWidget *parent)
21 : PimCommon::TemplateListWidget(configName, parent)
22{
23 setKNewStuffConfigFile(QStringLiteral("ksieve_script.knsrc"));
24 loadTemplates();
25 mTemplateManager = new PimCommon::TemplateManager(QStringLiteral("sieve/scripts"), this);
26}
27
28void SieveTemplateListWidget::setSieveCapabilities(const QStringList &capabilities)
29{
30 mCapabilities = capabilities;
31}
32
33QMimeData *SieveTemplateListWidget::mimeData(const QList<QListWidgetItem *> &items) const
34{
35 if (items.isEmpty()) {
36 return nullptr;
37 }
38 auto mimeData = new QMimeData();
39 QListWidgetItem *item = items.first();
40 QString templateStr = item->data(TemplateListWidget::Text).toString();
41 if (!mCapabilities.contains(QLatin1StringView("imap4flags")) && templateStr.contains(QLatin1StringView("imap4flags"))) {
42 templateStr.replace(QStringLiteral("imap4flags"), QStringLiteral("imapflags"));
43 }
44 mimeData->setText(templateStr);
45 return mimeData;
46}
47
48SieveTemplateListWidget::~SieveTemplateListWidget() = default;
49
50QList<PimCommon::defaultTemplate> SieveTemplateListWidget::defaultTemplates()
51{
52 return KSieveUi::SieveDefaultTemplate::defaultTemplates();
53}
54
55bool SieveTemplateListWidget::addNewTemplate(QString &templateName, QString &templateScript)
56{
57 QPointer<SieveTemplateEditDialog> dlg = new SieveTemplateEditDialog(this);
58 dlg->setSieveCapabilities(mCapabilities);
59 bool result = false;
60 if (dlg->exec()) {
61 templateName = dlg->templateName();
62 templateScript = dlg->script();
63 result = true;
64 }
65 delete dlg;
66 return result;
67}
68
69bool SieveTemplateListWidget::modifyTemplate(QString &templateName, QString &templateScript, bool defaultTemplate)
70{
71 QPointer<SieveTemplateEditDialog> dlg = new SieveTemplateEditDialog(this, defaultTemplate);
72 dlg->setTemplateName(templateName);
73 dlg->setScript(templateScript);
74 dlg->setSieveCapabilities(mCapabilities);
75 bool result = false;
76 if (dlg->exec()) {
77 if (!defaultTemplate) {
78 templateName = dlg->templateName();
79 templateScript = dlg->script();
80 }
81 result = true;
82 }
83 delete dlg;
84 return result;
85}
86
87SieveTemplateWidget::SieveTemplateWidget(const QString &title, QWidget *parent)
88 : QWidget(parent)
89{
90 auto lay = new QVBoxLayout(this);
91 lay->setContentsMargins({});
92 auto lab = new QLabel(title, this);
93 lay->addWidget(lab);
94 mListTemplate = new SieveTemplateListWidget(QStringLiteral("sievetemplaterc"), this);
95 mListTemplate->setWhatsThis(i18n("You can drag and drop element on editor to import template"));
96 connect(mListTemplate, &SieveTemplateListWidget::insertTemplate, this, &SieveTemplateWidget::insertTemplate);
97 lay->addWidget(mListTemplate);
98}
99
100SieveTemplateWidget::~SieveTemplateWidget() = default;
101
102void SieveTemplateWidget::setSieveCapabilities(const QStringList &capabilities)
103{
104 mListTemplate->setSieveCapabilities(capabilities);
105}
106
107#include "moc_sievetemplatewidget.cpp"
QString i18n(const char *text, const TYPE &arg...)
Capabilities capabilities()
QListWidgetItem * item(int row) const const
QList< QListWidgetItem * > items(const QMimeData *data) const const
virtual QVariant data(int role) const const
void setText(const QString &text)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.