view frontends/quick_frontend/quick_app.py @ 71:efe81b61673c

quick app: updatedValue wrong parameter fix
author Goffi <goffi@goffi.org>
date Sat, 06 Mar 2010 14:57:23 +1100
parents 8f2ed279784b
children f271fff3a713
line wrap: on
line source

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

"""
helper class for making a SAT frontend
Copyright (C) 2009, 2010  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 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from logging import debug, info, error
from tools.jid  import JID
from sat_bridge_frontend.DBus import DBusBridgeFrontend
import pdb

import gettext
gettext.install('sat_frontend', "../i18n", unicode=True)

class QuickApp():
    """This class contain the main methods needed for the frontend"""

    def __init__(self, single_profile=True):
        self.rosterList = {}
        self.profiles = {}
        self.single_profile = single_profile
        
        ## bridge ##
        self.bridge=DBusBridgeFrontend()
        self.bridge.register("connected", self.connected)
        self.bridge.register("disconnected", self.disconnected)
        self.bridge.register("newContact", self.newContact)
        self.bridge.register("newMessage", self.newMessage)
        self.bridge.register("presenceUpdate", self.presenceUpdate)
        self.bridge.register("subscribe", self.subscribe)
        self.bridge.register("paramUpdate", self.paramUpdate)
        self.bridge.register("contactDeleted", self.contactDeleted)
        self.bridge.register("updatedValue", self.updatedValue, "request")
        self.bridge.register("askConfirmation", self.askConfirmation, "request")
        self.bridge.register("actionResult", self.actionResult, "request")
        self.bridge.register("actionResultExt", self.actionResult, "request")
        
        self.current_action_ids = set()
        self.current_action_ids_cb = {}
    
    def __check_profile(self, profile):
        """Tell if the profile is currently followed by the application"""
        return profile in self.profiles.keys()

    def plug_profile(self, profile_key='@DEFAULT@'):
        """Tell application which profile must be used"""
        if self.single_profile and self.profiles:
            error(_('There is already one profile plugged (we are in single profile mode) !'))
            return
        profile = self.bridge.getProfileName(profile_key)
        if not profile:
            error(_("The profile asked doesn't exist"))
            return
        if self.profiles.has_key(profile):
            warning(_("The profile is already plugged"))
            return
        self.profiles[profile]={}
        if self.single_profile:
            self.profile = profile
        
        ###now we get the essential params###
        self.profiles[profile]['whoami']=JID(self.bridge.getParamA("JabberID","Connection", profile))
        self.profiles[profile]['watched']=self.bridge.getParamA("Watched", "Misc", profile).split() #TODO: put this in a plugin

        ## misc ##
        self.profiles[profile]['onlineContact'] = set()  #FIXME: temporary

        #TODO: managed multi-profiles here
        if self.bridge.isConnected(profile):
            self.setStatusOnline(True)
        else:
            self.setStatusOnline(False)
            return

        ### now we fill the contact list ###
        for contact in self.bridge.getContacts(profile):
            self.newContact(contact[0], contact[1], contact[2], profile)

        presences = self.bridge.getPresenceStatus(profile)
        for contact in presences:
            for res in presences[contact]:
                jabber_id = contact+('/'+res if res else '')
                show = presences[contact][res][0]
                priority = presences[contact][res][1]
                statuses = presences[contact][res][2]
                self.presenceUpdate(jabber_id, show, priority, statuses, profile)

        waitingSub = self.bridge.getWaitingSub(profile)
        for sub in waitingSub:
            self.subscribe(waitingSub[sub], sub, profile)

    def unplug_profile(self, profile):
        """Tell the application to not follow anymore the profile"""
        if not profile in self.profiles:
            warning (_("This profile is not plugged"))
            return
        self.profiles.remove(profile)

    def clear_profile(self):
        self.profiles.clear()

    def connected(self, profile):
        """called when the connection is made"""
        if not self.__check_profile(profile):
            return
        debug(_("Connected"))
        self.setStatusOnline(True)

    def disconnected(self, profile):
        """called when the connection is closed"""
        if not self.__check_profile(profile):
            return
        debug(_("Disconnected"))
        self.CM.clear()
        self.contactList.clear_contacts()
        self.setStatusOnline(False)
    
    def newContact(self, JabberId, attributes, groups, profile):
        if not self.__check_profile(profile):
            return
        entity=JID(JabberId)
        self.rosterList[entity.short]=(dict(attributes), list(groups))
    
    def newMessage(self, from_jid, msg, type, to_jid, profile):
        if not self.__check_profile(profile):
            return
        sender=JID(from_jid)
        addr=JID(to_jid)
        win = addr if sender.short == self.profiles[profile]['whoami'].short else sender
        self.current_action_ids = set()
        self.current_action_ids_cb = {}
        self.chat_wins[win.short].printMessage(sender, msg, profile)

    def setStatusOnline(self, online=True):
        pass

    def presenceUpdate(self, jabber_id, show, priority, statuses, profile):
        if not self.__check_profile(profile):
            return
        print "check ok"
        debug (_("presence update for %(jid)s (show=%(show)s, statuses=%(statuses)s)") % {'jid':jabber_id, 'show':show, 'statuses':statuses});
        from_jid=JID(jabber_id)
        debug ("from_jid.short=%(from_jid)s whoami.short=%(whoami)s" % {'from_jid':from_jid.short, 'whoami':self.profiles[profile]['whoami'].short})

        if from_jid.short==self.profiles[profile]['whoami'].short:
            if not type:
                self.setStatusOnline(True)
            elif type=="unavailable":
                self.setStatusOnline(False)
            return

        if show != 'unavailable':
            name=""
            groups = []
            if self.rosterList.has_key(from_jid.short):
                if self.rosterList[from_jid.short][0].has_key("name"):
                    name=self.rosterList[from_jid.short][0]["name"]
                groups=self.rosterList[from_jid.short][1]

            #FIXME: must be moved in a plugin
            if from_jid.short in self.profiles[profile]['watched'] and not from_jid.short in self.profiles[profile]['onlineContact']:
                self.showAlert(_("Watched jid [%s] is connected !") % from_jid.short)

            self.profiles[profile]['onlineContact'].add(from_jid)  #FIXME onlineContact is useless with CM, must be removed
            self.CM.add(from_jid)
            self.CM.update(from_jid, 'name', name)
            self.CM.update(from_jid, 'show', show)
            self.CM.update(from_jid, 'statuses', statuses)
            self.CM.update(from_jid, 'groups', groups)
            cache = self.bridge.getCardCache(from_jid)
            if cache.has_key('nick'): 
                self.CM.update(from_jid, 'nick', cache['nick'])
            if cache.has_key('avatar'): 
                self.CM.update(from_jid, 'avatar', self.bridge.getAvatarFile(cache['avatar']))
            self.contactList.replace(from_jid)

        if show=="unavailable" and from_jid in self.profiles[profile]['onlineContact']:
            self.profiles[profile]['onlineContact'].remove(from_jid)
            self.CM.remove(from_jid)
            if not self.CM.isConnected(from_jid):
                self.contactList.disconnect(from_jid)

    def subscribe(self, type, raw_jid, profile):
        """Called when a subsciption maangement signal is received"""
        if not self.__check_profile(profile):
            return
        entity = JID(raw_jid)
        if type=="subscribed":
            # this is a subscription confirmation, we just have to inform user
            self.showDialog(_("The contact %s has accepted your subscription") % entity.short, _('Subscription confirmation'))
        elif type=="unsubscribed":
            # this is a subscription refusal, we just have to inform user
            self.showDialog(_("The contact %s has refused your subscription") % entity.short, _('Subscription refusal'), 'error')
        elif type=="subscribe":
            # this is a subscriptionn request, we have to ask for user confirmation
            answer = self.showDialog(_("The contact %s wants to subscribe to your presence.\nDo you accept ?") % entity.short, _('Subscription confirmation'), 'yes/no')
            if answer:
                self.bridge.subscription("subscribed", entity.short)
            else:
                self.bridge.subscribed("unsubscribed", entity.short)

    def showDialog(self, message, title, type="info"):
        raise NotImplementedError
    
    def showAlert(self, message):
        pass  #FIXME
    
    def paramUpdate(self, name, value, namespace, profile):
        if not self.__check_profile(profile):
            return
        debug(_("param update: [%(namespace)s] %(name)s = %(value)s") % {'namespace':namespace, 'name':name, 'value':value})
        if (namespace,name) == ("Connection", "JabberID"):
            debug (_("Changing JID to %s"), value)
            self.profiles[profile]['whoami']=JID(value)
        elif (namespace,name) == ("Misc", "Watched"):
            self.profiles[profile]['watched']=value.split()

    def contactDeleted(self, jid, profile):
        if not self.__check_profile(profile):
            return
        target = JID(jid)
        self.CM.remove(target)
        self.contactList.remove(self.CM.get_full(target))
        try:
            self.profiles[profile]['onlineContact'].remove(target.short)
        except KeyError:
            pass

    def updatedValue(self, name, data):
        if name == "card_nick":
            target = JID(data['jid'])
            self.CM.update(target, 'nick', data['nick'])
            self.contactList.replace(target)
        elif name == "card_avatar":
            target = JID(data['jid'])
            filename = self.bridge.getAvatarFile(data['avatar'])
            self.CM.update(target, 'avatar', filename)
            self.contactList.replace(target)

    def askConfirmation(self, type, id, data):
        raise NotImplementedError
    
    def actionResult(self, type, id, data):
        raise NotImplementedError