Generalize save_avatar_files() and generate notif and bw version for local avatars.
This commit is contained in:
parent
09388ddf1a
commit
b1136a6959
41
src/gajim.py
41
src/gajim.py
|
@ -1463,50 +1463,63 @@ class Interface:
|
||||||
if self.remote_ctrl:
|
if self.remote_ctrl:
|
||||||
self.remote_ctrl.raise_signal('NewGmail', (account, array))
|
self.remote_ctrl.raise_signal('NewGmail', (account, array))
|
||||||
|
|
||||||
def save_avatar_files(self, jid, photo_decoded, puny_nick = None):
|
def save_avatar_files(self, jid, photo, puny_nick = None, local = False):
|
||||||
'''Save the decoded avatar to a separate file, and generate files for dbus notifications'''
|
'''Saves an avatar to a separate file, and generate files for dbus notifications. An avatar can be given as a pixmap directly or as an decoded image.'''
|
||||||
puny_jid = helpers.sanitize_filename(jid)
|
puny_jid = helpers.sanitize_filename(jid)
|
||||||
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid)
|
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid)
|
||||||
if puny_nick:
|
if puny_nick:
|
||||||
path_to_file = os.path.join(path_to_file, puny_nick)
|
path_to_file = os.path.join(path_to_file, puny_nick)
|
||||||
# remove old avatars
|
# remove old avatars
|
||||||
for typ in ('jpeg', 'png'):
|
for typ in ('jpeg', 'png'):
|
||||||
|
if local:
|
||||||
|
path_to_original_file = path_to_file + '_local'+ '.' + typ
|
||||||
|
else:
|
||||||
path_to_original_file = path_to_file + '.' + typ
|
path_to_original_file = path_to_file + '.' + typ
|
||||||
if os.path.isfile(path_to_original_file):
|
if os.path.isfile(path_to_original_file):
|
||||||
os.remove(path_to_original_file)
|
os.remove(path_to_original_file)
|
||||||
pixbuf, typ = gtkgui_helpers.get_pixbuf_from_data(photo_decoded,
|
if local and photo:
|
||||||
want_type = True)
|
pixbuf = photo
|
||||||
|
type = 'png'
|
||||||
|
extension = '_local.png' # save local avatars as png file
|
||||||
|
else:
|
||||||
|
pixbuf, typ = gtkgui_helpers.get_pixbuf_from_data(photo, want_type = True)
|
||||||
|
extension = '.' + typ
|
||||||
if pixbuf is None:
|
if pixbuf is None:
|
||||||
return
|
return
|
||||||
if typ not in ('jpeg', 'png'):
|
if typ not in ('jpeg', 'png'):
|
||||||
gajim.log.debug('gtkpixbuf cannot save other than jpeg and png formats. saving %s\'avatar as png file (originaly %s)' % (jid, typ))
|
gajim.log.debug('gtkpixbuf cannot save other than jpeg and png formats. saving %s\'avatar as png file (originaly %s)' % (jid, typ))
|
||||||
typ = 'png'
|
typ = 'png'
|
||||||
path_to_original_file = path_to_file + '.' + typ
|
extension = '.png'
|
||||||
|
path_to_original_file = path_to_file + extension
|
||||||
pixbuf.save(path_to_original_file, typ)
|
pixbuf.save(path_to_original_file, typ)
|
||||||
# Generate and save the resized, color avatar
|
# Generate and save the resized, color avatar
|
||||||
pixbuf = gtkgui_helpers.get_scaled_pixbuf(
|
pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'notification')
|
||||||
gtkgui_helpers.get_pixbuf_from_data(photo_decoded), 'notification')
|
|
||||||
if pixbuf:
|
if pixbuf:
|
||||||
path_to_normal_file = path_to_file + '_notif_size_colored.png'
|
path_to_normal_file = path_to_file + '_notif_size_colored' + extension
|
||||||
pixbuf.save(path_to_normal_file, 'png')
|
pixbuf.save(path_to_normal_file, 'png')
|
||||||
# Generate and save the resized, black and white avatar
|
# Generate and save the resized, black and white avatar
|
||||||
bwbuf = gtkgui_helpers.get_scaled_pixbuf(
|
bwbuf = gtkgui_helpers.get_scaled_pixbuf(
|
||||||
gtkgui_helpers.make_pixbuf_grayscale(pixbuf), 'notification')
|
gtkgui_helpers.make_pixbuf_grayscale(pixbuf), 'notification')
|
||||||
if bwbuf:
|
if bwbuf:
|
||||||
path_to_bw_file = path_to_file + '_notif_size_bw.png'
|
path_to_bw_file = path_to_file + '_notif_size_bw' + extension
|
||||||
bwbuf.save(path_to_bw_file, 'png')
|
bwbuf.save(path_to_bw_file, 'png')
|
||||||
|
|
||||||
def remove_avatar_files(self, jid, puny_nick = None):
|
def remove_avatar_files(self, jid, puny_nick = None, local = False):
|
||||||
'''remove avatar files of a jid'''
|
'''remove avatar files of a jid'''
|
||||||
puny_jid = helpers.sanitize_filename(jid)
|
puny_jid = helpers.sanitize_filename(jid)
|
||||||
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid)
|
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid)
|
||||||
if puny_nick:
|
if puny_nick:
|
||||||
path_to_file = os.path.join(path_to_file, puny_nick)
|
path_to_file = os.path.join(path_to_file, puny_nick)
|
||||||
for ext in ('.jpeg', '.png', '_notif_size_colored.png',
|
for ext in ('.jpeg', '.png'):
|
||||||
'_notif_size_bw.png'):
|
if local:
|
||||||
|
ext = '_local' + ext
|
||||||
path_to_original_file = path_to_file + ext
|
path_to_original_file = path_to_file + ext
|
||||||
if os.path.isfile(path_to_original_file):
|
if os.path.isfile(path_to_file + ext):
|
||||||
os.remove(path_to_original_file)
|
os.remove(path_to_file + ext)
|
||||||
|
if os.path.isfile(path_to_file + '_notif_size_colored' + ext):
|
||||||
|
os.remove(path_to_file + '_notif_size_colored' + ext)
|
||||||
|
if os.path.isfile(path_to_file + '_notif_size_bw' + ext):
|
||||||
|
os.remove(path_to_file + '_notif_size_bw' + ext)
|
||||||
|
|
||||||
# list the retained secrets we have for a local account and a remote jid
|
# list the retained secrets we have for a local account and a remote jid
|
||||||
def list_secrets(self, account, jid):
|
def list_secrets(self, account, jid):
|
||||||
|
|
|
@ -1888,7 +1888,7 @@ class RosterWindow:
|
||||||
try:
|
try:
|
||||||
pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
|
pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
|
||||||
if filesize > 16384: # 16 kb
|
if filesize > 16384: # 16 kb
|
||||||
# get the image at 'notification size'
|
# get the image at 'tooltip size'
|
||||||
# and hope that user did not specify in ACE crazy size
|
# and hope that user did not specify in ACE crazy size
|
||||||
pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'tooltip')
|
pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'tooltip')
|
||||||
except gobject.GError, msg: # unknown format
|
except gobject.GError, msg: # unknown format
|
||||||
|
@ -1896,21 +1896,14 @@ class RosterWindow:
|
||||||
msg = str(msg)
|
msg = str(msg)
|
||||||
dialogs.ErrorDialog(_('Could not load image'), msg)
|
dialogs.ErrorDialog(_('Could not load image'), msg)
|
||||||
return
|
return
|
||||||
puny_jid = helpers.sanitize_filename(contact.jid)
|
gajim.interface.save_avatar_files(contact.jid, pixbuf, local = True)
|
||||||
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid) + '_local.png'
|
|
||||||
pixbuf.save(path_to_file, 'png')
|
|
||||||
dlg.destroy()
|
dlg.destroy()
|
||||||
self.update_avatar_in_gui(contact.jid, account)
|
self.update_avatar_in_gui(contact.jid, account)
|
||||||
|
|
||||||
def on_clear(widget):
|
def on_clear(widget):
|
||||||
dlg.destroy()
|
dlg.destroy()
|
||||||
# Delete file:
|
# Delete file:
|
||||||
puny_jid = helpers.sanitize_filename(contact.jid)
|
gajim.interface.remove_avatar_files(contact.jid, local = True)
|
||||||
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid) + '_local.png'
|
|
||||||
try:
|
|
||||||
os.remove(path_to_file)
|
|
||||||
except OSError:
|
|
||||||
gajim.log.debug('Cannot remove %s' % path_to_file)
|
|
||||||
self.update_avatar_in_gui(contact.jid, account)
|
self.update_avatar_in_gui(contact.jid, account)
|
||||||
|
|
||||||
dlg = dialogs.AvatarChooserDialog(on_response_ok = on_ok,
|
dlg = dialogs.AvatarChooserDialog(on_response_ok = on_ok,
|
||||||
|
|
Loading…
Reference in New Issue