-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
290 lines (229 loc) · 8.04 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import asyncio
from datetime import datetime
from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager
from kivy.clock import Clock
from kivy.core.clipboard import Clipboard
import os
import json
PORT = 5920
Builder.load_string("""
#:import C kivy.utils.get_color_from_hex
#:import RiseInTransition kivy.uix.screenmanager.RiseInTransition
<BoxLayout>:
padding: 10
spacing: 10
<GridLayout>:
rows: 2
cols: 2
spacing: 10
row_default_height: 90
row_force_default: True
<Label>:
font_size: 25
font_name:'data/droid.ttf'
<Button>:
font_size: 30
height: 90
size_hint: (1, None)
background_normal: 'data/button_normal.png'
background_down: 'data/button_down.png'
border: (2, 2, 2, 2)
<TextInput>:
font_size: 30
multiline: False
padding: [10, 0.5 * (self.height - self.line_height)]
font_name:'data/droid.ttf'
<ScrollView>:
canvas.before:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
<ChatLabel@Label>:
color: C('#101010')
text_size: (self.width, None)
halign: 'left'
valign: 'top'
padding: (0, 0) # fixed in Kivy 1.8.1
size_hint: (1, None)
height: self.texture_size[1]
markup: True
<RootWidget>:
transition: RiseInTransition()
Screen:
name: 'login'
BoxLayout:
orientation: 'vertical'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "data/background.jpg"
FloatLayout:
canvas:
Ellipse:
id: user_picture
pos: root.width/2 - 150/2, self.height/2 - 150/2 + 150*1.5
size: 150, 150
source: 'data/gcomm-logo.png'
angle_start: 0
angle_end: 360
TextInput:
id: server
hint_text: "Server IP"
size_hint: (3.9/6.8, 1/12)
pos_hint: {'center_x': 0.5, 'y': 0.47}
background_normal: 'data/input_line.png'
background_active: 'data/white.png'
text: app.host
TextInput:
id: nickname
hint_text: "Nickname"
size_hint: (3.9/6.8, 1/12)
pos_hint: {'center_x': 0.5, 'y': 0.37}
background_normal: 'data/input_line.png'
background_active: 'data/white.png'
text: app.nick
Button:
text: 'Connect'
on_press: app.connect()
size_hint: (4/6.8, 1/12)
pos_hint: {'center_x': 0.5, 'y': 0.22}
Label:
text: "Powered by The Elite"
pos: root.width/2 - 600/2, self.height/2 - 150/2 - 450*1.5
Screen:
name: 'chatroom'
BoxLayout:
orientation: 'vertical'
ScrollView:
ListView:
ChatLabel:
id: chat_logs
text: ''
BoxLayout:
height: 90
orientation: 'horizontal'
padding: 0
size_hint: (1, None)
TextInput:
id: message
hint_text: "Type a message"
on_text_validate: app.send_msg()
Button:
background_normal: "data/send-button.png"
background_down: "data/send-button.png"
on_press: app.send_msg()
size_hint: (0.2, 1)
""")
def esc_markup(msg):
return (msg.replace('&', '&')
.replace('[', '&bl;')
.replace(']', '&br;'))
class ClientProtocol(asyncio.Protocol):
def __init__(self, app, loop):
self.app = app
self.loop = loop
def connection_made(self, transport):
self.transport = transport
def data_received(self, data):
if data:
text = data.decode('utf-8', 'ignore')
if text == "*1*":
self.app.last_connection_time = datetime.now()
return
print(text)
nickname = ''
msg = ''
for num, i in enumerate(text.split(':'), start=0):
if num == 0:
nickname = i
else:
msg += i
self.app.root.ids.chat_logs.text += (
'[b][color=2980b9]{}:[/color][/b] {}\n'.format(nickname, esc_markup(msg))
)
Clipboard.copy(msg)
def connection_lost(self, exc):
print('The server closed the connection')
self.transport.close()
class RootWidget(ScreenManager):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
class ChatApp(App):
def build(self):
self.base_folder =os.path.dirname(os.path.abspath('.'))
self.setting_file = os.path.join(self.base_folder, 'chat_setting.json')
self.read_config()
return RootWidget()
def read_config(self):
try:
with open(self.setting_file, 'r') as f:
text = f.read()
self.setting_dict = json.loads(text)
self.host = self.setting_dict['host']
self.nick = self.setting_dict['nick']
except:
self.host = "127.0.0.1"
self.nick = "kivy"
def save_config(self):
self.setting_dict = {'host': self.host, 'nick': self.nick}
with open(self.setting_file, 'w') as f:
f.write(json.dumps(self.setting_dict))
def connect(self):
self.host = self.root.ids.server.text
self.nick = self.root.ids.nickname.text
self.is_stop = False
self.loop = asyncio.get_event_loop()
if self.reconnect():
self.clock_receive = Clock.schedule_interval(self.receive_msg, 1)
self.clock_detect = Clock.schedule_interval(self.detect_if_offline, 3)
self.root.current = 'chatroom'
self.save_config()
print('-- connecting to ' + self.host)
def reconnect(self):
try:
self.coro = self.loop.create_connection(lambda: ClientProtocol(self, self.loop),
self.host, PORT)
self.transport, self.protocol = self.loop.run_until_complete(self.coro)
self.last_connection_time = datetime.now()
print("I just reconnected the server.")
return True
except Exception as e:
#print(e)
self.root.current = 'login'
try:
self.clock_receive.cancel()
self.clock_detect.cancel()
except:
print("No server available.")
return False
def detect_if_offline(self, dt): #run every 3 seconds
if (datetime.now() - self.last_connection_time).total_seconds() > 45:
self.transport.close()
self.reconnect()
def send_msg(self):
if self.transport.is_closing():
self.transport.close()
self.reconnect()
msg = self.root.ids.message.text
self.transport.write('{0}:{1}'.format(self.nick, msg).encode('utf-8', 'ignore'))
self.root.ids.chat_logs.text += (
'[b][color=2980b9]{}:[/color][/b] {}\n'
.format(self.nick, esc_markup(msg)))
self.root.ids.message.text = ''
def receive_msg(self, dt):
self.loop.run_until_complete(self.coro)
def on_stop(self):
exit()
if __name__ == '__main__':
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '800')
Config.set('graphics', 'resizable', '1')
Config.set('graphics', 'borderless', '0')
Config.set('graphics', 'window_state', 'visible')
ChatApp().run()