From 5ad43b561393913848165ff0331968f6d78ef8d9 Mon Sep 17 00:00:00 2001 From: pztrn Date: Thu, 6 Oct 2016 22:43:45 +0500 Subject: [PATCH] Fixed show/hide offline servers. --- cachemodels/server.go | 5 +++- ui/mainwindow.go | 70 ++++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/cachemodels/server.go b/cachemodels/server.go index 30879cf..33412cc 100644 --- a/cachemodels/server.go +++ b/cachemodels/server.go @@ -20,7 +20,10 @@ import ( type Server struct { Server *datamodels.Server AllServersIter *gtk.TreeIter - FavServersIter *gtk.TreeIter AllServersIterSet bool + AllServersIterInList bool + + FavServersIter *gtk.TreeIter FavServersIterSet bool + FavServersIterInList bool } diff --git a/ui/mainwindow.go b/ui/mainwindow.go index a2fbb9c..759cc21 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -951,31 +951,40 @@ func (m *MainWindow) loadAllServers() { fmt.Println("Loading all servers...") // ToDo: do it without clearing. for _, server := range ctx.Cache.Servers { - ping, _ := strconv.Atoi(server.Server.Ping) - if m.all_servers_hide_offline.GetActive() && ping > 9999 { - continue - } - iter := new (gtk.TreeIter) + iter := new(gtk.TreeIter) + if !server.AllServersIterSet { server.AllServersIter = iter - m.all_servers_store.Append(iter) server.AllServersIterSet = true } else { iter = server.AllServersIter } - if server.Server.Name == "" && server.Server.Players == "" { + + if !server.AllServersIterInList { + m.all_servers_store.Append(iter) + server.AllServersIterInList = true + } + + if m.all_servers_hide_offline.GetActive() && server.Server.Players == "" && server.Server.Maxplayers == "" { + m.all_servers_store.Remove(iter) + server.AllServersIterInList = false + continue + } + + if server.Server.Name == "" && server.Server.Players == "" && server.Server.Maxplayers == "" { m.all_servers_store.Set(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.all_servers_store.SetValue(iter, 7, server.Server.Ip + ":" + server.Server.Port) } else { m.all_servers_store.Set(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + server_name := ctx.Colorizer.Fix(server.Server.Name) + m.all_servers_store.SetValue(iter, 1, server_name) + m.all_servers_store.SetValue(iter, 2, m.gamemodes[server.Server.Gamemode]) + m.all_servers_store.SetValue(iter, 3, server.Server.Map) + m.all_servers_store.SetValue(iter, 4, server.Server.Players + "/" + server.Server.Maxplayers) + m.all_servers_store.SetValue(iter, 5, server.Server.Ping) + m.all_servers_store.SetValue(iter, 6, server.Server.Version) + m.all_servers_store.SetValue(iter, 7, server.Server.Ip + ":" + server.Server.Port) } - server_name := ctx.Colorizer.Fix(server.Server.Name) - m.all_servers_store.SetValue(iter, 1, server_name) - m.all_servers_store.SetValue(iter, 2, m.gamemodes[server.Server.Gamemode]) - m.all_servers_store.SetValue(iter, 3, server.Server.Map) - m.all_servers_store.SetValue(iter, 4, server.Server.Players + "/" + server.Server.Maxplayers) - m.all_servers_store.SetValue(iter, 5, server.Server.Ping) - m.all_servers_store.SetValue(iter, 6, server.Server.Version) - m.all_servers_store.SetValue(iter, 7, server.Server.Ip + ":" + server.Server.Port) } } @@ -985,23 +994,32 @@ func (m *MainWindow) loadFavoriteServers() { if server.Server.Favorite != "1" { continue } - if m.fav_servers_hide_offline.GetActive() && server.Server.Name == "" && server.Server.Players == "" { - continue - } + iter := new(gtk.TreeIter) + if !server.FavServersIterSet { server.FavServersIter = iter - m.fav_servers_store.Append(iter) server.FavServersIterSet = true } else { iter = server.FavServersIter } - if m.fav_servers_store.IterIsValid(iter) { - if server.Server.Name == "" && server.Server.Players == "" { - m.fav_servers_store.Set(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) - } else { - m.fav_servers_store.Set(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) - } + + if !server.FavServersIterInList { + m.fav_servers_store.Append(iter) + server.FavServersIterInList = true + } + + if m.fav_servers_hide_offline.GetActive() && server.Server.Players == "" && server.Server.Maxplayers == "" { + m.fav_servers_store.Remove(iter) + server.FavServersIterInList = false + continue + } + + if server.Server.Name == "" && server.Server.Players == "" { + m.fav_servers_store.Set(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.fav_servers_store.SetValue(iter, 7, server.Server.Ip + ":" + server.Server.Port) + } else { + m.fav_servers_store.Set(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) server_name := ctx.Colorizer.Fix(server.Server.Name) m.fav_servers_store.SetValue(iter, 1, server_name) m.fav_servers_store.SetValue(iter, 2, m.gamemodes[server.Server.Gamemode]) @@ -1010,8 +1028,6 @@ func (m *MainWindow) loadFavoriteServers() { m.fav_servers_store.SetValue(iter, 5, server.Server.Ping) m.fav_servers_store.SetValue(iter, 6, server.Server.Version) m.fav_servers_store.SetValue(iter, 7, server.Server.Ip + ":" + server.Server.Port) - } else { - fmt.Println("Invalid iter for server: " + server.Server.Ip + ":" + server.Server.Port) } } }