File indexing completed on 2024-12-29 04:54:44
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "sieveeditorgraphicalmodewidget.h" 0008 #include "ksieveui/editor/warningwidget/sieveeditorparsingmissingfeaturewarning.h" 0009 #include "scriptsparsing/parsingutil.h" 0010 #include "sievescriptlistbox.h" 0011 #include "sievescriptpage.h" 0012 0013 #include <KConfigGroup> 0014 #include <KLocalizedString> 0015 #include <KMessageBox> 0016 #include <KSharedConfig> 0017 0018 #include "libksieveui_debug.h" 0019 #include <QSplitter> 0020 #include <QStackedWidget> 0021 #include <QVBoxLayout> 0022 0023 using namespace KSieveUi; 0024 namespace 0025 { 0026 static const char myAutoCreateScriptDialogGroupNameModeWidget[] = "AutoCreateScriptDialog"; 0027 } 0028 SieveEditorGraphicalModeWidget::SieveEditorGraphicalModeWidget(QWidget *parent) 0029 : SieveEditorGraphicalModeWidgetAbstract::SieveEditorGraphicalModeWidgetAbstract(parent) 0030 , mSieveScript(new SieveScriptListBox(i18n("Sieve Script"), this)) 0031 , mSplitter(new QSplitter) 0032 { 0033 auto vlay = new QVBoxLayout(this); 0034 vlay->setContentsMargins({}); 0035 0036 mSplitter->setChildrenCollapsible(false); 0037 mSieveScript->setSieveEditorGraphicalModeWidget(this); 0038 connect(mSieveScript, &SieveScriptListBox::valueChanged, this, &SieveEditorGraphicalModeWidget::valueChanged); 0039 connect(mSieveScript, &SieveScriptListBox::addNewPage, this, &SieveEditorGraphicalModeWidget::slotAddScriptPage); 0040 connect(mSieveScript, &SieveScriptListBox::removePage, this, &SieveEditorGraphicalModeWidget::slotRemoveScriptPage); 0041 connect(mSieveScript, &SieveScriptListBox::activatePage, this, &SieveEditorGraphicalModeWidget::slotActivateScriptPage); 0042 connect(mSieveScript, &SieveScriptListBox::enableButtonOk, this, &SieveEditorGraphicalModeWidget::enableButtonOk); 0043 mSplitter->addWidget(mSieveScript); 0044 vlay->addWidget(mSplitter); 0045 0046 mStackWidget = new QStackedWidget; 0047 mSplitter->addWidget(mStackWidget); 0048 0049 mSieveParsingWarning = new SieveEditorParsingMissingFeatureWarning(SieveEditorParsingMissingFeatureWarning::GraphicEditor); 0050 connect(mSieveParsingWarning, &SieveEditorParsingMissingFeatureWarning::switchToTextMode, this, &SieveEditorGraphicalModeWidget::slotSwitchToTextMode); 0051 vlay->addWidget(mSieveParsingWarning); 0052 0053 readConfig(); 0054 } 0055 0056 SieveEditorGraphicalModeWidget::~SieveEditorGraphicalModeWidget() 0057 { 0058 writeConfig(); 0059 } 0060 0061 void SieveEditorGraphicalModeWidget::loadScript(const QString &doc, QString &error) 0062 { 0063 for (int i = mStackWidget->count(); i >= 0; --i) { 0064 mStackWidget->removeWidget(mStackWidget->widget(i)); 0065 } 0066 mSieveScript->loadScript(doc, error); 0067 } 0068 0069 void SieveEditorGraphicalModeWidget::readConfig() 0070 { 0071 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAutoCreateScriptDialogGroupNameModeWidget)); 0072 const QList<int> size{100, 400}; 0073 mSplitter->setSizes(group.readEntry("mainSplitter", size)); 0074 } 0075 0076 void SieveEditorGraphicalModeWidget::writeConfig() 0077 { 0078 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAutoCreateScriptDialogGroupNameModeWidget)); 0079 group.writeEntry("mainSplitter", mSplitter->sizes()); 0080 } 0081 0082 void SieveEditorGraphicalModeWidget::setSieveCapabilities(const QStringList &capabilities) 0083 { 0084 mCapabilities = capabilities; 0085 } 0086 0087 QStringList SieveEditorGraphicalModeWidget::sieveCapabilities() 0088 { 0089 return mCapabilities; 0090 } 0091 0092 QString SieveEditorGraphicalModeWidget::script(QStringList &required) const 0093 { 0094 return mSieveScript->generatedScript(required); 0095 } 0096 0097 void SieveEditorGraphicalModeWidget::slotAddScriptPage(KSieveUi::SieveScriptPage *page) 0098 { 0099 mStackWidget->addWidget(page); 0100 mStackWidget->setCurrentWidget(page); 0101 connect(page, &SieveScriptPage::valueChanged, this, &SieveEditorGraphicalModeWidget::valueChanged); 0102 } 0103 0104 void SieveEditorGraphicalModeWidget::slotRemoveScriptPage(QWidget *page) 0105 { 0106 mStackWidget->removeWidget(page); 0107 } 0108 0109 void SieveEditorGraphicalModeWidget::slotActivateScriptPage(QWidget *page) 0110 { 0111 mStackWidget->setCurrentWidget(page); 0112 } 0113 0114 QString SieveEditorGraphicalModeWidget::currentscript() 0115 { 0116 QStringList requireModules; 0117 QString script = mSieveScript->generatedScript(requireModules); 0118 if (!requireModules.isEmpty()) { 0119 script.prepend(requireModules.join(QLatin1Char('\n')) + QStringLiteral("\n\n")); 0120 } 0121 return script; 0122 } 0123 0124 void SieveEditorGraphicalModeWidget::setImportScript(const QString &script) 0125 { 0126 bool result = false; 0127 const QString doc = KSieveCore::ParsingUtil::parseScript(script, result); 0128 mSieveParsingWarning->animatedHide(); 0129 if (result) { 0130 QString error; 0131 loadScript(doc, error); 0132 if (!error.isEmpty()) { 0133 mSieveParsingWarning->setErrors(script, error); 0134 mSieveParsingWarning->animatedShow(); 0135 } 0136 } else { 0137 const int answer = KMessageBox::questionTwoActions(this, 0138 i18n("Error during importing script. Do you want to switch to text mode?"), 0139 QString(), 0140 KGuiItem(i18nc("@action:button", "Switch to Text Mode")), 0141 KGuiItem(i18nc("@action:button", "Do Not Switch"), QStringLiteral("dialog-cancel"))); 0142 if (answer == KMessageBox::ButtonCode::PrimaryAction) { 0143 Q_EMIT switchTextMode(script); 0144 } 0145 qCDebug(LIBKSIEVEUI_LOG) << " cannot import script"; 0146 } 0147 } 0148 0149 void SieveEditorGraphicalModeWidget::setListOfIncludeFile(const QStringList &listOfIncludeFile) 0150 { 0151 mListOfIncludeFile = listOfIncludeFile; 0152 } 0153 0154 void SieveEditorGraphicalModeWidget::slotSwitchToTextMode() 0155 { 0156 Q_EMIT switchTextMode(mSieveParsingWarning->initialScript()); 0157 } 0158 0159 QStringList KSieveUi::SieveEditorGraphicalModeWidget::listOfIncludeFile() const 0160 { 0161 return mListOfIncludeFile; 0162 } 0163 0164 #include "moc_sieveeditorgraphicalmodewidget.cpp"