minor refactoring and todo's for file transfers

This commit is contained in:
ingvar1995 2018-05-16 14:47:14 +03:00
parent 2883ce5c4c
commit 7209dfae72
7 changed files with 38 additions and 38 deletions

View File

@ -75,7 +75,7 @@ class Contact(basecontact.BaseContact):
Get data to save in db Get data to save in db
:return: list of unsaved messages or [] :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 [] return messages[-self._unsaved_messages:] if self._unsaved_messages else []
def get_corr(self): def get_corr(self):
@ -86,11 +86,12 @@ class Contact(basecontact.BaseContact):
:param message: text or file transfer message :param message: text or file transfer message
""" """
self._corr.append(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 self._unsaved_messages += 1
def get_last_message_text(self): 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: if messages:
return messages[-1].text return messages[-1].text
else: else:
@ -122,7 +123,8 @@ class Contact(basecontact.BaseContact):
""" """
:return list of unsent messages for saving :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)) return list(map(lambda x: x.get_data(), messages))
def mark_as_sent(self): def mark_as_sent(self):
@ -157,7 +159,7 @@ class Contact(basecontact.BaseContact):
old = filter(save_message, self._corr[:-SAVE_MESSAGES]) old = filter(save_message, self._corr[:-SAVE_MESSAGES])
self._corr = list(old) + 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._unsaved_messages = min(self._unsaved_messages, len(list(text_messages)))
self._search_index = 0 self._search_index = 0
@ -175,7 +177,8 @@ class Contact(basecontact.BaseContact):
self._unsaved_messages = 0 self._unsaved_messages = 0
else: else:
self._corr = list(filter(lambda x: (x.get_type() == 2 and x.get_status() in ft.ACTIVE_FILE_TRANSFERS) 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._corr))
self._unsaved_messages = len(self.get_unsent_messages()) 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): for i in range(self._search_index - 1, -l - 1, -1):
if self._corr[i].get_type() > 1: if self._corr[i].get_type() > 1:
continue 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: if re.search(self._search_string, message, re.IGNORECASE) is not None:
self._search_index = i self._search_index = i
return i return i
@ -208,7 +211,7 @@ class Contact(basecontact.BaseContact):
for i in range(self._search_index + 1, 0): for i in range(self._search_index + 1, 0):
if self._corr[i].get_type() > 1: if self._corr[i].get_type() > 1:
continue 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: if re.search(self._search_string, message, re.IGNORECASE) is not None:
self._search_index = i self._search_index = i
return i return i

View File

@ -24,12 +24,14 @@ DO_NOT_SHOW_ACCEPT_BUTTON = (2, 3, 4, 6)
SHOW_PROGRESS_BAR = (0, 1, 4) SHOW_PROGRESS_BAR = (0, 1, 4)
ALLOWED_FILES = ('toxygen_inline.png', 'utox-inline.png', 'sticker.png')
def is_inline(file_name): 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): class StateSignal(QtCore.QObject):

View File

