common: Make SyncableObject non-copyable
[quassel.git] / src / common / highlightrulemanager.h
index cabf0d2..8f9a381 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2018 by the Quassel Project                        *
+ *   Copyright (C) 2005-2020 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -20,6 +20,8 @@
 
 #pragma once
 
+#include "common-export.h"
+
 #include <utility>
 
 #include <QString>
 
 #include "expressionmatch.h"
 #include "message.h"
+#include "nickhighlightmatcher.h"
 #include "syncableobject.h"
 
-class HighlightRuleManager : public SyncableObject
+class COMMON_EXPORT HighlightRuleManager : public SyncableObject
 {
-    SYNCABLE_OBJECT
     Q_OBJECT
+    SYNCABLE_OBJECT
 
     Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
     Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
 
 public:
-    enum HighlightNickType {
+    enum HighlightNickType
+    {
         NoNick = 0x00,
         CurrentNick = 0x01,
         AllNicks = 0x02
     };
 
-    inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
-    HighlightRuleManager &operator=(const HighlightRuleManager &other);
+    inline HighlightRuleManager(QObject* parent = nullptr)
+        : SyncableObject(parent)
+    {
+        setAllowClientUpdates(true);
+    }
 
     /**
      * Individual highlight rule
      */
-    class HighlightRule
+    class COMMON_EXPORT HighlightRule
     {
     public:
         /**
          * Construct an empty highlight rule
          */
-        HighlightRule() {}
+        HighlightRule() = default;
 
         /**
          * Construct a highlight rule with the given parameters
@@ -72,10 +79,16 @@ public:
          * @param sender           String representing a message sender expression to match
          * @param chanName         String representing a channel name expression to match
          */
-        HighlightRule(int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
-                      bool isInverse, QString sender, QString chanName)
-            : _id(id), _contents(contents), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
-              _isEnabled(isEnabled), _isInverse(isInverse), _sender(sender), _chanName(chanName)
+        HighlightRule(
+            int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName)
+            : _id(id)
+            , _contents(std::move(contents))
+            , _isRegEx(isRegEx)
+            , _isCaseSensitive(isCaseSensitive)
+            , _isEnabled(isEnabled)
+            , _isInverse(isInverse)
+            , _sender(std::move(sender))
+            , _chanName(std::move(chanName))
         {
             _cacheInvalid = true;
             // Cache expression matches on construction
@@ -94,9 +107,7 @@ public:
          *
          * @return Integer ID of the rule
          */
-        inline int id() const {
-            return _id;
-        }
+        inline int id() const { return _id; }
         /**
          * Sets the ID of this rule
          *
@@ -104,9 +115,7 @@ public:
          *
          * @param id Integer ID of the rule
          */
-        inline void setId(int id) {
-            _id = id;
-        }
+        inline void setId(int id) { _id = id; }
 
         /**
          * Gets the message contents this rule matches
@@ -115,15 +124,14 @@ public:
          *
          * @return String representing a phrase or expression to match
          */
-        inline QString contents() const {
-            return _contents;
-        }
+        inline QString contents() const { return _contents; }
         /**
          * Sets the message contents this rule matches
          *
          * @param contents String representing a phrase or expression to match
          */
-        inline void setContents(const QString &contents) {
+        inline void setContents(const QString& contents)
+        {
             _contents = contents;
             _cacheInvalid = true;
         }
@@ -133,15 +141,14 @@ public:
          *
          * @return True if regular expression, otherwise false
          */
-        inline bool isRegEx() const {
-            return _isRegEx;
-        }
+        inline bool isRegEx() const { return _isRegEx; }
         /**
          * Sets if this rule is a regular expression rule
          *
          * @param isRegEx True if regular expression, otherwise false
          */
-        inline void setIsRegEx(bool isRegEx) {
+        inline void setIsRegEx(bool isRegEx)
+        {
             _isRegEx = isRegEx;
             _cacheInvalid = true;
         }
@@ -151,15 +158,14 @@ public:
          *
          * @return True if case sensitive, otherwise false
          */
-        inline bool isCaseSensitive() const {
-            return _isCaseSensitive;
-        }
+        inline bool isCaseSensitive() const { return _isCaseSensitive; }
         /**
          * Sets if this rule is case sensitive
          *
          * @param isCaseSensitive True if case sensitive, otherwise false
          */
-        inline void setIsCaseSensitive(bool isCaseSensitive) {
+        inline void setIsCaseSensitive(bool isCaseSensitive)
+        {
             _isCaseSensitive = isCaseSensitive;
             _cacheInvalid = true;
         }
@@ -169,34 +175,26 @@ public:
          *
          * @return True if enabled, otherwise false
          */
-        inline bool isEnabled() const {
-            return _isEnabled;
-        }
+        inline bool isEnabled() const { return _isEnabled; }
         /**
          * Sets if this rule is enabled and active
          *
          * @param isEnabled True if enabled, otherwise false
          */
-        inline void setIsEnabled(bool isEnabled) {
-            _isEnabled = isEnabled;
-        }
+        inline void setIsEnabled(bool isEnabled) { _isEnabled = isEnabled; }
 
         /**
          * Gets if this rule is a highlight ignore rule
          *
          * @return True if rule is treated as highlight ignore, otherwise false
          */
-        inline bool isInverse() const {
-            return _isInverse;
-        }
+        inline bool isInverse() const { return _isInverse; }
         /**
          * Sets if this rule is a highlight ignore rule
          *
          * @param isInverse True if rule is treated as highlight ignore, otherwise false
          */
-        inline void setIsInverse(bool isInverse) {
-            _isInverse = isInverse;
-        }
+        inline void setIsInverse(bool isInverse) { _isInverse = isInverse; }
 
         /**
          * Gets the message sender this rule matches
@@ -211,7 +209,8 @@ public:
          *
          * @param sender String representing a phrase or expression to match
          */
-        inline void setSender(const QString &sender) {
+        inline void setSender(const QString& sender)
+        {
             _sender = sender;
             _cacheInvalid = true;
         }
@@ -229,7 +228,8 @@ public:
          *
          * @param chanName String representing a phrase or expression to match
          */
-        inline void setChanName(const QString &chanName) {
+        inline void setChanName(const QString& chanName)
+        {
             _chanName = chanName;
             _cacheInvalid = true;
         }
@@ -239,7 +239,8 @@ public:
          *
          * @return Expression matcher to compare with message contents
          */
-        inline ExpressionMatch contentsMatcher() const {
+        inline ExpressionMatch contentsMatcher() const
+        {
             if (_cacheInvalid) {
                 determineExpressions();
             }
@@ -251,7 +252,8 @@ public:
          *
          * @return Expression matcher to compare with message sender
          */
-        inline ExpressionMatch senderMatcher() const {
+        inline ExpressionMatch senderMatcher() const
+        {
             if (_cacheInvalid) {
                 determineExpressions();
             }
@@ -263,14 +265,15 @@ public:
          *
          * @return Expression matcher to compare with channel name
          */
-        inline ExpressionMatch chanNameMatcher() const {
+        inline ExpressionMatch chanNameMatcher() const
+        {
             if (_cacheInvalid) {
                 determineExpressions();
             }
             return _chanNameMatch;
         }
 
-        bool operator!=(const HighlightRule &other) const;
+        bool operator!=(const HighlightRuleother) const;
 
     private:
         /**
@@ -289,10 +292,10 @@ public:
 
         // These represent internal cache and should be safe to mutate in 'const' functions
         // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
-        mutable bool _cacheInvalid = true;           ///< If true, match cache needs redone
-        mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
-        mutable ExpressionMatch _senderMatch = {};   ///< Expression match cache for sender
-        mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
+        mutable bool _cacheInvalid = true;            ///< If true, match cache needs redone
+        mutable ExpressionMatch _contentsMatch = {};  ///< Expression match cache for message content
+        mutable ExpressionMatch _senderMatch = {};    ///< Expression match cache for sender
+        mutable ExpressionMatch _chanNameMatch = {};  ///< Expression match cache for channel name
     };
 
     using HighlightRuleList = QList<HighlightRule>;
@@ -303,9 +306,9 @@ public:
     inline int count() const { return _highlightRuleList.count(); }
     inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
     inline void clear() { _highlightRuleList.clear(); }
-    inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
-    inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
-    inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
+    inline HighlightRuleoperator[](int i) { return _highlightRuleList[i]; }
+    inline const HighlightRuleoperator[](int i) const { return _highlightRuleList.at(i); }
+    inline const HighlightRuleListhighlightRuleList() const { return _highlightRuleList; }
 
     int nextId();
 
@@ -314,109 +317,111 @@ public:
 
     //! Check if a message matches the HighlightRule
     /** This method checks if a message matches the users highlight rules.
-      * \param msg The Message that should be checked
-      */
-    bool match(const Message &msg, const QString &currentNick, const QStringList &identityNicks);
+     * \param msg The Message that should be checked
+     */
+    bool match(const Message& msg, const QString& currentNick, const QStringList& identityNicks);
 
 public slots:
     virtual QVariantMap initHighlightRuleList() const;
-    virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
+    virtual void initSetHighlightRuleList(const QVariantMapHighlightRuleList);
 
     //! Request removal of an ignore rule based on the rule itself.
     /** Use this method if you want to remove a single ignore rule
-      * and get that synced with the core immediately.
-      * \param highlightRule A valid ignore rule
-      */
+     * and get that synced with the core immediately.
+     * \param highlightRule A valid ignore rule
+     */
     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
-      */
+     * and get that synced with the core immediately.
+     * \param highlightRule A valid ignore rule
+     */
     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
-      * \param name The rule
-      * \param isRegEx If the rule should be interpreted as a nickname, or a regex
-      * \param isCaseSensitive If the rule should be interpreted as case-sensitive
-      * \param isEnabled If the rule is active
-      * @param chanName The channel in which the rule should apply
-      */
-    virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
-                                                bool isInverse, const QString &sender, const QString &chanName)
+     * \param name The rule
+     * \param isRegEx If the rule should be interpreted as a nickname, or a regex
+     * \param isCaseSensitive If the rule should be interpreted as case-sensitive
+     * \param isEnabled If the rule is active
+     * @param chanName The channel in which the rule should apply
+     */
+    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(id), 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(int id,
+                                  const QString& name,
+                                  bool isRegEx,
+                                  bool isCaseSensitive,
+                                  bool isEnabled,
+                                  bool isInverse,
+                                  const QString& sender,
+                                  const QString& chanName);
 
-    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) { REQUEST(ARG(highlightNick)) }
 
-    virtual inline void requestSetHighlightNick(int highlightNick)
+    inline void setHighlightNick(int highlightNick)
     {
-        REQUEST(ARG(highlightNick))
-    }
-
-    inline void setHighlightNick(int highlightNick) {
         _highlightNick = static_cast<HighlightNickType>(highlightNick);
-        _cacheNickConfigInvalid = true;
+        // Convert from HighlightRuleManager::HighlightNickType to
+        // NickHighlightMatcher::HighlightNickType
+        _nickMatcher.setHighlightMode(static_cast<NickHighlightMatcher::HighlightNickType>(_highlightNick));
     }
 
-    virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
+    virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive) { REQUEST(ARG(nicksCaseSensitive)) }
+
+    inline void setNicksCaseSensitive(bool nicksCaseSensitive)
     {
-        REQUEST(ARG(nicksCaseSensitive))
+        _nicksCaseSensitive = nicksCaseSensitive;
+        // Update nickname matcher, too
+        _nickMatcher.setCaseSensitive(nicksCaseSensitive);
     }
 
-    inline void setNicksCaseSensitive(bool nicksCaseSensitive) {
-        _nicksCaseSensitive = nicksCaseSensitive;
-        _cacheNickConfigInvalid = true;
+    /**
+     * Network removed from system
+     *
+     * Handles cleaning up cache from stale networks.
+     *
+     * @param id Network ID of removed network
+     */
+    inline void networkRemoved(NetworkId id)
+    {
+        // Clean up nickname matching cache
+        _nickMatcher.removeNetwork(id);
     }
 
 protected:
-    void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
+    void setHighlightRuleList(const QList<HighlightRule>HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
 
-    bool match(const QString &msgContents,
-               const QString &msgSender,
+    bool match(const NetworkId& netId,
+               const QString& msgContents,
+               const QString& msgSender,
                Message::Type msgType,
                Message::Flags msgFlags,
-               const QString &bufferName,
-               const QString &currentNick,
-               const QStringList identityNicks);
+               const QStringbufferName,
+               const QStringcurrentNick,
+               const QStringList& identityNicks);
 
 signals:
     void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
 
 private:
-    /**
-     * Update internal cache of expression matching if needed
-     */
-    void determineNickExpressions(const QString &currentNick,
-                                  const QStringList identityNicks) const;
+    HighlightRuleList _highlightRuleList = {};  ///< Custom highlight rule list
+    NickHighlightMatcher _nickMatcher = {};     ///< Nickname highlight matcher
 
-    /**
-     * Check if nickname matching cache is invalid
-     * @param currentNick
-     * @param identityNicks
-     * @return
-     */
-    bool cacheNickInvalid(const QString &currentNick, const QStringList identityNicks) const {
-        if (_cacheNickConfigInvalid) return true;
-        if (_cachedNickCurrent != currentNick) return true;
-        if (_cachedIdentityNicks != identityNicks) return true;
-    }
-
-    HighlightRuleList _highlightRuleList;
+    /// Nickname highlighting mode
     HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
-    bool _nicksCaseSensitive = false;
-
-    // These represent internal cache and should be safe to mutate in 'const' functions
-    mutable bool _cacheNickConfigInvalid = true;     ///< If true, nick match cache needs redone
-    mutable QString _cachedNickCurrent = {};         ///< Last cached current nick
-    mutable QStringList _cachedIdentityNicks = {};   ///< Last cached identity nicks
-    mutable ExpressionMatch _cachedNickMatcher = {}; ///< Expression match cache for nicks
+    bool _nicksCaseSensitive = false;  ///< If true, match nicknames with exact case
 };