sievescriptlistbox.cpp 30.8 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2017 Laurent Montel <montel@kde.org>
3

Laurent Montel's avatar
Laurent Montel committed
4 5 6 7
   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.
8

Laurent Montel's avatar
Laurent Montel committed
9 10 11 12
   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.
13

Laurent Montel's avatar
Laurent Montel committed
14 15 16 17
   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.
18
*/
Laurent Montel's avatar
Laurent Montel committed
19

20 21 22 23 24 25
#include "sievescriptlistbox.h"
#include "sievescriptdescriptiondialog.h"
#include "sieveglobalvariablewidget.h"
#include "sieveforeverypartwidget.h"
#include "sievescriptpage.h"
#include "sieveincludewidget.h"
26

27 28 29 30 31 32 33
#include <QHBoxLayout>
#include <KMessageBox>
#include <KLocalizedString>
#include <QInputDialog>
#include <QPushButton>
#include <KIconLoader>
#include <QIcon>
Laurent Montel's avatar
Laurent Montel committed
34
#include <QXmlStreamWriter>
35 36 37 38 39 40 41

#include <QVBoxLayout>
#include <QListWidget>
#include <QPointer>
#include <QDomElement>
#include "libksieve_debug.h"

Laurent Montel's avatar
Laurent Montel committed
42
namespace {
43 44 45 46 47 48 49 50 51
inline const QString defaultScriptName()
{
    return QStringLiteral("SCRIPTNAME: ");
}
}

using namespace KSieveUi;

SieveScriptListItem::SieveScriptListItem(const QString &text, QListWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
52 53
    : QListWidgetItem(text, parent)
    , mScriptPage(nullptr)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
}

SieveScriptListItem::~SieveScriptListItem()
{
}

void SieveScriptListItem::setDescription(const QString &desc)
{
    mDescription = desc;
}

QString SieveScriptListItem::description() const
{
    return mDescription;
}

SieveScriptPage *SieveScriptListItem::scriptPage() const
{
    return mScriptPage;
}

void SieveScriptListItem::setScriptPage(SieveScriptPage *page)
{
    mScriptPage = page;
}

QString SieveScriptListItem::generatedScript(QStringList &requires) const
{
    QString script;
84 85 86 87 88 89 90 91 92
    if (!mDescription.trimmed().isEmpty()) {
        const QStringList commentList = mDescription.split(QLatin1Char('\n'));
        for (const QString &str : commentList) {
            if (str.isEmpty()) {
                script += QLatin1Char('\n');
            } else {
                script += QLatin1Char('#') + str + QLatin1Char('\n');
            }
        }
93 94 95 96 97 98 99 100
    }
    if (mScriptPage) {
        mScriptPage->generatedScript(script, requires);
    }
    return script;
}

