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

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 "autocreatescriptdialog.h"
0008 #include "sieveeditorgraphicalmodewidget.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 #include <KSharedConfig>
0013 #include <KWindowConfig>
0014 #include <QDialogButtonBox>
0015 #include <QKeyEvent>
0016 #include <QPushButton>
0017 #include <QVBoxLayout>
0018 #include <QWindow>
0019 
0020 using namespace KSieveUi;
0021 namespace
0022 {
0023 static const char myAutoCreateScriptDialogGroupName[] = "AutoCreateScriptDialog";
0024 }
0025 AutoCreateScriptDialog::AutoCreateScriptDialog(QWidget *parent)
0026     : QDialog(parent)
0027     , mEditor(new SieveEditorGraphicalModeWidget(this))
0028 {
0029     setWindowTitle(i18nc("@title:window", "Create Sieve Filter"));
0030     auto mainLayout = new QVBoxLayout(this);
0031     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0032     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0033     okButton->setDefault(true);
0034     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0035     connect(buttonBox, &QDialogButtonBox::accepted, this, &AutoCreateScriptDialog::accept);
0036     connect(buttonBox, &QDialogButtonBox::rejected, this, &AutoCreateScriptDialog::reject);
0037     okButton->setFocus();
0038     mainLayout->addWidget(mEditor);
0039     mainLayout->addWidget(buttonBox);
0040     readConfig();
0041 }
0042 
0043 AutoCreateScriptDialog::~AutoCreateScriptDialog()
0044 {
0045     writeConfig();
0046 }
0047 
0048 void AutoCreateScriptDialog::loadScript(const QString &doc, QString &error)
0049 {
0050     mEditor->loadScript(doc, error);
0051 }
0052 
0053 void AutoCreateScriptDialog::setSieveImapAccountSettings(const KSieveCore::SieveImapAccountSettings &sieveImapAccountSettings)
0054 {
0055     mEditor->setSieveImapAccountSettings(sieveImapAccountSettings);
0056 }
0057 
0058 void AutoCreateScriptDialog::setSieveCapabilities(const QStringList &capabilities)
0059 {
0060     mEditor->setSieveCapabilities(capabilities);
0061 }
0062 
0063 void AutoCreateScriptDialog::setListOfIncludeFile(const QStringList &listOfIncludeFile)
0064 {
0065     mEditor->setListOfIncludeFile(listOfIncludeFile);
0066 }
0067 
0068 QString AutoCreateScriptDialog::script(QStringList &required) const
0069 {
0070     return mEditor->script(required);
0071 }
0072 
0073 void AutoCreateScriptDialog::readConfig()
0074 {
0075     create(); // ensure a window is created
0076     windowHandle()->resize(QSize(800, 600));
0077     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAutoCreateScriptDialogGroupName));
0078     KWindowConfig::restoreWindowSize(windowHandle(), group);
0079     resize(windowHandle()->size()); // workaround for QTBUG-40584
0080 }
0081 
0082 void AutoCreateScriptDialog::writeConfig()
0083 {
0084     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAutoCreateScriptDialogGroupName));
0085     KWindowConfig::saveWindowSize(windowHandle(), group);
0086     group.sync();
0087 }
0088 
0089 bool AutoCreateScriptDialog::event(QEvent *e)
0090 {
0091     const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
0092     if (shortCutOverride || e->type() == QEvent::KeyPress) {
0093         auto kev = static_cast<QKeyEvent *>(e);
0094         if (kev->key() == Qt::Key_Escape) {
0095             e->ignore();
0096             return true;
0097         }
0098     }
0099     return QDialog::event(e);
0100 }
0101 
0102 #include "moc_autocreatescriptdialog.cpp"