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