SieveScriptListBox::SieveScriptListBox(const QString &title, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
101 102 103
    : QGroupBox(title, parent)
    , mSieveGraphicalModeWidget(nullptr)
    , mScriptNumber(0)
104
{
Laurent Montel's avatar
Laurent Montel committed
105 106
    QVBoxLayout *layout = new QVBoxLayout(this);
    mSieveListScript = new QListWidget(this);
107 108 109 110 111 112 113 114
    layout->addWidget(mSieveListScript);

    //----------- the first row of buttons
    QWidget *hb = new QWidget(this);
    QHBoxLayout *hbHBoxLayout = new QHBoxLayout(hb);
    hbHBoxLayout->setMargin(0);
    hbHBoxLayout->setSpacing(4);

Laurent Montel's avatar
Laurent Montel committed
115
    mBtnTop = new QPushButton(hb);
116 117 118 119 120
    hbHBoxLayout->addWidget(mBtnTop);
    mBtnTop->setIcon(QIcon::fromTheme(QStringLiteral("go-top")));
    mBtnTop->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnTop->setMinimumSize(mBtnTop->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
121
    mBtnUp = new QPushButton(hb);
122 123 124 125 126
    hbHBoxLayout->addWidget(mBtnUp);
    mBtnUp->setAutoRepeat(true);
    mBtnUp->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
    mBtnUp->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnUp->setMinimumSize(mBtnUp->sizeHint() * 1.2);
Laurent Montel's avatar
Laurent Montel committed
127
    mBtnDown = new QPushButton(hb);
128 129 130 131 132 133
    hbHBoxLayout->addWidget(mBtnDown);
    mBtnDown->setAutoRepeat(true);
    mBtnDown->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
    mBtnDown->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnDown->setMinimumSize(mBtnDown->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
134
    mBtnBottom = new QPushButton(hb);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    hbHBoxLayout->addWidget(mBtnBottom);
    mBtnBottom->setIcon(QIcon::fromTheme(QStringLiteral("go-bottom")));
    mBtnBottom->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnBottom->setMinimumSize(mBtnBottom->sizeHint() * 1.2);

    mBtnUp->setToolTip(i18nc("Move selected filter up.", "Up"));
    mBtnDown->setToolTip(i18nc("Move selected filter down.", "Down"));
    mBtnTop->setToolTip(i18nc("Move selected filter to the top.", "Top"));
    mBtnBottom->setToolTip(i18nc("Move selected filter to the bottom.", "Bottom"));

    layout->addWidget(hb);

    hb = new QWidget(this);
    hbHBoxLayout = new QHBoxLayout(hb);
    hbHBoxLayout->setMargin(0);
    hbHBoxLayout->setSpacing(4);

Laurent Montel's avatar
Laurent Montel committed
152
    mBtnNew = new QPushButton(hb);
153 154 155 156 157 158
    hbHBoxLayout->addWidget(mBtnNew);
    mBtnNew->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
    mBtnNew->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnNew->setToolTip(i18n("New Script"));
    mBtnNew->setMinimumSize(mBtnNew->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
159
    mBtnDelete = new QPushButton(hb);
160 161 162 163 164 165
    hbHBoxLayout->addWidget(mBtnDelete);
    mBtnDelete->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
    mBtnDelete->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnDelete->setToolTip(i18n("Delete Script"));
    mBtnDelete->setMinimumSize(mBtnDelete->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
166
    mBtnRename = new QPushButton(hb);
167
    mBtnRename->setToolTip(i18n("Rename Script"));
Laurent Montel's avatar
Laurent Montel committed
168 169 170
    mBtnRename->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
    mBtnRename->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnRename->setMinimumSize(mBtnRename->sizeHint() * 1.2);
171 172
    hbHBoxLayout->addWidget(mBtnRename);

Laurent Montel's avatar
Laurent Montel committed
173
    mBtnDescription = new QPushButton(hb);
174
    mBtnDescription->setToolTip(i18n("Edit Script Description"));
Laurent Montel's avatar
Laurent Montel committed
175 176 177 178
    mBtnDescription->setIcon(QIcon::fromTheme(QStringLiteral("edit-comment")));
    mBtnDescription->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    mBtnDescription->setMinimumSize(mBtnDescription->sizeHint() * 1.2);

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    hbHBoxLayout->addWidget(mBtnDescription);

    layout->addWidget(hb);

    connect(mBtnNew, &QPushButton::clicked, this, &SieveScriptListBox::slotNew);
    connect(mBtnDelete, &QPushButton::clicked, this, &SieveScriptListBox::slotDelete);
    connect(mBtnRename, &QPushButton::clicked, this, &SieveScriptListBox::slotRename);
    connect(mBtnDescription, &QPushButton::clicked, this, &SieveScriptListBox::slotEditDescription);

    connect(mBtnUp, &QPushButton::clicked, this, &SieveScriptListBox::slotUp);
    connect(mBtnDown, &QPushButton::clicked, this, &SieveScriptListBox::slotDown);
    connect(mBtnTop, &QPushButton::clicked, this, &SieveScriptListBox::slotTop);
    connect(mBtnBottom, &QPushButton::clicked, this, &SieveScriptListBox::slotBottom);

    connect(mSieveListScript, &QListWidget::itemSelectionChanged, this, &SieveScriptListBox::updateButtons);
    connect(mSieveListScript, &QListWidget::itemActivated, this, &SieveScriptListBox::slotItemActived);
    connect(mSieveListScript, &QListWidget::itemDoubleClicked, this, &SieveScriptListBox::slotEditDescription);
    updateButtons();
}

SieveScriptListBox::~SieveScriptListBox()
{
}

Laurent Montel's avatar
Laurent Montel committed
203 204 205 206 207
void SieveScriptListBox::setSieveEditorGraphicalModeWidget(SieveEditorGraphicalModeWidget *graphicalModeWidget)
{
    mSieveGraphicalModeWidget = graphicalModeWidget;
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
void SieveScriptListBox::slotItemActived(QListWidgetItem *item)
{
    if (item) {
        SieveScriptListItem *itemScript = static_cast<SieveScriptListItem *>(item);
        Q_EMIT activatePage(itemScript->scriptPage());
    }
}

void SieveScriptListBox::updateButtons()
{
    const int currentIndex = mSieveListScript->currentRow();
    const bool theFirst = (currentIndex == 0);
    const int numberOfElement(mSieveListScript->count());
    const bool theLast = (currentIndex >= numberOfElement - 1);

    const QList<QListWidgetItem *> lst = mSieveListScript->selectedItems();
    mBtnDelete->setEnabled(!lst.isEmpty());
    mBtnRename->setEnabled(lst.count() == 1);
    mBtnDescription->setEnabled(lst.count() == 1);
    mBtnBottom->setEnabled(!lst.isEmpty() && !theLast);
    mBtnTop->setEnabled(!lst.isEmpty() && !theFirst);
    mBtnDown->setEnabled(!lst.isEmpty() && !theLast);
    mBtnUp->setEnabled(!lst.isEmpty() && !theFirst);
}

SieveScriptPage *SieveScriptListBox::createNewScript(const QString &newName, const QString &description)
{
    SieveScriptListItem *item = new SieveScriptListItem(newName, mSieveListScript);
    item->setDescription(description);
Laurent Montel's avatar
Laurent Montel committed
237
    SieveScriptPage *page = new SieveScriptPage(mSieveGraphicalModeWidget);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    item->setScriptPage(page);
    Q_EMIT addNewPage(page);
    Q_EMIT enableButtonOk(true);
    mSieveListScript->setCurrentItem(item);
    updateButtons();
    return page;
}

void SieveScriptListBox::slotNew()
{
    const QString newName = QInputDialog::getText(this, i18n("New Script"), i18n("Add new name:"));
    if (!newName.trimmed().isEmpty()) {
        createNewScript(newName);
        Q_EMIT valueChanged();
    }
}

void SieveScriptListBox::slotDelete()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
        if (KMessageBox::warningYesNo(this, i18n("Do you want to delete \"%1\" script?", item->text()), i18n("Delete script")) == KMessageBox::Yes) {
            SieveScriptListItem *itemScript = static_cast<SieveScriptListItem *>(item);
            Q_EMIT removePage(itemScript->scriptPage());
            delete item;
            Q_EMIT enableButtonOk(mSieveListScript->count() > 0);
            updateButtons();
            Q_EMIT valueChanged();
        }
    }
}

void SieveScriptListBox::slotRename()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
Laurent Montel's avatar
Laurent Montel committed
274 275 276
        QString newName = QInputDialog::getText(this, i18n("Rename"), i18n("Add new name:"), QLineEdit::Normal, item->text());
        newName = newName.trimmed();
        if (!newName.isEmpty()) {
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            item->setText(newName);
            Q_EMIT valueChanged();
        }
    }
}

void SieveScriptListBox::slotEditDescription()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
        SieveScriptListItem *sieveItem = static_cast<SieveScriptListItem *>(item);
        QPointer<SieveScriptDescriptionDialog> dlg = new SieveScriptDescriptionDialog(this);
        dlg->setDescription(sieveItem->description());
        if (dlg->exec()) {
            sieveItem->setDescription(dlg->description());
            Q_EMIT valueChanged();
        }
        delete dlg;
    }
}

