SàT: multi-profile: DBus signals and frontend adaptation (first draft)

- Quick App: new single_profile parameter in __init__ (default: yes), used to tell if the application use only one profile at the time or not
- Quick App: new __check_profile method, tell if the profile is used by the current frontend
- Quick App: new methods plug_profile, unplug_profile and clear_profile, must be called by the frontend to tell which profiles to use
- DBus Bridge: new methods getProfileName, getProfilesList and createProfile
This commit is contained in:
Goffi 2010-02-03 23:35:57 +11:00
commit ca242c7aee

View file

@ -27,8 +27,10 @@ import pdb
class QuickApp():
"""This class contain the main methods needed for the frontend"""
def __init__(self):
def __init__(self, single_profile=True):
self.rosterList = {}
self.profiles = {}
self.single_profile = single_profile
## bridge ##
self.bridge=DBusBridgeFrontend()
@ -45,70 +47,113 @@ class QuickApp():
self.bridge.register("actionResult", self.actionResult, "request")
self.bridge.register("actionResultExt", self.actionResult, "request")
###now we get the essential params###
self.whoami=JID(self.bridge.getParamA("JabberID","Connection"))
self.watched=self.bridge.getParamA("Watched", "Misc").split() #TODO: put this in a plugin
## misc ##
self.current_action_ids = set()
self.current_action_ids_cb = {}
self.onlineContact = set() #FIXME: temporary
def __check_profile(self, profile):
"""Tell if the profile is currently followed by the application"""
return profile in self.profiles.keys()
if self.bridge.isConnected():
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")
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():
self.newContact(contact[0], contact[1], contact[2])
for contact in self.bridge.getContacts(profile):
self.newContact(contact[0], contact[1], contact[2], profile)
presences = self.bridge.getPresenceStatus()
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)
self.presenceUpdate(jabber_id, show, priority, statuses, profile)
waitingSub = self.bridge.getWaitingSub()
waitingSub = self.bridge.getWaitingSub(profile)
for sub in waitingSub:
self.subscribe(waitingSub[sub], sub)
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 connected(self):
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):
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):
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):
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.whoami.short else sender
self.chat_wins[win.short].printMessage(sender, msg)
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):
def presenceUpdate(self, jabber_id, show, priority, statuses, profile):
if not self.__check_profile(profile):
return
print "check ok"
debug ("presence update for %s (show=%s, statuses=%s)", jabber_id, show, statuses);
from_jid=JID(jabber_id)
debug ("from_jid.short=%s whoami.short=%s", from_jid.short, self.whoami.short)
debug ("from_jid.short=%s whoami.short=%s", from_jid.short, self.profiles[profile]['whoami'].short)
if from_jid.short==self.whoami.short:
if from_jid.short==self.profiles[profile]['whoami'].short:
if not type:
self.setStatusOnline(True)
elif type=="unavailable":
@ -124,10 +169,10 @@ class QuickApp():
groups=self.rosterList[from_jid.short][1]
#FIXME: must be moved in a plugin
if from_jid.short in self.watched and not from_jid.short in self.onlineContact:
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.onlineContact.add(from_jid) #FIXME onlineContact is useless with CM, must be removed
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)
@ -140,14 +185,16 @@ class QuickApp():
self.CM.update(from_jid, 'avatar', self.bridge.getAvatarFile(cache['avatar']))
self.contactList.replace(from_jid)
if show=="unavailable" and from_jid in self.onlineContact:
self.onlineContact.remove(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):
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
@ -169,29 +216,35 @@ class QuickApp():
def showAlert(self, message):
pass #FIXME
def paramUpdate(self, name, value, namespace):
def paramUpdate(self, name, value, namespace, profile):
if not self.__check_profile(profile):
return
debug("param update: [%s] %s = %s", namespace, name, value)
if (namespace,name) == ("Connection", "JabberID"):
debug ("Changing ID to %s", value)
self.whoami=JID(value)
self.profiles[profile]['whoami']=JID(value)
elif (namespace,name) == ("Misc", "Watched"):
self.watched=value.split()
self.profiles[profile]['watched']=value.split()
def contactDeleted(self, jid):
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.onlineContact.remove(target.short)
self.profiles[profile]['onlineContact'].remove(target.short)
except KeyError:
pass
def updatedValue(self, name, data):
if name == "profile_nick":
def updatedValue(self, name, data, profile):
if not self.__check_profile(profile):
return
if name == "card_nick":
target = JID(data['jid'])
self.CM.update(target, 'nick', data['nick'])
self.contactList.replace(target)
elif name == "profile_avatar":
elif name == "card_avatar":
target = JID(data['jid'])
filename = self.bridge.getAvatarFile(data['avatar'])
self.CM.update(target, 'avatar', filename)