From e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 22:34:10 +0200 Subject: Adding upstream version 4.2.2. Signed-off-by: Daniel Baumann --- ui/qt/preference_editor_frame.cpp | 296 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 ui/qt/preference_editor_frame.cpp (limited to 'ui/qt/preference_editor_frame.cpp') diff --git a/ui/qt/preference_editor_frame.cpp b/ui/qt/preference_editor_frame.cpp new file mode 100644 index 00000000..4ebce5c9 --- /dev/null +++ b/ui/qt/preference_editor_frame.cpp @@ -0,0 +1,296 @@ +/* preference_editor_frame.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "config.h" + +#include + +#include +#include +#include + +#include +#include + +#include "preference_editor_frame.h" +#include + +#include +#include +#include + +#include "main_application.h" + +#include +#include +#include + +PreferenceEditorFrame::PreferenceEditorFrame(QWidget *parent) : + AccordionFrame(parent), + ui(new Ui::PreferenceEditorFrame), + module_(NULL), + pref_(NULL), + new_uint_(0), + new_str_(""), + new_range_(NULL) +{ + ui->setupUi(this); + +#ifdef Q_OS_MAC + foreach (QWidget *w, findChildren()) { + w->setAttribute(Qt::WA_MacSmallSize, true); + } +#endif + + connect(ui->preferenceBrowseButton, &QPushButton::clicked, this, &PreferenceEditorFrame::browsePushButtonClicked); +} + +PreferenceEditorFrame::~PreferenceEditorFrame() +{ + delete ui; +} + +void PreferenceEditorFrame::editPreference(preference *pref, pref_module *module) +{ + pref_ = pref; + module_ = module; + + if (!pref || !module) { + hide(); + return; + } + + ui->modulePreferencesToolButton->setText(tr("Open %1 preferences…").arg(module_->title)); + + pref_stash(pref_, NULL); + ui->preferenceTitleLabel->setText(QString("%1:").arg(prefs_get_title(pref))); + + // Convert the pref description from plain text to rich text. + QString description = html_escape(prefs_get_description(pref)); + description.replace('\n', "
"); + QString tooltip = QString("%1").arg(description); + ui->preferenceTitleLabel->setToolTip(tooltip); + ui->preferenceLineEdit->setToolTip(tooltip); + + ui->preferenceLineEdit->clear(); + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty); + disconnect(ui->preferenceLineEdit, 0, 0, 0); + + bool show = false; + bool browse_button = false; + + switch (prefs_get_type(pref_)) { + case PREF_UINT: + case PREF_DECODE_AS_UINT: + connect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged, + this, &PreferenceEditorFrame::uintLineEditTextEdited); + show = true; + break; + case PREF_SAVE_FILENAME: + case PREF_OPEN_FILENAME: + case PREF_DIRNAME: + browse_button = true; + // Fallthrough + case PREF_STRING: + case PREF_PASSWORD: + connect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged, + this, &PreferenceEditorFrame::stringLineEditTextEdited); + show = true; + break; + case PREF_RANGE: + case PREF_DECODE_AS_RANGE: + connect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged, + this, &PreferenceEditorFrame::rangeLineEditTextEdited); + show = true; + break; + default: + break; + } + + if (show) { + ui->preferenceLineEdit->setText(gchar_free_to_qstring(prefs_pref_to_str(pref_, pref_stashed)).remove(QRegularExpression("\n\t"))); + ui->preferenceBrowseButton->setHidden(!browse_button); + animatedShow(); + } +} + +void PreferenceEditorFrame::uintLineEditTextEdited(const QString &new_str) +{ + if (new_str.isEmpty()) { + new_uint_ = prefs_get_uint_value_real(pref_, pref_stashed); + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty); + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); + return; + } + + bool ok; + uint new_uint = new_str.toUInt(&ok, 0); + if (ok) { + new_uint_ = new_uint; + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Valid); + } else { + new_uint_ = prefs_get_uint_value_real(pref_, pref_stashed); + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Invalid); + } + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok); +} + +void PreferenceEditorFrame::stringLineEditTextEdited(const QString &new_str) +{ + new_str_ = new_str; + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); +} + +void PreferenceEditorFrame::browsePushButtonClicked() +{ + QString caption = mainApp->windowTitleString(prefs_get_title(pref_)); + QString dir = prefs_get_string_value(pref_, pref_stashed); + QString filename; + + switch (prefs_get_type(pref_)) { + case PREF_SAVE_FILENAME: + filename = WiresharkFileDialog::getSaveFileName(this, caption, dir); + break; + case PREF_OPEN_FILENAME: + filename = WiresharkFileDialog::getOpenFileName(this, caption, dir); + break; + case PREF_DIRNAME: + filename = WiresharkFileDialog::getExistingDirectory(this, caption, dir); + break; + } + + if (!filename.isEmpty()) { + ui->preferenceLineEdit->setText(filename); + } +} + +void PreferenceEditorFrame::rangeLineEditTextEdited(const QString &new_str) +{ + range_t *new_range = NULL; + + convert_ret_t ret = range_convert_str(NULL, &new_range, new_str.toUtf8().constData(), prefs_get_max_value(pref_)); + wmem_free(NULL, new_range_); + new_range_ = new_range; + + if (ret == CVT_NO_ERROR) { + if (new_str.isEmpty()) { + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty); + } else { + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Valid); + } + } else { + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Invalid); + } + + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ret == CVT_NO_ERROR); +} + +void PreferenceEditorFrame::showEvent(QShowEvent *event) +{ + ui->preferenceLineEdit->setFocus(); + ui->preferenceLineEdit->selectAll(); + + AccordionFrame::showEvent(event); +} + +void PreferenceEditorFrame::on_modulePreferencesToolButton_clicked() +{ + if (module_) { + emit showProtocolPreferences(module_->name); + } + on_buttonBox_rejected(); +} + +void PreferenceEditorFrame::on_preferenceLineEdit_returnPressed() +{ + if (ui->buttonBox->button(QDialogButtonBox::Ok)->isEnabled()) { + on_buttonBox_accepted(); + } +} + +void PreferenceEditorFrame::on_buttonBox_accepted() +{ + unsigned int changed_flags = 0; + unsigned int apply = 0; + switch(prefs_get_type(pref_)) { + case PREF_UINT: + case PREF_DECODE_AS_UINT: + apply = prefs_set_uint_value(pref_, new_uint_, pref_stashed); + break; + case PREF_STRING: + case PREF_SAVE_FILENAME: + case PREF_OPEN_FILENAME: + case PREF_DIRNAME: + apply = prefs_set_string_value(pref_, new_str_.toStdString().c_str(), pref_stashed); + break; + case PREF_PASSWORD: + apply = prefs_set_password_value(pref_, new_str_.toStdString().c_str(), pref_stashed); + break; + case PREF_RANGE: + case PREF_DECODE_AS_RANGE: + apply = prefs_set_range_value(pref_, new_range_, pref_stashed); + break; + default: + break; + } + + if (apply && module_) { + changed_flags = module_->prefs_changed_flags; + pref_unstash_data_t unstashed_data; + + unstashed_data.module = module_; + unstashed_data.handle_decode_as = TRUE; + + pref_unstash(pref_, &unstashed_data); + prefs_apply(module_); + prefs_main_write(); + + gchar* err = NULL; + if (save_decode_as_entries(&err) < 0) + { + simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err); + g_free(err); + } + } + on_buttonBox_rejected(); + // Emit signals once UI is hidden + if (apply) { + if (changed_flags & PREF_EFFECT_FIELDS) { + mainApp->emitAppSignal(MainApplication::FieldsChanged); + } + mainApp->emitAppSignal(MainApplication::PacketDissectionChanged); + mainApp->emitAppSignal(MainApplication::PreferencesChanged); + } +} + +void PreferenceEditorFrame::on_buttonBox_rejected() +{ + pref_ = NULL; + module_ = NULL; + wmem_free(NULL, new_range_); + new_range_ = NULL; + animatedHide(); +} + +void PreferenceEditorFrame::keyPressEvent(QKeyEvent *event) +{ + if (event->modifiers() == Qt::NoModifier) { + if (event->key() == Qt::Key_Escape) { + on_buttonBox_rejected(); + } else if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { + if (ui->buttonBox->button(QDialogButtonBox::Ok)->isEnabled()) { + on_buttonBox_accepted(); + } else if (ui->preferenceLineEdit->syntaxState() == SyntaxLineEdit::Invalid) { + mainApp->pushStatus(MainApplication::FilterSyntax, tr("Invalid value.")); + } + } + } + + AccordionFrame::keyPressEvent(event); +} -- cgit v1.2.3