Small cleanups for avatar save. Also fixes #5461.

* set default file type for avatars centrally
 * remove client side decision logic determing if a avatar loookup shall happen for a groupchat contact or for a normal contact
 * remove unused method (this one was triggering the traceback in #5461)
This commit is contained in:
Stephan Erb 2009-11-29 23:06:49 +01:00
parent b2efc9685b
commit 9b7ae8cac3
7 changed files with 44 additions and 72 deletions

View File

@ -1351,7 +1351,7 @@ class ChatControl(ChatControlBase):
# per jid
self.show_bigger_avatar_timeout_id = None
self.bigger_avatar_window = None
self.show_avatar(self.contact.resource)
self.show_avatar()
# chatstate timers and state
self.reset_kbd_mouse_timeout_vars()
@ -1601,11 +1601,7 @@ class ChatControl(ChatControlBase):
bigger avatar after 0.5 sec
"""
jid = self.contact.jid
is_fake = False
if self.type_id == message_control.TYPE_PM:
is_fake = True
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid,
is_fake)
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid)
if avatar_pixbuf in ('ask', None):
return
avatar_w = avatar_pixbuf.get_width()
@ -1638,8 +1634,7 @@ class ChatControl(ChatControlBase):
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
id_ = menuitem.connect('activate',
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
self.contact.jid, self.account, self.contact.get_shown_name() + \
'.jpeg')
self.contact.jid, self.account, self.contact.get_shown_name())
self.handlers[id_] = menuitem
menu.append(menuitem)
menu.show_all()
@ -2403,24 +2398,12 @@ class ChatControl(ChatControlBase):
# Re-show the small avatar
self.show_avatar()
def show_avatar(self, resource = None):
def show_avatar(self):
if not gajim.config.get('show_avatar_in_chat'):
return
is_fake = False
if self.TYPE_ID == message_control.TYPE_PM:
is_fake = True
jid_with_resource = self.contact.jid # fake jid
else:
jid_with_resource = self.contact.jid
if resource:
jid_with_resource += '/' + resource
# we assume contact has no avatar
scaled_pixbuf = None
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid_with_resource,
is_fake)
jid_with_resource = self.contact.get_full_jid()
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid_with_resource)
if pixbuf == 'ask':
# we don't have the vcard
if self.TYPE_ID == message_control.TYPE_PM:
@ -2436,8 +2419,10 @@ class ChatControl(ChatControlBase):
else:
gajim.connections[self.account].request_vcard(jid_with_resource)
return
if pixbuf is not None:
elif pixbuf:
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'chat')
else:
scaled_pixbuf = None
image = self.xml.get_widget('avatar_image')
image.set_from_pixbuf(scaled_pixbuf)
@ -2629,11 +2614,8 @@ class ChatControl(ChatControlBase):
if not small_avatar.window:
# Tab has been closed since we hovered the avatar
return
is_fake = False
if self.type_id == message_control.TYPE_PM:
is_fake = True
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(
self.contact.jid, is_fake)
self.contact.jid)
if avatar_pixbuf in ('ask', None):
return
# Hide the small avatar

View File

@ -373,14 +373,6 @@ class Contacts:
nbr_total += 1
return nbr_online, nbr_total
def is_pm_from_jid(self, account, jid):
"""
Return True if the given jid is a private message jid
"""
if jid in self._contacts[account]:
return False
return True
def __getattr__(self, attr_name):
# Only called if self has no attr_name
if hasattr(self._metacontact_manager, attr_name):

View File

@ -1171,8 +1171,8 @@ class GroupchatControl(ChatControlBase):
iter_ = self.get_contact_iter(nick)
if not iter_:
return
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.room_jid + \
'/' + nick, True)
fake_jid = self.room_jid + '/' + nick
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(fake_jid)
if pixbuf in ('ask', None):
scaled_pixbuf = None
else:
@ -1514,7 +1514,7 @@ class GroupchatControl(ChatControlBase):
if gajim.config.get('ask_avatars_on_startup') and \
not server.startswith('irc'):
fake_jid = self.room_jid + '/' + nick
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(fake_jid, True)
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(fake_jid)
if pixbuf == 'ask':
if j:
fjid = j

View File

@ -30,6 +30,7 @@
import xml.sax.saxutils
import gtk
import gtk.glade
import glib
import gobject
import pango
import os
@ -593,7 +594,7 @@ def get_scaled_pixbuf(pixbuf, kind):
scaled_buf = pixbuf.scale_simple(w, h, gtk.gdk.INTERP_HYPER)
return scaled_buf
def get_avatar_pixbuf_from_cache(fjid, is_fake_jid = False, use_local = True):
def get_avatar_pixbuf_from_cache(fjid, use_local=True):
"""
Check if jid has cached avatar and if that avatar is valid image (can be
shown)
@ -607,9 +608,15 @@ def get_avatar_pixbuf_from_cache(fjid, is_fake_jid = False, use_local = True):
gajim.jid_is_transport(jid):
# don't show avatar for the transport itself
return None
room, nick = gajim.get_room_and_nick_from_fjid(jid)
if any(room in gajim.contacts.get_gc_list(acc) for acc in gajim.connections):
is_groupchat_contact = True
else:
is_groupchat_contact = False
puny_jid = helpers.sanitize_filename(jid)
if is_fake_jid:
if is_groupchat_contact:
puny_nick = helpers.sanitize_filename(nick)
path = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
local_avatar_basepath = os.path.join(gajim.AVATAR_PATH, puny_jid,
@ -631,7 +638,7 @@ def get_avatar_pixbuf_from_cache(fjid, is_fake_jid = False, use_local = True):
return 'ask'
vcard_dict = gajim.connections.values()[0].get_cached_vcard(fjid,
is_fake_jid)
is_groupchat_contact)
if not vcard_dict: # This can happen if cached vcard is too old
return 'ask'
if 'PHOTO' not in vcard_dict:
@ -831,39 +838,33 @@ def get_possible_button_event(event):
def destroy_widget(widget):
widget.destroy()
def on_avatar_save_as_menuitem_activate(widget, jid, account,
default_name = ''):
def on_avatar_save_as_menuitem_activate(widget, jid, account, default_name=''):
def on_continue(response, file_path):
if response < 0:
return
# Get pixbuf
pixbuf = None
is_fake = False
if account and gajim.contacts.is_pm_from_jid(account, jid):
is_fake = True
pixbuf = get_avatar_pixbuf_from_cache(jid, is_fake, False)
ext = file_path.split('.')[-1]
type_ = ''
if not ext:
pixbuf = get_avatar_pixbuf_from_cache(jid)
path, extension = os.path.splitext(file_path)
if not extension:
# Silently save as Jpeg image
image_format = 'jpeg'
file_path += '.jpeg'
type_ = 'jpeg'
elif ext == 'jpg':
type_ = 'jpeg'
elif extension == 'jpg':
image_format = 'jpeg'
else:
type_ = ext
image_format = extension[1:] # remove leading dot
# Save image
try:
pixbuf.save(file_path, type_)
except Exception:
pixbuf.save(file_path, image_format)
except glib.GError:
if os.path.exists(file_path):
os.remove(file_path)
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
def on_ok(file_path, pixbuf):
pixbuf.save(file_path, 'jpeg')
dialogs.ConfirmationDialog(_('Extension not supported'),
_('Image cannot be saved in %(type)s format. Save as %(new_filename)s?') % {'type': type_, 'new_filename': new_file_path},
_('Image cannot be saved in %(type)s format. Save as %(new_filename)s?'
) % {'type': image_format, 'new_filename': new_file_path},
on_response_ok = (on_ok, new_file_path, pixbuf))
else:
dialog.destroy()
@ -905,7 +906,7 @@ default_name = ''):
current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok,
on_response_cancel=on_cancel)
dialog.set_current_name(default_name)
dialog.set_current_name(default_name + '.jpeg')
dialog.connect('delete-event', lambda widget, event:
on_cancel(widget))

