update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / ui / qt / models / export_objects_model.cpp
blob3546db5ccb9a2c0f9adef6e0dd30580cef7237df
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
9 */
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>
18 #include <QDir>
20 extern "C" {
22 static void
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);
37 return NULL;
40 } // extern "C"
45 ExportObjectModel::ExportObjectModel(register_eo_t* eo, QObject *parent) :
46 QAbstractTableModel(parent),
47 eo_(eo)
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))) {
66 return QVariant();
69 if (role == Qt::DisplayRole)
71 export_object_entry_t *entry = VariantPointer<export_object_entry_t>::asPtr(objects_.value(index.row()));
72 if (entry == NULL)
73 return QVariant();
75 switch(index.column())
77 case colPacket:
78 return QString::number(entry->pkt_num);
79 case colHostname:
80 return QString::fromUtf8(entry->hostname);
81 case colContent:
82 return QString::fromUtf8(entry->content_type);
83 case colSize:
84 return file_size_to_qstring(entry->payload_len);
85 case colFilename:
86 return QString::fromUtf8(entry->filename);
89 else if (role == Qt::UserRole)
91 return objects_.value(index.row());
94 return QVariant();
97 QVariant ExportObjectModel::headerData(int section, Qt::Orientation orientation, int role) const
99 if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
100 return QVariant();
102 switch (section) {
103 case colPacket:
104 return tr("Packet");
105 case colHostname:
106 return tr("Hostname");
107 case colContent:
108 return tr("Content Type");
109 case colSize:
110 return tr("Size");
111 case colFilename:
112 return tr("Filename");
115 return QVariant();
118 int ExportObjectModel::rowCount(const QModelIndex &parent) const
120 // there are no children
121 if (parent.isValid()) {
122 return 0;
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)
135 if (entry == NULL)
136 return;
138 int count = static_cast<int>(objects_.count());
139 beginInsertRows(QModelIndex(), count, count);
140 objects_.append(VariantPointer<export_object_entry_t>::asQVariant(entry));
141 endInsertRows();
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())
152 return false;
154 export_object_entry_t *entry = VariantPointer<export_object_entry_t>::asPtr(objects_.value(index.row()));
155 if (entry == NULL)
156 return false;
158 if (filename.length() > 0) {
159 write_file_binary_mode(qUtf8Printable(filename), entry->payload_data, entry->payload_len);
162 return true;
165 void ExportObjectModel::saveAllEntries(QString path)
167 if (path.isEmpty())
168 return;
170 QDir save_dir(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);
176 if (entry == NULL)
177 continue;
179 unsigned count = 0;
180 QString filename;
182 do {
183 GString *safe_filename;
185 if (entry->filename)
186 safe_filename = eo_massage_str(entry->filename,
187 EXPORT_OBJECT_MAXFILELEN, count);
188 else {
189 char generic_name[EXPORT_OBJECT_MAXFILELEN+1];
190 const char *ext;
191 ext = eo_ct2ext(entry->content_type);
192 snprintf(generic_name, sizeof(generic_name),
193 "object%u%s%s", entry->pkt_num, ext ? "." : "",
194 ext ? 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_);
210 beginResetModel();
211 objects_.clear();
212 endResetModel();
214 if (reset_cb)
215 reset_cb();
218 // Called by taps
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:
270 break;
274 return QSortFilterProxyModel::lessThan(source_left, source_right);
277 void ExportObjectProxyModel::setContentFilterString(QString filter_)
279 contentFilter_ = filter_;
280 invalidateFilter();
283 void ExportObjectProxyModel::setTextFilterString(QString filter_)
285 textFilter_ = filter_;
286 invalidateFilter();
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);
294 if (!idx.isValid())
295 return false;
297 if (contentFilter_.compare(idx.data().toString()) != 0)
298 return false;
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())
306 return false;
308 QString host = hostIdx.data().toString();
309 QString file = fileIdx.data().toString();
311 if (!host.contains(textFilter_) && !file.contains(textFilter_))
312 return false;
315 return true;