view browser_side/dialog.py @ 51:9f19e16187ff

browser side: HTML sanitization
author Goffi <goffi@goffi.org>
date Thu, 26 May 2011 23:03:45 +0200
parents f1d2eb9b2523
children 4419ef07bb2b
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/>.
"""

from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.PopupPanel import PopupPanel
from pyjamas.ui.DialogBox import DialogBox
from pyjamas.ui.ListBox import ListBox
from pyjamas.ui.Button import Button
from pyjamas.ui.HTML import HTML
from pyjamas.ui import HasAlignment
from pyjamas.ui.KeyboardListener import KEY_ESCAPE

class ContactsChooser(DialogBox):

    def __init__(self, host, callback, nb_contact=None, text='Please select contacts'):
        """
        ContactsChooser allow to select one or several connected contacts
        @param host: SatWebFrontend instance
        @param callback: method to call when contacts have been choosed
        @param nb_contact: number of contacts that have to be selected, None for no limit
        """
        self.host = host
        self.callback = callback
        self.nb_contact = nb_contact
        DialogBox.__init__(self, centered=True)

        content = VerticalPanel()
        content.setWidth('100%')
        self.contacts_list = ListBox()
        self.contacts_list.setVisibleItemCount(10)
        self.contacts_list.setMultipleSelect(True)
        self.contacts_list.setWidth("100%")
        self.contacts_list.setStyleName('contactsChooser')
        self.contacts_list.addChangeListener(self.onChange)
        content.add(self.contacts_list)
        button_panel = HorizontalPanel()
        self.choose_button = Button("Choose", self.onChoose)
        self.choose_button.setEnabled(False)
        button_panel.add(self.choose_button)
        button_panel.add(Button("Cancel", self.onCancel))
        content.add(button_panel)
        self.setHTML(text)
        self.setWidget(content)	
    
    def onChange(self):
        if self.nb_contact:
            if len(self.contacts_list.getSelectedValues()) == self.nb_contact:
                self.choose_button.setEnabled(True)
            else:
                self.choose_button.setEnabled(False)

    def getContacts(self):
        """
        Actually ask to choose the contacts
        """
        self.contacts_list.clear()
        for contact in self.host.contact_panel.getConnected():
            if contact not in [room.bare for room in self.host.room_list]:
                self.contacts_list.addItem(contact)
        self.show()

    def onChoose(self):
        self.hide()
        self.callback(self.contacts_list.getSelectedValues())

    def onCancel(self):
        self.hide()

class ConfirmDialog(DialogBox):

    def __init__(self, callback, text='Are you sure ?'):
        """
        Dialog to confirm an action
        @param callback: method to call when contacts have been choosed
        """
        self.callback = callback
        DialogBox.__init__(self, centered=True)

        content = VerticalPanel()
        content.setWidth('100%')
        button_panel = HorizontalPanel()
        self.confirm_button = Button("OK", self.onConfirm)
        button_panel.add(self.confirm_button)
        button_panel.add(Button("Cancel", self.onCancel))
        content.add(button_panel)
        self.setHTML(text)
        self.setWidget(content)	
    
    def onConfirm(self):
        self.hide()
        self.callback(True)

    def onCancel(self):
        self.hide()
        self.callback(False)

class InfoDialog(DialogBox):
    """Dialog which just show a widget and a close button"""

    def __init__(self, title, widget, callback = None):
        """Simple notice dialog box
        @param title: HTML put in the header
        @param body: widget put in the body"""
        DialogBox.__init__(self, centered=True)
        self.callback = callback
        _body = VerticalPanel()
        _body.setSize('100%','100%')
        _body.setSpacing(4)
        _body.add(widget)
        _body.setCellWidth(widget, '100%')
        _close_button = Button("Close", self.onClose)
        _body.add(_close_button)
        _body.setCellHorizontalAlignment(_close_button, HasAlignment.ALIGN_CENTER)
        self.setHTML(title)
        self.setWidget(_body)
        self.panel.setWidth('100%')

    def onClose(self):
        self.hide()
        if self.callback:
            self.callback()

class SimpleDialog(InfoDialog):

    def __init__(self, title, body, callback = None):
        InfoDialog.__init__(self, title, HTML(body), callback)

class PopupPanelWrapper(PopupPanel):
    """This wrapper catch Escape event to avoid request cancellation by Firefox"""

    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 PopupPanel.onEventPreview(self, event)