OSDN Git Service

用POST替换GET
[chatyl/chatyl.git] / FriendList.py
1 #!/usr/bin/python
2 #coding=utf-8
3 import os
4 import wx
5 import sys
6 import time
7 import Talk
8 import pymongo
9 from pymongo_pubsub import Publisher
10 from pymongo_pubsub import Subscriber
11 import thread
12 import simplejson as json
13 import wx.lib.agw.toasterbox as TB
14 from wx.lib.pubsub import setupkwargs
15 from wx.lib.pubsub import pub
16 def default_cb(evt):
17     Talk.myapp(None,id=-1,title=_("With ") + text_json['send'] + _(" Talking"),user_name=text_json['send'],un=text_json['user'],addcon=text_json['content'])
18 class MyFrame(wx.Frame):
19     def OnClickLeftKey(self, event,un):
20         Talk.myapp(None,id=-1,title=_("With ") + self.tree.GetItemText(event.GetItem()) + _(" Talking"),user_name=self.tree.GetItemText(event.GetItem()),un=un,addcon="")
21     def OnClose(self, event,un):
22         dlg = wx.MessageDialog(self, 
23             _("Do you really want to close this application?"),
24             _("Confirm Exit"), wx.OK|wx.CANCEL|wx.ICON_QUESTION)
25         result = dlg.ShowModal()
26         dlg.Destroy()
27         if result == wx.ID_OK:
28             connection = pymongo.MongoClient('mongodb://tyl:22842218@ds051738.mongolab.com:51738/tylchat?authMechanism=SCRAM-SHA-1').get_default_database()
29             connection.drop_collection(un)
30             sys.exit()
31     def __init__(self, parent, id, title,user,un):
32         wx.Frame.__init__(self, parent, id, title,
33                           wx.DefaultPosition, wx.Size(210, 450))
34         hbox = wx.BoxSizer(wx.HORIZONTAL)
35         vbox = wx.BoxSizer(wx.VERTICAL)
36         panel1 = wx.Panel(self, -1)
37         self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1, -1),
38                                 wx.TR_HIDE_ROOT|wx.TR_DEFAULT_STYLE)
39         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, lambda evt,un=un : self.OnClickLeftKey(evt,un), self.tree)
40         root = self.tree.AddRoot('My friend')
41         for i in user['item']:
42                 ch=self.tree.AppendItem(root, i)
43                 for j in user[i]:
44                         self.tree.AppendItem(ch, j)
45         vbox.Add(self.tree, 1, wx.EXPAND)
46         hbox.Add(panel1, 1, wx.EXPAND)
47         panel1.SetSizer(vbox)
48         self.SetSizer(hbox)
49         self.Center()
50         self.Bind(wx.EVT_CLOSE, lambda evt,un=un : self.OnClose(evt,un))
51         global un_g
52         un_g = un
53         toaster = TB.ToasterBox(self, tbstyle=TB.TB_COMPLEX)
54         toaster.SetPopupPauseTime(3000)
55         tbpanel = toaster.GetToasterBoxWindow()
56         panel = wx.Panel(tbpanel, -1)
57         sizer = wx.BoxSizer(wx.VERTICAL)
58         text = wx.StaticText(panel, wx.ID_ANY, label="Welcome \n My friend " + un + "\n Nice to meet you")
59         sizer.Add(text, 0, wx.EXPAND)
60         panel.SetSizer(sizer)
61         toaster.AddPanel(panel)
62         wx.CallLater(100, toaster.Play)
63         thread.start_new_thread(self.receive, ())
64     def putinfo(self,data):
65         global text_json
66         text_json= json.loads(data['message'])
67         toaster = TB.ToasterBox(self, tbstyle=TB.TB_COMPLEX)
68         wx.CallAfter(toaster.SetPopupPauseTime,3000)
69         tbpanel = toaster.GetToasterBoxWindow()
70         panel = wx.Panel(tbpanel,-1)
71         sizer = wx.BoxSizer(wx.VERTICAL)
72         text = wx.StaticText(panel, wx.ID_ANY, label=text_json['send'] + " say: \n " + text_json['content'])
73         wx.CallAfter(sizer.Add,text, 0, wx.EXPAND)
74         button = wx.Button(panel, wx.ID_ANY, "Click to view")
75         wx.CallAfter(sizer.Add,button, 0, wx.EXPAND)
76         wx.CallAfter(button.Bind,wx.EVT_BUTTON,default_cb)
77         wx.CallAfter(panel.SetSizer,sizer)
78         wx.CallAfter(toaster.AddPanel,panel)
79         time.sleep (1)
80         wx.CallAfter(toaster.Play)
81         time.sleep (1)
82     def put_info(self,data):
83         wx.CallAfter(self.putinfo,data=data)
84     def receive(self):
85         connection = pymongo.MongoClient('mongodb://tyl:22842218@ds051738.mongolab.com:51738/tylchat?authMechanism=SCRAM-SHA-1').get_default_database()
86         if un_g not in connection.collection_names():
87             connection.create_collection(un_g,
88                                                     capped=True,
89                                                     size=1000000,
90                                                     max=None) 
91         i = 1
92         while (i == 1):
93             subscriber = Subscriber(connection, un_g,callback=self.put_info ,
94                         matching={'send': 'info-chat'})
95             subscriber.listen()