From 9768cfdd1ed3fb54e556fe07cb9ba9d369690711 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 17 Mar 2016 20:36:34 +0300 Subject: [PATCH] avatars of friends support --- src/callbacks.py | 10 +++++----- src/profile.py | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 683f73d..548a5b4 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -159,11 +159,11 @@ def tox_file_recv(window, tray): def file_recv_chunk(tox, friend_number, file_number, position, chunk, length, user_data): - print 'Len:', type(length) - print 'Chunk', type(chunk) - print 'Pos', type(position) - #chunk = cast(pointer(chunk), c_char_p)[0] - Profile.get_instance().incoming_chunk(friend_number, file_number, position, chunk[:length] if length else None) + invoke_in_main_thread(Profile.get_instance().incoming_chunk, + friend_number, + file_number, + position, + chunk[:length] if length else None) # ----------------------------------------------------------------------------------------------------------------- # Callbacks - initialization # ----------------------------------------------------------------------------------------------------------------- diff --git a/src/profile.py b/src/profile.py index 5ce5cf2..9fc3720 100644 --- a/src/profile.py +++ b/src/profile.py @@ -313,7 +313,7 @@ class Profile(Contact, Singleton): self._screen = screen self._messages = screen.messages self._tox = tox - self._file_transfers = [] # list of file transfers + self._file_transfers = {} # dict of file transfers settings = Settings.get_instance() self._show_online = settings['show_online_friends'] screen.online_contacts.setChecked(self._show_online) @@ -705,7 +705,7 @@ class Profile(Contact, Singleton): def incoming_file_transfer(self, friend_number, file_number, size, file_name): rt = ReceiveTransfer(Settings.get_default_path() + file_name, self._tox, friend_number) - self._file_transfers.append(rt) + self._file_transfers[(friend_number, file_number)] = rt self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['RESUME']) def incoming_avatar(self, friend_number, file_number, size): @@ -724,13 +724,19 @@ class Profile(Contact, Singleton): new_avatar_hash = self._tox.file_get_file_id(friend_number, file_number) if hash == new_avatar_hash: # avatar is the same self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL']) # ignore file - else: - pass + else: # get new avatar + path = ProfileHelper.get_path() + '/avatars/{}.png'.format(friend.tox_id) + rt = ReceiveTransfer(path, self._tox, friend_number) + self._file_transfers[(friend_number, file_number)] = rt + self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['RESUME']) def incoming_chunk(self, friend_number, file_number, position, data): - # TODO: better transfer filtering - transfer = filter(lambda x: x._friend_number == friend_number, self._file_transfers)[0] + transfer = self._file_transfers[(friend_number, file_number)] transfer.write_chunk(position, data) + if data is None: + del self._file_transfers[(friend_number, file_number)] + friend = self.get_friend_by_number(friend_number) + friend.load_avatar() def send_avatar(self, friend_number): pass