Kerberos: add kerberos_inject_longterm_key() helper function
[wireshark-sm.git] / ui / qt / stats_tree_dialog.cpp
blobf51e252a6f80ce508f2fa00a83e398ba06afaf48
1 /* stats_tree_dialog.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "stats_tree_dialog.h"
12 #include "file.h"
14 #include "epan/stats_tree_priv.h"
16 #include <ui/qt/utils/qt_ui_utils.h>
18 #include <ui/qt/utils/variant_pointer.h>
20 #include <QHeaderView>
21 #include <QMessageBox>
22 #include <QTreeWidget>
23 #include <QTreeWidgetItemIterator>
25 const int item_col_ = 0;
27 const int sn_type_ = 1000;
28 class StatsTreeWidgetItem : public QTreeWidgetItem
30 public:
31 StatsTreeWidgetItem(int type = sn_type_) : QTreeWidgetItem (type)
33 for (int col = 1; col < columnCount(); col++) {
34 setTextAlignment(col, Qt::AlignRight);
37 bool operator< (const QTreeWidgetItem &other) const
39 stat_node *thisnode = VariantPointer<stat_node>::asPtr(data(item_col_, Qt::UserRole));
40 stat_node *othernode = VariantPointer<stat_node>::asPtr(other.data(item_col_, Qt::UserRole));
41 Qt::SortOrder order = treeWidget()->header()->sortIndicatorOrder();
42 int result;
44 result = stats_tree_sort_compare(thisnode, othernode, treeWidget()->sortColumn(),
45 order==Qt::DescendingOrder);
46 if (order==Qt::DescendingOrder) {
47 result = -result;
49 return result < 0;
53 StatsTreeDialog::StatsTreeDialog(QWidget &parent, CaptureFile &cf, const char *cfg_abbr) :
54 TapParameterDialog(parent, cf),
55 st_(NULL),
56 st_cfg_(NULL)
58 loadGeometry(800, height(), cfg_abbr);
59 st_cfg_ = stats_tree_get_cfg_by_abbr(cfg_abbr);
60 memset(&cfg_pr_, 0, sizeof(struct _tree_cfg_pres));
62 addTreeCollapseAllActions();
64 if (!st_cfg_) {
65 QMessageBox::critical(this, tr("Configuration not found"),
66 tr("Unable to find configuration for %1.").arg(cfg_abbr));
67 QMetaObject::invokeMethod(this, "reject", Qt::QueuedConnection);
71 StatsTreeDialog::~StatsTreeDialog()
73 if (st_) {
74 stats_tree_free(st_);
78 // Adds a node to the QTreeWidget
79 // Note: We're passing QTreeWidgetItem pointers as st_node_pres pointers
80 void StatsTreeDialog::setupNode(stat_node* node)
82 if (!node || !node->st || !node->st->cfg || !node->st->cfg->pr
83 || !node->st->cfg->pr->st_dlg) return;
84 StatsTreeDialog *st_dlg = node->st->cfg->pr->st_dlg;
86 QTreeWidgetItem *ti = new StatsTreeWidgetItem(), *parent = NULL;
88 ti->setText(item_col_, node->name);
89 ti->setData(item_col_, Qt::UserRole, VariantPointer<stat_node>::asQVariant(node));
90 node->pr = (st_node_pres *) ti;
91 if (node->parent && node->parent->pr) {
92 parent = (QTreeWidgetItem *) node->parent->pr;
93 parent->setExpanded(true);
95 if (parent) {
96 parent->addChild(ti);
97 } else {
98 st_dlg->statsTreeWidget()->addTopLevelItem(ti);
102 void StatsTreeDialog::fillTree()
104 if (!st_cfg_ || file_closed_) return;
106 // The GTK+ UI appended "Stats Tree" to the window title. If we do the same
107 // here we should expand the name completely, e.g. to "Statistics Tree".
108 setWindowSubtitle(st_cfg_->title);
110 st_cfg_->pr = &cfg_pr_;
111 cfg_pr_.st_dlg = this;
113 if (st_) {
114 stats_tree_free(st_);
116 QString display_filter = displayFilter();
117 st_ = stats_tree_new(st_cfg_, NULL, display_filter.toUtf8().constData());
119 // Add number of columns for this stats_tree
120 QStringList header_labels;
121 for (int count = 0; count<st_->num_columns; count++) {
122 header_labels.push_back(stats_tree_get_column_name(st_cfg_, count));
124 statsTreeWidget()->setColumnCount(static_cast<int>(header_labels.count()));
125 statsTreeWidget()->setHeaderLabels(header_labels);
126 statsTreeWidget()->setSortingEnabled(false);
128 if (!registerTapListener(st_cfg_->tapname,
129 st_,
130 st_->filter,
131 st_cfg_->flags,
132 resetTap,
133 stats_tree_packet,
134 drawTreeItems)) {
135 reject(); // XXX Stay open instead?
136 return;
139 cap_file_.retapPackets();
140 drawTreeItems(st_);
142 removeTapListeners();
143 st_cfg_->pr = NULL;
145 statsTreeWidget()->setSortingEnabled(true);
146 statsTreeWidget()->resizeColumnToContents(item_col_);
149 void StatsTreeDialog::resetTap(void *st_ptr)
151 stats_tree *st = (stats_tree *) st_ptr;
152 if (!st || !st->cfg || !st->cfg->pr || !st->cfg->pr->st_dlg) return;
154 st->cfg->pr->st_dlg->statsTreeWidget()->clear();
155 st->cfg->init(st);
158 void StatsTreeDialog::drawTreeItems(void *st_ptr)
160 stats_tree *st = (stats_tree *) st_ptr;
161 if (!st || !st->cfg || !st->cfg->pr || !st->cfg->pr->st_dlg) return;
162 TapParameterDialog *st_dlg = st->cfg->pr->st_dlg;
163 QTreeWidgetItemIterator iter(st_dlg->statsTreeWidget());
165 while (*iter) {
166 stat_node *node = VariantPointer<stat_node>::asPtr((*iter)->data(item_col_, Qt::UserRole));
167 if (node) {
168 char **valstrs = stats_tree_get_values_from_node(node);
169 for (int count = 0; count<st->num_columns; count++) {
170 (*iter)->setText(count,valstrs[count]);
171 g_free(valstrs[count]);
173 (*iter)->setExpanded((node->parent==(&st->root)) &&
174 (!(node->st_flags&ST_FLG_DEF_NOEXPAND)));
175 g_free(valstrs);
177 ++iter;
180 st_dlg->drawTreeItems();
183 QByteArray StatsTreeDialog::getTreeAsString(st_format_type format)
185 GString *str_tree;
187 // produce output in selected format using current sort information
188 str_tree = stats_tree_format_as_str(st_, format, statsTreeWidget()->sortColumn(),
189 statsTreeWidget()->header()->sortIndicatorOrder()==Qt::DescendingOrder);
191 return gstring_free_to_qbytearray(str_tree);
194 extern "C" {
196 void register_tap_listener_qt_stats_tree_stat(void);
198 void
199 register_tap_listener_qt_stats_tree_stat(void)
201 stats_tree_presentation(NULL,
202 StatsTreeDialog::setupNode,
203 NULL, NULL);