diff libervia.py @ 0:140cec48224a

Initial commit
author Goffi <goffi@goffi.org>
date Sun, 30 Jan 2011 21:50:22 +0100
parents
children 0a7c685faa53
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia.py	Sun Jan 30 21:50:22 2011 +0100
@@ -0,0 +1,186 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""
+Libervia: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+import pyjd # this is dummy in pyjs
+from pyjamas.ui.SimplePanel import SimplePanel
+from pyjamas.ui.RootPanel import RootPanel
+from pyjamas.ui.VerticalPanel import VerticalPanel
+from pyjamas.ui.HorizontalPanel import HorizontalPanel
+from pyjamas.ui.Label import Label
+from pyjamas.ui import HasAlignment
+from pyjamas.ui.MenuBar import MenuBar
+from pyjamas.ui.MenuItem import MenuItem
+from pyjamas import Window
+from pyjamas.ui.AutoComplete import AutoCompleteTextBox
+from pyjamas.JSONService import JSONProxy
+from register import RegisterPanel, RegisterBox
+
+
+class LiberviaJsonProxy(JSONProxy):
+    def __init__(self, *args, **kwargs):
+        JSONProxy.__init__(self, *args, **kwargs)
+        self.handler=self
+        self.cb={}
+
+    def call(self, method, cb, *args):
+        self.cb[method] = cb
+        self.callMethod(method,args)
+
+    def onRemoteResponse(self, response, request_info):
+        if self.cb.has_key(request_info.method):
+            self.cb[request_info.method](response)
+            del self.cb[request_info.method]
+        
+    def onRemoteError(self, code, errobj, request_info):
+        if code != 0:
+            Window.alert("Internal server error")
+        else:
+            if isinstance(errobj['message'],dict):
+                Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
+            else:
+                Window.alert("Error: %s" % errobj['message'])
+                import pdb
+                pdb.set_trace()
+
+class RegisterCall(LiberviaJsonProxy):
+    def __init__(self):
+        LiberviaJsonProxy.__init__(self, "/register_api",
+                        ["isRegistered","isConnected","connect"])
+        self.handler=self
+        self.cb={}
+
+class BrigeCall(LiberviaJsonProxy):
+    def __init__(self):
+        LiberviaJsonProxy.__init__(self, "/json_api",
+                        ["getContacts"])
+
+class MenuCmd:
+
+    def __init__(self, object, handler):
+        self._object  = object
+        self._handler = handler
+
+    def execute(self):
+        handler = getattr(self._object, self._handler)
+        handler()
+
+class Menu(SimplePanel):
+
+    def __init__(self):
+        SimplePanel.__init__(self)
+
+        menu_general = MenuBar(vertical=True)
+        menu_general.addItem("Properties", MenuCmd(self, "onProperties"))
+
+        menu_games = MenuBar(vertical=True)
+        menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame"))
+        menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame"))
+
+        menubar = MenuBar(vertical=False)
+        menubar.addItem(MenuItem("General", menu_general))
+        menubar.addItem(MenuItem("Games", True, menu_games))
+        self.add(menubar)
+
+    def onProperties(self):
+        Window.alert("Properties selected")
+
+    def onTarotGame(self):
+        Window.alert("Tarot selected")
+
+    def onXiangqiGame(self):
+        Window.alert("Xiangqi selected")
+
+class MagicBox(AutoCompleteTextBox):
+
+    def __init__(self):
+        AutoCompleteTextBox.__init__(self)
+        self.setCompletionItems(['@amis: ','@famille: ', '@collègues'])
+
+class ContactPanel(SimplePanel):
+    
+    def __init__(self):
+        SimplePanel.__init__(self)
+
+
+class MiddlePannel(HorizontalPanel):
+    
+    def __init__(self):
+        HorizontalPanel.__init__(self)
+
+class MainPanel(VerticalPanel):
+
+    def __init__(self):
+        VerticalPanel.__init__(self)
+
+        self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
+        self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
+
+        menu = Menu()
+        magic_box = MagicBox()
+        middle_panel = MiddlePannel()
+
+        self.add(menu)
+        self.add(magic_box)
+        self.add(middle_panel)
+        
+        self.setCellHeight(menu, "5%")
+        self.setCellHeight(magic_box, "5%")
+        self.setCellVerticalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
+        self.setCellHorizontalAlignment(magic_box, HasAlignment.ALIGN_CENTER)
+        self.setCellHeight(middle_panel, "90%")
+
+        self.setWidth("100%")
+        self.setHeight("100%")
+
+class SatWebFrontend:
+    def onModuleLoad(self):
+        self.panel = MainPanel()
+        self._dialog = None
+        RootPanel().add(self.panel)
+        self._register = RegisterCall()
+        self._register.call('isRegistered',self.isRegistered)
+
+    def isRegistered(self, registered):
+        if not registered:
+            self._dialog = RegisterBox(self.logged,centered=True)
+            self._dialog.show()
+        else:
+            self._register.call('isConnected', self.isConnected)
+
+    def isConnected(self, connected):
+        if not connected:
+            self._register.call('connect',self.logged)
+        else:
+            self.logged()
+
+    def logged(self):
+        if self._dialog:
+            self._dialog.hide()
+            del self._dialog # don't work if self._dialog is None
+        Window.alert("Logged successfuly :o)")
+
+
+if __name__ == '__main__':
+    pyjd.setup("./public/Libervia.html")
+    app = SatWebFrontend()
+    app.onModuleLoad()
+    pyjd.run()
+