From e8a0a3f5bebb171f05bdb6cd0e60e60d8f4696d3 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Fri, 18 May 2018 12:54:00 +0300 Subject: [PATCH] file transfers fixes - part 8 (unsent files minor fixes) --- toxygen/contacts/friend.py | 13 +++++----- .../file_transfers/file_transfers_handler.py | 26 +++++++++---------- .../file_transfers_messages_service.py | 13 ++++++++++ toxygen/messenger/messages.py | 17 ++++++++++-- toxygen/ui/items_factories.py | 8 +++--- toxygen/ui/main_screen.py | 3 ++- toxygen/ui/messages_widgets.py | 4 +-- 7 files changed, 55 insertions(+), 29 deletions(-) diff --git a/toxygen/contacts/friend.py b/toxygen/contacts/friend.py index 81830da..63edd6a 100644 --- a/toxygen/contacts/friend.py +++ b/toxygen/contacts/friend.py @@ -32,15 +32,15 @@ class Friend(contact.Contact): pass def get_unsent_files(self): - messages = filter(lambda x: type(x) is UnsentFile, self._corr) - return messages + messages = filter(lambda x: type(x) is UnsentFileMessage, self._corr) + return list(messages) def clear_unsent_files(self): - self._corr = list(filter(lambda x: type(x) is not UnsentFile, self._corr)) + self._corr = list(filter(lambda x: type(x) is not UnsentFileMessage, self._corr)) - def remove_invalid_unsent_files(self): + def remove_invalid_unsent_files(self): # TODO: fix def is_valid(message): - if type(message) is not UnsentFile: + if type(message) is not UnsentFileMessage: return True if message.get_data()[1] is not None: return True @@ -49,7 +49,8 @@ class Friend(contact.Contact): self._corr = list(filter(is_valid, self._corr)) def delete_one_unsent_file(self, message_id): - self._corr = list(filter(lambda m: not (type(m) is UnsentFile and m.message_id == message_id), self._corr)) + self._corr = list(filter(lambda m: not (type(m) is UnsentFileMessage and m.message_id == message_id), + self._corr)) # ----------------------------------------------------------------------------------------------------------------- # History support diff --git a/toxygen/file_transfers/file_transfers_handler.py b/toxygen/file_transfers/file_transfers_handler.py index f64c201..c46ee58 100644 --- a/toxygen/file_transfers/file_transfers_handler.py +++ b/toxygen/file_transfers/file_transfers_handler.py @@ -145,8 +145,7 @@ class FileTransfersHandler: def send_inline(self, data, file_name, friend_number, is_resend=False): friend = self._get_friend_by_number(friend_number) if friend.status is None and not is_resend: - m = UnsentFile(file_name, data, time.time()) - friend.append_message(m) + self._file_transfers_message_service.add_unsent_file_message(friend, file_name, data) return elif friend.status is None and is_resend: raise RuntimeError() @@ -163,8 +162,7 @@ class FileTransfersHandler: """ friend = self._get_friend_by_number(friend_number) if friend.status is None and not is_resend: - m = UnsentFile(path, None, util.get_unix_time()) - friend.append_message(m) + self._file_transfers_message_service.add_unsent_file_message(friend, path, None) return elif friend.status is None and is_resend: print('Error in sending') @@ -192,7 +190,7 @@ 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.data) + inline = InlineImageMessage(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) @@ -203,20 +201,20 @@ class FileTransfersHandler: friend = self._get_friend_by_number(friend_number) friend.remove_invalid_unsent_files() files = friend.get_unsent_files() - try: # TODO: fix + try: for fl in files: - data = fl.get_data() - if data[1] is not None: - self.send_inline(data[1], data[0], friend_number, True) + data, path = fl.data, fl.path + if data is not None: + self.send_inline(data, path, friend_number, True) else: - self.send_file(data[0], friend_number, True) + self.send_file(path, friend_number, True) friend.clear_unsent_files() for key in list(self._paused_file_transfers.keys()): - data = self._paused_file_transfers[key] - if not os.path.exists(data[0]): + (path, ft_friend_number, is_incoming, start_position) = self._paused_file_transfers[key] + if not os.path.exists(path): del self._paused_file_transfers[key] - elif data[1] == friend_number and not data[2]: - self.send_file(data[0], friend_number, True, key) + elif ft_friend_number == friend_number and not is_incoming: + self.send_file(path, friend_number, True, key) del self._paused_file_transfers[key] except Exception as ex: print('Exception in file sending: ' + str(ex)) diff --git a/toxygen/file_transfers/file_transfers_messages_service.py b/toxygen/file_transfers/file_transfers_messages_service.py index 06b1e14..7f8b5f6 100644 --- a/toxygen/file_transfers/file_transfers_messages_service.py +++ b/toxygen/file_transfers/file_transfers_messages_service.py @@ -45,6 +45,16 @@ class FileTransfersMessagesService: if count + index + 1 >= 0: self._create_inline_item(transfer.data, count + index + 1) + def add_unsent_file_message(self, friend, file_path, data): + tm = UnsentFileMessage(file_path, data, util.get_unix_time()) + friend.append_message(tm) + + if self._is_active(friend.number): + self._create_unsent_file_item(tm) + self._messages.scrollToBottom() + + return tm + # ----------------------------------------------------------------------------------------------------------------- # Private methods # ----------------------------------------------------------------------------------------------------------------- @@ -60,3 +70,6 @@ class FileTransfersMessagesService: def _create_inline_item(self, data, position): return self._messages_items_factory.create_inline_item(data, False, position) + + def _create_unsent_file_item(self, tm): + return self._messages_items_factory.create_unsent_file_item(tm) diff --git a/toxygen/messenger/messages.py b/toxygen/messenger/messages.py index 9a56aba..69b5b39 100644 --- a/toxygen/messenger/messages.py +++ b/toxygen/messenger/messages.py @@ -173,17 +173,30 @@ class TransferMessage(Message): return FileTransferItem(self, *args) -class UnsentFile(Message): +class UnsentFileMessage(Message): def __init__(self, path, data, time): super().__init__(MESSAGE_TYPE['FILE_TRANSFER'], 0, time) self._data, self._path = data, path + def get_data(self): + return self._data + + data = property(get_data) + + def get_path(self): + return self._path + + path = property(get_path) + def get_status(self): return None + def _create_widget(self, *args): + return UnsentFileItem(self, *args) -class InlineImage(Message): + +class InlineImageMessage(Message): """ Inline image """ diff --git a/toxygen/ui/items_factories.py b/toxygen/ui/items_factories.py index 84bf9c3..6197066 100644 --- a/toxygen/ui/items_factories.py +++ b/toxygen/ui/items_factories.py @@ -57,8 +57,8 @@ class MessagesItemsFactory: return item - def create_unsent_file_item(self, tm, append=True): - item = UnsentFileItem(self._file_transfers_handler, self._settings, tm, self._messages.width()) + def create_unsent_file_item(self, message, append=True): + item = message.get_widget(self._file_transfers_handler, self._settings, self._messages.width(), self._messages) elem = QtWidgets.QListWidgetItem() elem.setSizeHint(QtCore.QSize(self._messages.width() - 30, 34)) if append: @@ -69,8 +69,8 @@ class MessagesItemsFactory: return item - def create_file_transfer_item(self, tm, append=True): - item = tm.get_widget(self._file_transfers_handler, self._settings, self._messages.width(), self._messages) + def create_file_transfer_item(self, message, append=True): + item = message.get_widget(self._file_transfers_handler, self._settings, self._messages.width(), self._messages) elem = QtWidgets.QListWidgetItem() elem.setSizeHint(QtCore.QSize(self._messages.width() - 30, 34)) if append: diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index 67b09a7..c87a675 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -308,7 +308,8 @@ class MainWindow(QtWidgets.QMainWindow): def load(pos): if not pos: - self._history_loader.load_history() + friend = self._contacts_manager.get_curr_friend() + self._history_loader.load_history(friend) self.messages.verticalScrollBar().setValue(1) self.messages.verticalScrollBar().valueChanged.connect(load) self.messages.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel) diff --git a/toxygen/ui/messages_widgets.py b/toxygen/ui/messages_widgets.py index ad337dd..9b5f1cb 100644 --- a/toxygen/ui/messages_widgets.py +++ b/toxygen/ui/messages_widgets.py @@ -390,8 +390,8 @@ class FileTransferItem(QtWidgets.QListWidget): class UnsentFileItem(FileTransferItem): - def __init__(self, file_transfer_handler, settings, transfer_message, width, parent=None): - super().__init__(file_transfer_handler, settings, transfer_message, width, parent) + def __init__(self, transfer_message, file_transfer_handler, settings, width, parent=None): + super().__init__(transfer_message, file_transfer_handler, settings, width, parent) self._time = time self.pb.setVisible(False) movie = QtGui.QMovie(util.join_path(util.get_images_directory(), 'spinner.gif'))