aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/filter_list_model.cpp
blob: b65e11f5b95d715ec6e0329ba0fc25b75e6714f8 (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
/* filter_list_model.cpp
 * Model for all filter types
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <glib.h>

#include <wsutil/filesystem.h>

#include <ui/qt/utils/qt_ui_utils.h>
#include <ui/qt/utils/wireshark_mime_data.h>
#include <ui/qt/models/filter_list_model.h>
#include <ui/qt/models/profile_model.h>

#include <QFile>
#include <QTextStream>
#include <QRegExp>
#include <QDir>
#include <QMimeData>

/*
 * Old filter file name.
 */
#define FILTER_FILE_NAME      "filters"

/*
 * Capture filter file name.
 */
#define CFILTER_FILE_NAME     "cfilters"

/*
 * Display filter file name.
 */
#define DFILTER_FILE_NAME     "dfilters"

FilterListModel::FilterListModel(QObject * parent) :
    QAbstractListModel(parent),
    type_(FilterListModel::Display)
{
    reload();
}

FilterListModel::FilterListModel(FilterListModel::FilterListType type, QObject * parent) :
    QAbstractListModel(parent),
    type_(type)
{
    reload();
}

void FilterListModel::reload()
{
    storage.clear();

    const char * cfile = (type_ == FilterListModel::Capture) ? CFILTER_FILE_NAME : DFILTER_FILE_NAME;

    /* Try personal config file first */
    QString fileName = gchar_free_to_qstring(get_persconffile_path(cfile, TRUE));
    if ( fileName.length() <= 0 || ! QFileInfo::exists(fileName) )
        fileName = gchar_free_to_qstring(get_persconffile_path(FILTER_FILE_NAME, TRUE));
    if ( fileName.length() <= 0 || ! QFileInfo::exists(fileName) )
        fileName = gchar_free_to_qstring(get_datafile_path(cfile));
    if ( fileName.length() <= 0 || ! QFileInfo::exists(fileName) )
        return;

    QFile file(fileName);
    /* Still can use the model, just have to start from an empty set */
    if ( ! file.open(QIODevice::ReadOnly | QIODevice::Text) )
        return;

    QTextStream in(&file);
    QRegExp rx("\\s*\\\"(.*)\\\"\\s(.*)");
    while (!in.atEnd())
    {
        QString line = in.readLine().trimmed();
        if ( line.startsWith("#") || line.indexOf("\"") <= -1 )
            continue;

        rx.indexIn(line);
        QStringList groups = rx.capturedTexts();
        if ( groups.count() != 3 )
            continue;
        addFilter(groups.at(1), groups.at(2));
    }
}

void FilterListModel::setFilterType(FilterListModel::FilterListType type)
{
    type_ = type;
    reload();
}

FilterListModel::FilterListType FilterListModel::filterType() const
{
    return type_;
}

int FilterListModel::rowCount(const QModelIndex &/* parent */) const
{
    return storage.count();
}

int FilterListModel::columnCount(const QModelIndex &/* parent */) const
{
    return 2;
}

QVariant FilterListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if ( section >= columnCount() || section < 0 || orientation != Qt::Horizontal )
        return QVariant();

    if ( role == Qt::DisplayRole )
    {
        switch ( section ) {
            case ColumnName:
                return tr("Filter Name");
                break;
            case ColumnExpression:
                return tr("Filter Expression");
                break;
        }
    }

    return QVariant();
}

QVariant FilterListModel::data(const QModelIndex &index, int role) const
{
    if ( ! index.isValid() || index.row() >= rowCount() )
        return QVariant();

    QStringList row = storage.at(index.row()).split("\n");
    if ( role == Qt::DisplayRole )
        return row.at(index.column());

    return QVariant();
}

bool FilterListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if ( ! index.isValid() || index.row() >= rowCount() || role != Qt::EditRole )
        return false;

    QStringList row = storage.at(index.row()).split("\n");
    if ( row.count() <= index.column() )
        return false;

    if ( index.column() == FilterListModel::ColumnName && value.toString().contains("\"") )
        return false;

    row[index.column()] = value.toString();
    storage[index.row()] = row.join("\n");

    return true;
}

