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