if we have avatar in HD, we get it from there now

This commit is contained in:
Nikos Kouremenos 2005-10-30 21:39:09 +00:00
parent 89d00ada0d
commit ee46edcfb8
4 changed files with 1127 additions and 47 deletions

1080
src/common/BeautifulSoup.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,8 @@ import sys
import pygtk
import os
import common.BeautifulSoup as BeautifulSoup
from common import i18n
i18n.init()
_ = i18n._
@ -661,7 +663,6 @@ class Interface:
'''vcard holds the vcard data'''
jid = vcard['jid']
resource = vcard['resource']
self.store_avatar(vcard)
# vcard window
win = None
@ -670,7 +671,7 @@ class Interface:
elif self.windows[account]['infos'].has_key(jid + '/' + resource):
win = self.windows[account]['infos'][jid + '/' + resource]
if win:
win.set_values(vcard) #FIXME: maybe store all vcard data?
win.set_values(vcard)
# show avatar in chat
win = None
@ -679,6 +680,7 @@ class Interface:
elif self.windows[account]['chats'].has_key(jid + '/' + resource):
win = self.windows[account]['chats'][jid + '/' + resource]
if win:
# FIXME: this will be removed when we have the thread working
win.show_avatar(jid, resource)
if self.remote is not None:
self.remote.raise_signal('VcardInfo', (account, vcard))
@ -943,32 +945,41 @@ class Interface:
def handle_event_vcard_not_published(self, account, array):
dialogs.InformationDialog(_('vCard publication failed'), _('There was an error while publishing your personal information, try again later.'))
def store_avatar(self, vcard):
'''stores avatar per jid so we do not have to ask everytime for vcard'''
jid = vcard['jid']
# we assume contact has no avatar
self.avatar_pixbufs[jid] = None
if not vcard.has_key('PHOTO'):
def get_avatar_pixbuf(self, jid):
'''checks if jid has avatar and if that avatar is valid image
(can be shown)'''
if jid not in os.listdir(gajim.VCARDPATH):
return
if not isinstance(vcard['PHOTO'], dict):
return
img_decoded = None
if vcard['PHOTO'].has_key('BINVAL'):
path_to_file = os.path.join(gajim.VCARDPATH, jid)
vcard_data = open(path_to_file).read()
xmldoc = BeautifulSoup.BeautifulSoup(vcard_data)
# check for BINVAL
if isinstance(xmldoc.vcard.photo.binval, BeautifulSoup.NullType):
# no BINVAL, check for EXTVAL
if isinstance(xmldoc.vcard.photo.extval, BeautifulSoup.NullType):
return # no EXTVAL, contact has no avatar in his vcard
else:
# we have EXTVAL
url = xmldoc.vcard.photo.extval
try:
fd = urllib.urlopen(url)
img_decoded = fd.read()
except:
img_decoded = None
else:
# we have BINVAL
try:
img_decoded = base64.decodestring(vcard['PHOTO']['BINVAL'])
text = xmldoc.vcard.photo.binval.string
img_decoded = base64.decodestring(text)
except:
pass
elif vcard['PHOTO'].has_key('EXTVAL'):
url = vcard['PHOTO']['EXTVAL']
try:
fd = urllib.urlopen(url)
img_decoded = fd.read()
except:
pass
if img_decoded:
img_decoded = None
if img_decoded is not None:
pixbuf = gtkgui_helpers.get_pixbuf_from_data(img_decoded)
# store avatar for jid
self.avatar_pixbufs[jid] = pixbuf
return pixbuf
def read_sleepy(self):
'''Check idle status and change that status if needed'''
@ -1286,9 +1297,6 @@ class Interface:
self.windows = {'logs': {}}
# keep avatar (pixbuf) per jid
self.avatar_pixbufs = {}
for a in gajim.connections:
self.windows[a] = {'infos': {}, 'disco': {}, 'chats': {},
'gc': {}, 'gc_config': {}}

View File

@ -285,7 +285,7 @@ def get_abspath_for_script(scriptname, want_type = False):
return path_to_script
def get_pixbuf_from_data(file_data):
'''wants img data and returns gtk.gdk.Pixbuf'''
'''Gets image data and returns gtk.gdk.Pixbuf'''
pixbufloader = gtk.gdk.PixbufLoader()
try:
pixbufloader.write(file_data)

View File

@ -131,7 +131,7 @@ class TabbedChatWindow(chat.Chat):
'''we enter the eventbox area so we under conditions add a timeout
to show a bigger avatar after 0.5 sec'''
jid = self.get_active_jid()
avatar_pixbuf = gajim.interface.avatar_pixbufs[jid]
avatar_pixbuf = gajim.interface.get_avatar_pixbuf(jid)
avatar_w = avatar_pixbuf.get_width()
avatar_h = avatar_pixbuf.get_height()
@ -155,7 +155,7 @@ class TabbedChatWindow(chat.Chat):
'''resizes the avatar, if needed, so it has at max half the screen size
and shows it'''
jid = self.get_active_jid()
avatar_pixbuf = gajim.interface.avatar_pixbufs[jid]
avatar_pixbuf = gajim.interface.get_avatar_pixbuf(jid)
screen_w = gtk.gdk.screen_width()
screen_h = gtk.gdk.screen_height()
avatar_w = avatar_pixbuf.get_width()
@ -290,13 +290,12 @@ class TabbedChatWindow(chat.Chat):
if not xml:
return
if gajim.interface.avatar_pixbufs[jid] is None:
# contact has no avatar
scaled_buf = None
else:
pixbuf = gajim.interface.avatar_pixbufs[jid]
# we assume contact has no avatar
scaled_buf = None
# resize to a width / height for the avatar not to have distortion
pixbuf = gajim.interface.get_avatar_pixbuf(jid)
if pixbuf is not None:
# resize to a width / height for the avatar not to have distortion
# (keep aspect ratio)
ratio = float(pixbuf.get_width()) / float(pixbuf.get_height())
if ratio > 1:
@ -438,7 +437,6 @@ class TabbedChatWindow(chat.Chat):
self.childs[contact.jid] = self.xmls[contact.jid].get_widget('chats_vbox')
self.contacts[contact.jid] = contact
# add MessageTextView to UI
message_scrolledwindow = self.xmls[contact.jid].get_widget(
'message_scrolledwindow')
@ -448,19 +446,13 @@ class TabbedChatWindow(chat.Chat):
self.on_message_textview_mykeypress_event)
message_scrolledwindow.add(msg_textview)
#FIXME: request in thread or idle and show in roster
# this is to prove cache code works:
# should we ask vcard? (only the first time we should ask)
if not gajim.interface.avatar_pixbufs.has_key(contact.jid):
if contact.jid in os.listdir(gajim.VCARDPATH):
# show avatar from HD
self.show_avatar(contact.jid, contact.resource)
else:
# it's the first time, so we should ask vcard
gajim.connections[self.account].request_vcard(contact.jid)
#please do not remove this commented print until I'm done with showing
#avatars in roster
#print 'REQUESTING VCARD for', contact.jid
else:
# show avatar from stored place
self.show_avatar(contact.jid, contact.resource)
self.childs[contact.jid].connect('drag_data_received',
self.on_drag_data_received, contact)