Libksieve

sievescriptdebuggerdialog.cpp
1 /*
2  Copyright (C) 2015-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "sievescriptdebuggerdialog.h"
21 #include "sievescriptdebuggerwidget.h"
22 
23 #include <KLocalizedString>
24 #include <KConfigGroup>
25 #include <KSharedConfig>
26 
27 #include <QDialogButtonBox>
28 #include <QPushButton>
29 #include <QVBoxLayout>
30 
31 using namespace KSieveUi;
32 
33 SieveScriptDebuggerDialog::SieveScriptDebuggerDialog(QWidget *parent)
34  : QDialog(parent)
35 {
36  QVBoxLayout *mainLayout = new QVBoxLayout(this);
37  setWindowTitle(i18nc("@title:window", "Debug Sieve Script"));
38 
39  mSieveScriptDebuggerWidget = new SieveScriptDebuggerWidget(this);
40  mSieveScriptDebuggerWidget->setObjectName(QStringLiteral("sievescriptdebuggerwidget"));
41  mainLayout->addWidget(mSieveScriptDebuggerWidget);
42  connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::sieveTestNotFound, this, &SieveScriptDebuggerDialog::updateButtons);
43 
44  QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
45  mainLayout->addWidget(buttonBox);
46  buttonBox->setObjectName(QStringLiteral("buttonbox"));
47  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
48  mOkButton->setObjectName(QStringLiteral("okbutton"));
49  mOkButton->setText(i18n("Apply Changes"));
50  mOkButton->setDefault(true);
51  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
52  mOkButton->setEnabled(false);
53  connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveScriptDebuggerDialog::slotAccepted);
54  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveScriptDebuggerDialog::reject);
55  connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
56  connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugButtonEnabled, this, &SieveScriptDebuggerDialog::debugButtonEnableStateChanged);
57 
58  mDebugScriptButton = new QPushButton(i18n("Debug"), this);
59  mDebugScriptButton->setObjectName(QStringLiteral("debug_button"));
60  mDebugScriptButton->setEnabled(false);
61  connect(mDebugScriptButton, &QPushButton::clicked, mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugScriptButtonClicked);
62  buttonBox->addButton(mDebugScriptButton, QDialogButtonBox::ActionRole);
63  readConfig();
64 }
65 
66 SieveScriptDebuggerDialog::~SieveScriptDebuggerDialog()
67 {
68  disconnect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
69  writeConfig();
70 }
71 
72 void SieveScriptDebuggerDialog::updateButtons()
73 {
74  mOkButton->setEnabled(false);
75 }
76 
77 void SieveScriptDebuggerDialog::debugButtonEnableStateChanged(bool state)
78 {
79  mDebugScriptButton->setEnabled(state);
80 }
81 
82 void SieveScriptDebuggerDialog::slotScriptTextChanged()
83 {
84  if (mSieveScriptDebuggerWidget->haveDebugApps()) {
85  mOkButton->setEnabled(mOriginScript != mSieveScriptDebuggerWidget->script());
86  }
87 }
88 
89 void SieveScriptDebuggerDialog::slotAccepted()
90 {
91  if (mSieveScriptDebuggerWidget->canAccept()) {
92  accept();
93  }
94 }
95 
96 void SieveScriptDebuggerDialog::setScript(const QString &script)
97 {
98  mOriginScript = script;
99  mSieveScriptDebuggerWidget->setScript(script);
100  mOkButton->setEnabled(false);
101 }
102 
103 QString SieveScriptDebuggerDialog::script() const
104 {
105  return mSieveScriptDebuggerWidget->script();
106 }
107 
108 void SieveScriptDebuggerDialog::writeConfig()
109 {
110  KConfigGroup group(KSharedConfig::openConfig(), "SieveScriptDebuggerDialog");
111  group.writeEntry("Size", size());
112  group.writeEntry("Splitter", mSieveScriptDebuggerWidget->splitterSizes());
113 }
114 
115 void SieveScriptDebuggerDialog::readConfig()
116 {
117  KConfigGroup group(KSharedConfig::openConfig(), "SieveScriptDebuggerDialog");
118  const QSize sizeDialog = group.readEntry("Size", QSize(800, 600));
119  if (sizeDialog.isValid()) {
120  resize(sizeDialog);
121  }
122  const QList<int> size {
123  100, 400
124  };
125  mSieveScriptDebuggerWidget->setSplitterSizes(group.readEntry("Splitter", size));
126 }
bool isValid() const const
void addButton(QAbstractButton *button, QDialogButtonBox::ButtonRole role)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setObjectName(const QString &name)
void clicked(bool checked)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
The SieveScriptDebuggerWidget class.
QPushButton * button(QDialogButtonBox::StandardButton which) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:09:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.