view libervia.py @ 83:68d360caeecb

New widget system: first draft - adaptation of Adrien's design - widgets are now based on a flextable - widgets now use a base class LiberviaWidget (the name is to avoid confustion with pyjamas' widgets) - widgets have a title, and setting/close buttons
author Goffi <goffi@goffi.org>
date Sun, 26 Jun 2011 03:04:17 +0200
parents 9d8e79ac4c9c
children 8f35e9970e7f
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.RootPanel import RootPanel
from pyjamas.ui.HTML import HTML
from pyjamas.ui.KeyboardListener import KEY_ESCAPE
from pyjamas import Window, DOM
from pyjamas.JSONService import JSONProxy
from browser_side.register import RegisterBox
from browser_side.contact import ContactPanel
from browser_side import panels, dialog
from browser_side.jid import JID
from browser_side.tools import html_sanitize

MAX_MBLOG_CACHE = 500 #Max microblog entries kept in memories

class LiberviaJsonProxy(JSONProxy):
    def __init__(self, *args, **kwargs):
        JSONProxy.__init__(self, *args, **kwargs)
        self.handler=self
        self.cb={}

    def call(self, method, cb, *args):
        id = self.callMethod(method,args)
        if cb:
            self.cb[id] = cb

    def onRemoteResponse(self, response, request_info):
        if self.cb.has_key(request_info.id):
            self.cb[request_info.id](response)
            del self.cb[request_info.id]
        
    def onRemoteError(self, code, errobj, request_info):
        """def dump(obj):
            print "\n\nDUMPING %s\n\n" % obj
            for i in dir(obj):
                print "%s: %s" % (i, getattr(obj,i))"""
        if code != 0:
            Window.alert("Internal server error")
            """for o in code, error, request_info:
                dump(o)"""
        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", "addContact", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", "getPresenceStatus",
                         "joinMUC", "getRoomJoined", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", "tarotGameContratChoosed",
                         "tarotGamePlayCards", "getWaitingSub", "subscription", "delContact", "updateContact"])

class BridgeSignals(LiberviaJsonProxy):
    def __init__(self):
        LiberviaJsonProxy.__init__(self, "/json_signal_api",
                        ["getSignals"])

class MicroblogEntry():
    def __init__(self, data):
        self.id = data['id']
        self.content = data['content']
        self.author = data['author']
        self.timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here
        

