OSDN Git Service

修复多线程BUG
[chatyl/chatyl.git] / LoginGui.py
1 #!/usr/bin/python
2 # encoding: utf-8
3 import os
4 import wx
5 import sys
6 import ssl
7 import FriendList
8 from Crypto.Cipher import AES
9 from binascii import b2a_hex, a2b_hex
10 import MySQLdb
11 import simplejson as json
12 from gi.repository import Notify
13 import thread
14 import gettext
15 import time
16 import locale
17 if locale.getdefaultlocale()[0] == 'zh_CN':
18     gettext.install('messages', './locale', unicode=False)
19     gettext.translation('messages', './locale', languages=['cn']).install(True)
20 class prpcrypt():
21     def __init__(self, key):
22         self.key = key
23         self.mode = AES.MODE_CBC    
24     def decrypt(self, text):
25         cryptor = AES.new(self.key, self.mode, self.key)
26         plain_text = cryptor.decrypt(a2b_hex(text))
27         return plain_text.rstrip('\0')
28 class LoginFrame(wx.Frame):
29     def __init__(self, parent, id, title, size):
30         wx.Frame.__init__(self, parent, id, title)
31         self.SetSize(size)
32         self.Center()
33         self.passWordLabel = wx.StaticText(self, label = _("UserName"), pos = (30, 50), size = (120, 25))
34         self.userNameLabel = wx.StaticText(self, label = _("Password"), pos = (30, 100), size = (120, 25))
35         self.userName = wx.TextCtrl(self, pos = (100, 47), size = (150, 25))
36         self.passWord= wx.TextCtrl(self, pos = (100, 97), size = (150, 25),style=wx.TE_PASSWORD)
37         self.loginButton = wx.Button(self, label = _('Login'), pos = (80, 145), size = (130, 30))
38         self.loginButton.Bind(wx.EVT_BUTTON,self.login_thread)
39         self.Show()
40     def login_thread(self,event):
41             thread.start_new_thread(self.login, ())
42             self.loginButton.Disable()
43     def login(self):
44             if not self.userName.GetValue():
45                    wx.CallAfter(wx.MessageBox,_('Please enter the username'), _('Error'), 
46                    wx.OK | wx.ICON_ERROR)
47                    wx.CallAfter(self.loginButton.Enable)
48             elif not self.passWord.GetValue():
49                     wx.CallAfter(wx.MessageBox,_('Please enter the password'), _('Error'), 
50                     wx.OK | wx.ICON_ERROR)     
51                     wx.CallAfter(self.loginButton.Enable)      
52             else:
53                  try:
54                      db=MySQLdb.connect(host="sql6.freesqldatabase.com",user="sql685198",passwd="jH8*bX3*",db="sql685198",port=3306 )
55                      cursor = db.cursor()
56                      sql = "SELECT uncompress(password) FROM user WHERE name = '%s' LIMIT 1"%(self.userName.GetValue())
57                      cursor.execute(sql)
58                      results = cursor.fetchall()
59                      if results:
60                          for row in results:
61                                  password = row[0]
62                      else:
63                          wx.CallAfter(wx.MessageBox,_('Unable to fecth data,Please check your username'),_('Try it again'), 
64                          wx.OK | wx.ICON_ERROR)
65                          wx.CallAfter(self.loginButton.Enable)                   
66                  except MySQLdb.Error, e:
67                        wx.CallAfter(wx.MessageBox,'Error %d: %s' % (e.args[0], e.args[1]),_('Try it again'), 
68                        wx.OK | wx.ICON_ERROR)
69                        wx.CallAfter(self.loginButton.Enable)                                   
70                  passwd0 = pc.decrypt(password)
71                  if self.passWord.GetValue()==passwd0:
72                     try:
73                         cursor.execute("SELECT uncompress(Data) FROM friendlist WHERE name = '%s' LIMIT 1"%(self.userName.GetValue()))
74                         data = json.loads(cursor.fetchone()[0])
75                         cursor.close()
76                         #conn.close()
77                         db.close()
78                     except IOError, e:      
79                           wx.CallAfter(wx.MessageBox,'Error %d: %s' % (e.args[0], e.args[1]),_('Try it again'), 
80                           wx.OK | wx.ICON_ERROR) 
81                           wx.CallAfter(self.loginButton.Enable)     
82                     #urllib2.urlopen('http://chat-tyl.coding.io/user_log?info=User___'+self.userName.GetValue()+'___Login')
83                     wx.CallAfter(Notify.init,"Chat-TYL")
84                     wx.CallAfter(Notify.init ,"Chat-TYL") 
85                     bubble_notify = Notify.Notification.new (_("Information"),_("Login Successful"),"file://" + os.path.abspath(os.path.curdir) + "/Chat-TYL.ico")
86                     wx.CallAfter(bubble_notify.show) 
87                     wx.CallAfter(self.Hide)
88                     time.sleep(0.1)
89                     frame = FriendList.MyFrame(None, id=-1, title=_("Friend List"),user=data,un=self.userName.GetValue())
90                     wx.CallAfter(frame.Show,True)
91                  else:
92                      wx.CallAfter(wx.MessageBox,_('Your Password is wrong'), _('Try it again'), 
93                      wx.OK | wx.ICON_ERROR)
94 if __name__ == '__main__':
95     pc = prpcrypt('keyskeyskeyskeys')
96     app = wx.App()
97     LoginFrame(None, -1, title = _("Login"), size = (280, 180))
98     app.MainLoop()