comparison libervia.py @ 19:e8e3704eb97f

Added basic chat panel - the chat panel show history, timestamp, and nickname (pretty similar to primitivus and wix chat window) - JID has be rewritten to work with pyjamas, and is now in browser_side directory - a widget can now be selected: the message send in uniBox will be sent to it if there is no explicit target prefix ("@something") - a basic status panel is added under the uniBox, but not used yet
author Goffi <goffi@goffi.org>
date Sat, 16 Apr 2011 01:46:01 +0200
parents 795d144fc1d2
children 8f4b1a8914c3
comparison
equal deleted inserted replaced
18:795d144fc1d2 19:e8e3704eb97f
26 from pyjamas import Window 26 from pyjamas import Window
27 from pyjamas.JSONService import JSONProxy 27 from pyjamas.JSONService import JSONProxy
28 from pyjamas.ui.KeyboardListener import KEY_ENTER 28 from pyjamas.ui.KeyboardListener import KEY_ENTER
29 from browser_side.register import RegisterPanel, RegisterBox 29 from browser_side.register import RegisterPanel, RegisterBox
30 from browser_side.contact import ContactPanel 30 from browser_side.contact import ContactPanel
31 from browser_side.panels import MainPanel, EmptyPanel, MicroblogPanel 31 from browser_side.panels import MainPanel, EmptyPanel, MicroblogPanel, ChatPanel, StatusPanel
32 from browser_side.jid import JID
32 33
33 34
34 class LiberviaJsonProxy(JSONProxy): 35 class LiberviaJsonProxy(JSONProxy):
35 def __init__(self, *args, **kwargs): 36 def __init__(self, *args, **kwargs):
36 JSONProxy.__init__(self, *args, **kwargs) 37 JSONProxy.__init__(self, *args, **kwargs)
64 self.cb={} 65 self.cb={}
65 66
66 class BridgeCall(LiberviaJsonProxy): 67 class BridgeCall(LiberviaJsonProxy):
67 def __init__(self): 68 def __init__(self):
68 LiberviaJsonProxy.__init__(self, "/json_api", 69 LiberviaJsonProxy.__init__(self, "/json_api",
69 ["getContacts", "sendMblog", "getMblogNodes"]) 70 ["getContacts", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory"])
70 71
71 class BridgeSignals(LiberviaJsonProxy): 72 class BridgeSignals(LiberviaJsonProxy):
72 def __init__(self): 73 def __init__(self):
73 LiberviaJsonProxy.__init__(self, "/json_signal_api", 74 LiberviaJsonProxy.__init__(self, "/json_signal_api",
74 ["getSignals"]) 75 ["getSignals"])
82 83
83 def addKey(self, key): 84 def addKey(self, key):
84 self.getCompletionItems().completions.append(key) 85 self.getCompletionItems().completions.append(key)
85 86
86 def onKeyPress(self, sender, keycode, modifiers): 87 def onKeyPress(self, sender, keycode, modifiers):
87 if keycode == KEY_ENTER and not self.visible: 88 if keycode == KEY_ENTER and not self.visible:
88 self.host.bridge.call('sendMblog', None, self.getText()) 89 _txt = self.getText()
90 if _txt:
91 if _txt.startswith('@'):
92 self.host.bridge.call('sendMblog', None, self.getText())
93 elif isinstance(self.host.selected, ChatPanel):
94 _chat = self.host.selected
95 self.host.bridge.call('sendMessage', None, str(_chat.target), _txt, '', 'chat')
89 self.setText('') 96 self.setText('')
90 97
91 def complete(self): 98 def complete(self):
92 #self.visible=False #XXX: self.visible is not unset in pyjamas when ENTER is pressed and a completion is done 99 #self.visible=False #XXX: self.visible is not unset in pyjamas when ENTER is pressed and a completion is done
93 #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this 100 #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this
94 return AutoCompleteTextBox.complete(self) 101 return AutoCompleteTextBox.complete(self)
95 102
96 103
97 class SatWebFrontend: 104 class SatWebFrontend:
98 def onModuleLoad(self): 105 def onModuleLoad(self):
106 self.whoami = None
99 self.bridge = BridgeCall() 107 self.bridge = BridgeCall()
100 self.bridge_signals = BridgeSignals() 108 self.bridge_signals = BridgeSignals()
109 self.selected = None
101 self.uniBox = UniBox(self) 110 self.uniBox = UniBox(self)
102 self.uniBox.addKey("@@: ") 111 self.uniBox.addKey("@@: ")
112 self.statusPanel = StatusPanel()
103 self.contactPanel = ContactPanel(self) 113 self.contactPanel = ContactPanel(self)
104 self.panel = MainPanel(self) 114 self.panel = MainPanel(self)
105 self.middle_panel = self.panel.middle_panel 115 self.middle_panel = self.panel.middle_panel
106 self.mpanels = [EmptyPanel(self), MicroblogPanel(self, accept_all=True), EmptyPanel(self)] 116 self.mpanels = [EmptyPanel(self), MicroblogPanel(self, accept_all=True), EmptyPanel(self)]
107 self.middle_panel.changePanel(0,self.mpanels[0]) 117 self.middle_panel.changePanel(0,self.mpanels[0])
110 self._dialog = None 120 self._dialog = None
111 RootPanel().add(self.panel) 121 RootPanel().add(self.panel)
112 self._register = RegisterCall() 122 self._register = RegisterCall()
113 self._register.call('isRegistered',self._isRegisteredCB) 123 self._register.call('isRegistered',self._isRegisteredCB)
114 124
125 def select(self, widget):
126 """Define the selected widget"""
127 if self.selected:
128 self.selected.removeStyleName('selected_widget')
129 self.selected = widget
130 if widget:
131 self.selected.addStyleName('selected_widget')
132
115 def _isRegisteredCB(self, registered): 133 def _isRegisteredCB(self, registered):
116 if not registered: 134 if not registered:
117 self._dialog = RegisterBox(self.logged,centered=True) 135 self._dialog = RegisterBox(self.logged,centered=True)
118 self._dialog.show() 136 self._dialog.show()
119 else: 137 else:
131 del self._dialog # don't work if self._dialog is None 149 del self._dialog # don't work if self._dialog is None
132 150
133 #it's time to fill the page 151 #it's time to fill the page
134 self.bridge.call('getContacts', self._getContactsCB) 152 self.bridge.call('getContacts', self._getContactsCB)
135 self.bridge_signals.call('getSignals', self._getSignalsCB) 153 self.bridge_signals.call('getSignals', self._getSignalsCB)
154 #We want to know our own jid
155 self.bridge.call('getProfileJid', self._getProfileJidCB)
136 156
137 def _getContactsCB(self, contacts_data): 157 def _getContactsCB(self, contacts_data):
138 for contact in contacts_data: 158 for contact in contacts_data:
139 jid, attributes, groups = contact 159 jid, attributes, groups = contact
140 self.contactPanel.addContact(jid, attributes, groups) 160 self.contactPanel.addContact(jid, attributes, groups)
144 bridge_signals.call('getSignals', self._getSignalsCB) 164 bridge_signals.call('getSignals', self._getSignalsCB)
145 print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1])) 165 print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1]))
146 name,args = signal_data 166 name,args = signal_data
147 if name == 'personalEvent': 167 if name == 'personalEvent':
148 self._personalEventCb(*args) 168 self._personalEventCb(*args)
169 elif name == 'newMessage':
170 self._newMessageCb(*args)
171
172 def _getProfileJidCB(self, jid):
173 self.whoami = JID(jid)
174
149 175
150 ## Signals callbacks ## 176 ## Signals callbacks ##
151 177
152 def _personalEventCb(self, sender, event_type, data, profile): 178 def _personalEventCb(self, sender, event_type, data, profile):
153 if event_type == "MICROBLOG": 179 if event_type == "MICROBLOG":
167 author = data.get('author') 193 author = data.get('author')
168 print "timestamp: %s" % data.get('timestamp') 194 print "timestamp: %s" % data.get('timestamp')
169 timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here 195 timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here
170 panel.addEntry(content, author, timestamp) 196 panel.addEntry(content, author, timestamp)
171 197
198 def _newMessageCb(self, from_jid, msg, msg_type, to_jid):
199 _from = JID(from_jid)
200 _to = JID(to_jid)
201 for panel in self.mpanels:
202 if isinstance(panel,ChatPanel) and (panel.target.bare == _from.bare or panel.target.bare == _to.bare):
203 panel.printMessage(_from, msg)
204
172 if __name__ == '__main__': 205 if __name__ == '__main__':
173 pyjd.setup("http://localhost:8080/libervia.html") 206 pyjd.setup("http://localhost:8080/libervia.html")
174 app = SatWebFrontend() 207 app = SatWebFrontend()
175 app.onModuleLoad() 208 app.onModuleLoad()
176 pyjd.run() 209 pyjd.run()