sievescriptlistbox.cpp 22.8 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   SPDX-FileCopyrightText: 2013-2022 Laurent Montel <montel@kde.org>
3

4
   SPDX-License-Identifier: LGPL-2.0-or-later
5
*/
Laurent Montel's avatar
Laurent Montel committed
6

7
#include "sievescriptlistbox.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "sieveeditorgraphicalmodewidget.h"
9
#include "sieveforeverypartwidget.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include "sieveglobalvariablewidget.h"
11
#include "sieveincludewidget.h"
Laurent Montel's avatar
Laurent Montel committed
12
13
#include "sievescriptdescriptiondialog.h"
#include "sievescriptpage.h"
14
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
15
16
17
#include <KMessageBox>
#include <QHBoxLayout>
#include <QIcon>
18
19
#include <QInputDialog>
#include <QPushButton>
Laurent Montel's avatar
Laurent Montel committed
20
#include <QXmlStreamReader>
21

Laurent Montel's avatar
Laurent Montel committed
22
#include "libksieve_debug.h"
23
#include <QListWidget>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QMenu>
Laurent Montel's avatar
Laurent Montel committed
25
26
#include <QPointer>
#include <QVBoxLayout>
27

Laurent Montel's avatar
Laurent Montel committed
28
29
namespace
{
30
31
32
33
34
35
36
37
38
inline const QString defaultScriptName()
{
    return QStringLiteral("SCRIPTNAME: ");
}
}

using namespace KSieveUi;

SieveScriptListItem::SieveScriptListItem(const QString &text, QListWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
39
    : QListWidgetItem(text, parent)
40
41
42
{
}

Laurent Montel's avatar
Laurent Montel committed
43
SieveScriptListItem::~SieveScriptListItem() = default;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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;
}

