Qt: Keep byte tab when reloading/closing capture file
[metze/wireshark/wip.git] / ui / qt / packet_dialog.cpp
1 /* packet_dialog.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "packet_dialog.h"
23 #include <ui_packet_dialog.h>
24
25 #include "file.h"
26
27 #include "epan/column.h"
28 #include "epan/ftypes/ftypes.h"
29
30 #include "frame_tvbuff.h"
31
32 #include <wsutil/utf8_entities.h>
33
34 #include "byte_view_tab.h"
35 #include "proto_tree.h"
36 #include "wireshark_application.h"
37
38 #include <QTreeWidgetItemIterator>
39
40 // To do:
41 // - Copy over experimental packet editing code.
42 // - Fix ElidedText width.
43
44 PacketDialog::PacketDialog(QWidget &parent, CaptureFile &cf, frame_data *fdata) :
45     WiresharkDialog(parent, cf),
46     ui(new Ui::PacketDialog),
47     packet_data_(NULL)
48 {
49     ui->setupUi(this);
50     loadGeometry(parent.width() * 4 / 5, parent.height() * 4 / 5);
51     ui->hintLabel->setSmallText();
52
53     setWindowSubtitle(tr("Packet %1").arg(fdata->num));
54
55     phdr_ = cap_file_.capFile()->phdr;
56     packet_data_ = (guint8 *) g_memdup(ws_buffer_start_ptr(&(cap_file_.capFile()->buf)), fdata->cap_len);
57
58     if (!cf_read_record(cap_file_.capFile(), fdata)) reject();
59     /* proto tree, visible. We need a proto tree if there's custom columns */
60     epan_dissect_init(&edt_, cap_file_.capFile()->epan, TRUE, TRUE);
61     col_custom_prime_edt(&edt_, &(cap_file_.capFile()->cinfo));
62
63     epan_dissect_run(&edt_, cap_file_.capFile()->cd_t, &phdr_,
64                      frame_tvbuff_new(fdata, packet_data_),
65                      fdata, &(cap_file_.capFile()->cinfo));
66     epan_dissect_fill_in_columns(&edt_, TRUE, TRUE);
67
68     proto_tree_ = new ProtoTree(ui->packetSplitter);
69     proto_tree_->fillProtocolTree(edt_.tree);
70
71     byte_view_tab_ = new ByteViewTab(ui->packetSplitter);
72     byte_view_tab_->setCaptureFile(cap_file_.capFile());
73     byte_view_tab_->clear();
74
75     GSList *src_le;
76     for (src_le = edt_.pi.data_src; src_le != NULL; src_le = src_le->next) {
77         struct data_source *source;
78         char* source_name;
79         source = (struct data_source *)src_le->data;
80         source_name = get_data_source_name(source);
81         byte_view_tab_->addTab(source_name, get_data_source_tvb(source), edt_.tree, proto_tree_,
82                                (packet_char_enc)cap_file_.capFile()->current_frame->flags.encoding);
83         wmem_free(NULL, source_name);
84     }
85     byte_view_tab_->setCurrentIndex(0);
86
87     ui->packetSplitter->setStretchFactor(1, 0);
88
89     QStringList col_parts;
90     for (int i = 0; i < cap_file_.capFile()->cinfo.num_cols; ++i) {
91         // ElidedLabel doesn't support rich text / HTML
92         col_parts << QString("%1: %2")
93                      .arg(get_column_title(i))
94                      .arg(cap_file_.capFile()->cinfo.columns[i].col_data);
95     }
96     col_info_ = col_parts.join(" " UTF8_MIDDLE_DOT " ");
97     setHintText();
98
99     connect(this, SIGNAL(monospaceFontChanged(QFont)),
100             proto_tree_, SLOT(setMonospaceFont(QFont)));
101     connect(this, SIGNAL(monospaceFontChanged(QFont)),
102             byte_view_tab_, SLOT(setMonospaceFont(QFont)));
103
104     connect(proto_tree_, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
105             byte_view_tab_, SLOT(protoTreeItemChanged(QTreeWidgetItem*)));
106     connect(byte_view_tab_, SIGNAL(byteFieldHovered(const QString&)),
107             this, SLOT(setHintText(const QString&)));
108 }
109
110 PacketDialog::~PacketDialog()
111 {
112     delete ui;
113     epan_dissect_cleanup(&edt_);
114     g_free(packet_data_);
115 }
116
117 void PacketDialog::captureFileClosing()
118 {
119     QString closed_title = tr("[%1 closed] " UTF8_MIDDLE_DOT " %2")
120             .arg(cap_file_.fileName())
121             .arg(col_info_);
122     setHintText(closed_title);
123     WiresharkDialog::captureFileClosing();
124 }
125
126 void PacketDialog::setHintText(const QString &hint)
127 {
128     ui->hintLabel->setText(hint.isEmpty() ? col_info_ : hint);
129 }
130
131 void PacketDialog::on_buttonBox_helpRequested()
132 {
133     wsApp->helpTopicAction(HELP_NEW_PACKET_DIALOG);
134 }
135
136 /*
137  * Editor modelines
138  *
139  * Local Variables:
140  * c-basic-offset: 4
141  * tab-width: 8
142  * indent-tabs-mode: nil
143  * End:
144  *
145  * ex: set shiftwidth=4 tabstop=8 expandtab:
146  * :indentSize=4:tabSize=8:noTabs=true:
147  */