view src/browser/libervia_main.py @ 650:7e3cdc39c3e7 frontends_multi_profiles

browser_side: fixes misc issues (forgotten parenthesis and wrong argument)
author souliane <souliane@mailoo.org>
date Fri, 27 Feb 2015 00:56:08 +0100
parents 6d3142b782c3
children 2df91d0308ac
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Libervia: a Salut à Toi frontend
# Copyright (C) 2011, 2012, 2013, 2014 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/>.


### logging configuration ###
from sat_browser import logging
logging.configure()
from sat.core.log import getLogger
log = getLogger(__name__)
###

from sat_frontends.quick_frontend.quick_app import QuickApp
from sat_frontends.quick_frontend import quick_widgets

from sat_frontends.tools.misc import InputHistory
from sat_frontends.tools import strings
from sat_frontends.tools import jid
from sat.core.i18n import _

from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HTML import HTML
from pyjamas.ui.KeyboardListener import KEY_ESCAPE
from pyjamas.Timer import Timer
from pyjamas import Window, DOM

from sat_browser import json
from sat_browser import register
from sat_browser.contact_list import ContactList
from sat_browser import widget
from sat_browser import main_panel
from sat_browser import blog
from sat_browser import dialog
from sat_browser import xmlui
from sat_browser import html_tools
from sat_browser import notification
from sat_browser import libervia_widget

from sat_browser.constants import Const as C
import os.path


try:
    # FIXME: import plugin dynamically
    from sat_browser import plugin_sec_otr
except ImportError:
    pass


unicode = str  # FIXME: pyjamas workaround


MAX_MBLOG_CACHE = 500  # Max microblog entries kept in memories

# Set to true to not create a new LiberviaWidget when a similar one
# already exist (i.e. a chat panel with the same target). Instead
# the existing widget will be eventually removed from its parent
# and added to new libervia_widget.WidgetsPanel, or replaced to the expected
# position if the previous and the new parent are the same.
REUSE_EXISTING_LIBERVIA_WIDGETS = True


