cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / common / message.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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     out << msg.msgId();
75
76     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) {
77         // toMSecs returns a qint64, signed rather than unsigned
78         out << (qint64) msg.timestamp().toMSecsSinceEpoch();
79     }
80     else {
81         out << (quint32) msg.timestamp().toTime_t();
82     }
83
84     out << (quint32) msg.type()
85         << (quint8) msg.flags()
86         << msg.bufferInfo()
87         << msg.sender().toUtf8();
88
89     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::SenderPrefixes))
90         out << msg.senderPrefixes().toUtf8();
91
92     if (SignalProxy::current()->targetPeer()->hasFeature(Quassel::Feature::RichMessages)) {
93         out << msg.realName().toUtf8();
94         out << msg.avatarUrl().toUtf8();
95     }
96
97     out << msg.contents().toUtf8();
98     return out;
99 }
100
101 QDataStream& operator>>(QDataStream& in, Message& msg)
102 {
103     Q_ASSERT(SignalProxy::current());
104     Q_ASSERT(SignalProxy::current()->sourcePeer());
105
106     in >> msg._msgId;
107
108     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
109         // timestamp is a qint64, signed rather than unsigned
110         qint64 timeStamp;
111         in >> timeStamp;
112         msg._timestamp = QDateTime::fromMSecsSinceEpoch(timeStamp);
113     }
114     else {
115         quint32 timeStamp;
116         in >> timeStamp;
117         msg._timestamp = QDateTime::fromTime_t(timeStamp);
118     }
119
120     quint32 type;
121     in >> type;
122     msg._type = Message::Type(type);
123
124     quint8 flags;
125     in >> flags;
126     msg._flags = Message::Flags(flags);
127
128     in >> msg._bufferInfo;
129
130     QByteArray sender;
131     in >> sender;
132     msg._sender = QString::fromUtf8(sender);
133
134     QByteArray senderPrefixes;
135     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::SenderPrefixes))
136         in >> senderPrefixes;
137     msg._senderPrefixes = QString::fromUtf8(senderPrefixes);
138
139     QByteArray realName;
140     QByteArray avatarUrl;
141     if (SignalProxy::current()->sourcePeer()->hasFeature(Quassel::Feature::RichMessages)) {
142         in >> realName;
143         in >> avatarUrl;
144     }
145     msg._realName = QString::fromUtf8(realName);
146     msg._avatarUrl = QString::fromUtf8(avatarUrl);
147
148     QByteArray contents;
149     in >> contents;
150     msg._contents = QString::fromUtf8(contents);
151
152     return in;
153 }
154
155 QDebug operator<<(QDebug dbg, const Message& msg)
156 {
157     dbg.nospace() << qPrintable(QString("Message(MsgId:")) << msg.msgId()
158                   << qPrintable(QString(",")) << msg.timestamp()
159                   << qPrintable(QString(", Type:")) << msg.type()
160                   << qPrintable(QString(", RealName:")) << msg.realName()
161                   << qPrintable(QString(", AvatarURL:")) << msg.avatarUrl()
162                   << qPrintable(QString(", Flags:")) << msg.flags()
163                   << qPrintable(QString(")"))
164                   << msg.senderPrefixes() << msg.sender() << ":"
165                   << msg.contents();
166     return dbg;
167 }