void SieveScriptListBox::slotTop()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
        const int currentIndex = mSieveListScript->currentRow();
        if (currentIndex != 0) {
            item = mSieveListScript->takeItem(currentIndex);
            mSieveListScript->insertItem(0, item);
            mSieveListScript->setCurrentItem(item);
            Q_EMIT valueChanged();
        }
    }
}

void SieveScriptListBox::slotBottom()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
        const int currentIndex = mSieveListScript->currentRow();
        if (currentIndex != mSieveListScript->count() - 1) {
            item = mSieveListScript->takeItem(currentIndex);
            mSieveListScript->insertItem(mSieveListScript->count(), item);
            mSieveListScript->setCurrentItem(item);
            Q_EMIT valueChanged();
        }
    }
}

void SieveScriptListBox::slotDown()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
        const int currentIndex = mSieveListScript->currentRow();
        if (currentIndex < mSieveListScript->count() - 1) {
            item = mSieveListScript->takeItem(currentIndex);
            mSieveListScript->insertItem(currentIndex + 1, item);
            mSieveListScript->setCurrentItem(item);
            Q_EMIT valueChanged();
        }
    }
}

void SieveScriptListBox::slotUp()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
        const int currentIndex = mSieveListScript->currentRow();
        if (currentIndex >= 1) {
            item = mSieveListScript->takeItem(currentIndex);
            mSieveListScript->insertItem(currentIndex - 1, item);
            mSieveListScript->setCurrentItem(item);
            Q_EMIT valueChanged();
        }
    }
}