class SatWebFrontend(InputHistory, QuickApp):
    def onModuleLoad(self):
        log.info("============ onModuleLoad ==============")
        self.bridge_signals = json.BridgeSignals(self)
        QuickApp.__init__(self, json.BridgeCall)
        self.uni_box = None # FIXME: to be removed
        self.status_panel = HTML('<br />')
        self.panel = main_panel.MainPanel(self)
        self.tab_panel = self.panel.tab_panel
        self.tab_panel.addTabListener(self)
        self._register_box = None
        RootPanel().add(self.panel)

        self.notification = notification.Notification()
        DOM.addEventPreview(self)
        self.importPlugins()
        self._register = json.RegisterCall()
        self._register.call('getMenus', self.gotMenus)
        self._register.call('registerParams', None)
        self._register.call('isRegistered', self._isRegisteredCB)
        self.initialised = False
        self.init_cache = []  # used to cache events until initialisation is done
        self.cached_params = {}

        #FIXME: microblog cache should be managed directly in blog module
        self.mblog_cache = []  # used to keep our own blog entries in memory, to show them in new mblog panel

        # self._selected_listeners = set() # FIXME: to be done with new listeners mechanism

    @property
    def whoami(self):
        # XXX: works because Libervia is mono-profile
        #      if one day Libervia manage several profiles at once, this must be deleted
        return self.profiles[C.PROF_KEY_NONE].whoami

    @property
    def contact_list(self):
        return self.contact_lists[C.PROF_KEY_NONE]

    @property
    def visible_widgets(self):
        widgets_panel = self.tab_panel.getCurrentPanel()
        return [wid for wid in widgets_panel.widgets if isinstance(wid, quick_widgets.QuickWidget)]

    @property
    def base_location(self):
        """Return absolute base url of this Libervia instance"""
        url = Window.getLocation().getHref()
        if url.endswith(C.LIBERVIA_MAIN_PAGE):
            url = url[:-len(C.LIBERVIA_MAIN_PAGE)]
        if url.endswith("/"):
            url = url[:-1]
        return url

    def registerSignal(self, functionName, handler=None, iface="core", with_profile=True):
        if handler is None:
            callback = getattr(self, "{}{}".format(functionName, "Handler"))
        else:
            callback = handler

        self.bridge_signals.register(functionName, callback, with_profile=with_profile)

    def importPlugins(self):
        self.plugins = {}
        inhibited_menus = []
        # FIXME: plugins import should be dynamic and generic like in sat
        try:
            self.plugins['otr'] = plugin_sec_otr.OTR(self)
        except TypeError:  # plugin_sec_otr has not been imported
            inhibited_menus.append('OTR')

        class DummyPlugin(object):
            def inhibitMenus(self):
                return inhibited_menus

        self.plugins['dummy_plugin'] = DummyPlugin()

    # def addSelectedListener(self, callback):
    #     self._selected_listeners.add(callback)

    def getSelected(self):
        wid = self.tab_panel.getCurrentPanel()
        if not isinstance(wid, libervia_widget.WidgetsPanel):
            log.error("Tab widget is not a WidgetsPanel, can't get selected widget")
            return None
        return wid.selected

    def setSelected(self, widget):
        """Define the selected widget"""
        widgets_panel = self.tab_panel.getCurrentPanel()
        if not isinstance(widgets_panel, libervia_widget.WidgetsPanel):
            return

        selected = widgets_panel.selected

        if selected == widget:
            return

        if selected:
            selected.removeStyleName('selected_widget')

        # FIXME: check that widget is in the current WidgetsPanel
        widgets_panel.selected = widget
        self.selected_widget = widget

        if widget:
            widgets_panel.selected.addStyleName('selected_widget')

        # FIXME:
        # for callback in self._selected_listeners:
        #     callback(widget)

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

    def onBeforeTabSelected(self, sender, tab_index):
        return True

    def onTabSelected(self, sender, tab_index):
        pass
        # selected = self.getSelected()
        # FIXME:
        # for callback in self._selected_listeners:
        #     callback(selected)

    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

    # FIXME: must not call _entityDataUpdatedCb by itself
    #        should not get VCard, backend plugin must be fixed too
    def getAvatarURL(self, jid_):
        """Return avatar of a jid if in cache, else ask for it.

        @param jid_ (jid.JID): JID of the contact
        @return: the URL to the avatar (str)
        """
        assert isinstance(jid_, jid.JID)
        contact_list = self.contact_list  # pyjamas issue: need a temporary variable to call a property's method
        avatar_hash = contact_list.getCache(jid_, 'avatar')
        if avatar_hash is None:
            # we have no value for avatar_hash, so we request the vcard
            self.bridge.getCard(unicode(jid_), profile=C.PROF_KEY_NONE)
        if not avatar_hash:
            return C.DEFAULT_AVATAR_URL
        ret = os.path.join(C.AVATARS_DIR, avatar_hash)
        return ret

    def registerWidget(self, wid):
        log.debug("Registering %s" % wid.getDebugName())
        self.libervia_widgets.add(wid)

    def unregisterWidget(self, wid):
        try:
            self.libervia_widgets.remove(wid)
        except KeyError:
            log.warning('trying to remove a non registered Widget: %s' % wid.getDebugName())

    def refresh(self):
        """Refresh the general display."""
        self.panel.refresh()
        for lib_wid in self.libervia_widgets:
            lib_wid.refresh()
        self.resize()

    def addWidget(self, wid, tab_index=None):
        """ Add a widget at the bottom of the current or specified tab

        @param wid: LiberviaWidget to add
        @param tab_index: index of the tab to add the widget to
        """
        if tab_index is None or tab_index < 0 or tab_index >= self.tab_panel.getWidgetCount():
            panel = self.tab_panel.getCurrentPanel()
        else:
            panel = self.tab_panel.deck.getWidget(tab_index)
        panel.addWidget(wid)

    def displayNotification(self, title, body):
        self.notification.notify(title, body)

    def gotMenus(self, menus):
        """Put the menus data in cache and build the main menu bar

        @param menus (list[tuple]): menu data
        """
        def process(menus, inhibited=None):
            for raw_menu in menus:
                id_, type_, path, path_i18n = raw_menu
                if inhibited and path[0] in inhibited:
                    continue
                menus_data = self.menus.setdefault(type_, [])
                menus_data.append((id_, path, path_i18n))

        self.menus = {}
        inhibited = set() # FIXME
        extras = []
        for plugin in self.plugins.values():
            if hasattr(plugin, "inhibitMenus"):
                inhibited.update(plugin.inhibitMenus())
            if hasattr(plugin, "extraMenus"):
                extras.extend(plugin.extraMenus())
        process(menus, inhibited)
        process(extras)
        self.panel.menu.createMenus()

    def _isRegisteredCB(self, result):
        registered, warning = result
        if not registered:
            self._register_box = register.RegisterBox(self.logged)
            self._register_box.centerBox()
            self._register_box.show()
            if warning:
                dialog.InfoDialog(_('Security warning'), warning).show()
            self._tryAutoConnect(skip_validation=not not warning)
        else:
            self._register.call('isConnected', self._isConnectedCB)

    def _isConnectedCB(self, connected):
        if not connected:
            self._register.call('asyncConnect', 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

        # display the real presence status panel
        self.panel.header.remove(self.status_panel)
        self.status_panel = main_panel.PresenceStatusPanel(self)
        self.panel.header.add(self.status_panel)

        self.bridge_signals.call('getSignals', self.bridge_signals.signalHandler)

        #it's time to fill the page
        # self.bridge.call('getContacts', self._getContactsCB)
        # self.bridge.call('getParamsUI', self._getParamsUICB)
        # self.bridge_signals.call('getSignals', self._getSignalsCB)
        # #We want to know our own jid
        # self.bridge.call('getProfileJid', self._getProfileJidCB)

        def domain_cb(value):
            self._defaultDomain = value
            log.info("new account domain: %s" % value)

        def domain_eb(value):
            self._defaultDomain = "libervia.org"

        self.bridge.getNewAccountDomain(callback=domain_cb, errback=domain_eb)
        self.plug_profiles([C.PROF_KEY_NONE]) # XXX: None was used intitially, but pyjamas bug when using variable arguments and None is the only arg.
        microblog_widget = self.displayWidget(blog.MicroblogPanel, ())
        self.setSelected(microblog_widget)
        # self.discuss_panel.addWidget(panel.MicroblogPanel(self, []))

        # # get cached params and refresh the display
        # def param_cb(cat, name, count):
        #     count[0] += 1
        #     refresh = count[0] == len(C.CACHED_PARAMS)
        #     return lambda value: self._paramUpdate(name, value, cat, refresh)

        # count = [0]  # used to do something similar to DeferredList
        # for cat, name in C.CACHED_PARAMS:
        #     self.bridge.call('asyncGetParamA', param_cb(cat, name, count), name, cat)

    def profilePlugged(self, dummy):
        QuickApp.profilePlugged(self, dummy)
        # we fill the panels already here
        for wid in self.widgets.getWidgets(blog.MicroblogPanel):
            if wid.accept_all():
                self.bridge.getMassiveLastMblogs('ALL', [], 10, profile=C.PROF_KEY_NONE, callback=wid.massiveInsert)
            else:
                self.bridge.getMassiveLastMblogs('GROUP', wid.accepted_groups, 10, profile=C.PROF_KEY_NONE, callback=wid.massiveInsert)

        #we ask for our own microblogs:
        self.bridge.getMassiveLastMblogs('JID', [unicode(self.whoami.bare)], 10, profile=C.PROF_KEY_NONE, callback=self._ownBlogsFills)

        # initialize plugins which waited for the connection to be done
        for plugin in self.plugins.values():
            if hasattr(plugin, 'profileConnected'):
                plugin.profileConnected()

    def addContactList(self, dummy):
        contact_list = ContactList(self)
        self.panel.addContactList(contact_list)
        return contact_list

    def newWidget(self, wid):
        log.debug("newWidget: {}".format(wid))
        self.addWidget(wid)

    def newMessageHandler(self, from_jid_s, msg, type_, to_jid_s, extra, profile):
        if type_ == C.MESS_TYPE_HEADLINE:
            from_jid = jid.JID(from_jid_s)
            if from_jid.domain == self._defaultDomain:
                # we display announcement from the server in a dialog for better visibility
                try:
                    title = extra['subject']
                except KeyError:
                    title = _('Announcement from %s') % from_jid
                msg = strings.addURLToText(html_tools.XHTML2Text(msg))
                dialog.InfoDialog(title, msg).show()
                return
        QuickApp.newMessageHandler(self, from_jid_s, msg, type_, to_jid_s, extra, profile)

    def disconnectedHandler(self, profile):
        QuickApp.disconnectedHandler(self, profile)
        Window.getLocation().reload()

    def setStatusOnline(self, online=True, show="", statuses={}, profile=C.PROF_KEY_NONE):
        log.warning("setStatusOnline is not implemented, as session are for unique profile which is always online for now")

    def _tryAutoConnect(self, skip_validation=False):
        """This method retrieve the eventual URL parameters to auto-connect the user.
        @param skip_validation: if True, set the form values but do not validate it
        """
        params = strings.getURLParams(Window.getLocation().getSearch())
        if "login" in params:
            self._register_box._form.right_side.selectTab(0)
            self._register_box._form.login_box.setText(params["login"])
            self._register_box._form.login_pass_box.setFocus(True)
            if "passwd" in params:
                # try to connect
                self._register_box._form.login_pass_box.setText(params["passwd"])
                if not skip_validation:
                    self._register_box._form.onLogin(None)
                return True
            else:
                # this would eventually set the browser saved password
                Timer(5, lambda: self._register_box._form.login_pass_box.setFocus(True))

    def _actionCb(self, data):
        if not data:
            # action was a one shot, nothing to do
            pass
        elif "xmlui" in data:
            ui = xmlui.create(self, xml_data=data['xmlui'])
            ui.show()
        elif "public_blog" in data:
            # TODO: use the bare instead of node when all blogs can be retrieved
            node = jid.JID(data['public_blog']).node
            # FIXME: "/blog/{}" won't work with unicode nodes
            self.displayWidget(widget.WebWidget, "/blog/{}".format(node), show_url=False, new_tab="{}'s blog".format(unicode(node)))
        else:
            dialog.InfoDialog("Error",
                              "Unmanaged action result", Width="400px").center()

    def _actionEb(self, err_data):
        err_code, err_obj = err_data
        dialog.InfoDialog("Error",
                          str(err_obj), Width="400px").center()

    def launchAction(self, callback_id, data):
        """ Launch a dynamic action
        @param callback_id: id of the action to launch
        @param data: data needed only for certain actions

        """
        if data is None:
            data = {}
        self.bridge.launchAction(callback_id, data, profile=C.PROF_KEY_NONE, callback=self._actionCb, errback=self._actionEb)

    def _getContactsCB(self, contacts_data):
        for contact_ in contacts_data:
            jid, attributes, groups = contact_
            self._newContactCb(jid, attributes, groups)

    def _getParamsUICB(self, xml_ui):
        """Hide the parameters item if there's nothing to display"""
        if not xml_ui:
            self.panel.menu.removeItemParams()

    def _ownBlogsFills(self, mblogs):
        #put our own microblogs in cache, then fill all panels with them
        for publisher in mblogs:
            for mblog in mblogs[publisher]:
                if 'content' not in mblog:
                    log.warning("No content found in microblog [%s]" % mblog)
                    continue
                if 'groups' in mblog:
                    _groups = set(mblog['groups'].split() if mblog['groups'] else [])
                else:
                    _groups = None
                mblog_entry = blog.MicroblogItem(mblog)
                self.mblog_cache.append((_groups, mblog_entry))

        if len(self.mblog_cache) > MAX_MBLOG_CACHE:
            del self.mblog_cache[0:len(self.mblog_cache - MAX_MBLOG_CACHE)]
        for wid in self.widgets.getWidgets(blog.MicroblogPanel):
            self.FillMicroblogPanel(wid)

        # FIXME
        self.initialised = True  # initialisation phase is finished here
        for event_data in self.init_cache:  # so we have to send all the cached events
            self._personalEventCb(*event_data)
        del self.init_cache

    def _getProfileJidCB(self, jid_s):
        # FIXME
        raise Exception("should not be here !")
        # self.whoami = jid.JID(jid_s)
        # #we can now ask our status
        # self.bridge.call('getPresenceStatuses', self._getPresenceStatusesCb)
        # #the rooms where we are
        # self.bridge.call('getRoomsJoined', self._getRoomsJoinedCb)
        # #and if there is any subscription request waiting for us
        # self.bridge.call('getWaitingSub', self._getWaitingSubCb)
        # #we fill the panels already here
        # for lib_wid in self.libervia_widgets:
        #     if isinstance(lib_wid, panel.MicroblogPanel):
        #         if lib_wid.accept_all():
        #             self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'ALL', [], 10)
        #         else:
        #             self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups, 10)

        # #we ask for our own microblogs:
        # self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10)

        # # initialize plugins which waited for the connection to be done
        # for plugin in self.plugins.values():
        #     if hasattr(plugin, 'profileConnected'):
        #         plugin.profileConnected()

    ## Signals callbacks ##

    def _personalEventCb(self, sender, event_type, data):
        if not self.initialised:
            self.init_cache.append((sender, event_type, data))
            return
        sender = jid.JID(sender).bare
        if event_type == "MICROBLOG":
            if not 'content' in data:
                log.warning("No content found in microblog data")
                return
            if 'groups' in data:
                _groups = set(data['groups'].split() if data['groups'] else [])
            else:
                _groups = None
            mblog_entry = blog.MicroblogItem(data)

            for wid in self.widgets.getWidgets(blog.MicroblogPanel):
                wid.addEntryIfAccepted(sender, _groups, mblog_entry)

            if sender == self.whoami.bare:
                found = False
                for index in xrange(0, len(self.mblog_cache)):
                    entry = self.mblog_cache[index]
                    if entry[1].id == mblog_entry.id:
                        # replace existing entry
                        self.mblog_cache.remove(entry)
                        self.mblog_cache.insert(index, (_groups, mblog_entry))
                        found = True
                        break
                if not found:
                    self.mblog_cache.append((_groups, mblog_entry))
                    if len(self.mblog_cache) > MAX_MBLOG_CACHE:
                        del self.mblog_cache[0:len(self.mblog_cache - MAX_MBLOG_CACHE)]
        elif event_type == 'MICROBLOG_DELETE':
            for wid in self.widgets.getWidgets(blog.MicroblogPanel):
                wid.removeEntry(data['type'], data['id'])
            log.debug("%s %s %s" % (self.whoami.bare, sender, data['type']))

            if sender == self.whoami.bare and data['type'] == 'main_item':
                for index in xrange(0, len(self.mblog_cache)):
                    entry = self.mblog_cache[index]
                    if entry[1].id == data['id']:
                        self.mblog_cache.remove(entry)
                        break

    def FillMicroblogPanel(self, mblog_panel):
        """Fill a microblog panel with entries in cache

        @param mblog_panel: MicroblogPanel instance
        """
        #XXX: only our own entries are cached
        for cache_entry in self.mblog_cache:
            _groups, mblog_entry = cache_entry
            mblog_panel.addEntryIfAccepted(self.whoami.bare, *cache_entry)

    def getEntityMBlog(self, entity):
        log.info("geting mblog for entity [%s]" % (entity,))
        for lib_wid in self.libervia_widgets:
            if isinstance(lib_wid, blog.MicroblogPanel):
                if lib_wid.isJidAccepted(entity):
                    self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10)

    # def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True):
    #     """Get the corresponding panel if it exists.
    #     @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...)
    #     @param entity (dict): dictionnary to define the entity.
    #     @param ignoreOtherTabs (bool): if True, the widgets that are not
    #     contained by the currently selected tab will be ignored
    #     @return: the existing widget that has been found or None."""
    #     selected_tab = self.tab_panel.getCurrentPanel()
    #     for lib_wid in self.libervia_widgets:
    #         parent = lib_wid.getWidgetsPanel(expect=False)
    #         if parent is None or (ignoreOtherTabs and parent != selected_tab):
    #             # do not return a widget that is not in the currently selected tab
    #             continue
    #         if isinstance(lib_wid, class_):
    #             try:
    #                 if lib_wid.matchEntity(*(entity.values())):  # XXX: passing **entity bugs!
    #                     log.debug("existing widget found: %s" % lib_wid.getDebugName())
    #                     return lib_wid
    #             except AttributeError as e:
    #                 e.stack_list()
    #                 return None
    #     return None

    def displayWidget(self, class_, target, dropped=False, new_tab=None, *args, **kwargs):
        """Get or create a LiberviaWidget and select it. When the user dropped
        something, a new widget is always created, otherwise we look for an
        existing widget and re-use it if it's in the current tab.

        @arg class_(class): see quick_widgets.getOrCreateWidget
        @arg target: see quick_widgets.getOrCreateWidget
        @arg dropped(bool): if True, assume the widget has been dropped
        @arg new_tab(unicode): if not None, it holds the name of a new tab to
            open for the widget. If None, use the default behavior.
        @param args(list): optional args to create a new instance of class_
        @param kwargs(list): optional kwargs to create a new instance of class_
        @return: the widget
        """
        kwargs['profile'] = C.PROF_KEY_NONE

        if dropped:
            kwargs['on_new_widget'] = None
            kwargs['on_existing_widget'] = C.WIDGET_RECREATE
            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
            self.setSelected(wid)
            return wid

        if new_tab:
            kwargs['on_new_widget'] = None
            kwargs['on_existing_widget'] = C.WIDGET_RECREATE
            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
            self.tab_panel.addWidgetsTab(new_tab)
            self.addWidget(wid)
            return wid

        kwargs['on_existing_widget'] = C.WIDGET_RAISE
        try:
            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
        except quick_widgets.WidgetAlreadyExistsError:
            kwargs['on_existing_widget'] = C.WIDGET_KEEP
            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
            widgets_panel = wid.getParent(libervia_widget.WidgetsPanel, expect=False)
            if widgets_panel is None:
                # The widget exists but is hidden
                self.addWidget(wid)
            elif widgets_panel != self.tab_panel.getCurrentPanel():
                # the widget is on an other tab, so we add a new one here
                kwargs['on_existing_widget'] = C.WIDGET_RECREATE
                wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
                self.addWidget(wid)
        self.setSelected(wid)
        return wid


    # def getOrCreateLiberviaWidget(self, class_, entity, select=True, new_tab=None):
    #     """Get the matching LiberviaWidget if it exists, or create a new one.
    #     @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...)
    #     @param entity (dict): dictionnary to define the entity.
    #     @param select (bool): if True, select the widget that has been found or created
    #     @param new_tab (str): if not None, a widget which is created is created in
    #     a new tab. In that case new_tab is a unicode to label that new tab.
    #     If new_tab is not None and a widget is found, no tab is created.
    #     @return: the newly created wigdet if REUSE_EXISTING_LIBERVIA_WIDGETS
    #      is set to False or if the widget has not been found, the existing
    #      widget that has been found otherwise."""
    #     lib_wid = None
    #     tab = None
    #     if REUSE_EXISTING_LIBERVIA_WIDGETS:
    #         lib_wid = self.getLiberviaWidget(class_, entity, new_tab is None)
    #     if lib_wid is None:  # create a new widget
    #         lib_wid = class_.createPanel(self, *(entity.values()))  # XXX: passing **entity bugs!
    #         if new_tab is None:
    #             self.addWidget(lib_wid)
    #         else:
    #             tab = self.addTab(new_tab, lib_wid, False)
    #     else:  # reuse existing widget
    #         tab = lib_wid.getWidgetsPanel(expect=False)
    #         if new_tab is None:
    #             if tab is not None:
    #                 tab.removeWidget(lib_wid)
    #             self.addWidget(lib_wid)
    #     if select:
    #         if new_tab is not None:
    #             self.tab_panel.selectTab(tab)
    #         # must be done after the widget is added,
    #         # for example to scroll to the bottom
    #         self.setSelected(lib_wid)
    #         lib_wid.refresh()
    #     return lib_wid

    # def getRoomWidget(self, target):
    #     """Get the MUC widget for the given target.

    #     @param target (jid.JID): BARE jid of the MUC
    #     @return: panel.ChatPanel instance or None
    #     """
    #     entity = {'item': target, 'type_': 'group'}
    #     if target.full() in self.room_list or target in self.room_list:  # as JID is a string-based class, we don't know what will please Pyjamas...
    #         return self.getLiberviaWidget(panel.ChatPanel, entity, ignoreOtherTabs=False)
    #     return None

    # def getOrCreateRoomWidget(self, target):
    #     """Get the MUC widget for the given target, create it if necessary.

    #     @param target (jid.JID): BARE jid of the MUC
    #     @return: panel.ChatPanel instance
    #     """
    #     lib_wid = self.getRoomWidget(target)
    #     if lib_wid:
    #         return lib_wid

    #     # XXX: target.node.startwith(...) raises an error "startswith is not a function"
    #     # This happens when node a is property defined in the JID class
    #     # FIXME: pyjamas doesn't handle the properties well
    #     node = target.node

    #     # XXX: it's not really beautiful, but it works :)
    #     if node.startswith('sat_tarot_'):
    #         tab_name = "Tarot"
    #     elif node.startswith('sat_radiocol_'):
    #         tab_name = "Radio collective"
    #     else:
    #         tab_name = target.node

    #     self.room_list.append(target)
    #     entity = {'item': target, 'type_': 'group'}
    #     return self.getOrCreateLiberviaWidget(panel.ChatPanel, entity, new_tab=tab_name)

    # def _newMessageCb(self, from_jid_s, msg, msg_type, to_jid_s, extra):
    #     from_jid = jid.JID(from_jid_s)
    #     to_jid = jid.JID(to_jid_s)
    #     for plugin in self.plugins.values():
    #         if hasattr(plugin, 'messageReceivedTrigger'):
    #             if not plugin.messageReceivedTrigger(from_jid, msg, msg_type, to_jid, extra):
    #                 return  # plugin returned False to interrupt the process
    #     self.newMessageCb(from_jid, msg, msg_type, to_jid, extra)

    # def newMessageCb(self, from_jid, msg, msg_type, to_jid, extra):
    #     other = to_jid if from_jid.bare == self.whoami.bare else from_jid
    #     lib_wid = self.getLiberviaWidget(panel.ChatPanel, {'item': other}, ignoreOtherTabs=False)
    #     self.displayNotification(from_jid, msg)
    #     if msg_type == 'headline' and from_jid.full() == self._defaultDomain:
    #         try:
    #             assert extra['subject']  # subject is defined and not empty
    #             title = extra['subject']
    #         except (KeyError, AssertionError):
    #             title = _('Announcement from %s') % from_jid.full()
    #         msg = strings.addURLToText(html_tools.XHTML2Text(msg))
    #         dialog.InfoDialog(title, msg).show()
    #         return
    #     if lib_wid is not None:
    #         if msg_type == C.MESS_TYPE_INFO:
    #             lib_wid.printInfo(msg)
    #         else:
    #             lib_wid.printMessage(from_jid, msg, extra)
    #         if 'header_info' in extra:
    #             lib_wid.setHeaderInfo(extra['header_info'])
    #     else:
    #         # FIXME: "info" message and header info will be lost here
    #         if not self.contact_panel.isContactInRoster(other.bare):
    #             self.contact_panel.updateContact(other.bare, {}, [C.GROUP_NOT_IN_ROSTER])
    #         # The message has not been shown, we must indicate it
    #         self.contact_panel.setContactMessageWaiting(other.bare, True)

    # def _presenceUpdateCb(self, entity, show, priority, statuses):
    #     entity_jid = jid.JID(entity)
    #     if self.whoami and self.whoami == entity_jid:  # XXX: QnD way to get our presence/status
    #         assert(isinstance(self.status_panel, main_panel.PresenceStatusPanel))
    #         self.status_panel.setPresence(show)  # pylint: disable=E1103
    #         if statuses:
    #             self.status_panel.setStatus(statuses.values()[0])  # pylint: disable=E1103
    #     else:
    #         bare_jid = entity_jid.bareJID()
    #         if bare_jid.full() in self.room_list or bare_jid in self.room_list:  # as JID is a string-based class, we don't know what will please Pyjamas...
    #             wid = self.getRoomWidget(bare_jid)
    #         else:
    #             wid = self.contact_panel
    #             if show == 'unavailable':  # XXX: save some resources as for now we only need 'unavailable'
    #                 for plugin in self.plugins.values():
    #                     if hasattr(plugin, 'presenceReceivedTrigger'):
    #                         plugin.presenceReceivedTrigger(entity_jid, show, priority, statuses)
    #         if wid:
    #             wid.setConnected(entity_jid.bare, entity_jid.resource, show, priority, statuses)

    # def _roomJoinedCb(self, room_jid_s, room_nicks, user_nick):
    #     chat_panel = self.getOrCreateRoomWidget(jid.JID(room_jid_s))
    #     chat_panel.setUserNick(user_nick)
    #     chat_panel.setPresents(room_nicks)
    #     chat_panel.refresh()

    # def _roomLeftCb(self, room_jid_s, room_nicks, user_nick):
    #     try:
    #         del self.room_list[room_jid_s]
    #     except KeyError:
    #         try:  # as JID is a string-based class,  we don't know what will please Pyjamas...
    #             del self.room_list[jid.JID(room_jid_s)]
    #         except KeyError:
    #             pass

    # def _roomUserJoinedCb(self, room_jid_s, user_nick, user_data):
    #     lib_wid = self.getOrCreateRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         lib_wid.userJoined(user_nick, user_data)

    # def _roomUserLeftCb(self, room_jid_s, user_nick, user_data):
    #     lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         lib_wid.userLeft(user_nick, user_data)

    # def _roomUserChangedNickCb(self, room_jid_s, old_nick, new_nick):
    #     """Called when an user joined a MUC room"""
    #     lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         lib_wid.changeUserNick(old_nick, new_nick)

    # def _tarotGameStartedCb(self, waiting, room_jid_s, referee, players):
    #     lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         lib_wid.startGame("Tarot", waiting, referee, players)

    # def _tarotGameGenericCb(self, event_name, room_jid_s, args):
    #     lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         getattr(lib_wid.getGame("Tarot"), event_name)(*args)

    # def _radioColStartedCb(self, waiting, room_jid_s, referee, players, queue_data):
    #     lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         lib_wid.startGame("RadioCol", waiting, referee, players, queue_data)

    # def _radioColGenericCb(self, event_name, room_jid_s, args):
    #     lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
    #     if lib_wid:
    #         getattr(lib_wid.getGame("RadioCol"), event_name)(*args)

    def _getPresenceStatusesCb(self, presence_data):
        for entity in presence_data:
            for resource in presence_data[entity]:
                args = presence_data[entity][resource]
                full = ('%s/%s' % (jid.JID(entity).bare, resource)) if resource else entity
                self._presenceUpdateCb(full, *args)

    def _getRoomsJoinedCb(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_tools.html_sanitize(entity)).show()
            self.getEntityMBlog(entity)

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

        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_tools.html_sanitize(entity))

            def ok_cb(ignore):
                self.bridge.call('subscription', None, "subscribed", entity)
                self.bridge.updateContact(entity, '', _dialog.getSelectedGroups())

            def cancel_cb(ignore):
                self.bridge.call('subscription', None, "unsubscribed", entity, '', '')

            _dialog = dialog.GroupSelector([msg], self.contact_panel.getGroups(), [], "Add", 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_jid, attributes, groups):
        self.contact_panel.updateContact(contact_jid, attributes, groups)

    def _entityDataUpdatedCb(self, entity_jid_s, key, value):
        raise Exception # FIXME should not be here
        if key == "avatar":
            avatar = '/' + C.AVATARS_DIR + value
            self.avatars_cache[entity_jid_s] = avatar
            self.contact_panel.updateAvatar(entity_jid_s, avatar)

            for lib_wid in self.libervia_widgets:
                if isinstance(lib_wid, blog.MicroblogPanel):
                    if lib_wid.isJidAccepted(entity_jid_s) or (self.whoami and entity_jid_s == self.whoami.bare):
                        lib_wid.updateValue('avatar', entity_jid_s, avatar)

    # def _chatStateReceivedCb(self, from_jid_s, state):
    #     """Callback when a new chat state is received.
    #     @param from_jid_s: JID of the contact who sent his state, or '@ALL@'
    #     @param state: new state (string)
    #     """
    #     if from_jid_s == '@ALL@':
    #         for lib_wid in self.libervia_widgets:
    #             if isinstance(lib_wid, panel.ChatPanel):
    #                 lib_wid.setState(state, nick=C.ALL_OCCUPANTS)
    #         return
    #     from_jid = jid.JID(from_jid_s)
    #     lib_wid = self.getLiberviaWidget(panel.ChatPanel, {'item': from_jid}, ignoreOtherTabs=False)
    #     lib_wid.setState(state, nick=from_jid.resource)

    def _askConfirmation(self, confirmation_id, confirmation_type, data):
        answer_data = {}

        def confirm_cb(result):
            self.bridge.call('confirmationAnswer', None, confirmation_id, result, answer_data)

        if confirmation_type == "YES/NO":
            dialog.ConfirmDialog(confirm_cb, text=data["message"], title=data["title"]).show()

    def _newAlert(self, message, title, alert_type):
        dialog.InfoDialog(title, message).show()

    def _paramUpdate(self, name, value, category, refresh=True):
        """This is called when the paramUpdate signal is received, but also
        during initialization when the UI parameters values are retrieved.
        @param refresh: set to True to refresh the general UI
        """
        for param_cat, param_name in C.CACHED_PARAMS:
            if name == param_name and category == param_cat:
                self.cached_params[(category, name)] = value
                if refresh:
                    self.refresh()
                break

    def getCachedParam(self, category, name):
        """Return a parameter cached value (e.g for refreshing the UI)

        @param category (str): the parameter category
        @pram name (str): the parameter name
        """
        return self.cached_params[(category, name)] if (category, name) in self.cached_params else None

    def sendError(self, errorData):
        dialog.InfoDialog("Error while sending message",
                          "Your message can't be sent", Width="400px").center()
        log.error("sendError: %s" % str(errorData))

    # FIXME: this method is fat too complicated and depend of widget type
    #        must be refactored and moved to each widget instead
    # def send(self, targets, text, extra={}):
    #     """Send a message to any target type.
    #     @param targets: list of tuples (type, entities, addr) with:
    #     - type in ("PUBLIC", "GROUP", "COMMENT", "STATUS" , "groupchat" , "chat")
    #     - entities could be a JID, a list groups, a node hash... depending the target
    #     - addr in ("To", "Cc", "Bcc") - ignore case
    #     @param text: the message content
    #     @param extra: options
    #     """
    #     # FIXME: too many magic strings, we should use constants instead
    #     addresses = []
    #     for target in targets:
    #         type_, entities, addr = target[0], target[1], 'to' if len(target) < 3 else target[2].lower()
    #         if type_ in ("PUBLIC", "GROUP"):
    #             self.bridge.call("sendMblog", None, type_, entities if type_ == "GROUP" else None, text, extra)
    #         elif type_ == "COMMENT":
    #             self.bridge.call("sendMblogComment", None, entities, text, extra)
    #         elif type_ == "STATUS":
    #             assert(isinstance(self.status_panel, main_panel.PresenceStatusPanel))
    #             self.bridge.call('setStatus', None, self.status_panel.presence, text)  # pylint: disable=E1103
    #         elif type_ in ("groupchat", "chat"):
    #             addresses.append((addr, entities))
    #         else:
    #             log.error("Unknown target type")
    #     if addresses:
    #         if len(addresses) == 1 and addresses[0][0] == 'to':
    #             to_jid_s = addresses[0][1]
    #             for plugin in self.plugins.values():
    #                 if hasattr(plugin, 'sendMessageTrigger'):
    #                     if not plugin.sendMessageTrigger(jid.JID(to_jid_s), text, type_, extra):
    #                         return  # plugin returned False to interrupt the process
    #             self.bridge.call('sendMessage', (None, self.sendError), to_jid_s, text, '', type_, extra)
    #         else:
    #             extra.update({'address': '\n'.join([('%s:%s' % entry) for entry in addresses])})
    #             self.bridge.call('sendMessage', (None, self.sendError), self.whoami.domain, text, '', type_, extra)

    def showWarning(self, type_=None, msg=None):
        """Display a popup information message, e.g. to notify the recipient of a message being composed.
        If type_ is None, a popup being currently displayed will be hidden.
        @type_: a type determining the CSS style to be applied (see WarningPopup.showWarning)
        @msg: message to be displayed
        """
        if not hasattr(self, "warning_popup"):
            self.warning_popup = main_panel.WarningPopup()
        self.warning_popup.showWarning(type_, msg)

    def showDialog(self, message, title="", type_="info", answer_cb=None, answer_data=None):
        if type_ == 'info':
            popup = dialog.InfoDialog(unicode(title), unicode(message), callback=answer_cb)
        elif type_ == 'error':
            popup = dialog.InfoDialog(unicode(title), unicode(message), callback=answer_cb)
        elif type_ == 'yes/no':
            popup = dialog.ConfirmDialog(lambda answer: answer_cb(answer, answer_data),
                                         text=unicode(message), title=unicode(title))
            popup.cancel_button.setText(_("No"))
        else:
            popup = dialog.InfoDialog(unicode(title), unicode(message), callback=answer_cb)
            log.error(_('unmanaged dialog type: %s'), type_)
        popup.show()


if __name__ == '__main__':
    app = SatWebFrontend()
    app.onModuleLoad()