Libksieve

sieveeditorgraphicalmodewidget.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 "sieveeditorgraphicalmodewidget.h"
21 #include "autocreatescripts/sievescriptparsingerrordialog.h"
22 #include "ksieveui/editor/warningwidget/sieveeditorparsingmissingfeaturewarning.h"
23 #include "sievescriptlistbox.h"
24 #include "scriptsparsing/parsingutil.h"
25 #include "sievescriptpage.h"
26 
27 #include <KLocalizedString>
28 #include <KSharedConfig>
29 #include <KConfigGroup>
30 #include <KMessageBox>
31 
32 #include <QVBoxLayout>
33 #include <QSplitter>
34 #include <QStackedWidget>
35 #include "libksieve_debug.h"
36 
37 using namespace KSieveUi;
38 
39 SieveEditorGraphicalModeWidget::SieveEditorGraphicalModeWidget(QWidget *parent)
40  : SieveEditorGraphicalModeWidgetAbstract::SieveEditorGraphicalModeWidgetAbstract(parent)
41 {
42  QVBoxLayout *vlay = new QVBoxLayout(this);
43  vlay->setContentsMargins(0, 0, 0, 0);
44 
45  mSplitter = new QSplitter;
46  mSplitter->setChildrenCollapsible(false);
47  mSieveScript = new SieveScriptListBox(i18n("Sieve Script"));
48  mSieveScript->setSieveEditorGraphicalModeWidget(this);
49  connect(mSieveScript, &SieveScriptListBox::valueChanged, this, &SieveEditorGraphicalModeWidget::valueChanged);
50  connect(mSieveScript, &SieveScriptListBox::addNewPage, this, &SieveEditorGraphicalModeWidget::slotAddScriptPage);
51  connect(mSieveScript, &SieveScriptListBox::removePage, this, &SieveEditorGraphicalModeWidget::slotRemoveScriptPage);
52  connect(mSieveScript, &SieveScriptListBox::activatePage, this, &SieveEditorGraphicalModeWidget::slotActivateScriptPage);
53  connect(mSieveScript, &SieveScriptListBox::enableButtonOk, this, &SieveEditorGraphicalModeWidget::enableButtonOk);
54  mSplitter->addWidget(mSieveScript);
55  vlay->addWidget(mSplitter);
56 
57  mStackWidget = new QStackedWidget;
58  mSplitter->addWidget(mStackWidget);
59 
60  mSieveParsingWarning = new SieveEditorParsingMissingFeatureWarning(SieveEditorParsingMissingFeatureWarning::GraphicEditor);
61  connect(mSieveParsingWarning, &SieveEditorParsingMissingFeatureWarning::switchToTextMode, this, &SieveEditorGraphicalModeWidget::slotSwitchToTextMode);
62  vlay->addWidget(mSieveParsingWarning);
63 
64  readConfig();
65 }
66 
67 SieveEditorGraphicalModeWidget::~SieveEditorGraphicalModeWidget()
68 {
69  writeConfig();
70 }
71 
72 void SieveEditorGraphicalModeWidget::loadScript(const QString &doc, QString &error)
73 {
74  for (int i = mStackWidget->count(); i >= 0; --i) {
75  mStackWidget->removeWidget(mStackWidget->widget(i));
76  }
77  mSieveScript->loadScript(doc, error);
78 }
79 
80 void SieveEditorGraphicalModeWidget::readConfig()
81 {
82  KConfigGroup group(KSharedConfig::openConfig(), "AutoCreateScriptDialog");
83  const QList<int> size { 100, 400 };
84  mSplitter->setSizes(group.readEntry("mainSplitter", size));
85 }
86 
87 void SieveEditorGraphicalModeWidget::writeConfig()
88 {
89  KConfigGroup group(KSharedConfig::openConfig(), "AutoCreateScriptDialog");
90  group.writeEntry("mainSplitter", mSplitter->sizes());
91 }
92 
93 void SieveEditorGraphicalModeWidget::setSieveCapabilities(const QStringList &capabilities)
94 {
95  mCapabilities = capabilities;
96 }
97 
98 QStringList SieveEditorGraphicalModeWidget::sieveCapabilities()
99 {
100  return mCapabilities;
101 }
102 
103 QString SieveEditorGraphicalModeWidget::script(QStringList &required) const
104 {
105  return mSieveScript->generatedScript(required);
106 }
107 
108 void SieveEditorGraphicalModeWidget::slotAddScriptPage(KSieveUi::SieveScriptPage *page)
109 {
110  mStackWidget->addWidget(page);
111  mStackWidget->setCurrentWidget(page);
112  connect(page, &SieveScriptPage::valueChanged, this, &SieveEditorGraphicalModeWidget::valueChanged);
113 }
114 
115 void SieveEditorGraphicalModeWidget::slotRemoveScriptPage(QWidget *page)
116 {
117  mStackWidget->removeWidget(page);
118 }
119 
120 void SieveEditorGraphicalModeWidget::slotActivateScriptPage(QWidget *page)
121 {
122  mStackWidget->setCurrentWidget(page);
123 }
124 
125 QString SieveEditorGraphicalModeWidget::currentscript()
126 {
127  QStringList requireModules;
128  QString script = mSieveScript->generatedScript(requireModules);
129  if (!requireModules.isEmpty()) {
130  script.prepend(requireModules.join(QLatin1Char('\n')) + QStringLiteral("\n\n"));
131  }
132  return script;
133 }
134 
135 void SieveEditorGraphicalModeWidget::setImportScript(const QString &script)
136 {
137  bool result = false;
138  const QString doc = ParsingUtil::parseScript(script, result);
139  mSieveParsingWarning->animatedHide();
140  if (result) {
141  QString error;
142  loadScript(doc, error);
143  if (!error.isEmpty()) {
144  mSieveParsingWarning->setErrors(script, error);
145  mSieveParsingWarning->animatedShow();
146  }
147  } else {
148  if (KMessageBox::Yes == KMessageBox::questionYesNo(this, i18n("Error during importing script. Do you want to switch to text mode?"))) {
149  Q_EMIT switchTextMode(script);
150  }
151  qCDebug(LIBKSIEVE_LOG) << " cannot import script";
152  }
153 }
154 
155 void SieveEditorGraphicalModeWidget::setListOfIncludeFile(const QStringList &listOfIncludeFile)
156 {
157  mListOfIncludeFile = listOfIncludeFile;
158 }
159 
160 void SieveEditorGraphicalModeWidget::slotSwitchToTextMode()
161 {
162  Q_EMIT switchTextMode(mSieveParsingWarning->initialScript());
163 }
164 
165 QStringList KSieveUi::SieveEditorGraphicalModeWidget::listOfIncludeFile() const
166 {
167  return mListOfIncludeFile;
168 }
void setContentsMargins(int left, int top, int right, int bottom)
QString & prepend(QChar ch)
QString join(const QString &separator) const const
QSize size() const const
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setChildrenCollapsible(bool)
bool isEmpty() const const
bool isEmpty() const const
int result() const const
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
int addWidget(QWidget *widget)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:09:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.