Libksieve

sieveeditor.cpp
1 /* SPDX-FileCopyrightText: 2011-2021 Laurent Montel <[email protected]>
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 <QDialogButtonBox>
16 #include <QKeyEvent>
17 #include <QPushButton>
18 #include <QVBoxLayout>
19 
20 using namespace KSieveUi;
21 class KSieveUi::SieveEditorPrivate
22 {
23 public:
24  SieveEditorPrivate()
25  {
26  }
27 
28  SieveEditorWidget *mSieveEditorWidget = nullptr;
29  QPushButton *mOkButton = nullptr;
30 };
31 
32 SieveEditor::SieveEditor(QWidget *parent)
33  : QDialog(parent)
34  , d(new KSieveUi::SieveEditorPrivate)
35 {
36  setWindowTitle(i18nc("@title:window", "Edit Sieve Script"));
37  auto mainLayout = new QVBoxLayout(this);
38 
40  d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
41  d->mOkButton->setDefault(true);
42  d->mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
43  connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveEditor::slotAccepted);
44  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveEditor::slotCanceled);
45  d->mSieveEditorWidget = new SieveEditorWidget(true);
46  connect(d->mSieveEditorWidget, &SieveEditorWidget::valueChanged, this, &SieveEditor::valueChanged);
47  mainLayout->addWidget(d->mSieveEditorWidget);
48  mainLayout->addWidget(buttonBox);
49  connect(d->mSieveEditorWidget, &SieveEditorWidget::enableButtonOk, this, &SieveEditor::slotEnableButtonOk);
50  connect(this, &SieveEditor::finished, this, &SieveEditor::cancelClicked);
51  connect(d->mSieveEditorWidget, &SieveEditorWidget::checkSyntax, this, &SieveEditor::checkSyntax);
52  readConfig();
53 }
54 
55 SieveEditor::~SieveEditor()
56 {
57  writeConfig();
58 }
59 
60 void SieveEditor::slotAccepted()
61 {
62  Q_EMIT okClicked();
63  // Don't accept here we need to check script. If it's failed we don't close this editor otherwise we will lose data
64 }
65 
66 void SieveEditor::slotCanceled()
67 {
68  Q_EMIT cancelClicked();
69  reject();
70 }
71 
72 bool SieveEditor::event(QEvent *e)
73 {
74  // Close the bar when pressing Escape.
75  // Not using a QShortcut for this because it could conflict with
76  // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
77  // With a shortcut override we can catch this before it gets to kactions.
78  const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
79  if (shortCutOverride || e->type() == QEvent::KeyPress) {
80  auto kev = static_cast<QKeyEvent *>(e);
81  if (kev->key() == Qt::Key_Escape) {
82  e->ignore();
83  return true;
84  }
85  }
86  return QDialog::event(e);
87 }
88 
89 void SieveEditor::setModified(bool b)
90 {
91  d->mSieveEditorWidget->setModified(b);
92 }
93 
94 void SieveEditor::slotEnableButtonOk(bool b)
95 {
96  d->mOkButton->setEnabled(b);
97 }
98 
99 void SieveEditor::writeConfig()
100 {
101  KConfigGroup group(KSharedConfig::openStateConfig(), "SieveEditor");
102  group.writeEntry("Size", size());
103 }
104 
105 void SieveEditor::readConfig()
106 {
107  KConfigGroup group(KSharedConfig::openStateConfig(), "SieveEditor");
108  const QSize sizeDialog = group.readEntry("Size", QSize(800, 600));
109  if (sizeDialog.isValid()) {
110  resize(sizeDialog);
111  }
112 }
113 
114 QString SieveEditor::script() const
115 {
116  return d->mSieveEditorWidget->script();
117 }
118 
119 QString SieveEditor::originalScript() const
120 {
121  return d->mSieveEditorWidget->originalScript();
122 }
123 
124 void SieveEditor::setScript(const QString &script, bool clearUndoRedo)
125 {
126  d->mSieveEditorWidget->setScript(script, clearUndoRedo);
127 }
128 
129 void SieveEditor::setDebugScript(const QString &debug)
130 {
131  d->mSieveEditorWidget->setDebugScript(debug);
132 }
133 
134 void SieveEditor::setScriptName(const QString &name)
135 {
136  d->mSieveEditorWidget->setScriptName(name);
137 }
138 
139 void SieveEditor::resultDone()
140 {
141  d->mSieveEditorWidget->resultDone();
142 }
143 
144 void SieveEditor::setSieveImapAccountSettings(const KSieveUi::SieveImapAccountSettings &account)
145 {
146  d->mSieveEditorWidget->setSieveImapAccountSettings(account);
147 }
148 
149 void SieveEditor::setSieveCapabilities(const QStringList &capabilities)
150 {
151  d->mSieveEditorWidget->setSieveCapabilities(capabilities);
152 }
153 
154 void SieveEditor::setListOfIncludeFile(const QStringList &listOfIncludeFile)
155 {
156  d->mSieveEditorWidget->setListOfIncludeFile(listOfIncludeFile);
157 }
158 
159 void SieveEditor::addFailedMessage(const QString &err)
160 {
161  d->mSieveEditorWidget->addFailedMessage(err);
162 }
163 
164 void SieveEditor::addOkMessage(const QString &msg)
165 {
166  d->mSieveEditorWidget->addOkMessage(msg);
167 }
168 
169 void SieveEditor::addNormalMessage(const QString &msg)
170 {
171  d->mSieveEditorWidget->addNormalMessage(msg);
172 }
173 
174 void SieveEditor::closeEvent(QCloseEvent *e)
175 {
176  if (d->mSieveEditorWidget->originalScript() != d->mSieveEditorWidget->script()) {
177  if (KMessageBox::No == KMessageBox::warningYesNo(this, i18n("Script is modified. Do you want to close editor ?"))) {
178  e->ignore();
179  return;
180  }
181  }
182  Q_EMIT cancelClicked();
183  e->accept();
184 }
void resize(int w, int h)
bool isValid() const const
ShortcutOverride
QEvent::Type type() const const
virtual void reject()
void finished(int result)
QSize size() const const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
void ignore()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void accept()
QString i18n(const char *text, const TYPE &arg...)
void setWindowTitle(const QString &)
The SieveImapAccountSettings class.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The SieveEditorWidget class.
virtual bool event(QEvent *event) override
Key_Return
Q_EMITQ_EMIT
ButtonCode warningYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.