Qt::ItemFlags FilterListModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags fl = QAbstractListModel::flags(index);
    fl |= Qt::ItemIsDropEnabled;

    if ( ! index.isValid() || index.row() >= rowCount() )
        return fl;

    fl |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled;

    return fl;
}
QModelIndex FilterListModel::addFilter(QString name, QString expression)
{
    if ( name.length() == 0 || expression.length() == 0 )
        return QModelIndex();

    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    storage << QString("%1\n%2").arg(name).arg(expression);
    endInsertRows();

    return index(rowCount() - 1, 0);
}

QModelIndex FilterListModel::findByName(QString name)
{
    if ( name.length() == 0 )
        return QModelIndex();

    for ( int cnt = 0; cnt < rowCount(); cnt++ )
    {
        if ( storage.at(cnt).startsWith(QString("%1\n").arg(name)) )
            return index(cnt, 0);
    }

    return QModelIndex();
}

QModelIndex FilterListModel::findByExpression(QString expression)
{
    if ( expression.length() == 0 )
        return QModelIndex();

    for ( int cnt = 0; cnt < rowCount(); cnt++ )
    {
        if ( storage.at(cnt).endsWith(QString("\n%1").arg(expression)) )
            return index(cnt, 0);
    }

    return QModelIndex();
}

void FilterListModel::removeFilter(QModelIndex idx)
{
    if ( ! idx.isValid() || idx.row() >= rowCount() )
        return;

    beginRemoveRows(QModelIndex(), idx.row(), idx.row());
    storage.removeAt(idx.row());
    endRemoveRows();
}

void FilterListModel::saveList()
{
    QString filename = (type_ == FilterListModel::Capture) ? CFILTER_FILE_NAME : DFILTER_FILE_NAME;

    filename = QString("%1%2%3").arg(ProfileModel::activeProfilePath()).arg(QDir::separator()).arg(filename);
    QFile file(filename);

    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Text) )
        return;

    QTextStream out(&file);
    for ( int row = 0; row < rowCount(); row++ )
    {
        QString line = QString("\"%1\"").arg(index(row, ColumnName).data().toString());
        line.append(QString(" %1").arg(index(row, ColumnExpression).data().toString()));

#ifdef _WIN32
        line = line.append("\r\n");
#else
        line = line.append("\n");
#endif
        out << line;
    }

    file.close();
}

Qt::DropActions FilterListModel::supportedDropActions() const
{
    return Qt::MoveAction;
}

QStringList FilterListModel::mimeTypes() const
{
    return QStringList() << WiresharkMimeData::FilterListMimeType;
}

QMimeData *FilterListModel::mimeData(const QModelIndexList &indexes) const
{
    QMimeData *mimeData = new QMimeData();
    QStringList rows;

    foreach (const QModelIndex &index, indexes)
    {
        if ( ! rows.contains(QString::number(index.row())) )
            rows << QString::number(index.row());
    }

    mimeData->setData(WiresharkMimeData::FilterListMimeType, rows.join(",").toUtf8());
    return mimeData;
}

bool FilterListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int /* column */, const QModelIndex & parent)
{
    if ( action != Qt::MoveAction )
        return true;

    if ( ! data->hasFormat(WiresharkMimeData::FilterListMimeType) )
        return true;

    QStringList rows = QString(data->data(WiresharkMimeData::FilterListMimeType)).split(",");

    int insertRow = parent.isValid() ? parent.row() : row;

    /* for now, only single rows can be selected */
    if ( rows.count() > 0 )
    {
        bool ok = false;
        int strow = rows[0].toInt(&ok);
        if ( ok )
        {
            int storeTo = insertRow;
            if ( storeTo < 0 || storeTo >= storage.count() )
                storeTo = storage.count() - 1;

            beginResetModel();
            storage.move(strow, storeTo);
            endResetModel();
        }
    }

    return true;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */