Rework sync protocol for highlight rules
authorJanne Koschinski <janne@kuschku.de>
Sun, 10 Jun 2018 22:50:09 +0000 (00:50 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 18 Jun 2018 22:17:30 +0000 (00:17 +0200)
- Sync highlightNick and nicksCaseSensitive as actual Q_PROPERTY
- Identify rules by their id, not their name
- This allows multiple rules to have the same rule, but different scopes
  and senders

src/common/highlightrulemanager.cpp
src/common/highlightrulemanager.h
src/core/corehighlightrulemanager.h
src/qtui/settingspages/corehighlightsettingspage.cpp
src/qtui/settingspages/corehighlightsettingspage.h

index 3fe3691..deaf42a 100644 (file)
@@ -25,8 +25,7 @@
 #include <QDebug>
 
 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
-HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
-{
+HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other) {
     if (this == &other)
         return *this;
 
@@ -38,18 +37,28 @@ HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager
 }
 
 
-int HighlightRuleManager::indexOf(const QString &name) const
-{
+int HighlightRuleManager::indexOf(int id) const {
     for (int i = 0; i < _highlightRuleList.count(); i++) {
-        if (_highlightRuleList[i].name == name)
+        if (_highlightRuleList[i].id == id)
             return i;
     }
     return -1;
 }
 
+int HighlightRuleManager::nextId() {
+    int max = 0;
+    for (int i = 0; i < _highlightRuleList.count(); i++) {
+        int id = _highlightRuleList[i].id;
+        if (id > max) {
+            max = id;
+        }
+    }
+    return max+1;
+}
 
-QVariantMap HighlightRuleManager::initHighlightRuleList() const
-{
+
+QVariantMap HighlightRuleManager::initHighlightRuleList() const {
+    QVariantList id;
     QVariantMap highlightRuleListMap;
     QStringList name;
     QVariantList isRegEx;
@@ -60,6 +69,7 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const
     QStringList channel;
 
     for (int i = 0; i < _highlightRuleList.count(); i++) {
+        id << _highlightRuleList[i].id;
         name << _highlightRuleList[i].name;
         isRegEx << _highlightRuleList[i].isRegEx;
         isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
@@ -69,6 +79,7 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const
         channel << _highlightRuleList[i].chanName;
     }
 
+    highlightRuleListMap["id"] = id;
     highlightRuleListMap["name"] = name;
     highlightRuleListMap["isRegEx"] = isRegEx;
     highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
@@ -76,14 +87,12 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const
     highlightRuleListMap["isInverse"] = isInverse;
     highlightRuleListMap["sender"] = sender;
     highlightRuleListMap["channel"] = channel;
-    highlightRuleListMap["highlightNick"] = _highlightNick;
-    highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive;
     return highlightRuleListMap;
 }
 
 
-void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
-{
+void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList) {
+    QVariantList id = highlightRuleList["id"].toList();
     QStringList name = highlightRuleList["name"].toStringList();
     QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
     QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
@@ -92,37 +101,33 @@ void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlight
     QStringList sender = highlightRuleList["sender"].toStringList();
     QStringList channel = highlightRuleList["channel"].toStringList();
 
-    int count = name.count();
-    if (count != isRegEx.count() || count != isCaseSensitive.count() || count != isActive.count() ||
-        count != isInverse.count() || count != sender.count() || count != channel.count()) {
+    int count = id.count();
+    if (count != name.count() || count != isRegEx.count() || count != isCaseSensitive.count() ||
+        count != isActive.count() || count != isInverse.count() || count != sender.count() ||
+        count != channel.count()) {
         qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
         return;
     }
 
     _highlightRuleList.clear();
     for (int i = 0; i < name.count(); i++) {
-        _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
+        _highlightRuleList << HighlightRule(id[i].toInt(), name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
                                             isActive[i].toBool(), isInverse[i].toBool(), sender[i], channel[i]);
     }
-
-    // Make sure the default for _highlightNick is "CurrentNick" if not set
-    _highlightNick = HighlightNickType(
-                highlightRuleList.value("highlightNick", HighlightNickType::CurrentNick).toInt());
-
-    _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool();
 }
 
-void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
-                                            bool isInverse, const QString &sender, const QString &channel)
-{
-    if (contains(name)) {
+void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive,
+                                            bool isActive, bool isInverse, const QString &sender,
+                                            const QString &channel) {
+    if (contains(id)) {
         return;
     }
 
-    HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
+    HighlightRule newItem = HighlightRule(id, name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
     _highlightRuleList << newItem;
 
-    SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender), ARG(channel))
+    SYNC(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender),
+         ARG(channel))
 }
 
 
@@ -132,8 +137,7 @@ bool HighlightRuleManager::match(const QString &msgContents,
                                  Message::Flags msgFlags,
                                  const QString &bufferName,
                                  const QString &currentNick,
-                                 const QStringList identityNicks)
-{
+                                 const QStringList identityNicks) {
     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
        return false;
     }
@@ -203,15 +207,13 @@ bool HighlightRuleManager::match(const QString &msgContents,
     return false;
 }
 
-void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
-{
+void HighlightRuleManager::removeHighlightRule(int highlightRule) {
     removeAt(indexOf(highlightRule));
     SYNC(ARG(highlightRule))
 }
 
 
-void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
-{
+void HighlightRuleManager::toggleHighlightRule(int highlightRule) {
     int idx = indexOf(highlightRule);
     if (idx == -1)
         return;
@@ -219,7 +221,6 @@ void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
     SYNC(ARG(highlightRule))
 }
 
-bool HighlightRuleManager::match(const Message &msg, const QString &currentNick, const QStringList &identityNicks)
-{
+bool HighlightRuleManager::match(const Message &msg, const QString &currentNick, const QStringList &identityNicks) {
     return match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);
 }
index 6fa9c27..0e50954 100644 (file)
 class HighlightRuleManager : public SyncableObject
 {
     SYNCABLE_OBJECT
-        Q_OBJECT
+    Q_OBJECT
+
+    Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
+    Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
 public:
     enum HighlightNickType {
         NoNick = 0x00,
@@ -46,6 +49,7 @@ public:
     HighlightRuleManager &operator=(const HighlightRuleManager &other);
 
     struct HighlightRule {
+        int id;
         QString name;
         bool isRegEx = false;
         bool isCaseSensitive = false;
@@ -54,14 +58,15 @@ public:
         QString sender;
         QString chanName;
         HighlightRule() {}
-        HighlightRule(QString name_, bool isRegEx_, bool isCaseSensitive_, bool isEnabled_, bool isInverse_,
+        HighlightRule(int id_, QString name_, bool isRegEx_, bool isCaseSensitive_, bool isEnabled_, bool isInverse_,
                       QString sender_, QString chanName_)
-            : name(std::move(name_)), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_), isEnabled(isEnabled_),
-              isInverse(isInverse_), sender(std::move(sender_)), chanName(std::move(chanName_)) {
+            : id(id_), name(std::move(name_)), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_),
+              isEnabled(isEnabled_), isInverse(isInverse_), sender(std::move(sender_)), chanName(std::move(chanName_)) {
         }
-        bool operator!=(const HighlightRule &other)
-        {
-            return (name != other.name ||
+
+        bool operator!=(const HighlightRule &other) {
+            return (id != other.id ||
+                    name != other.name ||
                     isRegEx != other.isRegEx ||
                     isCaseSensitive != other.isCaseSensitive ||
                     isEnabled != other.isEnabled ||
@@ -72,8 +77,8 @@ public:
     };
     typedef QList<HighlightRule> HighlightRuleList;
 
-    int indexOf(const QString &rule) const;
-    inline bool contains(const QString &rule) const { return indexOf(rule) != -1; }
+    int indexOf(int rule) const;
+    inline bool contains(int rule) const { return indexOf(rule) != -1; }
     inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
     inline int count() const { return _highlightRuleList.count(); }
     inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
@@ -82,7 +87,9 @@ public:
     inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
     inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
 
-    inline HighlightNickType highlightNick() { return _highlightNick; }
+    int nextId();
+
+    inline int highlightNick() { return _highlightNick; }
     inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
 
     //! Check if a message matches the HighlightRule
@@ -100,16 +107,16 @@ public slots:
       * and get that synced with the core immediately.
       * \param highlightRule A valid ignore rule
       */
-    virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
-    virtual void removeHighlightRule(const QString &highlightRule);
+    virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
+    virtual void removeHighlightRule(int highlightRule);
 
     //! Request toggling of "isEnabled" flag of a given ignore rule.
     /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
       * and get that synced with the core immediately.
       * \param highlightRule A valid ignore rule
       */
-    virtual inline void requestToggleHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) }
-    virtual void toggleHighlightRule(const QString &highlightRule);
+    virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
+    virtual void toggleHighlightRule(int highlightRule);
 
     //! Request an HighlightRule to be added to the ignore list
     /** Items added to the list with this method, get immediately synced with the core
@@ -119,14 +126,15 @@ public slots:
       * \param isEnabled If the rule is active
       * @param chanName The channel in which the rule should apply
       */
-    virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
+    virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
                                                 bool isInverse, const QString &sender, const QString &chanName)
     {
-        REQUEST(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender), ARG(chanName))
+        REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender),
+                ARG(chanName))
     }
 
 
-    virtual void addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
+    virtual void addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
                                   bool isInverse, const QString &sender, const QString &chanName);
 
     virtual inline void requestSetHighlightNick(int highlightNick)
index 4daf016..0c4877b 100644 (file)
@@ -40,12 +40,12 @@ public:
     bool match(const RawMessage &msg, const QString &currentNick, const QStringList &identityNicks);
 
 public slots:
-    virtual inline void requestToggleHighlightRule(const QString &highlightRule) { toggleHighlightRule(highlightRule); }
-    virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { removeHighlightRule(highlightRule); }
-    virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
-                                                bool isInverse, const QString &sender, const QString &chanName)
-    {
-        addHighlightRule(name, isRegEx, isCaseSensitive, isEnabled, isInverse, sender, chanName);
+    virtual inline void requestToggleHighlightRule(int highlightRule) { toggleHighlightRule(highlightRule); }
+    virtual inline void requestRemoveHighlightRule(int highlightRule) { removeHighlightRule(highlightRule); }
+    virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive,
+                                                bool isEnabled, bool isInverse, const QString &sender,
+                                                const QString &chanName) {
+        addHighlightRule(id, name, isRegEx, isCaseSensitive, isEnabled, isInverse, sender, chanName);
     }
 
 
index 70cc84f..7e50229 100644 (file)
@@ -243,11 +243,15 @@ void CoreHighlightSettingsPage::defaults()
     widgetHasChanged();
 }
 
-void CoreHighlightSettingsPage::addNewHighlightRow(bool enable, const QString &name, bool regex, bool cs,
+void CoreHighlightSettingsPage::addNewHighlightRow(bool enable, int id, const QString &name, bool regex, bool cs,
                                                    const QString &sender, const QString &chanName, bool self)
 {
     ui.highlightTable->setRowCount(ui.highlightTable->rowCount() + 1);
 
+    if (id < 0) {
+        id = nextId();
+    }
+
     auto *nameItem = new QTableWidgetItem(name);
 
     auto *regexItem = new QTableWidgetItem("");
@@ -322,14 +326,18 @@ void CoreHighlightSettingsPage::addNewHighlightRow(bool enable, const QString &n
     if (!self)
         ui.highlightTable->setCurrentItem(nameItem);
 
-    highlightList << HighlightRuleManager::HighlightRule(name, regex, cs, enable, false, sender, chanName);
+    highlightList << HighlightRuleManager::HighlightRule(id, name, regex, cs, enable, false, sender, chanName);
 }
 
-void CoreHighlightSettingsPage::addNewIgnoredRow(bool enable, const QString &name, bool regex, bool cs,
+void CoreHighlightSettingsPage::addNewIgnoredRow(bool enable, int id, const QString &name, bool regex, bool cs,
                                                  const QString &sender, const QString &chanName, bool self)
 {
     ui.ignoredTable->setRowCount(ui.ignoredTable->rowCount() + 1);
 
+    if (id < 0) {
+        id = nextId();
+    }
+
     auto *nameItem = new QTableWidgetItem(name);
 
     auto *regexItem = new QTableWidgetItem("");
@@ -392,7 +400,7 @@ void CoreHighlightSettingsPage::addNewIgnoredRow(bool enable, const QString &nam
     if (!self)
         ui.ignoredTable->setCurrentItem(nameItem);
 
-    ignoredList << HighlightRuleManager::HighlightRule(name, regex, cs, enable, true, sender, chanName);
+    ignoredList << HighlightRuleManager::HighlightRule(id, name, regex, cs, enable, true, sender, chanName);
 }
 
 void CoreHighlightSettingsPage::removeSelectedHighlightRows()
@@ -565,6 +573,7 @@ void CoreHighlightSettingsPage::load()
         for (auto &rule : ruleManager->highlightRuleList()) {
             if (rule.isInverse) {
                 addNewIgnoredRow(rule.isEnabled,
+                                 rule.id,
                                  rule.name,
                                  rule.isRegEx,
                                  rule.isCaseSensitive,
@@ -572,7 +581,7 @@ void CoreHighlightSettingsPage::load()
                                  rule.chanName);
             }
             else {
-                addNewHighlightRow(rule.isEnabled, rule.name, rule.isRegEx, rule.isCaseSensitive, rule.sender,
+                addNewHighlightRow(rule.isEnabled, rule.id, rule.name, rule.isRegEx, rule.isCaseSensitive, rule.sender,
                                    rule.chanName);
             }
         }
@@ -607,12 +616,12 @@ void CoreHighlightSettingsPage::save()
     clonedManager.clear();
 
     for (auto &rule : highlightList) {
-        clonedManager.addHighlightRule(rule.name, rule.isRegEx, rule.isCaseSensitive, rule.isEnabled, false,
+        clonedManager.addHighlightRule(rule.id, rule.name, rule.isRegEx, rule.isCaseSensitive, rule.isEnabled, false,
                                        rule.sender, rule.chanName);
     }
 
     for (auto &rule : ignoredList) {
-        clonedManager.addHighlightRule(rule.name, rule.isRegEx, rule.isCaseSensitive, rule.isEnabled, true,
+        clonedManager.addHighlightRule(rule.id, rule.name, rule.isRegEx, rule.isCaseSensitive, rule.isEnabled, true,
                                        rule.sender, rule.chanName);
     }
 
@@ -626,6 +635,23 @@ void CoreHighlightSettingsPage::save()
     load();
 }
 
+int CoreHighlightSettingsPage::nextId() {
+    int max = 0;
+    for (int i = 0; i < highlightList.count(); i++) {
+        int id = highlightList[i].id;
+        if (id > max) {
+            max = id;
+        }
+    }
+    for (int i = 0; i < ignoredList.count(); i++) {
+        int id = ignoredList[i].id;
+        if (id > max) {
+            max = id;
+        }
+    }
+    return max+1;
+}
+
 void CoreHighlightSettingsPage::widgetHasChanged()
 {
     setChangedState(true);
@@ -692,6 +718,7 @@ void CoreHighlightSettingsPage::importRules() {
         auto highlightRule = variant.toMap();
 
         clonedManager.addHighlightRule(
+                clonedManager.nextId(),
                 highlightRule["Name"].toString(),
                 highlightRule["RegEx"].toBool(),
                 highlightRule["CS"].toBool(),
index 1a0ea71..d4682ec 100644 (file)
@@ -49,14 +49,14 @@ public slots:
 private slots:
     void coreConnectionStateChanged(bool state);
     void widgetHasChanged();
-    void addNewHighlightRow(bool enable = true, const QString &name = tr("highlight rule"), bool regex = false,
+    void addNewHighlightRow(bool enable = true, int id = -1, const QString &name = tr("highlight rule"), bool regex = false,
                             bool cs = false, const QString &sender = "", const QString &chanName = "",
                             bool self = false);
-    void addNewIgnoredRow(bool enable = true, const QString &name = tr("highlight rule"), bool regex = false,
+    void addNewIgnoredRow(bool enable = true, int id = -1, const QString &name = tr("highlight rule"), bool regex = false,
                           bool cs = false, const QString &sender = "", const QString &chanName = "", bool self = false);
     void removeSelectedHighlightRows();
     void removeSelectedIgnoredRows();
-    void highlightNicksChanged(const int index);
+    void highlightNicksChanged(int index);
     void selectHighlightRow(QTableWidgetItem *item);
     void selectIgnoredRow(QTableWidgetItem *item);
     void highlightTableChanged(QTableWidgetItem *item);
@@ -103,5 +103,7 @@ private:
      */
     void updateCoreSupportStatus(bool state);
 
+    int nextId();
+
     bool _initialized;
 };