prefs,ui: properly migrate old filter expressions

When legacy filter expressions preferences are present, be sure to
(1) save the dfilter_buttons file when updating the preferences file and
(2) save the preferences file after updating the dfilter_buttons file.

Without doing (1), the old buttons will be lost. Without (2), restarting
Wireshark will result in duplicate buttons. Only write the files if the
user actually made other changes to avoid breaking profiles by just
opening Wireshark.

Bug: 15114
Bug: 15121
Change-Id: I5b9417c34146c4450526781a20308c9c98fb44a1
Reviewed-on: https://code.wireshark.org/review/29488
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
Petri-Dish: Stig Bjørlykke <stig@bjorlykke.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
This commit is contained in:
Peter Wu 2018-09-08 11:49:22 +02:00
parent 7573f7dab4
commit ad0de0ffbc
6 changed files with 45 additions and 16 deletions

View File

@ -5388,6 +5388,8 @@ set_pref(gchar *pref_name, const gchar *value, void *private_data _U_,
filter_expression_new(filter_label, value, "", filter_enabled);
g_free(filter_label);
filter_label = NULL;
/* Remember to save the new UAT to file. */
prefs.filter_expressions_old = TRUE;
} else if (strcmp(pref_name, "gui.version_in_start_page") == 0) {
/* Convert deprecated value to closest current equivalent */
if (g_ascii_strcasecmp(value, "true") == 0) {
@ -6586,6 +6588,21 @@ write_prefs(char **pf_path_return)
pf = stdout;
}
/*
* If the preferences file is being written, be sure to write UAT files
* first that were migrated from the preferences file.
*/
if (pf_path_return != NULL) {
if (prefs.filter_expressions_old) {
char *err = NULL;
prefs.filter_expressions_old = FALSE;
if (!uat_save(uat_get_table_by_name("Display expressions"), &err)) {
g_warning("Unable to save Display expressions: %s", err);
g_free(err);
}
}
}
fputs("# Configuration file for Wireshark " VERSION ".\n"
"#\n"
"# This file is regenerated each time preferences are saved within\n"

View File

@ -213,6 +213,7 @@ typedef struct _e_prefs {
gboolean enable_incomplete_dissectors_check;
gboolean incomplete_dissectors_check_debug;
gboolean strict_conversation_tracking_heuristics;
gboolean filter_expressions_old; /* TRUE if old filter expressions preferences were loaded. */
gboolean gui_update_enabled;
software_update_channel_e gui_update_channel;
gint gui_update_interval;

View File

@ -18,6 +18,7 @@
#include <epan/prefs-int.h>
#include <epan/packet.h>
#include <epan/decode_as.h>
#include <epan/uat-int.h>
#ifdef HAVE_LIBPCAP
#include "capture_opts.h"
@ -213,6 +214,23 @@ column_prefs_remove_nth(gint col)
column_prefs_remove_link(g_list_nth(prefs.col_list, col));
}
void save_migrated_uat(const char *uat_name, gboolean *old_pref)
{
char *err = NULL;
if (!uat_save(uat_get_table_by_name(uat_name), &err)) {
g_warning("Unable to save %s: %s", uat_name, err);
g_free(err);
return;
}
// Ensure that any old preferences are removed after successful migration.
if (*old_pref) {
*old_pref = FALSE;
prefs_main_write();
}
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*

View File

@ -83,6 +83,10 @@ void column_prefs_remove_link(GList* col_link);
*/
void column_prefs_remove_nth(gint col);
/** Save the UAT and complete migration of old preferences by writing the main
* preferences file (if necessary).
*/
void save_migrated_uat(const char *uat_name, gboolean *old_pref);
#ifdef __cplusplus
}

View File

@ -11,7 +11,6 @@
#include <ui_filter_expression_frame.h>
#include <epan/filter_expressions.h>
#include <epan/uat-int.h>
#include <ui/preference_utils.h>
#include <ui/qt/models/uat_model.h>
@ -122,7 +121,6 @@ void FilterExpressionFrame::on_displayFilterLineEdit_textChanged(const QString)
void FilterExpressionFrame::on_buttonBox_accepted()
{
gchar* err = NULL;
QByteArray label_ba = ui->labelLineEdit->text().toUtf8();
QByteArray expr_ba = ui->displayFilterLineEdit->text().toUtf8();
QByteArray comment_ba = ui->commentLineEdit->text().toUtf8();
@ -147,12 +145,10 @@ void FilterExpressionFrame::on_buttonBox_accepted()
{
filter_expression_new(label_ba.constData(), expr_ba.constData(), comment_ba.constData(), TRUE);
}
uat_save(uat_get_table_by_name("Display expressions"), &err);
save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
on_buttonBox_rejected();
emit filterExpressionsChanged();
g_free(err);
}
void FilterExpressionFrame::on_buttonBox_rejected()

View File

@ -14,6 +14,7 @@
#include <ui/qt/wireshark_application.h>
#include <epan/filter_expressions.h>
#include <ui/preference_utils.h>
#include <QApplication>
#include <QFrame>
@ -114,7 +115,6 @@ void FilterExpressionToolBar::filterExpressionsChanged()
void FilterExpressionToolBar::removeFilter()
{
gchar* err = NULL;
UatModel * uatModel = new UatModel(this, "Display expressions");
QString label = ((QAction *)sender())->property(dfe_property_label_).toString();
@ -126,8 +126,7 @@ void FilterExpressionToolBar::removeFilter()
if ( rowIndex.isValid() ) {
uatModel->removeRow(rowIndex.row());
uat_save(uat_get_table_by_name("Display expressions"), &err);
g_free(err);
save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
filterExpressionsChanged();
}
}
@ -155,8 +154,6 @@ void FilterExpressionToolBar::onActionMoved(QAction* action, int oldPos, int new
void FilterExpressionToolBar::disableFilter()
{
gchar* err = NULL;
QString label = ((QAction *)sender())->property(dfe_property_label_).toString();
QString expr = ((QAction *)sender())->property(dfe_property_expression_).toString();
@ -167,8 +164,7 @@ void FilterExpressionToolBar::disableFilter()
if ( rowIndex.isValid() ) {
uatModel->setData(rowIndex, QVariant::fromValue(false));
uat_save(uat_get_table_by_name("Display expressions"), &err);
g_free(err);
save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
filterExpressionsChanged();
}
}
@ -189,16 +185,13 @@ void FilterExpressionToolBar::editFilter()
void FilterExpressionToolBar::onFilterDropped(QString description, QString filter)
{
gchar* err = NULL;
if ( filter.length() == 0 )
return;
filter_expression_new(qUtf8Printable(description),
qUtf8Printable(filter), qUtf8Printable(description), TRUE);
uat_save(uat_get_table_by_name("Display expressions"), &err);
g_free(err);
save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
filterExpressionsChanged();
}