1 /* protocol_preferences_menu.h
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
14 #include <epan/prefs.h>
15 #include <epan/prefs-int.h>
16 #include <epan/proto.h>
18 #include <ui/preference_utils.h>
19 #include <wsutil/utf8_entities.h>
21 #include "protocol_preferences_menu.h"
23 #include <ui/qt/models/enabled_protocols_model.h>
24 #include <ui/qt/utils/qt_ui_utils.h>
25 #include "uat_dialog.h"
26 #include "wireshark_application.h"
29 // - Elide really long items?
30 // - Handle PREF_SAVE_FILENAME, PREF_OPEN_FILENAME and PREF_DIRNAME.
31 // - Handle color prefs.
33 class BoolPreferenceAction : public QAction
36 BoolPreferenceAction(pref_t *pref) :
40 setText(prefs_get_title(pref_));
42 setChecked(prefs_get_bool_value(pref_, pref_current));
45 unsigned int setBoolValue() {
46 return prefs_set_bool_value(pref_, isChecked(), pref_current);
53 class EnumPreferenceAction : public QAction
56 EnumPreferenceAction(pref_t *pref, const char *title, int enumval, QActionGroup *ag) :
66 unsigned int setEnumValue() {
67 return prefs_set_enum_value(pref_, enumval_, pref_current);
75 class UatPreferenceAction : public QAction
78 UatPreferenceAction(pref_t *pref) :
82 setText(QString("%1" UTF8_HORIZONTAL_ELLIPSIS).arg(prefs_get_title(pref_)));
85 void showUatDialog() {
86 UatDialog uat_dlg(parentWidget(), prefs_get_uat_value(pref_));
88 // Emitting PacketDissectionChanged directly from a QDialog can cause
90 wsApp->flushAppSignals();
97 // Preference requires an external editor (PreferenceEditorFrame)
98 class EditorPreferenceAction : public QAction
101 EditorPreferenceAction(pref_t *pref) :
105 QString title = prefs_get_title(pref_);
107 title.append(QString(": %1" UTF8_HORIZONTAL_ELLIPSIS).arg(gchar_free_to_qstring(prefs_pref_to_str(pref_, pref_current))));
111 pref_t *pref() { return pref_; }
118 // Preference callback
121 add_prefs_menu_item(pref_t *pref, gpointer menu_ptr)
123 ProtocolPreferencesMenu *pp_menu = static_cast<ProtocolPreferencesMenu *>(menu_ptr);
124 if (!pp_menu) return 1;
126 pp_menu->addMenuItem(pref);
133 ProtocolPreferencesMenu::ProtocolPreferencesMenu()
135 setTitle(tr("Protocol Preferences"));
139 void ProtocolPreferencesMenu::setModule(const QString module_name)
144 if (!module_name.isEmpty()) {
145 proto_id = proto_get_id_by_filter_name(module_name.toUtf8().constData());
149 module_name_.clear();
152 protocol_ = find_protocol_by_id(proto_id);
153 const QString long_name = proto_get_protocol_long_name(protocol_);
154 const QString short_name = proto_get_protocol_short_name(protocol_);
155 if (module_name.isEmpty() || proto_id < 0 || !protocol_) {
156 action = addAction(tr("No protocol preferences available"));
157 action->setDisabled(true);
161 QAction *disable_action = new QAction(tr("Disable %1" UTF8_HORIZONTAL_ELLIPSIS).arg(short_name), this);
162 connect(disable_action, SIGNAL(triggered(bool)), this, SLOT(disableProtocolTriggered()));
163 disable_action->setDisabled(!proto_can_toggle_protocol(proto_id));
165 module_ = prefs_find_module(module_name.toUtf8().constData());
166 if (!module_ || !prefs_is_registered_protocol(module_name.toUtf8().constData())) {
167 action = addAction(tr("%1 has no preferences").arg(long_name));
168 action->setDisabled(true);
170 addAction(disable_action);
174 module_name_ = module_name;
176 action = addAction(tr("Open %1 preferences" UTF8_HORIZONTAL_ELLIPSIS).arg(long_name));
177 action->setData(QString(module_name));
178 connect(action, SIGNAL(triggered(bool)), this, SLOT(modulePreferencesTriggered()));
181 prefs_pref_foreach(module_, add_prefs_menu_item, this);
183 if (!actions().last()->isSeparator()) {
186 addAction(disable_action);
189 void ProtocolPreferencesMenu::addMenuItem(preference *pref)
191 switch (prefs_get_type(pref)) {
194 BoolPreferenceAction *bpa = new BoolPreferenceAction(pref);
196 connect(bpa, SIGNAL(triggered(bool)), this, SLOT(boolPreferenceTriggered()));
201 QActionGroup *ag = new QActionGroup(this);
202 QMenu *enum_menu = addMenu(prefs_get_title(pref));
203 for (const enum_val_t *enum_valp = prefs_get_enumvals(pref); enum_valp->name; enum_valp++) {
204 EnumPreferenceAction *epa = new EnumPreferenceAction(pref, enum_valp->description, enum_valp->value, ag);
205 if (prefs_get_enum_value(pref, pref_current) == enum_valp->value) {
206 epa->setChecked(true);
208 enum_menu->addAction(epa);
209 connect(epa, SIGNAL(triggered(bool)), this, SLOT(enumPreferenceTriggered()));
216 case PREF_DECODE_AS_UINT:
217 case PREF_DECODE_AS_RANGE:
219 EditorPreferenceAction *epa = new EditorPreferenceAction(pref);
221 connect(epa, SIGNAL(triggered(bool)), this, SLOT(editorPreferenceTriggered()));
226 UatPreferenceAction *upa = new UatPreferenceAction(pref);
228 connect(upa, SIGNAL(triggered(bool)), this, SLOT(uatPreferenceTriggered()));
232 case PREF_STATIC_TEXT:
236 // A type we currently don't handle (e.g. PREF_SAVE_FILENAME). Just open
238 QString title = QString("%1" UTF8_HORIZONTAL_ELLIPSIS).arg(prefs_get_title(pref));
239 QAction *mpa = addAction(title);
240 connect(mpa, SIGNAL(triggered(bool)), this, SLOT(modulePreferencesTriggered()));
245 void ProtocolPreferencesMenu::disableProtocolTriggered()
247 EnabledProtocolsModel::disableProtocol(protocol_);
250 void ProtocolPreferencesMenu::modulePreferencesTriggered()
252 if (!module_name_.isEmpty()) {
253 emit showProtocolPreferences(module_name_);
257 void ProtocolPreferencesMenu::editorPreferenceTriggered()
259 EditorPreferenceAction *epa = static_cast<EditorPreferenceAction *>(QObject::sender());
262 if (epa->pref() && module_) {
263 emit editProtocolPreference(epa->pref(), module_);
267 void ProtocolPreferencesMenu::boolPreferenceTriggered()
269 BoolPreferenceAction *bpa = static_cast<BoolPreferenceAction *>(QObject::sender());
272 module_->prefs_changed_flags |= bpa->setBoolValue();
274 prefs_apply(module_);
275 if (!prefs.gui_use_pref_save) {
279 /* Protocol preference changes almost always affect dissection,
280 so don't bother checking flags */
281 wsApp->emitAppSignal(WiresharkApplication::PacketDissectionChanged);
284 void ProtocolPreferencesMenu::enumPreferenceTriggered()
286 EnumPreferenceAction *epa = static_cast<EnumPreferenceAction *>(QObject::sender());
289 unsigned int changed_flags = epa->setEnumValue();
290 if (changed_flags) { // Changed
291 module_->prefs_changed_flags |= changed_flags;
292 prefs_apply(module_);
293 if (!prefs.gui_use_pref_save) {
297 /* Protocol preference changes almost always affect dissection,
298 so don't bother checking flags */
299 wsApp->emitAppSignal(WiresharkApplication::PacketDissectionChanged);
303 void ProtocolPreferencesMenu::uatPreferenceTriggered()
305 UatPreferenceAction *upa = static_cast<UatPreferenceAction *>(QObject::sender());
308 upa->showUatDialog();
317 * indent-tabs-mode: nil
320 * ex: set shiftwidth=4 tabstop=8 expandtab:
321 * :indentSize=4:tabSize=8:noTabs=true: