Provide (de)serialization for all event types
[quassel.git] / src / common / ircevent.h
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 #ifndef IRCEVENT_H
22 #define IRCEVENT_H
23
24 #include "networkevent.h"
25 #include "util.h"
26
27 class IrcEvent : public NetworkEvent {
28 public:
29   explicit IrcEvent(EventManager::EventType type, Network *network, const QString &prefix, const QStringList &params = QStringList())
30     : NetworkEvent(type, network),
31       _prefix(prefix),
32       _params(params)
33   {}
34
35   inline QString prefix() const { return _prefix; }
36   inline void setPrefix(const QString &prefix) { _prefix = prefix; }
37
38   inline QString nick() const { return nickFromMask(prefix()); }
39
40   inline QStringList params() const { return _params; }
41   inline void setParams(const QStringList &params) { _params = params; }
42
43   static Event *create(EventManager::EventType type, QVariantMap &map, Network *network);
44
45 protected:
46   explicit IrcEvent(EventManager::EventType type, QVariantMap &map, Network *network);
47   void toVariantMap(QVariantMap &map) const;
48
49   virtual inline QString className() const { return "IrcEvent"; }
50   virtual inline void debugInfo(QDebug &dbg) const {
51     NetworkEvent::debugInfo(dbg);
52     dbg << ", prefix = " << qPrintable(prefix())
53         << ", params = " << params();
54   }
55
56 private:
57   QString _prefix;
58   QStringList _params;
59
60 };
61
62 class IrcEventNumeric : public IrcEvent {
63 public:
64   explicit IrcEventNumeric(uint number, Network *network, const QString &prefix, const QString &target, const QStringList &params = QStringList())
65     : IrcEvent(EventManager::IrcEventNumeric, network, prefix, params),
66       _number(number),
67       _target(target)
68   {}
69
70   inline uint number() const { return _number; }
71
72   inline QString target() const { return _target; }
73   inline void setTarget(const QString &target) { _target = target; }
74
75 protected:
76   explicit IrcEventNumeric(EventManager::EventType type, QVariantMap &map, Network *network);
77   void toVariantMap(QVariantMap &map) const;
78
79   virtual inline QString className() const { return "IrcEventNumeric"; }
80   virtual inline void debugInfo(QDebug &dbg) const {
81     dbg << ", num = " << number();
82     NetworkEvent::debugInfo(dbg);
83     dbg << ", target = " << qPrintable(target())
84         << ", prefix = " << qPrintable(prefix())
85         << ", params = " << params();
86   }
87
88 private:
89   uint _number;
90   QString _target;
91
92   friend class IrcEvent;
93 };
94
95 class IrcEventRawMessage : public IrcEvent {
96 public:
97   explicit inline IrcEventRawMessage(EventManager::EventType type, Network *network,
98                                      const QByteArray &rawMessage, const QString &prefix, const QString &target,
99                                      const QDateTime &timestamp = QDateTime())
100     : IrcEvent(type, network, prefix, QStringList() << target),
101       _rawMessage(rawMessage)
102   {
103     setTimestamp(timestamp);
104   }
105
106   inline QString target() const { return params().at(0); }
107   inline void setTarget(const QString &target) { setParams(QStringList() << target); }
108
109   inline QByteArray rawMessage() const { return _rawMessage; }
110   inline void setRawMessage(const QByteArray &rawMessage) { _rawMessage = rawMessage; }
111
112 protected:
113   explicit IrcEventRawMessage(EventManager::EventType type, QVariantMap &map, Network *network);
114   void toVariantMap(QVariantMap &map) const;
115
116   virtual inline QString className() const { return "IrcEventRawMessage"; }
117   virtual inline void debugInfo(QDebug &dbg) const {
118     NetworkEvent::debugInfo(dbg);
119     dbg << ", target = " << qPrintable(target())
120         << ", prefix = " << qPrintable(prefix())
121         << ", msg = " << rawMessage();
122   }
123
124
125 private:
126   QByteArray _rawMessage;
127
128   friend class IrcEvent;
129 };
130
131 #endif