aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/packet_list.cpp
blob: 91cbb4cb823b02ceca301cfd00eb26d77fd9ebad (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* packet_list.cpp
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>

#include <epan/epan.h>
#include <epan/epan_dissect.h>

#include <epan/column_info.h>
#include <epan/column.h>
#include <epan/packet.h>

#include "packet_list.h"
#include "monospace_font.h"
#include "proto_tree.h"

#include "ui_util.h"
#include "globals.h"
#include "qt_ui_utils.h"
#include "main_statusbar.h"

#include "ui/gtk/recent.h"

#include <QTreeWidget>
#include <QTabWidget>
#include <QTextEdit>

static gboolean enable_color;

// If we ever add the ability to open multiple capture files we might be
// able to use something like QMap<capture_file *, PacketList *> to match
// capture files against packet lists and models.
static PacketList *cur_packet_list = NULL;

guint
new_packet_list_append(column_info *cinfo, frame_data *fdata, packet_info *pinfo)
{
    Q_UNUSED(cinfo);
    Q_UNUSED(pinfo);

    if (!cur_packet_list)
        return 0;

    /* fdata should be filled with the stuff we need
     * strings are built at display time.
     */
    guint visible_pos;

    visible_pos = cur_packet_list->packetListModel()->appendPacket(fdata);
    return visible_pos;
}

// Copied from ui/gtk/new_packet_list.c
void new_packet_list_resize_column(gint col)
{
    // xxx qtshark
//    gint col_width;
//    const gchar *long_str;

g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_resize_column %d", col);
//    long_str = packet_list_get_widest_column_string(packetlist, col);
//    if(!long_str || strcmp("",long_str)==0)
//        /* If we get an empty string leave the width unchanged */
//        return;
//    column = gtk_tree_view_get_column (GTK_TREE_VIEW(packetlist->view), col);
//    col_width = get_default_col_size (packetlist->view, long_str);
//    gtk_tree_view_column_set_fixed_width(column, col_width);
}

void
new_packet_list_select_first_row(void)
{
    cur_packet_list->setCurrentIndex(cur_packet_list->packetListModel()->index(0,0));
    cur_packet_list->setFocus();
}

void
new_packet_list_select_last_row(void)
{
    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_select_last_row");
//    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(packetlist->view));
//    GtkTreeIter iter;
//    gint children;
//    guint last_row;

//    if((children = gtk_tree_model_iter_n_children(model, NULL)) == 0)
//        return;

//    last_row = children-1;
//    if(!gtk_tree_model_iter_nth_child(model, &iter, NULL, last_row))
//        return;

//    scroll_to_and_select_iter(model, NULL, &iter);
}

/*
 * Given a frame_data structure, scroll to and select the row in the
 * packet list corresponding to that frame.  If there is no such
 * row, return FALSE, otherwise return TRUE.
 */
gboolean
new_packet_list_select_row_from_data(frame_data *fdata_needle)
{
    int row = cur_packet_list->packetListModel()->visibleIndexOf(fdata_needle);
    if (row >= 0) {
        cur_packet_list->setCurrentIndex(cur_packet_list->packetListModel()->index(row,0));
        return TRUE;
    }

    return FALSE;
}

gboolean
new_packet_list_check_end(void)
{
        gboolean at_end = FALSE;
//	GtkAdjustment *adj;

        g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_check_end");

//	adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(packetlist->view));
//	g_return_val_if_fail(adj != NULL, FALSE);

//#if GTK_CHECK_VERSION(2,14,0)
//	if (gtk_adjustment_get_value(adj) >= gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj)) {
//#else
//	if (adj->value >= adj->upper - adj->page_size) {
//#endif
//		at_end = TRUE;
//	}
//#ifdef HAVE_LIBPCAP
//#if GTK_CHECK_VERSION(2,14,0)
//	if (gtk_adjustment_get_value(adj) > 0 && at_end != last_at_end && at_end != auto_scroll_live) {
//#else
//	if (adj->value > 0 && at_end != last_at_end && at_end != auto_scroll_live) {
//#endif
//		menu_auto_scroll_live_changed(at_end);
//	}
//#endif
//	last_at_end = at_end;
        return at_end;
}

void
new_packet_list_clear(void)
{

    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_clear");

    cur_packet_list->clear();
}

void
new_packet_list_enable_color(gboolean enable)
{
    enable_color = enable;
    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_enable_color: %d", enable);

    //	gtk_widget_queue_draw (packetlist->view);
}

void
new_packet_list_freeze(void)
{
    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_freeze");
//	/* So we don't lose the model by the time we want to thaw it */
//	g_object_ref(packetlist);

//	/* Detach view from model */
//	gtk_tree_view_set_model(GTK_TREE_VIEW(packetlist->view), NULL);
}

void
new_packet_list_thaw(void)
{
    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_thaw");
//	/* Apply model */
//	gtk_tree_view_set_model( GTK_TREE_VIEW(packetlist->view), GTK_TREE_MODEL(packetlist));

//	/* Remove extra reference added by new_packet_list_freeze() */
//	g_object_unref(packetlist);

        packets_bar_update();
}

void
new_packet_list_recreate_visible_rows(void)
{
    cur_packet_list->packetListModel()->recreateVisibleRows();
}

frame_data *
new_packet_list_get_row_data(gint row)
{
    if (!cur_packet_list)
        return NULL;
    return cur_packet_list->packetListModel()->getRowFdata(row);
}

void
new_packet_list_moveto_end(void)
{
//    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(packetlist->view));
//    GtkTreeIter iter;
//    GtkTreePath *path;
//    gint children;
//    guint last_row;

    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_moveto_end");

//    if((children = gtk_tree_model_iter_n_children(model, NULL)) == 0)
//        return;

//    last_row = children-1;
//    if(!gtk_tree_model_iter_nth_child(model, &iter, NULL, last_row))
//        return;

//    path = gtk_tree_model_get_path(model, &iter);

//    gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(packetlist->view),
//                                 path,
//                                 NULL,
//                                 TRUE,	/* use_align */
//                                 0.5,	/* row_align determines where the row is placed, 0.5 means center */
//                                 0); 	/* The horizontal alignment of the column */

//    gtk_tree_path_free(path);

}

/* Redraw the packet list *and* currently-selected detail */
void
new_packet_list_queue_draw(void)
{
//    GtkTreeSelection *selection;
//    GtkTreeIter iter;
//    gint row;

    g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: new_packet_list_queue_draw");
//    gtk_widget_queue_draw (packetlist->view);

//    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(packetlist->view));
//    if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
//        return;
//    row = row_number_from_iter(&iter);
//    cf_select_packet(&cfile, row);
    cf_select_packet(&cfile, 1);
}

#define MIN_COL_WIDTH_STR "...."

PacketList::PacketList(QWidget *parent) :
    QTreeView(parent)
{
    setItemsExpandable(FALSE);
    setRootIsDecorated(FALSE);
    setSortingEnabled(TRUE);
    setUniformRowHeights(TRUE);
    setAccessibleName("Packet list");

    m_packet_list_model = new PacketListModel(this, &cfile);
    setModel(m_packet_list_model);

    g_assert(cur_packet_list == NULL);
    cur_packet_list = this;

    m_protoTree = NULL;
    m_byteViewTab = NULL;
}

void PacketList::setProtoTree (ProtoTree *protoTree) {
    m_protoTree = protoTree;
}

void PacketList::setByteViewTab (ByteViewTab *byteViewTab) {
    m_byteViewTab = byteViewTab;
}

PacketListModel *PacketList::packetListModel() const {
    return m_packet_list_model;
}

void PacketList::showEvent (QShowEvent *event) {
    Q_UNUSED(event);
//    g_log(NULL, G_LOG_LEVEL_DEBUG, "cols: %d", cfile.cinfo.num_cols);
    for (int i = 0; i < cfile.cinfo.num_cols; i++) {
        int fmt, col_width;
        const char *long_str;

        fmt = get_column_format(i);
        long_str = get_column_width_string(fmt, i);
        if (long_str) {
            col_width = get_monospace_text_size(long_str, TRUE);
        } else {
            col_width = get_monospace_text_size(MIN_COL_WIDTH_STR, TRUE);
        }
        setColumnWidth(i, col_width);
    }
}

void PacketList::selectionChanged (const QItemSelection & selected, const QItemSelection & deselected) {
    QTreeView::selectionChanged(selected, deselected);

    if (m_protoTree) {
        int row = selected.first().top();
        cf_select_packet(&cfile, row);

        if (!cfile.edt && !cfile.edt->tree) {
            return;
        }

        m_protoTree->fillProtocolTree(cfile.edt->tree);
    }

    if (m_byteViewTab && cfile.edt) {
        GSList *src_le;
        data_source *source;

        // Clear out existing tabs
        while (m_byteViewTab->currentWidget()) {
            delete m_byteViewTab->currentWidget();
        }

        for (src_le = cfile.edt->pi.data_src; src_le != NULL; src_le = src_le->next) {
            source = (data_source *)src_le->data;
            m_byteViewTab->addTab(get_data_source_name(source), source->tvb, cfile.edt->tree, m_protoTree, cfile.current_frame->flags.encoding);
        }
    }

    if (m_protoTree && m_byteViewTab) {
        // Connect signals between the proto tree and byte views.
        connect(m_protoTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
                m_byteViewTab, SLOT(protoTreeItemChanged(QTreeWidgetItem*)));
    }
}

void PacketList::clear() {
    //    packet_history_clear();
    packetListModel()->clear();
    m_protoTree->clear();

    // Clear out existing tabs
    while (m_byteViewTab->currentWidget()) {
        delete m_byteViewTab->currentWidget();
    }

//	/* XXX is this correct in all cases?
//	 * Reset the sort column, use packetlist as model in case the list is frozen.
//	 */
    cur_packet_list->sortByColumn(0, Qt::AscendingOrder);
}

void new_packet_list_recent_write_all(FILE *rf) {
    if (!cur_packet_list)
        return;

    cur_packet_list->writeRecent(rf);
}

void PacketList::writeRecent(FILE *rf) {
    gint col, width, col_fmt;
    gchar xalign;

    fprintf (rf, "%s:", RECENT_KEY_COL_WIDTH);
    for (col = 0; col < packetListModel()->columnCount(); col++) {
        if (col > 0) {
            fprintf (rf, ",");
        }
        col_fmt = get_column_format(col);
        if (col_fmt == COL_CUSTOM) {
            fprintf (rf, " %%Cus:%s,", get_column_custom_field(col));
        } else {
            fprintf (rf, " %s,", col_format_to_string(col_fmt));
        }
        width = columnWidth(col);
        xalign = recent_get_column_xalign (col);
        if (width == 0) {
            /* We have not initialized the packet list yet, use old values */
            width = recent_get_column_width (col);
        }
        fprintf (rf, " %d", width);
        if (xalign != COLUMN_XALIGN_DEFAULT) {
            fprintf (rf, ":%c", xalign);
        }
    }
    fprintf (rf, "\n");

}