QString SieveScriptListBox::generatedScript(QString &requires) const
{
    QString resultScript;
    QStringList lstRequires;
    const int numberOfScripts(mSieveListScript->count());
    for (int i = 0; i < numberOfScripts; ++i) {
        SieveScriptListItem *item = static_cast<SieveScriptListItem *>(mSieveListScript->item(i));
        if (i != 0) {
            resultScript += QLatin1String("\n\n");
        }
        resultScript += QLatin1Char('#') + defaultScriptName() + item->text() + QLatin1Char('\n');
        resultScript += item->generatedScript(lstRequires);
    }
367 368 369
    if (!resultScript.isEmpty()) {
        resultScript += QLatin1Char('\n');
    }
370 371

    QStringList endRequires;
Laurent Montel's avatar
Laurent Montel committed
372
    for (const QString &r : qAsConst(lstRequires)) {
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        if (!endRequires.contains(r)) {
            endRequires.append(r);
            requires += QStringLiteral("require \"%1\";\n").arg(r);
        }
    }

    return resultScript;
}

void SieveScriptListBox::clear()
{
    mScriptNumber = 0;
    Q_EMIT enableButtonOk(false);
    //Clear tabpage
    mSieveListScript->clear();
    updateButtons();
}

Laurent Montel's avatar
Laurent Montel committed
391
void SieveScriptListBox::loadScript(const QString &doc, QString &error)
392 393
{
    clear();
Laurent Montel's avatar
Laurent Montel committed
394 395 396 397 398 399 400 401
    QXmlStreamReader streamReader(doc);
    if (streamReader.readNextStartElement()) {
        if (streamReader.name() == QLatin1String("script")) {
            SieveScriptPage *currentPage = nullptr;
            ParseSieveScriptTypeBlock typeBlock = TypeUnknown;
            loadBlock(streamReader, currentPage, typeBlock, error);
        }
    }
402 403
}

