OSDN Git Service

接收消息并提示,以及若干细节优化
[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 gettext
14 import locale
15 if locale.getdefaultlocale()[0] == 'zh_CN':
16     gettext.install('messages', './locale', unicode=False)
17     gettext.translation('messages', './locale', languages=['cn']).install(True)
18 class prpcrypt():
19     def __init__(self, key):
20         self.key = key
21         self.mode = AES.MODE_CBC    
22     def decrypt(self, text):
23         cryptor = AES.new(self.key, self.mode, self.key)
24         plain_text = cryptor.decrypt(a2b_hex(text))
25         return plain_text.rstrip('\0')
26 class LoginFrame(wx.Frame):
27     def __init__(self, parent, id, title, size):
28         wx.Frame.__init__(self, parent, id, title)
29         self.SetSize(size)
30         self.Center()
31         self.passWordLabel = wx.StaticText(self, label = _("UserName"), pos = (30, 50), size = (120, 25))
32         self.userNameLabel = wx.StaticText(self, label = _("Password"), pos = (30, 100), size = (120, 25))
33         self.userName = wx.TextCtrl(self, pos = (100, 47), size = (150, 25))
34         self.passWord= wx.TextCtrl(self, pos = (100, 97), size = (150, 25),style=wx.TE_PASSWORD)
35         self.loginButton = wx.Button(self, label = _('Login'), pos = (80, 145), size = (130, 30))
36         self.loginButton.Bind(wx.EVT_BUTTON,self.login)
37         self.Show()
38     def login(self,evt):
39             if not self.userName.GetValue():
40                    wx.MessageBox(_('Please enter the username'), _('Error'), 
41                    wx.OK | wx.ICON_ERROR)
42             elif not self.passWord.GetValue():
43                     wx.MessageBox(_('Please enter the password'), _('Error'), 
44                     wx.OK | wx.ICON_ERROR) 
45             else:                 
46                  try:
47                      db=MySQLdb.connect(host="sql6.freesqldatabase.com",user="sql685198",passwd="jH8*bX3*",db="sql685198",port=3306 )
48                      cursor = db.cursor()
49                      sql = "SELECT uncompress(password) FROM user WHERE name = '%s' LIMIT 1"%(self.userName.GetValue())
50                      cursor.execute(sql)
51                      results = cursor.fetchall()
52                      if results:
53                          for row in results:
54                                  password = row[0]
55                      else:
56                          wx.MessageBox(_('Unable to fecth data,Please check your username'), _('Try it again'), 
57                          wx.OK | wx.ICON_ERROR)  
58                  except MySQLdb.Error, e:
59                        wx.MessageBox('Error %d: %s' % (e.args[0], e.args[1]), _('Try it again'), 
60                        wx.OK | wx.ICON_ERROR)  
61                  passwd0 = pc.decrypt(password)
62                  if self.passWord.GetValue()==passwd0:
63                     try:
64                         cursor.execute("SELECT uncompress(Data) FROM friendlist WHERE name = '%s' LIMIT 1"%(self.userName.GetValue()))
65                         data = json.loads(cursor.fetchone()[0])
66                         cursor.close()
67                         #conn.close()
68                         db.close()
69                     except IOError, e:
70                           wx.MessageBox('Error %d: %s' % (e.args[0], e.args[1]), 'Try it again', 
71                           wx.OK | wx.ICON_ERROR)
72                     #urllib2.urlopen('http://chat-tyl.coding.io/user_log?info=User___'+self.userName.GetValue()+'___Login')
73                     Notify.init ("Chat-TYL")
74                     bubble_notify = Notify.Notification.new (_("Information"),_("Login Successful"),"file://" + os.path.abspath(os.path.curdir) + "/Chat-TYL.ico")
75                     bubble_notify.show ()  
76                     self.Hide()
77                     frame = FriendList.MyFrame(None, id=-1, title=_("Friend List"),user=data,un=self.userName.GetValue())
78                     frame.Show(True)
79                  else:
80                      wx.MessageBox(_('Your Password is wrong'), _('Try it again'), 
81                      wx.OK | wx.ICON_ERROR)         
82 if __name__ == '__main__':
83     pc = prpcrypt('keyskeyskeyskeys')
84     app = wx.App()
85     LoginFrame(None, -1, title = _("Login"), size = (280, 200))
86     app.MainLoop()