1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
25 #include "signalproxy.h"
27 #include <QDataStream>
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),
35 _senderPrefixes(senderPrefixes),
37 _avatarUrl(avatarUrl),
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)
48 _bufferInfo(bufferInfo),
51 _senderPrefixes(senderPrefixes),
53 _avatarUrl(avatarUrl),
60 QDataStream &operator<<(QDataStream &out, const Message &msg)
62 Q_ASSERT(SignalProxy::current());
63 Q_ASSERT(SignalProxy::current()->targetPeer());
65 // We do not serialize the sender prefixes until we have a new protocol or client-features implemented
68 if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) {
69 // toMSecs returns a qint64, signed rather than unsigned
70 out << (qint64) msg.timestamp().toMSecsSinceEpoch();
72 out << (quint32) msg.timestamp().toTime_t();
75 out << (quint32) msg.type()
76 << (quint8) msg.flags()
78 << msg.sender().toUtf8();
80 if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes))
81 out << msg.senderPrefixes().toUtf8();
83 if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::RichMessages)) {
84 out << msg.realName().toUtf8();
85 out << msg.avatarUrl().toUtf8();
88 out << msg.contents().toUtf8();
93 QDataStream &operator>>(QDataStream &in, Message &msg)
95 Q_ASSERT(SignalProxy::current());
96 Q_ASSERT(SignalProxy::current()->sourcePeer());
100 if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
101 // timestamp is a qint64, signed rather than unsigned
104 msg._timestamp = QDateTime::fromMSecsSinceEpoch(timeStamp);
108 msg._timestamp = QDateTime::fromTime_t(timeStamp);
113 msg._type = Message::Type(type);
117 msg._flags = Message::Flags(flags);
119 in >> msg._bufferInfo;
123 msg._sender = QString::fromUtf8(sender);
125 QByteArray senderPrefixes;
126 if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes))
127 in >> senderPrefixes;
128 msg._senderPrefixes = QString::fromUtf8(senderPrefixes);
131 QByteArray avatarUrl;
132 if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::RichMessages)) {
136 msg._realName = QString::fromUtf8(realName);
137 msg._avatarUrl = QString::fromUtf8(avatarUrl);
141 msg._contents = QString::fromUtf8(contents);
147 QDebug operator<<(QDebug dbg, const Message &msg)
149 dbg.nospace() << qPrintable(QString("Message(MsgId:")) << msg.msgId()
150 << qPrintable(QString(",")) << msg.timestamp()
151 << qPrintable(QString(", Type:")) << msg.type()
152 << qPrintable(QString(", RealName:")) << msg.realName()
153 << qPrintable(QString(", AvatarURL:")) << msg.avatarUrl()
154 << qPrintable(QString(", Flags:")) << msg.flags() << qPrintable(QString(")"))
155 << msg.senderPrefixes() << msg.sender() << ":" << msg.contents();