Implement sender realname/avatarurl storage
[quassel.git] / src / common / message.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 "message.h"
22
23 #include "util.h"
24 #include "peer.h"
25 #include "signalproxy.h"
26
27 #include <QDataStream>
28
29 Message::Message(const BufferInfo &bufferInfo, Type type, const QString &contents, const QString &sender,
30                  const QString &senderPrefixes, const QString &realName, const QString &avatarUrl, Flags flags)
31     : _timestamp(QDateTime::currentDateTime().toUTC()),
32     _bufferInfo(bufferInfo),
33     _contents(contents),
34     _sender(sender),
35     _senderPrefixes(senderPrefixes),
36     _realName(realName),
37     _avatarUrl(avatarUrl),
38     _type(type),
39     _flags(flags)
40 {
41 }
42
43
44 Message::Message(const QDateTime &ts, const BufferInfo &bufferInfo, Type type, const QString &contents,
45                  const QString &sender, const QString &senderPrefixes, const QString &realName,
46                  const QString &avatarUrl, Flags flags)
47     : _timestamp(ts),
48     _bufferInfo(bufferInfo),
49     _contents(contents),
50     _sender(sender),
51     _senderPrefixes(senderPrefixes),
52     _realName(realName),
53     _avatarUrl(avatarUrl),
54     _type(type),
55     _flags(flags)
56 {
57 }
58
59
60 QDataStream &operator<<(QDataStream &out, const Message &msg)
61 {
62     Q_ASSERT(SignalProxy::current());
63     Q_ASSERT(SignalProxy::current()->targetPeer());
64
65     // We do not serialize the sender prefixes until we have a new protocol or client-features implemented
66     out << msg.msgId();
67
68     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongMessageTime)) {
69         out << (quint64) msg.timestamp().toMSecsSinceEpoch();
70     } else {
71         out << (quint32) msg.timestamp().toTime_t();
72     }
73
74     out << (quint32) msg.type()
75         << (quint8) msg.flags()
76         << msg.bufferInfo()
77         << msg.sender().toUtf8();
78
79     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes))
80         out << msg.senderPrefixes().toUtf8();
81
82     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::RichMessages)) {
83         out << msg.realName().toUtf8();
84         out << msg.avatarUrl().toUtf8();
85     }
86
87     out << msg.contents().toUtf8();
88     return out;
89 }
90
91
92 QDataStream &operator>>(QDataStream &in, Message &msg)
93 {
94     Q_ASSERT(SignalProxy::current());
95     Q_ASSERT(SignalProxy::current()->sourcePeer());
96
97     in >> msg._msgId;
98
99     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongMessageTime)) {
100         quint64 timeStamp;
101         in >> timeStamp;
102         msg._timestamp = QDateTime::fromMSecsSinceEpoch(timeStamp);
103     } else {
104         quint32 timeStamp;
105         in >> timeStamp;
106         msg._timestamp = QDateTime::fromTime_t(timeStamp);
107     }
108
109     quint32 type;
110     in >> type;
111     msg._type = Message::Type(type);
112
113     quint8 flags;
114     in >> flags;
115     msg._flags = Message::Flags(flags);
116
117     in >> msg._bufferInfo;
118
119     QByteArray sender;
120     in >> sender;
121     msg._sender = QString::fromUtf8(sender);
122
123     QByteArray senderPrefixes;
124     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes))
125         in >> senderPrefixes;
126     msg._senderPrefixes = QString::fromUtf8(senderPrefixes);
127
128     QByteArray realName;
129     QByteArray avatarUrl;
130     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::RichMessages)) {
131         in >> realName;
132         in >> avatarUrl;
133     }
134     msg._realName = QString::fromUtf8(realName);
135     msg._avatarUrl = QString::fromUtf8(avatarUrl);
136
137     QByteArray contents;
138     in >> contents;
139     msg._contents = QString::fromUtf8(contents);
140
141     return in;
142 }
143
144
145 QDebug operator<<(QDebug dbg, const Message &msg)
146 {
147     dbg.nospace() << qPrintable(QString("Message(MsgId:")) << msg.msgId()
148     << qPrintable(QString(",")) << msg.timestamp()
149     << qPrintable(QString(", Type:")) << msg.type()
150     << qPrintable(QString(", RealName:")) << msg.realName()
151     << qPrintable(QString(", AvatarURL:")) << msg.avatarUrl()
152     << qPrintable(QString(", Flags:")) << msg.flags() << qPrintable(QString(")"))
153     << msg.senderPrefixes() << msg.sender() << ":" << msg.contents();
154     return dbg;
155 }