From e9db205110b605274d8089829772692d3328f799 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Tue, 28 Nov 2006 15:40:08 +0000
Subject: [PATCH] remove avatar files when we publish a vcard without an
 avatar. see #2718

---
 src/common/connection_handlers.py |  4 +++-
 src/gajim.py                      | 10 ++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index 89ebd89460..050884558e 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -940,15 +940,17 @@ class ConnectionVcard:
 		iq.setID(id)
 		self.connection.send(iq)
 
+		our_jid = gajim.get_jid_from_account(self.name)
 		# Add the sha of the avatar
 		if vcard.has_key('PHOTO') and isinstance(vcard['PHOTO'], dict) and \
 		vcard['PHOTO'].has_key('BINVAL'):
 			photo = vcard['PHOTO']['BINVAL']
 			photo_decoded = base64.decodestring(photo)
-			our_jid = gajim.get_jid_from_account(self.name)
 			gajim.interface.save_avatar_files(our_jid, photo_decoded)
 			avatar_sha = sha.sha(photo_decoded).hexdigest()
 			iq2.getTag('PHOTO').setTagData('SHA', avatar_sha)
+		else:
+			gajim.interface.remove_avatar_files(our_jid)
 
 		self.awaiting_answers[id] = (VCARD_PUBLISHED, iq2)
 	
diff --git a/src/gajim.py b/src/gajim.py
index 7e06295814..cdc8af0a50 100755
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -1196,6 +1196,16 @@ class Interface:
 				path_to_bw_file = path_to_file + '_notif_size_bw.png'
 				bwbuf.save(path_to_bw_file, 'png')
 
+	def remove_avatar_files(self, jid):
+		'''remove avatar files of a jid'''
+		puny_jid = helpers.sanitize_filename(jid)
+		path_to_file = os.path.join(gajim.AVATAR_PATH, puny_jid)
+		for ext in ('.jpeg', '.png', '_notif_size_colored.png',
+		'_notif_size_bw.png'):
+			path_to_original_file = path_to_file + ext
+			if os.path.isfile(path_to_original_file):
+				os.remove(path_to_original_file)
+
 	def add_event(self, account, jid, type_, event_args):
 		'''add an event to the gajim.events var'''
 		# We add it to the gajim.events queue
-- 
GitLab