aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/credentials_model.cpp
blob: 5530514a5ef817ab871b37727bdc959f4ef8f4ad (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
/*
 * credentials_model.h
 *
 * Copyright 2019 - Dario Lombardo <lomato@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "credentials_model.h"

#include <file.h>
#include <log.h>
#include <ui/qt/utils/qt_ui_utils.h>

CredentialsModel::CredentialsModel(QObject *parent)
    :QAbstractListModel(parent)
{
}

int CredentialsModel::rowCount(const QModelIndex &) const
{
    return credentials_.count();
}

int CredentialsModel::columnCount(const QModelIndex &) const
{
    return COL_INFO + 1;
}

QVariant CredentialsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    tap_credential_t * auth = credentials_.at(index.row());
    if (!auth)
        return QVariant();


    if (role == Qt::DisplayRole) {
        switch (index.column()) {
            case COL_NUM:
                return QVariant::fromValue(auth->num);
            case COL_PROTO:
                return QString(auth->proto);
            case COL_USERNAME:
                return QString(auth->username);
            case COL_INFO:
                return QString(auth->info);
            default:
                return QVariant();
        }
    }

    if (role == Qt::UserRole) {
        switch (index.column()) {
            case COL_NUM:
                if (auth->num > 0)
                    return QVariant::fromValue(auth->num);
                break;
            case COL_USERNAME:
                if (auth->username_num > 0)
                    return QVariant::fromValue(auth->username_num);
                break;
            default:
                return QVariant();
        }
    }

    if (role == CredentialsModel::ColumnHFID)
        return QVariant::fromValue(auth->password_hf_id);

    if (role == Qt::ToolTipRole) {
        const QString select_msg(tr("Click to select the packet"));
        switch (index.column()) {
            case COL_NUM:
                if (auth->num > 0)
                    return select_msg;
                break;
            case  COL_USERNAME:
                if (auth->username_num > 0) {
                    if (auth->username_num != auth->num)
                        return QString(tr("Click to select the packet with username"));
                    else
                        return select_msg;
                } else {
                    return QString(tr("Username not available"));
                }
                break;
            default:
                return QVariant();
        }
    }

    return QVariant();
}

void CredentialsModel::addRecord(const tap_credential_t* auth)
{
    emit beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1);

    tap_credential_t* clone = new tap_credential_t;
    clone->num = auth->num;
    clone->username_num = auth->username_num;
    clone->password_hf_id = auth->password_hf_id;
    clone->username = qstring_strdup(auth->username);
    clone->proto = auth->proto;
    clone->info = qstring_strdup(auth->info);
    credentials_.append(clone);

    emit endInsertRows();
}

void CredentialsModel::clear()
{
    if (!credentials_.isEmpty()) {
        emit beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
        for (QList<tap_credential_t*>::iterator itr = credentials_.begin(); itr != credentials_.end(); ++itr) {
            g_free((*itr)->username);
            g_free((*itr)->info);
            delete *itr;
        }
        credentials_.clear();
        emit endRemoveRows();
    }
}

QVariant CredentialsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
            case COL_NUM:
                return QString(tr("Packet No."));
            case COL_PROTO:
                return QString(tr("Protocol"));
            case COL_USERNAME:
                return QString(tr("Username"));
            case COL_INFO:
                return QString(tr("Additional Info"));
        }
    }

    return QVariant();
}