@ -214,20 +214,18 @@ class FileTransfersHandler:
st.set_state_changed_handler(item.update_transfer_state) st.set_state_changed_handler(item.update_transfer_state)
self._messages.scrollToBottom() 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 Send file to current active friend
:param path: file path :param path: file path
:param number: friend_number :param friend_number: friend_number
:param is_resend: is 'offline' message :param is_resend: is 'offline' message
:param file_id: file id of transfer :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) friend = self._get_friend_by_number(friend_number)
if friend.status is None and not is_resend: if friend.status is None and not is_resend:
m = UnsentFile(path, None, time.time()) m = UnsentFile(path, None, time.time())
friend.append_message(m) friend.append_message(m)
self.update()
return return
elif friend.status is None and is_resend: elif friend.status is None and is_resend:
print('Error in sending') print('Error in sending')
@ -235,18 +233,18 @@ class FileTransfersHandler:
st = SendTransfer(path, self._tox, friend_number, TOX_FILE_KIND['DATA'], file_id) st = SendTransfer(path, self._tox, friend_number, TOX_FILE_KIND['DATA'], file_id)
st.set_transfer_finished_handler(self.transfer_finished) st.set_transfer_finished_handler(self.transfer_finished)
self._file_transfers[(friend_number, st.get_file_number())] = st self._file_transfers[(friend_number, st.get_file_number())] = st
tm = TransferMessage(MESSAGE_AUTHOR['ME'], # tm = TransferMessage(MESSAGE_AUTHOR['ME'],
time.time(), # time.time(),
TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED'], # TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED'],
os.path.getsize(path), # os.path.getsize(path),
os.path.basename(path), # os.path.basename(path),
friend_number, # friend_number,
st.get_file_number()) # st.get_file_number())
if friend_number == self.get_active_number(): # if friend_number == self.get_active_number():
item = self.create_file_transfer_item(tm) # item = self.create_file_transfer_item(tm)
st.set_state_changed_handler(item.update_transfer_state) # st.set_state_changed_handler(item.update_transfer_state)
self._messages.scrollToBottom() # self._messages.scrollToBottom()
self._contacts[friend_number].append_message(tm) # self._contacts[friend_number].append_message(tm)
def incoming_chunk(self, friend_number, file_number, position, data): def incoming_chunk(self, friend_number, file_number, position, data):
""" """
@ -265,8 +263,6 @@ class FileTransfersHandler:
t = type(transfer) t = type(transfer)
if t is ReceiveAvatar: if t is ReceiveAvatar:
self._get_friend_by_number(friend_number).load_avatar() 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 elif t is ReceiveToBuffer or (t is SendFromBuffer and self._settings['allow_inline']): # inline image
print('inline') print('inline')
inline = InlineImage(transfer.get_data()) inline = InlineImage(transfer.get_data())
@ -284,7 +280,7 @@ class FileTransfersHandler:
self._messages.scrollToBottom() self._messages.scrollToBottom()
elif t is not SendAvatar: elif t is not SendAvatar:
self._get_friend_by_number(friend_number).update_transfer_data(file_number, 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 self._file_transfers[(friend_number, file_number)]
del transfer del transfer

View File

@ -15,7 +15,6 @@ class History:
def __del__(self): def __del__(self):
del self._db del self._db
def set_contacts_manager(self, contacts_manager): def set_contacts_manager(self, contacts_manager):
self._contacts_manager = contacts_manager self._contacts_manager = contacts_manager
@ -69,7 +68,7 @@ class History:
messages.reverse() messages.reverse()
messages = messages[self._messages.count():self._messages.count() + PAGE_SIZE] messages = messages[self._messages.count():self._messages.count() + PAGE_SIZE]
for message in messages: 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) self._create_message_item(message)
elif message.get_type() == MESSAGE_TYPE['FILE_TRANSFER']: # file transfer elif message.get_type() == MESSAGE_TYPE['FILE_TRANSFER']: # file transfer
if message.get_status() is None: if message.get_status() is None:

View File

@ -151,8 +151,8 @@ class TransferMessage(Message):
class UnsentFile(Message): class UnsentFile(Message):
def __init__(self, id, path, data, time): def __init__(self, path, data, time):
super().__init__(id, MESSAGE_TYPE['FILE_TRANSFER'], 0, time) super().__init__(MESSAGE_TYPE['FILE_TRANSFER'], 0, time)
self._data, self._path = data, path self._data, self._path = data, path
def get_status(self): def get_status(self):
@ -164,8 +164,8 @@ class InlineImage(Message):
Inline image Inline image
""" """
def __init__(self, id, data): def __init__(self, data):
super().__init__(id, MESSAGE_TYPE['INLINE'], None, None) super().__init__(MESSAGE_TYPE['INLINE'], None, None)
self._data = data self._data = data
def get_data(self): def get_data(self):

View File

@ -519,7 +519,7 @@ class MainWindow(QtWidgets.QMainWindow):
caption = util_ui.tr('Choose file') caption = util_ui.tr('Choose file')
name = util_ui.file_dialog(caption) name = util_ui.file_dialog(caption)
if name[0]: 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): def send_screenshot(self, hide=False):
self.menu.hide() self.menu.hide()

View File

@ -116,7 +116,7 @@ class ScreenShotWindow(RubberBandWindow):
buffer.open(QtCore.QIODevice.WriteOnly) buffer.open(QtCore.QIODevice.WriteOnly)
p.save(buffer, 'PNG') p.save(buffer, 'PNG')
friend = self._contacts_manager.get_curr_contact() 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() self.close()