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