1 /*
2   protocol.h
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2013-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef GAMMARAY_PROTOCOL_H
30 #define GAMMARAY_PROTOCOL_H
31 
32 #include "gammaray_common_export.h"
33 
34 #include <QAbstractItemModel>
35 #include <QDataStream>
36 #include <QDebug>
37 #include <QVector>
38 #include <QModelIndex>
39 
40 #include <limits>
41 
42 namespace GammaRay {
43 /*! Helper functions and constants defining the communication protocol between client and server. */
44 namespace Protocol {
45 /*! Message payload size type. */
46 using PayloadSize = qint32;
47 /*! Remote object address type. */
48 using ObjectAddress = quint16;
49 /*! Message type type. */
50 using MessageType = quint8;
51 
52 /*! Invalid object address. */
53 static const ObjectAddress InvalidObjectAddress = 0;
54 /*! Address of the launcher remote object for probe <-> launcher communication. */
55 static const ObjectAddress LauncherAddress = std::numeric_limits<ObjectAddress>::max();
56 /*! Invalid message type. */
57 static const MessageType InvalidMessageType = 0;
58 
59 /*! Protocol message types. */
60 enum BuildInMessageType {
61     // object management
62     // client -> server
63     ObjectMonitored = InvalidMessageType + 1,
64     ObjectUnmonitored,
65 
66     // server -> client
67     ServerVersion,
68     ServerDataVersionNegotiated,
69 
70     ObjectMapReply,
71     ObjectAdded,
72     ObjectRemoved,
73 
74     // remote model messages
75     // client -> server
76     ClientDataVersionNegotiated,
77     ModelRowColumnCountRequest,
78     ModelContentRequest,
79     ModelHeaderRequest,
80     ModelSetDataRequest,
81     ModelSortRequest,
82     ModelSyncBarrier,
83     SelectionModelStateRequest,
84 
85     // server -> client
86     ModelRowColumnCountReply,
87     ModelContentReply,
88     ModelContentChanged,
89     ModelHeaderReply,
90     ModelHeaderChanged,
91     ModelRowsAdded,
92     ModelRowsMoved,
93     ModelRowsRemoved,
94     ModelColumnsAdded,
95     ModelColumnsMoved,
96     ModelColumnsRemoved,
97     ModelReset,
98     ModelLayoutChanged,
99 
100     // server <-> client
101     SelectionModelSelect,
102     SelectionModelCurrent,
103 
104     MethodCall,
105     PropertySyncRequest,
106     PropertyValuesChanged,
107 
108     ServerInfo,
109 
110     // probe settings provided by the launcher
111     ProbeSettings,
112     ServerAddress,
113     ServerLaunchError,
114 
115     MESSAGE_TYPE_COUNT // NOTE when changing this enum, also update MessageStatisticsModel!
116 };
117 
118 ///@cond internal
119 /*! Transport protocol representation of a model index element. */
120 class ModelIndexData
121 {
122 public:
123     explicit ModelIndexData(qint32 row_ = 0, qint32 column_ = 0)
row(row_)124         : row(row_), column(column_) {}
125 
126     qint32 row;
127     qint32 column;
128 };
129 /*! Transport protocol representation of a QModelIndex. */
130 using ModelIndex = QVector<ModelIndexData>;
131 
132 /*! Protocol representation of an QItemSelectionRange. */
133 struct ItemSelectionRange {
134     ModelIndex topLeft;
135     ModelIndex bottomRight;
136 };
137 /*! Protocol representation of an QItemSelection. */
138 using ItemSelection = QVector<ItemSelectionRange>;
139 
140 /*! Serializes a QModelIndex. */
141 GAMMARAY_COMMON_EXPORT ModelIndex fromQModelIndex(const QModelIndex &index);
142 
143 /*! Deserializes a QModelIndex. */
144 GAMMARAY_COMMON_EXPORT QModelIndex toQModelIndex(const QAbstractItemModel *model,
145                                                  const ModelIndex &index);
146 ///@endcond
147 
148 /*! Protocol version, must match exactly between client and server. */
149 GAMMARAY_COMMON_EXPORT qint32 version();
150 
151 /*! Broadcast format version. */
152 GAMMARAY_COMMON_EXPORT qint32 broadcastFormatVersion();
153 }
154 }
155 
156 ///@cond internal
157 QT_BEGIN_NAMESPACE
158 inline QDataStream& operator>>(QDataStream& s, GammaRay::Protocol::ModelIndexData& data)
159 {
160     s >> data.row >> data.column;
161     return s;
162 }
163 inline QDataStream& operator<<(QDataStream& s, const GammaRay::Protocol::ModelIndexData& data)
164 {
165     s << data.row << data.column;
166     return s;
167 }
168 
169 inline QDebug& operator<<(QDebug &s, const GammaRay::Protocol::ModelIndexData &data)
170 {
171     s << '(' << data.row << ',' << data.column << ')';
172     return s;
173 }
174 ///@endcond
175 
176 Q_DECLARE_TYPEINFO(GammaRay::Protocol::ModelIndexData, Q_MOVABLE_TYPE);
177 Q_DECLARE_TYPEINFO(GammaRay::Protocol::ItemSelectionRange, Q_MOVABLE_TYPE);
178 QT_END_NAMESPACE
179 
180 #endif
181