File indexing completed on 2024-12-29 04:54:48

0001 /* SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0002  *
0003  * SPDX-License-Identifier: LGPL-2.0-or-later
0004  */
0005 
0006 #include "sieveeditortextmodewidget.h"
0007 #include "autocreatescripts/autocreatescriptdialog.h"
0008 #include "editor/sieveeditortabwidget.h"
0009 #include "editor/sievetextedit.h"
0010 #include "editor/warningwidget/sieveeditorparsingmissingfeaturewarning.h"
0011 #include "editor/warningwidget/sieveeditorwarning.h"
0012 #include "sievescriptdebugger/sievescriptdebuggerdialog.h"
0013 #include "templates/sievetemplatewidget.h"
0014 
0015 #include "scriptsparsing/parsingresultdialog.h"
0016 #include "scriptsparsing/xmlprintingscriptbuilder.h"
0017 
0018 #include "KSplitterCollapserButton"
0019 #include "webengine/sieveeditorhelphtmlwidget.h"
0020 #include <PimCommon/PurposeMenuMessageWidget>
0021 #include <TextAddonsWidgets/SlideContainer>
0022 #include <TextCustomEditor/PlainTextEditFindBar>
0023 #include <TextCustomEditor/PlainTextEditorWidget>
0024 #include <TextCustomEditor/TextGotoLineWidget>
0025 #ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
0026 #include <TextEditTextToSpeech/TextToSpeechContainerWidget>
0027 #endif
0028 
0029 #include "error.h"
0030 #include "parser.h"
0031 #include "scriptbuilder.h"
0032 
0033 #include <KConfigGroup>
0034 #include <KLocalizedString>
0035 #include <KMessageBox>
0036 #include <KSharedConfig>
0037 #include <KWindowStateSaver>
0038 
0039 #include <QPointer>
0040 #include <QPrintDialog>
0041 #include <QPrintPreviewDialog>
0042 #include <QPrinter>
0043 #include <QPushButton>
0044 #include <QShortcut>
0045 #include <QSplitter>
0046 #include <QVBoxLayout>
0047 
0048 #include <cerrno>
0049 #include <memory>
0050 
0051 using namespace KSieveUi;
0052 
0053 SieveEditorTextModeWidget::SieveEditorTextModeWidget(QWidget *parent)
0054     : SieveEditorAbstractWidget(parent)
0055     , mPurposeMenuMessageWidget(new PimCommon::PurposeMenuMessageWidget(this))
0056 {
0057     auto lay = new QVBoxLayout(this);
0058     lay->setContentsMargins({});
0059 
0060     mMainSplitter = new QSplitter(this);
0061     mMainSplitter->setOrientation(Qt::Vertical);
0062     lay->addWidget(mMainSplitter);
0063 
0064     mTemplateSplitter = new QSplitter;
0065     mTemplateSplitter->setOrientation(Qt::Horizontal);
0066     //
0067     mSieveTemplateWidget = new SieveTemplateWidget(i18n("Sieve Template:"));
0068 
0069     auto textEditWidget = new QWidget(this);
0070     auto textEditLayout = new QVBoxLayout;
0071     textEditLayout->setContentsMargins({});
0072 
0073     mEditorWidget = new QWidget(this);
0074     auto editorWidgetLayout = new QVBoxLayout;
0075     editorWidgetLayout->setContentsMargins({});
0076     editorWidgetLayout->setSpacing(0);
0077     mEditorWidget->setLayout(editorWidgetLayout);
0078 
0079     mTabWidget = new SieveEditorTabWidget(this);
0080     connect(mTabWidget, &SieveEditorTabWidget::currentChanged, this, &SieveEditorTextModeWidget::sieveEditorTabCurrentChanged);
0081     connect(mTabWidget, &SieveEditorTabWidget::copyAvailable, this, &SieveEditorTextModeWidget::copyAvailable);
0082 #ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
0083     mTextToSpeechWidget = new TextEditTextToSpeech::TextToSpeechContainerWidget(this);
0084     editorWidgetLayout->addWidget(mTextToSpeechWidget);
0085 #endif
0086 
0087     mTextEdit = new SieveTextEdit(this);
0088     mPurposeMenuMessageWidget->setPosition(KMessageWidget::Header);
0089     editorWidgetLayout->addWidget(mPurposeMenuMessageWidget);
0090     editorWidgetLayout->addWidget(mTextEdit);
0091     connect(mTextEdit, &SieveTextEdit::textChanged, this, &SieveEditorTextModeWidget::valueChanged);
0092     mTabWidget->addTab(mEditorWidget, i18n("Editor"));
0093     mTabWidget->tabBar()->hide();
0094     textEditLayout->addWidget(mTabWidget);
0095     connect(mTextEdit, &SieveTextEdit::openHelp, mTabWidget, &SieveEditorTabWidget::slotAddHelpPage);
0096 #ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
0097     connect(mTextEdit, &SieveTextEdit::say, mTextToSpeechWidget, &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
0098 #endif
0099     connect(mTextEdit, &SieveTextEdit::editRule, this, &SieveEditorTextModeWidget::slotEditRule);
0100     connect(mTextEdit, &SieveTextEdit::insertRule, this, &SieveEditorTextModeWidget::slotInsertRule);
0101 
0102     mGotoLineSliderContainer = new TextAddonsWidgets::SlideContainer(this);
0103     mGoToLine = new TextCustomEditor::TextGoToLineWidget(this);
0104     mGoToLine->hide();
0105     mGotoLineSliderContainer->setContent(mGoToLine);
0106     editorWidgetLayout->addWidget(mGotoLineSliderContainer);
0107     connect(mGoToLine, &TextCustomEditor::TextGoToLineWidget::hideGotoLine, mGotoLineSliderContainer, &TextAddonsWidgets::SlideContainer::slideOut);
0108 
0109     connect(mGoToLine, &TextCustomEditor::TextGoToLineWidget::moveToLine, this, &SieveEditorTextModeWidget::slotGoToLine);
0110     connect(mTextEdit, &SieveTextEdit::blockCountChanged, mGoToLine, &TextCustomEditor::TextGoToLineWidget::slotBlockCountChanged);
0111 
0112     mSliderContainer = new TextAddonsWidgets::SlideContainer(this);
0113     mFindBar = new TextCustomEditor::PlainTextEditFindBar(mTextEdit, textEditWidget);
0114     mFindBar->setHideWhenClose(false);
0115     connect(mFindBar, &TextCustomEditor::TextEditFindBarBase::hideFindBar, mSliderContainer, &TextAddonsWidgets::SlideContainer::slideOut);
0116     connect(mFindBar,
0117             &TextCustomEditor::TextEditFindBarBase::displayMessageIndicator,
0118             mTextEdit,
0119             &TextCustomEditor::PlainTextEditor::slotDisplayMessageIndicator);
0120     mSliderContainer->setContent(mFindBar);
0121     editorWidgetLayout->addWidget(mSliderContainer);
0122 
0123     mSieveEditorWarning = new SieveEditorWarning(this);
0124     editorWidgetLayout->addWidget(mSieveEditorWarning);
0125 
0126     mSieveParsingWarning = new SieveEditorParsingMissingFeatureWarning(SieveEditorParsingMissingFeatureWarning::TextEditor);
0127     connect(mSieveParsingWarning, &SieveEditorParsingMissingFeatureWarning::switchToGraphicalMode, this, &SieveEditorTextModeWidget::switchToGraphicalMode);
0128     editorWidgetLayout->addWidget(mSieveParsingWarning);
0129 
0130     textEditWidget->setLayout(textEditLayout);
0131 
0132     mTemplateSplitter->addWidget(textEditWidget);
0133     mTemplateSplitter->addWidget(mSieveTemplateWidget);
0134     mTemplateSplitter->setCollapsible(0, false);
0135     new KSplitterCollapserButton(mSieveTemplateWidget, mTemplateSplitter);
0136 
0137     connect(mSieveTemplateWidget, &SieveTemplateWidget::insertTemplate, mTextEdit, &SieveTextEdit::insertPlainText);
0138 
0139     //
0140     connect(mTextEdit, &SieveTextEdit::findText, this, &SieveEditorTextModeWidget::slotFind);
0141     connect(mTextEdit, &SieveTextEdit::replaceText, this, &SieveEditorTextModeWidget::slotReplace);
0142 
0143     mDebugTextEdit = new TextCustomEditor::PlainTextEditorWidget(this);
0144     mDebugTextEdit->editor()->setSearchSupport(false);
0145     mDebugTextEdit->editor()->setReadOnly(true);
0146     mDebugTextEdit->editor()->setPlaceholderText(i18n("Show result from \"check syntax\""));
0147     mMainSplitter->addWidget(mTemplateSplitter);
0148     mMainSplitter->addWidget(mDebugTextEdit);
0149     mMainSplitter->setChildrenCollapsible(false);
0150 
0151     connect(mTextEdit, &SieveTextEdit::textChanged, this, &SieveEditorTextModeWidget::slotTextChanged);
0152     connect(mTextEdit, &SieveTextEdit::undoAvailable, this, &SieveEditorTextModeWidget::undoAvailable);
0153     connect(mTextEdit, &SieveTextEdit::redoAvailable, this, &SieveEditorTextModeWidget::redoAvailable);
0154     connect(mTextEdit, &SieveTextEdit::copyAvailable, this, &SieveEditorTextModeWidget::copyAvailable);
0155     readConfig();
0156 
0157     mTextEdit->setFocus();
0158 }
0159 
0160 SieveEditorTextModeWidget::~SieveEditorTextModeWidget()
0161 {
0162     disconnect(mTextEdit, &SieveTextEdit::textChanged, this, &SieveEditorTextModeWidget::slotTextChanged);
0163     disconnect(mTextEdit, &SieveTextEdit::textChanged, this, &SieveEditorTextModeWidget::valueChanged);
0164     writeConfig();
0165 }
0166 
0167 void SieveEditorTextModeWidget::writeConfig()
0168 {
0169     KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("SieveEditor"));
0170     group.writeEntry("mainSplitter", mMainSplitter->sizes());
0171     group.writeEntry("templateSplitter", mTemplateSplitter->sizes());
0172 }
0173 
0174 QStringList SieveEditorTextModeWidget::sieveCapabilities() const
0175 {
0176     return mSieveCapabilities;
0177 }
0178 
0179 SieveEditorTabWidget *SieveEditorTextModeWidget::tabWidget() const
0180 {
0181     return mTabWidget;
0182 }
0183 
0184 void SieveEditorTextModeWidget::readConfig()
0185 {
0186     KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("SieveEditor"));
0187     const QList<int> size = {400, 100};
0188 
0189     mMainSplitter->setSizes(group.readEntry("mainSplitter", size));
0190     mTemplateSplitter->setSizes(group.readEntry("templateSplitter", size));
0191 }
0192 
0193 void SieveEditorTextModeWidget::slotGoToLine(int line)
0194 {
0195     if (line > 0) {
0196         QTextCursor cursor = mTextEdit->textCursor();
0197         cursor.beginEditBlock();
0198         cursor.movePosition(QTextCursor::Start);
0199         cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, (line - 1));
0200         cursor.endEditBlock();
0201         mTextEdit->setTextCursor(cursor);
0202         mTextEdit->setFocus();
0203     }
0204 }
0205 
0206 void SieveEditorTextModeWidget::slotShowGoToLine()
0207 {
0208     mGoToLine->setMaximumLineCount(mTextEdit->document()->blockCount());
0209     mGotoLineSliderContainer->slideIn();
0210     mGoToLine->goToLine();
0211 }
0212 
0213 void SieveEditorTextModeWidget::slotShareError(const QString &message)
0214 {
0215     mPurposeMenuMessageWidget->slotShareError(message);
0216 }
0217 
0218 void SieveEditorTextModeWidget::slotShareSuccess(const QString &url)
0219 {
0220     mPurposeMenuMessageWidget->slotShareSuccess(url);
0221 }
0222 
0223 void SieveEditorTextModeWidget::generateXml()
0224 {
0225     const QByteArray script = mTextEdit->toPlainText().toUtf8();
0226     KSieve::Parser parser(script.begin(), script.begin() + script.length());
0227     KSieveCore::XMLPrintingScriptBuilder psb(2);
0228     parser.setScriptBuilder(&psb);
0229     const bool result = parser.parse();
0230     QPointer<ParsingResultDialog> dlg = new ParsingResultDialog(this);
0231     if (result) {
0232         dlg->setResultParsing(psb.result());
0233     } else {
0234         dlg->setResultParsing(i18n("Error during parsing"));
0235     }
0236     dlg->exec();
0237     delete dlg;
0238 }
0239 
0240 void SieveEditorTextModeWidget::slotEditRule(const QString &selectedText)
0241 {
0242     const QByteArray script = selectedText.toUtf8();
0243     KSieve::Parser parser(script.begin(), script.begin() + script.length());
0244     KSieveCore::XMLPrintingScriptBuilder psb(2);
0245     parser.setScriptBuilder(&psb);
0246     const bool result = parser.parse();
0247     if (result) {
0248         QPointer<AutoCreateScriptDialog> dlg = new AutoCreateScriptDialog(this);
0249         dlg->setSieveCapabilities(mSieveCapabilities);
0250         dlg->setSieveImapAccountSettings(mSieveImapAccountSettings);
0251         dlg->setListOfIncludeFile(mListOfIncludeFile);
0252         QString error;
0253         // qDebug() << " psb.result()" << psb.result();
0254         dlg->loadScript(psb.result(), error);
0255         if (dlg->exec()) {
0256             QStringList requireModules;
0257             const QString newScript = dlg->script(requireModules);
0258             const QStringList needToAddRequire = insertNecessaryRequires(requireModules);
0259             mTextEdit->insertPlainText(newScript);
0260             insertRequires(needToAddRequire);
0261         }
0262         delete dlg;
0263     } else {
0264         KMessageBox::error(this, i18n("Selected text is not a full sieve script"), i18nc("@title:window", "Parsing error"));
0265     }
0266 }
0267 
0268 void SieveEditorTextModeWidget::insertRequires(const QStringList &needToAddRequire)
0269 {
0270     if (!needToAddRequire.isEmpty()) {
0271         QTextCursor textCursor = mTextEdit->textCursor();
0272         textCursor.movePosition(QTextCursor::MoveOperation::Start);
0273         textCursor.insertText(needToAddRequire.join(QLatin1Char('\n')) + QLatin1Char('\n'));
0274     }
0275 }
0276 
0277 QStringList SieveEditorTextModeWidget::insertNecessaryRequires(const QStringList &requireModules)
0278 {
0279     QStringList needToAddRequire;
0280     const QString plainText = mTextEdit->toPlainText();
0281     for (const QString &module : std::as_const(requireModules)) {
0282         if (!plainText.contains(module)) {
0283             needToAddRequire.append(module);
0284         }
0285     }
0286     return needToAddRequire;
0287 }
0288 
0289 void SieveEditorTextModeWidget::slotInsertRule()
0290 {
0291     QPointer<AutoCreateScriptDialog> dlg = new AutoCreateScriptDialog(this);
0292     dlg->setSieveCapabilities(mSieveCapabilities);
0293     dlg->setSieveImapAccountSettings(mSieveImapAccountSettings);
0294     dlg->setListOfIncludeFile(mListOfIncludeFile);
0295     if (dlg->exec()) {
0296         QStringList requireModules;
0297         const QString newScript = dlg->script(requireModules);
0298         const QStringList needToAddRequire = insertNecessaryRequires(requireModules);
0299         mTextEdit->insertPlainText(newScript);
0300         insertRequires(needToAddRequire);
0301     }
0302     delete dlg;
0303 }
0304 
0305 void SieveEditorTextModeWidget::createRulesGraphically()
0306 {
0307     QPointer<AutoCreateScriptDialog> dlg = new AutoCreateScriptDialog(this);
0308     dlg->setSieveCapabilities(mSieveCapabilities);
0309     dlg->setSieveImapAccountSettings(mSieveImapAccountSettings);
0310     dlg->setListOfIncludeFile(mListOfIncludeFile);
0311     if (dlg->exec()) {
0312         QStringList requireModules;
0313         const QString script = dlg->script(requireModules);
0314         const QStringList needToAddRequire = insertNecessaryRequires(requireModules);
0315         QString newPlainText = mTextEdit->toPlainText() + script;
0316         if (!needToAddRequire.isEmpty()) {
0317             newPlainText.prepend(requireModules.join(QLatin1Char('\n')) + QLatin1Char('\n'));
0318         }
0319         mTextEdit->selectAll();
0320 
0321         mTextEdit->insertPlainText(newPlainText);
0322     }
0323     delete dlg;
0324 }
0325 
0326 void SieveEditorTextModeWidget::find()
0327 {
0328     slotFind();
0329 }
0330 
0331 void SieveEditorTextModeWidget::replace()
0332 {
0333     slotReplace();
0334 }
0335 
0336 void SieveEditorTextModeWidget::undo()
0337 {
0338     const QWidget *w = mTabWidget->currentWidget();
0339     if (w == mEditorWidget) {
0340         mTextEdit->undo();
0341     }
0342 }
0343 
0344 void SieveEditorTextModeWidget::redo()
0345 {
0346     const QWidget *w = mTabWidget->currentWidget();
0347     if (w == mEditorWidget) {
0348         mTextEdit->redo();
0349     }
0350 }
0351 
0352 void SieveEditorTextModeWidget::paste()
0353 {
0354     const QWidget *w = mTabWidget->currentWidget();
0355     if (w == mEditorWidget) {
0356         mTextEdit->paste();
0357     }
0358 }
0359 
0360 void SieveEditorTextModeWidget::cut()
0361 {
0362     const QWidget *w = mTabWidget->currentWidget();
0363     if (w == mEditorWidget) {
0364         mTextEdit->cut();
0365     }
0366 }
0367 
0368 void SieveEditorTextModeWidget::copy()
0369 {
0370     QWidget *w = mTabWidget->currentWidget();
0371     if (w == mEditorWidget) {
0372         mTextEdit->copy();
0373     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0374         page->copy();
0375     }
0376 }
0377 
0378 void SieveEditorTextModeWidget::selectAll()
0379 {
0380     QWidget *w = mTabWidget->currentWidget();
0381     if (w == mEditorWidget) {
0382         mTextEdit->selectAll();
0383     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0384         page->selectAll();
0385     }
0386 }
0387 
0388 bool SieveEditorTextModeWidget::isUndoAvailable() const
0389 {
0390     const QWidget *w = mTabWidget->currentWidget();
0391     if (w == mEditorWidget) {
0392         return mTextEdit->document()->isUndoAvailable();
0393     }
0394     return false;
0395 }
0396 
0397 bool SieveEditorTextModeWidget::isRedoAvailable() const
0398 {
0399     const QWidget *w = mTabWidget->currentWidget();
0400     if (w == mEditorWidget) {
0401         return mTextEdit->document()->isRedoAvailable();
0402     }
0403     return false;
0404 }
0405 
0406 bool SieveEditorTextModeWidget::hasSelection() const
0407 {
0408     QWidget *w = mTabWidget->currentWidget();
0409     if (w == mEditorWidget) {
0410         return mTextEdit->textCursor().hasSelection();
0411     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0412         return page->hasSelection();
0413     }
0414     return false;
0415 }
0416 
0417 void SieveEditorTextModeWidget::zoomIn()
0418 {
0419     QWidget *w = mTabWidget->currentWidget();
0420     if (w == mEditorWidget) {
0421         mTextEdit->zoomIn();
0422     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0423         page->zoomIn();
0424     }
0425 }
0426 
0427 void SieveEditorTextModeWidget::zoomOut()
0428 {
0429     QWidget *w = mTabWidget->currentWidget();
0430     if (w == mEditorWidget) {
0431         mTextEdit->zoomOut();
0432     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0433         page->zoomOut();
0434     }
0435 }
0436 
0437 bool SieveEditorTextModeWidget::isWordWrap() const
0438 {
0439     return mTextEdit->isWordWrap();
0440 }
0441 
0442 void SieveEditorTextModeWidget::print()
0443 {
0444     const QWidget *w = mTabWidget->currentWidget();
0445     if (w == mEditorWidget) {
0446         QPrinter printer;
0447 
0448         std::unique_ptr<QPrintDialog> dlg(new QPrintDialog(&printer));
0449 
0450         bool restoreSpellCheck = mTextEdit->checkSpellingEnabled();
0451         mTextEdit->setCheckSpellingEnabled(false);
0452         if (dlg && dlg->exec() == QDialog::Accepted) {
0453             mTextEdit->print(&printer);
0454         }
0455         mTextEdit->setCheckSpellingEnabled(restoreSpellCheck);
0456     }
0457 }
0458 
0459 void SieveEditorTextModeWidget::printPreview()
0460 {
0461     const QWidget *w = mTabWidget->currentWidget();
0462     if (w == mEditorWidget) {
0463         bool restoreSpellCheck = mTextEdit->checkSpellingEnabled();
0464         mTextEdit->setCheckSpellingEnabled(false);
0465         QPointer<QPrintPreviewDialog> previewdlg = new QPrintPreviewDialog(this);
0466         new KWindowStateSaver(previewdlg.data(), QLatin1StringView("SieveEditorPrintPreviewDialog"));
0467         connect(previewdlg.data(), &QPrintPreviewDialog::paintRequested, this, [this](QPrinter *printer) {
0468             mTextEdit->print(printer);
0469         });
0470         previewdlg->exec();
0471         delete previewdlg;
0472         mTextEdit->setCheckSpellingEnabled(restoreSpellCheck);
0473     }
0474 }
0475 
0476 void SieveEditorTextModeWidget::setWordWrap(bool state)
0477 {
0478     mTextEdit->setWordWrap(state);
0479 }
0480 
0481 void SieveEditorTextModeWidget::zoomReset()
0482 {
0483     QWidget *w = mTabWidget->currentWidget();
0484     if (w == mEditorWidget) {
0485         mTextEdit->slotZoomReset();
0486     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0487         page->resetZoom();
0488     }
0489 }
0490 
0491 void SieveEditorTextModeWidget::slotFind()
0492 {
0493     QWidget *w = mTabWidget->currentWidget();
0494     if (w == mEditorWidget) {
0495         if (mTextEdit->textCursor().hasSelection()) {
0496             mFindBar->setText(mTextEdit->textCursor().selectedText());
0497         }
0498         mTextEdit->moveCursor(QTextCursor::Start);
0499         mFindBar->showFind();
0500         mSliderContainer->slideIn();
0501         mFindBar->focusAndSetCursor();
0502     } else if (auto page = qobject_cast<SieveEditorHelpHtmlWidget *>(w)) {
0503         page->find();
0504     }
0505 }
0506 
0507 void SieveEditorTextModeWidget::slotReplace()
0508 {
0509     if (mTextEdit->textCursor().hasSelection()) {
0510         mFindBar->setText(mTextEdit->textCursor().selectedText());
0511     }
0512     mFindBar->showReplace();
0513     mSliderContainer->slideIn();
0514     mFindBar->focusAndSetCursor();
0515 }
0516 
0517 QString SieveEditorTextModeWidget::currentscript()
0518 {
0519     return mTextEdit->toPlainText();
0520 }
0521 
0522 void SieveEditorTextModeWidget::setImportScript(const QString &script)
0523 {
0524     mTextEdit->selectAll();
0525     mTextEdit->insertPlainText(script);
0526 }
0527 
0528 void SieveEditorTextModeWidget::slotTextChanged()
0529 {
0530     const bool enabled = !script().isEmpty();
0531     Q_EMIT enableButtonOk(enabled);
0532 }
0533 
0534 QString SieveEditorTextModeWidget::script() const
0535 {
0536     return mTextEdit->toPlainText();
0537 }
0538 
0539 void SieveEditorTextModeWidget::setScript(const QString &script, bool clearUndoRedo)
0540 {
0541     if (clearUndoRedo) {
0542         mTextEdit->setPlainText(script);
0543     } else {
0544         mTextEdit->selectAll();
0545         mTextEdit->insertPlainText(script);
0546     }
0547 }
0548 
0549 void SieveEditorTextModeWidget::setDebugScript(const QString &debug)
0550 {
0551     mDebugTextEdit->editor()->clear();
0552     mDebugTextEdit->editor()->appendHtml(debug);
0553 }
0554 
0555 void SieveEditorTextModeWidget::setListOfIncludeFile(const QStringList &listOfIncludeFile)
0556 {
0557     mListOfIncludeFile = listOfIncludeFile;
0558 }
0559 
0560 void SieveEditorTextModeWidget::setSieveCapabilities(const QStringList &capabilities)
0561 {
0562     mSieveCapabilities = capabilities;
0563     mTextEdit->setSieveCapabilities(mSieveCapabilities);
0564     mSieveTemplateWidget->setSieveCapabilities(mSieveCapabilities);
0565 }
0566 
0567 void SieveEditorTextModeWidget::showEditorWarning()
0568 {
0569     mSieveEditorWarning->animatedShow();
0570 }
0571 
0572 void SieveEditorTextModeWidget::hideEditorWarning()
0573 {
0574     mSieveEditorWarning->animatedHide();
0575     mSieveParsingWarning->animatedHide();
0576 }
0577 
0578 void SieveEditorTextModeWidget::showParsingEditorWarning()
0579 {
0580     mSieveParsingWarning->animatedShow();
0581 }
0582 
0583 void SieveEditorTextModeWidget::setParsingEditorWarningError(const QString &script, const QString &error)
0584 {
0585     mSieveParsingWarning->setErrors(script, error);
0586 }
0587 
0588 void SieveEditorTextModeWidget::checkSpelling()
0589 {
0590     mTextEdit->slotCheckSpelling();
0591 }
0592 
0593 void SieveEditorTextModeWidget::comment()
0594 {
0595     mTextEdit->comment();
0596 }
0597 
0598 void SieveEditorTextModeWidget::uncomment()
0599 {
0600     mTextEdit->uncomment();
0601 }
0602 
0603 void SieveEditorTextModeWidget::setReadOnly(bool b)
0604 {
0605     mTextEdit->setReadOnly(b);
0606 }
0607 
0608 void SieveEditorTextModeWidget::upperCase()
0609 {
0610     mTextEdit->upperCase();
0611 }
0612 
0613 void SieveEditorTextModeWidget::lowerCase()
0614 {
0615     mTextEdit->lowerCase();
0616 }
0617 
0618 void SieveEditorTextModeWidget::sentenceCase()
0619 {
0620     mTextEdit->sentenceCase();
0621 }
0622 
0623 void SieveEditorTextModeWidget::reverseCase()
0624 {
0625     mTextEdit->reverseCase();
0626 }
0627 
0628 QString SieveEditorTextModeWidget::currentHelpTitle() const
0629 {
0630     return mTabWidget->currentHelpTitle();
0631 }
0632 
0633 QUrl SieveEditorTextModeWidget::currentHelpUrl() const
0634 {
0635     return mTabWidget->currentHelpUrl();
0636 }
0637 
0638 void SieveEditorTextModeWidget::openBookmarkUrl(const QUrl &url)
0639 {
0640     mTabWidget->slotAddHelpPage(url);
0641 }
0642 
0643 void SieveEditorTextModeWidget::debugSieveScript()
0644 {
0645     QPointer<KSieveUi::SieveScriptDebuggerDialog> dlg = new KSieveUi::SieveScriptDebuggerDialog(this);
0646     dlg->setScript(mTextEdit->toPlainText());
0647     if (dlg->exec()) {
0648         const QString script = dlg->script();
0649         mTextEdit->selectAll();
0650         mTextEdit->insertPlainText(script);
0651     }
0652     delete dlg;
0653 }
0654 
0655 bool SieveEditorTextModeWidget::isTextEditor() const
0656 {
0657     const QWidget *w = mTabWidget->currentWidget();
0658     return w == mEditorWidget;
0659 }
0660 
0661 bool SieveEditorTextModeWidget::printSupportEnabled() const
0662 {
0663     return isTextEditor();
0664 }
0665 
0666 #include "moc_sieveeditortextmodewidget.cpp"