From e231e177d0a0473ae314a108f9526cd912de3dc0 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 17 Nov 2024 02:16:55 +0200
Subject: [PATCH 88/88] gtk4x: Use GListStore implemention of pages.c host list

See RM #1129

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-5.0/pages.c | 202 +++++--------------------------------
 1 file changed, 26 insertions(+), 176 deletions(-)

diff --git a/client/gui-gtk-5.0/pages.c b/client/gui-gtk-5.0/pages.c
index f116c9551c..cc5dd364c4 100644
--- a/client/gui-gtk-5.0/pages.c
+++ b/client/gui-gtk-5.0/pages.c
@@ -67,14 +67,13 @@ static GtkWidget *scenario_authors;
 static GtkWidget *scenario_filename;
 static GtkWidget *scenario_version;
 
-static GtkListStore *load_store, *scenario_store, *meta_store_depr, *lan_store_depr;
+static GtkListStore *load_store, *scenario_store;
 static GListStore *lan_store, *meta_store;
 
 static GtkListStore *server_playerlist_store;
 static GtkWidget *server_playerlist_view;
 
 static GtkTreeSelection *load_selection, *scenario_selection;
-static GtkTreeSelection *meta_selection_depr, *lan_selection_depr;
 static GtkSingleSelection *meta_selection, *lan_selection;
 
 /* This is the current page. Invalid value at start, to be sure that it won't
@@ -993,40 +992,36 @@ static GtkWidget *network_confirm_password_label, *network_confirm_password;
 static void update_server_list(enum server_scan_type sstype,
                                const struct server_list *list)
 {
-  GtkTreeSelection *sel_depr = nullptr;
-  GListStore *store = nullptr;
-  GtkTreeView *view;
-  GtkTreeIter it;
-  GtkListStore *store_depr;
+  GListStore *store;
+  GtkSingleSelection *sel = nullptr;
   const gchar *host, *portstr;
   int port;
+  int i;
 
   switch (sstype) {
   case SERVER_SCAN_LOCAL:
-    sel_depr = lan_selection_depr;
-    store = G_LIST_STORE(gtk_single_selection_get_model(lan_selection));
+    sel = lan_selection;
     break;
   case SERVER_SCAN_GLOBAL:
-    sel_depr = meta_selection_depr;
-    store = G_LIST_STORE(gtk_single_selection_get_model(meta_selection));
+    sel = meta_selection;
     break;
   default:
     break;
   }
 
-  if (sel_depr == nullptr) {
+  if (sel == nullptr) {
     return;
   }
 
+  store = G_LIST_STORE(gtk_single_selection_get_model(sel));
+
   if (store == nullptr) {
     return;
   }
 
-  view = gtk_tree_selection_get_tree_view(sel_depr);
-  store_depr = GTK_LIST_STORE(gtk_tree_view_get_model(view));
-  gtk_list_store_clear(store_depr);
+  g_list_store_remove_all(store);
 
-  if (!list) {
+  if (list == nullptr) {
     return;
   }
 
@@ -1034,7 +1029,9 @@ static void update_server_list(enum server_scan_type sstype,
   portstr = gtk_entry_buffer_get_text(gtk_entry_get_buffer(GTK_ENTRY(network_port)));
   port = atoi(portstr);
 
+  i = 0;
   server_list_iterate(list, pserver) {
+    FcHostRow *row = fc_host_row_new();
     char buf[35];
 
     if (pserver->humans >= 0) {
@@ -1042,21 +1039,6 @@ static void update_server_list(enum server_scan_type sstype,
     } else {
       sz_strlcpy(buf, _("Unknown"));
     }
-    gtk_list_store_append(store_depr, &it);
-    gtk_list_store_set(store_depr, &it,
-                       0, pserver->host,
-                       1, pserver->port,
-                       2, pserver->version,
-                       3, _(pserver->state),
-                       4, pserver->nplayers,
-                       5, buf,
-                       6, pserver->message,
-                       -1);
-    if (strcmp(host, pserver->host) == 0 && port == pserver->port) {
-      gtk_tree_selection_select_iter(sel_depr, &it);
-    }
-
-    FcHostRow *row = fc_host_row_new();
 
     row->host = pserver->host;
     row->port = pserver->port;
@@ -1068,6 +1050,12 @@ static void update_server_list(enum server_scan_type sstype,
 
     g_list_store_append(store, row);
     g_object_unref(row);
+
+    if (strcmp(host, pserver->host) == 0 && port == pserver->port) {
+      gtk_single_selection_set_selected(sel, i);
+    }
+
+    i++;
   } server_list_iterate_end;
 }
 
@@ -1407,17 +1395,6 @@ static void connect_callback(GtkWidget *w, gpointer data)
   log_error("Unsupported connection status: %d", connection_status);
 }
 
-/**********************************************************************//**
-  Connect on list item double-click.
-**************************************************************************/
-static void network_activate_callback_depr(GtkTreeView *view,
-                                           GtkTreePath *arg1,
-                                           GtkTreeViewColumn *arg2,
-                                           gpointer data)
-{
-  connect_callback(NULL, data);
-}
-
 /**********************************************************************//**
   Fills the server player list with the players in the given server, or
   clears it if there is no player data.
@@ -1448,52 +1425,6 @@ static void update_server_playerlist(const struct server *pserver)
   }
 }
 
-/**********************************************************************//**
-  Sets the host, port and player list of the selected server.
-**************************************************************************/
-static void network_list_callback_depr(GtkTreeSelection *select, gpointer data)
-{
-  GtkTreeModel *model;
-  GtkTreeIter it;
-  const char *host;
-  int port;
-  char portstr[32];
-  const struct server *pserver = NULL;
-
-  if (!gtk_tree_selection_get_selected(select, &model, &it)) {
-    return;
-  }
-
-  if (select == meta_selection_depr) {
-    GtkTreePath *path;
-    struct srv_list *srvrs;
-
-    srvrs = server_scan_get_list(meta_scan.scan);
-    path = gtk_tree_model_get_path(model, &it);
-    if (!holding_srv_list_mutex) {
-      /* We are not yet inside mutex protected block */
-      fc_mutex_allocate(&srvrs->mutex);
-    }
-    if (srvrs->servers && path) {
-      gint pos = gtk_tree_path_get_indices(path)[0];
-
-      pserver = server_list_get(srvrs->servers, pos);
-    }
-    if (!holding_srv_list_mutex) {
-      /* We are not yet inside mutex protected block */
-      fc_mutex_release(&srvrs->mutex);
-    }
-    gtk_tree_path_free(path);
-  }
-  update_server_playerlist(pserver);
-
-  gtk_tree_model_get(model, &it, 0, &host, 1, &port, -1);
-
-  gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(network_host)), host, -1);
-  fc_snprintf(portstr, sizeof(portstr), "%d", port);
-  gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(network_port)), portstr, -1);
-}
-
 /**********************************************************************//**
   Sets the host, port and player list of the selected server.
 **************************************************************************/
