From 0f9aa4f51561dbca3f1ffd84788eb4a03e81f009 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 23 Aug 2018 23:51:05 +0300 Subject: [PATCH] refactoring and is_muted fix --- toxygen/bootstrap/bootstrap.py | 30 +++++++++++++-------------- toxygen/contacts/basecontact.py | 31 +++++++++++++++------------- toxygen/contacts/common.py | 9 +++++++++ toxygen/groups/group_peer.py | 36 +++++++++++++++++++++++---------- toxygen/wrapper/tox.py | 2 ++ 5 files changed, 68 insertions(+), 40 deletions(-) diff --git a/toxygen/bootstrap/bootstrap.py b/toxygen/bootstrap/bootstrap.py index 6c34e0e..fad68c4 100644 --- a/toxygen/bootstrap/bootstrap.py +++ b/toxygen/bootstrap/bootstrap.py @@ -20,11 +20,7 @@ class Node: priority = property(get_priority) def get_data(self): - return bytes(self._ip, 'utf-8'), self._port, self._tox_key - - -def _get_nodes_path(): - return join_path(curr_directory(__file__), 'nodes.json') + return self._ip, self._port, self._tox_key def generate_nodes(nodes_count=DEFAULT_NODES_COUNT): @@ -39,14 +35,6 @@ def generate_nodes(nodes_count=DEFAULT_NODES_COUNT): yield node.get_data() -def save_nodes(nodes): - if not nodes: - return - print('Saving nodes...') - with open(_get_nodes_path(), 'wb') as fl: - fl.write(nodes) - - def download_nodes_list(settings): url = 'https://nodes.tox.chat/json' if not settings['download_nodes_list']: @@ -58,7 +46,7 @@ def download_nodes_list(settings): req.add_header('Content-Type', 'application/json') response = urllib.request.urlopen(req) result = response.read() - save_nodes(result) + _save_nodes(result) except Exception as ex: log('TOX nodes loading error: ' + str(ex)) else: # proxy @@ -78,6 +66,18 @@ def download_nodes_list(settings): QtCore.QThread.msleep(1) QtCore.QCoreApplication.processEvents() data = bytes(reply.readAll().data()) - save_nodes(data) + _save_nodes(data) except Exception as ex: log('TOX nodes loading error: ' + str(ex)) + + +def _get_nodes_path(): + return join_path(curr_directory(__file__), 'nodes.json') + + +def _save_nodes(nodes): + if not nodes: + return + print('Saving nodes...') + with open(_get_nodes_path(), 'wb') as fl: + fl.write(nodes) diff --git a/toxygen/contacts/basecontact.py b/toxygen/contacts/basecontact.py index fa709db..c64ffdb 100644 --- a/toxygen/contacts/basecontact.py +++ b/toxygen/contacts/basecontact.py @@ -39,11 +39,12 @@ class BaseContact: return self._name def set_name(self, value): - if self._name != value: - self._name = value - self._widget.name.setText(self._name) - self._widget.name.repaint() - self._name_changed_event(self._name) + if self._name == value: + return + self._name = value + self._widget.name.setText(self._name) + self._widget.name.repaint() + self._name_changed_event(self._name) name = property(get_name, set_name) @@ -60,11 +61,12 @@ class BaseContact: return self._status_message def set_status_message(self, value): - if self._status_message != value: - self._status_message = value - self._widget.status_message.setText(self._status_message) - self._widget.status_message.repaint() - self._status_message_changed_event(self._status_message) + if self._status_message == value: + return + self._status_message = value + self._widget.status_message.setText(self._status_message) + self._widget.status_message.repaint() + self._status_message_changed_event(self._status_message) status_message = property(get_status_message, set_status_message) @@ -81,10 +83,11 @@ class BaseContact: return self._status def set_status(self, value): - if self._status != value: - self._status = value - self._widget.connection_status.update(value) - self._status_changed_event(self._status) + if self._status == value: + return + self._status = value + self._widget.connection_status.update(value) + self._status_changed_event(self._status) status = property(get_status, set_status) diff --git a/toxygen/contacts/common.py b/toxygen/contacts/common.py index ba5dac6..27750a2 100644 --- a/toxygen/contacts/common.py +++ b/toxygen/contacts/common.py @@ -2,6 +2,10 @@ from pydenticon import Generator import hashlib +# ----------------------------------------------------------------------------------------------------------------- +# Typing notifications +# ----------------------------------------------------------------------------------------------------------------- + class BaseTypingNotificationHandler: DEFAULT_HANDLER = None @@ -26,6 +30,11 @@ class FriendTypingNotificationHandler(BaseTypingNotificationHandler): BaseTypingNotificationHandler.DEFAULT_HANDLER = BaseTypingNotificationHandler() +# ----------------------------------------------------------------------------------------------------------------- +# Identicons support +# ----------------------------------------------------------------------------------------------------------------- + + def generate_avatar(public_key): foreground = ['rgb(45,79,255)', 'rgb(185, 66, 244)', 'rgb(185, 66, 244)', 'rgb(254,180,44)', 'rgb(252, 2, 2)', 'rgb(109, 198, 0)', diff --git a/toxygen/groups/group_peer.py b/toxygen/groups/group_peer.py index 57724a5..4eaf255 100644 --- a/toxygen/groups/group_peer.py +++ b/toxygen/groups/group_peer.py @@ -1,6 +1,9 @@ class GroupChatPeer: + """ + Represents peer in group chat. + """ def __init__(self, peer_id, name, status, role, public_key, is_current_user=False, is_muted=False): self._peer_id = peer_id @@ -11,11 +14,29 @@ class GroupChatPeer: self._is_current_user = is_current_user self._is_muted = is_muted + # ----------------------------------------------------------------------------------------------------------------- + # Readonly properties + # ----------------------------------------------------------------------------------------------------------------- + def get_id(self): return self._peer_id id = property(get_id) + def get_public_key(self): + return self._public_key + + public_key = property(get_public_key) + + def get_is_current_user(self): + return self._is_current_user + + is_current_user = property(get_is_current_user) + + # ----------------------------------------------------------------------------------------------------------------- + # Read-write properties + # ----------------------------------------------------------------------------------------------------------------- + def get_name(self): return self._name @@ -40,17 +61,10 @@ class GroupChatPeer: role = property(get_role, set_role) - def get_public_key(self): - return self._public_key - - public_key = property(get_public_key) - - def get_is_current_user(self): - return self._is_current_user - - is_current_user = property(get_is_current_user) - def get_is_muted(self): return self._is_muted - is_muted = property(get_is_muted) + def set_is_muted(self, is_muted): + self._is_muted = is_muted + + is_muted = property(get_is_muted, set_is_muted) diff --git a/toxygen/wrapper/tox.py b/toxygen/wrapper/tox.py index fcdf266..1ba0bbc 100644 --- a/toxygen/wrapper/tox.py +++ b/toxygen/wrapper/tox.py @@ -218,6 +218,7 @@ class Tox: :param public_key: The long term public key of the bootstrap node (TOX_PUBLIC_KEY_SIZE bytes). :return: True on success. """ + address = bytes(address, 'utf-8') tox_err_bootstrap = c_int() result = Tox.libtoxcore.tox_bootstrap(self._tox_pointer, c_char_p(address), c_uint16(port), string_to_bin(public_key), byref(tox_err_bootstrap)) @@ -244,6 +245,7 @@ class Tox: :param public_key: The long term public key of the TCP relay (TOX_PUBLIC_KEY_SIZE bytes). :return: True on success. """ + address = bytes(address, 'utf-8') tox_err_bootstrap = c_int() result = Tox.libtoxcore.tox_add_tcp_relay(self._tox_pointer, c_char_p(address), c_uint16(port), string_to_bin(public_key), byref(tox_err_bootstrap))