remove unneeded BeautifulSoup class
refactor the code so that it's not dup (vcard.get_avatar_pixbuf)
This commit is contained in:
parent
9b1881cbbd
commit
17f67e6a90
5 changed files with 83 additions and 1158 deletions
File diff suppressed because it is too large
Load diff
|
@ -211,9 +211,30 @@ class Connection:
|
|||
For feature: var is mandatory'''
|
||||
self._discover(common.xmpp.NS_DISCO_INFO, jid, node)
|
||||
|
||||
def node_to_dict(self, node):
|
||||
dict = {}
|
||||
for info in node.getChildren():
|
||||
name = info.getName()
|
||||
if name in ('ADR', 'TEL', 'EMAIL'): # we can have several
|
||||
if not dict.has_key(name):
|
||||
dict[name] = []
|
||||
entry = {}
|
||||
for c in info.getChildren():
|
||||
entry[c.getName()] = c.getData()
|
||||
dict[name].append(entry)
|
||||
elif info.getChildren() == []:
|
||||
dict[name] = info.getData()
|
||||
else:
|
||||
dict[name] = {}
|
||||
for c in info.getChildren():
|
||||
dict[name][c.getName()] = c.getData()
|
||||
return dict
|
||||
|
||||
def _vCardCB(self, con, vc):
|
||||
"""Called when we receive a vCard
|
||||
Parse the vCard and send it to plugins"""
|
||||
if not vc.getTag('vCard'):
|
||||
return
|
||||
frm_iq = vc.getFrom()
|
||||
our_jid = gajim.get_jid_from_account(self.name)
|
||||
resource = ''
|
||||
|
@ -222,30 +243,15 @@ class Connection:
|
|||
resource = frm_iq.getResource()
|
||||
else:
|
||||
frm = our_jid
|
||||
vcard = {'jid': frm, 'resource': resource}
|
||||
if not vc.getTag('vCard'):
|
||||
return
|
||||
if vc.getTag('vCard').getNamespace() == common.xmpp.NS_VCARD:
|
||||
card = vc.getChildren()[0]
|
||||
path_to_file = os.path.join(gajim.VCARDPATH, frm)
|
||||
fil = open(path_to_file, 'w')
|
||||
fil.write(str(card))
|
||||
fil.close()
|
||||
for info in card.getChildren():
|
||||
name = info.getName()
|
||||
if name in ('ADR', 'TEL', 'EMAIL'): # we can have several
|
||||
if not vcard.has_key(name):
|
||||
vcard[name] = []
|
||||
entry = {}
|
||||
for c in info.getChildren():
|
||||
entry[c.getName()] = c.getData()
|
||||
vcard[name].append(entry)
|
||||
elif info.getChildren() == []:
|
||||
vcard[name] = info.getData()
|
||||
else:
|
||||
vcard[name] = {}
|
||||
for c in info.getChildren():
|
||||
vcard[name][c.getName()] = c.getData()
|
||||
vcard = self.node_to_dict(card)
|
||||
vcard['jid'] = frm
|
||||
vcard['resource'] = resource
|
||||
if frm == our_jid:
|
||||
if vcard.has_key('PHOTO') and type(vcard['PHOTO']) == type({}) and \
|
||||
vcard['PHOTO'].has_key('BINVAL'):
|
||||
|
@ -1861,8 +1867,21 @@ class Connection:
|
|||
common.xmpp.NS_VERSION)
|
||||
self.to_be_sent.append(iq)
|
||||
|
||||
def get_cached_vcard(self, jid):
|
||||
j = gajim.get_jid_without_resource(jid)
|
||||
path_to_file = os.path.join(gajim.VCARDPATH, j)
|
||||
if os.path.isfile(path_to_file):
|
||||
# We have the vcard cached
|
||||
f = open(path_to_file)
|
||||
c = f.read()
|
||||
card = common.xmpp.Node(node = c)
|
||||
vcard = self.node_to_dict(card)
|
||||
vcard['jid'] = j
|
||||
vcard['resource'] = gajim.get_resource_from_jid(jid)
|
||||
return vcard
|
||||
|
||||
def request_vcard(self, jid = None):
|
||||
'''request the VCARD and return the iq'''
|
||||
'''request the VCARD'''
|
||||
if not self.connection:
|
||||
return
|
||||
iq = common.xmpp.Iq(typ = 'get')
|
||||
|
|
44
src/gajim.py
44
src/gajim.py
|
@ -26,8 +26,6 @@ import sys
|
|||
import pygtk
|
||||
import os
|
||||
|
||||
import common.BeautifulSoup as BeautifulSoup
|
||||
|
||||
from common import i18n
|
||||
i18n.init()
|
||||
_ = i18n._
|
||||
|
@ -78,6 +76,8 @@ import base64
|
|||
|
||||
from common import socks5
|
||||
import gtkgui_helpers
|
||||
import vcard
|
||||
|
||||
|
||||
import common.sleepy
|
||||
import check_for_new_version
|
||||
|
@ -947,41 +947,17 @@ 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 get_avatar_pixbuf(self, jid):
|
||||
'''checks if jid has avatar and if that avatar is valid image
|
||||
def get_avatar_pixbuf_from_cache(self, account, jid):
|
||||
'''checks if jid has avatar cached and if that avatar is valid image
|
||||
(can be shown)'''
|
||||
if jid not in os.listdir(gajim.VCARDPATH):
|
||||
return
|
||||
return None
|
||||
|
||||
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:
|
||||
text = xmldoc.vcard.photo.binval.string
|
||||
img_decoded = base64.decodestring(text)
|
||||
except:
|
||||
img_decoded = None
|
||||
|
||||
if img_decoded is not None:
|
||||
pixbuf = gtkgui_helpers.get_pixbuf_from_data(img_decoded)
|
||||
return pixbuf
|
||||
vcard_dict = gajim.connections[account].get_cached_vcard(jid)
|
||||
if not vcard_dict.has_key('PHOTO'):
|
||||
return None
|
||||
pix, _t, _t = vcard.get_avatar_pixbuf(vcard_dict['PHOTO'])
|
||||
return pix
|
||||
|
||||
def read_sleepy(self):
|
||||
'''Check idle status and change that status if needed'''
|
||||
|
|
|
@ -131,7 +131,8 @@ 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.get_avatar_pixbuf(jid)
|
||||
avatar_pixbuf = gajim.interface.get_avatar_pixbuf_from_cache(self.account,
|
||||
jid)
|
||||
avatar_w = avatar_pixbuf.get_width()
|
||||
avatar_h = avatar_pixbuf.get_height()
|
||||
|
||||
|
@ -155,7 +156,8 @@ 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.get_avatar_pixbuf(jid)
|
||||
avatar_pixbuf = gajim.interface.get_avatar_pixbuf_from_cache(self.account,
|
||||
jid)
|
||||
screen_w = gtk.gdk.screen_width()
|
||||
screen_h = gtk.gdk.screen_height()
|
||||
avatar_w = avatar_pixbuf.get_width()
|
||||
|
@ -293,7 +295,8 @@ class TabbedChatWindow(chat.Chat):
|
|||
# we assume contact has no avatar
|
||||
scaled_buf = None
|
||||
|
||||
pixbuf = gajim.interface.get_avatar_pixbuf(jid)
|
||||
pixbuf = gajim.interface.get_avatar_pixbuf_from_cache(self.account,
|
||||
jid)
|
||||
if pixbuf is not None:
|
||||
# resize to a width / height for the avatar not to have distortion
|
||||
# (keep aspect ratio)
|
||||
|
|
51
src/vcard.py
51
src/vcard.py
|
@ -39,6 +39,31 @@ gtk.glade.textdomain (APP)
|
|||
|
||||
GTKGUI_GLADE = 'gtkgui.glade'
|
||||
|
||||
def get_avatar_pixbuf(photo):
|
||||
'''return the pixbuf of the image
|
||||
photo is a dictionary containing PHOTO information'''
|
||||
if not isinstance(photo, dict):
|
||||
return None, None, None
|
||||
img_decoded = None
|
||||
if photo.has_key('BINVAL') and photo.has_key('TYPE'):
|
||||
img_encoded = photo['BINVAL']
|
||||
avatar_encoded = img_encoded
|
||||
avatar_mime_type = photo['TYPE']
|
||||
try:
|
||||
img_decoded = base64.decodestring(img_encoded)
|
||||
except:
|
||||
pass
|
||||
elif photo.has_key('EXTVAL'):
|
||||
url = photo['EXTVAL']
|
||||
try:
|
||||
fd = urllib.urlopen(url)
|
||||
img_decoded = fd.read()
|
||||
except:
|
||||
pass
|
||||
if img_decoded:
|
||||
return gtkgui_helpers.get_pixbuf_from_data(img_decoded), avatar_encoded, avatar_mime_type
|
||||
return None, None, None
|
||||
|
||||
class VcardWindow:
|
||||
'''Class for contact's information window'''
|
||||
|
||||
|
@ -201,28 +226,10 @@ class VcardWindow:
|
|||
def set_values(self, vcard):
|
||||
for i in vcard.keys():
|
||||
if i == 'PHOTO':
|
||||
if not isinstance(vcard[i], dict):
|
||||
continue
|
||||
img_decoded = None
|
||||
if vcard[i].has_key('BINVAL') and vcard[i].has_key('TYPE'):
|
||||
img_encoded = vcard[i]['BINVAL']
|
||||
self.avatar_encoded = img_encoded
|
||||
self.avatar_mime_type = vcard[i]['TYPE']
|
||||
try:
|
||||
img_decoded = base64.decodestring(img_encoded)
|
||||
except:
|
||||
pass
|
||||
elif vcard[i].has_key('EXTVAL'):
|
||||
url = vcard[i]['EXTVAL']
|
||||
try:
|
||||
fd = urllib.urlopen(url)
|
||||
img_decoded = fd.read()
|
||||
except:
|
||||
pass
|
||||
if img_decoded:
|
||||
pixbuf = gtkgui_helpers.get_pixbuf_from_data(img_decoded)
|
||||
image = self.xml.get_widget('PHOTO_image')
|
||||
image.set_from_pixbuf(pixbuf)
|
||||
pixbuf, self.avatar_encoded, self.avatar_mime_type = \
|
||||
get_avatar_pixbuf(vcard[i])
|
||||
image = self.xml.get_widget('PHOTO_image')
|
||||
image.set_from_pixbuf(pixbuf)
|
||||
continue
|
||||
if i == 'ADR' or i == 'TEL' or i == 'EMAIL':
|
||||
for entry in vcard[i]:
|
||||
|
|
Loading…
Add table
Reference in a new issue