Mercurial > libervia-web
view 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 |
line wrap: on
line source
#!/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 from pyjamas import DOM from contact import ContactPanel 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']) class RegisterCall(LiberviaJsonProxy): def __init__(self): LiberviaJsonProxy.__init__(self, "/register_api", ["isRegistered","isConnected","connect"]) self.handler=self self.cb={} class BridgeCall(LiberviaJsonProxy): def __init__(self): LiberviaJsonProxy.__init__(self, "/json_api", ["getContacts"]) class BridgeSignals(LiberviaJsonProxy): def __init__(self): LiberviaJsonProxy.__init__(self, "/json_signal_api", ["getSignals"]) 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) def addKey(self, key): self.getCompletionItems().completions.append(key) def addContact(self, jid, attributes, groups): """Add a contact to the panel @param jid: jid @attributes: cf SàT Bridge API's newContact @param groups: list of groups""" for group in groups: if not self.groups.has_key(group): self.groups[group] = set() self._groupList.add(group) self.host.magicBox.addKey("@%s: " % group) self.groups[group].add(jid) self._contactList.add(jid) def onMouseMove(self, sender, x, y): pass def onMouseDown(self, sender, x, y): pass def onMouseUp(self, sender, x, y): pass def onMouseEnter(self, sender): if isinstance(sender, GroupLabel): for contact in self._contactList: if contact.jid in self.groups[sender.group]: contact.addStyleName("selected") def onMouseLeave(self, sender): if isinstance(sender, GroupLabel): for contact in self._contactList: if contact.jid in self.groups[sender.group]: contact.removeStyleName("selected") class MicroblogEntry(SimplePanel): def __init__(self, text): SimplePanel.__init__(self) self._vPanel = VerticalPanel() self._vPanel.setStyleName('microblogEntry') _label = Label(text) self._vPanel.add(_label) self.add(self._vPanel) class MicroblogPanel(VerticalPanel): def __init__(self): VerticalPanel.__init__(self) self.setStyleName('microblogPanel') def addEntry(self, text, author=None, date=None): """Add an entry to the panel @param text: main text of the entry @param author: who wrote the entry @param date: when the entry was written""" _entry = MicroblogEntry(text) self.add(_entry) class MiddlePannel(HorizontalPanel): def __init__(self,host): self.host=host HorizontalPanel.__init__(self) self._left = self.host.contactPanel self._right = HorizontalPanel() self._right.setWidth('100%') self._right.setHeight('100%') test = MicroblogPanel() self._right.add(test) test.addEntry("test entry") self.add(self._left) self.setCellWidth(self._left, "15%") self.add(self._right) self.setCellWidth(self._right, "85%") class MainPanel(VerticalPanel): def __init__(self, host): self.host=host VerticalPanel.__init__(self) self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT) self.setVerticalAlignment(HasAlignment.ALIGN_TOP) menu = Menu() magic_box = host.magicBox middle_panel = MiddlePannel(self.host) middle_panel.setWidth('100%') 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.setCellWidth(middle_panel, "100%") self.setWidth("100%") self.setHeight("100%") class SatWebFrontend: def onModuleLoad(self): self.magicBox = MagicBox() self.contactPanel = ContactPanel(self) self.panel = MainPanel(self) self._dialog = None RootPanel().add(self.panel) self._register = RegisterCall() self._register.call('isRegistered',self._isRegisteredCB) def _isRegisteredCB(self, registered): if not registered: self._dialog = RegisterBox(self.logged,centered=True) self._dialog.show() else: self._register.call('isConnected', self._isConnectedCB) def _isConnectedCB(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 #it's time to fill the page bridge = BridgeCall() bridge.call('getContacts', self._getContactsCB) bridge_signals = BridgeSignals() bridge_signals.call('getSignals', self._getSignalsCB) def _getContactsCB(self, contacts_data): for contact in contacts_data: jid, attributes, groups = contact self.contactPanel.addContact(jid, attributes, groups) def _getSignalsCB(self, signal_data): bridge_signals = BridgeSignals() bridge_signals.call('getSignals', self._getSignalsCB) print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1])) if __name__ == '__main__': pyjd.setup("http://localhost:8080/libervia.html") app = SatWebFrontend() app.onModuleLoad() pyjd.run()