comparison libervia.py @ 1:0a7c685faa53

ContactPanel: first draft
author Goffi <goffi@goffi.org>
date Mon, 31 Jan 2011 20:31:25 +0100
parents 140cec48224a
children 669c531a857e
comparison
equal deleted inserted replaced
0:140cec48224a 1:0a7c685faa53
30 from pyjamas.ui.MenuItem import MenuItem 30 from pyjamas.ui.MenuItem import MenuItem
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 36
36 37
37 class LiberviaJsonProxy(JSONProxy): 38 class LiberviaJsonProxy(JSONProxy):
38 def __init__(self, *args, **kwargs): 39 def __init__(self, *args, **kwargs):
39 JSONProxy.__init__(self, *args, **kwargs) 40 JSONProxy.__init__(self, *args, **kwargs)
55 else: 56 else:
56 if isinstance(errobj['message'],dict): 57 if isinstance(errobj['message'],dict):
57 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString'])) 58 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
58 else: 59 else:
59 Window.alert("Error: %s" % errobj['message']) 60 Window.alert("Error: %s" % errobj['message'])
60 import pdb
61 pdb.set_trace()
62 61
63 class RegisterCall(LiberviaJsonProxy): 62 class RegisterCall(LiberviaJsonProxy):
64 def __init__(self): 63 def __init__(self):
65 LiberviaJsonProxy.__init__(self, "/register_api", 64 LiberviaJsonProxy.__init__(self, "/register_api",
66 ["isRegistered","isConnected","connect"]) 65 ["isRegistered","isConnected","connect"])
110 109
111 class MagicBox(AutoCompleteTextBox): 110 class MagicBox(AutoCompleteTextBox):
112 111
113 def __init__(self): 112 def __init__(self):
114 AutoCompleteTextBox.__init__(self) 113 AutoCompleteTextBox.__init__(self)
115 self.setCompletionItems(['@amis: ','@famille: ', '@collègues']) 114
115 def addKey(self, key):
116 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)
116 158
117 class ContactPanel(SimplePanel): 159 class ContactPanel(SimplePanel):
118 160 """Manage the contacts and groups"""
119 def __init__(self): 161
162 def __init__(self, host):
120 SimplePanel.__init__(self) 163 SimplePanel.__init__(self)
121 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
182 def addContact(self, jid, attributes, groups):
183 """Add a contact to the panel
184 @param jid: jid
185 @attributes: cf SàT Bridge API's newContact
186 @param groups: list of groups"""
187 for group in groups:
188 if not self.groups.has_key(group):
189 self.groups[group] = set()
190 self._groupList.add(group)
191 self.host.magicBox.addKey("@%s: " % group)
192 self.groups[group].add(jid)
193 self._contactList.add(jid)
194
195 def onMouseMove(self, sender, x, y):
196 pass
197
198 def onMouseDown(self, sender, x, y):
199 pass
200
201 def onMouseUp(self, sender, x, y):
202 pass
203
204 def onMouseEnter(self, sender):
205 if isinstance(sender, GroupLabel):
206 for contact in self._contactList:
207 if contact.jid in self.groups[sender.group]:
208 contact.addStyleName("selected")
209
210 def onMouseLeave(self, sender):
211 if isinstance(sender, GroupLabel):
212 for contact in self._contactList:
213 if contact.jid in self.groups[sender.group]:
214 contact.removeStyleName("selected")
122 215
123 class MiddlePannel(HorizontalPanel): 216 class MiddlePannel(HorizontalPanel):
124 217
125 def __init__(self): 218 def __init__(self,host):
219 self.host=host
126 HorizontalPanel.__init__(self) 220 HorizontalPanel.__init__(self)
221 self.add(self.host.contactPanel)
127 222
128 class MainPanel(VerticalPanel): 223 class MainPanel(VerticalPanel):
129 224
130 def __init__(self): 225 def __init__(self, host):
226 self.host=host
131 VerticalPanel.__init__(self) 227 VerticalPanel.__init__(self)
132 228
133 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT) 229 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
134 self.setVerticalAlignment(HasAlignment.ALIGN_TOP) 230 self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
135 231
136 menu = Menu() 232 menu = Menu()
137 magic_box = MagicBox() 233 magic_box = host.magicBox
138 middle_panel = MiddlePannel() 234 middle_panel = MiddlePannel(self.host)
139 235
140 self.add(menu) 236 self.add(menu)
141 self.add(magic_box) 237 self.add(magic_box)
142 self.add(middle_panel) 238 self.add(middle_panel)
143 239
150 self.setWidth("100%") 246 self.setWidth("100%")
151 self.setHeight("100%") 247 self.setHeight("100%")
152 248
153 class SatWebFrontend: 249 class SatWebFrontend:
154 def onModuleLoad(self): 250 def onModuleLoad(self):
155 self.panel = MainPanel() 251 self.magicBox = MagicBox()
252 self.contactPanel = ContactPanel(self)
253 self.panel = MainPanel(self)
156 self._dialog = None 254 self._dialog = None
157 RootPanel().add(self.panel) 255 RootPanel().add(self.panel)
158 self._register = RegisterCall() 256 self._register = RegisterCall()
159 self._register.call('isRegistered',self.isRegistered) 257 self._register.call('isRegistered',self._isRegisteredCB)
160 258
161 def isRegistered(self, registered): 259 def _isRegisteredCB(self, registered):
162 if not registered: 260 if not registered:
163 self._dialog = RegisterBox(self.logged,centered=True) 261 self._dialog = RegisterBox(self.logged,centered=True)
164 self._dialog.show() 262 self._dialog.show()
165 else: 263 else:
166 self._register.call('isConnected', self.isConnected) 264 self._register.call('isConnected', self._isConnectedCB)
167 265
168 def isConnected(self, connected): 266 def _isConnectedCB(self, connected):
169 if not connected: 267 if not connected:
170 self._register.call('connect',self.logged) 268 self._register.call('connect',self.logged)
171 else: 269 else:
172 self.logged() 270 self.logged()
173 271
174 def logged(self): 272 def logged(self):
175 if self._dialog: 273 if self._dialog:
176 self._dialog.hide() 274 self._dialog.hide()
177 del self._dialog # don't work if self._dialog is None 275 del self._dialog # don't work if self._dialog is None
178 Window.alert("Logged successfuly :o)") 276
277 #it's time to fill the page
278 bridge = BrigeCall()
279 bridge.call('getContacts', self._getContactsCB)
280
281 def _getContactsCB(self, contacts_data):
282 for contact in contacts_data:
283 jid, attributes, groups = contact
284 self.contactPanel.addContact(jid, attributes, groups)
179 285
180 286
181 if __name__ == '__main__': 287 if __name__ == '__main__':
182 pyjd.setup("./public/Libervia.html") 288 pyjd.setup("http://localhost:8080/libervia.html")
183 app = SatWebFrontend() 289 app = SatWebFrontend()
184 app.onModuleLoad() 290 app.onModuleLoad()
185 pyjd.run() 291 pyjd.run()
186 292