-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·374 lines (311 loc) · 14 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# These are only needed for Python v2 but are harmless for Python v3
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
import logging
import datetime
import time
import sys
import re
from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, SLOT, QSettings, Qt, QUrl
from PyQt4 import uic
import interface.resource.res
from BuddyList import BuddyList
from im import Client
from constants import SHOW, MUC_GROUP_TITLE, PATH_UI_MAIN, PATH_UI_CONNECTION, PATH_UI_LOGS, PATH_UI_ABOUT_PYTALK, PATH_UI_JOIN_MUC, PATH_UI_ADD_BUDDY, DEFAULT_GROUP, PATH_UI_HELP, PATH_DOC_HELP
class MainWindow(QMainWindow):
def __init__(self, parent=None):
#QMainWindow.__init__(self, None, Qt.WindowStaysOnTopHint) # always on top
super(MainWindow, self).__init__(parent)
self.settings = QSettings("Dae-ekleN", "PyTalk")
# add logs widget
QWidget.__init__(self)
self.logsWidget = uic.loadUi(PATH_UI_LOGS)
self.logsWidget.etx_logs.setReadOnly(True)
# loading .ui
uic.loadUi(PATH_UI_MAIN, self)
# Set status Offline
self.cmb_status_box.setCurrentIndex(5)
self.cmb_status_box.setEnabled(False)
self.eln_status_edit.hide()
# connecting signals
self.connect(self.cmb_status_box, SIGNAL("activated(int)"), self.statusUpdate)
self.connect(self.eln_status_edit, SIGNAL("returnPressed()"), self.statusUpdate)
# Set BuddyList
self.BuddyList = BuddyList(self)
self.vboxlayout.insertWidget(0, self.BuddyList)
#self.connect(self.BuddyList, SIGNAL("rename"), self.addBuddy)
# sleekxmpp connection
self.im = None
# Account
self.connect(self.act_connection, SIGNAL("triggered()"), self.showConnectDialog)
self.connect(self.act_deconnection, SIGNAL("triggered()"), self.disconnect)
self.connect(self.act_join_group_chat, SIGNAL("triggered()"), self.showMUCDialog)
self.connect(self.act_add_buddy, SIGNAL("triggered()"), self.showBuddyDialog)
self.connect(self.act_quit, SIGNAL("triggered()"), self.quitApp)
self.act_join_group_chat.setEnabled(False)
self.act_add_buddy.setEnabled(False)
# View
self.act_away_buddies.setEnabled(False)
self.act_offline_buddies.setEnabled(False)
self.connect(self.act_away_buddies, SIGNAL("toogled()"), self.showAwayBuddies)
self.connect(self.act_offline_buddies, SIGNAL("toogled()"), self.showOfflineBuddies)
self.connect(self.act_away_buddies, SIGNAL("triggered()"), self.showAwayBuddies)
self.connect(self.act_offline_buddies, SIGNAL("triggered()"), self.showOfflineBuddies)
# Tools
self.connect(self.act_logs, SIGNAL("triggered()"), self.showLogs)
# About Dialogs
self.connect(self.act_about_pytalk, SIGNAL("triggered()"), self.aboutPyTalk)
self.act_help.triggered.connect(self.showHelp)
self.act_about_pystudy.triggered.connect(self.aboutPyStudy)
def showHelp(self):
# opens help dialog
QDialog.__init__(self)
self.helpForm = uic.loadUi(PATH_UI_HELP)
self.helpForm.show()
self.connect(self.helpForm.btn_back, SIGNAL('clicked()'),
self.helpForm.tbr_help, SLOT("backward()"))
self.connect(self.helpForm.btn_home, SIGNAL('clicked()'),
self.helpForm.tbr_help, SLOT("home()"))
self.connect(self.helpForm.tbr_help, SIGNAL("sourceChanged(QUrl)"),
self.updatePageTitle)
self.helpForm.tbr_help.setSource((QUrl.fromLocalFile(PATH_DOC_HELP))) # "../docs/help/index.html")))
def updatePageTitle(self):
# used to update label for title (helpForm.lab_title) in help dialog
self.helpForm.lab_title.setText(self.helpForm.tbr_help.documentTitle())
def aboutPyStudy(self):
# opens about dialog
QMessageBox.about(self, "About",
"""<p>The <b>PyStudy</b> is a tool for sharing your ideas and thoughts with others.
The features that distinguish it from other instant messengers are canvas and voice sessions with
further possibility of saving the data into various file formats.</p>
<p>It is an open source project; you can always find the latest
version of code at <a href="https://github.com/dae-eklen/E-learning">github</a>
page.</p>
<p>Current version: 0.9</p>
<p>Author: Casian Olga</p>
""")
def aboutPyTalk(self):
QDialog.__init__(self)
self.aboutPyTalk = uic.loadUi(PATH_UI_ABOUT_PYTALK)
self.aboutPyTalk.show()
self.aboutPyTalk.raise_()
def showBuddyDialog(self, jidFrom = None):
QDialog.__init__(self)
self.addNewBuddy = uic.loadUi(PATH_UI_ADD_BUDDY)
if jidFrom:
self.addNewBuddy.eln_jid.setText(jidFrom)
for el in self.BuddyList.groups:
self.addNewBuddy.cmb_group.addItem(el)
if DEFAULT_GROUP not in self.BuddyList.groups:
self.addNewBuddy.cmb_group.addItem(DEFAULT_GROUP)
self.addNewBuddy.show()
self.connect(self.addNewBuddy, SIGNAL("accepted()"), self.addBuddy)
def addBuddy(self):
username = str(self.addNewBuddy.eln_jid.text())
server = str(self.addNewBuddy.cmb_server.currentText())
group = str(self.addNewBuddy.cmb_group.currentText())
jid = username + "@" + server
#if not self.im.dicsoveryJid(str(jid)):
# self.information("Error", "The contact you want to add, " + jid + ", does not exist.")
if jid == self.im.jabberID:
self.information("Error", "You cannot add yourself.")
elif jid not in self.BuddyList.buddies:
self.BuddyList.newBuddy(jid, group, "offline")
self.BuddyList.presence((jid, "offline", self.im.getSubscription(jid)))
self.im.subscribeResp(True, jid, group)
else:
self.information("Contact already exists", "The contact that you want to add, " + jid + ", is already added.")
def subscribeReq(self, jidFrom):
reply = QMessageBox.question(self, "Subscription request", "New subscription request from " + jidFrom +
". Do you want to approve it and establish bidirectional subscription?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if reply == QMessageBox.Yes:
if jidFrom not in self.BuddyList.buddies:
namePattern = """([\w\-][\w\-\.]*)+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}"""
username = re.findall(namePattern, jidFrom)
self.showBuddyDialog(username[0])
else:
# if subscription req without adding to list
self.im.subscribeResp(True, jidFrom, self.im.getGroups(jidFrom))
else:
self.im.subscribeResp(False, jidFrom)
def handleUnsubscribedReq(self, jidFrom):
reply = QMessageBox.question(self, "Subscription removed", "Contact " + jidFrom +
" removed subscription from you. You will always see him offline. Do you want to remove him from your contact list?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if reply == QMessageBox.Yes:
self.BuddyList.removeBuddy(jidFrom)
self.im.unsubscribe(jidFrom)
def showMUCDialog(self):
QDialog.__init__(self)
self.joinGroupChat = uic.loadUi(PATH_UI_JOIN_MUC)
self.joinGroupChat.show()
self.connect(self.joinGroupChat, SIGNAL("accepted()"), self.joinMUC)
def joinMUC(self):
room = str(self.joinGroupChat.eln_room.text()) # dae-eklen-test2|dae-eklen-test|dae-eklen
server = str(self.joinGroupChat.cmb_server.currentText())
self.checkAndJoinMUC(room)
def inviteMUC(self, room, jidFrom):
if jidFrom:
text = "Received invitation from " + jidFrom + " to room " + room
else:
text = "Received invitation to room " + room
text += "\n\nDo you want to accept the invitation?"
reply = QMessageBox.question(self, "Groupchat invitation", text, QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.Yes:
mucTitlePattern = """([\w\-][\w\-\.\|]*)+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}"""
name = re.findall(mucTitlePattern, room)
self.checkAndJoinMUC(name[0])
else:
self.im.declineMUCInvite(room, jidFrom)
def checkAndJoinMUC(self, roomName):
jids = roomName.split("|")
match = 0
users = []
for el in range(len(jids)):
users.append(unicode(jids[el] + "@talkr.im"))
if self.im.dicsoveryJid(users[el]): # if user exists
match += 1
if len(jids) == match and match != 0:
# our type of group
if not self.BuddyList.MUCExists(users):
self.im.joinMUC(users)
else:
self.information("Join Group Chat", "Specified room is already added to '" + MUC_GROUP_TITLE + "' group.")
else:
# usual group: currently unavaiilable
self.information("Join Group Chat", "This type of group is currently N/A")
def showConnectDialog(self):
# opens connection dialog
QDialog.__init__(self)
self.connectionDialog = uic.loadUi(PATH_UI_CONNECTION)
self.connectionDialog.show()
self.connect(self.connectionDialog, SIGNAL("accepted()"), self.connection)
self.connectionDialog.eln_jid.setText(self.settings.value("jid", ""))
self.connectionDialog.eln_pass.setText(self.settings.value("password", ""))
self.connectionDialog.eln_resource.setText(self.settings.value("resource", ""))
def closeEvent(self, event):
# called on close (Ctrl+Q)
reply = QMessageBox.question(self, app.translate("wnd_main", "Exit"),
app.translate("wnd_main", "Are you sure to quit?"),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.Yes:
try:
self.disconnect()
event.accept()
except:
time.sleep(1)
else:
event.ignore()
def quitApp(self):
self.disconnect()
QApplication.instance().quit()
def disconnect(self):
if self.im:
self.im.stop()
self.im = None
self.BuddyList.clear()
self.act_connection.setEnabled(True)
self.act_deconnection.setEnabled(False)
self.eln_status_edit.hide()
self.cmb_status_box.setCurrentIndex(5)
self.cmb_status_box.setEnabled(False)
self.act_away_buddies.setEnabled(False)
self.act_offline_buddies.setEnabled(False)
self.act_join_group_chat.setEnabled(False)
self.act_add_buddy.setEnabled(False)
def failedAuth(self):
self.critical("Authentication failed", "Authentication failed, please check Username - Password combination.")
def connection(self):
# settings for jid and pass
self.settings.setValue("jid", self.connectionDialog.eln_jid.text())
self.settings.setValue("password", self.connectionDialog.eln_pass.text())
if str(self.connectionDialog.eln_resource.text()) == "":
resource = "PyStudy"
else: resource = str(self.connectionDialog.eln_resource.text())
self.settings.setValue("resource", resource)
# latest status and show
self.clientJid = str(self.connectionDialog.eln_jid.text() + "@" + self.connectionDialog.cmb_server.currentText())
self.settings.beginGroup(self.clientJid)
self.latestShow = self.settings.value("latestShow", "") # text as in SHOW
self.latestStatus = self.settings.value("latestStatus", "")
self.settings.endGroup()
# starting xmpp thread
self.im = Client(self.clientJid, resource, self.connectionDialog.eln_pass.text(),
self.latestShow, self.latestStatus)
self.connect(self.im, SIGNAL("failedAuth"), self.failedAuth)
self.im.start()
self.cmb_status_box.setItemText(5, "Please wait...")
# connecting signals
self.connect(self.im, SIGNAL("sessionStarted(PyQt_PyObject)"), self.sessionStarted)
self.connect(self.im, SIGNAL("presence(PyQt_PyObject)"), self.BuddyList.presence)
self.connect(self.im, SIGNAL("disconnect"), self.disconnect)
self.connect(self.im, SIGNAL("message"), self.BuddyList.message)
self.connect(self.im, SIGNAL("messageMUC"), self.BuddyList.messageMUC)
self.connect(self.im, SIGNAL("inviteMUC"), self.inviteMUC)
self.connect(self.im, SIGNAL("subscribeReq"), self.subscribeReq)
self.connect(self.im, SIGNAL("handleUnsubscribedReq"), self.handleUnsubscribedReq)
self.connect(self.im, SIGNAL("sendPresenceToBuddy"), self.statusUpdate)
self.connect(self.im, SIGNAL("rcvCanvasStroke"), self.BuddyList.rcvCanvasStroke)
self.connect(self.im, SIGNAL("rcvCanvasStrokeMUC"), self.BuddyList.rcvCanvasStrokeMUC)
self.connect(self.im, SIGNAL("critical"), self.critical)
self.connect(self.im, SIGNAL("information"), self.information)
self.connect(self.im, SIGNAL("debug"), self.debug)
def sessionStarted(self, roster_keys):
self.act_connection.setEnabled(False)
self.act_deconnection.setEnabled(True)
self.act_join_group_chat.setEnabled(True)
self.act_away_buddies.setEnabled(True)
self.act_offline_buddies.setEnabled(True)
self.act_add_buddy.setEnabled(True)
# construct contact list
self.BuddyList.setConnection(self.im)
#store roster in settings
self.settings.beginGroup(self.clientJid)
self.settings.setValue("roster", roster_keys)
self.settings.endGroup()
self.BuddyList.constructList(roster_keys)
self.showAwayBuddies()
self.showOfflineBuddies()
# restore show and status
self.eln_status_edit.show()
self.eln_status_edit.setText(self.latestStatus)
self.cmb_status_box.setItemText(5, "Offline")
if self.latestShow == "": self.cmb_status_box.setCurrentIndex(SHOW.index('available'))
else: self.cmb_status_box.setCurrentIndex(SHOW.index(self.latestShow))
self.cmb_status_box.setEnabled(True)
def statusUpdate(self, jidTo = None):
if SHOW[self.cmb_status_box.currentIndex()] != "offline":
# update settings
self.settings.beginGroup(self.clientJid)
self.settings.setValue("latestShow", SHOW[self.cmb_status_box.currentIndex()])
self.settings.setValue("latestStatus", self.eln_status_edit.text())
self.settings.endGroup()
self.debug("new presence set. show: '" + SHOW[self.cmb_status_box.currentIndex()] +
"'; status: '" + self.eln_status_edit.text() + "'\n\n")
self.im.changeStatus(self.cmb_status_box.currentIndex(), self.eln_status_edit.text(), jidTo)
def showLogs(self):
self.logsWidget.show()
self.logsWidget.raise_()
def debug(self, message):
self.logsWidget.etx_logs.append(datetime.datetime.now().strftime("[%H:%M:%S]")+":\n" + message)
def showAwayBuddies(self):
self.BuddyList.showAwayBuddies(not self.act_away_buddies.isChecked())
def showOfflineBuddies(self):
self.BuddyList.showOfflineBuddies(not self.act_offline_buddies.isChecked())
def critical(self, title, content):
QMessageBox.critical(self, title, content, QMessageBox.Ok)
def information(self, title, content):
QMessageBox.information(self, title, content, QMessageBox.Ok)
if __name__ == "__main__":
# Setup logging
logging.basicConfig(level=logging.DEBUG, format='%(levelname)-10s %(message)s')
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())