Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / ui / qt / uat_frame.cpp
blob2f8e89ca45ab46c1aee4502cb680d68ff5f7d431
1 /* uat_frame.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "config.h"
12 #include <epan/filter_expressions.h>
14 #include "uat_frame.h"
15 #include <ui_uat_frame.h>
16 #include <ui/qt/widgets/display_filter_edit.h>
17 #include "main_application.h"
19 #include <ui/qt/widgets/copy_from_profile_button.h>
20 #include <ui/qt/utils/qt_ui_utils.h>
21 #include <wsutil/report_message.h>
23 #include <QLineEdit>
24 #include <QKeyEvent>
25 #include <QTreeWidgetItemIterator>
26 #include <QUrl>
28 #include <QDebug>
30 UatFrame::UatFrame(QWidget *parent) :
31 QFrame(parent),
32 ui(new Ui::UatFrame),
33 uat_model_(NULL),
34 uat_delegate_(NULL),
35 uat_(NULL)
37 ui->setupUi(this);
39 ui->newToolButton->setStockIcon("list-add");
40 ui->deleteToolButton->setStockIcon("list-remove");
41 ui->copyToolButton->setStockIcon("list-copy");
42 ui->moveUpToolButton->setStockIcon("list-move-up");
43 ui->moveDownToolButton->setStockIcon("list-move-down");
44 ui->clearToolButton->setStockIcon("list-clear");
46 #ifdef Q_OS_MAC
47 ui->newToolButton->setAttribute(Qt::WA_MacSmallSize, true);
48 ui->deleteToolButton->setAttribute(Qt::WA_MacSmallSize, true);
49 ui->copyToolButton->setAttribute(Qt::WA_MacSmallSize, true);
50 ui->moveUpToolButton->setAttribute(Qt::WA_MacSmallSize, true);
51 ui->moveDownToolButton->setAttribute(Qt::WA_MacSmallSize, true);
52 ui->clearToolButton->setAttribute(Qt::WA_MacSmallSize, true);
53 ui->pathLabel->setAttribute(Qt::WA_MacSmallSize, true);
54 #endif
56 // FIXME: this prevents the columns from being resized, even if the text
57 // within a combobox needs more space (e.g. in the USER DLT settings). For
58 // very long filenames in the TLS RSA keys dialog, it also results in a
59 // vertical scrollbar. Maybe remove this since the editor is not limited to
60 // the column width (and overlays other fields if more width is needed)?
61 ui->uatTreeView->header()->setSectionResizeMode(QHeaderView::Interactive);
63 // start editing as soon as the field is selected or when typing starts
64 ui->uatTreeView->setEditTriggers(ui->uatTreeView->editTriggers() |
65 QAbstractItemView::CurrentChanged | QAbstractItemView::AnyKeyPressed);
68 UatFrame::~UatFrame()
70 delete ui;
71 delete uat_delegate_;
72 delete uat_model_;
75 void UatFrame::setUat(epan_uat *uat)
77 QString title(tr("Unknown User Accessible Table"));
79 uat_ = uat;
81 ui->pathLabel->clear();
82 ui->pathLabel->setEnabled(false);
84 if (uat_) {
85 if (uat_->name) {
86 title = uat_->name;
89 if (uat->from_profile) {
90 ui->copyFromProfileButton->setFilename(uat->filename);
91 connect(ui->copyFromProfileButton, &CopyFromProfileButton::copyProfile, this, &UatFrame::copyFromProfile);
94 QString abs_path = gchar_free_to_qstring(uat_get_actual_filename(uat_, false));
95 if (abs_path.length() > 0) {
96 ui->pathLabel->setText(abs_path);
97 ui->pathLabel->setUrl(QUrl::fromLocalFile(abs_path).toString());
98 ui->pathLabel->setToolTip(tr("Open ") + uat->filename);
99 } else {
100 ui->pathLabel->setText(uat_->filename);
102 ui->pathLabel->setEnabled(true);
104 uat_model_ = new UatModel(NULL, uat);
105 uat_delegate_ = new UatDelegate;
106 ui->uatTreeView->setModel(uat_model_);
107 ui->uatTreeView->setItemDelegate(uat_delegate_);
108 ui->uatTreeView->setSelectionMode(QAbstractItemView::ContiguousSelection);
109 resizeColumns();
110 ui->clearToolButton->setEnabled(uat_model_->rowCount() != 0);
112 connect(uat_model_, &UatModel::dataChanged, this, &UatFrame::modelDataChanged);
113 connect(uat_model_, &UatModel::rowsRemoved,this, &UatFrame::modelRowsRemoved);
114 connect(uat_model_, &UatModel::modelReset, this, &UatFrame::modelRowsReset);
116 connect(ui->uatTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
117 this, &UatFrame::uatTreeViewSelectionChanged);
120 setWindowTitle(title);
123 void UatFrame::copyFromProfile(QString filename)
125 char *err = NULL;
126 if (uat_load(uat_, filename.toUtf8().constData(), &err)) {
127 uat_->changed = true;
128 uat_model_->reloadUat();
129 } else {
130 report_failure("Error while loading %s: %s", uat_->name, err);
131 g_free(err);
135 void UatFrame::showEvent(QShowEvent *)
137 #ifndef Q_OS_MAC
138 ui->copyFromProfileButton->setFixedHeight(ui->copyToolButton->geometry().height());
139 #endif
142 void UatFrame::applyChanges()
144 if (!uat_) return;
146 if (uat_->flags & UAT_AFFECTS_FIELDS) {
147 /* Recreate list with new fields */
148 mainApp->queueAppSignal(MainApplication::FieldsChanged);
150 if (uat_->flags & UAT_AFFECTS_DISSECTION) {
151 /* Redissect packets if we have any */
152 mainApp->queueAppSignal(MainApplication::PacketDissectionChanged);
156 void UatFrame::acceptChanges()
158 if (!uat_model_) return;
160 QString error;
161 if (uat_model_->applyChanges(error)) {
162 if (!error.isEmpty()) {
163 report_failure("%s", qPrintable(error));
165 applyChanges();
169 void UatFrame::rejectChanges()
171 if (!uat_model_) return;
173 QString error;
174 if (uat_model_->revertChanges(error)) {
175 if (!error.isEmpty()) {
176 report_failure("%s", qPrintable(error));
181 void UatFrame::addRecord(bool copy_from_current)
183 if (!uat_) return;
185 QModelIndex current = ui->uatTreeView->currentIndex();
186 if (copy_from_current && !current.isValid()) return;
188 QModelIndex new_index;
189 if (copy_from_current) {
190 new_index = uat_model_->copyRow(current);
191 } else {
192 // should not fail, but you never know.
193 if (!uat_model_->insertRows(uat_model_->rowCount(), 1)) {
194 qDebug() << "Failed to add a new record";
195 return;
197 new_index = uat_model_->index(uat_model_->rowCount() - 1, 0);
200 // due to an EditTrigger, this will also start editing.
201 ui->uatTreeView->setCurrentIndex(new_index);
202 // trigger updating error messages and the OK button state.
203 modelDataChanged(new_index);
206 void UatFrame::uatTreeViewSelectionChanged(const QItemSelection&, const QItemSelection&)
208 QModelIndexList selectedRows = ui->uatTreeView->selectionModel()->selectedRows();
209 qsizetype num_selected = selectedRows.size();
210 if (num_selected > 0) {
211 std::sort(selectedRows.begin(), selectedRows.end());
212 ui->deleteToolButton->setEnabled(true);
213 ui->copyToolButton->setEnabled(true);
214 ui->moveUpToolButton->setEnabled(selectedRows.first().row() > 0);
215 ui->moveDownToolButton->setEnabled(selectedRows.last().row() < uat_model_->rowCount() - 1);
216 } else {
217 ui->deleteToolButton->setEnabled(false);
218 ui->copyToolButton->setEnabled(false);
219 ui->moveUpToolButton->setEnabled(false);
220 ui->moveDownToolButton->setEnabled(false);
224 // Invoked when a different field is selected. Note: when selecting a different
225 // field after editing, this event is triggered after modelDataChanged.
226 void UatFrame::on_uatTreeView_currentItemChanged(const QModelIndex &current, const QModelIndex &previous)
228 if (current.isValid()) {
229 ui->clearToolButton->setEnabled(true);
230 } else {
231 ui->clearToolButton->setEnabled(false);
234 checkForErrorHint(current, previous);
237 // Invoked when a field in the model changes (e.g. by closing the editor)
238 void UatFrame::modelDataChanged(const QModelIndex &topLeft)
240 checkForErrorHint(topLeft, QModelIndex());
241 resizeColumns();
244 // Invoked after a row has been removed from the model.
245 void UatFrame::modelRowsRemoved()
247 const QModelIndex &current = ui->uatTreeView->currentIndex();
249 // Because currentItemChanged() is called before the row is removed from the model
250 // we also need to check for button enabling here.
251 if (current.isValid()) {
252 ui->moveUpToolButton->setEnabled(current.row() != 0);
253 ui->moveDownToolButton->setEnabled(current.row() != (uat_model_->rowCount() - 1));
254 } else {
255 ui->moveUpToolButton->setEnabled(false);
256 ui->moveDownToolButton->setEnabled(false);
258 ui->clearToolButton->setEnabled(uat_model_->rowCount() != 0);
260 checkForErrorHint(current, QModelIndex());
263 void UatFrame::modelRowsReset()
265 ui->deleteToolButton->setEnabled(false);
266 ui->clearToolButton->setEnabled(uat_model_->rowCount() != 0);
267 ui->copyToolButton->setEnabled(false);
268 ui->moveUpToolButton->setEnabled(false);
269 ui->moveDownToolButton->setEnabled(false);
272 // If the current field has errors, show them.
273 // Otherwise if the row has not changed, but the previous field has errors, show them.
274 // Otherwise pick the first error in the current row.
275 // Otherwise show the error from the previous field (if any).
276 // Otherwise clear the error hint.
277 void UatFrame::checkForErrorHint(const QModelIndex &current, const QModelIndex &previous)
279 if (current.isValid()) {
280 if (trySetErrorHintFromField(current)) {
281 return;
284 const int row = current.row();
285 if (row == previous.row() && trySetErrorHintFromField(previous)) {
286 return;
289 for (int i = 0; i < uat_model_->columnCount(); i++) {
290 if (trySetErrorHintFromField(uat_model_->index(row, i))) {
291 return;
296 if (previous.isValid()) {
297 if (trySetErrorHintFromField(previous)) {
298 return;
302 ui->hintLabel->clear();
305 bool UatFrame::trySetErrorHintFromField(const QModelIndex &index)
307 const QVariant &data = uat_model_->data(index, Qt::UserRole + 1);
308 if (!data.isNull()) {
309 // use HTML instead of PlainText because that handles wordwrap properly
310 ui->hintLabel->setText("<small><i>" + html_escape(data.toString()) + "</i></small>");
311 return true;
313 return false;
316 void UatFrame::on_newToolButton_clicked()
318 addRecord();
321 void UatFrame::on_deleteToolButton_clicked()
323 if (uat_model_ == nullptr) {
324 return;
327 for (const auto &range : ui->uatTreeView->selectionModel()->selection()) {
328 // Each QItemSelectionRange is contiguous
329 if (!range.isEmpty()) {
330 if (!uat_model_->removeRows(range.top(), range.bottom() - range.top() + 1)) {
331 qDebug() << "Failed to remove rows" << range.top() << "to" << range.bottom();
337 void UatFrame::on_copyToolButton_clicked()
339 if (uat_model_ == nullptr) {
340 return;
343 QModelIndexList selectedRows = ui->uatTreeView->selectionModel()->selectedRows();
344 if (selectedRows.size() > 0) {
345 std::sort(selectedRows.begin(), selectedRows.end());
347 QModelIndex copyIdx;
349 for (const auto &idx : selectedRows) {
350 copyIdx = uat_model_->copyRow(idx);
351 if (!copyIdx.isValid())
353 qDebug() << "Failed to copy row" << idx.row();
355 // trigger updating error messages and the OK button state.
356 modelDataChanged(copyIdx);
358 // due to an EditTrigger, this will also start editing.
359 ui->uatTreeView->setCurrentIndex(copyIdx);
364 void UatFrame::on_moveUpToolButton_clicked()
366 if (uat_model_ == nullptr) {
367 return;
370 for (const auto &range : ui->uatTreeView->selectionModel()->selection()) {
371 // Each QItemSelectionRange is contiguous
372 if (!range.isEmpty() && range.top() > 0) {
373 // Swap range of rows with the row above the top
374 if (! uat_model_->moveRows(QModelIndex(), range.top(), range.bottom() - range.top() + 1, QModelIndex(), range.top() - 1)) {
375 qDebug() << "Failed to move up rows" << range.top() << "to" << range.bottom();
377 // Our moveRows implementation calls begin/endMoveRows(), so
378 // range.top() already has the new row number.
379 ui->moveUpToolButton->setEnabled(range.top() > 0);
380 ui->moveDownToolButton->setEnabled(true);
385 void UatFrame::on_moveDownToolButton_clicked()
387 if (uat_model_ == nullptr) {
388 return;
391 for (const auto &range : ui->uatTreeView->selectionModel()->selection()) {
392 // Each QItemSelectionRange is contiguous
393 if (!range.isEmpty() && range.bottom() + 1 < uat_model_->rowCount()) {
394 // Swap range of rows with the row below the top
395 if (! uat_model_->moveRows(QModelIndex(), range.top(), range.bottom() - range.top() + 1, QModelIndex(), range.bottom() + 1)) {
396 qDebug() << "Failed to move down rows" << range.top() << "to" << range.bottom();
398 // Our moveRows implementation calls begin/endMoveRows, so
399 // range.bottom() already has the new row number.
400 ui->moveUpToolButton->setEnabled(true);
401 ui->moveDownToolButton->setEnabled(range.bottom() < uat_model_->rowCount() - 1);
406 void UatFrame::on_clearToolButton_clicked()
408 if (uat_model_) {
409 uat_model_->clearAll();
413 void UatFrame::resizeColumns()
415 for (int i = 0; i < uat_model_->columnCount(); i++) {
416 ui->uatTreeView->resizeColumnToContents(i);
417 if (i == 0) {
418 ui->uatTreeView->setColumnWidth(i, ui->uatTreeView->columnWidth(i)+ui->uatTreeView->indentation());