set custom avatar is now fonctional. fixes #3127

This commit is contained in:
Yann Leboulanger 2007-07-10 17:53:42 +00:00
parent 5a5ed1530c
commit cf814269db
4 changed files with 1672 additions and 2871 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2705,7 +2705,10 @@ class ImageChooserDialog(FileChooserDialog):
path_to_file = gtkgui_helpers.decode_filechooser_file_paths(
(path_to_file,))[0]
if os.path.exists(path_to_file):
callback(widget, path_to_file)
if isinstance(callback, tuple):
callback[0](widget, path_to_file, *callback[1:])
else:
callback(widget, path_to_file)
try:
if os.name == 'nt':
@ -2765,12 +2768,19 @@ class AvatarChooserDialog(ImageChooserDialog):
ImageChooserDialog.__init__(self, path_to_file, on_response_ok,
on_response_cancel)
button = gtk.Button(None, gtk.STOCK_CLEAR)
self.response_clear = on_response_clear
if on_response_clear:
button.connect('clicked', on_response_clear)
button.connect('clicked', self.on_clear)
button.show_all()
self.action_area.pack_start(button)
self.action_area.reorder_child(button, 0)
def on_clear(self, widget):
if isinstance(self.response_clear, tuple):
self.response_clear[0](widget, *self.response_clear[1:])
else:
self.response_clear(widget)
class AddSpecialNotificationDialog:
def __init__(self, jid):
'''jid is the jid for which we want to add special notification

View File

@ -1830,6 +1830,63 @@ class RosterWindow:
keys_str += jid + ' ' + keys[jid] + ' '
gajim.config.set_per('accounts', account, 'attached_gpg_keys', keys_str)
def update_avatar_in_gui(self, jid, account):
# Update roster
self.draw_avatar(jid, account)
# Update chat window
if gajim.interface.msg_win_mgr.has_window(jid, account):
win = gajim.interface.msg_win_mgr.get_window(jid, account)
ctrl = win.get_control(jid, account)
if win and ctrl.type_id != message_control.TYPE_GC:
ctrl.show_avatar()
def on_set_custom_avatar_activate(self, widget, contact, account):
def on_ok(widget, path_to_file):
filesize = os.path.getsize(path_to_file) # in bytes
invalid_file = False
msg = ''
if os.path.isfile(path_to_file):
stat = os.stat(path_to_file)
if stat[6] == 0:
invalid_file = True
msg = _('File is empty')
else:
invalid_file = True
msg = _('File does not exist')
if invalid_file:
dialogs.ErrorDialog(_('Could not load image'), msg)
return
try:
pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
if filesize > 16384: # 16 kb
# get the image at 'notification size'
# and hope that user did not specify in ACE crazy size
pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'tooltip')
except gobject.GError, msg: # unknown format
# msg should be string, not object instance
msg = str(msg)
dialogs.ErrorDialog(_('Could not load image'), msg)
return
puny_jid = helpers.sanitize_filename(contact.jid)
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid) + '_local.png'
pixbuf.save(path_to_file, 'png')
dlg.destroy()
self.update_avatar_in_gui(contact.jid, account)
def on_clear(widget):
dlg.destroy()
# Delete file:
puny_jid = helpers.sanitize_filename(contact.jid)
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)
dlg = dialogs.AvatarChooserDialog(on_response_ok = on_ok,
on_response_clear = on_clear)
def on_edit_groups(self, widget, list_):
dlg = dialogs.EditGroupsDialog(list_)
dlg.run()
@ -1962,7 +2019,7 @@ class RosterWindow:
account)
if _('Not in Roster') not in contact.groups:
#contact is in normal group
# contact is in normal group
edit_groups_menuitem.set_no_show_all(False)
assign_openpgp_key_menuitem.set_no_show_all(False)
edit_groups_menuitem.connect('activate', self.on_edit_groups, [(
@ -2025,6 +2082,7 @@ class RosterWindow:
send_file_menuitem = xml.get_widget('send_file_menuitem')
assign_openpgp_key_menuitem = xml.get_widget(
'assign_openpgp_key_menuitem')
set_custom_avatar_menuitem = xml.get_widget('set_custom_avatar_menuitem')
add_special_notification_menuitem = xml.get_widget(
'add_special_notification_menuitem')
execute_command_menuitem = xml.get_widget(
@ -2211,7 +2269,7 @@ class RosterWindow:
account)
if _('Not in Roster') not in contact.groups:
#contact is in normal group
# contact is in normal group
edit_groups_menuitem.set_no_show_all(False)
assign_openpgp_key_menuitem.set_no_show_all(False)
add_to_roster_menuitem.hide()
@ -2256,6 +2314,8 @@ class RosterWindow:
add_to_roster_menuitem.connect('activate',
self.on_add_to_roster, contact, account)
set_custom_avatar_menuitem.connect('activate',
self.on_set_custom_avatar_activate, contact, account)
# Remove many items when it's self contact row
if our_jid:
menuitem = xml.get_widget('manage_contact')

View File

@ -71,27 +71,17 @@ class VcardWindow:
self.account = account
self.gc_contact = gc_contact
self.xml.get_widget('no_user_avatar_label').set_no_show_all(True)
self.xml.get_widget('no_user_avatar_label').hide()
self.xml.get_widget('PHOTO_image').set_no_show_all(True)
self.xml.get_widget('PHOTO_image').hide()
image = gtk.Image()
self.photo_button = self.xml.get_widget('PHOTO_button')
self.photo_button.set_image(image)
self.nophoto_button = self.xml.get_widget('NOPHOTO_button')
puny_jid = helpers.sanitize_filename(contact.jid)
local_avatar_basepath = os.path.join(gajim.AVATAR_PATH, puny_jid) + \
'_local'
for extension in ('.png', '.jpeg'):
local_avatar_path = local_avatar_basepath + extension
if os.path.isfile(local_avatar_path):
image = self.xml.get_widget('custom_avatar_image')
image.set_from_file(local_avatar_path)
self.nophoto_button.set_no_show_all(True)
self.nophoto_button.hide()
image.show()
self.xml.get_widget('custom_avatar_label').show()
break
else:
self.photo_button.set_no_show_all(True)
self.photo_button.hide()
self.avatar_mime_type = None
self.avatar_encoded = None
self.vcard_arrived = False
@ -124,94 +114,6 @@ class VcardWindow:
if win and ctrl.type_id != message_control.TYPE_GC:
ctrl.show_avatar()
def on_NOPHOTO_button_clicked(self, button):
def on_ok(widget, path_to_file):
filesize = os.path.getsize(path_to_file) # in bytes
invalid_file = False
msg = ''
if os.path.isfile(path_to_file):
stat = os.stat(path_to_file)
if stat[6] == 0:
invalid_file = True
msg = _('File is empty')
else:
invalid_file = True
msg = _('File does not exist')
if invalid_file:
dialogs.ErrorDialog(_('Could not load image'), msg)
return
try:
pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
if filesize > 16384: # 16 kb
# get the image at 'notification size'
# and hope that user did not specify in ACE crazy size
pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'tooltip')
except gobject.GError, msg: # unknown format
# msg should be string, not object instance
msg = str(msg)
dialogs.ErrorDialog(_('Could not load image'), msg)
return
puny_jid = helpers.sanitize_filename(self.contact.jid)
path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid) + '_local.png'
pixbuf.save(path_to_file, 'png')
self.dialog.destroy()
self.update_avatar_in_gui()
# rescale it
pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'vcard')
image = self.photo_button.get_image()
image.set_from_pixbuf(pixbuf)
self.photo_button.show()
self.nophoto_button.hide()
def on_clear(widget):
self.dialog.destroy()
self.on_clear_button_clicked(widget)
self.dialog = dialogs.AvatarChooserDialog(on_response_ok = on_ok,
on_response_clear = on_clear)
def on_clear_button_clicked(self, widget):
# empty the image
image = self.photo_button.get_image()
image.set_from_pixbuf(None)
self.photo_button.hide()
self.nophoto_button.show()
# Delete file:
puny_jid = helpers.sanitize_filename(self.contact.jid)
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()
def on_PHOTO_button_press_event(self, widget, event):
'''If right-clicked, show popup'''
if event.button == 3 and self.avatar_encoded: # right click
menu = gtk.Menu()
# Try to get pixbuf
# pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(self.jid)
# if pixbuf:
# nick = self.contact.get_shown_name()
# menuitem = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
# menuitem.connect('activate',
# gtkgui_helpers.on_avatar_save_as_menuitem_activate, self.jid,
# None, nick + '.jpeg')
# menu.append(menuitem)
# show clear
menuitem = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
menuitem.connect('activate', self.on_clear_button_clicked)
menu.append(menuitem)
menu.connect('selection-done', lambda w:w.destroy())
# show the menu
menu.show_all()
menu.popup(None, None, None, event.button, event.time)
elif event.button == 1: # left click
self.on_NOPHOTO_button_clicked(widget)
def on_vcard_information_window_destroy(self, widget):
if self.update_progressbar_timeout_id is not None:
gobject.source_remove(self.update_progressbar_timeout_id)