diff --git a/src/ksieveui/autocreatescripts/sieveactions/sieveactionremoveflags.cpp b/src/ksieveui/autocreatescripts/sieveactions/sieveactionremoveflags.cpp index dcd03b8..fec657f 100644 --- a/src/ksieveui/autocreatescripts/sieveactions/sieveactionremoveflags.cpp +++ b/src/ksieveui/autocreatescripts/sieveactions/sieveactionremoveflags.cpp @@ -1,45 +1,44 @@ /* Copyright (C) 2013-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "sieveactionremoveflags.h" #include "editor/sieveeditorutil.h" -#include #include using namespace KSieveUi; SieveActionRemoveFlags::SieveActionRemoveFlags(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) : SieveActionAbstractFlags(sieveGraphicalModeWidget, QStringLiteral("removeflag"), i18n("Remove Flags"), parent) { } QString SieveActionRemoveFlags::flagsCode() const { return QStringLiteral("removeflag"); } QString SieveActionRemoveFlags::help() const { return i18n( "Removeflag is used to remove flags from a list of [IMAP] flags. Removeflag clears flags previously set by \"set\"/\"addflag\". Calling removeflag with a flag that wasn't set before is not an error and is ignored."); } QUrl SieveActionRemoveFlags::href() const { return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); } diff --git a/src/ksieveui/autocreatescripts/sieveactions/sieveactionsetflags.cpp b/src/ksieveui/autocreatescripts/sieveactions/sieveactionsetflags.cpp index da9ecff..0628b91 100644 --- a/src/ksieveui/autocreatescripts/sieveactions/sieveactionsetflags.cpp +++ b/src/ksieveui/autocreatescripts/sieveactions/sieveactionsetflags.cpp @@ -1,44 +1,43 @@ /* Copyright (C) 2013-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "sieveactionsetflags.h" #include "editor/sieveeditorutil.h" -#include #include using namespace KSieveUi; SieveActionSetFlags::SieveActionSetFlags(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) : SieveActionAbstractFlags(sieveGraphicalModeWidget, QStringLiteral("setflag"), i18n("Set Flags"), parent) { } QString SieveActionSetFlags::flagsCode() const { return QStringLiteral("setflag"); } QString SieveActionSetFlags::help() const { return i18n("Setflag is used for setting [IMAP] system flags or keywords. Setflag replaces any previously set flags."); } QUrl SieveActionSetFlags::href() const { return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); } diff --git a/src/ksieveui/autocreatescripts/sieveactionwidgetlister.cpp b/src/ksieveui/autocreatescripts/sieveactionwidgetlister.cpp index 42d3f00..33d8eb2 100644 --- a/src/ksieveui/autocreatescripts/sieveactionwidgetlister.cpp +++ b/src/ksieveui/autocreatescripts/sieveactionwidgetlister.cpp @@ -1,458 +1,460 @@ /* Copyright (C) 2013-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "sieveactionwidgetlister.h" #include "autocreatescriptdialog.h" #include "sieveeditorgraphicalmodewidget.h" #include "sievescriptdescriptiondialog.h" #include "sieveactions/sieveaction.h" #include "sieveactions/sieveactionlist.h" #include "commonwidgets/sievehelpbutton.h" #include "autocreatescriptutil_p.h" -#include #include #include #include +#include #include #include +#include #include "libksieve_debug.h" #include #include #include using namespace KSieveUi; static int MINIMUMACTION = 1; static int MAXIMUMACTION = 8; SieveActionWidget::SieveActionWidget(SieveEditorGraphicalModeWidget *graphicalModeWidget, QWidget *parent) : QWidget(parent) , mSieveGraphicalModeWidget(graphicalModeWidget) { initWidget(); } SieveActionWidget::~SieveActionWidget() { qDeleteAll(mActionList); mActionList.clear(); } bool SieveActionWidget::isConfigurated() const { return mComboBox->currentIndex() != (mComboBox->count() - 1); } void SieveActionWidget::setFilterAction(QWidget *widget) { if (mLayout->itemAtPosition(1, 3)) { delete mLayout->itemAtPosition(1, 3)->widget(); } if (widget) { mLayout->addWidget(widget, 1, 3); } else { mLayout->addWidget(new QLabel(i18n("Please select an action."), this), 1, 3); } } void SieveActionWidget::generatedScript(QString &script, QStringList &required, bool onlyActions, bool inForEveryPartLoop) { const int index = mComboBox->currentIndex(); if (index != mComboBox->count() - 1) { KSieveUi::SieveAction *widgetAction = mActionList.at(mComboBox->currentIndex()); QWidget *currentWidget = mLayout->itemAtPosition(1, 3)->widget(); const QStringList lstRequires = widgetAction->needRequires(currentWidget); for (const QString &r : lstRequires) { if (!required.contains(r)) { required.append(r); } } QString comment = widgetAction->comment(); QString indent; if (!onlyActions) { indent += AutoCreateScriptUtil::indentation(); } if (inForEveryPartLoop) { indent += AutoCreateScriptUtil::indentation(); } if (!comment.trimmed().isEmpty()) { const QVector commentList = comment.splitRef(QLatin1Char('\n')); for (const QStringRef &str : commentList) { if (str.isEmpty()) { script += QLatin1Char('\n'); } else { script += indent + QLatin1Char('#') + str + QLatin1Char('\n'); } } } script += indent + widgetAction->code(currentWidget) + QLatin1Char('\n'); } } void SieveActionWidget::initWidget() { mLayout = new QGridLayout(this); mLayout->setContentsMargins(0, 0, 0, 0); - mComboBox = new PimCommon::MinimumComboBox; + mComboBox = new QComboBox; mComboBox->setEditable(false); + mComboBox->setMinimumWidth(50); const QList list = KSieveUi::SieveActionList::actionList(mSieveGraphicalModeWidget); QList::const_iterator it; QList::const_iterator end(list.constEnd()); int index = 0; QStringList listCapabilities = mSieveGraphicalModeWidget->sieveCapabilities(); //imapflags was old name of imap4flags but still used. if (listCapabilities.contains(QLatin1String("imap4flags"))) { listCapabilities.append(QStringLiteral("imapflags")); } for (index = 0, it = list.constBegin(); it != end; ++it, ++index) { if ((*it)->needCheckIfServerHasCapability()) { if (listCapabilities.contains((*it)->serverNeedsCapability())) { // append to the list of actions: mActionList.append(*it); connect(*it, &SieveAction::valueChanged, this, &SieveActionWidget::valueChanged); // add (i18n-ized) name to combo box mComboBox->addItem((*it)->label(), (*it)->name()); } else { delete(*it); } } else { // append to the list of actions: mActionList.append(*it); connect(*it, &SieveAction::valueChanged, this, &SieveActionWidget::valueChanged); // add (i18n-ized) name to combo box mComboBox->addItem((*it)->label(), (*it)->name()); } } mHelpButton = new SieveHelpButton(this); connect(mHelpButton, &SieveHelpButton::clicked, this, &SieveActionWidget::slotHelp); mLayout->addWidget(mHelpButton, 1, 0); mCommentButton = new QToolButton(this); mCommentButton->setToolTip(i18n("Add comment")); mLayout->addWidget(mCommentButton, 1, 1); mCommentButton->setIcon(QIcon::fromTheme(QStringLiteral("view-pim-notes"))); connect(mCommentButton, &QToolButton::clicked, this, &SieveActionWidget::slotAddComment); mComboBox->addItem(QLatin1String("")); mComboBox->setMaxCount(mComboBox->count()); mComboBox->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); mComboBox->adjustSize(); mLayout->addWidget(mComboBox, 1, 2); updateGeometry(); - connect(mComboBox, QOverload::of(&PimCommon::MinimumComboBox::activated), this, &SieveActionWidget::slotActionChanged); + connect(mComboBox, QOverload::of(&QComboBox::activated), this, &SieveActionWidget::slotActionChanged); mAdd = new QPushButton(this); mAdd->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); mAdd->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); mRemove = new QPushButton(this); mRemove->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); mRemove->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); mLayout->addWidget(mAdd, 1, 4); mLayout->addWidget(mRemove, 1, 5); // redirect focus to the filter action combo box setFocusProxy(mComboBox); connect(mAdd, &QPushButton::clicked, this, &SieveActionWidget::slotAddWidget); connect(mRemove, &QPushButton::clicked, this, &SieveActionWidget::slotRemoveWidget); clear(); } void SieveActionWidget::slotHelp() { const int index = mComboBox->currentIndex(); if (index < mActionList.count()) { KSieveUi::SieveAction *action = mActionList.at(index); const QString help = action->help(); const QUrl href = action->href(); const QString fullWhatsThis = AutoCreateScriptUtil::createFullWhatsThis(help, href.toString()); QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton); } } void SieveActionWidget::clear() { mComboBox->setCurrentIndex(mComboBox->count() - 1); setFilterAction(nullptr); mCommentButton->setEnabled(false); mHelpButton->setEnabled(false); } void SieveActionWidget::slotAddComment() { const int index = mComboBox->currentIndex(); if (index < mActionList.count()) { KSieveUi::SieveAction *action = mActionList.at(index); const QString comment = action->comment(); QPointer dlg = new SieveScriptDescriptionDialog; dlg->setDescription(comment); if (dlg->exec()) { action->setComment(dlg->description()); Q_EMIT valueChanged(); } delete dlg; } } void SieveActionWidget::slotActionChanged(int index) { if (index < mActionList.count()) { KSieveUi::SieveAction *action = mActionList.at(index); mHelpButton->setEnabled(!action->help().isEmpty()); mCommentButton->setEnabled(true); setFilterAction(action->createParamWidget(this)); //All actions after stop will not execute => don't allow to add more actions. const bool enableAddAction = (action->name() != QLatin1String("stop")); mAdd->setEnabled(enableAddAction); } else { mAdd->setEnabled(true); mCommentButton->setEnabled(false); setFilterAction(nullptr); mHelpButton->setEnabled(false); } Q_EMIT valueChanged(); } void SieveActionWidget::slotAddWidget() { Q_EMIT valueChanged(); Q_EMIT addWidget(this); } void SieveActionWidget::slotRemoveWidget() { Q_EMIT valueChanged(); Q_EMIT removeWidget(this); } void SieveActionWidget::updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled) { mAdd->setEnabled(addButtonEnabled); mRemove->setEnabled(removeButtonEnabled); } void SieveActionWidget::setLocaleVariable(const SieveGlobalVariableActionWidget::VariableElement &var) { const int index = mComboBox->findData(QStringLiteral("set")); if (index != -1) { mComboBox->setCurrentIndex(index); slotActionChanged(index); KSieveUi::SieveActionSetVariable *localVar = dynamic_cast(mActionList.at(index)); if (localVar) { localVar->setLocalVariable(this, var); } } else { //error += i18n("Script contains unsupported feature \"%1\"", actionName) + QLatin1Char('\n'); //qCDebug(LIBKSIEVE_LOG) << "Action " << actionName << " not supported"; } } bool SieveActionWidget::setAction(const QString &actionName, QXmlStreamReader &element, const QString &comment, QString &error) { const int index = mComboBox->findData(actionName); bool result = false; if (index != -1) { mComboBox->setCurrentIndex(index); slotActionChanged(index); KSieveUi::SieveAction *action = mActionList.at(index); result = action->setParamWidgetValue(element, this, error); action->setComment(comment); } else { error += i18n("Script contains unsupported feature \"%1\"", actionName) + QLatin1Char('\n'); qCDebug(LIBKSIEVE_LOG) << "Action " << actionName << " not supported"; element.skipCurrentElement(); } return result; } SieveActionWidgetLister::SieveActionWidgetLister(SieveEditorGraphicalModeWidget *graphicalModeWidget, QWidget *parent) : KPIM::KWidgetLister(false, MINIMUMACTION, MAXIMUMACTION, parent) , mSieveGraphicalModeWidget(graphicalModeWidget) { slotClear(); updateAddRemoveButton(); } SieveActionWidgetLister::~SieveActionWidgetLister() { } void SieveActionWidgetLister::slotAddWidget(QWidget *w) { addWidgetAfterThisWidget(w); updateAddRemoveButton(); } void SieveActionWidgetLister::slotRemoveWidget(QWidget *w) { removeWidget(w); updateAddRemoveButton(); } void SieveActionWidgetLister::updateAddRemoveButton() { QList widgetList = widgets(); const int numberOfWidget(widgetList.count()); bool addButtonEnabled = false; bool removeButtonEnabled = false; if (numberOfWidget <= widgetsMinimum()) { addButtonEnabled = true; removeButtonEnabled = false; } else if (numberOfWidget >= widgetsMaximum()) { addButtonEnabled = false; removeButtonEnabled = true; } else { addButtonEnabled = true; removeButtonEnabled = true; } QList::ConstIterator wIt = widgetList.constBegin(); QList::ConstIterator wEnd = widgetList.constEnd(); for (; wIt != wEnd; ++wIt) { SieveActionWidget *w = qobject_cast(*wIt); w->updateAddRemoveButton(addButtonEnabled, removeButtonEnabled); } } void SieveActionWidgetLister::generatedScript(QString &script, QStringList &requireModules, bool onlyActions, bool inForEveryPartLoop) { const QList widgetList = widgets(); QList::ConstIterator wIt = widgetList.constBegin(); QList::ConstIterator wEnd = widgetList.constEnd(); for (; wIt != wEnd; ++wIt) { SieveActionWidget *w = qobject_cast(*wIt); w->generatedScript(script, requireModules, onlyActions, inForEveryPartLoop); } } void SieveActionWidgetLister::reconnectWidget(SieveActionWidget *w) { connect(w, &SieveActionWidget::addWidget, this, &SieveActionWidgetLister::slotAddWidget, Qt::UniqueConnection); connect(w, &SieveActionWidget::removeWidget, this, &SieveActionWidgetLister::slotRemoveWidget, Qt::UniqueConnection); connect(w, &SieveActionWidget::valueChanged, this, &SieveActionWidgetLister::valueChanged, Qt::UniqueConnection); } void SieveActionWidgetLister::clearWidget(QWidget *aWidget) { if (aWidget) { SieveActionWidget *widget = static_cast(aWidget); widget->clear(); } Q_EMIT valueChanged(); } QWidget *SieveActionWidgetLister::createWidget(QWidget *parent) { SieveActionWidget *w = new SieveActionWidget(mSieveGraphicalModeWidget, parent); reconnectWidget(w); return w; } int SieveActionWidgetLister::actionNumber() const { return widgets().count(); } void SieveActionWidgetLister::loadLocalVariable(const SieveGlobalVariableActionWidget::VariableElement &var) { SieveActionWidget *w = qobject_cast(widgets().constLast()); if (w->isConfigurated()) { addWidgetAfterThisWidget(widgets().constLast()); w = qobject_cast(widgets().constLast()); } w->setLocaleVariable(var); } void SieveActionWidgetLister::loadScript(QXmlStreamReader &element, bool onlyActions, QString &error) { QString comment; if (onlyActions) { const QStringRef tagName = element.name(); if (tagName == QLatin1String("action")) { if (element.attributes().hasAttribute(QLatin1String("name"))) { const QString actionName = element.attributes().value(QLatin1String("name")).toString(); SieveActionWidget *w = qobject_cast(widgets().constLast()); if (w->isConfigurated()) { addWidgetAfterThisWidget(widgets().constLast()); w = qobject_cast(widgets().constLast()); } w->setAction(actionName, element, comment, error); //comment.clear(); } else if (tagName == QLatin1String("crlf")) { element.skipCurrentElement(); //nothing } else { qCDebug(LIBKSIEVE_LOG) << " SieveActionWidgetLister::loadScript don't have name attribute " << tagName; } } else { qCDebug(LIBKSIEVE_LOG) << " SieveActionWidgetLister::loadScript Unknow tag name " << tagName; } } else { bool firstAction = true; bool previousActionWasAComment = false; while (element.readNextStartElement()) { const QStringRef tagName = element.name(); if (tagName == QLatin1String("action") || tagName == QLatin1String("control") /*for break action*/) { if (element.attributes().hasAttribute(QLatin1String("name"))) { const QString actionName = element.attributes().value(QLatin1String("name")).toString(); if (tagName == QLatin1String("control") && actionName == QLatin1String("if")) { qCDebug(LIBKSIEVE_LOG) << "We found an loop if in a loop if. Not supported"; error += i18n("We detected a loop if in a loop if. It's not supported") + QLatin1Char('\n'); } if (firstAction) { firstAction = false; } else { addWidgetAfterThisWidget(widgets().constLast()); } SieveActionWidget *w = qobject_cast(widgets().constLast()); w->setAction(actionName, element, comment, error); comment.clear(); } else { qCDebug(LIBKSIEVE_LOG) << " SieveActionWidgetLister::loadScript don't have name attribute " << tagName; } previousActionWasAComment = false; } else if (tagName == QLatin1String("comment")) { if (!comment.isEmpty()) { comment += QLatin1Char('\n'); } previousActionWasAComment = true; comment += element.readElementText(); } else if (tagName == QLatin1String("crlf")) { //Add new line if previous action was a comment if (previousActionWasAComment) { comment += QLatin1Char('\n'); } element.skipCurrentElement(); } else { qCDebug(LIBKSIEVE_LOG) << " SieveActionWidgetLister::loadScript unknown tagName " << tagName; } } } } diff --git a/src/ksieveui/autocreatescripts/sieveactionwidgetlister.h b/src/ksieveui/autocreatescripts/sieveactionwidgetlister.h index 3f9dbf5..054ab5b 100644 --- a/src/ksieveui/autocreatescripts/sieveactionwidgetlister.h +++ b/src/ksieveui/autocreatescripts/sieveactionwidgetlister.h @@ -1,109 +1,105 @@ /* Copyright (C) 2013-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef SIEVEACTIONWIDGETLISTER_H #define SIEVEACTIONWIDGETLISTER_H #include #include "sieveglobalvariablewidget.h" class QPushButton; - +class QComboBox; class QGridLayout; class QToolButton; class QXmlStreamReader; -namespace PimCommon { -class MinimumComboBox; -} - namespace KSieveUi { class SieveAction; class SieveHelpButton; class SieveEditorGraphicalModeWidget; class SieveActionWidget : public QWidget { Q_OBJECT public: explicit SieveActionWidget(SieveEditorGraphicalModeWidget *graphicalModeWidget, QWidget *parent); ~SieveActionWidget(); void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled); void generatedScript(QString &script, QStringList &required, bool onlyActions, bool inForEveryPartLoop); Q_REQUIRED_RESULT bool setAction(const QString &actionName, QXmlStreamReader &element, const QString &comment, QString &error); Q_REQUIRED_RESULT bool isConfigurated() const; void clear(); void setLocaleVariable(const SieveGlobalVariableActionWidget::VariableElement &var); Q_SIGNALS: void addWidget(QWidget *w); void removeWidget(QWidget *w); void actionModified(); void valueChanged(); private: void slotAddWidget(); void slotRemoveWidget(); void slotActionChanged(int index); void slotHelp(); void slotAddComment(); void setFilterAction(QWidget *widget); void initWidget(); QList mActionList; QPushButton *mAdd = nullptr; QPushButton *mRemove = nullptr; - PimCommon::MinimumComboBox *mComboBox = nullptr; + QComboBox *mComboBox = nullptr; QGridLayout *mLayout = nullptr; SieveHelpButton *mHelpButton = nullptr; QToolButton *mCommentButton = nullptr; SieveEditorGraphicalModeWidget *mSieveGraphicalModeWidget = nullptr; }; class SieveActionWidgetLister : public KPIM::KWidgetLister { Q_OBJECT public: explicit SieveActionWidgetLister(SieveEditorGraphicalModeWidget *graphicalModeWidget, QWidget *parent = nullptr); ~SieveActionWidgetLister() override; void generatedScript(QString &script, QStringList &requireModules, bool onlyActions, bool inForEveryPartLoop); void loadScript(QXmlStreamReader &element, bool onlyActions, QString &error); int actionNumber() const; void loadLocalVariable(const SieveGlobalVariableActionWidget::VariableElement &var); public Q_SLOTS: void slotAddWidget(QWidget *w); void slotRemoveWidget(QWidget *w); Q_SIGNALS: void valueChanged(); protected: void clearWidget(QWidget *aWidget) override; QWidget *createWidget(QWidget *parent) override; private: void reconnectWidget(SieveActionWidget *w); void updateAddRemoveButton(); SieveEditorGraphicalModeWidget *mSieveGraphicalModeWidget = nullptr; }; } #endif // SIEVEACTIONWIDGETLISTER_H diff --git a/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.cpp b/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.cpp index ea2b663..145e3a4 100644 --- a/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.cpp +++ b/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.cpp @@ -1,408 +1,410 @@ /* Copyright (C) 2013-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "sieveconditionwidgetlister.h" #include "autocreatescriptdialog.h" #include "autocreatescriptutil_p.h" #include "libksieve_debug.h" #include "commonwidgets/sievehelpbutton.h" #include "sieveeditorgraphicalmodewidget.h" #include "sieveconditions/sieveconditionlist.h" #include "sieveconditions/sievecondition.h" -#include #include #include #include +#include +#include #include #include #include #include "sievescriptdescriptiondialog.h" using namespace KSieveUi; static const int MINIMUMCONDITION = 1; static const int MAXIMUMCONDITION = 8; SieveConditionWidget::SieveConditionWidget(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent) : QWidget(parent) , mSieveGraphicalModeWidget(sieveGraphicalModeWidget) { initWidget(); } SieveConditionWidget::~SieveConditionWidget() { qDeleteAll(mConditionList); mConditionList.clear(); } void SieveConditionWidget::setFilterCondition(QWidget *widget) { if (mLayout->itemAtPosition(1, 3)) { delete mLayout->itemAtPosition(1, 3)->widget(); } if (widget) { mLayout->addWidget(widget, 1, 3); } else { mLayout->addWidget(new QLabel(i18n("Please select an condition."), this), 1, 3); } } void SieveConditionWidget::generatedScript(QString &script, QStringList &required, bool inForEveryPartLoop) { Q_UNUSED(inForEveryPartLoop); const int index = mComboBox->currentIndex(); if (index != mComboBox->count() - 1) { KSieveUi::SieveCondition *widgetCondition = mConditionList.at(mComboBox->currentIndex()); QWidget *currentWidget = mLayout->itemAtPosition(1, 3)->widget(); const QStringList lstRequires = widgetCondition->needRequires(currentWidget); for (const QString &r : lstRequires) { if (!required.contains(r)) { required.append(r); } } script += mConditionList.at(mComboBox->currentIndex())->code(currentWidget) + QLatin1Char('\n'); } } void SieveConditionWidget::initWidget() { mLayout = new QGridLayout(this); mLayout->setContentsMargins(0, 0, 0, 0); - mComboBox = new PimCommon::MinimumComboBox; + mComboBox = new QComboBox; + mComboBox->setMinimumWidth(50); mComboBox->setEditable(false); const QList list = KSieveUi::SieveConditionList::conditionList(mSieveGraphicalModeWidget); QList::const_iterator it; QList::const_iterator end(list.constEnd()); int index = 0; for (index = 0, it = list.constBegin(); it != end; ++it, ++index) { if ((*it)->needCheckIfServerHasCapability()) { if (mSieveGraphicalModeWidget->sieveCapabilities().contains((*it)->serverNeedsCapability())) { // append to the list of actions: mConditionList.append(*it); connect(*it, &SieveCondition::valueChanged, this, &SieveConditionWidget::valueChanged); // add (i18n-ized) name to combo box mComboBox->addItem((*it)->label(), (*it)->name()); } else { delete(*it); } } else { // append to the list of actions: mConditionList.append(*it); connect(*it, &SieveCondition::valueChanged, this, &SieveConditionWidget::valueChanged); // add (i18n-ized) name to combo box mComboBox->addItem((*it)->label(), (*it)->name()); } } mHelpButton = new SieveHelpButton(this); mLayout->addWidget(mHelpButton, 1, 0); connect(mHelpButton, &SieveHelpButton::clicked, this, &SieveConditionWidget::slotHelp); mCommentButton = new QToolButton(this); mCommentButton->setToolTip(i18n("Add comment")); mLayout->addWidget(mCommentButton, 1, 1); mCommentButton->setIcon(QIcon::fromTheme(QStringLiteral("view-pim-notes"))); connect(mCommentButton, &QToolButton::clicked, this, &SieveConditionWidget::slotAddComment); mComboBox->addItem(QLatin1String("")); mLayout->addWidget(mComboBox, 1, 2); - connect(mComboBox, QOverload::of(&PimCommon::MinimumComboBox::activated), this, &SieveConditionWidget::slotConditionChanged); + connect(mComboBox, QOverload::of(&QComboBox::activated), this, &SieveConditionWidget::slotConditionChanged); mComboBox->setMaxCount(mComboBox->count()); mComboBox->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); mComboBox->adjustSize(); mAdd = new QPushButton(this); mAdd->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); mAdd->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); mRemove = new QPushButton(this); mRemove->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); mRemove->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); mLayout->addWidget(mAdd, 1, 4); mLayout->addWidget(mRemove, 1, 5); // redirect focus to the filter action combo box setFocusProxy(mComboBox); connect(mAdd, &QPushButton::clicked, this, &SieveConditionWidget::slotAddWidget); connect(mRemove, &QPushButton::clicked, this, &SieveConditionWidget::slotRemoveWidget); clear(); } void SieveConditionWidget::slotAddComment() { const int index = mComboBox->currentIndex(); if (index < mConditionList.count()) { KSieveUi::SieveCondition *condition = mConditionList.at(index); const QString comment = condition->comment(); QPointer dlg = new SieveScriptDescriptionDialog; dlg->setDescription(comment); if (dlg->exec()) { condition->setComment(dlg->description()); Q_EMIT valueChanged(); } delete dlg; } } void SieveConditionWidget::slotHelp() { const int index = mComboBox->currentIndex(); if (index < mConditionList.count()) { KSieveUi::SieveCondition *condition = mConditionList.at(index); const QString help = condition->help(); const QUrl href = condition->href(); const QString fullWhatsThis = AutoCreateScriptUtil::createFullWhatsThis(help, href.toString()); QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton); } } void SieveConditionWidget::slotConditionChanged(int index) { if (index < mConditionList.count()) { KSieveUi::SieveCondition *condition = mConditionList.at(index); mHelpButton->setEnabled(!condition->help().isEmpty()); setFilterCondition(condition->createParamWidget(this)); mCommentButton->setEnabled(true); } else { setFilterCondition(nullptr); mHelpButton->setEnabled(false); mCommentButton->setEnabled(false); } Q_EMIT valueChanged(); } void SieveConditionWidget::slotAddWidget() { Q_EMIT addWidget(this); Q_EMIT valueChanged(); } void SieveConditionWidget::slotRemoveWidget() { Q_EMIT removeWidget(this); Q_EMIT valueChanged(); } void SieveConditionWidget::clear() { mComboBox->setCurrentIndex(mComboBox->count() - 1); setFilterCondition(nullptr); mHelpButton->setEnabled(false); mCommentButton->setEnabled(false); } void SieveConditionWidget::updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled) { mAdd->setEnabled(addButtonEnabled); mRemove->setEnabled(removeButtonEnabled); } void SieveConditionWidget::setCondition(const QString &conditionName, QXmlStreamReader &element, bool notCondition, QString &error) { const int index = mComboBox->findData(conditionName); if (index != -1) { mComboBox->setCurrentIndex(index); slotConditionChanged(index); KSieveUi::SieveCondition *condition = mConditionList.at(index); condition->setParamWidgetValue(element, this, notCondition, error); } else { error += i18n("Script contains unsupported feature \"%1\"", conditionName) + QLatin1Char('\n'); qCDebug(LIBKSIEVE_LOG) << "Condition " << conditionName << " not supported"; element.skipCurrentElement(); } } SieveConditionWidgetLister::SieveConditionWidgetLister(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent) : KPIM::KWidgetLister(false, MINIMUMCONDITION, MAXIMUMCONDITION, parent) , mSieveGraphicalModeWidget(sieveGraphicalModeWidget) { slotClear(); updateAddRemoveButton(); } SieveConditionWidgetLister::~SieveConditionWidgetLister() { } void SieveConditionWidgetLister::slotAddWidget(QWidget *w) { addWidgetAfterThisWidget(w); updateAddRemoveButton(); } void SieveConditionWidgetLister::slotRemoveWidget(QWidget *w) { removeWidget(w); updateAddRemoveButton(); } void SieveConditionWidgetLister::updateAddRemoveButton() { QList widgetList = widgets(); const int numberOfWidget(widgetList.count()); bool addButtonEnabled = false; bool removeButtonEnabled = false; if (numberOfWidget <= widgetsMinimum()) { addButtonEnabled = true; removeButtonEnabled = false; } else if (numberOfWidget >= widgetsMaximum()) { addButtonEnabled = false; removeButtonEnabled = true; } else { addButtonEnabled = true; removeButtonEnabled = true; } QList::ConstIterator wIt = widgetList.constBegin(); QList::ConstIterator wEnd = widgetList.constEnd(); for (; wIt != wEnd; ++wIt) { SieveConditionWidget *w = qobject_cast(*wIt); w->updateAddRemoveButton(addButtonEnabled, removeButtonEnabled); } } void SieveConditionWidgetLister::reconnectWidget(SieveConditionWidget *w) { connect(w, &SieveConditionWidget::addWidget, this, &SieveConditionWidgetLister::slotAddWidget, Qt::UniqueConnection); connect(w, &SieveConditionWidget::removeWidget, this, &SieveConditionWidgetLister::slotRemoveWidget, Qt::UniqueConnection); connect(w, &SieveConditionWidget::valueChanged, this, &SieveConditionWidgetLister::valueChanged, Qt::UniqueConnection); } void SieveConditionWidgetLister::clearWidget(QWidget *aWidget) { if (aWidget) { SieveConditionWidget *widget = static_cast(aWidget); widget->clear(); } Q_EMIT valueChanged(); } QWidget *SieveConditionWidgetLister::createWidget(QWidget *parent) { SieveConditionWidget *w = new SieveConditionWidget(mSieveGraphicalModeWidget, parent); reconnectWidget(w); return w; } void SieveConditionWidgetLister::generatedScript(QString &script, int &numberOfCondition, QStringList &requireModules, bool inForEveryPartLoop) { const QList widgetList = widgets(); QList::ConstIterator wIt = widgetList.constBegin(); QList::ConstIterator wEnd = widgetList.constEnd(); bool wasFirst = true; for (; wIt != wEnd; ++wIt) { QString condition; SieveConditionWidget *w = qobject_cast(*wIt); w->generatedScript(condition, requireModules, inForEveryPartLoop); if (!condition.isEmpty()) { if (!wasFirst) { if (inForEveryPartLoop) { script += AutoCreateScriptUtil::indentation(); } script += QLatin1String(", "); } script += condition; wasFirst = false; ++numberOfCondition; } } } int SieveConditionWidgetLister::conditionNumber() const { return widgets().count(); } void SieveConditionWidgetLister::loadTest(QXmlStreamReader &element, bool notCondition, QString &error) { if (notCondition) { element.readNextStartElement(); } if (element.attributes().hasAttribute(QLatin1String("name"))) { const QString conditionName = element.attributes().value(QLatin1String("name")).toString(); SieveConditionWidget *w = qobject_cast(widgets().constLast()); w->setCondition(conditionName, element, notCondition, error); } if (notCondition) { element.skipCurrentElement(); } } void SieveConditionWidgetLister::loadScript(QXmlStreamReader &element, bool uniqTest, bool notCondition, QString &error) { if (uniqTest) { loadTest(element, notCondition, error); } else { bool firstCondition = true; if (notCondition) { element.readNextStartElement(); } while (element.readNextStartElement()) { const QStringRef tagName = element.name(); if (tagName == QLatin1String("testlist")) { while (element.readNextStartElement()) { const QStringRef testTagName = element.name(); if (testTagName == QLatin1String("test")) { if (element.attributes().hasAttribute(QLatin1String("name"))) { QString conditionName = element.attributes().value(QLatin1String("name")).toString(); if (firstCondition) { firstCondition = false; } else { addWidgetAfterThisWidget(widgets().constLast()); } SieveConditionWidget *w = qobject_cast(widgets().constLast()); if (conditionName == QLatin1String("not")) { notCondition = true; element.readNextStartElement(); if (element.attributes().hasAttribute(QLatin1String("name"))) { conditionName = element.attributes().value(QLatin1String("name")).toString(); } w->setCondition(conditionName, element, notCondition, error); element.skipCurrentElement(); } else { notCondition = false; w->setCondition(conditionName, element, notCondition, error); } } } else if (testTagName == QLatin1String("crlf")) { element.skipCurrentElement(); //nothing } else if (testTagName == QLatin1String("comment")) { qDebug() << "Need to implement comment here "; element.skipCurrentElement(); //nothing //implement in the future ? } else { qCDebug(LIBKSIEVE_LOG) << " SieveConditionWidgetLister::loadScript unknown condition tag: " << testTagName; } } } } } } diff --git a/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.h b/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.h index d1e294e..1f1b6fd 100644 --- a/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.h +++ b/src/ksieveui/autocreatescripts/sieveconditionwidgetlister.h @@ -1,103 +1,100 @@ /* Copyright (C) 2013-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef SIEVECONDITIONWIDGETLISTER_H #define SIEVECONDITIONWIDGETLISTER_H #include class QPushButton; class QGridLayout; class QXmlStreamReader; class QToolButton; - -namespace PimCommon { -class MinimumComboBox; -} +class QComboBox; namespace KSieveUi { class SieveCondition; class SieveHelpButton; class SieveEditorGraphicalModeWidget; class SieveConditionWidget : public QWidget { Q_OBJECT public: explicit SieveConditionWidget(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent); ~SieveConditionWidget(); void updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled); void generatedScript(QString &script, QStringList &required, bool inForEveryPartLoop); void setCondition(const QString &conditionName, QXmlStreamReader &element, bool notCondition, QString &error); void clear(); Q_SIGNALS: void addWidget(QWidget *w); void removeWidget(QWidget *w); void valueChanged(); private: void slotAddWidget(); void slotRemoveWidget(); void slotConditionChanged(int index); void slotHelp(); void slotAddComment(); void initWidget(); void setFilterCondition(QWidget *widget); QList mConditionList; QPushButton *mAdd = nullptr; QPushButton *mRemove = nullptr; - PimCommon::MinimumComboBox *mComboBox = nullptr; + QComboBox *mComboBox = nullptr; QGridLayout *mLayout = nullptr; SieveHelpButton *mHelpButton = nullptr; QToolButton *mCommentButton = nullptr; SieveEditorGraphicalModeWidget *mSieveGraphicalModeWidget = nullptr; }; class SieveConditionWidgetLister : public KPIM::KWidgetLister { Q_OBJECT public: explicit SieveConditionWidgetLister(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent = nullptr); ~SieveConditionWidgetLister(); void generatedScript(QString &script, int &numberOfCondition, QStringList &requireModules, bool inForEveryPartLoop); int conditionNumber() const; void loadScript(QXmlStreamReader &element, bool uniqTest, bool notCondition, QString &error); Q_SIGNALS: void valueChanged(); public Q_SLOTS: void slotAddWidget(QWidget *w); void slotRemoveWidget(QWidget *w); protected: void clearWidget(QWidget *aWidget) override; QWidget *createWidget(QWidget *parent) override; private: void loadTest(QXmlStreamReader &e, bool notCondition, QString &error); void reconnectWidget(SieveConditionWidget *w); void updateAddRemoveButton(); SieveEditorGraphicalModeWidget *mSieveGraphicalModeWidget = nullptr; }; } #endif // SIEVECONDITIONWIDGETLISTER_H