PacketModel.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "PacketModel.h"
00025 // \todo
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     /*log = 0;
00035     logFile.setFileName (tr ("packets.log"));
00036     if (logFile.open (QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
00037         log = new QTextStream (&logFile);
00038         log->setCodec ("ISO 8859-1");
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; // timestamp, direction, name, ttl, hops, descriptorId, rawPayload
00066 }
00067 
00068 QVariant PacketModel::data (const QModelIndex &index, int role) const
00069 {
00070     PacketInfo *packetInfo;
00071 //  if (role != Qt::DisplayRole && role != Qt::DecorationRole)
00072 //      return QVariant();
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;   // tr ("  -->") : tr ("  <--");
00080     //  case 1:     return packetInfo->timestamp.toString ("hh:mm:ss:zzz");
00081         case 1:     return packetInfo->timestamp;
00082         case 2:     return packetInfo->packetName; // rename to typeName() ??
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);   // tr ("  -->") : tr ("  <--");
00093 
00094     if (role == Qt::DecorationRole && index.column() == 0)
00095         return (packetInfo->direction == Incomming) ? inIcon : outIcon;   // tr ("  -->") : tr ("  <--");
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 // static QString toHexString (const QByteArray &data)
00122 // {
00123 //  int dataSize = data.size();
00124 //  QString res;
00125 //  QTextStream stream (&res);
00126 //  hex (stream);
00127 //  stream.setFieldWidth (2);
00128 //  stream.setPadChar ('0');
00129 //  for (int i = 0; i < dataSize; i++)
00130 //      stream << " 0x" << static_cast <uint> (data[i]);
00131 //  stream.flush();
00132 //  return res;
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 //  if (packet->payloadDescriptor() != PingDescriptor && packet->payloadDescriptor() != PongDescriptor)
00150 //      return false;
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 //  packetInfo->packet = packet->copy();
00160 //  packetInfo->packet = packet;
00161     packetInfo->packetName  = packet->name(); // rename to typeName() ??
00162     packetInfo->packetTtl   = packet->ttl();
00163     packetInfo->packetHops  = packet->hops();
00164     packetInfo->packetDescriptorId  = packet->descriptorId().toString();
00165 //  packetInfo->packetRawPayload    = toHexString (packet->rawPayload());
00166     packetInfo->packetRawPayload    = toString (packet->rawPayload());
00167     packetInfo->timestamp = QTime::currentTime().toString ("hh:mm:ss:zzz");
00168     packetInfo->direction = direction;
00169 
00170     /*if (log)
00171         (*log)  << packetInfo->timestamp << "\t"
00172                 << packetInfo->packetName << "\t"
00173                 << packetInfo->packetTtl << "\t"
00174                 << packetInfo->packetHops << "\t"
00175                 << packetInfo->packetDescriptorId << "\t"
00176                 << packetInfo->packetRawPayload << endl;
00177     */
00178     if (packetList.count() > MAX_ITEMS_TO_HOLD) {
00179 //      beginRemoveRows (QModelIndex(), 0, 0);
00180         delete packetList.takeFirst();
00181         packetList.append (packetInfo);
00182         dataChanged (index (0, 0), index (packetList.count(), packetList.count()));
00183 //      endRemoveRows();
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 }