Implement core-side highlights
[quassel.git] / src / common / messageevent.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "messageevent.h"
22
23 Event *MessageEvent::create(EventManager::EventType type, QVariantMap &map, Network *network)
24 {
25     if (type == EventManager::MessageEvent)
26         return new MessageEvent(type, map, network);
27
28     return 0;
29 }
30
31
32 MessageEvent::MessageEvent(Message::Type msgType, Network *net, const QString &msg, const QString &sender, const QString &target,
33     Message::Flags flags, const QDateTime &timestamp)
34     : NetworkEvent(EventManager::MessageEvent, net),
35     _msgType(msgType),
36     _text(msg),
37     _sender(sender),
38     _target(target),
39     _msgFlags(flags)
40 {
41     IrcChannel *channel = network()->ircChannel(_target);
42     if (!channel) {
43         if (!_target.isEmpty() && network()->prefixes().contains(_target.at(0)))
44             _target = _target.mid(1);
45
46         if (_target.startsWith('$') || _target.startsWith('#'))
47             _target = nickFromMask(sender);
48     }
49
50     _bufferType = bufferTypeByTarget(_target);
51
52     if (timestamp.isValid())
53         setTimestamp(timestamp);
54     else
55         setTimestamp(QDateTime::currentDateTime());
56 }
57
58
59 MessageEvent::MessageEvent(EventManager::EventType type, QVariantMap &map, Network *network)
60     : NetworkEvent(type, map, network)
61 {
62     _msgType = static_cast<Message::Type>(map.take("messageType").toInt());
63     _msgFlags = static_cast<Message::Flags>(map.take("messageFlags").toInt());
64     _bufferType = static_cast<BufferInfo::Type>(map.take("bufferType").toInt());
65     _text = map.take("text").toString();
66     _sender = map.take("sender").toString();
67     _target = map.take("target").toString();
68 }
69
70
71 void MessageEvent::toVariantMap(QVariantMap &map) const
72 {
73     NetworkEvent::toVariantMap(map);
74     map["messageType"] = msgType();
75     map["messageFlags"] = (int)msgFlags();
76     map["bufferType"] = bufferType();
77     map["text"] = text();
78     map["sender"] = sender();
79     map["target"] = target();
80 }
81
82
83 BufferInfo::Type MessageEvent::bufferTypeByTarget(const QString &target) const
84 {
85     if (target.isEmpty())
86         return BufferInfo::StatusBuffer;
87
88     if (network()->isChannelName(target))
89         return BufferInfo::ChannelBuffer;
90
91     return BufferInfo::QueryBuffer;
92 }