PacketModel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "PacketModel.h"
00025
00026 #include "Gnutella/Packets/Packet.h"
00027
00028 PacketModel::PacketModel()
00029 : packetList(), inIcon(), outIcon(), packetFlags(), flagValues()
00030 {
00031 inIcon = QIcon ("Icons/inArrow.png");
00032 outIcon = QIcon ("Icons/outArrow.png");
00033
00034
00035
00036
00037
00038
00039
00040
00041 flagValues.insert (tr ("Ping"), 0);
00042 flagValues.insert (tr ("Pong"), 2);
00043 flagValues.insert (tr ("Query"), 4);
00044 flagValues.insert (tr ("QueryHits"), 8);
00045 flagValues.insert (tr ("QueryRoutingReset"), 16);
00046 flagValues.insert (tr ("QueryRoutingPatch"), 32);
00047
00048 packetFlags = ~packetFlags;
00049 }
00050
00051 PacketModel::~PacketModel()
00052 {
00053 foreach (PacketInfo *info, packetList)
00054 delete info;
00055 packetList.clear();
00056 }
00057
00058 int PacketModel::rowCount (const QModelIndex &) const
00059 {
00060 return packetList.count();
00061 }
00062
00063 int PacketModel::columnCount (const QModelIndex &) const
00064 {
00065 return 7;
00066 }
00067
00068 QVariant PacketModel::data (const QModelIndex &index, int role) const
00069 {
00070 PacketInfo *packetInfo;
00071
00072
00073
00074 packetInfo = packetList.value (index.row());
00075
00076 if (role == Qt::DisplayRole) {
00077 switch (index.column())
00078 {
00079 case 0: return (packetInfo->direction == Incomming) ? inIcon : outIcon;
00080
00081 case 1: return packetInfo->timestamp;
00082 case 2: return packetInfo->packetName;
00083 case 3: return static_cast <uint> (packetInfo->packetTtl);
00084 case 4: return static_cast <uint> (packetInfo->packetHops);
00085 case 5: return packetInfo->packetDescriptorId;
00086 case 6: return packetInfo->packetRawPayload;
00087 default: return QVariant();
00088 }
00089 }
00090
00091 if (role == Qt::TextColorRole)
00092 return (packetInfo->direction == Incomming) ? QColor(8, 28, 194) : QColor(26, 138, 19);
00093
00094 if (role == Qt::DecorationRole && index.column() == 0)
00095 return (packetInfo->direction == Incomming) ? inIcon : outIcon;
00096
00097 if (role == Qt::TextAlignmentRole && index.column() != 6)
00098 return static_cast <uint> (Qt::AlignHCenter);
00099
00100 return QVariant();
00101 }
00102
00103 QVariant PacketModel::headerData (int section, Qt::Orientation orientation, int role) const
00104 {
00105 if (orientation == Qt::Vertical || role != Qt::DisplayRole)
00106 return QVariant();
00107
00108 switch (section)
00109 {
00110 case 0: return tr ("Dir");
00111 case 1: return tr ("Timestamp");
00112 case 2: return tr ("Type");
00113 case 3: return tr ("TTL");
00114 case 4: return tr ("Hops");
00115 case 5: return tr ("Descriptor ID ");
00116 case 6: return tr ("Payload");
00117 default: return QVariant();
00118 }
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 static QString toString (const QByteArray &ba)
00136 {
00137 int len = ba.count();
00138 QString result;
00139
00140 for (int i=0; i<len; i++) {
00141 result.append (ba.at (i));
00142 }
00143
00144 return result;
00145 }
00146
00147 bool PacketModel::insertPacket (const Packet *packet, Direction direction)
00148 {
00149
00150
00151
00152 QString name = packet->name();
00153 int i = flagValues.value (name);
00154 if (!((packetFlags & PacketFlags (i)) && (packetFlags & PacketFlags (1<<(direction + 6))))) {
00155 return 0;
00156 }
00157
00158 PacketInfo *packetInfo = new PacketInfo;
00159
00160
00161 packetInfo->packetName = packet->name();
00162 packetInfo->packetTtl = packet->ttl();
00163 packetInfo->packetHops = packet->hops();
00164 packetInfo->packetDescriptorId = packet->descriptorId().toString();
00165
00166 packetInfo->packetRawPayload = toString (packet->rawPayload());
00167 packetInfo->timestamp = QTime::currentTime().toString ("hh:mm:ss:zzz");
00168 packetInfo->direction = direction;
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 if (packetList.count() > MAX_ITEMS_TO_HOLD) {
00179
00180 delete packetList.takeFirst();
00181 packetList.append (packetInfo);
00182 dataChanged (index (0, 0), index (packetList.count(), packetList.count()));
00183
00184 } else {
00185 beginInsertRows (QModelIndex(), packetList.count(), packetList.count());
00186 packetList.append (packetInfo);
00187 endInsertRows();
00188 }
00189
00190 return true;
00191 }
00192
00193 void PacketModel::setFlags (PacketFlags &flags)
00194 {
00195 packetFlags = flags;
00196
00197 return;
00198 }