Libksieve

sieveeditorgraphicalmodewidget.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 "sieveeditorgraphicalmodewidget.h"
8#include "ksieveui/editor/warningwidget/sieveeditorparsingmissingfeaturewarning.h"
9#include "scriptsparsing/parsingutil.h"
10#include "sievescriptlistbox.h"
11#include "sievescriptpage.h"
12
13#include <KConfigGroup>
14#include <KLocalizedString>
15#include <KMessageBox>
16#include <KSharedConfig>
17
18#include "libksieveui_debug.h"
19#include <QSplitter>
20#include <QStackedWidget>
21#include <QVBoxLayout>
22
23using namespace KSieveUi;
24namespace
25{
26static const char myAutoCreateScriptDialogGroupNameModeWidget[] = "AutoCreateScriptDialog";
27}
28SieveEditorGraphicalModeWidget::SieveEditorGraphicalModeWidget(QWidget *parent)
29 : SieveEditorGraphicalModeWidgetAbstract::SieveEditorGraphicalModeWidgetAbstract(parent)
30 , mSieveScript(new SieveScriptListBox(i18n("Sieve Script"), this))
31 , mSplitter(new QSplitter)
32{
33 auto vlay = new QVBoxLayout(this);
34 vlay->setContentsMargins({});
35
36 mSplitter->setChildrenCollapsible(false);
37 mSieveScript->setSieveEditorGraphicalModeWidget(this);
38 connect(mSieveScript, &SieveScriptListBox::valueChanged, this, &SieveEditorGraphicalModeWidget::valueChanged);
39 connect(mSieveScript, &SieveScriptListBox::addNewPage, this, &SieveEditorGraphicalModeWidget::slotAddScriptPage);
40 connect(mSieveScript, &SieveScriptListBox::removePage, this, &SieveEditorGraphicalModeWidget::slotRemoveScriptPage);
41 connect(mSieveScript, &SieveScriptListBox::activatePage, this, &SieveEditorGraphicalModeWidget::slotActivateScriptPage);
42 connect(mSieveScript, &SieveScriptListBox::enableButtonOk, this, &SieveEditorGraphicalModeWidget::enableButtonOk);
43 mSplitter->addWidget(mSieveScript);
44 vlay->addWidget(mSplitter);
45
46 mStackWidget = new QStackedWidget;
47 mSplitter->addWidget(mStackWidget);
48
49 mSieveParsingWarning = new SieveEditorParsingMissingFeatureWarning(SieveEditorParsingMissingFeatureWarning::GraphicEditor);
50 connect(mSieveParsingWarning, &SieveEditorParsingMissingFeatureWarning::switchToTextMode, this, &SieveEditorGraphicalModeWidget::slotSwitchToTextMode);
51 vlay->addWidget(mSieveParsingWarning);
52
53 readConfig();
54}
55
56SieveEditorGraphicalModeWidget::~SieveEditorGraphicalModeWidget()
57{
58 writeConfig();
59}
60
61void SieveEditorGraphicalModeWidget::loadScript(const QString &doc, QString &error)
62{
63 for (int i = mStackWidget->count(); i >= 0; --i) {
64 mStackWidget->removeWidget(mStackWidget->widget(i));
65 }
66 mSieveScript->loadScript(doc, error);
67}
68
69void SieveEditorGraphicalModeWidget::readConfig()
70{
71 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAutoCreateScriptDialogGroupNameModeWidget));
72 const QList<int> size{100, 400};
73 mSplitter->setSizes(group.readEntry("mainSplitter", size));
74}
75
76void SieveEditorGraphicalModeWidget::writeConfig()
77{
78 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAutoCreateScriptDialogGroupNameModeWidget));
79 group.writeEntry("mainSplitter", mSplitter->sizes());
80}
81
82void SieveEditorGraphicalModeWidget::setSieveCapabilities(const QStringList &capabilities)
83{
84 mCapabilities = capabilities;
85}
86
87QStringList SieveEditorGraphicalModeWidget::sieveCapabilities()
88{
89 return mCapabilities;
90}
91
92QString SieveEditorGraphicalModeWidget::script(QStringList &required) const
93{
94 return mSieveScript->generatedScript(required);
95}
96
97void SieveEditorGraphicalModeWidget::slotAddScriptPage(KSieveUi::SieveScriptPage *page)
98{
99 mStackWidget->addWidget(page);
100 mStackWidget->setCurrentWidget(page);
101 connect(page, &SieveScriptPage::valueChanged, this, &SieveEditorGraphicalModeWidget::valueChanged);
102}
103
104void SieveEditorGraphicalModeWidget::slotRemoveScriptPage(QWidget *page)
105{
106 mStackWidget->removeWidget(page);
107}
108
109void SieveEditorGraphicalModeWidget::slotActivateScriptPage(QWidget *page)
110{
111 mStackWidget->setCurrentWidget(page);
112}
113
114QString SieveEditorGraphicalModeWidget::currentscript()
115{
116 QStringList requireModules;
117 QString script = mSieveScript->generatedScript(requireModules);
118 if (!requireModules.isEmpty()) {
119 script.prepend(requireModules.join(QLatin1Char('\n')) + QStringLiteral("\n\n"));
120 }
121 return script;
122}
123
124void SieveEditorGraphicalModeWidget::setImportScript(const QString &script)
125{
126 bool result = false;
127 const QString doc = KSieveCore::ParsingUtil::parseScript(script, result);
128 mSieveParsingWarning->animatedHide();
129 if (result) {
131 loadScript(doc, error);
132 if (!error.isEmpty()) {
133 mSieveParsingWarning->setErrors(script, error);
134 mSieveParsingWarning->animatedShow();
135 }
136 } else {
137 const int answer = KMessageBox::questionTwoActions(this,
138 i18n("Error during importing script. Do you want to switch to text mode?"),
139 QString(),
140 KGuiItem(i18nc("@action:button", "Switch to Text Mode")),
141 KGuiItem(i18nc("@action:button", "Do Not Switch"), QStringLiteral("dialog-cancel")));
142 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
143 Q_EMIT switchTextMode(script);
144 }
145 qCDebug(LIBKSIEVEUI_LOG) << " cannot import script";
146 }
147}
148
149void SieveEditorGraphicalModeWidget::setListOfIncludeFile(const QStringList &listOfIncludeFile)
150{
151 mListOfIncludeFile = listOfIncludeFile;
152}
153
154void SieveEditorGraphicalModeWidget::slotSwitchToTextMode()
155{
156 Q_EMIT switchTextMode(mSieveParsingWarning->initialScript());
157}
158
159QStringList KSieveUi::SieveEditorGraphicalModeWidget::listOfIncludeFile() const
160{
161 return mListOfIncludeFile;
162}
163
164#include "moc_sieveeditorgraphicalmodewidget.cpp"
void animatedHide()
void animatedShow()
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Capabilities capabilities()
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setSizes(const QList< int > &list)
QList< int > sizes() const const
int addWidget(QWidget *widget)
void removeWidget(QWidget *widget)
void setCurrentWidget(QWidget *widget)
QWidget * widget(int index) const const
QString & prepend(QChar ch)
QString join(QChar separator) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.