8ac1f4b04a0f6b3f80f82806e8f2b562480786a7
[quassel.git] / src / common / bufferinfo.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 <QString>
22 #include <QDataStream>
23 #include <QDebug>
24 #include <QByteArray>
25 #include <utility>
26
27 #include "bufferinfo.h"
28
29 #include "util.h"
30
31 BufferInfo::BufferInfo()
32     : _bufferId(0),
33     _netid(0),
34     _type(InvalidBuffer),
35     _groupId(0),
36     _bufferName(QString())
37 {
38 }
39
40
41 BufferInfo::BufferInfo(BufferId id,  NetworkId networkid, Type type, uint gid, QString buf)
42     : _bufferId(id),
43     _netid(networkid),
44     _type(type),
45     _groupId(gid),
46     _bufferName(std::move(buf))
47 {
48 }
49
50
51 BufferInfo BufferInfo::fakeStatusBuffer(NetworkId networkId)
52 {
53     return BufferInfo(0, networkId, StatusBuffer);
54 }
55
56
57 QString BufferInfo::bufferName() const
58 {
59     if (isChannelName(_bufferName))
60         return _bufferName;
61     else
62         return nickFromMask(_bufferName);  // FIXME get rid of global functions and use the Network stuff instead!
63 }
64
65
66 bool BufferInfo::acceptsRegularMessages() const
67 {
68     if(_type == StatusBuffer || _type == InvalidBuffer)
69         return false;
70     return true;
71 }
72
73
74 QDebug operator<<(QDebug dbg, const BufferInfo &b)
75 {
76     dbg.nospace() << "(bufId: " << b.bufferId() << ", netId: " << b.networkId() << ", groupId: " << b.groupId() << ", buf: " << b.bufferName() << ")";
77     return dbg.space();
78 }
79
80
81 QDataStream &operator<<(QDataStream &out, const BufferInfo &bufferInfo)
82 {
83     out << bufferInfo._bufferId << bufferInfo._netid << (qint16)bufferInfo._type << bufferInfo._groupId << bufferInfo._bufferName.toUtf8();
84     return out;
85 }
86
87
88 QDataStream &operator>>(QDataStream &in, BufferInfo &bufferInfo)
89 {
90     QByteArray buffername;
91     qint16 bufferType;
92     in >> bufferInfo._bufferId >> bufferInfo._netid >> bufferType >> bufferInfo._groupId >> buffername;
93     bufferInfo._type = (BufferInfo::Type)bufferType;
94     bufferInfo._bufferName = QString::fromUtf8(buffername);
95     return in;
96 }
97
98
99 uint qHash(const BufferInfo &bufferid)
100 {
101     return qHash(bufferid._bufferId);
102 }