Laurent Montel's avatar
Laurent Montel committed
65
QString SieveScriptListItem::generatedScript(QStringList &required) const
66
67
{
    QString script;
68
    if (!mDescription.trimmed().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
69
70
71
72
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
        const QVector<QStringView> commentList = QStringView(mDescription).split(QLatin1Char('\n'));
        for (const QStringView str : commentList) {
#else
Laurent Montel's avatar
Laurent Montel committed
73
74
        const QVector<QStringRef> commentList = mDescription.splitRef(QLatin1Char('\n'));
        for (const QStringRef &str : commentList) {
Laurent Montel's avatar
Laurent Montel committed
75
#endif
76
77
78
79
80
81
            if (str.isEmpty()) {
                script += QLatin1Char('\n');
            } else {
                script += QLatin1Char('#') + str + QLatin1Char('\n');
            }
        }
82
83
    }
    if (mScriptPage) {
Laurent Montel's avatar
Laurent Montel committed
84
        mScriptPage->generatedScript(script, required);
85
86
87
88
89
    }
    return script;
}

SieveScriptListBox::SieveScriptListBox(const QString &title, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
90
    : QGroupBox(title, parent)
91
    , mSieveListScript(new QListWidget(this))
92
{
93
    auto layout = new QVBoxLayout(this);
94
95
96
    layout->setObjectName(QStringLiteral("layout"));
    mSieveListScript->setObjectName(QStringLiteral("mSieveListScript"));
    mSieveListScript->setDragDropMode(QAbstractItemView::InternalMove);
Laurent Montel's avatar
Laurent Montel committed
97
98
    mSieveListScript->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(mSieveListScript, &QListWidget::customContextMenuRequested, this, &SieveScriptListBox::slotCustomMenuRequested);
99
100
101
    layout->addWidget(mSieveListScript);

    //----------- the first row of buttons
Laurent Montel's avatar
Laurent Montel committed
102
    auto hb = new QWidget(this);
103
    auto hbHBoxLayout = new QHBoxLayout(hb);
Laurent Montel's avatar
Laurent Montel committed
104
    hbHBoxLayout->setContentsMargins({});
105
106
    hbHBoxLayout->setSpacing(4);

Laurent Montel's avatar
Laurent Montel committed
107
    mBtnTop = new QPushButton(hb);
108
109
110
111
    hbHBoxLayout->addWidget(mBtnTop);
    mBtnTop->setIcon(QIcon::fromTheme(QStringLiteral("go-top")));
    mBtnTop->setMinimumSize(mBtnTop->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
112
    mBtnUp = new QPushButton(hb);
113
114
115
116
    hbHBoxLayout->addWidget(mBtnUp);
    mBtnUp->setAutoRepeat(true);
    mBtnUp->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
    mBtnUp->setMinimumSize(mBtnUp->sizeHint() * 1.2);
Laurent Montel's avatar
Laurent Montel committed
117
    mBtnDown = new QPushButton(hb);
118
119
120
121
122
    hbHBoxLayout->addWidget(mBtnDown);
    mBtnDown->setAutoRepeat(true);
    mBtnDown->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
    mBtnDown->setMinimumSize(mBtnDown->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
123
    mBtnBottom = new QPushButton(hb);
124
125
126
127
128
129
130
131
132
133
134
135
136
    hbHBoxLayout->addWidget(mBtnBottom);
    mBtnBottom->setIcon(QIcon::fromTheme(QStringLiteral("go-bottom")));
    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);
Laurent Montel's avatar
Laurent Montel committed
137
    hbHBoxLayout->setContentsMargins({});
138
139
    hbHBoxLayout->setSpacing(4);

Laurent Montel's avatar
Laurent Montel committed
140
    mBtnNew = new QPushButton(hb);
141
142
143
144
145
    hbHBoxLayout->addWidget(mBtnNew);
    mBtnNew->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
    mBtnNew->setToolTip(i18n("New Script"));
    mBtnNew->setMinimumSize(mBtnNew->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
146
    mBtnDelete = new QPushButton(hb);
147
148
149
150
151
    hbHBoxLayout->addWidget(mBtnDelete);
    mBtnDelete->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
    mBtnDelete->setToolTip(i18n("Delete Script"));
    mBtnDelete->setMinimumSize(mBtnDelete->sizeHint() * 1.2);

Laurent Montel's avatar
Laurent Montel committed
152
    mBtnRename = new QPushButton(hb);
153
    mBtnRename->setToolTip(i18n("Rename Script"));
Laurent Montel's avatar
Laurent Montel committed
154
155
    mBtnRename->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
    mBtnRename->setMinimumSize(mBtnRename->sizeHint() * 1.2);
156
157
    hbHBoxLayout->addWidget(mBtnRename);

Laurent Montel's avatar
Laurent Montel committed
158
    mBtnDescription = new QPushButton(hb);
159
    mBtnDescription->setToolTip(i18n("Edit Script Description"));
Laurent Montel's avatar
Laurent Montel committed
160
161
162
    mBtnDescription->setIcon(QIcon::fromTheme(QStringLiteral("edit-comment")));
    mBtnDescription->setMinimumSize(mBtnDescription->sizeHint() * 1.2);

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    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();
}

Laurent Montel's avatar
Laurent Montel committed
183
SieveScriptListBox::~SieveScriptListBox() = default;
184

Laurent Montel's avatar
Laurent Montel committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
void SieveScriptListBox::slotCustomMenuRequested(const QPoint &pos)
{
    QMenu menu(this);
    QAction *newScriptAction = menu.addAction(i18nc("@action:inmenu", "New Script"));
    newScriptAction->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
    connect(newScriptAction, &QAction::triggered, this, &SieveScriptListBox::slotNew);

    if (mSieveListScript->itemAt(pos)) {
        QAction *renameScriptAction = menu.addAction(i18nc("@action:inmenu", "Rename Script"));
        renameScriptAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
        connect(renameScriptAction, &QAction::triggered, this, &SieveScriptListBox::slotRename);

        menu.addSeparator();
        QAction *deleteScriptAction = menu.addAction(i18nc("@action:inmenu", "Delete Script"));
        deleteScriptAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
        connect(deleteScriptAction, &QAction::triggered, this, &SieveScriptListBox::slotDelete);
    }
Laurent Montel's avatar
Laurent Montel committed
202
    menu.exec(QCursor::pos());
Laurent Montel's avatar
Laurent Montel committed
203
204
}

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

210
211
212
void SieveScriptListBox::slotItemActived(QListWidgetItem *item)
{
    if (item) {
213
        auto itemScript = static_cast<SieveScriptListItem *>(item);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        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)
{
237
    auto item = new SieveScriptListItem(newName, mSieveListScript);
238
    item->setDescription(description);
239
    auto page = new SieveScriptPage(mSieveGraphicalModeWidget);
Laurent Montel's avatar
Laurent Montel committed
240
    page->setListOfIncludeFile(mSieveGraphicalModeWidget->listOfIncludeFile());
241
242
243
244
245
246
247
248
249
250
    item->setScriptPage(page);
    Q_EMIT addNewPage(page);
    Q_EMIT enableButtonOk(true);
    mSieveListScript->setCurrentItem(item);
    updateButtons();
    return page;
}

void SieveScriptListBox::slotNew()
{
251
252
253
    bool ok;
    const QString newName = QInputDialog::getText(this, i18nc("@title:window", "New Script"), i18n("New script name:"), {}, {}, &ok);
    if (!newName.trimmed().isEmpty() && ok) {
254
255
256
257
258
259
260
261
262
        createNewScript(newName);
        Q_EMIT valueChanged();
    }
}

void SieveScriptListBox::slotDelete()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
263
264
265
266
267
268
        const int answer = KMessageBox::warningYesNo(this,
                                                     i18n("Do you want to delete \"%1\" script?", item->text()),
                                                     i18nc("@title:window", "Delete Script"),
                                                     KStandardGuiItem::del(),
                                                     KStandardGuiItem::cancel());
        if (answer == KMessageBox::Yes) {
269
            auto itemScript = static_cast<SieveScriptListItem *>(item);
270
271
272
273
274
275
276
277
278
279
280
281
282
            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) {
283
        bool ok = false;
Laurent Montel's avatar
Laurent Montel committed
284
        QString newName =
285
286
287
288
289
290
291
            QInputDialog::getText(this, i18nc("@title:window", "Rename Script"), i18n("New name for the script:"), QLineEdit::Normal, item->text(), &ok);
        if (ok) {
            newName = newName.trimmed();
            if (!newName.isEmpty()) {
                item->setText(newName);
                Q_EMIT valueChanged();
            }
292
293
294
295
296
297
298
299
        }
    }
}

void SieveScriptListBox::slotEditDescription()
{
    QListWidgetItem *item = mSieveListScript->currentItem();
    if (item) {
300
        auto sieveItem = static_cast<SieveScriptListItem *>(item);
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
        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();
        }
    }
}

367
QString SieveScriptListBox::generatedScript(QStringList &requireModules) const
368
369
370
371
372
{
    QString resultScript;
    QStringList lstRequires;
    const int numberOfScripts(mSieveListScript->count());
    for (int i = 0; i < numberOfScripts; ++i) {
373
        auto item = static_cast<SieveScriptListItem *>(mSieveListScript->item(i));
374
375
376
377
378
379
        if (i != 0) {
            resultScript += QLatin1String("\n\n");
        }
        resultScript += QLatin1Char('#') + defaultScriptName() + item->text() + QLatin1Char('\n');
        resultScript += item->generatedScript(lstRequires);
    }
380
381
382
    if (!resultScript.isEmpty()) {
        resultScript += QLatin1Char('\n');
    }
383
384

    QStringList endRequires;
385
    for (const QString &r : std::as_const(lstRequires)) {
386
387
        if (!endRequires.contains(r)) {
            endRequires.append(r);
388
            requireModules += QStringLiteral("require \"%1\";").arg(r);
389
390
391
392
393
394
395
396
397
398
        }
    }

    return resultScript;
}

void SieveScriptListBox::clear()
{
    mScriptNumber = 0;
    Q_EMIT enableButtonOk(false);
Laurent Montel's avatar
Laurent Montel committed
399
    // Clear tabpage
400
401
402
403
    mSieveListScript->clear();
    updateButtons();
}

Laurent Montel's avatar
Laurent Montel committed
404
void SieveScriptListBox::loadScript(const QString &doc, QString &error)
405
406
{
    clear();
Laurent Montel's avatar
Laurent Montel committed
407
408
409
410
411
412
413
414
    QXmlStreamReader streamReader(doc);
    if (streamReader.readNextStartElement()) {
        if (streamReader.name() == QLatin1String("script")) {
            SieveScriptPage *currentPage = nullptr;
            ParseSieveScriptTypeBlock typeBlock = TypeUnknown;
            loadBlock(streamReader, currentPage, typeBlock, error);
        }
    }
415
416
}

Laurent Montel's avatar
Laurent Montel committed
417
void SieveScriptListBox::loadBlock(QXmlStreamReader &n, SieveScriptPage *currentPage, ParseSieveScriptTypeBlock typeBlock, QString &error)
418
{
419
420
421
422
    QString scriptName;
    QString comment;
    bool hasCreatedAIfBlock = false;
    bool previousElementWasAComment = false;
Laurent Montel's avatar
Laurent Montel committed
423
    while (n.readNextStartElement()) {
424
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Laurent Montel's avatar
Laurent Montel committed
425
        const QStringRef tagName = n.name();
426
427
428
#else
        const QStringView tagName = n.name();
#endif
Laurent Montel's avatar
Laurent Montel committed
429
        // qDebug() <<"SieveScriptListBox::loadBlock tagName " << tagName;
Laurent Montel's avatar
Laurent Montel committed
430
431
        if (tagName == QLatin1String("control")) {
            previousElementWasAComment = false;
Laurent Montel's avatar
Laurent Montel committed
432
            // Create a new page when before it was "onlyactions"
Laurent Montel's avatar
Laurent Montel committed
433
434
435
            if (typeBlock == TypeBlockAction) {
                currentPage = nullptr;
            }
Laurent Montel's avatar
Laurent Montel committed
436
437
            if (n.attributes().hasAttribute(QLatin1String("name"))) {
                const QString controlType = n.attributes().value(QLatin1String("name")).toString();
Laurent Montel's avatar
Laurent Montel committed
438
                // qCDebug(LIBKSIEVE_LOG)<<" controlType"<<controlType;
Laurent Montel's avatar
Laurent Montel committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
                if (controlType == QLatin1String("if")) {
                    typeBlock = TypeBlockIf;
                    if (!currentPage || hasCreatedAIfBlock) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
                    }
                    hasCreatedAIfBlock = true;
                    comment.clear();
                    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);
                    }
Laurent Montel's avatar
Laurent Montel committed
467
                    // We are sure that we can't have another elsif
468
                    currentPage = nullptr;
Laurent Montel's avatar
Laurent Montel committed
469
470
471
472
                } else if (controlType == QLatin1String("foreverypart")) {
                    typeBlock = TypeBlockForeachBlock;
                    if (!currentPage) {
                        currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
473
                        comment.clear();
Laurent Montel's avatar
Laurent Montel committed
474
475
476
                    }
                    if (currentPage->forEveryPartWidget()) {
                        currentPage->forEveryPartWidget()->loadScript(n, error);
477
                    } else {
Laurent Montel's avatar
Laurent Montel committed
478
                        error += i18n("forEveryPart is not supported by your server") + QLatin1Char('\n');
479
                    }
Laurent Montel's avatar
Laurent Montel committed
480
                    // TODO verify it.
Laurent Montel's avatar
Laurent Montel committed
481
482
483
#ifdef QDOMELEMENT_FIXME
                    QDomNode block = e.firstChildElement(QStringLiteral("block")).firstChild();
                    loadBlock(block, currentPage, typeBlock, error);
Laurent Montel's avatar
Laurent Montel committed
484
#endif
Laurent Montel's avatar
Laurent Montel committed
485
486
                } else if (controlType == QLatin1String("require")) {
                    n.skipCurrentElement();
Laurent Montel's avatar
Laurent Montel committed
487
                    // Nothing. autogenerated
488
                } else {
Laurent Montel's avatar
Laurent Montel committed
489
                    qCDebug(LIBKSIEVE_LOG) << " unknown controlType :" << controlType;
490
                }
Laurent Montel's avatar
Laurent Montel committed
491
492
493
            }
        } else if (tagName == QLatin1String("comment")) {
            previousElementWasAComment = true;
Laurent Montel's avatar
Laurent Montel committed
494
#ifdef FIXME_COMMENT
Laurent Montel's avatar
Laurent Montel committed
495
            if (e.hasAttribute(QStringLiteral("hash"))) {
Laurent Montel's avatar
Laurent Montel committed
496
                // TODO
Laurent Montel's avatar
Laurent Montel committed
497
            } else if (e.hasAttribute(QStringLiteral("bracket"))) {
Laurent Montel's avatar
Laurent Montel committed
498
                // TODO
Laurent Montel's avatar
Laurent Montel committed
499
500
501
502
503
504
505
506
507
508
509
            }
#endif
            QString str(n.readElementText());
            if (str.contains(defaultScriptName())) {
                scriptName = str.remove(defaultScriptName());
            } else {
                if (!comment.isEmpty()) {
                    comment += QLatin1Char('\n');
                }
                comment += str;
            }
Laurent Montel's avatar
Laurent Montel committed
510
            // qDebug() << " COMMENT " << comment;
Laurent Montel's avatar
Laurent Montel committed
511
512
        } else if (tagName == QLatin1String("action")) {
            previousElementWasAComment = false;
Laurent Montel's avatar
Laurent Montel committed
513
514
            if (n.attributes().hasAttribute(QLatin1String("name"))) {
                const QString actionName = n.attributes().value(QLatin1String("name")).toString();
Laurent Montel's avatar
Laurent Montel committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
                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)) {
Laurent Montel's avatar
Laurent Montel committed
538
                    // FIXME global not global variable.
Laurent Montel's avatar
Laurent Montel committed
539
                    if (currentPage->globalVariableWidget()) {
Laurent Montel's avatar
Laurent Montel committed
540
541
                        const SieveGlobalVariableActionWidget::VariableElement var = currentPage->globalVariableWidget()->loadSetVariable(n, error);
                        if (var.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
542
                            qCDebug(LIBKSIEVE_LOG) << "It's not a global variable";
Laurent Montel's avatar
Laurent Montel committed
543
                            if ((typeBlock == TypeBlockIf) || (typeBlock == TypeBlockElse) || (typeBlock == TypeBlockElsif)) {
Laurent Montel's avatar
Laurent Montel committed
544
                                currentPage = createNewScript(scriptName.isEmpty() ? createUniqName() : scriptName, comment);
545
                            }
Laurent Montel's avatar
Laurent Montel committed
546
547
                            typeBlock = TypeBlockAction;
                            comment.clear();
Laurent Montel's avatar
Laurent Montel committed
548
                            currentPage->blockIfWidget()->loadLocalVariable(var);
549
550
                        }
                    } else {
Laurent Montel's avatar
Laurent Montel committed
551
                        qCDebug(LIBKSIEVE_LOG) << " set not supported";
552
                    }
Laurent Montel's avatar
Laurent Montel committed
553
554
555
556
557
558
559
                } 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);
560
561
                }
            }
Laurent Montel's avatar
Laurent Montel committed
562
        } else if (tagName == QLatin1String("crlf")) {
Laurent Montel's avatar
Laurent Montel committed
563
            // If it was a comment previously you will create a \n
Laurent Montel's avatar
Laurent Montel committed
564
565
566
567
568
569
570
            if (previousElementWasAComment) {
                comment += QLatin1Char('\n');
            }
            n.skipCurrentElement();
        } else {
            qCDebug(LIBKSIEVE_LOG) << " unknown tagname" << tagName;
        }
Laurent Montel's avatar
Laurent Montel committed
571
    }
572
573
574
575
576
577
578
579
}

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