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
: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

View file

@ -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):

View file

@ -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

View file

@ -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:

View file

@ -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):

View file

@ -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()

View file

@ -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()