view contact.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
children a663b9955cf3
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.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.Label import Label
from pyjamas import Window
from pyjamas import DOM


class GroupLabel(Label):
    def __init__(self, group):
        Label.__init__(self, group)
        self.group = group
        self.setStyleName('group')

class ContactLabel(Label):
    def __init__(self, jid, name=None):
        if not name:
            name=jid
        Label.__init__(self, name)
        self.jid=jid
        self.setStyleName('contact')

class GroupList(VerticalPanel):

    def __init__(self, parent):
        VerticalPanel.__init__(self)
        self._parent = parent

    def add(self, group):
        _item = GroupLabel(group)
        _item.addMouseListener(self._parent)
        DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
        VerticalPanel.add(self, _item)
    
class ContactList(VerticalPanel):

    def __init__(self):
        VerticalPanel.__init__(self)
        self.contacts=[]

    def __iter__(self):
        return self.contacts.__iter__()
    
    def add(self, jid, name=None):
        _item = ContactLabel(jid, name)
        DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
        VerticalPanel.add(self, _item)
        self.contacts.append(_item)

class ContactPanel(SimplePanel):
    """Manage the contacts and groups"""
    
    def __init__(self, host):
        SimplePanel.__init__(self)
        self.host = host
        self.groups={}

        self.vPanel = VerticalPanel()
        _title = Label('Contacts')
        _title.setStyleName('contactTitle')

        self._contactList = ContactList()
        self._contactList.setStyleName('contactList')
        self._groupList = GroupList(self)
        self._groupList.setStyleName('groupList')
        
        self.vPanel.add(_title)
        self.vPanel.add(self._groupList)
        self.vPanel.add(self._contactList)
        self.add(self.vPanel)
        self.setStyleName('contactBox')

    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")