comparison libervia.py @ 2:669c531a857e

signals handling and first draft of microblogging - server side: signal handling throught json_signal_api page - browser side: - signal handling throught a json rpc call loop - first draft of microblog panel - ContactPanel put in a separate module
author Goffi <goffi@goffi.org>
date Tue, 08 Feb 2011 21:18:31 +0100
parents 0a7c685faa53
children 7325e787c22b
comparison
equal deleted inserted replaced
1:0a7c685faa53 2:669c531a857e
31 from pyjamas import Window 31 from pyjamas import Window
32 from pyjamas.ui.AutoComplete import AutoCompleteTextBox 32 from pyjamas.ui.AutoComplete import AutoCompleteTextBox
33 from pyjamas.JSONService import JSONProxy 33 from pyjamas.JSONService import JSONProxy
34 from register import RegisterPanel, RegisterBox 34 from register import RegisterPanel, RegisterBox
35 from pyjamas import DOM 35 from pyjamas import DOM
36 from contact import ContactPanel
36 37
37 38
38 class LiberviaJsonProxy(JSONProxy): 39 class LiberviaJsonProxy(JSONProxy):
39 def __init__(self, *args, **kwargs): 40 def __init__(self, *args, **kwargs):
40 JSONProxy.__init__(self, *args, **kwargs) 41 JSONProxy.__init__(self, *args, **kwargs)
64 LiberviaJsonProxy.__init__(self, "/register_api", 65 LiberviaJsonProxy.__init__(self, "/register_api",
65 ["isRegistered","isConnected","connect"]) 66 ["isRegistered","isConnected","connect"])
66 self.handler=self 67 self.handler=self
67 self.cb={} 68 self.cb={}
68 69
69 class BrigeCall(LiberviaJsonProxy): 70 class BridgeCall(LiberviaJsonProxy):
70 def __init__(self): 71 def __init__(self):
71 LiberviaJsonProxy.__init__(self, "/json_api", 72 LiberviaJsonProxy.__init__(self, "/json_api",
72 ["getContacts"]) 73 ["getContacts"])
74
75 class BridgeSignals(LiberviaJsonProxy):
76 def __init__(self):
77 LiberviaJsonProxy.__init__(self, "/json_signal_api",
78 ["getSignals"])
73 79
74 class MenuCmd: 80 class MenuCmd:
75 81
76 def __init__(self, object, handler): 82 def __init__(self, object, handler):
77 self._object = object 83 self._object = object
112 def __init__(self): 118 def __init__(self):
113 AutoCompleteTextBox.__init__(self) 119 AutoCompleteTextBox.__init__(self)
114 120
115 def addKey(self, key): 121 def addKey(self, key):
116 self.getCompletionItems().completions.append(key) 122 self.getCompletionItems().completions.append(key)
117
118 class GroupLabel(Label):
119 def __init__(self, group):
120 Label.__init__(self, group)
121 self.group = group
122 self.setStyleName('group')
123
124 class ContactLabel(Label):
125 def __init__(self, jid, name=None):
126 if not name:
127 name=jid
128 Label.__init__(self, name)
129 self.jid=jid
130 self.setStyleName('contact')
131
132 class GroupList(VerticalPanel):
133
134 def __init__(self, parent):
135 VerticalPanel.__init__(self)
136 self._parent = parent
137
138 def add(self, group):
139 _item = GroupLabel(group)
140 _item.addMouseListener(self._parent)
141 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
142 VerticalPanel.add(self, _item)
143
144 class ContactList(VerticalPanel):
145
146 def __init__(self):
147 VerticalPanel.__init__(self)
148 self.contacts=[]
149
150 def __iter__(self):
151 return self.contacts.__iter__()
152
153 def add(self, jid, name=None):
154 _item = ContactLabel(jid, name)
155 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
156 VerticalPanel.add(self, _item)
157 self.contacts.append(_item)
158
159 class ContactPanel(SimplePanel):
160 """Manage the contacts and groups"""
161
162 def __init__(self, host):
163 SimplePanel.__init__(self)
164 self.host = host
165 self.groups={}
166
167 self.vPanel = VerticalPanel()
168 _title = Label('Contacts')
169 _title.setStyleName('contactTitle')
170
171 self._contactList = ContactList()
172 self._contactList.setStyleName('contactList')
173 self._groupList = GroupList(self)
174 self._groupList.setStyleName('groupList')
175
176 self.vPanel.add(_title)
177 self.vPanel.add(self._groupList)
178 self.vPanel.add(self._contactList)
179 self.add(self.vPanel)
180 self.setStyleName('contactBox')
181 123
182 def addContact(self, jid, attributes, groups): 124 def addContact(self, jid, attributes, groups):
183 """Add a contact to the panel 125 """Add a contact to the panel
184 @param jid: jid 126 @param jid: jid
185 @attributes: cf SàT Bridge API's newContact 127 @attributes: cf SàT Bridge API's newContact
211 if isinstance(sender, GroupLabel): 153 if isinstance(sender, GroupLabel):
212 for contact in self._contactList: 154 for contact in self._contactList:
213 if contact.jid in self.groups[sender.group]: 155 if contact.jid in self.groups[sender.group]:
214 contact.removeStyleName("selected") 156 contact.removeStyleName("selected")
215 157
158 class MicroblogEntry(SimplePanel):
159
160 def __init__(self, text):
161 SimplePanel.__init__(self)
162 self._vPanel = VerticalPanel()
163 self._vPanel.setStyleName('microblogEntry')
164 _label = Label(text)
165 self._vPanel.add(_label)
166 self.add(self._vPanel)
167
168
169
170 class MicroblogPanel(VerticalPanel):
171
172 def __init__(self):
173 VerticalPanel.__init__(self)
174 self.setStyleName('microblogPanel')
175
176 def addEntry(self, text, author=None, date=None):
177 """Add an entry to the panel
178 @param text: main text of the entry
179 @param author: who wrote the entry
180 @param date: when the entry was written"""
181 _entry = MicroblogEntry(text)
182 self.add(_entry)
183
216 class MiddlePannel(HorizontalPanel): 184 class MiddlePannel(HorizontalPanel):
217 185
218 def __init__(self,host): 186 def __init__(self,host):
219 self.host=host 187 self.host=host
220 HorizontalPanel.__init__(self) 188 HorizontalPanel.__init__(self)
221 self.add(self.host.contactPanel) 189 self._left = self.host.contactPanel
190 self._right = HorizontalPanel()
191 self._right.setWidth('100%')
192 self._right.setHeight('100%')
193 test = MicroblogPanel()
194 self._right.add(test)
195 test.addEntry("test entry")
196 self.add(self._left)
197 self.setCellWidth(self._left, "15%")
198 self.add(self._right)
199 self.setCellWidth(self._right, "85%")
222 200
223 class MainPanel(VerticalPanel): 201 class MainPanel(VerticalPanel):
224 202
225 def __init__(self, host): 203 def __init__(self, host):
226 self.host=host 204 self.host=host
230 self.setVerticalAlignment(HasAlignment.ALIGN_TOP) 208 self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
231 209
232 menu = Menu() 210 menu = Menu()
233 magic_box = host.magicBox 211 magic_box = host.magicBox
234 middle_panel = MiddlePannel(self.host) 212 middle_panel = MiddlePannel(self.host)
213 middle_panel.setWidth('100%')
235 214
236 self.add(menu) 215 self.add(menu)
237 self.add(magic_box) 216 self.add(magic_box)
238 self.add(middle_panel) 217 self.add(middle_panel)
239 218
240 self.setCellHeight(menu, "5%") 219 self.setCellHeight(menu, "5%")
241 self.setCellHeight(magic_box, "5%") 220 self.setCellHeight(magic_box, "5%")
242 self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER) 221 self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
243 self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER) 222 self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
244 self.setCellHeight(middle_panel, "90%") 223 self.setCellHeight(middle_panel, "90%")
224 self.setCellWidth(middle_panel, "100%")
245 225
246 self.setWidth("100%") 226 self.setWidth("100%")
247 self.setHeight("100%") 227 self.setHeight("100%")
248 228
249 class SatWebFrontend: 229 class SatWebFrontend:
273 if self._dialog: 253 if self._dialog:
274 self._dialog.hide() 254 self._dialog.hide()
275 del self._dialog # don't work if self._dialog is None 255 del self._dialog # don't work if self._dialog is None
276 256
277 #it's time to fill the page 257 #it's time to fill the page
278 bridge = BrigeCall() 258 bridge = BridgeCall()
279 bridge.call('getContacts', self._getContactsCB) 259 bridge.call('getContacts', self._getContactsCB)
260
261 bridge_signals = BridgeSignals()
262 bridge_signals.call('getSignals', self._getSignalsCB)
263
280 264
281 def _getContactsCB(self, contacts_data): 265 def _getContactsCB(self, contacts_data):
282 for contact in contacts_data: 266 for contact in contacts_data:
283 jid, attributes, groups = contact 267 jid, attributes, groups = contact
284 self.contactPanel.addContact(jid, attributes, groups) 268 self.contactPanel.addContact(jid, attributes, groups)
269
270 def _getSignalsCB(self, signal_data):
271 bridge_signals = BridgeSignals()
272 bridge_signals.call('getSignals', self._getSignalsCB)
273 print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1]))
285 274
286 275
287 if __name__ == '__main__': 276 if __name__ == '__main__':
288 pyjd.setup("http://localhost:8080/libervia.html") 277 pyjd.setup("http://localhost:8080/libervia.html")
289 app = SatWebFrontend() 278 app = SatWebFrontend()