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 |
|
35 |
|
36 |
|
37 class LiberviaJsonProxy(JSONProxy): |
|
38 def __init__(self, *args, **kwargs): |
|
39 JSONProxy.__init__(self, *args, **kwargs) |
|
40 self.handler=self |
|
41 self.cb={} |
|
42 |
|
43 def call(self, method, cb, *args): |
|
44 self.cb[method] = cb |
|
45 self.callMethod(method,args) |
|
46 |
|
47 def onRemoteResponse(self, response, request_info): |
|
48 if self.cb.has_key(request_info.method): |
|
49 self.cb[request_info.method](response) |
|
50 del self.cb[request_info.method] |
|
51 |
|
52 def onRemoteError(self, code, errobj, request_info): |
|
53 if code != 0: |
|
54 Window.alert("Internal server error") |
|
55 else: |
|
56 if isinstance(errobj['message'],dict): |
|
57 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString'])) |
|
58 else: |
|
59 Window.alert("Error: %s" % errobj['message']) |
|
60 import pdb |
|
61 pdb.set_trace() |
|
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 |
|
70 class BrigeCall(LiberviaJsonProxy): |
|
71 def __init__(self): |
|
72 LiberviaJsonProxy.__init__(self, "/json_api", |
|
73 ["getContacts"]) |
|
74 |
|
75 class MenuCmd: |
|
76 |
|
77 def __init__(self, object, handler): |
|
78 self._object = object |
|
79 self._handler = handler |
|
80 |
|
81 def execute(self): |
|
82 handler = getattr(self._object, self._handler) |
|
83 handler() |
|
84 |
|
85 class Menu(SimplePanel): |
|
86 |
|
87 def __init__(self): |
|
88 SimplePanel.__init__(self) |
|
89 |
|
90 menu_general = MenuBar(vertical=True) |
|
91 menu_general.addItem("Properties", MenuCmd(self, "onProperties")) |
|
92 |
|
93 menu_games = MenuBar(vertical=True) |
|
94 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame")) |
|
95 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame")) |
|
96 |
|
97 menubar = MenuBar(vertical=False) |
|
98 menubar.addItem(MenuItem("General", menu_general)) |
|
99 menubar.addItem(MenuItem("Games", True, menu_games)) |
|
100 self.add(menubar) |
|
101 |
|
102 def onProperties(self): |
|
103 Window.alert("Properties selected") |
|
104 |
|
105 def onTarotGame(self): |
|
106 Window.alert("Tarot selected") |
|
107 |
|
108 def onXiangqiGame(self): |
|
109 Window.alert("Xiangqi selected") |
|
110 |
|
111 class MagicBox(AutoCompleteTextBox): |
|
112 |
|
113 def __init__(self): |
|
114 AutoCompleteTextBox.__init__(self) |
|
115 self.setCompletionItems(['@amis: ','@famille: ', '@collègues']) |
|
116 |
|
117 class ContactPanel(SimplePanel): |
|
118 |
|
119 def __init__(self): |
|
120 SimplePanel.__init__(self) |
|
121 |
|
122 |
|
123 class MiddlePannel(HorizontalPanel): |
|
124 |
|
125 def __init__(self): |
|
126 HorizontalPanel.__init__(self) |
|
127 |
|
128 class MainPanel(VerticalPanel): |
|
129 |
|
130 def __init__(self): |
|
131 VerticalPanel.__init__(self) |
|
132 |
|
133 self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT) |
|
134 self.setVerticalAlignment(HasAlignment.ALIGN_TOP) |
|
135 |
|
136 menu = Menu() |
|
137 magic_box = MagicBox() |
|
138 middle_panel = MiddlePannel() |
|
139 |
|
140 self.add(menu) |
|
141 self.add(magic_box) |
|
142 self.add(middle_panel) |
|
143 |
|
144 self.setCellHeight(menu, "5%") |
|
145 self.setCellHeight(magic_box, "5%") |
|
146 self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER) |
|
147 self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER) |
|
148 self.setCellHeight(middle_panel, "90%") |
|
149 |
|
150 self.setWidth("100%") |
|
151 self.setHeight("100%") |
|
152 |
|
153 class SatWebFrontend: |
|
154 def onModuleLoad(self): |
|
155 self.panel = MainPanel() |
|
156 self._dialog = None |
|
157 RootPanel().add(self.panel) |
|
158 self._register = RegisterCall() |
|
159 self._register.call('isRegistered',self.isRegistered) |
|
160 |
|
161 def isRegistered(self, registered): |
|
162 if not registered: |
|
163 self._dialog = RegisterBox(self.logged,centered=True) |
|
164 self._dialog.show() |
|
165 else: |
|
166 self._register.call('isConnected', self.isConnected) |
|
167 |
|
168 def isConnected(self, connected): |
|
169 if not connected: |
|
170 self._register.call('connect',self.logged) |
|
171 else: |
|
172 self.logged() |
|
173 |
|
174 def logged(self): |
|
175 if self._dialog: |
|
176 self._dialog.hide() |
|
177 del self._dialog # don't work if self._dialog is None |
|
178 Window.alert("Logged successfuly :o)") |
|
179 |
|
180 |
|
181 if __name__ == '__main__': |
|
182 pyjd.setup("./public/Libervia.html") |
|
183 app = SatWebFrontend() |
|
184 app.onModuleLoad() |
|
185 pyjd.run() |
|
186 |