class SatWebFrontend:
    def onModuleLoad(self):
        self.whoami = None
        self.bridge = BridgeCall()
        self.bridge_signals = BridgeSignals()
        self.selected = None
        self.uni_box = None
        self.status_panel = panels.StatusPanel(self)
        self.contact_panel = ContactPanel(self)
        self.panel = panels.MainPanel(self)
        self.discuss_panel = self.panel.discuss_panel
        self.tab_panel = self.panel.tab_panel 
        self.mpanels = [panels.EmptyPanel(self), panels.MicroblogPanel(self, accept_all=True), panels.EmptyPanel(self)]
        self.other_panels = [] #panels not on the main tab #FIXME: temporary, need to be changed
        self.room_list = set() #set of rooms 
        self.mblog_cache = [] #used to keep blog entries in memory, to show them in new mblog panel
        self.discuss_panel.addWidget(self.mpanels[0])
        self.discuss_panel.addWidget(self.mpanels[1])
        self.discuss_panel.addWidget(self.mpanels[2])
        self._register_box = None
        RootPanel().add(self.panel)
        DOM.addEventPreview(self)
        self.resize()
        self._register = RegisterCall()
        self._register.call('isRegistered',self._isRegisteredCB)

    def resize(self):
        """Resize elements"""
        Window.onResize()

    def onEventPreview(self, event):
        if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE:
            #needed to prevent request cancellation in Firefox
            event.preventDefault()
        return True

    def setUniBox(self, unibox):
        """register the unibox widget"""
        self.uni_box = unibox
        self.uni_box.addKey("@@: ")

    def select(self, widget):
        """Define the selected widget"""
        if self.selected:
            if self.selected == widget:
                return
            self.selected.removeStyleName('selected_widget')
        self.selected = widget
        if widget:
            self.selected.addStyleName('selected_widget')

    def addTab(self, panel, label):
        """Add a panel in a tab
        @param panel: panel to add
        @param label: label of the tab"""
        self.tab_panel.add(panel, label)
        self.other_panels.append(panel)

    def _isRegisteredCB(self, registered):
        if not registered:
            self._register_box = RegisterBox(self.logged)
            self._register_box.centerBox()
            self._register_box.show()
        else:
            self._register.call('isConnected', self._isConnectedCB)

    def _isConnectedCB(self, connected):
        if not connected:
            self._register.call('connect',lambda x:self.logged())
        else:
            self.logged()

    def logged(self):
        if self._register_box:
            self._register_box.hide()
            del self._register_box # don't work if self._register_box is None
        
        #it's time to fill the page
        self.bridge.call('getContacts', self._getContactsCB)
        self.bridge_signals.call('getSignals', self._getSignalsCB)
        #We want to know our own jid
        self.bridge.call('getProfileJid', self._getProfileJidCB)
        

    def _getContactsCB(self, contacts_data):
        for contact in contacts_data:
            jid, attributes, groups = contact
            self._newContactCb(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]))
        name,args = signal_data
        if name == 'personalEvent':
            self._personalEventCb(*args)
        elif name == 'newMessage':
            self._newMessageCb(*args)
        elif name == 'presenceUpdate':
            self._presenceUpdateCb(*args)
        elif name == 'roomJoined':
            self._roomJoinedCb(*args)
        elif name == 'roomUserJoined':
            self._roomUserJoinedCb(*args)
        elif name == 'roomUserLeft':
            self._roomUserLeftCb(*args)
        elif name == 'tarotGameStarted':
            self._tarotGameStartedCb(*args)
        elif name == 'tarotGameNew' or \
             name == 'tarotGameChooseContrat' or \
             name == 'tarotGameShowCards' or \
             name == 'tarotGameInvalidCards' or \
             name == 'tarotGameCardsPlayed' or \
             name == 'tarotGameYourTurn' or \
             name == 'tarotGameScore':
            self._tarotGameGenericCb(name, args[0], args[1:])
        elif name == 'subscribe':
            self._subscribeCb(*args)
        elif name == 'contactDeleted':
            self._contactDeletedCb(*args)
        elif name == 'newContact':
            self._newContactCb(*args)

    def _getProfileJidCB(self, jid):
        self.whoami = JID(jid)
        #we can now ask our status
        self.bridge.call('getPresenceStatus', self._getPresenceStatusCb)
        #the rooms where we are
        self.bridge.call('getRoomJoined', self._getRoomJoinedCb)
        #and if there is any subscription request waiting for us
        self.bridge.call('getWaitingSub', self._getWaitingSubCb)

    ## Signals callbacks ##

    def _personalEventCb(self, sender, event_type, data):
        if event_type == "MICROBLOG":
            if not data.has_key('content'):
                print ("WARNING: No content found in microblog data")
                return
            if data.has_key('groups'):
                _groups = set(data['groups'].split() if data['groups'] else [])
            else:
                _groups=None
            mblog_entry = MicroblogEntry(data)
            
            for panel in self.mpanels:
                if isinstance(panel, panels.MicroblogPanel):
                    self.addBlogEntry(panel, sender, _groups, mblog_entry) 

            self.mblog_cache.append((sender, _groups, mblog_entry))
            if len(self.mblog_cache) > MAX_MBLOG_CACHE:
                del self.mblog_cache[0:len(self.mblog_cache-MAX_MBLOG_CACHE)]

    def addBlogEntry(self, mblog_panel, sender, _groups, mblog_entry):
        """Check if an entry can go in MicroblogPanel and add to it
        @param mblog_panel: MicroblogPanel instance
        @param sender: jid of the entry sender
        @param _groups: groups which can receive this entry
        @param mblog_entry: MicroblogEntry instance"""
        if mblog_panel.isJidAccepted(sender) or (_groups == None and self.whoami and sender == self.whoami.bare) \
           or (_groups and _groups.intersection(mblog_panel.accepted_groups)):
            mblog_panel.addEntry(mblog_entry)

    def FillMicroblogPanel(self, mblog_entry):
        """Fill a microblog panel with entries in cache
        @param mblog_panel: MicroblogPanel instance
        """
        for cache_entry in self.mblog_cache:
            self.addBlogEntry(mblog_entry, *cache_entry)

    def _newMessageCb(self, from_jid, msg, msg_type, to_jid):
        _from = JID(from_jid)
        _to = JID(to_jid)
        showed = False
        for panel in self.mpanels + self.other_panels:
            if isinstance(panel,panels.ChatPanel) and (panel.target.bare == _from.bare or panel.target.bare == _to.bare):
                panel.printMessage(_from, msg)
                showed = True
        if not showed:
            #The message has not been showed, we must indicate it
            other = _to if _from.bare == self.whoami.bare else _from
            self.contact_panel.setContactMessageWaiting(other.bare, True)
            


    def _presenceUpdateCb(self, entity, show, priority, statuses):
        _entity = JID(entity)
        #XXX: QnD way to get our status
        if self.whoami and self.whoami.bare == _entity.bare and statuses:
            self.status_panel.changeStatus(statuses.values()[0])
        if (not self.whoami or self.whoami.bare != _entity.bare):
            self.contact_panel.setConnected(_entity.bare, _entity.resource, show, priority, statuses)

    def _roomJoinedCb(self, room_id, room_service, room_nicks, user_nick):
        _target = JID("%s@%s" % (room_id,room_service))
        self.room_list.add(_target)
        chat_panel = panels.ChatPanel(self, _target, type='group')
        chat_panel.setUserNick(user_nick)
        if room_id.startswith('sat_tarot_'): #XXX: it's not really beautiful, but it works :)
            self.addTab(chat_panel, "Tarot")
        else:
            self.addTab(chat_panel, str(_target))
        chat_panel.setPresents(room_nicks)
        chat_panel.historyPrint()

    def _roomUserJoinedCb(self, room_id, room_service, user_nick, user_data):
        for panel in self.mpanels + self.other_panels:
            if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == "%s@%s" % (room_id, room_service):
                panel.userJoined(user_nick, user_data)

    def _roomUserLeftCb(self, room_id, room_service, user_nick, user_data):
        for panel in self.mpanels + self.other_panels:
            if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == "%s@%s" % (room_id, room_service):
                panel.userLeft(user_nick, user_data)
        
    def _tarotGameStartedCb(self, room_jid, referee, players):
        print ("Tarot Game Started \o/")
        for panel in self.mpanels + self.other_panels:
            if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid:
                panel.startGame("Tarot", referee, players)

    def _tarotGameGenericCb(self, event_name, room_jid, args):
        for panel in self.mpanels + self.other_panels:
            if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid:
                getattr(panel.getGame("Tarot"), event_name)(*args) 

    def _getPresenceStatusCb(self, presence_data):
        for entity in presence_data:
            for resource in presence_data[entity]:
                args = presence_data[entity][resource]
                self._presenceUpdateCb("%s/%s" % (entity, resource), *args)

    def _getRoomJoinedCb(self, room_data):
        for room in room_data:
            self._roomJoinedCb(*room)

    def _getWaitingSubCb(self, waiting_sub):
        for sub in waiting_sub:
            self._subscribeCb(waiting_sub[sub], sub)

    def _subscribeCb(self, sub_type, entity):
        if sub_type == 'subscribed':
            dialog.InfoDialog('Subscription confirmation', 'The contact <b>%s</b> has added you to his/her contact list' % html_sanitize(entity)).show()

        elif sub_type == 'unsubscribed':
            dialog.InfoDialog('Subscription refusal', 'The contact <b>%s</b> has refused to add you in his/her contact list' % html_sanitize(entity)).show()

        elif sub_type == 'subscribe':
            #The user want to subscribe to our presence
            _dialog = None
            msg = HTML('The contact <b>%s</b> want to add you in his/her contact list, do you accept ?' % html_sanitize(entity))
            
            def ok_cb(ignore):
                self.bridge.call('subscription', None, "subscribed", entity, '', _dialog.getSelectedGroups())
            def cancel_cb(ignore):
                self.bridge.call('subscription', None, "unsubscribed", entity, '', '')
            
            _dialog = dialog.GroupSelector([msg], self.contact_panel.getGroups(), [], ok_cb, cancel_cb)
            _dialog.setHTML('<b>Add contact request</b>')
            _dialog.show()

    def _contactDeletedCb(self, entity):
        self.contact_panel.removeContact(entity)

    def _newContactCb(self, contact, attributes, groups):
        self.contact_panel.updateContact(contact, attributes, groups)

if __name__ == '__main__':
    pyjd.setup("http://localhost:8080/libervia.html")
    app = SatWebFrontend()
    app.onModuleLoad()
    pyjd.run()