Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2004:
[Freeciv-Dev] (PR#10917) connect to LAN game - GUI bug
Home

[Freeciv-Dev] (PR#10917) connect to LAN game - GUI bug

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: chrisk@xxxxxxxxx, jdorje@xxxxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#10917) connect to LAN game - GUI bug
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Mon, 8 Nov 2004 19:12:02 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10917 >

> [vasc - Mon Nov 08 19:54:48 2004]:
> 
> > [kauf - Sun Nov 07 02:15:16 2004]:
> > 
> > here's hack to fix it.
> 
> The hack should work, but there must be a better way of doing this.
> Shorter and hopefully re-using this feature for the metaserver list.
> 
> I'll try to make one, although someone else will need to test.

Didn't end up being shorter. But this uses the same code for both
the meta and lan lists, fixing some bugs along the way. I re-used kauf's
code for updating the list without destroying the selection.

Index: client/gui-gtk-2.0/pages.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/pages.c,v
retrieving revision 1.1.2.10
diff -u -r1.1.2.10 pages.c
--- client/gui-gtk-2.0/pages.c  1 Nov 2004 22:02:58 -0000       1.1.2.10
+++ client/gui-gtk-2.0/pages.c  9 Nov 2004 03:08:11 -0000
@@ -244,38 +244,104 @@
 static GtkWidget *network_confirm_password_label, *network_confirm_password;
 
 /**************************************************************************
-  get the list of servers from the metaserver
+  update a server list.
 **************************************************************************/
-static int get_meta_list(char *errbuf, int n_errbuf)
+static void update_server_list(GtkListStore *store, struct server_list *list)
 {
-  struct server_list *server_list = create_server_list(errbuf, n_errbuf);
+  GtkTreeModel *model = GTK_TREE_MODEL(store);
 
-  if (!server_list) {
-    return -1;
-  }
+  if (list) {
+    GtkTreeIter it, it_next;
 
-  gtk_list_store_clear(meta_store);
+    /* first pass: find matching servers already in the store. */
+    if (gtk_tree_model_get_iter_first(model, &it)) {
+      bool more;
 
-  server_list_iterate(*server_list, pserver) {
-    GtkTreeIter it;
-    gchar *row[6];
+      do {
+       char *host, *port;
+       bool found = FALSE;
+
+       gtk_tree_model_get(model, &it, 0, &host, 1, &port, -1);
+
+       server_list_iterate(*list, pserver) {
+         /* the store already has this server, don't delete it. */
+         if (strcmp(host, pserver->host) == 0
+             && strcmp(port, pserver->port) == 0) {
+           found = TRUE;
+           break;            
+         }
+       } server_list_iterate_end;
+
+       /* get next pointer now, we're going to nuke this list item. */
+       it_next = it;
+       more = gtk_tree_model_iter_next(model, &it_next);
+
+       /* if the server is not found, delete it from the store. */
+       if (!found) {
+         gtk_list_store_remove(store, &it);
+       }
+
+       it = it_next;
+      } while (more);
+    }
+
+    /* second pass: add all the new servers to the store. */
+    server_list_iterate(*list, pserver) {
+      gchar *row[6];
+      bool found = FALSE;
+
+      if (gtk_tree_model_get_iter_first(model, &it)) {
+       do {
+         char *host, *port;
+
+         gtk_tree_model_get(model, &it, 0, &host, 1, &port, -1);
+
+         /* the store already has this server, don't add it again. */
+         if (strcmp(host, pserver->host) == 0
+             && strcmp(port, pserver->port) == 0) {
+           found = TRUE;
+           break;
+         }
+       } while (gtk_tree_model_iter_next(model, &it));
+      }
 
-    row[0] = pserver->host;
-    row[1] = pserver->port;
-    row[2] = pserver->version;
-    row[3] = _(pserver->state);
-    row[4] = pserver->nplayers;
-    row[5] = pserver->message;
-
-    gtk_list_store_append(meta_store, &it);
-    gtk_list_store_set(meta_store, &it,
-                      0, row[0], 1, row[1], 2, row[2],
-                      3, row[3], 4, row[4], 5, row[5], -1);
+      /* if the server is not found, add it to the store. */
+      if (!found) {
+       gtk_list_store_append(store, &it);
+      }
+
+      /* update server data. */
+      row[0] = pserver->host;
+      row[1] = pserver->port;
+      row[2] = pserver->version;
+      row[3] = _(pserver->state);
+      row[4] = pserver->nplayers;
+      row[5] = pserver->message;
+
+      gtk_list_store_set(store, &it,
+         0, row[0], 1, row[1], 2, row[2],
+         3, row[3], 4, row[4], 5, row[5], -1);
+    } server_list_iterate_end;
+  } else {
+    gtk_list_store_clear(store);
   }
-  server_list_iterate_end;
+}
 
-  delete_server_list(server_list);
-  return 0;
+/**************************************************************************
+  get the list of servers from the metaserver.
+**************************************************************************/
+static bool get_meta_list(char *errbuf, int n_errbuf)
+{
+  struct server_list *server_list = create_server_list(errbuf, n_errbuf);
+
+  update_server_list(meta_store, server_list);
+
+  if (server_list) {
+    delete_server_list(server_list);
+    return TRUE;
+  } else {
+    return FALSE;
+  }
 }
 
 /**************************************************************************
@@ -295,26 +361,7 @@
 {
   struct server_list *server_list = get_lan_server_list();
 
-  if (server_list) {
-    gtk_list_store_clear(lan_store);
-
-    server_list_iterate(*server_list, pserver) {
-      GtkTreeIter it;
-      gchar *row[6];
-
-      row[0] = pserver->host;
-      row[1] = pserver->port;
-      row[2] = pserver->version;
-      row[3] = _(pserver->state);
-      row[4] = pserver->nplayers;
-      row[5] = pserver->message;
-
-      gtk_list_store_append(lan_store, &it);
-      gtk_list_store_set(lan_store, &it,
-                        0, row[0], 1, row[1], 2, row[2],
-                        3, row[3], 4, row[4], 5, row[5], -1);
-    } server_list_iterate_end;
-  }
+  update_server_list(lan_store, server_list);
 
   num_lanservers_timer++;
   if (num_lanservers_timer == 50) {
@@ -331,7 +378,7 @@
 {
   char errbuf[128];
 
-  if (get_meta_list(errbuf, sizeof(errbuf)) == -1)  {
+  if (!get_meta_list(errbuf, sizeof(errbuf)))  {
     append_output_window(errbuf);
   }
 

[Prev in Thread] Current Thread [Next in Thread]