Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

packetview.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2002-2004 Michael 'Mickey' Lauer.  All rights reserved.
00003 **
00004 ** This file is part of Wellenreiter II.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 **********************************************************************/
00015 
00016 #include "packetview.h"
00017 
00018 /* OPIE */
00019 #include <opie2/opcap.h>
00020 #include <opie2/odebug.h>
00021 #include <opie2/olistview.h>
00022 #include <opie2/oapplication.h>
00023 #include <qpe/config.h>
00024 
00025 /* QT */
00026 #include <qfont.h>
00027 #include <qlabel.h>
00028 #include <qlayout.h>
00029 #include <qlist.h>
00030 #include <qlistview.h>
00031 #include <qobjectlist.h>
00032 #include <qspinbox.h>
00033 #include <qtextview.h>
00034 
00035 using namespace Opie::Net;
00036 using namespace Opie::Core;
00037 using namespace Opie::Ui;
00038 
00039 PacketView::PacketView( QWidget * parent, const char * name, WFlags f )
00040            :QFrame( parent, name, f )
00041 {
00042     Config c( "qpe" );
00043     c.setGroup( "Appearance" );
00044     
00045     _number = new QSpinBox( this );
00046     _number->setPrefix( "Pkt# " );
00047     _label = new QLabel( this );
00048     _list = new OListView( this );
00049     _list->addColumn( "#" );
00050     _list->addColumn( "Packet Type" );
00051     _list->setColumnAlignment( 0, Qt::AlignCenter );
00052     _list->setColumnAlignment( 1, Qt::AlignLeft );
00053     _list->setAllColumnsShowFocus( true );
00054 
00055     _hex = new QTextView( this );
00056     _hex->setMargin( 0 );
00057     _hex->setFont( QFont( c.readEntry( "FixedFontFamily", "Fixed" ), c.readNumEntry( "FixedFontSize", 8 ) ) );
00058 
00059     QVBoxLayout* vb = new QVBoxLayout( this, 2, 2 );
00060     QHBoxLayout* hb = new QHBoxLayout( vb, 2 );
00061     hb->addWidget( _label, 5 );
00062     hb->addWidget( _number, 2 );
00063     vb->addWidget( _list, 3 );
00064     vb->addWidget( _hex, 4 ); // allow a bit (4/3) more space
00065 
00066     _packets.setAutoDelete( true );
00067 
00068     connect( _number, SIGNAL( valueChanged( int ) ), this, SLOT( showPacket( int ) ) );
00069     connect( parent, SIGNAL( currentChanged( QWidget *) ), this, SLOT( activated( QWidget* ) ) );
00070 
00071     clear();
00072 
00073 }
00074 
00075 void PacketView::add( const OPacket* p, int size )
00076 {
00077     /*
00078      * don't scroll away when somebody views packages
00079      * while scanning
00080      */
00081     int  value = _number->value();
00082     bool last  = (value == static_cast<int>( _packets.count() ) );
00083 
00084     odebug << "PacketView::add() size = " << size << oendl;
00085     if ( size == -1 ) // unlimited buffer
00086     {
00087         _packets.append( p );
00088     }
00089     else
00090     {   // limited buffer, limit = size
00091         while ( _packets.count() >= static_cast<uint>( size ) )
00092         {
00093             _packets.removeFirst();
00094             --value;
00095         }
00096 
00097         /* check if we lost our packet */
00098         last = ( value < 1 );
00099         _packets.append( p );
00100     }
00101 
00102     _number->setMinValue( 1 );
00103     _number->setMaxValue( _packets.count() );
00104     _number->setValue( last ? _packets.count() : value );
00105 }
00106 
00107 void PacketView::showPacket( int number )
00108 {
00109     _list->clear();
00110     _hex->setText("");
00111     const OPacket* p = _packets.at( number-1 );
00112 
00113     if ( p )
00114     {
00115         _doSubPackets( const_cast<QObjectList*>( p->children() ), 0 );
00116         _doHexPacket( p );
00117         QDateTime dt; dt.setTime_t( p->timeval().tv_sec );
00118         _label->setText( dt.toString() + QString().sprintf( " Len=%d", p->len() ) );
00119     }
00120     else
00121     {
00122         odebug << "D'oh! No packet!" << oendl;
00123     }
00124 }
00125 
00126 void PacketView::activated( QWidget* w )
00127 {
00128     if ( ( this == w ) && _packets.count() )
00129     {
00130         _number->setValue( 1 );
00131     }
00132 }
00133 
00134 void PacketView::_doSubPackets( QObjectList* l, int counter )
00135 {
00136     if (!l) return;
00137     QObject* o = l->first();
00138     while ( o )
00139     {
00140         new OListViewItem( _list, QString::number( counter++ ), o->name() );
00141         _doSubPackets( const_cast<QObjectList*>( o->children() ), counter );
00142         o = l->next();
00143     }
00144 }
00145 
00146 void PacketView::_doHexPacket( const OPacket* p )
00147 {
00148     if ( oApp->desktop()->width() > 320 )
00149         _hex->setText( p->dump( 16 ) );
00150     else
00151         _hex->setText( p->dump( 8 ) );
00152 }
00153 
00154 const QString PacketView::getLog() const
00155 {
00156     return QString::null;
00157 }
00158 
00159 void PacketView::clear()
00160 {
00161     _packets.clear();
00162     _number->setMinValue( 0 );
00163     _number->setMaxValue( 0 );
00164     _label->setText( "---" );
00165     _list->clear();
00166     _hex->setText( " <center><i>-- no Packet available --</i></center> " );
00167 }
00168 

Generated on Sat Nov 5 16:17:45 2005 for OPIE by  doxygen 1.4.2