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