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.
This commit is contained in:
Stephan Erb 2009-11-15 00:12:15 +01:00
parent 088916f4e7
commit e41e484855
3 changed files with 38 additions and 30 deletions

View File

@ -1432,34 +1432,13 @@ class ChatControl(ChatControlBase):
self._convert_to_gc_button.set_sensitive(False) self._convert_to_gc_button.set_sensitive(False)
def update_mood(self): def update_mood(self):
mood = None
text = None
if isinstance(self.contact, GC_Contact): if isinstance(self.contact, GC_Contact):
return return
if 'mood' in self.contact.mood: pep = self.contact.pep
mood = self.contact.mood['mood'].strip() if 'mood' in pep and not pep['mood'].was_retracted():
if 'text' in self.contact.mood: self._mood_image.set_from_pixbuf(pep['mood'].asPixbufIcon())
text = self.contact.mood['text'].strip() self._mood_image.set_tooltip_markup(pep['mood'].asMarkup())
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 = '<b>%s</b>' % mood
if text:
text = gobject.markup_escape_text(text)
tooltip += '\n' + text
self._mood_image.set_tooltip_markup(tooltip)
self._mood_image.show() self._mood_image.show()
else: else:
self._mood_image.hide() self._mood_image.hide()

View File

@ -110,6 +110,7 @@ class Contact(CommonContact):
self.msg_id = msg_id self.msg_id = msg_id
self.last_status_time = last_status_time self.last_status_time = last_status_time
self.pep = {}
self.mood = mood.copy() self.mood = mood.copy()
self.tune = tune.copy() self.tune = tune.copy()
self.activity = activity.copy() self.activity = activity.copy()

View File

@ -197,8 +197,10 @@ TUNE_DATA = ['artist', 'title', 'source', 'track', 'length']
import logging import logging
log = logging.getLogger('gajim.c.pep') log = logging.getLogger('gajim.c.pep')
import helpers import common.helpers
import atom import common.atom
import gtkgui_helpers
import gobject
class AbstractPEP(object): class AbstractPEP(object):
@ -223,6 +225,9 @@ class AbstractPEP(object):
'''To be implemented by subclasses''' '''To be implemented by subclasses'''
raise NotImplementedError raise NotImplementedError
def was_rectacted(self):
return self._retracted
class UserMoodPEP(AbstractPEP): class UserMoodPEP(AbstractPEP):
'''XEP-0107: User Mood''' '''XEP-0107: User Mood'''
@ -239,10 +244,10 @@ class UserMoodPEP(AbstractPEP):
name = child.getName().strip() name = child.getName().strip()
if name == 'text': if name == 'text':
mood_dict['text'] = child.getData() mood_dict['text'] = child.getData()
elif name in MOODS : else:
mood_dict['mood'] = name 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) return (mood_dict, retracted)
def do(self, jid, name): def do(self, jid, name):
@ -255,6 +260,7 @@ class UserMoodPEP(AbstractPEP):
user = common.gajim.get_room_and_nick_from_fjid(jid)[0] user = common.gajim.get_room_and_nick_from_fjid(jid)[0]
for contact in common.gajim.contacts.get_contacts(name, user): for contact in common.gajim.contacts.get_contacts(name, user):
contact.mood = mood_dict contact.mood = mood_dict
contact.pep['mood'] = self
if jid == common.gajim.get_jid_from_account(name): if jid == common.gajim.get_jid_from_account(name):
common.gajim.interface.roster.draw_account(name) common.gajim.interface.roster.draw_account(name)
@ -264,6 +270,28 @@ class UserMoodPEP(AbstractPEP):
ctrl.update_mood() 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 = '<b>%s</b>' % 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): class UserTunePEP(AbstractPEP):
'''XEP-0118: User Tune''' '''XEP-0118: User Tune'''