annotate libervia.py @ 0:140cec48224a

Initial commit
author Goffi <goffi@goffi.org>
date Sun, 30 Jan 2011 21:50:22 +0100
parents
children 0a7c685faa53
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
3
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
7
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import pyjd # this is dummy in pyjs
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from pyjamas.ui.SimplePanel import SimplePanel
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from pyjamas.ui.RootPanel import RootPanel
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from pyjamas.ui.VerticalPanel import VerticalPanel
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from pyjamas.ui.Label import Label
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from pyjamas.ui import HasAlignment
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from pyjamas.ui.MenuBar import MenuBar
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from pyjamas.ui.MenuItem import MenuItem
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from pyjamas import Window
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from pyjamas.ui.AutoComplete import AutoCompleteTextBox
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from pyjamas.JSONService import JSONProxy
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from register import RegisterPanel, RegisterBox
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
35
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37 class LiberviaJsonProxy(JSONProxy):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def __init__(self, *args, **kwargs):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39 JSONProxy.__init__(self, *args, **kwargs)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self.handler=self
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.cb={}
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def call(self, method, cb, *args):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.cb[method] = cb
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.callMethod(method,args)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
46
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def onRemoteResponse(self, response, request_info):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if self.cb.has_key(request_info.method):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.cb[request_info.method](response)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
50 del self.cb[request_info.method]
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
51
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def onRemoteError(self, code, errobj, request_info):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53 if code != 0:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54 Window.alert("Internal server error")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55 else:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if isinstance(errobj['message'],dict):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
58 else:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59 Window.alert("Error: %s" % errobj['message'])
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60 import pdb
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
61 pdb.set_trace()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
62
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 class RegisterCall(LiberviaJsonProxy):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
65 LiberviaJsonProxy.__init__(self, "/register_api",
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
66 ["isRegistered","isConnected","connect"])
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.handler=self
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.cb={}
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
69
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70 class BrigeCall(LiberviaJsonProxy):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
72 LiberviaJsonProxy.__init__(self, "/json_api",
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
73 ["getContacts"])
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
74
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
75 class MenuCmd:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
76
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def __init__(self, object, handler):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self._object = object
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self._handler = handler
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def execute(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 handler = getattr(self._object, self._handler)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83 handler()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 class Menu(SimplePanel):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
88 SimplePanel.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 menu_general = MenuBar(vertical=True)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 menu_general.addItem("Properties", MenuCmd(self, "onProperties"))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
93 menu_games = MenuBar(vertical=True)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
94 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame"))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
95 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame"))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
96
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
97 menubar = MenuBar(vertical=False)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98 menubar.addItem(MenuItem("General", menu_general))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
99 menubar.addItem(MenuItem("Games", True, menu_games))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.add(menubar)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
101
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def onProperties(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
103 Window.alert("Properties selected")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
104
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def onTarotGame(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
106 Window.alert("Tarot selected")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
107
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
108 def onXiangqiGame(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
109 Window.alert("Xiangqi selected")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
110
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
111 class MagicBox(AutoCompleteTextBox):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
112
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
114 AutoCompleteTextBox.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.setCompletionItems(['@amis: ','@famille: ', '@collègues'])
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
116
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117 class ContactPanel(SimplePanel):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120 SimplePanel.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123 class MiddlePannel(HorizontalPanel):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126 HorizontalPanel.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
128 class MainPanel(VerticalPanel):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
129
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
130 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
131 VerticalPanel.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
132
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
134 self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
135
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
136 menu = Menu()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
137 magic_box = MagicBox()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
138 middle_panel = MiddlePannel()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
139
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
140 self.add(menu)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self.add(magic_box)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self.add(middle_panel)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 self.setCellHeight(menu, "5%")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
145 self.setCellHeight(magic_box, "5%")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
146 self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
147 self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 self.setCellHeight(middle_panel, "90%")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 self.setWidth("100%")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.setHeight("100%")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
152
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 class SatWebFrontend:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 def onModuleLoad(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155 self.panel = MainPanel()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
156 self._dialog = None
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
157 RootPanel().add(self.panel)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self._register = RegisterCall()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
159 self._register.call('isRegistered',self.isRegistered)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
160
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
161 def isRegistered(self, registered):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if not registered:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
163 self._dialog = RegisterBox(self.logged,centered=True)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164 self._dialog.show()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
165 else:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
166 self._register.call('isConnected', self.isConnected)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
167
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
168 def isConnected(self, connected):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
169 if not connected:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
170 self._register.call('connect',self.logged)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
171 else:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
172 self.logged()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
173
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
174 def logged(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
175 if self._dialog:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
176 self._dialog.hide()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
177 del self._dialog # don't work if self._dialog is None
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
178 Window.alert("Logged successfuly :o)")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
179
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
180
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
181 if __name__ == '__main__':
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
182 pyjd.setup("./public/Libervia.html")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
183 app = SatWebFrontend()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184 app.onModuleLoad()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185 pyjd.run()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186