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)
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 = '<b>%s</b>' % 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()

View File

@ -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()

View File

@ -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 = '<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):
'''XEP-0118: User Tune'''