Laurent Montel's avatar
Laurent Montel committed
404
void SieveScriptListBox::loadBlock(QXmlStreamReader &n, SieveScriptPage *currentPage, ParseSieveScriptTypeBlock typeBlock, QString &error)
405
{
406 407 408 409
    QString scriptName;
    QString comment;
    bool hasCreatedAIfBlock = false;
    bool previousElementWasAComment = false;
Laurent Montel's avatar
Laurent Montel committed
410
    while (n.readNextStartElement()) {
Laurent Montel's avatar
Laurent Montel committed
411
        const QStringRef tagName = n.name();
Laurent Montel's avatar
Laurent Montel committed
412
        qDebug()<<"SieveScriptListBox::loadBlock*********** tagName "<<tagName;
Laurent Montel's avatar
Laurent Montel committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
        if (tagName == QLatin1String("control")) {
            previousElementWasAComment = false;
            //Create a new page when before it was "onlyactions"
            if (typeBlock == TypeBlockAction) {
                currentPage = nullptr;
            }
            if (n.attributes().hasAttribute(QStringLiteral("name"))) {
                const QString controlType =n.attributes().value(QStringLiteral("name")).toString();
                qDebug() <<" SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSs"<< controlType;
                //qCDebug(LIBKSIEVE_LOG)<<" controlType"<<controlType;
                if (controlType == QLatin1String("if")) {
                    typeBlock = TypeBlockIf;
                    if (!currentPage || hasCreatedAIfBlock) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                    }
                    hasCreatedAIfBlock = true;
                    comment.clear();
                    qDebug() << " IF BLOCK";
                    currentPage->blockIfWidget()->loadScript(n, false, error);
                } else if (controlType == QLatin1String("elsif")) {
                    typeBlock = TypeBlockElsif;
                    if (!currentPage) {
                        qCDebug(LIBKSIEVE_LOG) << " script is not correct missing if block";
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                    }
                    SieveScriptBlockWidget *blockWidget = currentPage->addScriptBlock(KSieveUi::SieveWidgetPageAbstract::BlockElsIf);
                    if (blockWidget) {
                        blockWidget->loadScript(n, false, error);
                    }
                } else if (controlType == QLatin1String("else")) {
                    typeBlock = TypeBlockElse;
                    if (!currentPage) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                        qCDebug(LIBKSIEVE_LOG) << " script is not correct missing if block";
                    }
                    SieveScriptBlockWidget *blockWidget = currentPage->addScriptBlock(KSieveUi::SieveWidgetPageAbstract::BlockElse);
                    if (blockWidget) {
                        blockWidget->loadScript(n, false, error);
                    }
                    //We are sure that we can't have another elsif
453
                    currentPage = nullptr;
Laurent Montel's avatar
Laurent Montel committed
454 455 456 457
                } else if (controlType == QLatin1String("foreverypart")) {
                    typeBlock = TypeBlockForeachBlock;
                    if (!currentPage) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
458
                        comment.clear();
Laurent Montel's avatar
Laurent Montel committed
459 460 461
                    }
                    if (currentPage->forEveryPartWidget()) {
                        currentPage->forEveryPartWidget()->loadScript(n, error);
462
                    } else {
Laurent Montel's avatar
Laurent Montel committed
463
                        error += i18n("forEveryPart is not supported by your server") + QLatin1Char('\n');
464
                    }
Laurent Montel's avatar
Laurent Montel committed
465 466 467 468
                    //TODO verify it.
#ifdef QDOMELEMENT_FIXME
                    QDomNode block = e.firstChildElement(QStringLiteral("block")).firstChild();
                    loadBlock(block, currentPage, typeBlock, error);
Laurent Montel's avatar
Laurent Montel committed
469
#endif
Laurent Montel's avatar
Laurent Montel committed
470 471 472 473
                } else if (controlType == QLatin1String("require")) {
                    qDebug() << " REQUIRES";
                    n.skipCurrentElement();
                    //Nothing. autogenerated
474
                } else {
Laurent Montel's avatar
Laurent Montel committed
475
                    qCDebug(LIBKSIEVE_LOG) << " unknown controlType :" << controlType;
476
                }
Laurent Montel's avatar
Laurent Montel committed
477 478
            }
        } else if (tagName == QLatin1String("comment")) {
Laurent Montel's avatar
Laurent Montel committed
479
            qDebug() << "SieveScriptListBox::loadBlock( COMMZE?T ";
Laurent Montel's avatar
Laurent Montel committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
            previousElementWasAComment = true;
#ifdef QDOMELEMENT_FIXME
            if (e.hasAttribute(QStringLiteral("hash"))) {
                //TODO
            } else if (e.hasAttribute(QStringLiteral("bracket"))) {
                //TODO
            }
#endif
            QString str(n.readElementText());
            if (str.contains(defaultScriptName())) {
                scriptName = str.remove(defaultScriptName());
            } else {
                if (!comment.isEmpty()) {
                    comment += QLatin1Char('\n');
                }
                comment += str;
            }
        } else if (tagName == QLatin1String("action")) {
Laurent Montel's avatar
Laurent Montel committed
498
            qDebug() << "SieveScriptListBox::loadBlock(QXmlStreamReader &n, SieveScriptPage *currentPage, ParseSieveScriptTypeBlock typeBlock, QString &error) ACTYION";
Laurent Montel's avatar
Laurent Montel committed
499 500 501
            previousElementWasAComment = false;
            if (n.attributes().hasAttribute(QStringLiteral("name"))) {
                const QString actionName = n.attributes().value(QStringLiteral("name")).toString();
Laurent Montel's avatar
Laurent Montel committed
502
                qDebug() << "===================================actionName "<<actionName;
Laurent Montel's avatar
Laurent Montel committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
                if (actionName == QLatin1String("include")) {
                    if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                        comment.clear();
                    }
                    typeBlock = TypeBlockInclude;
                    if (currentPage->includeWidget()) {
                        currentPage->includeWidget()->loadScript(n, error);
                    } else {
                        qCDebug(LIBKSIEVE_LOG) << " include not supported";
                    }
                } else if (actionName == QLatin1String("global")) {
                    if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                        comment.clear();
                    }
                    typeBlock = TypeBlockGlobal;
                    if (currentPage->globalVariableWidget()) {
                        currentPage->globalVariableWidget()->loadScript(n, error);
                    } else {
                        qCDebug(LIBKSIEVE_LOG) << " globalVariable not supported";
                    }
                } else if (actionName == QLatin1String("set") && (typeBlock == TypeBlockGlobal)) {
                    if (currentPage->globalVariableWidget()) {
                        if (!currentPage->globalVariableWidget()->loadSetVariable(n, error)) {
                            qCDebug(LIBKSIEVE_LOG) << "It's not a global variable";
                            if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                                currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
531
                            }
Laurent Montel's avatar
Laurent Montel committed
532 533 534
                            typeBlock = TypeBlockAction;
                            comment.clear();
                            currentPage->blockIfWidget()->loadScript(n, true, error);
535 536
                        }
                    } else {
Laurent Montel's avatar
Laurent Montel committed
537
                        qCDebug(LIBKSIEVE_LOG) << " set not supported";
538
                    }
