Libksieve

sievetemplatewidget.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "sievetemplatewidget.h"
21 #include "sievetemplateeditdialog.h"
22 #include "sievedefaulttemplate.h"
23 #include <PimCommon/TemplateManager>
24 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
25 
26 #include <KLocalizedString>
27 
28 #include <QPointer>
29 #include <QLabel>
30 #include <QVBoxLayout>
31 #include <QMimeData>
32 
33 using namespace KSieveUi;
34 SieveTemplateListWidget::SieveTemplateListWidget(const QString &configName, QWidget *parent)
35  : PimCommon::TemplateListWidget(configName, parent)
36 {
37  setKNewStuffConfigFile(QStringLiteral("ksieve_script.knsrc"));
38  loadTemplates();
39  mTemplateManager = new PimCommon::TemplateManager(QStringLiteral("sieve/scripts"), this);
40 }
41 
42 void SieveTemplateListWidget::setSieveCapabilities(const QStringList &capabilities)
43 {
44  mCapabilities = capabilities;
45 }
46 
47 QMimeData *SieveTemplateListWidget::mimeData(const QList<QListWidgetItem *> items) const
48 {
49  if (items.isEmpty()) {
50  return nullptr;
51  }
52  QMimeData *mimeData = new QMimeData();
53  QListWidgetItem *item = items.first();
54  QString templateStr = item->data(TemplateListWidget::Text).toString();
55  if (!mCapabilities.contains(QLatin1String("imap4flags")) && templateStr.contains(QLatin1String("imap4flags"))) {
56  templateStr.replace(QStringLiteral("imap4flags"), QStringLiteral("imapflags"));
57  }
58  mimeData->setText(templateStr);
59  return mimeData;
60 }
61 
62 SieveTemplateListWidget::~SieveTemplateListWidget()
63 {
64 }
65 
66 QVector<PimCommon::defaultTemplate> SieveTemplateListWidget::defaultTemplates()
67 {
68  return KSieveUi::SieveDefaultTemplate::defaultTemplates();
69 }
70 
71 bool SieveTemplateListWidget::addNewTemplate(QString &templateName, QString &templateScript)
72 {
73  QPointer<SieveTemplateEditDialog> dlg = new SieveTemplateEditDialog(this);
74  dlg->setSieveCapabilities(mCapabilities);
75  bool result = false;
76  if (dlg->exec()) {
77  templateName = dlg->templateName();
78  templateScript = dlg->script();
79  result = true;
80  }
81  delete dlg;
82  return result;
83 }
84 
85 bool SieveTemplateListWidget::modifyTemplate(QString &templateName, QString &templateScript, bool defaultTemplate)
86 {
87  QPointer<SieveTemplateEditDialog> dlg = new SieveTemplateEditDialog(this, defaultTemplate);
88  dlg->setTemplateName(templateName);
89  dlg->setScript(templateScript);
90  dlg->setSieveCapabilities(mCapabilities);
91  bool result = false;
92  if (dlg->exec()) {
93  if (!defaultTemplate) {
94  templateName = dlg->templateName();
95  templateScript = dlg->script();
96  }
97  result = true;
98  }
99  delete dlg;
100  return result;
101 }
102 
103 SieveTemplateWidget::SieveTemplateWidget(const QString &title, QWidget *parent)
104  : QWidget(parent)
105 {
106  QVBoxLayout *lay = new QVBoxLayout(this);
107  lay->setContentsMargins(0, 0, 0, 0);
108  QLabel *lab = new QLabel(title, this);
109  lay->addWidget(lab);
110  mListTemplate = new SieveTemplateListWidget(QStringLiteral("sievetemplaterc"), this);
111  mListTemplate->setWhatsThis(i18n("You can drag and drop element on editor to import template"));
112  connect(mListTemplate, &SieveTemplateListWidget::insertTemplate, this, &SieveTemplateWidget::insertTemplate);
113  lay->addWidget(mListTemplate);
114 }
115 
116 SieveTemplateWidget::~SieveTemplateWidget()
117 {
118 }
119 
120 void SieveTemplateWidget::setSieveCapabilities(const QStringList &capabilities)
121 {
122  mListTemplate->setSieveCapabilities(capabilities);
123 }
void setContentsMargins(int left, int top, int right, int bottom)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
bool isEmpty() const const
virtual QVariant data(int role) const const
void setText(const QString &text)
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-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.