54ee5702f95c97e88429ee9e6294bb420b3c1101
[quassel.git] / src / qtui / qtuimessageprocessor.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 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 QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent) : AbstractMessageProcessor(parent) {
30
31
32 }
33
34 void QtUiMessageProcessor::processMessage(Message &msg) {
35   checkForHighlight(msg);
36   Client::messageModel()->insertMessage(msg);
37 }
38
39 void QtUiMessageProcessor::processMessages(QList<Message> &msgs) {
40   foreach(Message msg, msgs) {
41     checkForHighlight(msg);
42     Client::messageModel()->insertMessage(msg);
43   }
44 }
45
46 // TODO optimize checkForHighlight
47 void QtUiMessageProcessor::checkForHighlight(Message &msg) {
48   if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
49     return;
50
51   NotificationSettings notificationSettings;
52   const Network *net = Client::network(msg.bufferInfo().networkId());
53   if(net && !net->myNick().isEmpty()) {
54     QStringList nickList;
55     if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
56       nickList << net->myNick();
57     } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
58       const Identity *myIdentity = Client::identity(net->identity());
59       if(myIdentity)
60         nickList = myIdentity->nicks();
61     }
62     foreach(QString nickname, nickList) {
63       QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
64       if(nickRegExp.exactMatch(msg.contents())) {
65         msg.setFlags(msg.flags() | Message::Highlight);
66         return;
67       }
68     }
69
70     foreach(QVariant highlight, notificationSettings.highlightList()) {
71       QVariantMap highlightRule = highlight.toMap();
72       if(!highlightRule["enable"].toBool())
73         continue;
74       Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
75       QString name = highlightRule["name"].toString();
76       QRegExp userRegExp;
77       if(highlightRule["regex"].toBool()) {
78         userRegExp = QRegExp(name, caseSensitivity);
79       } else {
80         userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
81       }
82       if(userRegExp.exactMatch(msg.contents())) {
83         msg.setFlags(msg.flags() | Message::Highlight);
84         return;
85       }
86     }
87   }
88 }