Mercurial > libervia-web
annotate 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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Libervia: a Salut à Toi frontend | |
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU Affero General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU Affero General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU Affero General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import pyjd # this is dummy in pyjs | |
23 from pyjamas.ui.SimplePanel import SimplePanel | |
24 from pyjamas.ui.RootPanel import RootPanel | |
25 from pyjamas.ui.VerticalPanel import VerticalPanel | |
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel | |
27 from pyjamas.ui.Label import Label | |
28 from pyjamas.ui import HasAlignment | |
29 from pyjamas.ui.MenuBar import MenuBar | |
30 from pyjamas.ui.MenuItem import MenuItem | |
31 from pyjamas import Window | |
32 from pyjamas.ui.AutoComplete import AutoCompleteTextBox | |
33 from pyjamas.JSONService import JSONProxy | |
34 from register import RegisterPanel, RegisterBox | |
1 | 35 from pyjamas import DOM |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
36 from contact import ContactPanel |
0 | 37 |
38 | |
39 class LiberviaJsonProxy(JSONProxy): | |
40 def __init__(self, *args, **kwargs): | |
41 JSONProxy.__init__(self, *args, **kwargs) | |
42 self.handler=self | |
43 self.cb={} | |
44 | |
45 def call(self, method, cb, *args): | |
46 self.cb[method] = cb | |
47 self.callMethod(method,args) | |
48 | |
49 def onRemoteResponse(self, response, request_info): | |
50 if self.cb.has_key(request_info.method): | |
51 self.cb[request_info.method](response) | |
52 del self.cb[request_info.method] | |
53 | |
54 def onRemoteError(self, code, errobj, request_info): | |
55 if code != 0: | |
56 Window.alert("Internal server error") | |
57 else: | |
58 if isinstance(errobj['message'],dict): | |
59 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString'])) | |
60 else: | |
61 Window.alert("Error: %s" % errobj['message']) | |
62 | |
63 class RegisterCall(LiberviaJsonProxy): | |
64 def __init__(self): | |
65 LiberviaJsonProxy.__init__(self, "/register_api", | |
66 ["isRegistered","isConnected","connect"]) | |
67 self.handler=self | |
68 self.cb={} | |
69 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
70 class BridgeCall(LiberviaJsonProxy): |
0 | 71 def __init__(self): |
72 LiberviaJsonProxy.__init__(self, "/json_api", | |
73 ["getContacts"]) | |
74 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
75 class BridgeSignals(LiberviaJsonProxy): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
76 def __init__(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
77 LiberviaJsonProxy.__init__(self, "/json_signal_api", |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
78 ["getSignals"]) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
79 |
0 | 80 class MenuCmd: |
81 | |
82 def __init__(self, object, handler): | |
83 self._object = object | |
84 self._handler = handler | |
85 | |
86 def execute(self): | |
87 handler = getattr(self._object, self._handler) | |
88 handler() | |
89 | |
90 class Menu(SimplePanel): | |
91 | |
92 def __init__(self): | |
93 SimplePanel.__init__(self) | |
94 | |
95 menu_general = MenuBar(vertical=True) | |
96 menu_general.addItem("Properties", MenuCmd(self, "onProperties")) | |
97 | |
98 menu_games = MenuBar(vertical=True) | |
99 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame")) | |
100 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame")) | |
101 | |
102 menubar = MenuBar(vertical=False) | |
103 menubar.addItem(MenuItem("General", menu_general)) | |
104 menubar.addItem(MenuItem("Games", True, menu_games)) | |
105 self.add(menubar) | |
106 | |
107 def onProperties(self): | |
108 Window.alert("Properties selected") | |
109 | |
110 def onTarotGame(self): | |
111 Window.alert("Tarot selected") | |
112 | |
113 def onXiangqiGame(self): | |
114 Window.alert("Xiangqi selected") | |
115 | |
116 class MagicBox(AutoCompleteTextBox): | |
117 | |
118 def __init__(self): | |
119 AutoCompleteTextBox.__init__(self) | |
1 | 120 |
121 def addKey(self, key): | |
122 self.getCompletionItems().completions.append(key) | |
123 | |
124 def addContact(self, jid, attributes, groups): | |
125 """Add a contact to the panel | |
126 @param jid: jid | |
127 @attributes: cf SàT Bridge API's newContact | |
128 @param groups: list of groups""" | |
129 for group in groups: | |
130 if not self.groups.has_key(group): | |
131 self.groups[group] = set() | |
132 self._groupList.add(group) | |
133 self.host.magicBox.addKey("@%s: " % group) | |
134 self.groups[group].add(jid) | |
135 self._contactList.add(jid) | |
136 | |
137 def onMouseMove(self, sender, x, y): | |
138 pass | |
139 | |
140 def onMouseDown(self, sender, x, y): | |
141 pass | |
142 | |
143 def onMouseUp(self, sender, x, y): | |
144 pass | |
145 | |
146 def onMouseEnter(self, sender): | |
147 if isinstance(sender, GroupLabel): | |
148 for contact in self._contactList: | |
149 if contact.jid in self.groups[sender.group]: | |
150 contact.addStyleName("selected") | |
151 | |
152 def onMouseLeave(self, sender): | |
153 if isinstance(sender, GroupLabel): | |
154 for contact in self._contactList: | |
155 if contact.jid in self.groups[sender.group]: | |
156 contact.removeStyleName("selected") | |
0 | 157 |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
158 class MicroblogEntry(SimplePanel): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
159 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
160 def __init__(self, text): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
161 SimplePanel.__init__(self) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
162 self._vPanel = VerticalPanel() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
163 self._vPanel.setStyleName('microblogEntry') |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
164 _label = Label(text) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
165 self._vPanel.add(_label) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
166 self.add(self._vPanel) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
167 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
168 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
169 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
170 class MicroblogPanel(VerticalPanel): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
171 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
172 def __init__(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
173 VerticalPanel.__init__(self) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
174 self.setStyleName('microblogPanel') |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
175 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
176 def addEntry(self, text, author=None, date=None): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
177 """Add an entry to the panel |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
178 @param text: main text of the entry |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
179 @param author: who wrote the entry |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
180 @param date: when the entry was written""" |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
181 _entry = MicroblogEntry(text) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
182 self.add(_entry) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
183 |
0 | 184 class MiddlePannel(HorizontalPanel): |
185 | |
1 | 186 def __init__(self,host): |
187 self.host=host | |
0 | 188 HorizontalPanel.__init__(self) |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
189 self._left = self.host.contactPanel |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
190 self._right = HorizontalPanel() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
191 self._right.setWidth('100%') |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
192 self._right.setHeight('100%') |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
193 test = MicroblogPanel() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
194 self._right.add(test) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
195 test.addEntry("test entry") |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
196 self.add(self._left) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
197 self.setCellWidth(self._left, "15%") |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
198 self.add(self._right) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
199 self.setCellWidth(self._right, "85%") |
0 | 200 |
201 class MainPanel(VerticalPanel): | |
202 | |
1 | 203 def __init__(self, host): |
204 self.host=host | |
0 | 205 VerticalPanel.__init__(self) |
206 | |
207 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT) | |
208 self.setVerticalAlignment(HasAlignment.ALIGN_TOP) | |
209 | |
210 menu = Menu() | |
1 | 211 magic_box = host.magicBox |
212 middle_panel = MiddlePannel(self.host) | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
213 middle_panel.setWidth('100%') |
0 | 214 |
215 self.add(menu) | |
216 self.add(magic_box) | |
217 self.add(middle_panel) | |
218 | |
219 self.setCellHeight(menu, "5%") | |
220 self.setCellHeight(magic_box, "5%") | |
221 self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER) | |
222 self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER) | |
223 self.setCellHeight(middle_panel, "90%") | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
224 self.setCellWidth(middle_panel, "100%") |
0 | 225 |
226 self.setWidth("100%") | |
227 self.setHeight("100%") | |
228 | |
229 class SatWebFrontend: | |
230 def onModuleLoad(self): | |
1 | 231 self.magicBox = MagicBox() |
232 self.contactPanel = ContactPanel(self) | |
233 self.panel = MainPanel(self) | |
0 | 234 self._dialog = None |
235 RootPanel().add(self.panel) | |
236 self._register = RegisterCall() | |
1 | 237 self._register.call('isRegistered',self._isRegisteredCB) |
0 | 238 |
1 | 239 def _isRegisteredCB(self, registered): |
0 | 240 if not registered: |
241 self._dialog = RegisterBox(self.logged,centered=True) | |
242 self._dialog.show() | |
243 else: | |
1 | 244 self._register.call('isConnected', self._isConnectedCB) |
0 | 245 |
1 | 246 def _isConnectedCB(self, connected): |
0 | 247 if not connected: |
248 self._register.call('connect',self.logged) | |
249 else: | |
250 self.logged() | |
251 | |
252 def logged(self): | |
253 if self._dialog: | |
254 self._dialog.hide() | |
255 del self._dialog # don't work if self._dialog is None | |
1 | 256 |
257 #it's time to fill the page | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
258 bridge = BridgeCall() |
1 | 259 bridge.call('getContacts', self._getContactsCB) |
260 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
261 bridge_signals = BridgeSignals() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
262 bridge_signals.call('getSignals', self._getSignalsCB) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
263 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
264 |
1 | 265 def _getContactsCB(self, contacts_data): |
266 for contact in contacts_data: | |
267 jid, attributes, groups = contact | |
268 self.contactPanel.addContact(jid, attributes, groups) | |
0 | 269 |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
270 def _getSignalsCB(self, signal_data): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
271 bridge_signals = BridgeSignals() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
272 bridge_signals.call('getSignals', self._getSignalsCB) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
273 print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1])) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
274 |
0 | 275 |
276 if __name__ == '__main__': | |
1 | 277 pyjd.setup("http://localhost:8080/libervia.html") |
0 | 278 app = SatWebFrontend() |
279 app.onModuleLoad() | |
280 pyjd.run() | |
281 |