summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/gui/packetview.cpp
blob: 69438fa14b21b3985e889509bf0fbf9a078e9486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**********************************************************************
** Copyright (C) 2002-2004 Michael 'Mickey' Lauer.  All rights reserved.
**
** This file is part of Wellenreiter II.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "packetview.h"

/* OPIE */
#include <opie2/opcap.h>
#include <opie2/odebug.h>
#include <opie2/olistview.h>
#include <opie2/oapplication.h>

/* QT */
#include <qfont.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlist.h>
#include <qlistview.h>
#include <qobjectlist.h>
#include <qspinbox.h>
#include <qtextview.h>

using namespace Opie::Net;
using namespace Opie::Core;
using namespace Opie::Ui;

PacketView::PacketView( QWidget * parent, const char * name, WFlags f )
           :QFrame( parent, name, f )
{
    _number = new QSpinBox( this );
    _number->setPrefix( "Pkt# " );
    _label = new QLabel( this );
    _list = new OListView( this );
    _list->addColumn( "#" );
    _list->addColumn( "Packet Type" );
    _list->setColumnAlignment( 0, Qt::AlignCenter );
    _list->setColumnAlignment( 1, Qt::AlignLeft );
    _list->setAllColumnsShowFocus( true );
    _list->setFont( QFont( "Fixed", 8 ) );

    _hex = new QTextView( this );
    _hex->setMargin( 0 );
    _hex->setFont( QFont( "Fixed", 8 ) );

    QVBoxLayout* vb = new QVBoxLayout( this, 2, 2 );
    QHBoxLayout* hb = new QHBoxLayout( vb, 2 );
    hb->addWidget( _label, 5 );
    hb->addWidget( _number, 2 );
    vb->addWidget( _list, 3 );
    vb->addWidget( _hex, 4 ); // allow a bit (4/3) more space

    _packets.setAutoDelete( true );

    connect( _number, SIGNAL( valueChanged( int ) ), this, SLOT( showPacket( int ) ) );
    connect( parent, SIGNAL( currentChanged( QWidget *) ), this, SLOT( activated( QWidget* ) ) );

    clear();

}

void PacketView::add( const OPacket* p, int size )
{
    odebug << "PacketView::add() size = " << size << oendl;
    if ( size == -1 ) // unlimited buffer
    {
        _packets.append( p );
    }
    else
    {   // limited buffer, limit = size
        while ( _packets.count() >= size )
        {
            _packets.removeFirst();
        }
        _packets.append( p );
    }

    _number->setMinValue( 1 );
    _number->setMaxValue( _packets.count() );
    _number->setValue( _packets.count() );
}

void PacketView::showPacket( int number )
{
    _list->clear();
    _hex->setText("");
    const OPacket* p = _packets.at( number-1 );

    if ( p )
    {
        _doSubPackets( const_cast<QObjectList*>( p->children() ), 0 );
        _doHexPacket( p );
        QDateTime dt; dt.setTime_t( p->timeval().tv_sec );
        _label->setText( dt.toString() + QString().sprintf( " Len=%d", p->len() ) );
    }
    else
    {
        odebug << "D'oh! No packet!" << oendl;
    }
}

void PacketView::activated( QWidget* w )
{
    if ( ( this == w ) && _packets.count() )
    {
        _number->setValue( 1 );
    }
}

void PacketView::_doSubPackets( QObjectList* l, int counter )
{
    if (!l) return;
    QObject* o = l->first();
    while ( o )
    {
        new OListViewItem( _list, QString::number( counter++ ), o->name() );
        _doSubPackets( const_cast<QObjectList*>( o->children() ), counter );
        o = l->next();
    }
}

void PacketView::_doHexPacket( const OPacket* p )
{
    if ( oApp->desktop()->width() > 320 )
        _hex->setText( p->dump( 16 ) );
    else
        _hex->setText( p->dump( 8 ) );
}

const QString PacketView::getLog() const
{
}

void PacketView::clear()
{
    _packets.clear();
    _number->setMinValue( 0 );
    _number->setMaxValue( 0 );
    _label->setText( "---" );
    _list->clear();
    _hex->setText( " <center><i>-- no Packet available --</i></center> " );
}