-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaddserverdialog.py
167 lines (129 loc) · 5.64 KB
/
addserverdialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# addserverdialog - dialog for adding servers!
# Copyright (c) 2012 Gavin Langdon <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#------------------------------------------------------------------------------
from gi.repository import Gtk
import server, statusmanager
DEFAULT_PORT = 27015
class AddServerDialog(object):
'''Manages all the functions relating to the dialog for adding servers'''
def __init__(self, manager, builder, dic):
self.ip_entry = builder.get_object("ip_entry")
self.port_entry = builder.get_object("port_entry")
self.query_spinner = builder.get_object("query_spinner")
self.query_infobox = builder.get_object("query_infobox")
self.query_label = builder.get_object("query_label")
self.window = builder.get_object("add_server_dialog")
self.main_window = builder.get_object("main_window")
self.confirm_add_server = builder.get_object("confirm_add_server")
self.manager = manager
d = { 'on_confirm_add_server_clicked' : self.on_confirm_add_server_clicked,
'on_add_server_dialog_delete_event' : self.on_add_server_dialog_delete_event,
'on_cancel_add_server_clicked' : self.on_cancel_add_server_clicked,
'on_act_add_server_activate' : self.on_act_add_server_activate,
'on_ip_entry_changed' : self.on_ip_entry_changed,
}
dic.update(d)
def on_confirm_add_server_clicked(self, confirm_add_server):
# Verify ip and port are valid and filled out
ip = self.ip_entry.get_text().strip()
err = ""
if not ip:
err = "Please enter a valid IP."
# If port entry is empty assume default port
if not self.port_entry.get_text().strip():
self.port_entry.set_text(str(DEFAULT_PORT))
# Attempt to convert the text into an integer
try:
port = int(self.port_entry.get_text().strip())
except:
if err:
err += "\n"
err += "Please enter a valid port."
if err:
self.send_message(err, "error")
return
self.add_server(ip, port)
def send_message(self, text="", msg_type="info"):
'''Handles the notification box, showing and hiding the spinner and setting the notifcation type.
Leaving 'text' blank will hide the message'''
self.query_infobox.set_visible(text != "")
if not text:
return
types = {"load" : Gtk.MessageType.INFO,
"info" : Gtk.MessageType.INFO,
"error" : Gtk.MessageType.ERROR,
"warning" : Gtk.MessageType.WARNING}
# Default type is info
if msg_type not in types:
msg_type = "info"
self.query_infobox.set_message_type(types[msg_type])
self.query_label.set_text(text)
self.query_spinner.set_visible(msg_type=="load")
def add_server(self, ip, port):
'''Create a server and query to check if the server can be reached'''
def post_query(ret_tuple, server):
success, errval = ret_tuple
if not success:
self.send_message(str(errval), "error")
self.confirm_add_server.set_sensitive(True)
self.manager.delete_server(server)
return
statusmanager.push_status("Added server "+self.manager.get_server_sid(server)+".")
self.manager.add_server_item(server)
self.close_server_dialog(self.window)
# Begin querying the server on a timer
self.manager.init_query_timer(server)
gs = server.Gameserver(ip, port)
d = self.manager.add_server(gs)
if d:
d.addCallback(post_query, gs)
self.send_message("Querying server...", "load")
self.confirm_add_server.set_sensitive(False)
else:
self.send_message("Server is already in the list.", "warning")
def close_server_dialog(self, server_dialog):
self.main_window.set_sensitive(True)
server_dialog.hide()
def on_act_add_server_activate(self, act_add_server):
'''Called when the "add server" action is activated, which triggers opening the dialog'''
self.main_window.set_sensitive(False)
self.ip_entry.set_text("")
self.ip_entry.grab_focus()
self.port_entry.set_text("27015")
self.send_message("Please enter the new server's IP and port.", "info")
self.confirm_add_server.set_sensitive(True)
self.window.show()
def on_cancel_add_server_clicked(self, cancel_add_server):
self.close_server_dialog(self.window)
def on_add_server_dialog_delete_event(self, add_server_dialog, event):
'''Called when x button pressed in the window, return True to not delete the window'''
self.close_server_dialog(add_server_dialog)
return True
def on_ip_entry_changed(self, ip_entry):
'''Ensures if the user tries to type a port or paste a server:port pair the port gets
redirected to the port field'''
text = ip_entry.get_text()
if ":" not in text:
return
port_index = text.index(":")
port_str = text[port_index+1:]
ip_entry.set_text(text[:port_index])
if port_str:
self.port_entry.set_text(port_str)
self.port_entry.grab_focus()