From e41e4848559c0446107cc378d15d8887daaea129 Mon Sep 17 00:00:00 2001 From: Stephan Erb Date: Sun, 15 Nov 2009 00:12:15 +0100 Subject: [PATCH] Proof of concept: Move markup / pixbuf determination logic from the UI to the different PEP classes. Currently this is only done for UserMood. We can decide later on (if needed), to move the asPixbufIcon and asMarkupText methods to a more appropriate place. Goal is to remove as much redundant code as possible. --- src/chat_control.py | 31 +++++-------------------------- src/common/contacts.py | 1 + src/common/pep.py | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/chat_control.py b/src/chat_control.py index 04d6abc00..fc59c2444 100644 --- a/src/chat_control.py +++ b/src/chat_control.py @@ -1432,34 +1432,13 @@ class ChatControl(ChatControlBase): self._convert_to_gc_button.set_sensitive(False) def update_mood(self): - mood = None - text = None - if isinstance(self.contact, GC_Contact): return - - if 'mood' in self.contact.mood: - mood = self.contact.mood['mood'].strip() - if 'text' in self.contact.mood: - text = self.contact.mood['text'].strip() - - if mood is not None: - if mood in MOODS: - self._mood_image.set_from_pixbuf(gtkgui_helpers.load_mood_icon( - mood).get_pixbuf()) - # Translate standard moods - mood = MOODS[mood] - else: - self._mood_image.set_from_pixbuf(gtkgui_helpers.load_mood_icon( - 'unknown').get_pixbuf()) - - mood = gobject.markup_escape_text(mood) - - tooltip = '%s' % mood - if text: - text = gobject.markup_escape_text(text) - tooltip += '\n' + text - self._mood_image.set_tooltip_markup(tooltip) + + pep = self.contact.pep + if 'mood' in pep and not pep['mood'].was_retracted(): + self._mood_image.set_from_pixbuf(pep['mood'].asPixbufIcon()) + self._mood_image.set_tooltip_markup(pep['mood'].asMarkup()) self._mood_image.show() else: self._mood_image.hide() diff --git a/src/common/contacts.py b/src/common/contacts.py index a4a64a0dd..b8b039288 100644 --- a/src/common/contacts.py +++ b/src/common/contacts.py @@ -110,6 +110,7 @@ class Contact(CommonContact): self.msg_id = msg_id self.last_status_time = last_status_time + self.pep = {} self.mood = mood.copy() self.tune = tune.copy() self.activity = activity.copy() diff --git a/src/common/pep.py b/src/common/pep.py index bd17e46ba..f11021ec7 100644 --- a/src/common/pep.py +++ b/src/common/pep.py @@ -197,8 +197,10 @@ TUNE_DATA = ['artist', 'title', 'source', 'track', 'length'] import logging log = logging.getLogger('gajim.c.pep') -import helpers -import atom +import common.helpers +import common.atom +import gtkgui_helpers +import gobject class AbstractPEP(object): @@ -223,6 +225,9 @@ class AbstractPEP(object): '''To be implemented by subclasses''' raise NotImplementedError + def was_rectacted(self): + return self._retracted + class UserMoodPEP(AbstractPEP): '''XEP-0107: User Mood''' @@ -239,10 +244,10 @@ class UserMoodPEP(AbstractPEP): name = child.getName().strip() if name == 'text': mood_dict['text'] = child.getData() - elif name in MOODS : + else: mood_dict['mood'] = name - retracted = items.getTag('retract') or not mood_dict + retracted = items.getTag('retract') or not 'mood' in mood_dict return (mood_dict, retracted) def do(self, jid, name): @@ -255,6 +260,7 @@ class UserMoodPEP(AbstractPEP): 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) @@ -262,7 +268,29 @@ class UserMoodPEP(AbstractPEP): ctrl = common.gajim.interface.msg_win_mgr.get_control(user, 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 + 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 + class UserTunePEP(AbstractPEP): '''XEP-0118: User Tune'''