Add a settingspage for configuring the input widget
[quassel.git] / src / qtui / qtuimessageprocessor.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 by the Quassel Project                          *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) version 3.                                           *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include "qtuimessageprocessor.h"
22
23 #include "client.h"
24 #include "clientsettings.h"
25 #include "identity.h"
26 #include "messagemodel.h"
27 #include "network.h"
28
29 const int progressUpdateDelay = 100;  // ms between progress signal updates
30
31 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent)
32   : AbstractMessageProcessor(parent),
33     _processing(false),
34     _processMode(TimerBased)
35 {
36   NotificationSettings notificationSettings;
37   _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
38   _highlightNick = notificationSettings.highlightNick();
39   highlightListChanged(notificationSettings.highlightList());
40   notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
41   notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
42   notificationSettings.notify("Highlights/HighlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
43
44   _processTimer.setInterval(0);
45   connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
46 }
47
48 void QtUiMessageProcessor::reset() {
49   if(processMode() == TimerBased) {
50     if(_processTimer.isActive()) _processTimer.stop();
51     _processing = false;
52     _currentBatch.clear();
53     _processQueue.clear();
54   }
55 }
56
57 void QtUiMessageProcessor::process(Message &msg) {
58   checkForHighlight(msg);
59   preProcess(msg);
60   Client::messageModel()->insertMessage(msg);
61 }
62
63 void QtUiMessageProcessor::process(QList<Message> &msgs) {
64   QList<Message>::iterator msgIter = msgs.begin();
65   QList<Message>::iterator msgIterEnd = msgs.end();
66   while(msgIter != msgIterEnd) {
67     checkForHighlight(*msgIter);
68     preProcess(*msgIter);
69     msgIter++;
70   }
71   Client::messageModel()->insertMessages(msgs);
72   return;
73
74
75   if(msgs.isEmpty()) return;
76   _processQueue.append(msgs);
77   if(!isProcessing())
78     startProcessing();
79 }
80
81 void QtUiMessageProcessor::startProcessing() {
82   if(processMode() == TimerBased) {
83     if(_currentBatch.isEmpty() && _processQueue.isEmpty())
84       return;
85     _processing = true;
86     if(!_processTimer.isActive())
87       _processTimer.start();
88   }
89 }
90
91 void QtUiMessageProcessor::processNextMessage() {
92   if(_currentBatch.isEmpty()) {
93     if(_processQueue.isEmpty()) {
94       _processTimer.stop();
95       _processing = false;
96       return;
97     }
98     _currentBatch = _processQueue.takeFirst();
99   }
100   Message msg = _currentBatch.takeFirst();
101   process(msg);
102 }
103
104 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
105   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
106     return;
107
108   // TODO: Cache this (per network)
109   const Network *net = Client::network(msg.bufferInfo().networkId());
110   if(net && !net->myNick().isEmpty()) {
111     QStringList nickList;
112     if(_highlightNick == NotificationSettings::CurrentNick) {
113       nickList << net->myNick();
114     } else if(_highlightNick == NotificationSettings::AllNicks) {
115       const Identity *myIdentity = Client::identity(net->identity());
116       if(myIdentity)
117         nickList = myIdentity->nicks();
118       if(!nickList.contains(net->myNick()))
119         nickList.prepend(net->myNick());
120     }
121     foreach(QString nickname, nickList) {
122       QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
123       if(nickRegExp.indexIn(msg.contents()) >= 0) {
124         msg.setFlags(msg.flags() | Message::Highlight);
125         return;
126       }
127     }
128
129     for(int i = 0; i < _highlightRules.count(); i++) {
130       const HighlightRule &rule = _highlightRules.at(i);
131       if(!rule.isEnabled)
132         continue;
133
134       bool match = false;
135       if(rule.isRegExp) {
136         QRegExp rx(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
137         match = rx.exactMatch(msg.contents());
138       } else {
139         QRegExp rx("\\W" + QRegExp::escape(rule.name) + "\\W", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
140         match = (rx.indexIn(msg.contents()) >= 0);
141       }
142       if(match) {
143         msg.setFlags(msg.flags() | Message::Highlight);
144         return;
145       }
146     }
147   }
148 }
149
150 void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) {
151   _nicksCaseSensitive = variant.toBool();
152 }
153
154 void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
155   QVariantList varList = variant.toList();
156
157   _highlightRules.clear();
158   QVariantList::const_iterator iter = varList.constBegin();
159   while(iter != varList.constEnd()) {
160     QVariantMap rule = iter->toMap();
161     _highlightRules << HighlightRule(rule["Name"].toString(),
162                                      rule["Enable"].toBool(),
163                                      rule["CS"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
164                                      rule["RegEx"].toBool());
165     iter++;
166   }
167 }
168
169 void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
170   _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
171 }