recent: Keep recent column width in sync with prefs

Keep the recent column width list in sync with the order of the
prefs.col_list by appending, inserting, and moving the recent
column width list at the same time, instead of allowing them
to get out of sync (as we use the format for a key.)

Fix an issue where column_prefs_add_custom did not always return
the position of the column added (when a column number was passed
in that was less than the maximum number of columns.)

Preparation for the width and alignment part of #15529
This commit is contained in:
John Thacker 2024-02-08 20:21:28 -05:00
parent 2574d5b9c6
commit 127548227e
5 changed files with 82 additions and 11 deletions

View File

@ -187,7 +187,8 @@ column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_fields
last_cfmt = (fmt_data *) clp->data;
if (position > 0 && position <= colnr) {
/* Custom fields may be added at any position, depending on the given argument */
prefs.col_list = g_list_insert(prefs.col_list, cfmt, position);
colnr = position;
prefs.col_list = g_list_insert(prefs.col_list, cfmt, colnr);
} else if (last_cfmt->fmt == COL_INFO) {
/* Last column is COL_INFO, add custom column before this */
colnr -= 1;
@ -199,6 +200,7 @@ column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_fields
cfmt->visible = FALSE; /* Will be set to TRUE in visible_toggled() when added to list */
prefs.col_list = g_list_append(prefs.col_list, cfmt);
}
recent_insert_column(colnr);
return colnr;
}
@ -274,6 +276,7 @@ void
column_prefs_remove_nth(gint col)
{
column_prefs_remove_link(g_list_nth(prefs.col_list, col));
recent_remove_column(col);
}
void save_migrated_uat(const char *uat_name, gboolean *old_pref)

View File

@ -563,10 +563,12 @@ void ColumnListModel::saveColumns()
prefs.col_list = new_col_list;
recent_free_column_width_info(&recent);
for (int row = 0; row < store_.count(); row++)
{
ListElement elem = store_.at(row);
recent_insert_column(row);
recent_set_column_width(row, elem.width);
recent_set_column_xalign(row, elem.xalign);
}

View File

@ -1823,6 +1823,7 @@ void PacketList::sectionResized(int col, int, int new_width)
void PacketList::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
{
GList *new_col_list = NULL;
GList *new_recent_col_list = NULL;
QList<int> saved_sizes;
int sort_idx;
@ -1847,9 +1848,14 @@ void PacketList::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisua
saved_sizes << header()->sectionSize(log_idx);
void *pref_data = g_list_nth_data(prefs.col_list, log_idx);
if (!pref_data) continue;
if (pref_data) {
new_col_list = g_list_append(new_col_list, pref_data);
}
new_col_list = g_list_append(new_col_list, pref_data);
pref_data = g_list_nth_data(recent.col_width_list, log_idx);
if (pref_data) {
new_recent_col_list = g_list_append(new_recent_col_list, pref_data);
}
}
// Undo move to ensure that the logical indices map to the visual indices,
@ -1867,6 +1873,8 @@ void PacketList::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisua
g_list_free(prefs.col_list);
prefs.col_list = new_col_list;
g_list_free(recent.col_width_list);
recent.col_width_list = new_recent_col_list;
thaw(true);

View File

@ -208,18 +208,17 @@ static const value_string show_bytes_decode_values[] = {
};
static void
free_col_width_data(gpointer data, gpointer user_data _U_)
free_col_width_data(gpointer data)
{
col_width_data *cfmt = (col_width_data *)data;
g_free(cfmt->cfield);
g_free(cfmt);
}
static void
free_col_width_info(recent_settings_t *rs)
void
recent_free_column_width_info(recent_settings_t *rs)
{
g_list_foreach(rs->col_width_list, free_col_width_data, NULL);
g_list_free(rs->col_width_list);
g_list_free_full(rs->col_width_list, free_col_width_data);
rs->col_width_list = NULL;
}
@ -1473,7 +1472,7 @@ read_set_recent_pair_static(gchar *key, const gchar *value,
/* Go past the width. */
col_l_elt = col_l_elt->next;
}
free_col_width_info(&recent);
recent_free_column_width_info(&recent);
recent.col_width_list = NULL;
col_l_elt = g_list_first(col_l);
while (col_l_elt) {
@ -1696,7 +1695,7 @@ recent_read_profile_static(char **rf_path_return, int *rf_errno_return)
}
if (recent.col_width_list) {
free_col_width_info(&recent);
recent_free_column_width_info(&recent);
}
if (recent.gui_fileopen_remembered_dir) {
@ -1801,6 +1800,43 @@ recent_read_dynamic(char **rf_path_return, int *rf_errno_return)
return TRUE;
}
void
recent_insert_column(int col)
{
col_width_data *col_w;
int cfmt;
const char *cfield = NULL;
cfmt = get_column_format(col);
if (cfmt == COL_CUSTOM) {
cfield = get_column_custom_fields(col);
}
col_w = g_new(col_width_data, 1);
col_w->cfmt = cfmt;
col_w->cfield = g_strdup(cfield);
col_w->width = -1;
col_w->xalign = COLUMN_XALIGN_DEFAULT;
recent.col_width_list = g_list_insert(recent.col_width_list, col_w, col);
}
void
recent_remove_column(int col)
{
GList *col_l = g_list_nth(recent.col_width_list, col);
col_width_data *col_w;
if (!col_l) return;
col_w = (col_width_data*)col_l->data;
if (col_w) {
free_col_width_data(col_w);
}
recent.col_width_list = g_list_delete_link(recent.col_width_list, col_l);
}
gint
recent_get_column_width(gint col)
{
@ -1942,7 +1978,7 @@ recent_init(void)
void
recent_cleanup(void)
{
free_col_width_info(&recent);
recent_free_column_width_info(&recent);
g_free(recent.gui_geometry_main);
g_free(recent.gui_geometry_main_master_split);
g_free(recent.gui_geometry_main_extra_split);

View File

@ -232,6 +232,28 @@ extern gboolean recent_read_dynamic(char **rf_path_return, int *rf_errno_return)
*/
extern int recent_set_arg(char *prefarg);
/** Free the recent settings list of column width information
*
* @param rs the recent settings (currently a global)
*/
extern void recent_free_column_width_info(recent_settings_t *rs);
/** Insert an entry in the recent column width setting for
* the given column, which should have been just added to
* the column list preference. (This keeps them in sync.)
*
* @param col column number
*/
extern void recent_insert_column(int col);
/** Remove an entry in the recent column width setting for
* the given column, which should have been just removed to
* the column list preference. (This keeps them in sync.)
*
* @param col column number
*/
extern void recent_remove_column(int col);
/** Get the column width for the given column
*
* @param col column number