@@ -1538,8 +1469,8 @@ static void update_network_page(void)
 {
   char buf[256];
 
-  gtk_tree_selection_unselect_all(lan_selection_depr);
-  gtk_tree_selection_unselect_all(meta_selection_depr);
+  gtk_single_selection_set_selected(lan_selection, GTK_INVALID_LIST_POSITION);
+  gtk_single_selection_set_selected(meta_selection, GTK_INVALID_LIST_POSITION);
 
   gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(network_login)), user_name, -1);
   gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(network_host)), server_host, -1);
@@ -1642,7 +1573,6 @@ GtkWidget *create_network_page(void)
 {
   GtkWidget *box, *sbox, *bbox, *hbox, *notebook;
   GtkWidget *button, *label, *view, *sw, *table;
-  GtkTreeSelection *selection_depr;
   GtkListStore *store;
   GtkEventController *controller;
   GtkListItemFactory *factory;
@@ -1659,14 +1589,6 @@ GtkWidget *create_network_page(void)
   gtk_box_append(GTK_BOX(box), notebook);
 
   /* LAN pane. */
-  lan_store_depr = gtk_list_store_new(7, G_TYPE_STRING, /* host */
-                                      G_TYPE_INT,       /* port */
-                                      G_TYPE_STRING,    /* version */
-                                      G_TYPE_STRING,    /* state */
-                                      G_TYPE_INT,       /* nplayers */
-                                      G_TYPE_STRING,    /* humans */
-                                      G_TYPE_STRING);   /* message */
-
   lan_store = g_list_store_new(FC_TYPE_HOST_ROW);
   lan_selection = gtk_single_selection_new(G_LIST_MODEL(lan_store));
   list = gtk_column_view_new(GTK_SELECTION_MODEL(lan_selection));
@@ -1734,60 +1656,20 @@ GtkWidget *create_network_page(void)
   column = gtk_column_view_column_new(_("Comment"), factory);
   gtk_column_view_append_column(GTK_COLUMN_VIEW(list), column);
 
-  view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(lan_store_depr));
-  gtk_widget_set_hexpand(view, TRUE);
-  gtk_widget_set_vexpand(view, TRUE);
-  g_object_unref(lan_store_depr);
-  gtk_tree_view_columns_autosize(GTK_TREE_VIEW(view));
-
-  selection_depr = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
-  lan_selection_depr = selection_depr;
-  gtk_tree_selection_set_mode(selection_depr, GTK_SELECTION_SINGLE);
-
   controller = gtk_event_controller_focus_new();
   g_signal_connect(controller, "enter",
                    G_CALLBACK(terminate_signal_processing), NULL);
