diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:34:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:34:10 +0000 |
commit | e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc (patch) | |
tree | 68cb5ef9081156392f1dd62a00c6ccc1451b93df /ui/qt/models/fileset_entry_model.cpp | |
parent | Initial commit. (diff) | |
download | wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.tar.xz wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.zip |
Adding upstream version 4.2.2.upstream/4.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ui/qt/models/fileset_entry_model.cpp')
-rw-r--r-- | ui/qt/models/fileset_entry_model.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/ui/qt/models/fileset_entry_model.cpp b/ui/qt/models/fileset_entry_model.cpp new file mode 100644 index 00000000..8c9f504b --- /dev/null +++ b/ui/qt/models/fileset_entry_model.cpp @@ -0,0 +1,155 @@ +/* fileset_entry_model.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/models/fileset_entry_model.h> + +#include "wsutil/utf8_entities.h" + +#include <ui/qt/utils/qt_ui_utils.h> + +#include <QRegularExpression> + +FilesetEntryModel::FilesetEntryModel(QObject * parent) : + QAbstractItemModel(parent) +{} + +QModelIndex FilesetEntryModel::index(int row, int column, const QModelIndex &) const +{ + if (row >= entries_.count() || row < 0 || column > ColumnCount) { + return QModelIndex(); + } + + return createIndex(row, column, const_cast<fileset_entry *>(entries_.at(row))); +} + +int FilesetEntryModel::rowCount(const QModelIndex &) const +{ + return static_cast<int>(entries_.count()); +} + +QVariant FilesetEntryModel::data(const QModelIndex &index, int role) const +{ + if (! index.isValid() || index.row() >= rowCount()) + return QVariant(); + + const fileset_entry *entry = static_cast<fileset_entry*>(index.internalPointer()); + if (role == Qt::DisplayRole && entry) { + switch (index.column()) { + case Name: + return QString(entry->name); + case Created: + { + QString created = nameToDate(entry->name); + if (created.length() < 1) { + /* if this file doesn't follow the file set pattern, */ + /* use the creation time of that file if available */ + /* https://en.wikipedia.org/wiki/ISO_8601 */ + /* + * macOS provides 0 if the file system doesn't support the + * creation time; FreeBSD provides -1. + * + * If this OS doesn't provide the creation time with stat(), + * it will be 0. + */ + if (entry->ctime > 0) { + created = time_tToString(entry->ctime); + } else { + created = UTF8_EM_DASH; + } + } + return created; + } + case Modified: + return time_tToString(entry->mtime); + case Size: + return file_size_to_qstring(entry->size); + default: + break; + } + } else if (role == Qt::ToolTipRole) { + return QString(tr("Open this capture file")); + } else if (role == Qt::TextAlignmentRole) { + switch (index.column()) { + case Size: + // Not perfect but better than nothing. + return Qt::AlignRight; + default: + return Qt::AlignLeft; + } + } + return QVariant(); +} + +QVariant FilesetEntryModel::headerData(int section, Qt::Orientation, int role) const +{ + if (role != Qt::DisplayRole) return QVariant(); + + switch (section) { + case Name: + return tr("Filename"); + case Created: + return tr("Created"); + case Modified: + return tr("Modified"); + case Size: + return tr("Size"); + default: + break; + } + return QVariant(); +} + +void FilesetEntryModel::appendEntry(const fileset_entry *entry) +{ + emit beginInsertRows(QModelIndex(), rowCount(), rowCount()); + entries_ << entry; + emit endInsertRows(); +} + +void FilesetEntryModel::clear() +{ + fileset_delete(); + beginResetModel(); + entries_.clear(); + endResetModel(); +} + +QString FilesetEntryModel::nameToDate(const char *name) const { + QString dn; + + if (!fileset_filename_match_pattern(name)) + return NULL; + + dn = name; + dn.remove(QRegularExpression(".*_")); + dn.truncate(14); + dn.insert(4, '-'); + dn.insert(7, '-'); + dn.insert(10, ' '); + dn.insert(13, ':'); + dn.insert(16, ':'); + return dn; +} + +QString FilesetEntryModel::time_tToString(time_t clock) const +{ + struct tm *local = localtime(&clock); + if (!local) return UTF8_EM_DASH; + + // yyyy-MM-dd HH:mm:ss + // The equivalent QDateTime call is pretty slow here, possibly related to QTBUG-21678 + // and/or QTBUG-41714. + return QString("%1-%2-%3 %4:%5:%6") + .arg(local->tm_year + 1900, 4, 10, QChar('0')) + .arg(local->tm_mon+1, 2, 10, QChar('0')) + .arg(local->tm_mday, 2, 10, QChar('0')) + .arg(local->tm_hour, 2, 10, QChar('0')) + .arg(local->tm_min, 2, 10, QChar('0')) + .arg(local->tm_sec, 2, 10, QChar('0')); +} |