Libksieve

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

KDE's Doxygen guidelines are available online.