2016-10-04 23:39:32 +05:00
// URTator - Urban Terror server browser and game launcher, written in
// Go.
//
// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n)
// All rights reserved.
//
// Licensed under Terms and Conditions of GNU General Public License
// version 3 or any higher.
// ToDo: put full text of license here.
package ui
import (
// stdlib
"encoding/base64"
"errors"
"fmt"
2016-11-22 01:10:31 +05:00
"runtime"
2016-10-04 23:39:32 +05:00
"strings"
// Local
"github.com/pztrn/urtrator/common"
"github.com/pztrn/urtrator/datamodels"
// Other
"github.com/mattn/go-gtk/gdkpixbuf"
"github.com/mattn/go-gtk/gtk"
)
type FavoriteDialog struct {
// Widgets.
// Dialog's window.
window * gtk . Window
// Main vertical box.
vbox * gtk . VBox
// Server name.
server_name * gtk . Entry
// Server address.
server_address * gtk . Entry
// Server password
server_password * gtk . Entry
// Profile.
profile * gtk . ComboBoxText
// Flags.
// Is known server update performed?
update bool
// Data.
2016-10-08 00:29:46 +05:00
// Server's we're working with.
2016-10-04 23:39:32 +05:00
server * datamodels . Server
2016-10-08 00:29:46 +05:00
// Profiles count that was added to profiles combobox.
profiles int
2016-10-04 23:39:32 +05:00
}
func ( f * FavoriteDialog ) Close ( ) { }
func ( f * FavoriteDialog ) closeByCancel ( ) {
f . window . Destroy ( )
}
func ( f * FavoriteDialog ) fill ( ) {
f . server_name . SetText ( f . server . Name )
f . server_address . SetText ( f . server . Ip + ":" + f . server . Port )
f . server_password . SetText ( f . server . Password )
// Profiles.
2016-10-08 00:29:46 +05:00
// Remove old profiles.
if f . profiles > 0 {
for i := 0 ; i <= f . profiles ; i ++ {
f . profile . RemoveText ( 0 )
}
}
2016-10-04 23:39:32 +05:00
profiles := [ ] datamodels . Profile { }
err := ctx . Database . Db . Select ( & profiles , "SELECT * FROM urt_profiles" )
if err != nil {
fmt . Println ( err . Error ( ) )
}
2016-10-05 00:04:24 +05:00
var idx_in_combobox int = 0
var idx_should_be_active int = 0
2016-10-04 23:39:32 +05:00
for p := range profiles {
if profiles [ p ] . Version == f . server . Version {
f . profile . AppendText ( profiles [ p ] . Name )
2016-10-05 00:04:24 +05:00
idx_should_be_active = idx_in_combobox
idx_in_combobox += 1
2016-10-08 00:29:46 +05:00
f . profiles += 1
2016-10-04 23:39:32 +05:00
}
}
2016-10-05 00:04:24 +05:00
f . profile . SetActive ( idx_should_be_active )
2016-10-04 23:39:32 +05:00
}
func ( f * FavoriteDialog ) InitializeNew ( ) {
f . update = false
2016-10-08 00:29:46 +05:00
f . server = & datamodels . Server { }
f . profiles = 0
2016-10-05 00:04:24 +05:00
f . initializeWindow ( )
2016-10-04 23:39:32 +05:00
}
func ( f * FavoriteDialog ) InitializeUpdate ( server * datamodels . Server ) {
fmt . Println ( "Favorites updating..." )
f . update = true
f . server = server
2016-10-08 00:29:46 +05:00
f . profiles = 0
2016-10-05 00:04:24 +05:00
f . initializeWindow ( )
2016-10-04 23:39:32 +05:00
f . fill ( )
}
func ( f * FavoriteDialog ) initializeWindow ( ) {
f . window = gtk . NewWindow ( gtk . WINDOW_TOPLEVEL )
if f . update {
f . window . SetTitle ( "URTrator - Updating favorite server" )
} else {
f . window . SetTitle ( "URTrator - New favorite server" )
}
f . window . Connect ( "destroy" , f . Close )
f . window . SetPosition ( gtk . WIN_POS_CENTER )
f . window . SetModal ( true )
f . window . SetSizeRequest ( 400 , 200 )
f . window . SetResizable ( false )
f . vbox = gtk . NewVBox ( false , 0 )
// Load program icon from base64.
icon_bytes , _ := base64 . StdEncoding . DecodeString ( common . Logo )
icon_pixbuf := gdkpixbuf . NewLoader ( )
icon_pixbuf . Write ( icon_bytes )
logo = icon_pixbuf . GetPixbuf ( )
f . window . SetIcon ( logo )
2016-10-08 00:29:46 +05:00
// Set some GTK options for this window.
gtk_opts_raw := gtk . SettingsGetDefault ( )
gtk_opts := gtk_opts_raw . ToGObject ( )
gtk_opts . Set ( "gtk-button-images" , true )
2016-10-04 23:39:32 +05:00
// Server name.
srv_name_hbox := gtk . NewHBox ( false , 0 )
f . vbox . PackStart ( srv_name_hbox , false , true , 5 )
srv_name_label := gtk . NewLabel ( "Server name:" )
srv_name_hbox . PackStart ( srv_name_label , false , true , 5 )
srv_name_sep := gtk . NewHSeparator ( )
srv_name_hbox . PackStart ( srv_name_sep , true , true , 5 )
f . server_name = gtk . NewEntry ( )
srv_name_hbox . PackStart ( f . server_name , true , true , 5 )
// Server address.
srv_addr_hbox := gtk . NewHBox ( false , 0 )
f . vbox . PackStart ( srv_addr_hbox , false , true , 5 )
srv_addr_label := gtk . NewLabel ( "Server address:" )
srv_addr_hbox . PackStart ( srv_addr_label , false , true , 5 )
srv_addr_sep := gtk . NewHSeparator ( )
srv_addr_hbox . PackStart ( srv_addr_sep , true , true , 5 )
f . server_address = gtk . NewEntry ( )
srv_addr_hbox . PackStart ( f . server_address , true , true , 5 )
2016-10-08 00:29:46 +05:00
srv_addr_update_btn := gtk . NewButton ( )
srv_addr_update_btn . SetTooltipText ( "Update server information" )
2016-11-04 18:28:52 +05:00
srv_addr_update_btn_image := gtk . NewImageFromStock ( gtk . STOCK_REDO , gtk . ICON_SIZE_SMALL_TOOLBAR )
2016-10-08 00:29:46 +05:00
srv_addr_update_btn . SetImage ( srv_addr_update_btn_image )
srv_addr_update_btn . Clicked ( f . updateServerInfo )
srv_addr_hbox . PackStart ( srv_addr_update_btn , false , true , 5 )
2016-10-04 23:39:32 +05:00
if f . update {
f . server_address . SetSensitive ( false )
}
// Server password.
srv_pass_hbox := gtk . NewHBox ( false , 0 )
f . vbox . PackStart ( srv_pass_hbox , false , true , 5 )
srv_pass_label := gtk . NewLabel ( "Password:" )
srv_pass_hbox . PackStart ( srv_pass_label , false , true , 5 )
srv_pass_sep := gtk . NewHSeparator ( )
srv_pass_hbox . PackStart ( srv_pass_sep , true , true , 5 )
f . server_password = gtk . NewEntry ( )
srv_pass_hbox . PackStart ( f . server_password , true , true , 5 )
// Profile to use.
profile_hbox := gtk . NewHBox ( false , 0 )
f . vbox . PackStart ( profile_hbox , false , true , 5 )
profile_label := gtk . NewLabel ( "Profile:" )
profile_hbox . PackStart ( profile_label , false , true , 5 )
profile_sep := gtk . NewHSeparator ( )
profile_hbox . PackStart ( profile_sep , true , true , 5 )
f . profile = gtk . NewComboBoxText ( )
profile_hbox . PackStart ( f . profile , false , true , 5 )
// Buttons hbox.
buttons_hbox := gtk . NewHBox ( false , 0 )
sep := gtk . NewHSeparator ( )
buttons_hbox . PackStart ( sep , true , true , 5 )
// OK-Cancel buttons.
cancel_button := gtk . NewButtonWithLabel ( "Cancel" )
cancel_button . Clicked ( f . closeByCancel )
buttons_hbox . PackStart ( cancel_button , false , true , 5 )
ok_button := gtk . NewButtonWithLabel ( "OK" )
ok_button . Clicked ( f . saveFavorite )
buttons_hbox . PackStart ( ok_button , false , true , 5 )
f . vbox . PackStart ( buttons_hbox , false , true , 5 )
f . window . Add ( f . vbox )
f . window . ShowAll ( )
}
func ( f * FavoriteDialog ) saveFavorite ( ) error {
2016-10-08 00:29:46 +05:00
// Update server's information.
f . server . Name = f . server_name . GetText ( )
//ctx.Requester.Pooler.UpdateSpecificServer(f.server)
2016-10-04 23:39:32 +05:00
if len ( f . server_address . GetText ( ) ) == 0 {
2016-11-22 01:10:31 +05:00
// Temporary disable all these modals on Linux.
// See https://github.com/mattn/go-gtk/issues/289.
if runtime . GOOS != "linux" {
mbox_string := "Server address is empty.\n\nServers without address cannot be added."
m := gtk . NewMessageDialog ( f . window , gtk . DIALOG_MODAL , gtk . MESSAGE_ERROR , gtk . BUTTONS_OK , mbox_string )
m . Response ( func ( ) {
m . Destroy ( )
} )
m . Run ( )
}
2016-10-04 23:39:32 +05:00
return errors . New ( "No server address specified" )
}
2016-10-08 00:29:46 +05:00
var port string = ""
if strings . Contains ( f . server_address . GetText ( ) , ":" ) {
port = strings . Split ( f . server_address . GetText ( ) , ":" ) [ 1 ]
} else {
port = "27960"
}
f . server . Ip = strings . Split ( f . server_address . GetText ( ) , ":" ) [ 0 ]
f . server . Port = port
2016-10-05 02:47:13 +05:00
if len ( f . profile . GetActiveText ( ) ) == 0 {
2016-11-22 01:10:31 +05:00
// Temporary disable all these modals on Linux.
// See https://github.com/mattn/go-gtk/issues/289.
if runtime . GOOS != "linux" {
mbox_string := "Profile wasn't selected.\n\nPlease, select valid profile for this server.\nIf you haven't add profiles yet - you can do it\nin options on \"Urban Terror\" tab."
m := gtk . NewMessageDialog ( f . window , gtk . DIALOG_MODAL , gtk . MESSAGE_ERROR , gtk . BUTTONS_OK , mbox_string )
m . Response ( func ( ) {
m . Destroy ( )
} )
m . Run ( )
}
2016-10-05 02:47:13 +05:00
return errors . New ( "No game profile specified" )
}
2016-10-04 23:39:32 +05:00
fmt . Println ( "Saving favorite server..." )
2016-10-07 14:28:44 +05:00
key := strings . Split ( f . server_address . GetText ( ) , ":" ) [ 0 ] + ":" + port
2016-10-08 00:29:46 +05:00
ctx . Cache . Servers [ key ] . Server . Ip = f . server . Ip
ctx . Cache . Servers [ key ] . Server . Port = f . server . Port
ctx . Cache . Servers [ key ] . Server . Name = f . server . Name
2016-10-07 14:28:44 +05:00
ctx . Cache . Servers [ key ] . Server . Password = f . server_password . GetText ( )
ctx . Cache . Servers [ key ] . Server . ProfileToUse = f . profile . GetActiveText ( )
ctx . Cache . Servers [ key ] . Server . Favorite = "1"
2016-10-08 00:29:46 +05:00
ctx . Cache . Servers [ key ] . Server . ExtendedConfig = f . server . ExtendedConfig
ctx . Cache . Servers [ key ] . Server . PlayersInfo = f . server . PlayersInfo
2016-10-04 23:39:32 +05:00
2016-10-09 01:23:38 +05:00
ctx . Eventer . LaunchEvent ( "flushServers" , map [ string ] string { } )
2016-10-08 19:57:33 +05:00
ctx . Eventer . LaunchEvent ( "loadFavoriteServers" , map [ string ] string { } )
2016-10-05 00:04:24 +05:00
f . window . Destroy ( )
2016-10-04 23:39:32 +05:00
return nil
}
2016-10-08 00:29:46 +05:00
func ( f * FavoriteDialog ) updateServerInfo ( ) {
fmt . Println ( "Updating server information..." )
var port string = ""
if strings . Contains ( f . server_address . GetText ( ) , ":" ) {
port = strings . Split ( f . server_address . GetText ( ) , ":" ) [ 1 ]
} else {
port = "27960"
}
f . server . Ip = strings . Split ( f . server_address . GetText ( ) , ":" ) [ 0 ]
f . server . Port = port
ctx . Requester . Pooler . UpdateSpecificServer ( f . server )
f . fill ( )
}