Laurent Montel's avatar
Laurent Montel committed
539 540 541 542 543 544 545
                } else {
                    if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                    }
                    typeBlock = TypeBlockAction;
                    comment.clear();
                    currentPage->blockIfWidget()->loadScript(n, true, error);
546 547
                }
            }
Laurent Montel's avatar
Laurent Montel committed
548
        } else if (tagName == QLatin1String("crlf")) {
Laurent Montel's avatar
Laurent Montel committed
549
            qDebug() << "SieveScriptListBox::loadBlock********************************************** " << tagName;
Laurent Montel's avatar
Laurent Montel committed
550 551 552 553 554
            //If it was a comment previously you will create a \n
            if (previousElementWasAComment) {
                comment += QLatin1Char('\n');
            }
            n.skipCurrentElement();
Laurent Montel's avatar
Laurent Montel committed
555
            qDebug() << "SieveScriptListBox::loadBlock comment "<<comment;
Laurent Montel's avatar
Laurent Montel committed
556 557 558
        } else {
            qCDebug(LIBKSIEVE_LOG) << " unknown tagname" << tagName;
        }
Laurent Montel's avatar
Laurent Montel committed
559 560
    }
#ifdef REMOVE_QDOMELEMENT
561 562 563
    QString scriptName;
    QString comment;
    bool hasCreatedAIfBlock = false;
564
    bool previousElementWasAComment = false;
