presentation/QuickRuleModel.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QAbstractListModel> | ||
| 4 | #include <QHash> | ||
| 5 | #include <QList> | ||
| 6 | #include <QStringList> | ||
| 7 | |||
| 8 | #include "data/SieveRuleCodec.h" | ||
| 9 | |||
| 10 | // Rule list of the manager (Sprint 95 S95.3.2): one row per rule, | ||
| 11 | // out-of-office and advanced block of the active script, in script order. | ||
| 12 | // | ||
| 13 | // Rows are keyed (named blocks by kind + name, others by content hash), so | ||
| 14 | // a save updates, inserts, moves or removes single rows instead of | ||
| 15 | // resetting the model — the list keeps its scroll position and selection | ||
| 16 | // at 72+ rules. Filters (text, target folder, problems, off) only change | ||
| 17 | // which rows are shown; they reset the visible list on purpose. | ||
| 18 | class QuickRuleModel final : public QAbstractListModel { | ||
| 19 | 302 | Q_OBJECT | |
| 20 | Q_PROPERTY(int count READ rowCount NOTIFY countChanged) | ||
| 21 | |||
| 22 | public: | ||
| 23 | enum Role { | ||
| 24 | KeyRole = Qt::UserRole + 1, | ||
| 25 | BlockIndexRole, | ||
| 26 | KindRole, // "rule", "vacation", "advanced" | ||
| 27 | NameRole, | ||
| 28 | NamedRole, | ||
| 29 | EnabledRole, | ||
| 30 | PositionRole, // 1-based number among rules and advanced blocks | ||
| 31 | SentenceRole, // rich text | ||
| 32 | PlainSentenceRole, | ||
| 33 | TargetRole, | ||
| 34 | StopRole, | ||
| 35 | ForwardRole, | ||
| 36 | DestructiveRole, | ||
| 37 | UnsupportedRole, | ||
| 38 | ExcerptRole, | ||
| 39 | LineRole, | ||
| 40 | HealthRole, // plain-text finding, empty when healthy | ||
| 41 | HealthFixRole, // label of the one-click fix, empty when none | ||
| 42 | HealthAlternativeRole, // label of a second fix (F95 follow-up), empty when none | ||
| 43 | MatchCountRole, // "42 in Inbox" or empty | ||
| 44 | AccessibleRole, | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct Row { | ||
| 48 | QString key; | ||
| 49 | int blockIndex = -1; | ||
| 50 | QString kind; | ||
| 51 | QString name; | ||
| 52 | bool named = false; | ||
| 53 | bool enabled = true; | ||
| 54 | int position = 0; | ||
| 55 | QString sentence; | ||
| 56 | QString plainSentence; | ||
| 57 | QString target; | ||
| 58 | bool stop = false; | ||
| 59 | bool forward = false; | ||
| 60 | bool destructive = false; | ||
| 61 | QStringList unsupported; | ||
| 62 | QString excerpt; | ||
| 63 | int line = 0; | ||
| 64 | QString health; | ||
| 65 | QString healthFix; | ||
| 66 | QString healthAlternative; // second solution, e.g. "Only reject" (empty = none) | ||
| 67 | QString matchCount; | ||
| 68 | |||
| 69 | bool operator==(const Row &other) const; | ||
| 70 | }; | ||
| 71 | |||
| 72 | 864 | explicit QuickRuleModel(QObject *parent = nullptr) : QAbstractListModel(parent) {} | |
| 73 | |||
| 74 | int rowCount(const QModelIndex &parent = {}) const override; | ||
| 75 | QVariant data(const QModelIndex &index, int role) const override; | ||
| 76 | QHash<int, QByteArray> roleNames() const override; | ||
| 77 | |||
| 78 | // Rebuilds the rows from a decoded document; `serverExtensions` marks | ||
| 79 | // rules that need extensions the server does not offer. | ||
| 80 | static QList<Row> rowsFor(const Sieve::Document &document, const QStringList &serverExtensions); | ||
| 81 | void setRows(const QList<Row> &rows); | ||
| 82 | const QList<Row> &allRows() const { return m_all; } | ||
| 83 | |||
| 84 | void setFilter(const QString &text, const QString &folder, bool problemsOnly, bool offOnly); | ||
| 85 | void setMatchCount(const QString &key, const QString &text); | ||
| 86 | |||
| 87 | Q_INVOKABLE int blockIndexAt(int row) const; | ||
| 88 | Q_INVOKABLE int rowForBlock(int blockIndex) const; | ||
| 89 | Q_INVOKABLE QString keyAt(int row) const; | ||
| 90 | int problemCount() const; | ||
| 91 | int offCount() const; | ||
| 92 | QStringList targets() const; | ||
| 93 | |||
| 94 | signals: | ||
| 95 | void countChanged(); | ||
| 96 | |||
| 97 | private: | ||
| 98 | bool accepts(const Row &row) const; | ||
| 99 | void applyVisible(const QList<Row> &target); | ||
| 100 | |||
| 101 | QList<Row> m_all; | ||
| 102 | QList<Row> m_rows; // visible | ||
| 103 | QString m_text; | ||
| 104 | QString m_folder; | ||
| 105 | bool m_problemsOnly = false; | ||
| 106 | bool m_offOnly = false; | ||
| 107 | }; | ||
| 108 |