From 5f4db2eed9c93e378317479cf3f2b28e6758017d Mon Sep 17 00:00:00 2001 From: Stephan Erb Date: Sun, 15 Nov 2009 10:55:31 +0100 Subject: [PATCH] Unify updating of accounts and contact pep information. Implement and use asMarkupText() for tunes. --- src/chat_control.py | 36 ++-------- src/common/connection.py | 1 + src/common/pep.py | 144 +++++++++++++++++++++++---------------- src/roster_window.py | 11 +-- src/tooltips.py | 36 ++-------- 5 files changed, 101 insertions(+), 127 deletions(-) diff --git a/src/chat_control.py b/src/chat_control.py index fc59c2444..ab6f10213 100644 --- a/src/chat_control.py +++ b/src/chat_control.py @@ -1434,11 +1434,10 @@ class ChatControl(ChatControlBase): def update_mood(self): if isinstance(self.contact, GC_Contact): return - pep = self.contact.pep - if 'mood' in pep and not pep['mood'].was_retracted(): + if 'mood' in pep: self._mood_image.set_from_pixbuf(pep['mood'].asPixbufIcon()) - self._mood_image.set_tooltip_markup(pep['mood'].asMarkup()) + self._mood_image.set_tooltip_markup(pep['mood'].asMarkupText()) self._mood_image.show() else: self._mood_image.hide() @@ -1489,35 +1488,12 @@ class ChatControl(ChatControlBase): self._activity_image.hide() def update_tune(self): - artist = None - title = None - source = None - if isinstance(self.contact, GC_Contact): return - - if 'artist' in self.contact.tune: - artist = self.contact.tune['artist'].strip() - artist = gobject.markup_escape_text(artist) - if 'title' in self.contact.tune: - title = self.contact.tune['title'].strip() - title = gobject.markup_escape_text(title) - if 'source' in self.contact.tune: - source = self.contact.tune['source'].strip() - source = gobject.markup_escape_text(source) - - if artist or title: - if not artist: - artist = _('Unknown Artist') - if not title: - title = _('Unknown Title') - if not source: - source = _('Unknown Source') - - self._tune_image.set_tooltip_markup( - _('"%(title)s" by %(artist)s\n' - 'from %(source)s') % {'title': title, 'artist': artist, - 'source': source}) + + pep = self.contact.pep + if 'tune' in pep: + self._tune_image.set_tooltip_markup(pep['tune'].asMarkupText()) self._tune_image.show() else: self._tune_image.hide() diff --git a/src/common/connection.py b/src/common/connection.py index 717684a6b..90df73d08 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -171,6 +171,7 @@ class Connection(ConnectionHandlers): self.mood = {} self.tune = {} self.activity = {} + self.pep = {} # Do we continue connection when we get roster (send presence,get vcard..) self.continue_connect_info = None # Do we auto accept insecure connection diff --git a/src/common/pep.py b/src/common/pep.py index e32c2c444..bbc388f2a 100644 --- a/src/common/pep.py +++ b/src/common/pep.py @@ -203,6 +203,13 @@ import gtkgui_helpers import gobject +def translate_mood(mood): + if mood in MOODS: + return MOODS[mood] + else: + return mood + + class AbstractPEP(object): type = '' @@ -219,14 +226,49 @@ class AbstractPEP(object): def __init__(self, jid, account, items): self._pep_specific_data, self._retracted = self._extract_info(items) + + self._update_contacts(jid, account) + if jid == common.gajim.get_jid_from_account(account): + self._update_account(account) + self.do(jid, account) def _extract_info(self, items): '''To be implemented by subclasses''' raise NotImplementedError - def was_rectacted(self): - return self._retracted + def _update_contacts(self, jid, account): + dict = {} if self._retracted else self._pep_specific_data + + for contact in common.gajim.contacts.get_contacts(account, jid): + setattr(contact, self.type, dict) + + if self._retracted: + if self.type in contact.pep: + del contact.pep[self.type] + else: + contact.pep[self.type] = self + + def _update_account(self, account): + dict = {} if self._retracted else self._pep_specific_data + + acc = common.gajim.connections[account] + setattr(acc, self.type, dict) + + if self._retracted: + if self.type in acc.pep: + del acc.pep[self.type] + else: + acc.pep[self.type] = self + + def asPixbufIcon(self): + '''To be implemented by subclasses''' + raise NotImplementedError + + def asMarkupText(self): + '''To be implemented by subclasses''' + raise NotImplementedError + class UserMoodPEP(AbstractPEP): '''XEP-0107: User Mood''' @@ -251,45 +293,31 @@ class UserMoodPEP(AbstractPEP): return (mood_dict, retracted) def do(self, jid, name): - mood_dict = {} if self._retracted else self._pep_specific_data - - if jid == common.gajim.get_jid_from_account(name): - acc = common.gajim.connections[name] - acc.mood = mood_dict - - user = common.gajim.get_room_and_nick_from_fjid(jid)[0] - for contact in common.gajim.contacts.get_contacts(name, user): - contact.mood = mood_dict - contact.pep['mood'] = self - + if jid == common.gajim.get_jid_from_account(name): common.gajim.interface.roster.draw_account(name) - common.gajim.interface.roster.draw_mood(user, name) - ctrl = common.gajim.interface.msg_win_mgr.get_control(user, name) + common.gajim.interface.roster.draw_mood(jid, name) + ctrl = common.gajim.interface.msg_win_mgr.get_control(jid, name) if ctrl: ctrl.update_mood() def asPixbufIcon(self): - if self._retracted: - return None - else: - received_mood = self._pep_specific_data['mood'] - mood = received_mood if received_mood in MOODS else 'unknown' - pixbuf = gtkgui_helpers.load_mood_icon(mood).get_pixbuf() - return pixbuf + assert not self._retracted + received_mood = self._pep_specific_data['mood'] + mood = received_mood if received_mood in MOODS else 'unknown' + pixbuf = gtkgui_helpers.load_mood_icon(mood).get_pixbuf() + return pixbuf def asMarkupText(self): - if self._retracted: - return None - else: - untranslated_mood = self._pep_specific_data['mood'] - mood = MOODS[untranslated_mood] if untranslated_mood in MOODS else untranslated_mood - markuptext = '%s' % gobject.markup_escape_text(mood) - if 'text' in self._pep_specific_data: - text = self._pep_specific_data['text'] - markuptext += '(%s)' + gobject.markup_escape_text(text) - return markuptext + assert not self._retracted + untranslated_mood = self._pep_specific_data['mood'] + mood = translate_mood(untranslated_mood) + markuptext = '%s' % gobject.markup_escape_text(mood) + if 'text' in self._pep_specific_data: + text = self._pep_specific_data['text'] + markuptext += ' (%s)' % gobject.markup_escape_text(text) + return markuptext class UserTunePEP(AbstractPEP): @@ -310,27 +338,36 @@ class UserTunePEP(AbstractPEP): if child.getName() in TUNE_DATA: tune_dict[name] = data - retracted = items.getTag('retract') or not tune_dict + retracted = items.getTag('retract') or not ('artist' in tune_dict or + 'title' in tune_dict) return (tune_dict, retracted) - def do(self, jid, name): - tune_dict = {} if self._retracted else self._pep_specific_data - - if jid == common.gajim.get_jid_from_account(name): - acc = common.gajim.connections[name] - acc.tune = tune_dict - - user = common.gajim.get_room_and_nick_from_fjid(jid)[0] - for contact in common.gajim.contacts.get_contacts(name, user): - contact.tune = tune_dict - + def do(self, jid, name): if jid == common.gajim.get_jid_from_account(name): common.gajim.interface.roster.draw_account(name) - common.gajim.interface.roster.draw_tune(user, name) - ctrl = common.gajim.interface.msg_win_mgr.get_control(user, name) + common.gajim.interface.roster.draw_tune(jid, name) + ctrl = common.gajim.interface.msg_win_mgr.get_control(jid, name) if ctrl: ctrl.update_tune() + + def asMarkupText(self): + assert not self._retracted + tune = self._pep_specific_data + + artist = tune.get('artist', _('Unknown Artist')) + artist = gobject.markup_escape_text(artist) + + title = tune.get('title', _('Unknown Title')) + title = gobject.markup_escape_text(title) + + source = tune.get('source', _('Unknown Source')) + source = gobject.markup_escape_text(source) + + tune_string = _('"%(title)s" by %(artist)s\n' + 'from %(source)s') % {'title': title, + 'artist': artist, 'source': source} + return tune_string class UserActivityPEP(AbstractPEP): @@ -361,20 +398,11 @@ class UserActivityPEP(AbstractPEP): return (activity_dict, retracted) def do(self, jid, name): - activity_dict = {} if self._retracted else self._pep_specific_data - - if jid == common.gajim.get_jid_from_account(name): - acc = common.gajim.connections[name] - acc.activity = activity_dict - - user = common.gajim.get_room_and_nick_from_fjid(jid)[0] - for contact in common.gajim.contacts.get_contacts(name, user): - contact.activity = activity_dict if jid == common.gajim.get_jid_from_account(name): common.gajim.interface.roster.draw_account(name) - common.gajim.interface.roster.draw_activity(user, name) - ctrl = common.gajim.interface.msg_win_mgr.get_control(user, name) + common.gajim.interface.roster.draw_activity(jid, name) + ctrl = common.gajim.interface.msg_win_mgr.get_control(jid, name) if ctrl: ctrl.update_activity() @@ -437,7 +465,7 @@ class ConnectionPEP: for pep_class in SUPPORTED_PERSONAL_USER_EVENTS: pep = pep_class.get_tag_as_PEP(jid, self.name, event_tag) if pep: - self.dispatch('PEP_RECEIVED', (pep.type, pep)) + self.dispatch('PEP_RECEIVED', (jid, pep.type)) items = event_tag.getTag('items') if items is None: return diff --git a/src/roster_window.py b/src/roster_window.py index 4ea9d884a..93bdef75a 100644 --- a/src/roster_window.py +++ b/src/roster_window.py @@ -1281,15 +1281,10 @@ class RosterWindow: iters = self._get_contact_iter(jid, account, model=self.model) if not iters or not gajim.config.get('show_mood_in_roster'): return - jid = self.model[iters[0]][C_JID] - jid = jid.decode('utf-8') + jid = self.model[iters[0]][C_JID].decode('utf-8') contact = gajim.contacts.get_contact(account, jid) - if 'mood' in contact.mood and contact.mood['mood'].strip() in MOODS: - pixbuf = gtkgui_helpers.load_mood_icon( - contact.mood['mood'].strip()).get_pixbuf() - elif 'mood' in contact.mood: - pixbuf = gtkgui_helpers.load_mood_icon( - 'unknown').get_pixbuf() + if 'mood' in contact.pep: + pixbuf = contact.pep['mood'].asPixbufIcon() else: pixbuf = None for child_iter in iters: diff --git a/src/tooltips.py b/src/tooltips.py index fa96cbd8c..47f44b1ff 100644 --- a/src/tooltips.py +++ b/src/tooltips.py @@ -579,17 +579,9 @@ class RosterTooltip(NotificationAreaTooltip): Append Tune, Mood, Activity information of the specified contact to the given property list. ''' - if 'mood' in contact.mood: - mood = contact.mood['mood'].strip() - mood = MOODS.get(mood, mood) - mood = gobject.markup_escape_text(mood) + if 'mood' in contact.pep: + mood = contact.pep['mood'].asMarkupText() mood_string = _('Mood:') + ' %s' % mood - if 'text' in contact.mood \ - and contact.mood['text'] != '': - mood_text = contact.mood['text'].strip() - mood_text = \ - gobject.markup_escape_text(mood_text) - mood_string += ' (%s)' % mood_text properties.append((mood_string, None)) if 'activity' in contact.activity: @@ -617,27 +609,9 @@ class RosterTooltip(NotificationAreaTooltip): activity_string += ' (%s)' % activity_text properties.append((activity_string, None)) - if 'artist' in contact.tune \ - or 'title' in contact.tune: - if 'artist' in contact.tune: - artist = contact.tune['artist'].strip() - artist = gobject.markup_escape_text(artist) - else: - artist = _('Unknown Artist') - if 'title' in contact.tune: - title = contact.tune['title'].strip() - title = gobject.markup_escape_text(title) - else: - title = _('Unknown Title') - if 'source' in contact.tune: - source = contact.tune['source'].strip() - source = gobject.markup_escape_text(source) - else: - source = _('Unknown Source') - tune_string = _('Tune:') + ' ' + \ - _('"%(title)s" by %(artist)s\n' - 'from %(source)s') % {'title': title, - 'artist': artist, 'source': source} + if 'tune' in contact.pep: + tune = contact.pep['tune'].asMarkupText() + tune_string = _('Tune:') + ' %s' % tune properties.append((tune_string, None))