-
Notifications
You must be signed in to change notification settings - Fork 9
/
Qt4_ChatBrick.py
402 lines (359 loc) · 14.4 KB
/
Qt4_ChatBrick.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#
# Project: MXCuBE
# https://github.com/mxcube.
#
# This file is part of MXCuBE software.
#
# MXCuBE 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 3 of the License, or
# (at your option) any later version.
#
# MXCuBE 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 MXCuBE. If not, see <http://www.gnu.org/licenses/>.
import os
import time
import InstanceServer
from QtImport import *
from BlissFramework import Qt4_Icons
from BlissFramework.Qt4_BaseComponents import BlissWidget
__category__ = "General"
class Qt4_ChatBrick(BlissWidget):
"""
Descript. :
"""
PRIORITY_COLORS = ('darkblue', 'black', 'red')
MY_COLOR = 'darkgrey'
incoming_unread_messages = pyqtSignal(int,bool)
reset_unread_messages = pyqtSignal(bool)
def __init__(self, *args):
"""
Descript. :
Args. :
Return :
"""
BlissWidget.__init__(self, *args)
# Properties ----------------------------------------------------------
self.addProperty('mnemonic', 'string', '')
self.addProperty('icons', 'string', '')
self.addProperty('myTabLabel', 'string', '')
# Signals ------------------------------------------------------------
self.defineSignal('incoming_unread_messages',())
self.defineSignal('reset_unread_message',())
# Slots ---------------------------------------------------------------
self.defineSlot('tabSelected',())
self.defineSlot('sessionSelected',())
# Hardware objects ----------------------------------------------------
self.instance_server_hwobj = None
# Internal values -----------------------------------------------------
self.session_id = None
self.nickname = ""
self.role = BlissWidget.INSTANCE_ROLE_UNKNOWN
# Graphic elements ----------------------------------------------------
self.conversation_textedit = QTextEdit(self)
self.conversation_textedit.setReadOnly(True)
_controls_widget = QWidget(self)
_say_label = QLabel("Say:", _controls_widget)
self.message_ledit = QLineEdit(_controls_widget)
self.send_button = QPushButton("Send", _controls_widget)
self.send_button.setEnabled(False)
# Layout --------------------------------------------------------------
_controls_widget_hlayout = QHBoxLayout(_controls_widget)
_controls_widget_hlayout.addWidget(_say_label)
_controls_widget_hlayout.addWidget(self.message_ledit)
_controls_widget_hlayout.addWidget(self.send_button)
_controls_widget_hlayout.setSpacing(2)
_controls_widget_hlayout.setContentsMargins(0, 0, 0, 0)
_main_vlayout = QVBoxLayout(self)
_main_vlayout.addWidget(self.conversation_textedit)
_main_vlayout.addWidget(_controls_widget)
_main_vlayout.setSpacing(2)
_main_vlayout.setContentsMargins(2, 2, 2, 2)
# Qt signal/slot connections ------------------------------------------
self.send_button.clicked.connect(self.send_current_message)
self.message_ledit.returnPressed.connect(self.send_current_message)
self.message_ledit.textChanged.connect(self.message_changed)
#self.setFixedHeight(120)
#self.setFixedWidth(790)
def run(self):
"""
Descript. :
Args. :
Return :
"""
self.set_role(self.role)
def session_selected(self, *args):
"""
Descript. :
Args. :
Return :
"""
session_id = args[0]
is_inhouse = args[-1]
self.conversation_textedit.clear()
if is_inhouse:
self.session_id = None
else:
self.session_id = session_id
self.load_chat_history()
def load_chat_history(self):
"""
Descript. :
Args. :
Return :
"""
if self.instance_server_hwobj is not None:
chat_history_filename = "/tmp/mxCuBE_chat_%s.%s" % (self.session_id, self.instance_server_hwobj.isClient() and "client" or "server")
else:
return
try:
chat_history = open(chat_history_filename, "r")
except:
return
if self.isEnabled():
for msg in chat_history.readlines():
self.conversation_textedit.append(msg)
def instance_role_changed(self,role):
"""
Descript. :
Args. :
Return :
"""
self.set_role(role)
def set_role(self, role):
"""
Descript. :
Args. :
Return :
"""
self.role = role
if role != BlissWidget.INSTANCE_ROLE_UNKNOWN and not self.isEnabled():
self.setEnabled(True)
self.load_chat_history()
def message_changed(self,text):
"""
Descript. :
Args. :
Return :
"""
self.send_button.setEnabled(len(str(text))>0)
def message_arrived(self,priority,user_id,message):
"""
Descript. :
Args. :
Return :
"""
color=Qt4_ChatBrick.PRIORITY_COLORS[priority]
msg_prefix=""
msg_suffix=""
if priority==InstanceServer.ChatInstanceMessage.PRIORITY_NORMAL:
if user_id is None:
header=""
else:
header=" %s:" % self.instance_server_hwobj.idPrettyPrint(user_id)
if user_id[0]==self.nickname:
color=Qt4_ChatBrick.MY_COLOR
else:
header=""
msg_prefix="<i>"
msg_suffix="</i>"
now = time.strftime("%T")
new_line = "<font color=%s><b>(%s)%s</b> %s%s%s</font>" % (color, now,header,msg_prefix,message,msg_suffix)
self.conversation_textedit.append(new_line)
if self.session_id is not None and self.instance_server_hwobj is not None:
chat_history_filename = "/tmp/mxCuBE_chat_%s.%s" % (self.session_id, self.instance_server_hwobj.isClient() and "client" or "server")
try:
if time.time() - os.stat(chat_history_filename).st_mtime > 24*3600:
os.unlink(chat_history_filename)
except OSError:
pass
chat_history_file = open(chat_history_filename, "a")
chat_history_file.write(new_line)
chat_history_file.write("\n")
chat_history_file.close()
#self.emit(QtCore.SIGNAL("incUnreadMessages"),1, True)
self.incoming_unread_messages.emit(1,True)
def new_client(self,client_id):
"""
Descript. :
Args. :
Return :
"""
msg="%s has joined the conversation." % self.instance_server_hwobj.idPrettyPrint(client_id)
self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_LOW,None,msg)
def wants_control(self,client_id):
"""
Descript. :
Args. :
Return :
"""
msg="%s wants to have control!" % self.instance_server_hwobj.idPrettyPrint(client_id)
self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_HIGH,None,msg)
def server_initialized(self,started,server_id=None):
"""
Descript. :
Args. :
Return :
"""
if started:
#sg="I'm moderating the chat as %s." % server_id[0]
#self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_LOW,None,msg)
self.nickname=server_id[0]
def client_closed(self,client_id):
"""
Descript. :
Args. :
Return :
"""
msg="%s has left the conversation..." % self.instance_server_hwobj.idPrettyPrint(client_id)
self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_LOW,None,msg)
def client_initialized(self,connected,server_id=None,my_nickname=None,quiet=False):
"""
Descript. :
Args. :
Return :
"""
if connected:
server_print=self.instance_server_hwobj.idPrettyPrint(server_id)
msg="I've joined the conversation as %s (moderator is %s)." % (my_nickname,server_print)
self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_LOW,None,msg)
self.nickname=my_nickname
def client_changed(self,old_client_id,new_client_id):
"""
Descript. :
Args. :
Return :
"""
#print "CHAT CLIENT CHANGED",old_client_id,new_client_id
if old_client_id[0]==self.nickname:
self.nickname=new_client_id[0]
else:
old_client_print=self.instance_server_hwobj.idPrettyPrint(old_client_id)
new_client_print=self.instance_server_hwobj.idPrettyPrint(new_client_id)
msg="%s has changed to %s." % (old_client_print,new_client_print)
self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_LOW,None,msg)
def send_current_message(self):
"""
Descript. :
Args. :
Return :
"""
txt=str(self.message_ledit.text())
if len(txt):
self.instance_server_hwobj.sendChatMessage(InstanceServer.ChatInstanceMessage.PRIORITY_NORMAL,txt)
self.message_ledit.setText("")
def propertyChanged(self, property_name, old_value, new_value):
"""
Descript. :
Args. :
Return :
"""
if property_name == 'mnemonic':
if self.instance_server_hwobj is not None:
self.disconnect(self.instance_server_hwobj,
'chatMessageReceived',
self.message_arrived)
self.disconnect(self.instance_server_hwobj,
'newClient',
self.new_client)
self.disconnect(self.instance_server_hwobj,
'serverInitialized',
self.server_initialized)
self.disconnect(self.instance_server_hwobj,
'clientInitialized',
self.client_initialized)
self.disconnect(self.instance_server_hwobj,
'serverClosed',
self.client_closed)
self.disconnect(self.instance_server_hwobj,
'wantsControl',
self.wants_control)
self.disconnect(self.instance_server_hwobj,
'haveControl',
self.have_control)
self.disconnect(self.instance_server_hwobj,
'passControl',
self.pass_control)
self.disconnect(self.instance_server_hwobj,
'clientClosed',
self.client_closed)
self.disconnect(self.instance_server_hwobj,
'clientChanged',
self.client_changed)
self.instance_server_hwobj = self.getHardwareObject(new_value)
if self.instance_server_hwobj is not None:
self.connect(self.instance_server_hwobj,
'chatMessageReceived',
self.message_arrived)
self.connect(self.instance_server_hwobj,
'newClient',
self.new_client)
self.connect(self.instance_server_hwobj,
'serverInitialized',
self.server_initialized)
self.connect(self.instance_server_hwobj,
'clientInitialized',
self.client_initialized)
self.connect(self.instance_server_hwobj,
'serverClosed',
self.client_closed)
self.connect(self.instance_server_hwobj,
'wantsControl',
self.wants_control)
self.connect(self.instance_server_hwobj,
'haveControl',
self.have_control)
self.connect(self.instance_server_hwobj,
'passControl',
self.pass_control)
self.connect(self.instance_server_hwobj,
'clientClosed',
self.client_closed)
self.connect(self.instance_server_hwobj,
'clientChanged',
self.client_changed)
elif property_name == 'icons':
icons_list = new_value.split()
try:
self.send_button.setIcon(Qt4_Icons.load_icon(icons_list[0]))
except IndexError:
pass
else:
BlissWidget.propertyChanged(self,property_name, old_value, new_value)
def have_control(self,have_control,gui_only=False):
"""
Descript. :
Args. :
Return :
"""
if not gui_only:
if have_control:
p=InstanceServer.ChatInstanceMessage.PRIORITY_HIGH
msg="I've gained control!"
else:
p=InstanceServer.ChatInstanceMessage.PRIORITY_HIGH
msg="I've lost control..."
self.message_arrived(p,None,msg)
def pass_control(self,has_control_id):
"""
Descript. :
Args. :
Return :
"""
has_control_print=self.instance_server_hwobj.idPrettyPrint(has_control_id)
msg="%s has control." % has_control_print
self.message_arrived(InstanceServer.ChatInstanceMessage.PRIORITY_LOW,None,msg)
def tabSelected(self,tab_name):
"""
Descript. :
Args. :
Return :
"""
if tab_name==self['myTabLabel']:
#self.emit(QtCore.SIGNAL("resetUnreadMessages"), True)
self.reset_unread_messages.emit(True)