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

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "sievescriptdebuggerdialog.h"
0008 #include "sievescriptdebuggerwidget.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KGuiItem>
0012 #include <KLocalizedString>
0013 #include <KSharedConfig>
0014 
0015 #include <KWindowConfig>
0016 #include <QDialogButtonBox>
0017 #include <QPushButton>
0018 #include <QVBoxLayout>
0019 #include <QWindow>
0020 
0021 using namespace KSieveUi;
0022 namespace
0023 {
0024 static const char mySieveScriptDebuggerDialog[] = "SieveScriptDebuggerDialog";
0025 }
0026 SieveScriptDebuggerDialog::SieveScriptDebuggerDialog(QWidget *parent)
0027     : QDialog(parent)
0028 {
0029     auto mainLayout = new QVBoxLayout(this);
0030     setWindowTitle(i18nc("@title:window", "Debug Sieve Script"));
0031 
0032     mSieveScriptDebuggerWidget = new SieveScriptDebuggerWidget(this);
0033     mSieveScriptDebuggerWidget->setObjectName(QLatin1StringView("sievescriptdebuggerwidget"));
0034     mainLayout->addWidget(mSieveScriptDebuggerWidget);
0035     connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::sieveTestNotFound, this, &SieveScriptDebuggerDialog::updateButtons);
0036 
0037     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0038     mainLayout->addWidget(buttonBox);
0039     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0040     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0041     mOkButton->setObjectName(QLatin1StringView("okbutton"));
0042     mOkButton->setText(i18n("Apply Changes"));
0043     mOkButton->setDefault(true);
0044     mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0045     mOkButton->setEnabled(false);
0046     connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveScriptDebuggerDialog::slotAccepted);
0047     connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveScriptDebuggerDialog::reject);
0048     connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
0049     connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugButtonEnabled, this, &SieveScriptDebuggerDialog::debugButtonEnableStateChanged);
0050 
0051     mDebugScriptButton = new QPushButton(i18n("Debug"), this);
0052     const KGuiItem item(i18n("Debug"), QIcon::fromTheme(QStringLiteral("debug-run")));
0053     KGuiItem::assign(mDebugScriptButton, item);
0054 
0055     mDebugScriptButton->setObjectName(QLatin1StringView("debug_button"));
0056     mDebugScriptButton->setEnabled(false);
0057     connect(mDebugScriptButton, &QPushButton::clicked, mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugScriptButtonClicked);
0058     buttonBox->addButton(mDebugScriptButton, QDialogButtonBox::ActionRole);
0059     readConfig();
0060 }
0061 
0062 SieveScriptDebuggerDialog::~SieveScriptDebuggerDialog()
0063 {
0064     disconnect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
0065     writeConfig();
0066 }
0067 
0068 void SieveScriptDebuggerDialog::updateButtons()
0069 {
0070     mOkButton->setEnabled(false);
0071 }
0072 
0073 void SieveScriptDebuggerDialog::debugButtonEnableStateChanged(bool state)
0074 {
0075     mDebugScriptButton->setEnabled(state);
0076 }
0077 
0078 void SieveScriptDebuggerDialog::slotScriptTextChanged()
0079 {
0080     if (mSieveScriptDebuggerWidget->haveDebugApps()) {
0081         mOkButton->setEnabled(mOriginScript != mSieveScriptDebuggerWidget->script());
0082     }
0083 }
0084 
0085 void SieveScriptDebuggerDialog::slotAccepted()
0086 {
0087     if (mSieveScriptDebuggerWidget->canAccept()) {
0088         accept();
0089     }
0090 }
0091 
0092 void SieveScriptDebuggerDialog::setScript(const QString &script)
0093 {
0094     mOriginScript = script;
0095     mSieveScriptDebuggerWidget->setScript(script);
0096     mOkButton->setEnabled(false);
0097 }
0098 
0099 QString SieveScriptDebuggerDialog::script() const
0100 {
0101     return mSieveScriptDebuggerWidget->script();
0102 }
0103 
0104 void SieveScriptDebuggerDialog::readConfig()
0105 {
0106     create(); // ensure a window is created
0107     windowHandle()->resize(QSize(800, 600));
0108     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveScriptDebuggerDialog));
0109     KWindowConfig::restoreWindowSize(windowHandle(), group);
0110     resize(windowHandle()->size()); // workaround for QTBUG-40584
0111     const QList<int> size{100, 400};
0112     mSieveScriptDebuggerWidget->setSplitterSizes(group.readEntry("Splitter", size));
0113 }
0114 
0115 void SieveScriptDebuggerDialog::writeConfig()
0116 {
0117     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveScriptDebuggerDialog));
0118     KWindowConfig::saveWindowSize(windowHandle(), group);
0119     group.writeEntry("Splitter", mSieveScriptDebuggerWidget->splitterSizes());
0120     group.sync();
0121 }
0122 
0123 #include "moc_sievescriptdebuggerdialog.cpp"