From bde69bd417172ab761e1a184f14fb82e9d7b9b5d Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Fri, 18 May 2018 12:26:02 +0300 Subject: [PATCH] file transfers fixes - part 7 --- toxygen/app.py | 1 + toxygen/contacts/friend.py | 5 ++--- toxygen/file_transfers/file_transfers.py | 4 ++++ .../file_transfers/file_transfers_handler.py | 19 +++++++++++++------ toxygen/middleware/callbacks.py | 12 +++++++++--- toxygen/ui/items_factories.py | 2 +- toxygen/ui/messages_widgets.py | 4 ++-- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index 3a3476e..d20eea7 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -29,6 +29,7 @@ from messenger.messenger import Messenger from network.tox_dns import ToxDns from history.history import History from file_transfers.file_transfers_messages_service import FileTransfersMessagesService +import styles.style # TODO: dynamic loading class App: diff --git a/toxygen/contacts/friend.py b/toxygen/contacts/friend.py index 9c4e719..81830da 100644 --- a/toxygen/contacts/friend.py +++ b/toxygen/contacts/friend.py @@ -18,13 +18,12 @@ class Friend(contact.Contact): # File transfers support # ----------------------------------------------------------------------------------------------------------------- - def update_transfer_data(self, file_number, inline): # TODO: rewrite + def insert_inline(self, before_message_id, inline): """ Update status of active transfer and load inline if needed """ try: - tr = list(filter(lambda x: x.get_type() == MESSAGE_TYPE['FILE_TRANSFER'] and x.is_active(file_number), - self._corr))[0] + tr = list(filter(lambda x: x.message_id == before_message_id, self._corr))[0] i = self._corr.index(tr) if inline: # inline was loaded self._corr.insert(i, inline) diff --git a/toxygen/file_transfers/file_transfers.py b/toxygen/file_transfers/file_transfers.py index 710320c..166cc7f 100644 --- a/toxygen/file_transfers/file_transfers.py +++ b/toxygen/file_transfers/file_transfers.py @@ -198,6 +198,8 @@ class SendFromBuffer(FileTransfer): def get_data(self): return self._data + data = property(get_data) + def send_chunk(self, position, size): if self._creation_time is None: self._creation_time = time() @@ -289,6 +291,8 @@ class ReceiveToBuffer(FileTransfer): def get_data(self): return self._data + data = property(get_data) + def write_chunk(self, position, data): if self._creation_time is None: self._creation_time = time() diff --git a/toxygen/file_transfers/file_transfers_handler.py b/toxygen/file_transfers/file_transfers_handler.py index 4ee1282..f64c201 100644 --- a/toxygen/file_transfers/file_transfers_handler.py +++ b/toxygen/file_transfers/file_transfers_handler.py @@ -14,6 +14,8 @@ class FileTransfersHandler: # key = (friend number, file number), value - transfer instance self._paused_file_transfers = dict(settings['paused_file_transfers']) # key - file id, value: [path, friend number, is incoming, start position] + self._insert_inline_before = {} + # key = (friend number, file number), value - message id profile.avatar_changed_event.add_callback(self._send_avatar_to_contacts) @@ -124,6 +126,8 @@ class FileTransfersHandler: rt.set_state_changed_handler(message.transfer_updated) self._file_transfers[(friend_number, file_number)] = rt rt.send_control(TOX_FILE_CONTROL['RESUME']) + if inline: + self._insert_inline_before[(friend_number, file_number)] = message.message_id def send_screenshot(self, data, friend_number): """ @@ -147,7 +151,7 @@ class FileTransfersHandler: elif friend.status is None and is_resend: raise RuntimeError() st = SendFromBuffer(self._tox, friend.number, data, file_name) - self._send_file_add_handlers(st, friend, file_name) + self._send_file_add_set_handlers(st, friend, file_name, True) def send_file(self, path, friend_number, is_resend=False, file_id=None): """ @@ -167,7 +171,7 @@ class FileTransfersHandler: raise RuntimeError() st = SendTransfer(path, self._tox, friend_number, TOX_FILE_KIND['DATA'], file_id) file_name = os.path.basename(path) - self._send_file_add_handlers(st, friend, file_name) + self._send_file_add_set_handlers(st, friend, file_name) def incoming_chunk(self, friend_number, file_number, position, data): """ @@ -188,11 +192,12 @@ class FileTransfersHandler: self._get_friend_by_number(friend_number).load_avatar() elif t is ReceiveToBuffer or (t is SendFromBuffer and self._settings['allow_inline']): # inline image print('inline') - inline = InlineImage(transfer.get_data()) - index = self._get_friend_by_number(friend_number).update_transfer_data(file_number, inline) + inline = InlineImage(transfer.data) + message_id = self._insert_inline_before[(friend_number, file_number)] + del self._insert_inline_before[(friend_number, file_number)] + index = self._get_friend_by_number(friend_number).insert_inline(message_id, inline) self._file_transfers_message_service.add_inline_message(transfer, index) del self._file_transfers[(friend_number, file_number)] - del transfer def send_files(self, friend_number): friend = self._get_friend_by_number(friend_number) @@ -268,12 +273,14 @@ class FileTransfersHandler: def _get_all_friends(self): return self._contact_provider.get_all_friends() - def _send_file_add_handlers(self, st, friend, file_name): + def _send_file_add_set_handlers(self, st, friend, file_name, inline=False): st.set_transfer_finished_handler(self.transfer_finished) file_number = st.get_file_number() self._file_transfers[(friend.number, file_number)] = st tm = self._file_transfers_message_service.add_outgoing_transfer_message(friend, st.size, file_name, file_number) st.set_state_changed_handler(tm.transfer_updated) + if inline: + self._insert_inline_before[(friend.number, file_number)] = tm.message_id @staticmethod def _generate_valid_path(path, from_position): diff --git a/toxygen/middleware/callbacks.py b/toxygen/middleware/callbacks.py index 83dcc64..bd122d6 100644 --- a/toxygen/middleware/callbacks.py +++ b/toxygen/middleware/callbacks.py @@ -195,8 +195,11 @@ def file_recv_chunk(file_transfer_handler): Incoming chunk """ def wrapped(tox, friend_number, file_number, position, chunk, length, user_data): - execute(file_transfer_handler.incoming_chunk, friend_number, file_number, position, - chunk[:length] if length else None) + chunk = chunk[:length] if length else None + if length: + execute(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk) + else: + invoke_in_main_thread(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk) return wrapped @@ -206,7 +209,10 @@ def file_chunk_request(file_transfer_handler): Outgoing chunk """ def wrapped(tox, friend_number, file_number, position, size, user_data): - execute(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size) + if size: + execute(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size) + else: + invoke_in_main_thread(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size) return wrapped diff --git a/toxygen/ui/items_factories.py b/toxygen/ui/items_factories.py index 46e9df8..84bf9c3 100644 --- a/toxygen/ui/items_factories.py +++ b/toxygen/ui/items_factories.py @@ -47,7 +47,7 @@ class MessagesItemsFactory: def create_inline_item(self, data, append=True, position=0): elem = QtWidgets.QListWidgetItem() - item = InlineImageItem(data, self._messages.width(), elem) + item = InlineImageItem(data, self._messages.width(), elem, self._messages) elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height())) if append: self._messages.addItem(elem) diff --git a/toxygen/ui/messages_widgets.py b/toxygen/ui/messages_widgets.py index 818f766..ad337dd 100644 --- a/toxygen/ui/messages_widgets.py +++ b/toxygen/ui/messages_widgets.py @@ -406,9 +406,9 @@ class UnsentFileItem(FileTransferItem): class InlineImageItem(QtWidgets.QScrollArea): - def __init__(self, data, width, elem): + def __init__(self, data, width, elem, parent=None): - QtWidgets.QScrollArea.__init__(self) + QtWidgets.QScrollArea.__init__(self, parent) self.setFocusPolicy(QtCore.Qt.NoFocus) self._elem = elem self._image_label = QtWidgets.QLabel(self)