speed improvement to checkForHighlight (non longer uses QSettings itself)
authorMarcus Eggenberger <egs@quassel-irc.org>
Fri, 22 Aug 2008 16:11:46 +0000 (18:11 +0200)
committerMarcus Eggenberger <egs@quassel-irc.org>
Fri, 22 Aug 2008 16:11:46 +0000 (18:11 +0200)
src/qtui/qtuimessageprocessor.cpp
src/qtui/qtuimessageprocessor.h

index a9209ae..ff59dde 100644 (file)
 
 const int progressUpdateDelay = 100;  // ms between progress signal updates
 
-QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) : AbstractMessageProcessor(parent) {
-  _msgsProcessed = _msgCount = 0;
-  _processing = false;
-  _processMode = TimerBased;
+QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent)
+  : AbstractMessageProcessor(parent),
+    _processing(false),
+    _processMode(TimerBased),
+    _msgsProcessed(0),
+    _msgCount(0)
+{
+  NotificationSettings notificationSettings;
+  _highlightNick = notificationSettings.highlightNick();
+  highlightListChanged(notificationSettings.highlightList());
+  notificationSettings.notify("highlightList", this, SLOT(highlightListChanged(const QVariant &)));
+  notificationSettings.notify("highlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
+  
   _processTimer.setInterval(0);
   connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
 }
@@ -99,18 +108,17 @@ void QtUiMessageProcessor::updateProgress(bool start) {
   }
 }
 
-// TODO optimize checkForHighlight
 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
     return;
 
-  NotificationSettings notificationSettings;
+  //NotificationSettings notificationSettings;
   const Network *net = Client::network(msg.bufferInfo().networkId());
   if(net && !net->myNick().isEmpty()) {
     QStringList nickList;
-    if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
+    if(_highlightNick == NotificationSettings::CurrentNick) {
       nickList << net->myNick();
-    } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
+    } else if(_highlightNick == NotificationSettings::AllNicks) {
       const Identity *myIdentity = Client::identity(net->identity());
       if(myIdentity)
         nickList = myIdentity->nicks();
@@ -123,17 +131,16 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) {
       }
     }
 
-    foreach(QVariant highlight, notificationSettings.highlightList()) {
-      QVariantMap highlightRule = highlight.toMap();
-      if(!highlightRule["enable"].toBool())
-        continue;
-      Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
-      QString name = highlightRule["name"].toString();
+    for(int i = 0; i < _highlightRules.count(); i++) {
+      const HighlightRule &rule = _highlightRules[i];
+      if(!rule.isEnabled)
+       continue;
+
       QRegExp userRegExp;
-      if(highlightRule["regex"].toBool()) {
-        userRegExp = QRegExp(name, caseSensitivity);
+      if(rule.isRegExp) {
+        userRegExp = QRegExp(rule.name, rule.caseSensitive);
       } else {
-        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
+        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(rule.name) + "(\\W.*)?$", rule.caseSensitive);
       }
       if(userRegExp.exactMatch(msg.contents())) {
         msg.setFlags(msg.flags() | Message::Highlight);
@@ -142,3 +149,23 @@ void QtUiMessageProcessor::checkForHighlight(Message &msg) {
     }
   }
 }
+
+void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
+  QVariantList varList = variant.toList();
+
+  _highlightRules.clear();
+  QVariantList::const_iterator iter = varList.constBegin();
+  QVariantList::const_iterator iterEnd = varList.constEnd();
+  while(iter != iterEnd) {
+    QVariantMap rule;
+    _highlightRules << HighlightRule(rule["name"].toString(),
+                                    rule["enable"].toBool(),
+                                    rule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
+                                    rule["regex"].toBool());
+    iter++;
+  }
+}
+
+void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
+  _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
+}
index 553d580..57ed9bc 100644 (file)
 class QtUiMessageProcessor : public AbstractMessageProcessor {
   Q_OBJECT
 
-  public:
-    enum Mode {
-      TimerBased,
-      Concurrent
-    };
+public:
+  enum Mode {
+    TimerBased,
+    Concurrent
+  };
 
-    QtUiMessageProcessor(QObject *parent);
+  QtUiMessageProcessor(QObject *parent);
 
-    inline bool isProcessing() const { return _processing; }
-    inline Mode processMode() const { return _processMode; }
+  inline bool isProcessing() const { return _processing; }
+  inline Mode processMode() const { return _processMode; }
 
-    void reset();
+  void reset();
 
-  public slots:
-    void process(Message &msg);
-    void process(QList<Message> &msgs);
+public slots:
+  void process(Message &msg);
+  void process(QList<Message> &msgs);
 
-  private slots:
-    void processNextMessage();
+private slots:
+  void processNextMessage();
+  void highlightListChanged(const QVariant &variant);
+  void highlightNickChanged(const QVariant &variant);
 
-  private:
-    void checkForHighlight(Message &msg);
-    void startProcessing();
-    void updateProgress(bool start = false);
+private:
+  void checkForHighlight(Message &msg);
+  void startProcessing();
+  void updateProgress(bool start = false);
 
-    QList<QList<Message> > _processQueue;
-    QList<Message> _currentBatch;
-    QTimer _processTimer;
-    bool _processing;
-    Mode _processMode;
-    int _msgsProcessed, _msgCount;
-    QTime _progressTimer;
+  QList<QList<Message> > _processQueue;
+  QList<Message> _currentBatch;
+  QTimer _processTimer;
+  bool _processing;
+  Mode _processMode;
+  int _msgsProcessed, _msgCount;
+  QTime _progressTimer;
+
+  struct HighlightRule {
+    QString name;
+    bool isEnabled;
+    Qt::CaseSensitivity caseSensitive;
+    bool isRegExp;
+    inline HighlightRule(const QString &name, bool enabled, Qt::CaseSensitivity cs, bool regExp) : name(name), isEnabled(enabled), caseSensitive(cs), isRegExp(regExp) {}
+  };
+  
+  QList<HighlightRule> _highlightRules;
+  NotificationSettings::HighlightNickType _highlightNick;
 };
 
 #endif