juick.optimization

This commit is contained in:
Denis Fomin
2011-11-17 19:56:39 +03:00
parent 23c1fdb5d1
commit 110439ab2f

View File

@@ -394,9 +394,9 @@ class Base(object):
return return
def insert_pic_preview(self, mark, special_text, url): def insert_pic_preview(self, mark, special_text, url):
pixbuf, is_unknown = self.get_pixbuf_from_url( url, self.plugin.config[ pixbuf = self.get_pixbuf_from_url( url, self.plugin.config[
'PREVIEW_SIZE']) 'PREVIEW_SIZE'])
if not is_unknown: if pixbuf:
# insert image # insert image
buffer_ = mark.get_buffer() buffer_ = mark.get_buffer()
end_iter = buffer_.get_iter_at_mark(mark) end_iter = buffer_.get_iter_at_mark(mark)
@@ -425,12 +425,11 @@ class Base(object):
return return
uid = user.getAttr('uid') uid = user.getAttr('uid')
pixbuf = self.get_avatar(uid, nick) pixbuf = self.get_avatar(uid, nick)
if pixbuf: anchor = buffer_.create_child_anchor(end_iter)
anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, nick)
img = TextViewImage(anchor, nick) img.set_from_pixbuf(pixbuf)
img.set_from_pixbuf(pixbuf) img.show()
img.show() self.textview.tv.add_child_at_anchor(img, anchor)
self.textview.tv.add_child_at_anchor(img, anchor)
@@ -445,26 +444,29 @@ class Base(object):
if (time.time() - os.stat(pic_path).st_mtime) < max_old: if (time.time() - os.stat(pic_path).st_mtime) < max_old:
return gtk.gdk.pixbuf_new_from_file(pic_path) return gtk.gdk.pixbuf_new_from_file(pic_path)
pixbuf,is_unknown = self.get_pixbuf_from_url(url,self.plugin.config[ avatar_size = self.plugin.config['AVATAR_SIZE']
'AVATAR_SIZE']) pixbuf = self.get_pixbuf_from_url(url, avatar_size)
# save to cache if pixbuf:
if not is_unknown: # save to cache
pixbuf.save(pic_path, 'png') pixbuf.save(pic_path, 'png')
if need_check: if need_check:
return pixbuf return pixbuf
query = "select nick, id from person where nick = :nick" query = "select nick, id from person where nick = :nick"
self.plugin.cursor.execute(query, {'nick':nick}) self.plugin.cursor.execute(query, {'nick':nick})
db_item = self.plugin.cursor.fetchone() db_item = self.plugin.cursor.fetchone()
if not db_item: if not db_item:
data = (nick.decode('utf-8'), uid.decode('utf-8')) data = (nick.decode('utf-8'), uid.decode('utf-8'))
self.plugin.cursor.execute('insert into person(nick, id)' self.plugin.cursor.execute('insert into person(nick, id)'
' values (?, ?)', data) ' values (?, ?)', data)
self.plugin.conn.commit() self.plugin.conn.commit()
else:
img_path = self.plugin.local_file_path('unknown.png')
pixbuf = gtk.gdk.pixbuf_new_from_file(img_path)
pixbuf, w, h = self.get_pixbuf_of_size(pixbuf, avatar_size)
return pixbuf return pixbuf
def get_pixbuf_from_url(self, url, size): def get_pixbuf_from_url(self, url, size):
# download avatar and resize him # download avatar and resize him
is_unknown = False
try: try:
data, alt = helpers.download_image(self.textview.account, data, alt = helpers.download_image(self.textview.account,
{'src': url}) {'src': url})
@@ -472,12 +474,10 @@ class Base(object):
pix.write(data) pix.write(data)
pix.close() pix.close()
pixbuf = pix.get_pixbuf() pixbuf = pix.get_pixbuf()
pixbuf, w, h = self.get_pixbuf_of_size(pixbuf, size)
except Exception,e: except Exception,e:
img_path = self.plugin.local_file_path('unknown.png') return
is_unknown = True return pixbuf
pixbuf = gtk.gdk.pixbuf_new_from_file(img_path)
pixbuf, w, h = self.get_pixbuf_of_size(pixbuf, size)
return pixbuf,is_unknown
def get_pixbuf_of_size(self, pixbuf, size): def get_pixbuf_of_size(self, pixbuf, size):
# Creates a pixbuf that fits in the specified square of sizexsize # Creates a pixbuf that fits in the specified square of sizexsize