565 566 567 568 569 570
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull()) {
            const QString tagName = e.tagName();
            //qCDebug(LIBKSIEVE_LOG)<<" tagName "<<tagName;
            if (tagName == QLatin1String("control")) {
571
                previousElementWasAComment = false;
572 573
                //Create a new page when before it was "onlyactions"
                if (typeBlock == TypeBlockAction) {
Laurent Montel's avatar
Laurent Montel committed
574
                    currentPage = nullptr;
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
                }
                if (e.hasAttribute(QStringLiteral("name"))) {
                    const QString controlType = e.attribute(QStringLiteral("name"));
                    //qCDebug(LIBKSIEVE_LOG)<<" controlType"<<controlType;
                    if (controlType == QLatin1String("if")) {
                        typeBlock = TypeBlockIf;
                        if (!currentPage || hasCreatedAIfBlock) {
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                        }
                        hasCreatedAIfBlock = true;
                        comment.clear();
                        currentPage->blockIfWidget()->loadScript(e, false, error);
                    } else if (controlType == QLatin1String("elsif")) {
                        typeBlock = TypeBlockElsif;
                        if (!currentPage) {
                            qCDebug(LIBKSIEVE_LOG) << " script is not correct missing if block";
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                        }
                        SieveScriptBlockWidget *blockWidget = currentPage->addScriptBlock(KSieveUi::SieveWidgetPageAbstract::BlockElsIf);
                        if (blockWidget) {
                            blockWidget->loadScript(e, false, error);
                        }
                    } else if (controlType == QLatin1String("else")) {
                        typeBlock = TypeBlockElse;
                        if (!currentPage) {
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                            qCDebug(LIBKSIEVE_LOG) << " script is not correct missing if block";
                        }
                        SieveScriptBlockWidget *blockWidget = currentPage->addScriptBlock(KSieveUi::SieveWidgetPageAbstract::BlockElse);
                        if (blockWidget) {
                            blockWidget->loadScript(e, false, error);
                        }
                        //We are sure that we can't have another elsif
Laurent Montel's avatar
Laurent Montel committed
608
                        currentPage = nullptr;
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
                    } else if (controlType == QLatin1String("foreverypart")) {
                        typeBlock = TypeBlockForeachBlock;
                        if (!currentPage) {
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                            comment.clear();
                        }
                        if (currentPage->forEveryPartWidget()) {
                            currentPage->forEveryPartWidget()->loadScript(e, error);
                        } else {
                            error += i18n("forEveryPart is not supported by your server") + QLatin1Char('\n');
                        }
                        //TODO verify it.
                        QDomNode block = e.firstChildElement(QStringLiteral("block")).firstChild();
                        loadBlock(block, currentPage, typeBlock, error);
                    } else if (controlType == QLatin1String("require")) {
                        //Nothing. autogenerated
                    } else {
                        qCDebug(LIBKSIEVE_LOG) << " unknown controlType :" << controlType;
                    }
                }
            } else if (tagName == QLatin1String("comment")) {
630 631 632 633 634 635
                previousElementWasAComment = true;
                if (e.hasAttribute(QStringLiteral("hash"))) {
                    //TODO
                } else if (e.hasAttribute(QStringLiteral("bracket"))) {
                    //TODO
                }
636 637 638 639 640 641 642 643 644 645
                QString str(e.text());
                if (str.contains(defaultScriptName())) {
                    scriptName = str.remove(defaultScriptName());
                } else {
                    if (!comment.isEmpty()) {
                        comment += QLatin1Char('\n');
                    }
                    comment += e.text();
                }
            } else if (tagName == QLatin1String("action")) {
646
                previousElementWasAComment = false;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
                if (e.hasAttribute(QStringLiteral("name"))) {
                    const QString actionName = e.attribute(QStringLiteral("name"));
                    if (actionName == QLatin1String("include")) {
                        if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                            comment.clear();
                        }
                        typeBlock = TypeBlockInclude;
                        if (currentPage->includeWidget()) {
                            currentPage->includeWidget()->loadScript(e, error);
                        } else {
                            qCDebug(LIBKSIEVE_LOG) << " include not supported";
                        }
                    } else if (actionName == QLatin1String("global")) {
                        if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                            comment.clear();
                        }
                        typeBlock = TypeBlockGlobal;
                        if (currentPage->globalVariableWidget()) {
                            currentPage->globalVariableWidget()->loadScript(e, error);
                        } else {
                            qCDebug(LIBKSIEVE_LOG) << " globalVariable not supported";
                        }
                    } else if (actionName == QLatin1String("set") && (typeBlock == TypeBlockGlobal)) {
                        if (currentPage->globalVariableWidget()) {
673 674 675 676 677 678 679 680 681
                            if (!currentPage->globalVariableWidget()->loadSetVariable(e, error)) {
                                qCDebug(LIBKSIEVE_LOG) << "It's not a global variable";
                                if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                                    currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                                }
                                typeBlock = TypeBlockAction;
                                comment.clear();
                                currentPage->blockIfWidget()->loadScript(e, true, error);
                            }
682
                        } else {
Laurent Montel's avatar
Laurent Montel committed
683
                            qCDebug(LIBKSIEVE_LOG) << " set not supported";
684 685 686 687 688 689 690 691 692 693 694
                        }
                    } else {
                        if (!currentPage || (typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
                            currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                        }
                        typeBlock = TypeBlockAction;
                        comment.clear();
                        currentPage->blockIfWidget()->loadScript(e, true, error);
                    }
                }
            } else if (tagName == QLatin1String("crlf")) {
695 696 697 698
                //If it was a comment previously you will create a \n
                if (previousElementWasAComment) {
                    comment += QLatin1Char('\n');
                }
699 700 701 702 703 704
            } else {
                qCDebug(LIBKSIEVE_LOG) << " unknown tagname" << tagName;
            }
        }
        n = n.nextSibling();
    }
Laurent Montel's avatar
Laurent Montel committed
705
#endif
706 707 708 709 710 711 712 713
}

QString SieveScriptListBox::createUniqName()
{
    const QString pattern = i18n("Script part %1", mScriptNumber);
    ++mScriptNumber;
    return pattern;
}