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 "sievescriptdebuggerwidget.h"
0008 #include "sievescriptdebuggerfrontendwidget.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QStackedWidget>
0015 #include <QStandardPaths>
0016 #include <QTimer>
0017 
0018 using namespace KSieveUi;
0019 SieveScriptDebuggerWidget::SieveScriptDebuggerWidget(QWidget *parent)
0020     : QWidget(parent)
0021 {
0022     auto mainLayout = new QHBoxLayout(this);
0023     mainLayout->setContentsMargins({});
0024     mStackedWidget = new QStackedWidget(this);
0025     mStackedWidget->setObjectName(QLatin1StringView("stackedwidget"));
0026     mainLayout->addWidget(mStackedWidget);
0027 
0028     mSieveScriptFrontEnd = new SieveScriptDebuggerFrontEndWidget;
0029     mSieveScriptFrontEnd->setObjectName(QLatin1StringView("sievescriptfrontend"));
0030     mStackedWidget->addWidget(mSieveScriptFrontEnd);
0031     connect(mSieveScriptFrontEnd, &SieveScriptDebuggerFrontEndWidget::scriptTextChanged, this, &SieveScriptDebuggerWidget::scriptTextChanged);
0032     connect(mSieveScriptFrontEnd, &SieveScriptDebuggerFrontEndWidget::debugButtonEnabled, this, &SieveScriptDebuggerWidget::debugButtonEnabled);
0033     connect(this, &SieveScriptDebuggerWidget::debugScriptButtonClicked, mSieveScriptFrontEnd, &SieveScriptDebuggerFrontEndWidget::debugScriptButtonClicked);
0034 
0035     mSieveNoExistingFrontEnd = new QLabel(i18n("\"sieve-test\" was not found on system. Please install it. (See in Dovecot package)"));
0036     mSieveNoExistingFrontEnd->setAlignment(Qt::AlignHCenter);
0037     QFont f = mSieveNoExistingFrontEnd->font();
0038     f.setBold(true);
0039     mSieveNoExistingFrontEnd->setFont(f);
0040     mSieveNoExistingFrontEnd->setObjectName(QLatin1StringView("sievenoexistingfrontend"));
0041     mStackedWidget->addWidget(mSieveNoExistingFrontEnd);
0042 
0043     QTimer::singleShot(0, this, &SieveScriptDebuggerWidget::checkSieveTestApplication);
0044 }
0045 
0046 SieveScriptDebuggerWidget::~SieveScriptDebuggerWidget() = default;
0047 
0048 QList<int> SieveScriptDebuggerWidget::splitterSizes() const
0049 {
0050     return mSieveScriptFrontEnd->splitterSizes();
0051 }
0052 
0053 void SieveScriptDebuggerWidget::setSplitterSizes(const QList<int> &sizes)
0054 {
0055     mSieveScriptFrontEnd->setSplitterSizes(sizes);
0056 }
0057 
0058 bool SieveScriptDebuggerWidget::canAccept() const
0059 {
0060     return mSieveScriptFrontEnd->canAccept();
0061 }
0062 
0063 void SieveScriptDebuggerWidget::setScript(const QString &script)
0064 {
0065     if (mStackedWidget->currentWidget() == mSieveScriptFrontEnd) {
0066         mSieveScriptFrontEnd->setScript(script);
0067     }
0068 }
0069 
0070 QString SieveScriptDebuggerWidget::script() const
0071 {
0072     if (mStackedWidget->currentWidget() == mSieveScriptFrontEnd) {
0073         return mSieveScriptFrontEnd->script();
0074     } else {
0075         return {};
0076     }
0077 }
0078 
0079 void SieveScriptDebuggerWidget::checkSieveTestApplication()
0080 {
0081     if (QStandardPaths::findExecutable(QStringLiteral("sieve-test")).isEmpty()) {
0082         mHaveDebugApps = false;
0083         mStackedWidget->setCurrentWidget(mSieveNoExistingFrontEnd);
0084         Q_EMIT sieveTestNotFound();
0085     } else {
0086         mHaveDebugApps = true;
0087         mStackedWidget->setCurrentWidget(mSieveScriptFrontEnd);
0088     }
0089 }
0090 
0091 bool SieveScriptDebuggerWidget::haveDebugApps() const
0092 {
0093     return mHaveDebugApps;
0094 }
0095 
0096 #include "moc_sievescriptdebuggerwidget.cpp"