From 7209dfae72e2769d0fee2e16930d2de7dff208c1 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 16 May 2018 14:47:14 +0300 Subject: [PATCH] minor refactoring and todo's for file transfers --- toxygen/contacts/contact.py | 19 ++++++----- toxygen/file_transfers/file_transfers.py | 8 +++-- .../file_transfers/file_transfers_handler.py | 34 ++++++++----------- toxygen/history/history.py | 3 +- toxygen/messenger/messages.py | 8 ++--- toxygen/ui/main_screen.py | 2 +- toxygen/ui/main_screen_widgets.py | 2 +- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/toxygen/contacts/contact.py b/toxygen/contacts/contact.py index 0718e5e..8c1fb06 100644 --- a/toxygen/contacts/contact.py +++ b/toxygen/contacts/contact.py @@ -75,7 +75,7 @@ class Contact(basecontact.BaseContact): Get data to save in db :return: list of unsaved messages or [] """ - messages = list(filter(lambda x: x.get_type() <= 1, self._corr)) + messages = list(filter(lambda x: x.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']), self._corr)) return messages[-self._unsaved_messages:] if self._unsaved_messages else [] def get_corr(self): @@ -86,11 +86,12 @@ class Contact(basecontact.BaseContact): :param message: text or file transfer message """ self._corr.append(message) - if message.get_type() <= 1: + if message.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']): self._unsaved_messages += 1 def get_last_message_text(self): - messages = list(filter(lambda x: x.get_type() <= 1 and x.get_owner() != MESSAGE_AUTHOR['FRIEND'], self._corr)) + messages = list(filter(lambda x: x.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']) + and x.get_owner() != MESSAGE_AUTHOR['FRIEND'], self._corr)) if messages: return messages[-1].text else: @@ -122,7 +123,8 @@ class Contact(basecontact.BaseContact): """ :return list of unsent messages for saving """ - messages = filter(lambda x: x.get_type() <= 1 and x.get_owner() == MESSAGE_AUTHOR['NOT_SENT'], self._corr) + messages = filter(lambda x: x.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']) + and x.get_owner() == MESSAGE_AUTHOR['NOT_SENT'], self._corr) return list(map(lambda x: x.get_data(), messages)) def mark_as_sent(self): @@ -157,7 +159,7 @@ class Contact(basecontact.BaseContact): old = filter(save_message, self._corr[:-SAVE_MESSAGES]) self._corr = list(old) + self._corr[-SAVE_MESSAGES:] - text_messages = filter(lambda x: x.get_type() <= 1, self._corr) + text_messages = filter(lambda x: x.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']), self._corr) self._unsaved_messages = min(self._unsaved_messages, len(list(text_messages))) self._search_index = 0 @@ -175,7 +177,8 @@ class Contact(basecontact.BaseContact): self._unsaved_messages = 0 else: self._corr = list(filter(lambda x: (x.get_type() == 2 and x.get_status() in ft.ACTIVE_FILE_TRANSFERS) - or (x.get_type() <= 1 and x.get_owner() == MESSAGE_AUTHOR['NOT_SENT']), + or (x.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']) + and x.get_owner() == MESSAGE_AUTHOR['NOT_SENT']), self._corr)) self._unsaved_messages = len(self.get_unsent_messages()) @@ -193,7 +196,7 @@ class Contact(basecontact.BaseContact): for i in range(self._search_index - 1, -l - 1, -1): if self._corr[i].get_type() > 1: continue - message = self._corr[i].get_data()[0] + message = self._corr[i].text if re.search(self._search_string, message, re.IGNORECASE) is not None: self._search_index = i return i @@ -208,7 +211,7 @@ class Contact(basecontact.BaseContact): for i in range(self._search_index + 1, 0): if self._corr[i].get_type() > 1: continue - message = self._corr[i].get_data()[0] + message = self._corr[i].text if re.search(self._search_string, message, re.IGNORECASE) is not None: self._search_index = i return i diff --git a/toxygen/file_transfers/file_transfers.py b/toxygen/file_transfers/file_transfers.py index 596642e..4bbcc18 100644 --- a/toxygen/file_transfers/file_transfers.py +++ b/toxygen/file_transfers/file_transfers.py @@ -24,12 +24,14 @@ DO_NOT_SHOW_ACCEPT_BUTTON = (2, 3, 4, 6) SHOW_PROGRESS_BAR = (0, 1, 4) -ALLOWED_FILES = ('toxygen_inline.png', 'utox-inline.png', 'sticker.png') - def is_inline(file_name): - return file_name in ALLOWED_FILES or file_name.startswith('qTox_Screenshot_') + allowed_inlines = ('toxygen_inline.png', 'utox-inline.png', 'sticker.png') + return file_name in allowed_inlines or file_name.startswith('qTox_Screenshot_') + + +# TODO: use events from common.event.py class StateSignal(QtCore.QObject): diff --git a/toxygen/file_transfers/file_transfers_handler.py b/toxygen/file_transfers/file_transfers_handler.py index 8da0811..42a69fa 100644 --- a/toxygen/file_transfers/file_transfers_handler.py +++ b/toxygen/file_transfers/file_transfers_handler.py @@ -214,20 +214,18 @@ class FileTransfersHandler: st.set_state_changed_handler(item.update_transfer_state) self._messages.scrollToBottom() - def send_file(self, path, number=None, is_resend=False, file_id=None): + def send_file(self, path, friend_number, is_resend=False, file_id=None): """ Send file to current active friend :param path: file path - :param number: friend_number + :param friend_number: friend_number :param is_resend: is 'offline' message :param file_id: file id of transfer """ - friend_number = self.get_active_number() if number is None else number friend = self._get_friend_by_number(friend_number) if friend.status is None and not is_resend: m = UnsentFile(path, None, time.time()) friend.append_message(m) - self.update() return elif friend.status is None and is_resend: print('Error in sending') @@ -235,18 +233,18 @@ class FileTransfersHandler: st = SendTransfer(path, self._tox, friend_number, TOX_FILE_KIND['DATA'], file_id) st.set_transfer_finished_handler(self.transfer_finished) self._file_transfers[(friend_number, st.get_file_number())] = st - tm = TransferMessage(MESSAGE_AUTHOR['ME'], - time.time(), - TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED'], - os.path.getsize(path), - os.path.basename(path), - friend_number, - st.get_file_number()) - if friend_number == self.get_active_number(): - item = self.create_file_transfer_item(tm) - st.set_state_changed_handler(item.update_transfer_state) - self._messages.scrollToBottom() - self._contacts[friend_number].append_message(tm) + # tm = TransferMessage(MESSAGE_AUTHOR['ME'], + # time.time(), + # TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED'], + # os.path.getsize(path), + # os.path.basename(path), + # friend_number, + # st.get_file_number()) + # if friend_number == self.get_active_number(): + # item = self.create_file_transfer_item(tm) + # st.set_state_changed_handler(item.update_transfer_state) + # self._messages.scrollToBottom() + # self._contacts[friend_number].append_message(tm) def incoming_chunk(self, friend_number, file_number, position, data): """ @@ -265,8 +263,6 @@ class FileTransfersHandler: t = type(transfer) if t is ReceiveAvatar: self._get_friend_by_number(friend_number).load_avatar() - if friend_number == self.get_active_number() and self.is_active_a_friend(): - self.set_active(None) elif t is ReceiveToBuffer or (t is SendFromBuffer and self._settings['allow_inline']): # inline image print('inline') inline = InlineImage(transfer.get_data()) @@ -284,7 +280,7 @@ class FileTransfersHandler: self._messages.scrollToBottom() elif t is not SendAvatar: self._get_friend_by_number(friend_number).update_transfer_data(file_number, - TOX_FILE_TRANSFER_STATE['FINISHED']) + TOX_FILE_TRANSFER_STATE['FINISHED']) del self._file_transfers[(friend_number, file_number)] del transfer diff --git a/toxygen/history/history.py b/toxygen/history/history.py index 21bad8c..e6f5e44 100644 --- a/toxygen/history/history.py +++ b/toxygen/history/history.py @@ -15,7 +15,6 @@ class History: def __del__(self): del self._db - def set_contacts_manager(self, contacts_manager): self._contacts_manager = contacts_manager @@ -69,7 +68,7 @@ class History: messages.reverse() messages = messages[self._messages.count():self._messages.count() + PAGE_SIZE] for message in messages: - if message.get_type() <= 1: # text message + if message.get_type() in (MESSAGE_TYPE['NORMAL'], MESSAGE_TYPE['ACTION']): # text message self._create_message_item(message) elif message.get_type() == MESSAGE_TYPE['FILE_TRANSFER']: # file transfer if message.get_status() is None: diff --git a/toxygen/messenger/messages.py b/toxygen/messenger/messages.py index 80376f4..42a0fd1 100644 --- a/toxygen/messenger/messages.py +++ b/toxygen/messenger/messages.py @@ -151,8 +151,8 @@ class TransferMessage(Message): class UnsentFile(Message): - def __init__(self, id, path, data, time): - super().__init__(id, MESSAGE_TYPE['FILE_TRANSFER'], 0, time) + def __init__(self, path, data, time): + super().__init__(MESSAGE_TYPE['FILE_TRANSFER'], 0, time) self._data, self._path = data, path def get_status(self): @@ -164,8 +164,8 @@ class InlineImage(Message): Inline image """ - def __init__(self, id, data): - super().__init__(id, MESSAGE_TYPE['INLINE'], None, None) + def __init__(self, data): + super().__init__(MESSAGE_TYPE['INLINE'], None, None) self._data = data def get_data(self): diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index cce78a4..3d9fd0f 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -519,7 +519,7 @@ class MainWindow(QtWidgets.QMainWindow): caption = util_ui.tr('Choose file') name = util_ui.file_dialog(caption) if name[0]: - self._contacts_manager.send_file(name[0]) + self._contacts_manager.send_file(name[0], self._contacts_manager.get_contact().number) def send_screenshot(self, hide=False): self.menu.hide() diff --git a/toxygen/ui/main_screen_widgets.py b/toxygen/ui/main_screen_widgets.py index ecf329a..dfd5ea7 100644 --- a/toxygen/ui/main_screen_widgets.py +++ b/toxygen/ui/main_screen_widgets.py @@ -116,7 +116,7 @@ class ScreenShotWindow(RubberBandWindow): buffer.open(QtCore.QIODevice.WriteOnly) p.save(buffer, 'PNG') friend = self._contacts_manager.get_curr_contact() - self._file_transfer_handler.send_screenshot(bytes(byte_array.data(), friend.number)) + self._file_transfer_handler.send_screenshot(bytes(byte_array.data()), friend.number) self.close()