-  gtk_widget_add_controller(view, controller);
-
-  g_signal_connect(view, "row-activated",
-                   G_CALLBACK(network_activate_callback_depr), NULL);
-  g_signal_connect(selection_depr, "changed",
-                   G_CALLBACK(network_list_callback_depr), NULL);
+  gtk_widget_add_controller(list, controller);
 
   g_signal_connect(lan_selection, "selection-changed",
                    G_CALLBACK(network_list_callback), NULL);
 
-  add_treeview_column(view, _("Server Name"), G_TYPE_STRING, 0);
-  add_treeview_column(view, _("Port"), G_TYPE_INT, 1);
-  add_treeview_column(view, _("Version"), G_TYPE_STRING, 2);
-  add_treeview_column(view, _("Status"), G_TYPE_STRING, 3);
-  add_treeview_column(view, _("Players"), G_TYPE_INT, 4);
-  add_treeview_column(view, _("Humans"), G_TYPE_STRING, 5);
-  add_treeview_column(view, _("Comment"), G_TYPE_STRING, 6);
-
   label = gtk_label_new_with_mnemonic(_("Local _Area Network"));
 
-  sw = gtk_scrolled_window_new();
-  gtk_widget_set_margin_start(sw, 4);
-  gtk_widget_set_margin_end(sw, 4);
-  gtk_widget_set_margin_top(sw, 4);
-  gtk_widget_set_margin_bottom(sw, 4);
-  gtk_scrolled_window_set_has_frame(GTK_SCROLLED_WINDOW(sw), TRUE);
-  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
-                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-  gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), view);
-  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, label);
+  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), list, label);
 
 
   /* Metaserver pane. */
-  meta_store_depr = gtk_list_store_new(7, G_TYPE_STRING, /* host */
-                                       G_TYPE_INT,       /* port */
-                                       G_TYPE_STRING,    /* version */
-                                       G_TYPE_STRING,    /* state */
-                                       G_TYPE_INT,       /* nplayers */
-                                       G_TYPE_STRING,    /* humans */
-                                       G_TYPE_STRING);   /* message */
-
   meta_store = g_list_store_new(FC_TYPE_HOST_ROW);
   meta_selection = gtk_single_selection_new(G_LIST_MODEL(meta_store));
   list = gtk_column_view_new(GTK_SELECTION_MODEL(meta_selection));
@@ -1855,52 +1737,20 @@ GtkWidget *create_network_page(void)
   column = gtk_column_view_column_new(_("Comment"), factory);
   gtk_column_view_append_column(GTK_COLUMN_VIEW(list), column);
 
-  view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(meta_store_depr));
-  gtk_widget_set_hexpand(view, TRUE);
-  gtk_widget_set_vexpand(view, TRUE);
-  g_object_unref(meta_store_depr);
-  gtk_tree_view_columns_autosize(GTK_TREE_VIEW(view));
-
-  selection_depr = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
-  meta_selection_depr = selection_depr;
-  gtk_tree_selection_set_mode(selection_depr, GTK_SELECTION_SINGLE);
-
   controller = gtk_event_controller_focus_new();
   g_signal_connect(controller, "enter",
                    G_CALLBACK(terminate_signal_processing), NULL);
-  gtk_widget_add_controller(view, controller);
-
-  g_signal_connect(view, "row-activated",
-                   G_CALLBACK(network_activate_callback_depr), NULL);
-  g_signal_connect(selection_depr, "changed",
-                   G_CALLBACK(network_list_callback_depr), NULL);
+  gtk_widget_add_controller(list, controller);
 
   g_signal_connect(meta_selection, "selection-changed",
                    G_CALLBACK(network_list_callback), NULL);
 
-  add_treeview_column(view, _("Server Name"), G_TYPE_STRING, 0);
-  add_treeview_column(view, _("Port"), G_TYPE_INT, 1);
-  add_treeview_column(view, _("Version"), G_TYPE_STRING, 2);
-  add_treeview_column(view, _("Status"), G_TYPE_STRING, 3);
-  add_treeview_column(view, _("Players"), G_TYPE_INT, 4);
-  add_treeview_column(view, _("Humans"), G_TYPE_STRING, 5);
-  add_treeview_column(view, _("Comment"), G_TYPE_STRING, 6);
-
   label = gtk_label_new_with_mnemonic(_("Internet _Metaserver"));
 
-  sw = gtk_scrolled_window_new();
-  gtk_widget_set_margin_start(sw, 4);
-  gtk_widget_set_margin_end(sw, 4);
-  gtk_widget_set_margin_top(sw, 4);
-  gtk_widget_set_margin_bottom(sw, 4);
-  gtk_scrolled_window_set_has_frame(GTK_SCROLLED_WINDOW(sw), TRUE);
-  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
-                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-  gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), view);
   if (GUI_GTK_OPTION(metaserver_tab_first)) {
-    gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook), sw, label);
+    gtk_notebook_prepend_page(GTK_NOTEBOOK(notebook), list, label);
   } else {
-    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, label);
+    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), list, label);
   }
 
   /* Bottom part of the page, outside the inner notebook. */
-- 
2.45.2

