1 /* export_objects_model.cpp
2 * Data model for Export Objects.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "export_objects_model.h"
13 #include <ui/qt/utils/qt_ui_utils.h>
14 #include <ui/qt/utils/variant_pointer.h>
15 #include <wsutil/filesystem.h>
16 #include <epan/prefs.h>
23 object_list_add_entry(void *gui_data
, export_object_entry_t
*entry
) {
24 export_object_list_gui_t
*object_list
= (export_object_list_gui_t
*)gui_data
;
26 if (object_list
&& object_list
->model
)
27 object_list
->model
->addObjectEntry(entry
);
30 static export_object_entry_t
*
31 object_list_get_entry(void *gui_data
, int row
) {
32 export_object_list_gui_t
*object_list
= (export_object_list_gui_t
*)gui_data
;
34 if (object_list
&& object_list
->model
)
35 return object_list
->model
->objectEntry(row
);
45 ExportObjectModel::ExportObjectModel(register_eo_t
* eo
, QObject
*parent
) :
46 QAbstractTableModel(parent
),
49 eo_gui_data_
.model
= this;
51 export_object_list_
.add_entry
= object_list_add_entry
;
52 export_object_list_
.get_entry
= object_list_get_entry
;
53 export_object_list_
.gui_data
= (void*)&eo_gui_data_
;
56 ExportObjectModel::~ExportObjectModel()
58 foreach (QVariant v
, objects_
) {
59 eo_free_entry(VariantPointer
<export_object_entry_t
>::asPtr(v
));
63 QVariant
ExportObjectModel::data(const QModelIndex
&index
, int role
) const
65 if ((!index
.isValid()) || ((role
!= Qt::DisplayRole
) && (role
!= Qt::UserRole
))) {
69 if (role
== Qt::DisplayRole
)
71 export_object_entry_t
*entry
= VariantPointer
<export_object_entry_t
>::asPtr(objects_
.value(index
.row()));
75 switch(index
.column())
78 return QString::number(entry
->pkt_num
);
80 return QString::fromUtf8(entry
->hostname
);
82 return QString::fromUtf8(entry
->content_type
);
84 return file_size_to_qstring(entry
->payload_len
);
86 return QString::fromUtf8(entry
->filename
);
89 else if (role
== Qt::UserRole
)
91 return objects_
.value(index
.row());
97 QVariant
ExportObjectModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
99 if (role
!= Qt::DisplayRole
|| orientation
!= Qt::Horizontal
)
106 return tr("Hostname");
108 return tr("Content Type");
112 return tr("Filename");
118 int ExportObjectModel::rowCount(const QModelIndex
&parent
) const
120 // there are no children
121 if (parent
.isValid()) {
125 return static_cast<int>(objects_
.count());
128 int ExportObjectModel::columnCount(const QModelIndex
&) const
130 return colExportObjectMax
;
133 void ExportObjectModel::addObjectEntry(export_object_entry_t
*entry
)
138 int count
= static_cast<int>(objects_
.count());
139 beginInsertRows(QModelIndex(), count
, count
);
140 objects_
.append(VariantPointer
<export_object_entry_t
>::asQVariant(entry
));
144 export_object_entry_t
* ExportObjectModel::objectEntry(int row
)
146 return VariantPointer
<export_object_entry_t
>::asPtr(objects_
.value(row
));
149 bool ExportObjectModel::saveEntry(QModelIndex
&index
, QString filename
)
151 if (!index
.isValid() || filename
.isEmpty())
154 export_object_entry_t
*entry
= VariantPointer
<export_object_entry_t
>::asPtr(objects_
.value(index
.row()));
158 if (filename
.length() > 0) {
159 write_file_binary_mode(qUtf8Printable(filename
), entry
->payload_data
, entry
->payload_len
);
165 void ExportObjectModel::saveAllEntries(QString path
)
171 export_object_entry_t
*entry
;
173 for (QList
<QVariant
>::iterator it
= objects_
.begin(); it
!= objects_
.end(); ++it
)
175 entry
= VariantPointer
<export_object_entry_t
>::asPtr(*it
);
183 GString
*safe_filename
;
186 safe_filename
= eo_massage_str(entry
->filename
,
187 EXPORT_OBJECT_MAXFILELEN
, count
);
189 char generic_name
[EXPORT_OBJECT_MAXFILELEN
+1];
191 ext
= eo_ct2ext(entry
->content_type
);
192 snprintf(generic_name
, sizeof(generic_name
),
193 "object%u%s%s", entry
->pkt_num
, ext
? "." : "",
195 safe_filename
= eo_massage_str(generic_name
,
196 EXPORT_OBJECT_MAXFILELEN
, count
);
198 filename
= QString::fromUtf8(safe_filename
->str
);
199 g_string_free(safe_filename
, TRUE
);
200 } while (save_dir
.exists(filename
) && ++count
< prefs
.gui_max_export_objects
);
201 write_file_binary_mode(qUtf8Printable(save_dir
.filePath(filename
)),
202 entry
->payload_data
, entry
->payload_len
);
206 void ExportObjectModel::resetObjects()
208 export_object_gui_reset_cb reset_cb
= get_eo_reset_func(eo_
);
219 /* Runs at the beginning of tapping only */
220 void ExportObjectModel::resetTap(void *tapdata
)
222 export_object_list_t
*tap_object
= (export_object_list_t
*)tapdata
;
223 export_object_list_gui_t
*object_list
= (export_object_list_gui_t
*)tap_object
->gui_data
;
224 if (object_list
&& object_list
->model
)
225 object_list
->model
->resetObjects();
228 const char* ExportObjectModel::getTapListenerName()
230 return get_eo_tap_listener_name(eo_
);
233 void* ExportObjectModel::getTapData()
235 return &export_object_list_
;
238 tap_packet_cb
ExportObjectModel::getTapPacketFunc()
240 return get_eo_packet_func(eo_
);
243 void ExportObjectModel::removeTap()
245 eo_gui_data_
.model
= NULL
;
250 ExportObjectProxyModel::ExportObjectProxyModel(QObject
* parent
)
251 : QSortFilterProxyModel(parent
)
256 bool ExportObjectProxyModel::lessThan(const QModelIndex
&source_left
, const QModelIndex
&source_right
) const
258 export_object_entry_t
*left_entry
= VariantPointer
<export_object_entry_t
>::asPtr(sourceModel()->data(source_left
, Qt::UserRole
)),
259 *right_entry
= VariantPointer
<export_object_entry_t
>::asPtr(sourceModel()->data(source_right
, Qt::UserRole
));
261 if ((left_entry
!= NULL
) && (right_entry
!= NULL
))
263 switch (source_left
.column())
265 case ExportObjectModel::colPacket
:
266 return left_entry
->pkt_num
< right_entry
->pkt_num
;
267 case ExportObjectModel::colSize
:
268 return left_entry
->payload_len
< right_entry
->payload_len
;
269 case ExportObjectModel::colFilename
:
274 return QSortFilterProxyModel::lessThan(source_left
, source_right
);
277 void ExportObjectProxyModel::setContentFilterString(QString filter_
)
279 contentFilter_
= filter_
;
283 void ExportObjectProxyModel::setTextFilterString(QString filter_
)
285 textFilter_
= filter_
;
289 bool ExportObjectProxyModel::filterAcceptsRow(int source_row
, const QModelIndex
&/*source_parent*/) const
291 if (contentFilter_
.length() > 0)
293 QModelIndex idx
= sourceModel()->index(source_row
, ExportObjectModel::colContent
);
297 if (contentFilter_
.compare(idx
.data().toString()) != 0)
301 if (textFilter_
.length() > 0)
303 QModelIndex hostIdx
= sourceModel()->index(source_row
, ExportObjectModel::colHostname
);
304 QModelIndex fileIdx
= sourceModel()->index(source_row
, ExportObjectModel::colFilename
);
305 if (!hostIdx
.isValid() || !fileIdx
.isValid())
308 QString host
= hostIdx
.data().toString();
309 QString file
= fileIdx
.data().toString();
311 if (!host
.contains(textFilter_
) && !file
.contains(textFilter_
))