8#include "sieveeditorwidget.h"
9#include "sievepurposemenuwidget.h"
11#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
12#include "editor/sieveinfodialog.h"
13#include "scriptsparsing/parsingutil.h"
14#include "sieve-editor.h"
15#include "sieveeditormenubar.h"
16#include "sieveeditortextmodewidget.h"
18#include "libksieveui_debug.h"
20#include <KLocalizedString>
21#include <KStandardActions>
22#include <PimCommon/PurposeMenuWidget>
27#include <QStackedWidget>
28#include <QStandardPaths>
33using namespace KSieveUi;
35SieveEditorWidget::SieveEditorWidget(
bool useMenuBar,
QWidget *parent)
39 lay->setContentsMargins({});
40 mDebug = !qEnvironmentVariableIsEmpty(
"KDEPIM_DEBUGGING");
45 mCheckSyntax =
new QAction(
i18nc(
"@action",
"Check Syntax"),
this);
47 toolbar->addAction(mCheckSyntax);
48 mSaveAs = KStandardActions::saveAs(
this, &SieveEditorWidget::slotSaveAs,
this);
49 toolbar->addAction(mSaveAs);
50 toolbar->addAction(
i18n(
"Import…"),
this, &SieveEditorWidget::slotImport);
52 mCreateRulesGraphically =
new QAction(
i18nc(
"@action",
"Create Rules Graphically…"),
this);
54 toolbar->addAction(mCreateRulesGraphically);
55 mSwitchMode =
new QAction(
this);
56 toolbar->addAction(mSwitchMode);
61 mGenerateXml =
new QAction(QStringLiteral(
"Generate xml"),
this);
63 toolbar->addAction(mGenerateXml);
65 auto purposeMenu =
new SievePurposeMenuWidget(
this,
this);
68 shareAction->setMenu(purposeMenu->menu());
70 purposeMenu->setEditorWidget(
this);
71 toolbar->addAction(shareAction);
72 mServerInfo =
new QAction(
i18nc(
"@action",
"Server Info"),
this);
74 toolbar->addAction(mServerInfo);
76 SieveEditorMenuBar *menuBar =
nullptr;
78 menuBar =
new SieveEditorMenuBar;
79 connect(
this, &SieveEditorWidget::changeModeEditor, menuBar, &SieveEditorMenuBar::setEditorMode);
80 connect(menuBar, &SieveEditorMenuBar::copy,
this, &SieveEditorWidget::copy);
81 connect(menuBar, &SieveEditorMenuBar::find,
this, &SieveEditorWidget::find);
82 connect(menuBar, &SieveEditorMenuBar::replace,
this, &SieveEditorWidget::replace);
83 connect(menuBar, &SieveEditorMenuBar::undo,
this, &SieveEditorWidget::undo);
84 connect(menuBar, &SieveEditorMenuBar::redo,
this, &SieveEditorWidget::redo);
85 connect(menuBar, &SieveEditorMenuBar::paste,
this, &SieveEditorWidget::paste);
86 connect(menuBar, &SieveEditorMenuBar::cut,
this, &SieveEditorWidget::cut);
87 connect(menuBar, &SieveEditorMenuBar::selectAll,
this, &SieveEditorWidget::selectAll);
88 connect(menuBar, &SieveEditorMenuBar::gotoLine,
this, &SieveEditorWidget::goToLine);
89 connect(menuBar, &SieveEditorMenuBar::comment,
this, &SieveEditorWidget::comment);
90 connect(menuBar, &SieveEditorMenuBar::uncomment,
this, &SieveEditorWidget::uncomment);
91 connect(menuBar, &SieveEditorMenuBar::zoomIn,
this, &SieveEditorWidget::zoomIn);
92 connect(menuBar, &SieveEditorMenuBar::zoomOut,
this, &SieveEditorWidget::zoomOut);
93 connect(menuBar, &SieveEditorMenuBar::zoomReset,
this, &SieveEditorWidget::zoomReset);
94 connect(menuBar, &SieveEditorMenuBar::debugSieveScript,
this, &SieveEditorWidget::debugSieveScript);
95 connect(menuBar, &SieveEditorMenuBar::wordWrap,
this, &SieveEditorWidget::setWordWrap);
96 connect(menuBar, &SieveEditorMenuBar::print,
this, &SieveEditorWidget::print);
97 connect(menuBar, &SieveEditorMenuBar::printPreview,
this, &SieveEditorWidget::printPreview);
99 connect(
this, &SieveEditorWidget::copyAvailable, menuBar, &SieveEditorMenuBar::slotCopyAvailable);
100 connect(
this, &SieveEditorWidget::redoAvailable, menuBar, &SieveEditorMenuBar::slotRedoAvailable);
101 connect(
this, &SieveEditorWidget::undoAvailable, menuBar, &SieveEditorMenuBar::slotUndoAvailable);
106 menuBar->fileMenu()->
addAction(shareAction);
108 menuBar->toolsMenu()->
addAction(mCreateRulesGraphically);
109 menuBar->toolsMenu()->
addAction(mCheckSyntax);
110 lay->addWidget(menuBar);
113 lay->addWidget(toolbar);
117 nameLayout->addWidget(label);
119 mScriptName->setReadOnly(
true);
120 nameLayout->addWidget(mScriptName);
121 lay->addLayout(nameLayout);
123 lay->setContentsMargins({});
127 connect(purposeMenu, &SievePurposeMenuWidget::shareError, mTextModeWidget, &SieveEditorTextModeWidget::slotShareError);
128 connect(purposeMenu, &SievePurposeMenuWidget::shareSuccess, mTextModeWidget, &SieveEditorTextModeWidget::slotShareSuccess);
129 connect(mTextModeWidget, &SieveEditorTextModeWidget::valueChanged,
this, &SieveEditorWidget::slotModified);
131 menuBar->setTextModeWidget(mTextModeWidget);
133 mStackedWidget->addWidget(mTextModeWidget);
134 mGraphicalModeWidget =
new SieveEditorGraphicalModeWidget;
135 connect(mGraphicalModeWidget, &SieveEditorGraphicalModeWidget::valueChanged,
this, &SieveEditorWidget::slotModified);
136 mStackedWidget->addWidget(mGraphicalModeWidget);
138 lay->addWidget(mStackedWidget);
139 connect(mTextModeWidget, &SieveEditorTextModeWidget::enableButtonOk,
this, &SieveEditorWidget::slotEnableButtonOk);
140 connect(mGraphicalModeWidget, &SieveEditorGraphicalModeWidget::enableButtonOk,
this, &SieveEditorWidget::slotEnableButtonOk);
141 connect(mGraphicalModeWidget, &SieveEditorGraphicalModeWidget::switchTextMode,
this, &SieveEditorWidget::slotSwitchTextMode);
142 connect(mTextModeWidget, &SieveEditorTextModeWidget::switchToGraphicalMode,
this, &SieveEditorWidget::slotSwitchToGraphicalMode);
143 connect(mTextModeWidget, &SieveEditorTextModeWidget::undoAvailable,
this, &SieveEditorWidget::undoAvailable);
144 connect(mTextModeWidget, &SieveEditorTextModeWidget::redoAvailable,
this, &SieveEditorWidget::redoAvailable);
145 connect(mTextModeWidget, &SieveEditorTextModeWidget::copyAvailable,
this, &SieveEditorWidget::copyAvailable);
146 connect(mTextModeWidget, &SieveEditorTextModeWidget::sieveEditorTabCurrentChanged,
this, &SieveEditorWidget::sieveEditorTabCurrentChanged);
147 if (KSieveUi::EditorSettings::useGraphicEditorByDefault()) {
148 changeMode(GraphicMode);
150 changeSwitchButtonText();
154SieveEditorWidget::~SieveEditorWidget() =
default;
156void SieveEditorWidget::setReadOnly(
bool b)
158 mTextModeWidget->setReadOnly(b);
162void SieveEditorWidget::slotModified()
165 Q_EMIT valueChanged(mModified);
168bool SieveEditorWidget::isModified()
const
173void SieveEditorWidget::undo()
175 if (mMode == TextMode) {
176 mTextModeWidget->undo();
180void SieveEditorWidget::redo()
182 if (mMode == TextMode) {
183 mTextModeWidget->redo();
187void SieveEditorWidget::goToLine()
189 if (mMode == TextMode) {
190 mTextModeWidget->slotShowGoToLine();
194void SieveEditorWidget::cut()
196 if (mMode == TextMode) {
197 mTextModeWidget->cut();
201void SieveEditorWidget::paste()
203 if (mMode == TextMode) {
204 mTextModeWidget->paste();
208void SieveEditorWidget::copy()
210 if (mMode == TextMode) {
211 mTextModeWidget->copy();
215void SieveEditorWidget::zoomIn()
217 if (mMode == TextMode) {
218 mTextModeWidget->zoomIn();
222bool SieveEditorWidget::isWordWrap()
const
224 if (mMode == TextMode) {
225 return mTextModeWidget->isWordWrap();
230void SieveEditorWidget::updateOriginalScript()
232 mOriginalScript = script();
235void SieveEditorWidget::print()
239 bool wasModified = isModified();
240 mTextModeWidget->print();
241 setModified(wasModified);
247 qCDebug(LIBKSIEVEUI_LOG) <<
" Unknown mode";
252void SieveEditorWidget::printPreview()
256 bool wasModified = isModified();
257 mTextModeWidget->printPreview();
258 setModified(wasModified);
264 qCDebug(LIBKSIEVEUI_LOG) <<
" Unknown mode";
269void SieveEditorWidget::setWordWrap(
bool state)
271 if (mMode == TextMode) {
272 mTextModeWidget->setWordWrap(state);
276void SieveEditorWidget::zoomReset()
278 if (mMode == TextMode) {
279 mTextModeWidget->zoomReset();
283void SieveEditorWidget::zoomOut()
285 if (mMode == TextMode) {
286 mTextModeWidget->zoomOut();
290void SieveEditorWidget::selectAll()
292 if (mMode == TextMode) {
293 mTextModeWidget->selectAll();
297void SieveEditorWidget::find()
299 if (mMode == TextMode) {
300 mTextModeWidget->find();
304void SieveEditorWidget::replace()
306 if (mMode == TextMode) {
307 mTextModeWidget->replace();
311bool SieveEditorWidget::isUndoAvailable()
const
313 if (mMode == TextMode) {
314 return mTextModeWidget->isUndoAvailable();
319bool SieveEditorWidget::isRedoAvailable()
const
321 if (mMode == TextMode) {
322 return mTextModeWidget->isRedoAvailable();
327bool SieveEditorWidget::hasSelection()
const
329 if (mMode == TextMode) {
330 return mTextModeWidget->hasSelection();
335void SieveEditorWidget::comment()
337 if (mMode == TextMode) {
338 mTextModeWidget->comment();
342void SieveEditorWidget::uncomment()
344 if (mMode == TextMode) {
345 mTextModeWidget->uncomment();
349SieveEditorWidget::EditorMode SieveEditorWidget::mode()
const
354void SieveEditorWidget::setModified(
bool b)
356 if (mModified != b) {
358 Q_EMIT valueChanged(mModified);
362void SieveEditorWidget::changeMode(EditorMode mode)
367 const bool isTextMode = (mMode == TextMode);
368 mCreateRulesGraphically->
setEnabled(isTextMode);
377 Q_EMIT modeEditorChanged(mode);
378 Q_EMIT changeModeEditor(isTextMode);
379 changeSwitchButtonText();
383void SieveEditorWidget::changeSwitchButtonText()
385 mSwitchMode->
setText((mMode == TextMode) ?
i18n(
"Simple Mode") :
i18n(
"Advanced Mode"));
388void SieveEditorWidget::slotEnableButtonOk(
bool b)
392 if (mMode == TextMode) {
399QString SieveEditorWidget::script()
const
404 currentScript = mTextModeWidget->script();
407 currentScript = mGraphicalModeWidget->currentscript();
410 qCDebug(LIBKSIEVEUI_LOG) <<
" Unknown Mode!";
413 return currentScript;
416QString SieveEditorWidget::originalScript()
const
418 return mOriginalScript;
421void SieveEditorWidget::setScript(
const QString &script,
bool clearUndoRedo)
423 mTextModeWidget->setScript(script, clearUndoRedo);
425 mOriginalScript = mTextModeWidget->script();
428void SieveEditorWidget::addFailedMessage(
const QString &err)
433void SieveEditorWidget::addOkMessage(
const QString &msg)
438void SieveEditorWidget::addNormalMessage(
const QString &msg)
443void SieveEditorWidget::addMessageEntry(
const QString &errorMsg,
const QColor &color)
447 const QString logText = QStringLiteral(
"<font color=%1>%2</font>").arg(color.
name(), msg);
452void SieveEditorWidget::setDebugScript(
const QString &debug)
454 mTextModeWidget->setDebugScript(debug);
457void SieveEditorWidget::setScriptName(
const QString &name)
462void SieveEditorWidget::resultDone()
467void SieveEditorWidget::setSieveCapabilities(
const QStringList &capabilities)
469 mTextModeWidget->setSieveCapabilities(capabilities);
470 mGraphicalModeWidget->setSieveCapabilities(capabilities);
475 mGraphicalModeWidget->setSieveImapAccountSettings(sieveImapAccountSettings);
476 mTextModeWidget->setSieveImapAccountSettings(sieveImapAccountSettings);
479void SieveEditorWidget::setListOfIncludeFile(
const QStringList &listOfIncludeFile)
481 mTextModeWidget->setListOfIncludeFile(listOfIncludeFile);
482 mGraphicalModeWidget->setListOfIncludeFile(listOfIncludeFile);
485void SieveEditorWidget::slotCreateRulesGraphically()
489 mTextModeWidget->createRulesGraphically();
497void SieveEditorWidget::slotCheckSyntax()
510void SieveEditorWidget::slotGenerateXml()
514 mTextModeWidget->generateXml();
522void SieveEditorWidget::checkSpelling()
526 mTextModeWidget->checkSpelling();
534void SieveEditorWidget::slotSaveAs()
538 mTextModeWidget->saveAs(mScriptName->
text());
541 mGraphicalModeWidget->saveAs(mScriptName->
text());
544 qCDebug(LIBKSIEVEUI_LOG) <<
" Unknown mode";
549void SieveEditorWidget::slotImport()
553 mTextModeWidget->slotImport();
556 mGraphicalModeWidget->slotImport();
559 qCDebug(LIBKSIEVEUI_LOG) <<
" Unknown mode";
564void SieveEditorWidget::slotSwitchToGraphicalMode()
566 mTextModeWidget->hideEditorWarning();
567 changeMode(GraphicMode);
570void SieveEditorWidget::slotSwitchMode()
575 const QString doc = KSieveCore::ParsingUtil::parseScript(mTextModeWidget->currentscript(), result);
578 mGraphicalModeWidget->loadScript(doc, error);
579 if (!
error.isEmpty()) {
580 mTextModeWidget->setParsingEditorWarningError(mTextModeWidget->currentscript(), error);
581 mTextModeWidget->showParsingEditorWarning();
583 mTextModeWidget->hideEditorWarning();
584 changeMode(GraphicMode);
587 mTextModeWidget->showEditorWarning();
588 qCDebug(LIBKSIEVEUI_LOG) <<
"Impossible to parse file";
593 const QString script = mGraphicalModeWidget->currentscript();
594 changeMode(TextMode);
595 mTextModeWidget->setScript(script);
599 qCDebug(LIBKSIEVEUI_LOG) <<
" Unknown mode";
604void SieveEditorWidget::slotSwitchTextMode(
const QString &script)
606 changeMode(TextMode);
607 mTextModeWidget->setScript(script);
610void SieveEditorWidget::reverseCase()
612 if (mMode == TextMode) {
613 mTextModeWidget->reverseCase();
617void SieveEditorWidget::lowerCase()
619 if (mMode == TextMode) {
620 mTextModeWidget->lowerCase();
624void SieveEditorWidget::debugSieveScript()
626 if (mMode == TextMode) {
627 mTextModeWidget->debugSieveScript();
631void SieveEditorWidget::upperCase()
633 if (mMode == TextMode) {
634 mTextModeWidget->upperCase();
638void SieveEditorWidget::sentenceCase()
640 if (mMode == TextMode) {
641 mTextModeWidget->sentenceCase();
645void SieveEditorWidget::openBookmarkUrl(
const QUrl &url)
647 if (mMode == TextMode) {
648 mTextModeWidget->openBookmarkUrl(url);
652QString SieveEditorWidget::currentHelpTitle()
const
654 if (mMode == TextMode) {
655 return mTextModeWidget->currentHelpTitle();
660QUrl SieveEditorWidget::currentHelpUrl()
const
662 if (mMode == TextMode) {
663 return mTextModeWidget->currentHelpUrl();
668bool SieveEditorWidget::isTextEditor()
const
670 if (mMode == TextMode) {
671 return mTextModeWidget->isTextEditor();
676bool SieveEditorWidget::printSupportEnabled()
const
678 if (mMode == TextMode) {
679 return mTextModeWidget->printSupportEnabled();
684void SieveEditorWidget::slotServerInfo()
686 SieveInfoDialog dlg(
this);
687 dlg.setServerInfo(mTextModeWidget->sieveCapabilities());
691#include "moc_sieveeditorwidget.cpp"
The SieveImapAccountSettings class.
The SieveEditorTextModeWidget class.
Q_SCRIPTABLE QStringList logText()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString label(StandardShortcut id)
void setText(const QString &text)
void triggered(bool checked)
QString name(NameFormat format) const const
QIcon fromTheme(const QString &name)
void setText(const QString &)
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)