Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / ui / qt / main_window_preferences_frame.cpp
blob5f1647f861d681da4796252e2bdaecf0420b8f3f
1 /* main_window_preferences_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 "main_window_preferences_frame.h"
11 #include <ui/qt/utils/qt_ui_utils.h>
13 #include <ui_main_window_preferences_frame.h>
14 #include "ui/language.h"
16 #include <epan/prefs-int.h>
17 #include <ui/qt/models/pref_models.h>
18 #include <ui/qt/utils/color_utils.h>
19 #include <wsutil/filesystem.h>
20 #include "ui/qt/widgets/wireshark_file_dialog.h"
22 #include <QDebug>
24 MainWindowPreferencesFrame::MainWindowPreferencesFrame(QWidget *parent) :
25 QFrame(parent),
26 ui(new Ui::MainWindowPreferencesFrame)
28 ui->setupUi(this);
30 pref_geometry_save_position_ = prefFromPrefPtr(&prefs.gui_geometry_save_position);
31 pref_geometry_save_size_ = prefFromPrefPtr(&prefs.gui_geometry_save_size);
32 pref_geometry_save_maximized_ = prefFromPrefPtr(&prefs.gui_geometry_save_maximized);
33 pref_fileopen_style_ = prefFromPrefPtr(&prefs.gui_fileopen_style);
34 pref_fileopen_dir_ = prefFromPrefPtr(&prefs.gui_fileopen_dir);
35 pref_recent_df_entries_max_ = prefFromPrefPtr(&prefs.gui_recent_df_entries_max);
36 pref_recent_files_count_max_ = prefFromPrefPtr(&prefs.gui_recent_files_count_max);
37 pref_ask_unsaved_ = prefFromPrefPtr(&prefs.gui_ask_unsaved);
38 pref_autocomplete_filter_ = prefFromPrefPtr(&prefs.gui_autocomplete_filter);
39 pref_toolbar_main_style_ = prefFromPrefPtr(&prefs.gui_toolbar_main_style);
40 pref_window_title_ = prefFromPrefPtr(&prefs.gui_window_title);
41 pref_prepend_window_title_ = prefFromPrefPtr(&prefs.gui_prepend_window_title);
43 QStyleOption style_opt;
44 QString indent_ss = QStringLiteral(
45 "QRadioButton, QLineEdit, QLabel {"
46 " margin-left: %1px;"
47 "}"
48 ).arg(ui->geometryCheckBox->style()->subElementRect(QStyle::SE_CheckBoxContents, &style_opt).left());
49 ui->foStyleLastOpenedRadioButton->setStyleSheet(indent_ss);
50 ui->foStyleSpecifiedRadioButton->setStyleSheet(indent_ss);
51 ui->foStyleCWDRadioButton->setStyleSheet(indent_ss);
52 ui->maxFilterLineEdit->setStyleSheet(indent_ss);
53 ui->maxRecentLineEdit->setStyleSheet(indent_ss);
55 int num_entry_width = ui->maxFilterLineEdit->fontMetrics().height() * 3;
56 int num_entry_height = ui->maxFilterLineEdit->fontMetrics().height();
57 // Some styles (e.g., adwaita) add some extra space around the contents.
58 // Find the actual maximum size to set the widget.
59 QStyleOptionFrame opt;
60 initStyleOption(&opt);
61 QSize num_entry_size = ui->maxRecentLineEdit->style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(num_entry_width, num_entry_height));
62 ui->maxFilterLineEdit->setMaximumWidth(num_entry_size.width());
63 ui->maxRecentLineEdit->setMaximumWidth(num_entry_size.width());
65 QString li_path = QStringLiteral(":/languages/language%1.svg").arg(ColorUtils::themeIsDark() ? ".dark" : "");
66 QIcon language_icon = QIcon(li_path);
67 ui->languageComboBox->setItemIcon(0, language_icon);
69 QString globalLanguagesPath(QStringLiteral("%1/languages/").arg(get_datafile_dir()));
70 QString userLanguagesPath(gchar_free_to_qstring(get_persconffile_path("languages/", false)));
72 QStringList filenames = QDir(":/i18n/").entryList(QStringList("wireshark_*.qm"));
73 filenames += QDir(globalLanguagesPath).entryList(QStringList("wireshark_*.qm"));
74 filenames += QDir(userLanguagesPath).entryList(QStringList("wireshark_*.qm"));
76 for (int i = 0; i < filenames.size(); i += 1) {
77 QString locale;
78 locale = filenames[i];
79 locale.truncate(locale.lastIndexOf('.'));
80 locale.remove(0, locale.indexOf('_') + 1);
82 QString lang = QLocale::languageToString(QLocale(locale).language());
84 ui->languageComboBox->addItem(lang, locale);
87 ui->languageComboBox->setItemData(0, USE_SYSTEM_LANGUAGE);
88 ui->languageComboBox->model()->sort(0);
90 for (int i = 0; i < ui->languageComboBox->count(); i += 1) {
91 if (QString(language) == ui->languageComboBox->itemData(i).toString()) {
92 ui->languageComboBox->setCurrentIndex(i);
93 break;
99 MainWindowPreferencesFrame::~MainWindowPreferencesFrame()
101 delete ui;
104 void MainWindowPreferencesFrame::showEvent(QShowEvent *)
106 updateWidgets();
109 void MainWindowPreferencesFrame::updateWidgets()
111 // Yes, this means we're potentially clobbering two prefs in favor of one.
112 if (prefs_get_bool_value(pref_geometry_save_position_, pref_stashed) || prefs_get_bool_value(pref_geometry_save_size_, pref_stashed) || prefs_get_bool_value(pref_geometry_save_maximized_, pref_stashed)) {
113 ui->geometryCheckBox->setChecked(true);
114 } else {
115 ui->geometryCheckBox->setChecked(false);
118 switch (prefs_get_enum_value(pref_fileopen_style_, pref_stashed)) {
120 case FO_STYLE_LAST_OPENED:
121 ui->foStyleLastOpenedRadioButton->setChecked(true);
122 break;
123 case FO_STYLE_CWD:
124 ui->foStyleCWDRadioButton->setChecked(true);
125 break;
126 case FO_STYLE_SPECIFIED:
127 default:
128 ui->foStyleSpecifiedRadioButton->setChecked(true);
129 break;
132 ui->foStyleSpecifiedLineEdit->setText(prefs_get_string_value(pref_fileopen_dir_, pref_stashed));
134 ui->maxFilterLineEdit->setText(QString::number(prefs_get_uint_value(pref_recent_df_entries_max_, pref_stashed)));
135 ui->maxRecentLineEdit->setText(QString::number(prefs_get_uint_value(pref_recent_files_count_max_, pref_stashed)));
137 ui->confirmUnsavedCheckBox->setChecked(prefs_get_bool_value(pref_ask_unsaved_, pref_stashed));
138 ui->displayAutoCompleteCheckBox->setChecked(prefs_get_bool_value(pref_autocomplete_filter_, pref_stashed));
140 ui->mainToolbarComboBox->setCurrentIndex(prefs_get_enum_value(pref_toolbar_main_style_, pref_stashed));
142 for (int i = 0; i < ui->languageComboBox->count(); i += 1) {
143 if (QString(language) == ui->languageComboBox->itemData(i).toString()) {
144 ui->languageComboBox->setCurrentIndex(i);
145 break;
149 ui->windowTitle->setText(prefs_get_string_value(pref_window_title_, pref_stashed));
150 ui->prependWindowTitle->setText(prefs_get_string_value(pref_prepend_window_title_, pref_stashed));
153 void MainWindowPreferencesFrame::on_geometryCheckBox_toggled(bool checked)
155 prefs_set_bool_value(pref_geometry_save_position_, checked, pref_stashed);
156 prefs_set_bool_value(pref_geometry_save_size_, checked, pref_stashed);
157 prefs_set_bool_value(pref_geometry_save_maximized_, checked, pref_stashed);
160 void MainWindowPreferencesFrame::on_foStyleCWDRadioButton_toggled(bool checked)
162 if (checked) {
163 prefs_set_enum_value(pref_fileopen_style_, FO_STYLE_CWD, pref_stashed);
167 void MainWindowPreferencesFrame::on_foStyleLastOpenedRadioButton_toggled(bool checked)
169 if (checked) {
170 prefs_set_enum_value(pref_fileopen_style_, FO_STYLE_LAST_OPENED, pref_stashed);
174 void MainWindowPreferencesFrame::on_foStyleSpecifiedRadioButton_toggled(bool checked)
176 if (checked) {
177 prefs_set_enum_value(pref_fileopen_style_, FO_STYLE_SPECIFIED, pref_stashed);
181 void MainWindowPreferencesFrame::on_foStyleSpecifiedLineEdit_textEdited(const QString &new_dir)
183 prefs_set_string_value(pref_fileopen_dir_, new_dir.toStdString().c_str(), pref_stashed);
184 ui->foStyleSpecifiedRadioButton->setChecked(true);
187 void MainWindowPreferencesFrame::on_foStyleSpecifiedPushButton_clicked()
189 QString specified_dir = WiresharkFileDialog::getExistingDirectory(this, tr("Open Files In"));
191 if (specified_dir.isEmpty()) return;
193 ui->foStyleSpecifiedLineEdit->setText(specified_dir);
194 prefs_set_string_value(pref_fileopen_dir_, specified_dir.toStdString().c_str(), pref_stashed);
195 ui->foStyleSpecifiedRadioButton->setChecked(true);
198 void MainWindowPreferencesFrame::on_maxFilterLineEdit_textEdited(const QString &new_max)
200 prefs_set_uint_value(pref_recent_df_entries_max_, new_max.toUInt(), pref_stashed);
203 void MainWindowPreferencesFrame::on_maxRecentLineEdit_textEdited(const QString &new_max)
205 prefs_set_uint_value(pref_recent_files_count_max_, new_max.toUInt(), pref_stashed);
208 void MainWindowPreferencesFrame::on_confirmUnsavedCheckBox_toggled(bool checked)
210 prefs_set_bool_value(pref_ask_unsaved_, checked, pref_stashed);
213 void MainWindowPreferencesFrame::on_displayAutoCompleteCheckBox_toggled(bool checked)
215 prefs_set_bool_value(pref_autocomplete_filter_, checked, pref_stashed);
218 void MainWindowPreferencesFrame::on_mainToolbarComboBox_currentIndexChanged(int index)
220 prefs_set_enum_value(pref_toolbar_main_style_, index, pref_stashed);
223 void MainWindowPreferencesFrame::on_languageComboBox_currentIndexChanged(int index)
225 g_free(language);
227 language = qstring_strdup(ui->languageComboBox->itemData(index).toString());
230 void MainWindowPreferencesFrame::on_windowTitle_textEdited(const QString &new_title)
232 prefs_set_string_value(pref_window_title_, new_title.toStdString().c_str(), pref_stashed);
235 void MainWindowPreferencesFrame::on_prependWindowTitle_textEdited(const QString &new_prefix)
237 prefs_set_string_value(pref_prepend_window_title_, new_prefix.toStdString().c_str(), pref_stashed);