summaryrefslogtreecommitdiffstats
path: root/ui/qt/utils/proto_node.cpp
blob: 68d1f4c7796696f4480d9bc1744077b9ee3e762f (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
/* proto_node.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/utils/proto_node.h>
#include <ui/qt/utils/field_information.h>

#include <epan/prefs.h>

ProtoNode::ProtoNode(proto_node *node, ProtoNode *parent) :
    node_(node), parent_(parent)
{
    if (node_) {

        int num_children = 0;
        for (proto_node *child = node_->first_child; child; child = child->next) {
            if (!isHidden(child)) {
                num_children++;
            }
        }

        m_children.reserve(num_children);

        for (proto_node *child = node_->first_child; child; child = child->next) {
            if (!isHidden(child)) {
                m_children.append(new ProtoNode(child, this));
            }
        }
    }
}

ProtoNode::~ProtoNode()
{
    qDeleteAll(m_children);
}

bool ProtoNode::isValid() const
{
    return node_;
}

bool ProtoNode::isChild() const
{
    return node_ && node_->parent;
}

ProtoNode* ProtoNode::parentNode()
{
    return parent_;
}

QString ProtoNode::labelText() const
{
    if (!node_) {
        return QString();
    }
    field_info *fi = PNODE_FINFO(node_);
    if (!fi) {
        return QString();
    }

    QString label;
    /* was a free format label produced? */
    if (fi->rep) {
        label = fi->rep->representation;
    }
    else { /* no, make a generic label */
        gchar label_str[ITEM_LABEL_LENGTH];
        proto_item_fill_label(fi, label_str);
        label = label_str;
    }

    // Generated takes precedence.
    if (proto_item_is_generated(node_)) {
        label.prepend("[");
        label.append("]");
    }
    if (proto_item_is_hidden(node_)) {
        label.prepend("<");
        label.append(">");
    }
    return label;
}

int ProtoNode::childrenCount() const
{
    if (!node_) return 0;

    return (int)m_children.count();
}

int ProtoNode::row()
{
    if (!isChild()) {
        return -1;
    }

    return (int)parent_->m_children.indexOf(const_cast<ProtoNode*>(this));
}

bool ProtoNode::isExpanded() const
{
    if (node_ && node_->finfo && node_->first_child && tree_expanded(node_->finfo->tree_type)) {
        return true;
    }
    return false;
}

proto_node * ProtoNode::protoNode() const
{
    return node_;
}

ProtoNode* ProtoNode::child(int row)
{
    if (row < 0 || row >= m_children.size())
        return nullptr;
    return m_children.at(row);
}

ProtoNode::ChildIterator ProtoNode::children() const
{
    /* XXX: Iterate over m_children instead?
     * Somewhat faster as m_children already excludes any hidden items. */
    proto_node *child = node_->first_child;
    while (child && isHidden(child)) {
        child = child->next;
    }

    return ProtoNode::ChildIterator(child);
}

ProtoNode::ChildIterator::ChildIterator(ProtoNode::ChildIterator::NodePtr n)
{
    node = n;
}

bool ProtoNode::ChildIterator::hasNext()
{
    if (! node || node->next == Q_NULLPTR)
        return false;
    return true;
}

ProtoNode::ChildIterator ProtoNode::ChildIterator::next()
{
    do {
        node = node->next;
    } while (node && isHidden(node));
    return *this;
}

ProtoNode ProtoNode::ChildIterator::element()
{
    return ProtoNode(node);
}

bool ProtoNode::isHidden(proto_node * node)
{
    return proto_item_is_hidden(node) && !prefs.display_hidden_proto_items;
}