qtui: Set proper icon for "About Quassel" menu option
[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::LongTime)) {
69         // toMSecs returns a qint64, signed rather than unsigned
70         out << (qint64) msg.timestamp().toMSecsSinceEpoch();
71     } else {
72         out << (quint32) msg.timestamp().toTime_t();
73     }
74
75     out << (quint32) msg.type()
76         << (quint8) msg.flags()
77         << msg.bufferInfo()
78         << msg.sender().toUtf8();
79
80     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes))
81         out << msg.senderPrefixes().toUtf8();
82
83     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::RichMessages)) {
84         out << msg.realName().toUtf8();
85         out << msg.avatarUrl().toUtf8();
86     }
87
88     out << msg.contents().toUtf8();
89     return out;
90 }
91
92
93 QDataStream &operator>>(QDataStream &in, Message &msg)
94 {
95     Q_ASSERT(SignalProxy::current());
96     Q_ASSERT(SignalProxy::current()->sourcePeer());
97
98     in >> msg._msgId;
99
100     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
101         // timestamp is a qint64, signed rather than unsigned
102         qint64 timeStamp;
103         in >> timeStamp;
104         msg._timestamp = QDateTime::fromMSecsSinceEpoch(timeStamp);
105     } else {
106         quint32 timeStamp;
107         in >> timeStamp;
108         msg._timestamp = QDateTime::fromTime_t(timeStamp);
109     }
110
111     quint32 type;
112     in >> type;
113     msg._type = Message::Type(type);
114
115     quint8 flags;
116     in >> flags;
117     msg._flags = Message::Flags(flags);
118
119     in >> msg._bufferInfo;
120
121     QByteArray sender;
122     in >> sender;
123     msg._sender = QString::fromUtf8(sender);
124
125     QByteArray senderPrefixes;
126     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes))
127         in >> senderPrefixes;
128     msg._senderPrefixes = QString::fromUtf8(senderPrefixes);
129
130     QByteArray realName;
131     QByteArray avatarUrl;
132     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::RichMessages)) {
133         in >> realName;
134         in >> avatarUrl;
135     }
136     msg._realName = QString::fromUtf8(realName);
137     msg._avatarUrl = QString::fromUtf8(avatarUrl);
138
139     QByteArray contents;
140     in >> contents;
141     msg._contents = QString::fromUtf8(contents);
142
143     return in;
144 }
145
146
147 QDebug operator<<(QDebug dbg, const Message &msg)
148 {
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();
156     return dbg;
157 }