View File

@ -183,14 +183,14 @@ class ProfileWindow:
# Try to get pixbuf
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.jid,
use_local = False)
use_local=False)
if pixbuf:
if pixbuf not in (None, 'ask'):
nick = gajim.config.get_per('accounts', self.account, 'name')
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
menuitem.connect('activate',
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
self.jid, None, nick + '.jpeg')
self.jid, self.account, nick)
menu.append(menuitem)
# show clear
menuitem = gtk.ImageMenuItem(gtk.STOCK_CLEAR)

View File

@ -1295,10 +1295,9 @@ class RosterWindow:
iters = self._get_contact_iter(jid, account, model=self.model)
if not iters or not gajim.config.get('show_avatars_in_roster'):
return
jid = self.model[iters[0]][C_JID]
jid = jid.decode('utf-8')
jid = self.model[iters[0]][C_JID].decode('utf-8')
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid)
if pixbuf is None or pixbuf == 'ask':
if pixbuf in (None, 'ask'):
scaled_pixbuf = None
else:
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'roster')

View File

@ -164,8 +164,7 @@ class VcardWindow:
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
menuitem.connect('activate',
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
self.contact.jid, self.account, self.contact.get_shown_name() +
'.jpeg')
self.contact.jid, self.account, self.contact.get_shown_name())
menu.append(menuitem)
menu.connect('selection-done', lambda w:w.destroy())
# show the menu
@ -480,8 +479,7 @@ class ZeroconfVcardWindow:
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
menuitem.connect('activate',
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
self.contact.jid, self.account, self.contact.get_shown_name() +
'.jpeg')
self.contact.jid, self.account, self.contact.get_shown_name())
menu.append(menuitem)
menu.connect('selection-done', lambda w:w.destroy())
# show the menu