9 from pymongo_pubsub import Publisher
10 from pymongo_pubsub import Subscriber
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
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()
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)
31 def AddFriend(self,event):
33 Based on the wxPython demo by the same name
35 dlg = wx.TextEntryDialog(
36 self, 'Please Enter Your Friend Name',
38 if dlg.ShowModal() == wx.ID_OK:
39 if not dlg.GetValue():
40 wx.MessageBox(_('Please enter your Friend name'), _('Error'),
41 wx.OK | wx.ICON_ERROR)
43 dlg = wx.SingleChoiceDialog(
44 self, "Find your Friend", 'Search results',
45 ["C++", "VB", "Python", "Perl", "Ruby", "FoxPro"],
48 if dlg.ShowModal() == wx.ID_OK:
49 print 'You selected: %s\n' % dlg.GetStringSelection()
52 def __init__(self, parent, id, title,user,un):
53 wx.Frame.__init__(self, parent, id, title,
54 wx.DefaultPosition, wx.Size(210, 450))
55 hbox = wx.BoxSizer(wx.HORIZONTAL)
56 vbox = wx.BoxSizer(wx.VERTICAL)
57 panel1 = wx.Panel(self, -1)
58 self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1, -1),
59 wx.TR_HIDE_ROOT|wx.TR_DEFAULT_STYLE)
60 self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, lambda evt,un=un : self.OnClickLeftKey(evt,un), self.tree)
61 root = self.tree.AddRoot('My friend')
63 il = wx.ImageList(isz[0], isz[1])
64 gra = il.Add(wx.Image("user.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
65 grb = il.Add(wx.Image("group.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap())
66 self.tree.SetImageList(il)
67 self.tree.AssignImageList(il)
69 for i in user['item']:
70 ch=self.tree.AppendItem(root, i)
71 self.tree.SetItemImage(ch,grb , which = wx.TreeItemIcon_Normal)
73 gr=self.tree.AppendItem(ch, j)
74 self.tree.SetItemImage(gr,gra , which = wx.TreeItemIcon_Normal)
75 menuBar = wx.MenuBar()
77 m_exit = menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Close window and exit program.")
78 self.Bind(wx.EVT_MENU, lambda evt,un=un : self.OnClose(evt,un), m_exit)
79 menuBar.Append(menu, "&File")
81 m_friend = menu.Append(wx.ID_NEW, "&Add Friend", "Meet your friend")
82 menuBar.Append(menu, "&Option")
83 self.Bind(wx.EVT_MENU, self.AddFriend, m_friend)
85 m_about = menu.Append(wx.ID_ABOUT, "&About", "Information about this program")
86 #self.Bind(wx.EVT_MENU, self.OnAbout, m_about)
87 menuBar.Append(menu, "&Help")
88 self.SetMenuBar(menuBar)
89 vbox.Add(self.tree, 1, wx.EXPAND)
90 hbox.Add(panel1, 1, wx.EXPAND)
94 self.Bind(wx.EVT_CLOSE, lambda evt,un=un : self.OnClose(evt,un))
97 toaster = TB.ToasterBox(self, tbstyle=TB.TB_COMPLEX)
98 toaster.SetPopupPauseTime(3000)
99 tbpanel = toaster.GetToasterBoxWindow()
100 panel = wx.Panel(tbpanel, -1)
101 sizer = wx.BoxSizer(wx.VERTICAL)
102 text = wx.StaticText(panel, wx.ID_ANY, label="Welcome \n My friend " + un + "\n Nice to meet you")
103 sizer.Add(text, 0, wx.EXPAND)
104 panel.SetSizer(sizer)
105 toaster.AddPanel(panel)
106 wx.CallLater(100, toaster.Play)
107 thread.start_new_thread(self.receive, ())
108 def putinfo(self,data):
110 text_json= json.loads(data['message'])
111 toaster = TB.ToasterBox(self, tbstyle=TB.TB_COMPLEX)
112 wx.CallAfter(toaster.SetPopupPauseTime,3000)
113 tbpanel = toaster.GetToasterBoxWindow()
114 panel = wx.Panel(tbpanel,-1)
115 sizer = wx.BoxSizer(wx.VERTICAL)
116 text = wx.StaticText(panel, wx.ID_ANY, label=text_json['send'] + " say: \n " + text_json['content'])
117 wx.CallAfter(sizer.Add,text, 0, wx.EXPAND)
118 button = wx.Button(panel, wx.ID_ANY, "Click to view")
119 wx.CallAfter(sizer.Add,button, 0, wx.EXPAND)
120 wx.CallAfter(button.Bind,wx.EVT_BUTTON,default_cb)
121 wx.CallAfter(panel.SetSizer,sizer)
122 wx.CallAfter(toaster.AddPanel,panel)
124 wx.CallAfter(toaster.Play)
126 def put_info(self,data):
127 wx.CallAfter(self.putinfo,data=data)
129 connection = pymongo.MongoClient('mongodb://tyl:22842218@ds051738.mongolab.com:51738/tylchat?authMechanism=SCRAM-SHA-1').get_default_database()
130 if un_g not in connection.collection_names():
131 connection.create_collection(un_g,
137 subscriber = Subscriber(connection, un_g,callback=self.put_info ,
138 matching={'send': 'info-chat'})