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:
parent
b2efc9685b
commit
9b7ae8cac3
|
@ -1351,7 +1351,7 @@ class ChatControl(ChatControlBase):
|
||||||
# per jid
|
# per jid
|
||||||
self.show_bigger_avatar_timeout_id = None
|
self.show_bigger_avatar_timeout_id = None
|
||||||
self.bigger_avatar_window = None
|
self.bigger_avatar_window = None
|
||||||
self.show_avatar(self.contact.resource)
|
self.show_avatar()
|
||||||
|
|
||||||
# chatstate timers and state
|
# chatstate timers and state
|
||||||
self.reset_kbd_mouse_timeout_vars()
|
self.reset_kbd_mouse_timeout_vars()
|
||||||
|
@ -1601,11 +1601,7 @@ class ChatControl(ChatControlBase):
|
||||||
bigger avatar after 0.5 sec
|
bigger avatar after 0.5 sec
|
||||||
"""
|
"""
|
||||||
jid = self.contact.jid
|
jid = self.contact.jid
|
||||||
is_fake = False
|
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid)
|
||||||
if self.type_id == message_control.TYPE_PM:
|
|
||||||
is_fake = True
|
|
||||||
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid,
|
|
||||||
is_fake)
|
|
||||||
if avatar_pixbuf in ('ask', None):
|
if avatar_pixbuf in ('ask', None):
|
||||||
return
|
return
|
||||||
avatar_w = avatar_pixbuf.get_width()
|
avatar_w = avatar_pixbuf.get_width()
|
||||||
|
@ -1638,8 +1634,7 @@ class ChatControl(ChatControlBase):
|
||||||
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
||||||
id_ = menuitem.connect('activate',
|
id_ = menuitem.connect('activate',
|
||||||
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
||||||
self.contact.jid, self.account, self.contact.get_shown_name() + \
|
self.contact.jid, self.account, self.contact.get_shown_name())
|
||||||
'.jpeg')
|
|
||||||
self.handlers[id_] = menuitem
|
self.handlers[id_] = menuitem
|
||||||
menu.append(menuitem)
|
menu.append(menuitem)
|
||||||
menu.show_all()
|
menu.show_all()
|
||||||
|
@ -2403,24 +2398,12 @@ class ChatControl(ChatControlBase):
|
||||||
# Re-show the small avatar
|
# Re-show the small avatar
|
||||||
self.show_avatar()
|
self.show_avatar()
|
||||||
|
|
||||||
def show_avatar(self, resource = None):
|
def show_avatar(self):
|
||||||
if not gajim.config.get('show_avatar_in_chat'):
|
if not gajim.config.get('show_avatar_in_chat'):
|
||||||
return
|
return
|
||||||
|
|
||||||
is_fake = False
|
jid_with_resource = self.contact.get_full_jid()
|
||||||
if self.TYPE_ID == message_control.TYPE_PM:
|
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid_with_resource)
|
||||||
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)
|
|
||||||
if pixbuf == 'ask':
|
if pixbuf == 'ask':
|
||||||
# we don't have the vcard
|
# we don't have the vcard
|
||||||
if self.TYPE_ID == message_control.TYPE_PM:
|
if self.TYPE_ID == message_control.TYPE_PM:
|
||||||
|
@ -2436,8 +2419,10 @@ class ChatControl(ChatControlBase):
|
||||||
else:
|
else:
|
||||||
gajim.connections[self.account].request_vcard(jid_with_resource)
|
gajim.connections[self.account].request_vcard(jid_with_resource)
|
||||||
return
|
return
|
||||||
if pixbuf is not None:
|
elif pixbuf:
|
||||||
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'chat')
|
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'chat')
|
||||||
|
else:
|
||||||
|
scaled_pixbuf = None
|
||||||
|
|
||||||
image = self.xml.get_widget('avatar_image')
|
image = self.xml.get_widget('avatar_image')
|
||||||
image.set_from_pixbuf(scaled_pixbuf)
|
image.set_from_pixbuf(scaled_pixbuf)
|
||||||
|
@ -2629,11 +2614,8 @@ class ChatControl(ChatControlBase):
|
||||||
if not small_avatar.window:
|
if not small_avatar.window:
|
||||||
# Tab has been closed since we hovered the avatar
|
# Tab has been closed since we hovered the avatar
|
||||||
return
|
return
|
||||||
is_fake = False
|
|
||||||
if self.type_id == message_control.TYPE_PM:
|
|
||||||
is_fake = True
|
|
||||||
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(
|
avatar_pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(
|
||||||
self.contact.jid, is_fake)
|
self.contact.jid)
|
||||||
if avatar_pixbuf in ('ask', None):
|
if avatar_pixbuf in ('ask', None):
|
||||||
return
|
return
|
||||||
# Hide the small avatar
|
# Hide the small avatar
|
||||||
|
|
|
@ -373,14 +373,6 @@ class Contacts:
|
||||||
nbr_total += 1
|
nbr_total += 1
|
||||||
return nbr_online, nbr_total
|
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):
|
def __getattr__(self, attr_name):
|
||||||
# Only called if self has no attr_name
|
# Only called if self has no attr_name
|
||||||
if hasattr(self._metacontact_manager, attr_name):
|
if hasattr(self._metacontact_manager, attr_name):
|
||||||
|
|
|
@ -1171,8 +1171,8 @@ class GroupchatControl(ChatControlBase):
|
||||||
iter_ = self.get_contact_iter(nick)
|
iter_ = self.get_contact_iter(nick)
|
||||||
if not iter_:
|
if not iter_:
|
||||||
return
|
return
|
||||||
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.room_jid + \
|
fake_jid = self.room_jid + '/' + nick
|
||||||
'/' + nick, True)
|
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(fake_jid)
|
||||||
if pixbuf in ('ask', None):
|
if pixbuf in ('ask', None):
|
||||||
scaled_pixbuf = None
|
scaled_pixbuf = None
|
||||||
else:
|
else:
|
||||||
|
@ -1514,7 +1514,7 @@ class GroupchatControl(ChatControlBase):
|
||||||
if gajim.config.get('ask_avatars_on_startup') and \
|
if gajim.config.get('ask_avatars_on_startup') and \
|
||||||
not server.startswith('irc'):
|
not server.startswith('irc'):
|
||||||
fake_jid = self.room_jid + '/' + nick
|
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 pixbuf == 'ask':
|
||||||
if j:
|
if j:
|
||||||
fjid = j
|
fjid = j
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
import xml.sax.saxutils
|
import xml.sax.saxutils
|
||||||
import gtk
|
import gtk
|
||||||
import gtk.glade
|
import gtk.glade
|
||||||
|
import glib
|
||||||
import gobject
|
import gobject
|
||||||
import pango
|
import pango
|
||||||
import os
|
import os
|
||||||
|
@ -593,7 +594,7 @@ def get_scaled_pixbuf(pixbuf, kind):
|
||||||
scaled_buf = pixbuf.scale_simple(w, h, gtk.gdk.INTERP_HYPER)
|
scaled_buf = pixbuf.scale_simple(w, h, gtk.gdk.INTERP_HYPER)
|
||||||
return scaled_buf
|
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
|
Check if jid has cached avatar and if that avatar is valid image (can be
|
||||||
shown)
|
shown)
|
||||||
|
@ -608,8 +609,14 @@ def get_avatar_pixbuf_from_cache(fjid, is_fake_jid = False, use_local = True):
|
||||||
# don't show avatar for the transport itself
|
# don't show avatar for the transport itself
|
||||||
return None
|
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)
|
puny_jid = helpers.sanitize_filename(jid)
|
||||||
if is_fake_jid:
|
if is_groupchat_contact:
|
||||||
puny_nick = helpers.sanitize_filename(nick)
|
puny_nick = helpers.sanitize_filename(nick)
|
||||||
path = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
|
path = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
|
||||||
local_avatar_basepath = os.path.join(gajim.AVATAR_PATH, puny_jid,
|
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'
|
return 'ask'
|
||||||
|
|
||||||
vcard_dict = gajim.connections.values()[0].get_cached_vcard(fjid,
|
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
|
if not vcard_dict: # This can happen if cached vcard is too old
|
||||||
return 'ask'
|
return 'ask'
|
||||||
if 'PHOTO' not in vcard_dict:
|
if 'PHOTO' not in vcard_dict:
|
||||||
|
@ -831,39 +838,33 @@ def get_possible_button_event(event):
|
||||||
def destroy_widget(widget):
|
def destroy_widget(widget):
|
||||||
widget.destroy()
|
widget.destroy()
|
||||||
|
|
||||||
def on_avatar_save_as_menuitem_activate(widget, jid, account,
|
def on_avatar_save_as_menuitem_activate(widget, jid, account, default_name=''):
|
||||||
default_name = ''):
|
|
||||||
def on_continue(response, file_path):
|
def on_continue(response, file_path):
|
||||||
if response < 0:
|
if response < 0:
|
||||||
return
|
return
|
||||||
# Get pixbuf
|
pixbuf = get_avatar_pixbuf_from_cache(jid)
|
||||||
pixbuf = None
|
path, extension = os.path.splitext(file_path)
|
||||||
is_fake = False
|
if not extension:
|
||||||
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:
|
|
||||||
# Silently save as Jpeg image
|
# Silently save as Jpeg image
|
||||||
|
image_format = 'jpeg'
|
||||||
file_path += '.jpeg'
|
file_path += '.jpeg'
|
||||||
type_ = 'jpeg'
|
elif extension == 'jpg':
|
||||||
elif ext == 'jpg':
|
image_format = 'jpeg'
|
||||||
type_ = 'jpeg'
|
|
||||||
else:
|
else:
|
||||||
type_ = ext
|
image_format = extension[1:] # remove leading dot
|
||||||
|
|
||||||
# Save image
|
# Save image
|
||||||
try:
|
try:
|
||||||
pixbuf.save(file_path, type_)
|
pixbuf.save(file_path, image_format)
|
||||||
except Exception:
|
except glib.GError:
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
|
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
|
||||||
def on_ok(file_path, pixbuf):
|
def on_ok(file_path, pixbuf):
|
||||||
pixbuf.save(file_path, 'jpeg')
|
pixbuf.save(file_path, 'jpeg')
|
||||||
dialogs.ConfirmationDialog(_('Extension not supported'),
|
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))
|
on_response_ok = (on_ok, new_file_path, pixbuf))
|
||||||
else:
|
else:
|
||||||
dialog.destroy()
|
dialog.destroy()
|
||||||
|
@ -905,7 +906,7 @@ default_name = ''):
|
||||||
current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok,
|
current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok,
|
||||||
on_response_cancel=on_cancel)
|
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:
|
dialog.connect('delete-event', lambda widget, event:
|
||||||
on_cancel(widget))
|
on_cancel(widget))
|
||||||
|
|
||||||
|
|
|
@ -183,14 +183,14 @@ class ProfileWindow:
|
||||||
|
|
||||||
# Try to get pixbuf
|
# Try to get pixbuf
|
||||||
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.jid,
|
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')
|
nick = gajim.config.get_per('accounts', self.account, 'name')
|
||||||
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
||||||
menuitem.connect('activate',
|
menuitem.connect('activate',
|
||||||
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
||||||
self.jid, None, nick + '.jpeg')
|
self.jid, self.account, nick)
|
||||||
menu.append(menuitem)
|
menu.append(menuitem)
|
||||||
# show clear
|
# show clear
|
||||||
menuitem = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
|
menuitem = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
|
||||||
|
|
|
@ -1295,10 +1295,9 @@ class RosterWindow:
|
||||||
iters = self._get_contact_iter(jid, account, model=self.model)
|
iters = self._get_contact_iter(jid, account, model=self.model)
|
||||||
if not iters or not gajim.config.get('show_avatars_in_roster'):
|
if not iters or not gajim.config.get('show_avatars_in_roster'):
|
||||||
return
|
return
|
||||||
jid = self.model[iters[0]][C_JID]
|
jid = self.model[iters[0]][C_JID].decode('utf-8')
|
||||||
jid = jid.decode('utf-8')
|
|
||||||
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid)
|
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid)
|
||||||
if pixbuf is None or pixbuf == 'ask':
|
if pixbuf in (None, 'ask'):
|
||||||
scaled_pixbuf = None
|
scaled_pixbuf = None
|
||||||
else:
|
else:
|
||||||
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'roster')
|
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'roster')
|
||||||
|
|
|
@ -164,8 +164,7 @@ class VcardWindow:
|
||||||
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
||||||
menuitem.connect('activate',
|
menuitem.connect('activate',
|
||||||
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
||||||
self.contact.jid, self.account, self.contact.get_shown_name() +
|
self.contact.jid, self.account, self.contact.get_shown_name())
|
||||||
'.jpeg')
|
|
||||||
menu.append(menuitem)
|
menu.append(menuitem)
|
||||||
menu.connect('selection-done', lambda w:w.destroy())
|
menu.connect('selection-done', lambda w:w.destroy())
|
||||||
# show the menu
|
# show the menu
|
||||||
|
@ -480,8 +479,7 @@ class ZeroconfVcardWindow:
|
||||||
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
|
||||||
menuitem.connect('activate',
|
menuitem.connect('activate',
|
||||||
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
gtkgui_helpers.on_avatar_save_as_menuitem_activate,
|
||||||
self.contact.jid, self.account, self.contact.get_shown_name() +
|
self.contact.jid, self.account, self.contact.get_shown_name())
|
||||||
'.jpeg')
|
|
||||||
menu.append(menuitem)
|
menu.append(menuitem)
|
||||||
menu.connect('selection-done', lambda w:w.destroy())
|
menu.connect('selection-done', lambda w:w.destroy())
|
||||||
# show the menu
|
# show the menu
|
||||||
|
|
Loading…
Reference in New Issue