Libksieve

sieveeditor.cpp
1/* SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org>
2 *
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6#include "sieveeditor.h"
7#include "sieve-editor.h"
8#include "sieveeditorwidget.h"
9
10#include <KLocalizedString>
11#include <KMessageBox>
12
13#include <KConfigGroup>
14#include <KSharedConfig>
15#include <KWindowConfig>
16#include <QDialogButtonBox>
17#include <QKeyEvent>
18#include <QPushButton>
19#include <QVBoxLayout>
20#include <QWindow>
21
22using namespace KSieveUi;
23class KSieveUi::SieveEditorPrivate
24{
25public:
26 SieveEditorPrivate() = default;
27
28 SieveEditorWidget *mSieveEditorWidget = nullptr;
29 QPushButton *mOkButton = nullptr;
30};
31namespace
32{
33static const char mySieveEditorGroupName[] = "SieveEditor";
34}
35SieveEditor::SieveEditor(QWidget *parent)
36 : QDialog(parent)
37 , d(new KSieveUi::SieveEditorPrivate)
38{
39 setWindowTitle(i18nc("@title:window", "Edit Sieve Script"));
40 auto mainLayout = new QVBoxLayout(this);
41
43 d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
44 d->mOkButton->setDefault(true);
45 d->mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
46 connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveEditor::slotAccepted);
47 connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveEditor::slotCanceled);
48 d->mSieveEditorWidget = new SieveEditorWidget(true);
49 connect(d->mSieveEditorWidget, &SieveEditorWidget::valueChanged, this, &SieveEditor::valueChanged);
50 mainLayout->addWidget(d->mSieveEditorWidget);
51 mainLayout->addWidget(buttonBox);
52 connect(d->mSieveEditorWidget, &SieveEditorWidget::enableButtonOk, this, &SieveEditor::slotEnableButtonOk);
53 connect(this, &SieveEditor::finished, this, &SieveEditor::cancelClicked);
54 connect(d->mSieveEditorWidget, &SieveEditorWidget::checkSyntax, this, &SieveEditor::checkSyntax);
55 readConfig();
56}
57
58SieveEditor::~SieveEditor()
59{
60 writeConfig();
61}
62
63void SieveEditor::slotAccepted()
64{
65 Q_EMIT okClicked();
66 // Don't accept here we need to check script. If it's failed we don't close this editor otherwise we will lose data
67}
68
69void SieveEditor::slotCanceled()
70{
71 Q_EMIT cancelClicked();
72 reject();
73}
74
75bool SieveEditor::event(QEvent *e)
76{
77 // Close the bar when pressing Escape.
78 // Not using a QShortcut for this because it could conflict with
79 // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
80 // With a shortcut override we can catch this before it gets to kactions.
81 const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
82 if (shortCutOverride || e->type() == QEvent::KeyPress) {
83 auto kev = static_cast<QKeyEvent *>(e);
84 if (kev->key() == Qt::Key_Escape) {
85 e->ignore();
86 return true;
87 }
88 }
89 return QDialog::event(e);
90}
91
92void SieveEditor::setModified(bool b)
93{
94 d->mSieveEditorWidget->setModified(b);
95}
96
97void SieveEditor::slotEnableButtonOk(bool b)
98{
99 d->mOkButton->setEnabled(b);
100}
101
102void SieveEditor::readConfig()
103{
104 create(); // ensure a window is created
105 windowHandle()->resize(QSize(800, 600));
106 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveEditorGroupName));
108 resize(windowHandle()->size()); // workaround for QTBUG-40584
109}
110
111void SieveEditor::writeConfig()
112{
113 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveEditorGroupName));
115 group.sync();
116}
117
118QString SieveEditor::script() const
119{
120 return d->mSieveEditorWidget->script();
121}
122
123QString SieveEditor::originalScript() const
124{
125 return d->mSieveEditorWidget->originalScript();
126}
127
128void SieveEditor::setScript(const QString &script, bool clearUndoRedo)
129{
130 d->mSieveEditorWidget->setScript(script, clearUndoRedo);
131}
132
133void SieveEditor::setDebugScript(const QString &debug)
134{
135 d->mSieveEditorWidget->setDebugScript(debug);
136}
137
138void SieveEditor::setScriptName(const QString &name)
139{
140 d->mSieveEditorWidget->setScriptName(name);
141}
142
143void SieveEditor::resultDone()
144{
145 d->mSieveEditorWidget->resultDone();
146}
147
148void SieveEditor::setSieveImapAccountSettings(const KSieveCore::SieveImapAccountSettings &account)
149{
150 d->mSieveEditorWidget->setSieveImapAccountSettings(account);
151}
152
153void SieveEditor::setSieveCapabilities(const QStringList &capabilities)
154{
155 d->mSieveEditorWidget->setSieveCapabilities(capabilities);
156}
157
158void SieveEditor::setListOfIncludeFile(const QStringList &listOfIncludeFile)
159{
160 d->mSieveEditorWidget->setListOfIncludeFile(listOfIncludeFile);
161}
162
163void SieveEditor::addFailedMessage(const QString &err)
164{
165 d->mSieveEditorWidget->addFailedMessage(err);
166}
167
168void SieveEditor::addOkMessage(const QString &msg)
169{
170 d->mSieveEditorWidget->addOkMessage(msg);
171}
172
173void SieveEditor::addNormalMessage(const QString &msg)
174{
175 d->mSieveEditorWidget->addNormalMessage(msg);
176}
177
178void SieveEditor::closeEvent(QCloseEvent *e)
179{
180 if (d->mSieveEditorWidget->originalScript() != d->mSieveEditorWidget->script()) {
181 const int answer = KMessageBox::warningTwoActions(this,
182 i18n("Script is modified. Do you want to close editor?"),
183 QString(),
184 KGuiItem(i18nc("@action:button", "Close Editor"), QStringLiteral("dialog-close")),
185 KGuiItem(i18nc("@action:button", "Do Not Close"), QStringLiteral("dialog-cancel")));
186 if (answer == KMessageBox::ButtonCode::SecondaryAction) {
187 e->ignore();
188 return;
189 }
190 }
191 Q_EMIT cancelClicked();
192 e->accept();
193}
194
195#include "moc_sieveeditor.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The SieveImapAccountSettings class.
The SieveEditorWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
ButtonCode warningTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
virtual void reject()
ShortcutOverride
void accept()
void ignore()
Type type() const const
Q_EMITQ_EMIT
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
virtual bool event(QEvent *event) override
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.