From c0a143c81719879db69b990cf249e33cb109f472 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 16 May 2018 20:25:21 +0300 Subject: [PATCH] identicons basic support --- setup.py | 6 +++++- toxygen/contacts/basecontact.py | 16 ++++++++++------ toxygen/contacts/common.py | 15 +++++++++++++++ toxygen/contacts/contacts_manager.py | 1 + toxygen/contacts/profile.py | 4 ++-- toxygen/file_transfers/file_transfers.py | 3 --- toxygen/file_transfers/file_transfers_handler.py | 2 +- toxygen/ui/menu.py | 2 +- toxygen/user_data/settings.py | 1 + 9 files changed, 36 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index f586ec3..8457ef5 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ version = main.__version__ + '.0' if system() == 'Windows': - MODULES = ['PyQt5', 'PyAudio', 'numpy', 'opencv-python'] + MODULES = ['PyQt5', 'PyAudio', 'numpy', 'opencv-python', 'pydenticon'] else: MODULES = [] try: @@ -29,6 +29,10 @@ else: import cv2 except ImportError: MODULES.append('opencv-python') + try: + import pydenticon + except ImportError: + MODULES.append('pydenticon') class InstallScript(install): diff --git a/toxygen/contacts/basecontact.py b/toxygen/contacts/basecontact.py index 037289c..988b0bc 100644 --- a/toxygen/contacts/basecontact.py +++ b/toxygen/contacts/basecontact.py @@ -3,6 +3,7 @@ from PyQt5 import QtCore, QtGui from wrapper.toxcore_enums_and_consts import TOX_PUBLIC_KEY_SIZE import utils.util as util import common.event as event +import contacts.common as common class BaseContact: @@ -119,14 +120,17 @@ class BaseContact: self._widget.avatar_label.repaint() self._avatar_changed_event(avatar_path) - def reset_avatar(self): + def reset_avatar(self, generate_new): avatar_path = self.get_avatar_path() - if os.path.isfile(avatar_path): + if os.path.isfile(avatar_path) and not avatar_path == self._get_default_avatar_path(): os.remove(avatar_path) + if generate_new: + self.set_avatar(common.generate_avatar(self.tox_id)) + else: self.load_avatar() def set_avatar(self, avatar): - avatar_path = self.get_avatar_path() + avatar_path = self.get_contact_avatar_path() with open(avatar_path, 'wb') as f: f.write(avatar) self.load_avatar() @@ -137,7 +141,7 @@ class BaseContact: def get_avatar_path(self): avatar_path = self.get_contact_avatar_path() if not os.path.isfile(avatar_path) or not os.path.getsize(avatar_path): # load default image - avatar_path = util.join_path(util.get_images_directory(), self._get_default_avatar_name()) + avatar_path = self._get_default_avatar_path() return avatar_path @@ -166,5 +170,5 @@ class BaseContact: # ----------------------------------------------------------------------------------------------------------------- @staticmethod - def _get_default_avatar_name(): - return 'avatar.png' + def _get_default_avatar_path(): + return util.join_path(util.get_images_directory(), 'avatar.png') diff --git a/toxygen/contacts/common.py b/toxygen/contacts/common.py index 584afc3..b37c6a2 100644 --- a/toxygen/contacts/common.py +++ b/toxygen/contacts/common.py @@ -1,3 +1,4 @@ +from pydenticon import Generator class BaseTypingNotificationHandler: @@ -22,3 +23,17 @@ class FriendTypingNotificationHandler(BaseTypingNotificationHandler): BaseTypingNotificationHandler.DEFAULT_HANDLER = BaseTypingNotificationHandler() + + +def generate_avatar(tox_id): + foreground = ["rgb(45,79,255)", + "rgb(254,180,44)", + "rgb(226,121,234)", + "rgb(30,179,253)", + "rgb(232,77,65)", + "rgb(49,203,115)", + "rgb(141,69,170)"] + generator = Generator(5, 5, foreground=foreground, background="rgba(42,42,42,0)") + identicon = generator.generate(tox_id, 220, 220, padding=(10, 10, 10, 10)) + + return identicon diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 7570c06..b4c1803 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -295,6 +295,7 @@ class ContactsManager: self._history.add_friend_to_db(tox_id) friend = self._contact_provider.get_friend_by_public_key(tox_id) self._contacts.append(friend) + friend.reset_avatar() def block_user(self, tox_id): """ diff --git a/toxygen/contacts/profile.py b/toxygen/contacts/profile.py index 1a8fe0f..b4dbaf2 100644 --- a/toxygen/contacts/profile.py +++ b/toxygen/contacts/profile.py @@ -136,8 +136,8 @@ class Profile(basecontact.BaseContact): self._call.stop() del self._call - def reset_avatar(self): - super().reset_avatar() + def reset_avatar(self, generate_new): + super().reset_avatar(generate_new) for friend in filter(lambda x: x.status is not None, self._contacts): self.send_avatar(friend.number) diff --git a/toxygen/file_transfers/file_transfers.py b/toxygen/file_transfers/file_transfers.py index 6673e17..91aeb86 100644 --- a/toxygen/file_transfers/file_transfers.py +++ b/toxygen/file_transfers/file_transfers.py @@ -316,9 +316,6 @@ class ReceiveAvatar(ReceiveTransfer): elif not size: self.send_control(TOX_FILE_CONTROL['CANCEL']) self._file.close() - if exists(path): - remove(path) - self._file.close() remove(full_path) elif exists(path): hash = self.get_file_id() diff --git a/toxygen/file_transfers/file_transfers_handler.py b/toxygen/file_transfers/file_transfers_handler.py index 470299c..6269295 100644 --- a/toxygen/file_transfers/file_transfers_handler.py +++ b/toxygen/file_transfers/file_transfers_handler.py @@ -271,7 +271,7 @@ class FileTransfersHandler: self._file_transfers[(friend_number, file_number)] = ra ra.set_transfer_finished_handler(self.transfer_finished) else: - friend.load_avatar() + friend.reset_avatar(self._settings['identicons']) # ----------------------------------------------------------------------------------------------------------------- # Private methods diff --git a/toxygen/ui/menu.py b/toxygen/ui/menu.py index d023cff..1a33bde 100644 --- a/toxygen/ui/menu.py +++ b/toxygen/ui/menu.py @@ -245,7 +245,7 @@ class ProfileSettings(CenteredWidget): self.tox_id.setText(self._profile.new_nospam()) def reset_avatar(self): - self._profile.reset_avatar() + self._profile.reset_avatar(self._settings['identicons']) def set_avatar(self): choose = util_ui.tr("Choose avatar") diff --git a/toxygen/user_data/settings.py b/toxygen/user_data/settings.py index 8289663..1d7329e 100644 --- a/toxygen/user_data/settings.py +++ b/toxygen/user_data/settings.py @@ -140,6 +140,7 @@ class Settings(dict): 'unread_color': 'red', 'save_unsent_only': False, 'compact_mode': False, + 'identicons': True, 'show_welcome_screen': True, 'close_to_tray': False, 'font': 'Times New Roman',