core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:

- QuickContactManagement is not used anymore and will be removed, ContactList + Core are used instead
	- disconnected contacts are now displayed in Primitivus (M-d to show/hide them)
	- avatars are temporary unavailable in wix
	- new bridge method: getContactsFromGroup
This commit is contained in:
Goffi 2012-09-25 00:58:34 +02:00
commit b44412ec0d
3 changed files with 207 additions and 130 deletions

View file

@ -19,7 +19,7 @@ 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 logging import debug, info, error
from logging import debug, info, warning, error
from sat.tools.jid import JID
from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
from optparse import OptionParser
@ -32,7 +32,6 @@ 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
self.check_options()
@ -157,7 +156,7 @@ class QuickApp():
### now we fill the contact list ###
for contact in self.bridge.getContacts(profile):
self.newContact(contact[0], contact[1], contact[2], profile)
self.newContact(*contact, profile=profile)
presences = self.bridge.getPresenceStatus(profile)
for contact in presences:
@ -207,8 +206,7 @@ class QuickApp():
if not self.check_profile(profile):
return
debug(_("Disconnected"))
self.CM.clear()
self.contactList.clear_contacts()
self.contactList.clearContacts()
self.setStatusOnline(False)
def connectionError(self, error_type, profile):
@ -227,10 +225,7 @@ class QuickApp():
return
entity=JID(JabberId)
_groups = list(groups)
self.rosterList[entity.short]=(dict(attributes), _groups)
if entity in self.CM:
self.CM.update(entity, 'groups', _groups)
self.contactList.replace(entity, self.CM.getAttr(entity, 'groups'))
self.contact_list._replace(entity, _groups, attributes)
def newMessage(self, from_jid, msg, type, to_jid, profile):
if not self.check_profile(profile):
@ -266,36 +261,20 @@ class QuickApp():
self.setStatusOnline(False)
return
self.contact_list.updatePresence(from_jid, show, priority, statuses)
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', unicode(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, profile)
if cache.has_key('nick'):
self.CM.update(from_jid, 'nick', unicode(cache['nick']))
if cache.has_key('avatar'):
self.CM.update(from_jid, 'avatar', self.bridge.getAvatarFile(cache['avatar']))
self.contactList.replace(from_jid, self.CM.getAttr(from_jid, 'groups'))
#TODO: vcard data (avatar)
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 roomJoined(self, room_jid, room_nicks, user_nick, profile):
"""Called when a MUC room is joined"""
@ -491,27 +470,27 @@ class QuickApp():
if not self.check_profile(profile):
return
target = JID(jid)
self.contactList.remove(self.CM.get_full(target))
self.CM.remove(target)
self.contactList.remove(target)
try:
self.profiles[profile]['onlineContact'].remove(target.short)
except KeyError:
pass
def updatedValue(self, name, data, profile):
#FIXME: to be removed
if not self.check_profile(profile):
return
if name == "card_nick":
target = JID(data['jid'])
if target in self.contactList:
self.CM.update(target, 'nick', unicode(data['nick']))
self.contactList.replace(target)
#self.CM.update(target, 'nick', unicode(data['nick']))
self.contact_list._replace(target)
elif name == "card_avatar":
target = JID(data['jid'])
if target in self.contactList:
filename = self.bridge.getAvatarFile(data['avatar'])
self.CM.update(target, 'avatar', filename)
self.contactList.replace(target)
#self.CM.update(target, 'avatar', filename)
self.contact_list._replace(target)
def askConfirmation(self, type, id, data):
raise NotImplementedError

View file

@ -99,7 +99,7 @@ class QuickChat():
def _get_nick(self, jid):
"""Return nick of this jid when possible"""
return jid.resource if self.type == "group" else (self.host.CM.getAttr(jid,'nick') or self.host.CM.getAttr(jid,'name') or jid.node)
return jid.resource if self.type == "group" else (self.host.contact_list.getCache(jid,'nick') or self.host.contact_list.getCache(jid,'name') or jid.node)
def printMessage(self, from_jid, msg, profile, timestamp):
"""Print message in chat window. Must be implemented by child class"""

View file

@ -19,33 +19,47 @@ 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 logging import debug, info, error
from sat.tools.jid import JID
from logging import debug
class QuickContactList():
"""This class manage the visual representation of contacts"""
def __init__(self, CM):
"""
@param CM: instance of QuickContactManagement
"""
def __init__(self):
debug(_("Contact List init"))
self.CM = CM
self._cache = {}
def update_jid(self, jid):
"""Update the jid in the list when something changed"""
raise NotImplementedError
def getCache(self, jid, name):
try:
jid_cache = self._cache[jid.short]
if name == 'status': #XXX: we get the first status for 'status' key
return jid_cache['statuses'].get('default','')
return jid_cache[name]
except (KeyError, IndexError):
return None
def setCache(self, jid, name, value):
jid_cache = self._cache.setdefault(jid.short, {})
jid_cache[name] = value
def __contains__(self, jid):
raise NotImplementedError
def clear_contacts(self, jid):
def clearContacts(self, jid):
"""Clear all the contact list"""
raise NotImplementedError
def replace(self, jid, groups=None):
"""add a contact to the list if doesn't exist, else update it"""
raise NotImplementedError
def _replace(self, jid, groups=None, attributes=None):
if 'name' in attributes:
self.setCache(jid, 'name', attributes['name'])
self.replace(jid, groups, attributes)
def disconnect(self, jid):
"""mark a contact disconnected"""
def replace(self, jid, groups, attributes):
"""add a contact to the list if doesn't exist, else update it"""
raise NotImplementedError
def remove(self, jid):
@ -55,3 +69,14 @@ class QuickContactList():
def add(self, jid, param_groups=None):
"""add a contact to the list"""
raise NotImplementedError
def updatePresence(self, jid, show, priority, statuses):
"""Update entity's presence status
@param jid: entity to update's jid
@param show: availability
@parap priority: resource's priority
@param statuses: dict of statuses"""
self.setCache(jid, 'show', show)
self.setCache(jid, 'prority', priority)
self.setCache(jid, 'statuses', statuses)
self.update_jid(jid)